infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
fuzz_regression_helpers.h
Go to the documentation of this file.
1#pragma once
33#include <stddef.h>
34#include <stdlib.h>
35#include <string.h>
36
47static unsigned char * b64_decode(const char * data, size_t * out_len) {
48 // A standard lookup table for Base64 decoding. -1 represents an invalid character.
49 // Size padded to 256 to prevent out-of-bounds access on invalid input.
50 static const int b64_decode_table[] = {-1,
51 -1,
52 -1,
53 -1,
54 -1,
55 -1,
56 -1,
57 -1,
58 -1,
59 -1,
60 -1,
61 -1,
62 -1,
63 -1,
64 -1,
65 -1,
66 -1,
67 -1,
68 -1,
69 -1,
70 -1,
71 -1,
72 -1,
73 -1,
74 -1,
75 -1,
76 -1,
77 -1,
78 -1,
79 -1,
80 -1,
81 -1,
82 -1,
83 -1,
84 -1,
85 -1,
86 -1,
87 -1,
88 -1,
89 -1,
90 -1,
91 -1,
92 -1,
93 62,
94 -1,
95 -1,
96 -1,
97 63,
98 52,
99 53,
100 54,
101 55,
102 56,
103 57,
104 58,
105 59,
106 60,
107 61,
108 -1,
109 -1,
110 -1,
111 -1,
112 -1,
113 -1,
114 -1,
115 0,
116 1,
117 2,
118 3,
119 4,
120 5,
121 6,
122 7,
123 8,
124 9,
125 10,
126 11,
127 12,
128 13,
129 14,
130 15,
131 16,
132 17,
133 18,
134 19,
135 20,
136 21,
137 22,
138 23,
139 24,
140 25,
141 -1,
142 -1,
143 -1,
144 -1,
145 -1,
146 -1,
147 26,
148 27,
149 28,
150 29,
151 30,
152 31,
153 32,
154 33,
155 34,
156 35,
157 36,
158 37,
159 38,
160 39,
161 40,
162 41,
163 42,
164 43,
165 44,
166 45,
167 46,
168 47,
169 48,
170 49,
171 50,
172 51,
173 -1,
174 -1,
175 -1,
176 -1,
177 -1,
178 // Padding to 256 elements
179 -1,
180 -1,
181 -1,
182 -1,
183 -1,
184 -1,
185 -1,
186 -1,
187 -1,
188 -1,
189 -1,
190 -1,
191 -1,
192 -1,
193 -1,
194 -1,
195 -1,
196 -1,
197 -1,
198 -1,
199 -1,
200 -1,
201 -1,
202 -1,
203 -1,
204 -1,
205 -1,
206 -1,
207 -1,
208 -1,
209 -1,
210 -1,
211 -1,
212 -1,
213 -1,
214 -1,
215 -1,
216 -1,
217 -1,
218 -1,
219 -1,
220 -1,
221 -1,
222 -1,
223 -1,
224 -1,
225 -1,
226 -1,
227 -1,
228 -1,
229 -1,
230 -1,
231 -1,
232 -1,
233 -1,
234 -1,
235 -1,
236 -1,
237 -1,
238 -1,
239 -1,
240 -1,
241 -1,
242 -1,
243 -1,
244 -1,
245 -1,
246 -1,
247 -1,
248 -1,
249 -1,
250 -1,
251 -1,
252 -1,
253 -1,
254 -1,
255 -1,
256 -1,
257 -1,
258 -1,
259 -1,
260 -1,
261 -1,
262 -1,
263 -1,
264 -1,
265 -1,
266 -1,
267 -1,
268 -1,
269 -1,
270 -1,
271 -1,
272 -1,
273 -1,
274 -1,
275 -1,
276 -1,
277 -1,
278 -1,
279 -1,
280 -1,
281 -1,
282 -1,
283 -1,
284 -1,
285 -1,
286 -1,
287 -1,
288 -1,
289 -1,
290 -1,
291 -1,
292 -1,
293 -1,
294 -1,
295 -1,
296 -1,
297 -1,
298 -1,
299 -1,
300 -1,
301 -1,
302 -1,
303 -1,
304 -1,
305 -1,
306 -1,
307 -1,
308 -1,
309 -1,
310 -1,
311 -1};
312
313 size_t in_len = strlen(data);
314 if (in_len % 4 != 0)
315 return NULL; // Base64 strings must have a length that is a multiple of 4.
316
317 // Calculate the output length, accounting for padding characters.
318 *out_len = in_len / 4 * 3;
319 if (data[in_len - 1] == '=')
320 (*out_len)--;
321 if (data[in_len - 2] == '=')
322 (*out_len)--;
323
324 unsigned char * out = (unsigned char *)malloc(*out_len);
325 if (out == NULL)
326 return NULL;
327
328 // Process the input string in 4-character chunks.
329 for (size_t i = 0, j = 0; i < in_len;) {
330 int sextet_a = b64_decode_table[(unsigned char)data[i++]];
331 int sextet_b = b64_decode_table[(unsigned char)data[i++]];
332 int sextet_c = b64_decode_table[(unsigned char)data[i++]];
333 int sextet_d = b64_decode_table[(unsigned char)data[i++]];
334
335 // Validate the characters and handle padding.
336 if (sextet_a == -1 || sextet_b == -1 || (data[i - 2] != '=' && sextet_c == -1) ||
337 (data[i - 1] != '=' && sextet_d == -1)) {
338 free(out);
339 return NULL;
340 }
341
342 // Combine the four 6-bit sextets into a 24-bit triple.
343 unsigned int triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
344
345 // Extract the three 8-bit bytes from the triple.
346 if (j < *out_len)
347 out[j++] = (triple >> 2 * 8) & 0xFF;
348 if (j < *out_len)
349 out[j++] = (triple >> 1 * 8) & 0xFF;
350 if (j < *out_len)
351 out[j++] = (triple >> 0 * 8) & 0xFF;
352 }
353
354 return out;
355}
static unsigned char * b64_decode(const char *data, size_t *out_len)
Definition fuzz_regression_helpers.h:47