28 unsigned int n16kBlocks = (nLenSrc+16383) / 16384;
29 return ( nLenSrc + 6 + (n16kBlocks*5) );
42int CompressData(
unsigned char *abSrc,
unsigned int nLenSrc,
unsigned char *abDst,
unsigned int nLenDst )
59 memset( &zInfo, 0,
sizeof( zInfo ) );
60 zInfo.total_in= zInfo.avail_in = nLenSrc;
61 zInfo.total_out= zInfo.avail_out= nLenDst;
62 zInfo.next_in= (
unsigned char *)abSrc;
63 zInfo.next_out= abDst;
66 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 )
79 nRet= zInfo.total_out;
83 n_log(
LOG_ERR,
"%s on string %p size %d", zError( nErr ), abSrc, nLenSrc );
99int UncompressData(
unsigned char *abSrc,
unsigned int nLenSrc,
unsigned char *abDst,
unsigned int nLenDst )
116 memset( &zInfo, 0,
sizeof( zInfo ) );
117 zInfo.total_in= zInfo.avail_in= nLenSrc;
118 zInfo.total_out= zInfo.avail_out= nLenDst;
119 zInfo.next_in= (
unsigned char *)abSrc;
120 zInfo.next_out= abDst;
123 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 )
136 nRet= zInfo.total_out;
140 n_log(
LOG_ERR,
"%s on string %p size %d", zError( nErr ), abSrc, nLenSrc );
142 inflateEnd( &zInfo );
158 if( src -> length <= 0 )
160 n_log(
LOG_ERR,
"length of src (%d) <= 0", src -> length );
163 if( src -> written <= 0 )
165 n_log(
LOG_ERR,
"written of src (%d) <= 0", src -> written );
178 int32_t src_length = htonl( src -> length );
179 memcpy( zipped -> data, &src_length,
sizeof( int32_t ) );
180 char *dataptr = zipped -> data + 4 ;
182 int compressed_size = 0 ;
183 compressed_size =
CompressData( (
unsigned char *)src -> data, src -> written, (
unsigned char *)dataptr, zip_max_size );
184 if( compressed_size == -1 )
187 n_log(
LOG_ERR,
"unable to zip string %p %d/%d bytes", src -> data, src -> written, src -> length );
190 zipped -> written = 4 + compressed_size ;
191 n_log(
LOG_DEBUG,
"zip :%d original: %d", zipped -> written, src -> length );
208 if( src -> length <= 0 )
210 n_log(
LOG_ERR,
"length of src (%d) <= 0", src -> length );
213 if( src -> written <= 0 )
215 n_log(
LOG_ERR,
"written of src (%d) <= 0", src -> written );
219 int32_t original_size = 0 ;
220 memcpy( &original_size, src -> data,
sizeof( int32_t ) );
221 original_size = ntohl( original_size );
222 if( original_size <= 0 )
224 n_log(
LOG_ERR,
"original size (%d) <= 0", original_size );
233 unzipped -> written =
UncompressData( ((
unsigned char *)src -> data) + 4, src -> written, (
unsigned char *)unzipped -> data, original_size );
234 if( unzipped -> written == 0 )
236 n_log(
LOG_ERR,
"unable to unzip string %p %d/%d bytes", unzipped -> data, unzipped -> written, unzipped -> length );
240 n_log(
LOG_DEBUG,
"Size: zip: %d => unzip :%d original:%d", src -> written, unzipped -> written, 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
#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.
int GetMaxCompressedLen(unsigned int nLenSrc)
Return the maximum compressed size.
int CompressData(unsigned char *abSrc, unsigned int nLenSrc, unsigned char *abDst, unsigned int nLenDst)
Compress a string to another.
int UncompressData(unsigned char *abSrc, unsigned int nLenSrc, unsigned char *abDst, unsigned int 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.