25 size_t n16kBlocks = (nLenSrc + 16383) / 16384;
26 return (nLenSrc + 6 + (n16kBlocks * 5));
37size_t CompressData(
unsigned char* abSrc,
size_t nLenSrc,
unsigned char* abDst,
size_t nLenDst) {
49 if (nLenSrc > UINT_MAX) {
53 if (nLenDst > UINT_MAX) {
59 memset(&zInfo, 0,
sizeof(zInfo));
60 zInfo.total_in = zInfo.avail_in = (
unsigned int)nLenSrc;
61 zInfo.total_out = zInfo.avail_out = (
unsigned int)nLenDst;
62 zInfo.next_in = (
unsigned char*)abSrc;
63 zInfo.next_out = abDst;
67 nErr = deflateInit(&zInfo, Z_DEFAULT_COMPRESSION);
73 n_log(
LOG_ERR,
"%s on string %p size %d", zError(nErr), abSrc, nLenSrc);
76 nErr = deflate(&zInfo, Z_FINISH);
77 if (nErr == Z_STREAM_END) {
78 nRet = zInfo.total_out;
80 n_log(
LOG_ERR,
"%s on string %p size %d", zError(nErr), abSrc, nLenSrc);
94size_t UncompressData(
unsigned char* abSrc,
size_t nLenSrc,
unsigned char* abDst,
size_t nLenDst) {
106 if (nLenSrc > UINT_MAX) {
110 if (nLenDst > UINT_MAX) {
116 memset(&zInfo, 0,
sizeof(zInfo));
117 zInfo.total_in = zInfo.avail_in = (
unsigned int)nLenSrc;
118 zInfo.total_out = zInfo.avail_out = (
unsigned int)nLenDst;
119 zInfo.next_in = (
unsigned char*)abSrc;
120 zInfo.next_out = abDst;
124 nErr = inflateInit(&zInfo);
130 n_log(
LOG_ERR,
"%s on string %p size %d", zError(nErr), abSrc, nLenSrc);
133 nErr = inflate(&zInfo, Z_FINISH);
134 if (nErr == Z_STREAM_END) {
135 nRet = zInfo.total_out;
137 n_log(
LOG_ERR,
"%s on string %p size %d", zError(nErr), abSrc, nLenSrc);
160 if (src->
length > UINT_MAX) {
178 uint32_t src_length = htonl((uint32_t)src->
length);
179 memcpy(zipped->
data, &src_length,
sizeof(uint32_t));
180 char* dataptr = zipped->
data + 4;
182 size_t compressed_size = CompressData((
unsigned char*)src->
data, src->
written, (
unsigned char*)dataptr, zip_max_size);
183 if (compressed_size == 0) {
188 zipped->
written = 4 + compressed_size;
212 uint32_t original_size = 0;
213 memcpy(&original_size, src->
data,
sizeof(uint32_t));
214 original_size = ntohl(original_size);
215 if (original_size == 0) {
#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 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.