42 size_t n16kBlocks = (nLenSrc + 16383) / 16384;
43 return (nLenSrc + 6 + (n16kBlocks * 5));
54size_t CompressData(
unsigned char* abSrc,
size_t nLenSrc,
unsigned char* abDst,
size_t nLenDst) {
66 if (nLenSrc > UINT_MAX) {
70 if (nLenDst > UINT_MAX) {
76 memset(&zInfo, 0,
sizeof(zInfo));
77 zInfo.total_in = zInfo.avail_in = (
unsigned int)nLenSrc;
78 zInfo.total_out = zInfo.avail_out = (
unsigned int)nLenDst;
79 zInfo.next_in = (
unsigned char*)abSrc;
80 zInfo.next_out = abDst;
84 nErr = deflateInit(&zInfo, Z_DEFAULT_COMPRESSION);
90 n_log(
LOG_ERR,
"%s on string %p size %zu", zError(nErr), abSrc, nLenSrc);
93 nErr = deflate(&zInfo, Z_FINISH);
94 if (nErr == Z_STREAM_END) {
95 nRet = zInfo.total_out;
97 n_log(
LOG_ERR,
"%s on string %p size %zu", zError(nErr), abSrc, nLenSrc);
111size_t UncompressData(
unsigned char* abSrc,
size_t nLenSrc,
unsigned char* abDst,
size_t nLenDst) {
123 if (nLenSrc > UINT_MAX) {
127 if (nLenDst > UINT_MAX) {
133 memset(&zInfo, 0,
sizeof(zInfo));
134 zInfo.total_in = zInfo.avail_in = (
unsigned int)nLenSrc;
135 zInfo.total_out = zInfo.avail_out = (
unsigned int)nLenDst;
136 zInfo.next_in = (
unsigned char*)abSrc;
137 zInfo.next_out = abDst;
141 nErr = inflateInit(&zInfo);
147 n_log(
LOG_ERR,
"%s on string %p size %zu", zError(nErr), abSrc, nLenSrc);
150 nErr = inflate(&zInfo, Z_FINISH);
151 if (nErr == Z_STREAM_END) {
152 nRet = zInfo.total_out;
154 n_log(
LOG_ERR,
"%s on string %p size %zu", zError(nErr), abSrc, nLenSrc);
177 if (src->
length > UINT_MAX) {
195 uint32_t src_length = htonl((uint32_t)src->
length);
196 memcpy(zipped->
data, &src_length,
sizeof(uint32_t));
197 char* dataptr = zipped->
data + 4;
199 size_t compressed_size =
CompressData((
unsigned char*)src->
data, src->
written, (
unsigned char*)dataptr, zip_max_size);
200 if (compressed_size == 0) {
205 zipped->
written = 4 + compressed_size;
234 uint32_t original_size = 0;
235 memcpy(&original_size, src->
data,
sizeof(uint32_t));
236 original_size = ntohl(original_size);
237 if (original_size == 0) {
241 if (original_size > 256 * 1024 * 1024) {
242 n_log(
LOG_ERR,
"decompressed size too large: %u", original_size);
#define __n_assert(__ptr, __ret)
macro to assert things
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
size_t written
size of the written data inside the string
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
A box including a string and his lenght.
size_t GetMaxCompressedLen(size_t nLenSrc)
Return the maximum compressed size.
size_t CompressData(unsigned char *abSrc, size_t nLenSrc, unsigned char *abDst, size_t nLenDst)
Compress a string to another.
size_t UncompressData(unsigned char *abSrc, size_t nLenSrc, unsigned char *abDst, size_t nLenDst)
Uncompress a string to another.
N_STR * unzip_nstr(N_STR *src)
return an uncompressed version of src
N_STR * zip_nstr(N_STR *src)
return a compressed version of src
ZLIB compression handler.