| 1 |
|
| /* |
| 2 |
|
| * libpdfmake — growable byte buffer implementation. |
| 3 |
|
| */ |
| 4 |
|
| |
| 5 |
|
| #include "pdfmake_buf.h" |
| 6 |
|
| #include <stdlib.h> |
| 7 |
|
| #include <string.h> |
| 8 |
|
| #include <stdio.h> |
| 9 |
|
| #include <stdarg.h> |
| 10 |
|
| |
| 11 |
|
| /*---------------------------------------------------------------------------- |
| 12 |
|
| * Internal helpers |
| 13 |
|
| *--------------------------------------------------------------------------*/ |
| 14 |
|
| |
| 15 |
|
| /* Grow buffer to at least `needed` total capacity. */ |
| 16 |
5247080 |
| static pdfmake_err_t buf_grow(pdfmake_buf_t *buf, size_t needed) { |
| 17 |
|
| size_t new_cap; |
| 18 |
|
| uint8_t *new_data; |
| 19 |
|
| |
| 20 |
5247080 |
▶ | if (needed <= buf->cap) return PDFMAKE_OK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (needed <= buf->cap) return PDFMAKE_OK; |
5246413 |
667 |
5247080 |
|
| 21 |
|
| |
| 22 |
|
| /* Geometric growth: double, but at least `needed`. */ |
| 23 |
667 |
| new_cap = buf->cap * 2; |
| 24 |
667 |
▶ | if (new_cap < needed) new_cap = needed; |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_cap < needed) new_cap = needed; |
112 |
555 |
667 |
|
| 25 |
667 |
▶ | if (new_cap < PDFMAKE_BUF_INIT_CAP) new_cap = PDFMAKE_BUF_INIT_CAP; |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_cap < PDFMAKE_BUF_INIT_CAP) new_cap = PDFMAKE_BUF_INIT_CAP; |
0 |
667 |
667 |
|
| 26 |
|
| |
| 27 |
667 |
| new_data = realloc(buf->data, new_cap); |
| 28 |
667 |
▶ | if (!new_data) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!new_data) return PDFMAKE_ENOMEM; |
0 |
667 |
667 |
|
| 29 |
|
| |
| 30 |
667 |
| buf->data = new_data; |
| 31 |
667 |
| buf->cap = new_cap; |
| 32 |
667 |
| return PDFMAKE_OK; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
|
| /*---------------------------------------------------------------------------- |
| 36 |
|
| * Lifecycle |
| 37 |
|
| *--------------------------------------------------------------------------*/ |
| 38 |
|
| |
| 39 |
4790 |
| pdfmake_err_t pdfmake_buf_init(pdfmake_buf_t *buf) { |
| 40 |
4790 |
| return pdfmake_buf_init_cap(buf, PDFMAKE_BUF_INIT_CAP); |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
4790 |
| pdfmake_err_t pdfmake_buf_init_cap(pdfmake_buf_t *buf, size_t cap) { |
| 44 |
4790 |
▶ | if (!buf) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) return PDFMAKE_EINVAL; |
0 |
4790 |
4790 |
|
| 45 |
|
| |
| 46 |
4790 |
| buf->data = NULL; |
| 47 |
4790 |
| buf->len = 0; |
| 48 |
4790 |
| buf->cap = 0; |
| 49 |
|
| |
| 50 |
4790 |
▶ | if (cap > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cap > 0) { |
4790 |
0 |
4790 |
|
| 51 |
4790 |
| buf->data = malloc(cap); |
| 52 |
4790 |
▶ | if (!buf->data) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf->data) return PDFMAKE_ENOMEM; |
0 |
4790 |
4790 |
|
| 53 |
4790 |
| buf->cap = cap; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
4790 |
| return PDFMAKE_OK; |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
4775 |
| void pdfmake_buf_free(pdfmake_buf_t *buf) { |
| 60 |
4775 |
▶ | if (!buf) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) return; |
0 |
4775 |
4775 |
|
| 61 |
4775 |
| free(buf->data); |
| 62 |
4775 |
| buf->data = NULL; |
| 63 |
4775 |
| buf->len = 0; |
| 64 |
4775 |
| buf->cap = 0; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
53 |
| void pdfmake_buf_clear(pdfmake_buf_t *buf) { |
| 68 |
53 |
▶ | if (buf) buf->len = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf) buf->len = 0; |
53 |
0 |
53 |
|
| 69 |
53 |
| } |
| 70 |
|
| |
| 71 |
|
| /*---------------------------------------------------------------------------- |
| 72 |
|
| * Writing |
| 73 |
|
| *--------------------------------------------------------------------------*/ |
| 74 |
|
| |
| 75 |
0 |
| pdfmake_err_t pdfmake_buf_reserve(pdfmake_buf_t *buf, size_t extra) { |
| 76 |
0 |
▶ | if (!buf) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
|
| 77 |
0 |
| return buf_grow(buf, buf->len + extra); |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
5247084 |
| pdfmake_err_t pdfmake_buf_append(pdfmake_buf_t *buf, const void *data, size_t len) { |
| 81 |
|
| pdfmake_err_t err; |
| 82 |
|
| |
| 83 |
5247084 |
▶ | if (!buf) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) return PDFMAKE_EINVAL; |
0 |
5247084 |
5247084 |
|
| 84 |
5247084 |
▶ | if (len == 0) return PDFMAKE_OK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) return PDFMAKE_OK; |
4 |
5247080 |
5247084 |
|
| 85 |
5247080 |
▶ | if (!data) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!data) return PDFMAKE_EINVAL; |
0 |
5247080 |
5247080 |
|
| 86 |
|
| |
| 87 |
5247080 |
| err = buf_grow(buf, buf->len + len); |
| 88 |
5247080 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
5247080 |
5247080 |
|
| 89 |
|
| |
| 90 |
5247080 |
| memcpy(buf->data + buf->len, data, len); |
| 91 |
5247080 |
| buf->len += len; |
| 92 |
5247080 |
| return PDFMAKE_OK; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
80579 |
| pdfmake_err_t pdfmake_buf_append_cstr(pdfmake_buf_t *buf, const char *s) { |
| 96 |
80579 |
▶ | if (!s) return PDFMAKE_OK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return PDFMAKE_OK; |
0 |
80579 |
80579 |
|
| 97 |
80579 |
| return pdfmake_buf_append(buf, s, strlen(s)); |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
5055786 |
| pdfmake_err_t pdfmake_buf_append_byte(pdfmake_buf_t *buf, uint8_t byte) { |
| 101 |
5055786 |
| return pdfmake_buf_append(buf, &byte, 1); |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
27227 |
| pdfmake_err_t pdfmake_buf_vappendf(pdfmake_buf_t *buf, const char *fmt, va_list ap) { |
| 105 |
|
| va_list ap_copy; |
| 106 |
|
| size_t avail; |
| 107 |
|
| int needed; |
| 108 |
|
| pdfmake_err_t err; |
| 109 |
|
| |
| 110 |
27227 |
▶ | if (!buf || !fmt) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!buf || !fmt) return PDFMAKE_EINVAL; |
27227 |
0 |
27227 |
| if (!buf || !fmt) return PDFMAKE_EINVAL; |
0 |
27227 |
27227 |
|
| 111 |
|
| |
| 112 |
|
| /* First, try to format directly into remaining space. */ |
| 113 |
27227 |
| PDFMAKE_VA_COPY(ap_copy, ap); |
| 114 |
|
| |
| 115 |
27227 |
| avail = buf->cap - buf->len; |
| 116 |
27227 |
| needed = vsnprintf((char *)(buf->data + buf->len), avail, fmt, ap_copy); |
| 117 |
27227 |
| va_end(ap_copy); |
| 118 |
|
| |
| 119 |
27227 |
▶ | if (needed < 0) return PDFMAKE_EINVAL; /* Encoding error. */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (needed < 0) return PDFMAKE_EINVAL; /* Encoding error. */ |
0 |
27227 |
27227 |
|
| 120 |
|
| |
| 121 |
27227 |
▶ | if ((size_t)needed < avail) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)needed < avail) { |
27227 |
0 |
27227 |
|
| 122 |
|
| /* Fit in existing space. */ |
| 123 |
27227 |
| buf->len += (size_t)needed; |
| 124 |
27227 |
| return PDFMAKE_OK; |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
|
| /* Need more space. Grow and retry. */ |
| 128 |
0 |
| err = buf_grow(buf, buf->len + (size_t)needed + 1); |
| 129 |
0 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
0 |
0 |
|
| 130 |
|
| |
| 131 |
0 |
| avail = buf->cap - buf->len; |
| 132 |
0 |
| needed = vsnprintf((char *)(buf->data + buf->len), avail, fmt, ap); |
| 133 |
0 |
▶ | if (needed < 0) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (needed < 0) return PDFMAKE_EINVAL; |
0 |
0 |
0 |
|
| 134 |
|
| |
| 135 |
0 |
| buf->len += (size_t)needed; |
| 136 |
0 |
| return PDFMAKE_OK; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
27227 |
| pdfmake_err_t pdfmake_buf_appendf(pdfmake_buf_t *buf, const char *fmt, ...) { |
| 140 |
|
| va_list ap; |
| 141 |
|
| pdfmake_err_t err; |
| 142 |
|
| |
| 143 |
27227 |
| va_start(ap, fmt); |
| 144 |
27227 |
| err = pdfmake_buf_vappendf(buf, fmt, ap); |
| 145 |
27227 |
| va_end(ap); |
| 146 |
27227 |
| return err; |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
|
| /*---------------------------------------------------------------------------- |
| 150 |
|
| * Output |
| 151 |
|
| *--------------------------------------------------------------------------*/ |
| 152 |
|
| |
| 153 |
0 |
| uint8_t *pdfmake_buf_take(pdfmake_buf_t *buf, size_t *len_out) { |
| 154 |
|
| uint8_t *data; |
| 155 |
|
| size_t len; |
| 156 |
|
| |
| 157 |
0 |
▶ | if (!buf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) { |
0 |
0 |
0 |
|
| 158 |
0 |
▶ | if (len_out) *len_out = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len_out) *len_out = 0; |
0 |
0 |
0 |
|
| 159 |
0 |
| return NULL; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
0 |
| data = buf->data; |
| 163 |
0 |
| len = buf->len; |
| 164 |
|
| |
| 165 |
0 |
| buf->data = NULL; |
| 166 |
0 |
| buf->len = 0; |
| 167 |
0 |
| buf->cap = 0; |
| 168 |
|
| |
| 169 |
0 |
▶ | if (len_out) *len_out = len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len_out) *len_out = len; |
0 |
0 |
0 |
|
| 170 |
0 |
| return data; |
| 171 |
|
| } |