| 1 | /* | ||
| 2 | * libpdfmake — growable byte buffer. | ||
| 3 | * | ||
| 4 | * A simple dynamic byte buffer for building output. Grows geometrically | ||
| 5 | * on demand. Used by the object serializer to accumulate PDF syntax. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef PDFMAKE_BUF_H | ||
| 9 | #define PDFMAKE_BUF_H | ||
| 10 | |||
| 11 | #include "pdfmake_types.h" | ||
| 12 | #include <stdarg.h> | ||
| 13 | |||
| 14 | #ifdef __cplusplus | ||
| 15 | extern "C" { | ||
| 16 | #endif | ||
| 17 | |||
| 18 | /* Default initial capacity. */ | ||
| 19 | #ifndef PDFMAKE_BUF_INIT_CAP | ||
| 20 | #define PDFMAKE_BUF_INIT_CAP 4096 | ||
| 21 | #endif | ||
| 22 | |||
| 23 | /* Growable byte buffer. */ | ||
| 24 | typedef struct pdfmake_buf { | ||
| 25 | uint8_t *data; /* heap-allocated bytes */ | ||
| 26 | size_t len; /* current byte count */ | ||
| 27 | size_t cap; /* allocated capacity */ | ||
| 28 | } pdfmake_buf_t; | ||
| 29 | |||
| 30 | /*---------------------------------------------------------------------------- | ||
| 31 | * Lifecycle | ||
| 32 | *--------------------------------------------------------------------------*/ | ||
| 33 | |||
| 34 | /* Initialize a buffer with default capacity. Returns PDFMAKE_OK or PDFMAKE_ENOMEM. */ | ||
| 35 | pdfmake_err_t pdfmake_buf_init(pdfmake_buf_t *buf); | ||
| 36 | |||
| 37 | /* Initialize with a specific initial capacity. */ | ||
| 38 | pdfmake_err_t pdfmake_buf_init_cap(pdfmake_buf_t *buf, size_t cap); | ||
| 39 | |||
| 40 | /* Free buffer memory and reset to empty. Safe to call on zeroed buf. */ | ||
| 41 | void pdfmake_buf_free(pdfmake_buf_t *buf); | ||
| 42 | |||
| 43 | /* Reset buffer length to 0, keeping allocated memory. */ | ||
| 44 | void pdfmake_buf_clear(pdfmake_buf_t *buf); | ||
| 45 | |||
| 46 | /*---------------------------------------------------------------------------- | ||
| 47 | * Writing | ||
| 48 | *--------------------------------------------------------------------------*/ | ||
| 49 | |||
| 50 | /* Ensure at least `extra` more bytes can be written without realloc. | ||
| 51 | * Returns PDFMAKE_OK or PDFMAKE_ENOMEM. */ | ||
| 52 | pdfmake_err_t pdfmake_buf_reserve(pdfmake_buf_t *buf, size_t extra); | ||
| 53 | |||
| 54 | /* Append `len` bytes from `data`. Returns PDFMAKE_OK or PDFMAKE_ENOMEM. */ | ||
| 55 | pdfmake_err_t pdfmake_buf_append(pdfmake_buf_t *buf, const void *data, size_t len); | ||
| 56 | |||
| 57 | /* Append a null-terminated string (not including the null). */ | ||
| 58 | pdfmake_err_t pdfmake_buf_append_cstr(pdfmake_buf_t *buf, const char *s); | ||
| 59 | |||
| 60 | /* Append a single byte. */ | ||
| 61 | pdfmake_err_t pdfmake_buf_append_byte(pdfmake_buf_t *buf, uint8_t byte); | ||
| 62 | |||
| 63 | /* Append formatted output (printf-style). Returns PDFMAKE_OK or PDFMAKE_ENOMEM. | ||
| 64 | * Note: Uses vsnprintf internally; locale-dependent for %f/%g. Use the | ||
| 65 | * dedicated number formatters for locale-independent output. */ | ||
| 66 | pdfmake_err_t pdfmake_buf_appendf(pdfmake_buf_t *buf, const char *fmt, ...) | ||
| 67 | __attribute__((format(printf, 2, 3))); | ||
| 68 | |||
| 69 | /* Append formatted output (va_list version). */ | ||
| 70 | pdfmake_err_t pdfmake_buf_vappendf(pdfmake_buf_t *buf, const char *fmt, va_list ap); | ||
| 71 | |||
| 72 | /*---------------------------------------------------------------------------- | ||
| 73 | * Output | ||
| 74 | *--------------------------------------------------------------------------*/ | ||
| 75 | |||
| 76 | /* Transfer ownership of the buffer's data to the caller. Returns the data | ||
| 77 | * pointer, sets *len_out to length, and resets buf to empty. Caller must | ||
| 78 | * free() the returned pointer. Returns NULL if buffer is empty. */ | ||
| 79 | uint8_t *pdfmake_buf_take(pdfmake_buf_t *buf, size_t *len_out); | ||
| 80 | |||
| 81 | /* Get a pointer to the buffer's data (not null-terminated). Valid until | ||
| 82 | * next append or free. */ | ||
| 83 | 5 | static PDFMAKE_INLINE const uint8_t *pdfmake_buf_data(const pdfmake_buf_t *buf) { | |
| 84 | 5 | return buf->data; | |
| 85 | } | ||
| 86 | |||
| 87 | /* Get current buffer length. */ | ||
| 88 | 45 | static PDFMAKE_INLINE size_t pdfmake_buf_len(const pdfmake_buf_t *buf) { | |
| 89 | 45 | return buf->len; | |
| 90 | } | ||
| 91 | |||
| 92 | #ifdef __cplusplus | ||
| 93 | } | ||
| 94 | #endif | ||
| 95 | |||
| 96 | #endif /* PDFMAKE_BUF_H */ |