Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/support/buffer/buffer.c

raw
1 #include "caryll/buffer.h"
2 #include "support/otfcc-alloc.h"
3
4 caryll_Buffer *bufnew() {
5 caryll_Buffer *buf;
6 NEW(buf);
7 buf->size = buf->free = 0;
8 return buf;
9 }
10 void buffree(caryll_Buffer *buf) {
11 if (!buf) return;
12 if (buf->data) FREE(buf->data);
13 FREE(buf);
14 }
15 size_t buflen(caryll_Buffer *buf) {
16 return buf->size;
17 }
18 size_t bufpos(caryll_Buffer *buf) {
19 return buf->cursor;
20 }
21 void bufseek(caryll_Buffer *buf, size_t pos) {
22 buf->cursor = pos;
23 }
24 void bufclear(caryll_Buffer *buf) {
25 buf->cursor = 0;
26 buf->free = buf->size + buf->free;
27 buf->size = 0;
28 }
29
30 static void bufbeforewrite(caryll_Buffer *buf, size_t towrite) {
31 size_t currentSize = buf->size;
32 size_t allocated = buf->size + buf->free;
33 size_t required = buf->cursor + towrite;
34 if (required < currentSize) {
35 // Completely overlap.
36 return;
37 } else if (required <= allocated) {
38 // Within range without reallocation
39 buf->size = required;
40 buf->free = allocated - buf->size;
41 } else {
42 // Needs realloc
43 buf->size = required;
44 buf->free = required; // Double growth
45 if (buf->free > 0x1000000) { buf->free = 0x1000000; }
46 RESIZE(buf->data, buf->size + buf->free);
47 }
48 }
49 void bufwrite8(caryll_Buffer *buf, uint8_t byte) {
50 bufbeforewrite(buf, 1);
51 buf->data[buf->cursor++] = byte;
52 }
53 void bufwrite16l(caryll_Buffer *buf, uint16_t x) {
54 bufbeforewrite(buf, 2);
55 buf->data[buf->cursor++] = x & 0xFF;
56 buf->data[buf->cursor++] = (x >> 8) & 0xFF;
57 }
58 void bufwrite16b(caryll_Buffer *buf, uint16_t x) {
59 bufbeforewrite(buf, 2);
60 buf->data[buf->cursor++] = (x >> 8) & 0xFF;
61 buf->data[buf->cursor++] = x & 0xFF;
62 }
63 void bufwrite24l(caryll_Buffer *buf, uint32_t x) {
64 bufbeforewrite(buf, 3);
65 buf->data[buf->cursor++] = x & 0xFF;
66 buf->data[buf->cursor++] = (x >> 8) & 0xFF;
67 buf->data[buf->cursor++] = (x >> 16) & 0xFF;
68 }
69 void bufwrite24b(caryll_Buffer *buf, uint32_t x) {
70 bufbeforewrite(buf, 3);
71 buf->data[buf->cursor++] = (x >> 16) & 0xFF;
72 buf->data[buf->cursor++] = (x >> 8) & 0xFF;
73 buf->data[buf->cursor++] = x & 0xFF;
74 }
75 void bufwrite32l(caryll_Buffer *buf, uint32_t x) {
76 bufbeforewrite(buf, 4);
77 buf->data[buf->cursor++] = x & 0xFF;
78 buf->data[buf->cursor++] = (x >> 8) & 0xFF;
79 buf->data[buf->cursor++] = (x >> 16) & 0xFF;
80 buf->data[buf->cursor++] = (x >> 24) & 0xFF;
81 }
82 void bufwrite32b(caryll_Buffer *buf, uint32_t x) {
83 bufbeforewrite(buf, 4);
84 buf->data[buf->cursor++] = (x >> 24) & 0xFF;
85 buf->data[buf->cursor++] = (x >> 16) & 0xFF;
86 buf->data[buf->cursor++] = (x >> 8) & 0xFF;
87 buf->data[buf->cursor++] = x & 0xFF;
88 }
89 void bufwrite64l(caryll_Buffer *buf, uint64_t x) {
90 bufbeforewrite(buf, 8);
91 buf->data[buf->cursor++] = x & 0xFF;
92 buf->data[buf->cursor++] = (x >> 8) & 0xFF;
93 buf->data[buf->cursor++] = (x >> 16) & 0xFF;
94 buf->data[buf->cursor++] = (x >> 24) & 0xFF;
95 buf->data[buf->cursor++] = (x >> 32) & 0xFF;
96 buf->data[buf->cursor++] = (x >> 40) & 0xFF;
97 buf->data[buf->cursor++] = (x >> 48) & 0xFF;
98 buf->data[buf->cursor++] = (x >> 56) & 0xFF;
99 }
100 void bufwrite64b(caryll_Buffer *buf, uint64_t x) {
101 bufbeforewrite(buf, 8);
102 buf->data[buf->cursor++] = (x >> 56) & 0xFF;
103 buf->data[buf->cursor++] = (x >> 48) & 0xFF;
104 buf->data[buf->cursor++] = (x >> 40) & 0xFF;
105 buf->data[buf->cursor++] = (x >> 32) & 0xFF;
106 buf->data[buf->cursor++] = (x >> 24) & 0xFF;
107 buf->data[buf->cursor++] = (x >> 16) & 0xFF;
108 buf->data[buf->cursor++] = (x >> 8) & 0xFF;
109 buf->data[buf->cursor++] = x & 0xFF;
110 }
111
112 caryll_Buffer *bufninit(uint32_t n, ...) {
113 caryll_Buffer *buf = bufnew();
114 bufbeforewrite(buf, n);
115 va_list ap;
116 va_start(ap, n);
117 for (uint16_t j = 0; j < n; j++) {
118 bufwrite8(buf, (uint8_t)va_arg(ap, int));
119 }
120 va_end(ap);
121 return buf;
122 }
123 void bufnwrite8(caryll_Buffer *buf, uint32_t n, ...) {
124 bufbeforewrite(buf, n);
125 va_list ap;
126 va_start(ap, n);
127 for (uint16_t j = 0; j < n; j++) {
128 bufwrite8(buf, (uint8_t)va_arg(ap, int));
129 }
130 va_end(ap);
131 }
132
133 void bufwrite_sds(caryll_Buffer *buf, sds str) {
134 if (!str) return;
135 size_t len = sdslen(str);
136 if (!len) return;
137 bufbeforewrite(buf, len);
138 memcpy(buf->data + buf->cursor, str, len);
139 buf->cursor += len;
140 }
141 void bufwrite_str(caryll_Buffer *buf, const char *str) {
142 if (!str) return;
143 size_t len = strlen(str);
144 if (!len) return;
145 bufbeforewrite(buf, len);
146 memcpy(buf->data + buf->cursor, str, len);
147 buf->cursor += len;
148 }
149 void bufwrite_bytes(caryll_Buffer *buf, size_t len, const uint8_t *str) {
150 if (!str) return;
151 if (!len) return;
152 bufbeforewrite(buf, len);
153 memcpy(buf->data + buf->cursor, str, len);
154 buf->cursor += len;
155 }
156 void bufwrite_buf(caryll_Buffer *buf, caryll_Buffer *that) {
157 if (!that || !that->data) return;
158 size_t len = buflen(that);
159 bufbeforewrite(buf, len);
160 memcpy(buf->data + buf->cursor, that->data, len);
161 buf->cursor += len;
162 }
163 void bufwrite_bufdel(caryll_Buffer *buf, caryll_Buffer *that) {
164 if (!that) return;
165 if (!that->data) {
166 buffree(that);
167 return;
168 }
169 size_t len = buflen(that);
170 bufbeforewrite(buf, len);
171 memcpy(buf->data + buf->cursor, that->data, len);
172 buffree(that);
173 buf->cursor += len;
174 }
175
176 void buflongalign(caryll_Buffer *buf) {
177 size_t cp = buf->cursor;
178 bufseek(buf, buflen(buf));
179 if (buflen(buf) % 4 == 1) {
180 bufwrite8(buf, 0);
181 bufwrite8(buf, 0);
182 bufwrite8(buf, 0);
183 } else if (buflen(buf) % 4 == 2) {
184 bufwrite8(buf, 0);
185 bufwrite8(buf, 0);
186 } else if (buflen(buf) % 4 == 3) {
187 bufwrite8(buf, 0);
188 }
189 bufseek(buf, cp);
190 }
191
192 // bufpingpong16b writes a buffer and an offset towards it.
193 // [ ^ ] + ###### that
194 // ^cp ^offset
195 // |
196 // V
197 // [ @^ ###### ] , and the value of [@] equals to the former
198 // offset.
199 // ^cp ^offset
200 // Common in writing OpenType features.
201 void bufping16b(caryll_Buffer *buf, size_t *offset, size_t *cp) {
202 bufwrite16b(buf, *offset);
203 *cp = buf->cursor;
204 bufseek(buf, *offset);
205 }
206 void bufping16bd(caryll_Buffer *buf, size_t *offset, size_t *shift, size_t *cp) {
207 bufwrite16b(buf, *offset - *shift);
208 *cp = buf->cursor;
209 bufseek(buf, *offset);
210 }
211 void bufpong(caryll_Buffer *buf, size_t *offset, size_t *cp) {
212 *offset = buf->cursor;
213 bufseek(buf, *cp);
214 }
215 void bufpingpong16b(caryll_Buffer *buf, caryll_Buffer *that, size_t *offset, size_t *cp) {
216 bufwrite16b(buf, *offset);
217 *cp = buf->cursor;
218 bufseek(buf, *offset);
219 bufwrite_bufdel(buf, that);
220 *offset = buf->cursor;
221 bufseek(buf, *cp);
222 }
223
224 void bufprint(caryll_Buffer *buf) {
225 for (size_t j = 0; j < buf->size; j++) {
226 if (j % 16) fprintf(stderr, " ");
227 fprintf(stderr, "%02X", buf->data[j]);
228 if (j % 16 == 15) fprintf(stderr, "\n");
229 }
230 fprintf(stderr, "\n");
231 }