19 size_t ret_size = ZSTD_getFrameContentSize(data, data_size);
21 if (ret_size == ZSTD_CONTENTSIZE_ERROR) {
27 if (ret_size == ZSTD_CONTENTSIZE_UNKNOWN) {
28 logmsg(
TMJ_LOG_ERR,
"Decode (zstd): Unable to determine uncompressed size of compressed data");
33 void* ret = malloc(ret_size);
36 logmsg(
TMJ_LOG_ERR,
"Decode (zstd): Unable to allocate buffer for decompressed data, the system is out of memory");
41 size_t dsize = ZSTD_decompress(ret, ret_size, data, data_size);
45 if (ZSTD_isError(dsize)) {
46 logmsg(
TMJ_LOG_ERR,
"Decode (zstd): Decompression error: %s", ZSTD_getErrorName(dsize));
53 *decompressed_size = ret_size;
71 const size_t INFLATE_BLOCK_SIZE = 262144;
73 uint8_t* out = malloc(INFLATE_BLOCK_SIZE);
76 logmsg(
TMJ_LOG_ERR,
"Decode (zlib): Unable to allocate buffer for decompressed data, the system is out of memory");
81 z_stream stream = {0};
83 stream.zalloc = Z_NULL;
84 stream.zfree = Z_NULL;
85 stream.opaque = Z_NULL;
87 stream.avail_in = data_size;
88 stream.avail_out = INFLATE_BLOCK_SIZE;
90 stream.next_in = data;
91 stream.next_out = out;
94 int ret = inflateInit2(&stream, 15 + 32);
102 logmsg(
TMJ_LOG_ERR,
"Decode (zlib): Unable to initialize inflate, the system is out of memory");
106 case Z_VERSION_ERROR:
107 logmsg(
TMJ_LOG_ERR,
"Decode (zlib): Unable to initialize inflate, incompatible zlib library version");
113 "Decode (zlib): Unable to initialize inflate, invalid parameter(s) to inflate initialization "
122 size_t realloc_scale = 2;
130 int stat = inflate(&stream, Z_NO_FLUSH);
132 while (stat != Z_STREAM_END) {
136 if (stream.avail_out != 0) {
146 out = realloc(out, INFLATE_BLOCK_SIZE * realloc_scale);
148 logmsg(
TMJ_LOG_ERR,
"Decode (zlib): Unable to grow inflate output buffer, the system is out memory");
153 stream.avail_out = INFLATE_BLOCK_SIZE;
155 stream.next_out = out + stream.total_out;
162 logmsg(
TMJ_LOG_ERR,
"Decode (zlib): Unable to complete inflate, preset dictionary required");
167 logmsg(
TMJ_LOG_ERR,
"Decode (zlib): Unable to complete inflate, input data appears corrupted");
172 logmsg(
TMJ_LOG_ERR,
"Decode (zlib): Unable to complete inflate, stream structure inconsistent");
177 logmsg(
TMJ_LOG_ERR,
"Decode (zlib): Unable to complete inflate, the system is out of memory");
185 stat = inflate(&stream, Z_NO_FLUSH);
188 logmsg(
TMJ_LOG_DEBUG,
"Decode (zlib): Completed inflate, %zd bytes written to output buffer", stream.total_out);
190 if (inflateEnd(&stream) != Z_OK) {
191 logmsg(
TMJ_LOG_ERR,
"Decode (zlib): Completed inflate, but could not clean up; stream state was inconsistent");
196 *decompressed_size = stream.total_out;
215 const char b64_encode_table[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
217 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
218 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
219 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255,
220 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
221 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26,
222 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
223 47, 48, 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
224 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
225 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
226 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
227 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
228 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
229 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
230 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
231 255, 255, 255, 255, 255, 255, 255, 255, 255, };
251 size_t len = strlen(data);
253 size_t ret = len / 4 * 3;
256 if (data[len - 1] ==
'=') {
259 if (data[len - 2] ==
'=') {
272 if (c ==
'+' || c ==
'/' || c ==
'=') {
294 size_t len = strlen(data);
297 logmsg(
TMJ_LOG_ERR,
"Decode (b64): Invalid Base64 string, input length is not a multiple of 4");
303 uint8_t* out = malloc(dSize);
306 logmsg(
TMJ_LOG_ERR,
"Decode (b64): Unable to allocate output buffer, the system is out of memory");
312 for (
size_t i = 0; i < len; i++) {
322 for (
size_t i = 0, j = 0; i < len; i += 4, j += 3) {
330 out[j] = (p >> 16) & 0xFF;
331 if (data[i + 2] !=
'=') {
332 out[j + 1] = (p >> 8) & 0xFF;
334 if (data[i + 3] !=
'=') {
335 out[j + 2] = p & 0xFF;
339 *decoded_size = dSize;
const unsigned char b64_decode_table[]
bool b64_is_valid_char(char c)
char * tmj_b64_encode(uint8_t *data)
Unimplemented, but may be useful to implement in the future.
const char b64_encode_table[]
size_t b64_decode_size(const char *data)
Calculates the size of the data decoded from the given base64 string.
uint8_t * tmj_zlib_decompress(const uint8_t *data, size_t data_size, size_t *decompressed_size)
Decompresses a zlib/gzip-compressed buffer of bytes.
uint8_t * tmj_b64_decode(const char *data, size_t *decoded_size)
Decodes a base64 string.
uint8_t * tmj_zstd_decompress(const uint8_t *data, size_t data_size, size_t *decompressed_size)
Decompresses a zstd-compressed buffer of bytes.
void logmsg(tmj_log_priority priority, char *msg,...)
Processes log messages and passes them to the active logging callback, if there is one.