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
20#include <stddef.h>
21#include <stdlib.h>
22#include <string.h>
23
43static unsigned char * b64_decode(const char * data, size_t * out_len) {
44 static const int b64_decode_table[] = {
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55,
47 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
48 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32,
49 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1};
50
51 size_t in_len = strlen(data);
52 if (in_len % 4 != 0)
53 return NULL;
54
55 *out_len = in_len / 4 * 3;
56 if (data[in_len - 1] == '=')
57 (*out_len)--;
58 if (data[in_len - 2] == '=')
59 (*out_len)--;
60
61 unsigned char * out = (unsigned char *)malloc(*out_len);
62 if (out == NULL)
63 return NULL;
64
65 for (size_t i = 0, j = 0; i < in_len;) {
66 int sextet_a = b64_decode_table[(unsigned char)data[i++]];
67 int sextet_b = b64_decode_table[(unsigned char)data[i++]];
68 int sextet_c = b64_decode_table[(unsigned char)data[i++]];
69 int sextet_d = b64_decode_table[(unsigned char)data[i++]];
70
71 if (sextet_a == -1 || sextet_b == -1 || (data[i - 2] != '=' && sextet_c == -1) ||
72 (data[i - 1] != '=' && sextet_d == -1)) {
73 free(out);
74 return NULL;
75 }
76
77 unsigned int triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
78
79 if (j < *out_len)
80 out[j++] = (triple >> 2 * 8) & 0xFF;
81 if (j < *out_len)
82 out[j++] = (triple >> 1 * 8) & 0xFF;
83 if (j < *out_len)
84 out[j++] = (triple >> 0 * 8) & 0xFF;
85 }
86
87 return out;
88}
static unsigned char * b64_decode(const char *data, size_t *out_len)
Definition fuzz_regression_helpers.h:43