1 /******************************************************************************
2 *
3 * Copyright (C) 2009, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexander Antypenko ( santy ) v. 1.00
11 *
12 ******************************************************************************/
13 import "zlib1.dll"
14 {
15 uint gzopen(uint,uint)
16 uint gzdopen(uint,uint)
17 int gzsetparams(uint,int,int)
18 int gzread(uint,uint,uint)
19 int gzwrite(uint,uint,uint)
20 int gzputs(uint,uint)
21 uint gzgets(uint,uint,int)
22 int gzputc(uint,int)
23 int gzgetc(uint)
24 int gzungetc(int,uint)
25 int gzflush(uint,int)
26 uint gzseek(uint,uint,int)
27 int gzrewind (uint)
28 uint gztell(uint)
29 int gzeof(uint)
30 int gzdirect(uint)
31 int gzclose(uint)
32 uint gzerror(uint,uint)
33 gzclearerr(uint)
34 //
35 int compress(uint,uint,uint,uint)
36 int compress2(uint,uint,uint,uint,int)
37 uint compressBound(uint)
38 int uncompress(uint,uint,uint,uint)
39
40
41 }
42
43 define
44 {
45
46 Z_NO_FLUSH = 0
47 Z_PARTIAL_FLUSH = 1 /* will be removed, use Z_SYNC_FLUSH instead */
48 Z_SYNC_FLUSH = 2
49 Z_FULL_FLUSH = 3
50 Z_FINISH = 4
51 Z_BLOCK = 5
52
53 /* Allowed flush values; see deflate() and inflate() below for details */
54
55 Z_OK = 0
56 Z_STREAM_END = 1
57 Z_NEED_DICT = 2
58 Z_ERRNO = (-1)
59 Z_STREAM_ERROR = (-2)
60 Z_DATA_ERROR = (-3)
61 Z_MEM_ERROR = (-4)
62 Z_BUF_ERROR = (-5)
63 Z_VERSION_ERROR = (-6)
64
65 /*
66 * Return codes for the compression/decompression functions. Negative
67 * values are errors, positive values are used for special but normal events.
68 */
69
70 Z_NO_COMPRESSION = 0
71 Z_BEST_SPEED = 1
72 Z_BEST_COMPRESSION = 9
73 Z_DEFAULT_COMPRESSION = (-1)
74 /* compression levels */
75
76 Z_FILTERED = 1
77 Z_HUFFMAN_ONLY = 2
78 Z_RLE = 3
79 Z_FIXED = 4
80 Z_DEFAULT_STRATEGY = 0 /* compression strategy; see deflateInit2() below for details */
81
82 Z_BINARY = 0
83 Z_TEXT = 1
84 Z_ASCII = $Z_TEXT /* for compatibility with 1.2.2 and earlier */
85 Z_UNKNOWN = 2 /* Possible values of the data_type field (though see inflate()) */
86
87 Z_DEFLATED = 8 /* The deflate compression method (the only one supported in this version) */
88 Z_NULL = 0 /* for initializing zalloc, zfree, opaque */
89 }
90
91
92 /*-----------------------------------------------------------------------------
93 * @syntax squeeze (str src_buffer)
94 *
95 * @param src_buffer Input uncompressed string data
96 *
97 * @return The string containing the compressed str_buffer.
98 *
99 * @example
100 * [ str str_z = squeeze ("Demo string") ]
101 -----------------------------------------------------------------------------*/
102 func str squeeze<result>(str src_buffer)
103 {
104 buf dest_buffer
105 str out_buffer=""
106
107 uint srclen = *src_buffer
108 uint destlen = srclen+ uint( (float(srclen) * 1.01f) + 12f)
109 uint lpdestlen = malloc(destlen)
110 dest_buffer.reserve(destlen)
111
112 if (compress(dest_buffer.ptr(),&lpdestlen,src_buffer.ptr(),srclen) == $Z_OK) : out_buffer.load(dest_buffer.ptr(),lpdestlen)
113 mfree(&lpdestlen)
114 result=out_buffer
115 }
116
117 /*-----------------------------------------------------------------------------
118 * @syntax unsqueeze(str src_buffer)
119 *
120 * @param src_buffer Input compressed buffer
121 *
122 * @return The original uncompressed string from a compressed buffer in src_buffer
123 *
124 * @example
125 * [ str str_z1 = unsqueeze(str_z) ]
126 -----------------------------------------------------------------------------*/
127 func str unsqueeze<result>(str src_buffer)
128 {
129
130 buf dest_buffer
131 str out_buffer=""
132 uint uRetCode
133
134 uint srclen = *src_buffer
135 uint destlen = srclen*3
136 uint lpdestlen = malloc(destlen)
137 dest_buffer.reserve(destlen)
138 while (uRetCode = uncompress(dest_buffer.ptr(),&lpdestlen,src_buffer.ptr(),srclen) == $Z_BUF_ERROR)
139 {
140 destlen = destlen*2
141 dest_buffer.reserve(destlen)
142 }
143 if (uRetCode == $Z_OK): out_buffer.load(dest_buffer.ptr(),lpdestlen)
144 mfree(&lpdestlen)
145 result=out_buffer
146 }
147
148 /*-----------------------------------------------------------------------------
149 * @syntax [ gzfile_read (str gzfilename)]
150 *
151 * @return A string buffer with the original contents.
152 *
153 * Uncompresses the GZ compressed file in gzfilename.
154 *
155 * @example
156 * [ str s_buffer = gzfile_read("arhive.gz") ]
157 -----------------------------------------------------------------------------*/
158 func str gzfile_read<result>(str gzfilename)
159 {
160 buf tmp_buffer
161 str out_buffer =""
162 uint length_buf = 0
163 tmp_buffer.reserve(0x1000)
164
165 uint fileNum = gzopen(gzfilename.ptr(),"rb".ptr())
166
167 if (fileNum != 0) {
168 while (length_buf = (gzread(fileNum,tmp_buffer.ptr(),0x1000)) ) : out_buffer.append(tmp_buffer.ptr(),length_buf)
169 gzclose(fileNum)
170 }
171
172 result = out_buffer
173 }
174
175 /*-----------------------------------------------------------------------------
176 * @syntax [ gzfile_write (str gzfilename, str src_buffer) ]
177 *
178 * @return The number of bytes in src_buffer.
179 *
180 * Does a GZ compatible comrpression of a buffer in src_buffer and
181 * writes it to the file in gzfilename.
182 *
183 * @example
184 * [ uint cnt_bytes = gzfile_write("arhive.gz",src_buffer) ]
185 -----------------------------------------------------------------------------*/
186 func uint gzfile_write(str gzfilename, str src_buffer)
187 {
188 uint count_bytes = 0
189 uint fileNum = gzopen(gzfilename.ptr(),"wb".ptr())
190 print("1 \(fileNum) \n")
191 if (fileNum != 0) {
192 count_bytes = gzwrite(fileNum,src_buffer.ptr(),*src_buffer)
193 gzclose(fileNum)
194 }
195
196 return count_bytes
197 }
198
199