#include "algorithm_registry.h"
#include "compress_utils.h"
#include "utils/single_stream.h"
#include <snappy.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
static cu_status_t snappy_err(int rc, cu_status_t fallback, const char* what) {
if (rc == 0) return CU_OK;
cu_set_last_errorf("snappy: %s failed (rc=%d)", what, rc);
return fallback;
}
static size_t snappy_compress_bound(size_t in_len) {
return snappy_max_compressed_length(in_len);
}
static cu_status_t snappy_compress_oneshot(
const uint8_t* in, size_t in_len,
uint8_t* out, size_t* out_len,
int level
) {
(void)level;
size_t needed = snappy_max_compressed_length(in_len);
if (*out_len < needed) {
*out_len = needed;
return CU_ERR_BUF_TOO_SMALL;
}
struct snappy_env env;
if (snappy_init_env(&env) != 0) {
cu_set_last_error("snappy: env alloc failed");
return CU_ERR_OOM;
}
size_t clen = *out_len;
int rc = snappy_compress(&env, (const char*)(in ? in : (const uint8_t*)""),
in_len, (char*)out, &clen);
snappy_free_env(&env);
if (rc != 0) return snappy_err(rc, CU_ERR_COMPRESSION, "compress");
*out_len = clen;
return CU_OK;
}
static cu_status_t snappy_decompress_oneshot(
const uint8_t* in, size_t in_len,
uint8_t* out, size_t* out_len
) {
if (in_len == 0) {
cu_set_last_error("snappy: empty input");
return CU_ERR_TRUNCATED;
}
size_t needed = 0;
if (!snappy_uncompressed_length((const char*)in, in_len, &needed)) {
cu_set_last_error("snappy: invalid header");
return CU_ERR_DECOMPRESSION;
}
size_t cap_limit = cu_get_max_decompressed_size();
if (cap_limit > 0 && needed > cap_limit) {
cu_set_last_errorf("snappy: decompressed size %zu exceeds cap %zu", needed, cap_limit);
return CU_ERR_SIZE_LIMIT;
}
if (*out_len < needed) {
*out_len = needed;
return CU_ERR_BUF_TOO_SMALL;
}
int rc = snappy_uncompress((const char*)in, in_len, (char*)out);
if (rc != 0) return snappy_err(rc, CU_ERR_DECOMPRESSION, "uncompress");
*out_len = needed;
return CU_OK;
}
static cu_status_t snappy_decompress_size_hint(
const uint8_t* in, size_t in_len, size_t* out_size
) {
if (in_len == 0) return CU_ERR_TRUNCATED;
size_t sz = 0;
if (!snappy_uncompressed_length((const char*)in, in_len, &sz)) {
cu_set_last_error("snappy: invalid header");
return CU_ERR_DECOMPRESSION;
}
*out_size = sz;
return CU_OK;
}
typedef struct {
uint8_t* in_buf;
size_t in_len;
size_t in_cap;
uint8_t* out_buf;
size_t out_head;
size_t out_tail;
int produced;
size_t cap;
int single;
int ended;
size_t scan_pos;
int have_len;
uint64_t declared;
uint64_t counted;
cu_unused_t unused;
} snappy_stream_state_t;
static cu_status_t sn_in_append(snappy_stream_state_t* st, const uint8_t* src, size_t n) {
if (n == 0) return CU_OK;
size_t need = st->in_len + n;
if (need > st->in_cap) {
size_t new_cap = st->in_cap ? st->in_cap * 2 : 64 * 1024;
while (new_cap < need) new_cap *= 2;
uint8_t* p = realloc(st->in_buf, new_cap);
if (!p) { cu_set_last_error("snappy: oom"); return CU_ERR_OOM; }
st->in_buf = p;
st->in_cap = new_cap;
}
memcpy(st->in_buf + st->in_len, src, n);
st->in_len += n;
return CU_OK;
}
static int sn_out_drain(snappy_stream_state_t* st,
uint8_t* out, size_t cap, size_t* written) {
size_t available = st->out_tail - st->out_head;
size_t room = cap - *written;
size_t n = available < room ? available : room;
if (n > 0) {
memcpy(out + *written, st->out_buf + st->out_head, n);
*written += n;
st->out_head += n;
}
return st->out_head < st->out_tail;
}
static void snappy_stream_destroy(void* state) {
snappy_stream_state_t* st = (snappy_stream_state_t*)state;
if (!st) return;
free(st->in_buf);
free(st->out_buf);
cu_unused_free(&st->unused);
free(st);
}
static cu_status_t snappy_cstream_create(int level, void** out_state) {
(void)level;
snappy_stream_state_t* st = calloc(1, sizeof(*st));
if (!st) { cu_set_last_error("snappy: oom"); return CU_ERR_OOM; }
*out_state = st;
return CU_OK;
}
static cu_status_t snappy_cstream_write(
void* state, const uint8_t* in, size_t in_len,
uint8_t* out, size_t* out_len
) {
(void)out;
snappy_stream_state_t* st = (snappy_stream_state_t*)state;
if (st->produced) {
cu_set_last_error("snappy: write after finish");
return CU_ERR_STREAM_STATE;
}
cu_status_t s = sn_in_append(st, in, in_len);
*out_len = 0;
return s;
}
static cu_status_t snappy_cstream_finish(
void* state, uint8_t* out, size_t* out_len
) {
snappy_stream_state_t* st = (snappy_stream_state_t*)state;
size_t cap = *out_len;
size_t written = 0;
if (!st->produced) {
size_t bound = snappy_max_compressed_length(st->in_len);
st->out_buf = malloc(bound ? bound : 1);
if (!st->out_buf) { cu_set_last_error("snappy: oom"); return CU_ERR_OOM; }
struct snappy_env env;
if (snappy_init_env(&env) != 0) {
cu_set_last_error("snappy: env alloc failed");
return CU_ERR_OOM;
}
size_t clen = bound;
int rc = snappy_compress(
&env, (const char*)(st->in_buf ? st->in_buf : (const uint8_t*)""),
st->in_len, (char*)st->out_buf, &clen);
snappy_free_env(&env);
if (rc != 0) return snappy_err(rc, CU_ERR_COMPRESSION, "compress");
st->out_tail = clen;
st->produced = 1;
}
int more = sn_out_drain(st, out, cap, &written);
*out_len = written;
return more ? CU_ERR_BUF_TOO_SMALL : CU_OK;
}
static cu_status_t snappy_dstream_create(void** out_state) {
snappy_stream_state_t* st = calloc(1, sizeof(*st));
if (!st) { cu_set_last_error("snappy: oom"); return CU_ERR_OOM; }
st->cap = cu_get_max_decompressed_size();
*out_state = st;
return CU_OK;
}
static cu_status_t snappy_dstream_create_single(void** out_state) {
snappy_stream_state_t* st = calloc(1, sizeof(*st));
if (!st) { cu_set_last_error("snappy: oom"); return CU_ERR_OOM; }
st->single = 1;
st->cap = cu_get_max_decompressed_size();
*out_state = st;
return CU_OK;
}
static cu_status_t sn_scan(snappy_stream_state_t* st, size_t* end) {
const uint8_t* p = st->in_buf;
size_t n = st->in_len;
size_t pos = st->scan_pos;
*end = 0;
if (!st->have_len) {
uint64_t v = 0;
for (unsigned i = 0;; i++) {
if (i == 5) {
cu_set_last_error("snappy: invalid header (length varint too long)");
return CU_ERR_DECOMPRESSION;
}
if (pos + i >= n) return CU_OK;
uint8_t b = p[pos + i];
v |= (uint64_t)(b & 0x7F) << (7 * i);
if (!(b & 0x80)) {
pos += i + 1;
break;
}
}
if (v > UINT32_MAX || v > SIZE_MAX) {
cu_set_last_error("snappy: invalid header (length out of range)");
return CU_ERR_DECOMPRESSION;
}
if (st->cap > 0 && v > st->cap) {
cu_set_last_errorf("snappy: decompressed size %llu exceeds cap %zu",
(unsigned long long)v, st->cap);
return CU_ERR_SIZE_LIMIT;
}
st->have_len = 1;
st->declared = v;
}
while (st->counted < st->declared && pos < n) {
uint8_t tag = p[pos];
uint64_t len, size;
switch (tag & 3) {
case 0: {
unsigned code = tag >> 2;
unsigned extra = code < 60 ? 0 : code - 59;
if (n - pos - 1 < extra) goto more;
uint64_t l = code;
if (extra) {
l = 0;
for (unsigned k = 0; k < extra; k++) l |= (uint64_t)p[pos + 1 + k] << (8 * k);
}
len = l + 1;
size = 1 + extra + len;
break;
}
case 1: len = 4 + ((tag >> 2) & 7); size = 2; break;
case 2: len = (tag >> 2) + 1; size = 3; break;
default: len = (tag >> 2) + 1; size = 5; break;
}
if (len > st->declared - st->counted) {
cu_set_last_error("snappy: element runs past the declared length");
return CU_ERR_DECOMPRESSION;
}
if ((uint64_t)(n - pos) < size) break;
pos += (size_t)size;
st->counted += len;
}
more:
st->scan_pos = pos;
if (st->counted == st->declared) *end = pos;
return CU_OK;
}
static cu_status_t sn_dstream_write_single(
snappy_stream_state_t* st, const uint8_t* in, size_t in_len,
uint8_t* out, size_t* out_len
) {
size_t cap = *out_len;
size_t written = 0;
*out_len = 0;
if (!st->ended) {
cu_status_t s = sn_in_append(st, in, in_len);
if (s != CU_OK) return s;
size_t end;
s = sn_scan(st, &end);
if (s != CU_OK || end == 0) return s;
s = cu_unused_append(&st->unused, st->in_buf + end, st->in_len - end);
if (s != CU_OK) return s;
size_t usize = (size_t)st->declared;
st->out_buf = malloc(usize ? usize : 1);
if (!st->out_buf) { cu_set_last_error("snappy: oom"); return CU_ERR_OOM; }
int rc = snappy_uncompress((const char*)st->in_buf, end, (char*)st->out_buf);
if (rc != 0) return snappy_err(rc, CU_ERR_DECOMPRESSION, "uncompress");
free(st->in_buf);
st->in_buf = NULL;
st->in_len = st->in_cap = 0;
st->out_tail = usize;
st->produced = 1;
st->ended = 1;
}
int more = sn_out_drain(st, out, cap, &written);
*out_len = written;
return more ? CU_ERR_BUF_TOO_SMALL : CU_OK;
}
static cu_status_t snappy_dstream_write(
void* state, const uint8_t* in, size_t in_len,
uint8_t* out, size_t* out_len
) {
snappy_stream_state_t* st = (snappy_stream_state_t*)state;
if (st->single) return sn_dstream_write_single(st, in, in_len, out, out_len);
if (st->produced) {
cu_set_last_error("snappy: data after end of stream");
return CU_ERR_DECOMPRESSION;
}
cu_status_t s = sn_in_append(st, in, in_len);
*out_len = 0;
return s;
}
static cu_status_t snappy_dstream_finish(
void* state, uint8_t* out, size_t* out_len
) {
snappy_stream_state_t* st = (snappy_stream_state_t*)state;
size_t cap = *out_len;
size_t written = 0;
if (st->single) {
if (st->ended) return sn_dstream_write_single(st, NULL, 0, out, out_len);
*out_len = 0;
cu_set_last_error("snappy: truncated block at finish");
return CU_ERR_TRUNCATED;
}
if (!st->produced) {
if (st->in_len == 0) {
cu_set_last_error("snappy: truncated stream at finish");
return CU_ERR_TRUNCATED;
}
size_t usize = 0;
if (!snappy_uncompressed_length((const char*)st->in_buf, st->in_len, &usize)) {
cu_set_last_error("snappy: invalid header");
return CU_ERR_DECOMPRESSION;
}
if (st->cap > 0 && usize > st->cap) {
*out_len = 0;
cu_set_last_errorf("snappy: decompressed size %zu exceeds cap %zu", usize, st->cap);
return CU_ERR_SIZE_LIMIT;
}
st->out_buf = malloc(usize ? usize : 1);
if (!st->out_buf) { cu_set_last_error("snappy: oom"); return CU_ERR_OOM; }
int rc = snappy_uncompress((const char*)st->in_buf, st->in_len,
(char*)st->out_buf);
if (rc != 0) return snappy_err(rc, CU_ERR_DECOMPRESSION, "uncompress");
st->out_tail = usize;
st->produced = 1;
}
int more = sn_out_drain(st, out, cap, &written);
*out_len = written;
return more ? CU_ERR_BUF_TOO_SMALL : CU_OK;
}
static int snappy_dstream_end(const void* state, const uint8_t** unused, size_t* unused_len) {
const snappy_stream_state_t* st = (const snappy_stream_state_t*)state;
if (!st->single || !st->ended) return 0;
cu_unused_get(&st->unused, unused, unused_len);
return 1;
}
const cu_algorithm_vtbl_t cu_snappy_vtbl = {
.name = "snappy",
#ifndef CU_OMIT_COMPRESS
.compress_bound = snappy_compress_bound,
.compress = snappy_compress_oneshot,
.compress_stream_create = snappy_cstream_create,
.compress_stream_write = snappy_cstream_write,
.compress_stream_finish = snappy_cstream_finish,
.compress_stream_destroy = snappy_stream_destroy,
#endif
#ifndef CU_OMIT_DECOMPRESS
.decompress = snappy_decompress_oneshot,
.decompress_size_hint = snappy_decompress_size_hint,
.decompress_stream_create = snappy_dstream_create,
.decompress_stream_write = snappy_dstream_write,
.decompress_stream_finish = snappy_dstream_finish,
.decompress_stream_destroy = snappy_stream_destroy,
.decompress_stream_create_single = snappy_dstream_create_single,
.decompress_stream_end = snappy_dstream_end,
#endif
};