1
+ #define ZF_DLL
2
+
3
+ // Ugly Windows hack
4
+ #ifndef ulong
5
+ typedef unsigned long ulong ;
6
+ #endif
7
+
8
+ #include "string.h"
9
+ #include "stdio.h"
10
+ #include "stdlib.h"
11
+ #include "zlib.h"
12
+ #include <cdzf.h>
13
+
14
+
15
+
16
+ int Compress (char * istream , CACHE_EXSTRP retval )
17
+ {
18
+ ulong srcLen = strlen (istream )+ 1 ; // +1 for the trailing `\0`
19
+ ulong destLen = compressBound (srcLen ); // this is how you should estimate size
20
+ // needed for the buffer
21
+ char * ostream = malloc (destLen );
22
+ int res = compress (ostream , & destLen , istream , srcLen );
23
+ CACHEEXSTRKILL (retval );
24
+ if (!CACHEEXSTRNEW (retval ,destLen )) {return ZF_FAILURE ;}
25
+ memcpy (retval -> str .ch ,ostream ,destLen ); // copy to retval->str.ch
26
+ return ZF_SUCCESS ;
27
+ }
28
+
29
+ void DumpHex (const void * data , size_t size ) {
30
+ char ascii [17 ];
31
+ size_t i , j ;
32
+ ascii [16 ] = '\0' ;
33
+ for (i = 0 ; i < size ; ++ i ) {
34
+ printf ("%02X " , ((unsigned char * )data )[i ]);
35
+ if (((unsigned char * )data )[i ] >= ' ' && ((unsigned char * )data )[i ] <= '~' ) {
36
+ ascii [i % 16 ] = ((unsigned char * )data )[i ];
37
+ } else {
38
+ ascii [i % 16 ] = '.' ;
39
+ }
40
+ if ((i + 1 ) % 8 == 0 || i + 1 == size ) {
41
+ printf (" " );
42
+ if ((i + 1 ) % 16 == 0 ) {
43
+ printf ("| %s \n" , ascii );
44
+ } else if (i + 1 == size ) {
45
+ ascii [(i + 1 ) % 16 ] = '\0' ;
46
+ if ((i + 1 ) % 16 <= 8 ) {
47
+ printf (" " );
48
+ }
49
+ for (j = (i + 1 ) % 16 ; j < 16 ; ++ j ) {
50
+ printf (" " );
51
+ }
52
+ printf ("| %s \n" , ascii );
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ int main ()
59
+ {
60
+ const char * istream = "123" ;
61
+ ulong srcLen = strlen (istream )+ 1 ; // +1 for the trailing `\0`
62
+ ulong destLen = compressBound (srcLen ); // this is how you should estimate size
63
+ // needed for the buffer
64
+ char * ostream = malloc (destLen );
65
+ int res = compress (ostream , & destLen , istream , srcLen );
66
+ // destLen is now the size of actuall buffer needed for compression
67
+ // you don't want to uncompress whole buffer later, just the used part
68
+ if (res == Z_BUF_ERROR ){
69
+ printf ("Buffer was too small!\n" );
70
+ return 1 ;
71
+ }
72
+ if (res == Z_MEM_ERROR ){
73
+ printf ("Not enough memory for compression!\n" );
74
+ return 2 ;
75
+ }
76
+
77
+ DumpHex (ostream , destLen );
78
+ //printf("%s", ostream);
79
+
80
+ const char * i2stream = ostream ;
81
+ char * o2stream = malloc (srcLen );
82
+ ulong destLen2 = destLen ; //destLen is the actual size of the compressed buffer
83
+ int des = uncompress (o2stream , & srcLen , i2stream , destLen2 );
84
+ ///printf("%s\n", o2stream);
85
+ return 0 ;
86
+ }
87
+
88
+
89
+ ZFBEGIN
90
+ ZFENTRY ("Compress" ,"cJ" ,Compress )
91
+ ZFEND
0 commit comments