pr-zlib 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,385 +1,385 @@
1
- # example.rb -- usage example of the zlib compression library
2
- # Copyright (C) 1995-2004 Jean-loup Gailly.
3
- # For conditions of distribution and use, see copyright notice in rbzlib.rb
4
- #
5
- # Ruby translation by Park Heesob
6
-
7
- require 'rbzlib'
8
- include Rbzlib
9
-
10
- def CHECK_ERR(err,msg)
11
- if(err != Z_OK)
12
- raise RuntimeError,"#{msg} error: #{err}"
13
- end
14
- end
15
-
16
- TESTFILE = "foo.gz"
17
- @@hello = "hello, hello!\0"
18
-
19
- @@dictionary = "hello"
20
-
21
- def test_compress(compr, comprLen, uncompr, uncomprLen)
22
- len = @@hello.length
23
-
24
- err,comprLen = compress(compr, comprLen, @@hello, len)
25
- CHECK_ERR(err, "compress")
26
- compr = compr[0,comprLen]
27
- uncompr[0,7] = "garbage"
28
- err,uncomprLen = uncompress(uncompr, uncomprLen, compr, comprLen)
29
- CHECK_ERR(err, "uncompress")
30
- uncompr = uncompr[0,uncomprLen]
31
- if uncompr != @@hello
32
- puts("bad uncompress")
33
- exit(1)
34
- else
35
- puts("uncompress(): #{uncompr}")
36
- end
37
- end
38
-
39
- def test_gzio(fname, uncompr, uncomprLen)
40
- len = @@hello.length
41
- err = 0
42
- file = gzopen(fname, "wb")
43
- if file.nil?
44
- puts("gzopen error")
45
- exit(1)
46
- end
47
- gzputc(file, 'h')
48
- if (gzputs(file, "ello") != 4)
49
- puts("gzputs err: #{gzerror(file, err)}")
50
- exit(1)
51
- end
52
- if (gzputs(file, ", hello!") != 8)
53
- puts("gzputs err: #{gzerror(file, err)}")
54
- exit(1)
55
- end
56
- gzseek(file, 1, SEEK_CUR)
57
- gzclose(file)
58
-
59
- file = gzopen(fname, "rb")
60
- if file.nil?
61
- puts("gzopen error")
62
- exit(1)
63
- end
64
- uncompr[0,7] = "garbage"
65
- if (gzread(file, uncompr, uncomprLen) != len)
66
- puts("gzread err: #{gzerror(file, err)}")
67
- exit(1)
68
- end
69
- uncompr = uncompr[0,len]
70
- if uncompr != @@hello
71
- puts("bad gzread: #{uncompr}")
72
- exit(1)
73
- else
74
- puts("gzread(): #{uncompr}")
75
- end
76
- pos = gzseek(file, -8, SEEK_CUR)
77
- if (pos != 6 || gztell(file) != pos)
78
- puts("gzseek error, pos=#{pos}, gztell=#{gztell(file)}")
79
- exit(1)
80
- end
81
-
82
- if (gzgetc(file) != ' ')
83
- puts("gzgetc error")
84
- exit(1)
85
- end
86
-
87
- if (gzungetc(' ', file) != ' ')
88
- puts("gzungetc error")
89
- exit(1)
90
- end
91
-
92
- gzgets(file, uncompr, uncomprLen)
93
- uncompr.chop!
94
- if uncompr.length != 7
95
- puts("gzgets err after gzseek: #{gzerror(file, err)}")
96
- exit(1)
97
- end
98
-
99
- if uncompr != @@hello[6..-2]
100
- puts("bad gzgets after gzseek")
101
- exit(1)
102
- else
103
- puts("gzgets() after gzseek: #{uncompr}")
104
- end
105
-
106
- gzclose(file)
107
- end
108
-
109
- def test_deflate(compr, comprLen)
110
- c_stream = Z_stream.new
111
- len = @@hello.length
112
-
113
- err = deflateInit(c_stream, Z_DEFAULT_COMPRESSION)
114
- CHECK_ERR(err, "deflateInit")
115
-
116
- c_stream.next_in = Bytef.new(@@hello)
117
- c_stream.next_out = Bytef.new(compr)
118
-
119
- while (c_stream.total_in != len && c_stream.total_out < comprLen)
120
- c_stream.avail_in = c_stream.avail_out = 1
121
- err = deflate(c_stream, Z_NO_FLUSH)
122
- CHECK_ERR(err, "deflate")
123
- end
124
- while true
125
- c_stream.avail_out = 1
126
- err = deflate(c_stream, Z_FINISH)
127
- break if (err == Z_STREAM_END)
128
- CHECK_ERR(err, "deflate")
129
- end
130
-
131
- err = deflateEnd(c_stream)
132
- CHECK_ERR(err, "deflateEnd")
133
- end
134
-
135
- def test_inflate(compr, comprLen, uncompr, uncomprLen)
136
- uncompr[0,7] = "garbage"
137
- d_stream = Z_stream.new
138
-
139
- d_stream.next_in = Bytef.new(compr)
140
- d_stream.avail_in = 0
141
- d_stream.next_out = Bytef.new(uncompr)
142
-
143
- err = inflateInit(d_stream)
144
- CHECK_ERR(err, "inflateInit")
145
-
146
- while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen)
147
- d_stream.avail_in = d_stream.avail_out = 1
148
- err = inflate(d_stream, Z_NO_FLUSH)
149
- break if (err == Z_STREAM_END)
150
- CHECK_ERR(err, "inflate")
151
- end
152
-
153
- err = inflateEnd(d_stream)
154
- CHECK_ERR(err, "inflateEnd")
155
- uncompr = uncompr[0,d_stream.total_out]
156
- if uncompr != @@hello
157
- puts("bad inflate")
158
- exit(1)
159
- else
160
- puts("inflate(): #{uncompr}")
161
- end
162
- end
163
-
164
- def test_large_deflate(compr, comprLen, uncompr, uncomprLen)
165
- c_stream = Z_stream.new
166
- err = deflateInit(c_stream, Z_BEST_SPEED)
167
- CHECK_ERR(err, "deflateInit")
168
-
169
- c_stream.next_out = Bytef.new(compr)
170
- c_stream.avail_out = comprLen
171
- c_stream.next_in = Bytef.new(uncompr)
172
- c_stream.avail_in = uncomprLen
173
- err = deflate(c_stream, Z_NO_FLUSH)
174
- CHECK_ERR(err, "deflate")
175
- if c_stream.avail_in.nonzero?
176
- puts("deflate not greedy")
177
- exit(1)
178
- end
179
-
180
- deflateParams(c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY)
181
- c_stream.next_in = Bytef.new(compr)
182
- c_stream.avail_in = comprLen/2
183
- err = deflate(c_stream, Z_NO_FLUSH)
184
- CHECK_ERR(err, "deflate")
185
-
186
- deflateParams(c_stream, Z_BEST_COMPRESSION, Z_FILTERED)
187
- c_stream.next_in = Bytef.new(uncompr)
188
- c_stream.avail_in = uncomprLen
189
- err = deflate(c_stream, Z_NO_FLUSH)
190
- CHECK_ERR(err, "deflate")
191
-
192
- err = deflate(c_stream, Z_FINISH)
193
- if (err != Z_STREAM_END)
194
- puts("deflate should report Z_STREAM_END")
195
- exit(1)
196
- end
197
- err = deflateEnd(c_stream)
198
- CHECK_ERR(err, "deflateEnd")
199
- end
200
-
201
- def test_large_inflate(compr, comprLen, uncompr, uncomprLen)
202
- d_stream = Z_stream.new
203
- uncompr[0,7] = "garbage"
204
-
205
- d_stream.next_in = Bytef.new(compr)
206
- d_stream.avail_in = comprLen
207
-
208
- err = inflateInit(d_stream)
209
- CHECK_ERR(err, "inflateInit")
210
-
211
- while true
212
- d_stream.next_out = Bytef.new(uncompr)
213
- d_stream.avail_out = uncomprLen
214
- err = inflate(d_stream, Z_NO_FLUSH)
215
- break if (err == Z_STREAM_END)
216
- CHECK_ERR(err, "large inflate")
217
- end
218
-
219
- err = inflateEnd(d_stream)
220
- CHECK_ERR(err, "inflateEnd")
221
-
222
- if (d_stream.total_out != 2*uncomprLen + comprLen/2)
223
- puts("bad large inflate: #{d_stream.total_out}")
224
- exit(1)
225
- else
226
- puts("large_inflate(): OK")
227
- end
228
- end
229
-
230
- def test_flush(compr, comprLen)
231
- c_stream = Z_stream.new
232
- len = @@hello.length
233
-
234
- err = deflateInit(c_stream, Z_DEFAULT_COMPRESSION)
235
- CHECK_ERR(err, "deflateInit")
236
-
237
- c_stream.next_in = Bytef.new(@@hello)
238
- c_stream.next_out = Bytef.new(compr)
239
- c_stream.avail_in = 3
240
- c_stream.avail_out = comprLen
241
- err = deflate(c_stream, Z_FULL_FLUSH)
242
- CHECK_ERR(err, "deflate")
243
-
244
- compr[3]=(compr[3].ord+1).chr
245
- c_stream.avail_in = len - 3
246
-
247
- err = deflate(c_stream, Z_FINISH)
248
- if (err != Z_STREAM_END)
249
- CHECK_ERR(err, "deflate")
250
- end
251
- err = deflateEnd(c_stream)
252
- CHECK_ERR(err, "deflateEnd")
253
-
254
- comprLen = c_stream.total_out
255
- end
256
-
257
- def test_sync(compr, comprLen, uncompr, uncomprLen)
258
- d_stream = Z_stream.new
259
- uncompr[0,7] = "garbage"
260
-
261
- d_stream.next_in = Bytef.new(compr)
262
- d_stream.avail_in = 2
263
-
264
- err = inflateInit(d_stream)
265
- CHECK_ERR(err, "inflateInit")
266
-
267
- d_stream.next_out = Bytef.new(uncompr)
268
- d_stream.avail_out = uncomprLen
269
-
270
- inflate(d_stream, Z_NO_FLUSH)
271
- CHECK_ERR(err, "inflate")
272
-
273
- d_stream.avail_in = comprLen-2
274
- err = inflateSync(d_stream)
275
- CHECK_ERR(err, "inflateSync")
276
-
277
- err = inflate(d_stream, Z_FINISH)
278
- if (err != Z_DATA_ERROR)
279
- puts("inflate should report DATA_ERROR")
280
- exit(1)
281
- end
282
- err = inflateEnd(d_stream)
283
- uncompr = uncompr[0,d_stream.total_out]
284
- CHECK_ERR(err, "inflateEnd")
285
- puts("after inflateSync(): hel#{uncompr}")
286
- end
287
-
288
- def test_dict_deflate(compr, comprLen)
289
- c_stream = Z_stream.new
290
- err = deflateInit(c_stream, Z_BEST_COMPRESSION)
291
- CHECK_ERR(err, "deflateInit")
292
-
293
- err = deflateSetDictionary(c_stream,@@dictionary, @@dictionary.length)
294
- CHECK_ERR(err, "deflateSetDictionary")
295
-
296
- @@dictId = c_stream.adler
297
- c_stream.next_out = Bytef.new(compr)
298
- c_stream.avail_out = comprLen
299
-
300
- c_stream.next_in = Bytef.new(@@hello)
301
- c_stream.avail_in = @@hello.length
302
-
303
- err = deflate(c_stream, Z_FINISH)
304
- if (err != Z_STREAM_END)
305
- puts("deflate should report Z_STREAM_END")
306
- exit(1)
307
- end
308
- err = deflateEnd(c_stream)
309
- CHECK_ERR(err, "deflateEnd")
310
- end
311
-
312
- def test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
313
- d_stream = Z_stream.new
314
- uncompr[0,7] = "garbage"
315
-
316
- d_stream.next_in = Bytef.new(compr)
317
- d_stream.avail_in = comprLen
318
-
319
- err = inflateInit(d_stream)
320
- CHECK_ERR(err, "inflateInit")
321
- d_stream.next_out = Bytef.new(uncompr)
322
- d_stream.avail_out = uncomprLen
323
-
324
- while true
325
- err = inflate(d_stream, Z_NO_FLUSH)
326
- break if (err == Z_STREAM_END)
327
- if (err == Z_NEED_DICT)
328
- if (d_stream.adler != @@dictId)
329
- puts("unexpected dictionary")
330
- exit(1)
331
- end
332
- err = inflateSetDictionary(d_stream, @@dictionary,@@dictionary.length)
333
-
334
- end
335
- CHECK_ERR(err, "inflate with dict")
336
- end
337
-
338
- err = inflateEnd(d_stream)
339
- CHECK_ERR(err, "inflateEnd")
340
- uncompr = uncompr[0,d_stream.total_out]
341
- if uncompr != @@hello
342
- puts("bad inflate with dict")
343
- exit(1)
344
- else
345
- puts("inflate with dictionary: #{uncompr}")
346
- end
347
- end
348
-
349
- comprLen = 10000*4
350
- uncomprLen = comprLen
351
- myVersion = ZLIB_VERSION
352
-
353
- if (zlibVersion[0] != myVersion[0])
354
- puts("incompatible zlib version")
355
- exit(1)
356
- elsif (zlibVersion != ZLIB_VERSION)
357
- puts("warning: different zlib version")
358
- end
359
-
360
- compr = 0.chr * comprLen
361
- uncompr = 0.chr * uncomprLen
362
- if (compr.nil? || uncompr.nil?)
363
- puts("out of memory")
364
- exit(1)
365
- end
366
- test_compress(compr, comprLen, uncompr, uncomprLen)
367
-
368
- test_gzio((ARGV.length > 0 ? ARGV[0] : TESTFILE),
369
- uncompr, uncomprLen)
370
-
371
- test_deflate(compr, comprLen)
372
- test_inflate(compr, comprLen, uncompr, uncomprLen)
373
- test_large_deflate(compr, comprLen, uncompr, uncomprLen)
374
- test_large_inflate(compr, comprLen, uncompr, uncomprLen)
375
-
376
- test_flush(compr, comprLen)
377
-
378
- test_sync(compr, comprLen, uncompr, uncomprLen)
379
- comprLen = uncomprLen
380
-
381
- test_dict_deflate(compr, comprLen)
382
- test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
383
-
384
-
385
-
1
+ # example.rb -- usage example of the zlib compression library
2
+ # Copyright (C) 1995-2004 Jean-loup Gailly.
3
+ # For conditions of distribution and use, see copyright notice in rbzlib.rb
4
+ #
5
+ # Ruby translation by Park Heesob
6
+
7
+ require 'rbzlib'
8
+ include Rbzlib
9
+
10
+ def CHECK_ERR(err,msg)
11
+ if(err != Z_OK)
12
+ raise RuntimeError,"#{msg} error: #{err}"
13
+ end
14
+ end
15
+
16
+ TESTFILE = "foo.gz"
17
+ @@hello = "hello, hello!\0"
18
+
19
+ @@dictionary = "hello"
20
+
21
+ def test_compress(compr, comprLen, uncompr, uncomprLen)
22
+ len = @@hello.length
23
+
24
+ err,comprLen = compress(compr, comprLen, @@hello, len)
25
+ CHECK_ERR(err, "compress")
26
+ compr = compr[0,comprLen]
27
+ uncompr[0,7] = "garbage"
28
+ err,uncomprLen = uncompress(uncompr, uncomprLen, compr, comprLen)
29
+ CHECK_ERR(err, "uncompress")
30
+ uncompr = uncompr[0,uncomprLen]
31
+ if uncompr != @@hello
32
+ puts("bad uncompress")
33
+ exit(1)
34
+ else
35
+ puts("uncompress(): #{uncompr}")
36
+ end
37
+ end
38
+
39
+ def test_gzio(fname, uncompr, uncomprLen)
40
+ len = @@hello.length
41
+ err = 0
42
+ file = gzopen(fname, "wb")
43
+ if file.nil?
44
+ puts("gzopen error")
45
+ exit(1)
46
+ end
47
+ gzputc(file, 'h')
48
+ if (gzputs(file, "ello") != 4)
49
+ puts("gzputs err: #{gzerror(file, err)}")
50
+ exit(1)
51
+ end
52
+ if (gzputs(file, ", hello!") != 8)
53
+ puts("gzputs err: #{gzerror(file, err)}")
54
+ exit(1)
55
+ end
56
+ gzseek(file, 1, SEEK_CUR)
57
+ gzclose(file)
58
+
59
+ file = gzopen(fname, "rb")
60
+ if file.nil?
61
+ puts("gzopen error")
62
+ exit(1)
63
+ end
64
+ uncompr[0,7] = "garbage"
65
+ if (gzread(file, uncompr, uncomprLen) != len)
66
+ puts("gzread err: #{gzerror(file, err)}")
67
+ exit(1)
68
+ end
69
+ uncompr = uncompr[0,len]
70
+ if uncompr != @@hello
71
+ puts("bad gzread: #{uncompr}")
72
+ exit(1)
73
+ else
74
+ puts("gzread(): #{uncompr}")
75
+ end
76
+ pos = gzseek(file, -8, SEEK_CUR)
77
+ if (pos != 6 || gztell(file) != pos)
78
+ puts("gzseek error, pos=#{pos}, gztell=#{gztell(file)}")
79
+ exit(1)
80
+ end
81
+
82
+ if (gzgetc(file) != ' ')
83
+ puts("gzgetc error")
84
+ exit(1)
85
+ end
86
+
87
+ if (gzungetc(' ', file) != ' ')
88
+ puts("gzungetc error")
89
+ exit(1)
90
+ end
91
+
92
+ gzgets(file, uncompr, uncomprLen)
93
+ uncompr.chop!
94
+ if uncompr.length != 7
95
+ puts("gzgets err after gzseek: #{gzerror(file, err)}")
96
+ exit(1)
97
+ end
98
+
99
+ if uncompr != @@hello[6..-2]
100
+ puts("bad gzgets after gzseek")
101
+ exit(1)
102
+ else
103
+ puts("gzgets() after gzseek: #{uncompr}")
104
+ end
105
+
106
+ gzclose(file)
107
+ end
108
+
109
+ def test_deflate(compr, comprLen)
110
+ c_stream = Z_stream.new
111
+ len = @@hello.length
112
+
113
+ err = deflateInit(c_stream, Z_DEFAULT_COMPRESSION)
114
+ CHECK_ERR(err, "deflateInit")
115
+
116
+ c_stream.next_in = Bytef.new(@@hello)
117
+ c_stream.next_out = Bytef.new(compr)
118
+
119
+ while (c_stream.total_in != len && c_stream.total_out < comprLen)
120
+ c_stream.avail_in = c_stream.avail_out = 1
121
+ err = deflate(c_stream, Z_NO_FLUSH)
122
+ CHECK_ERR(err, "deflate")
123
+ end
124
+ while true
125
+ c_stream.avail_out = 1
126
+ err = deflate(c_stream, Z_FINISH)
127
+ break if (err == Z_STREAM_END)
128
+ CHECK_ERR(err, "deflate")
129
+ end
130
+
131
+ err = deflateEnd(c_stream)
132
+ CHECK_ERR(err, "deflateEnd")
133
+ end
134
+
135
+ def test_inflate(compr, comprLen, uncompr, uncomprLen)
136
+ uncompr[0,7] = "garbage"
137
+ d_stream = Z_stream.new
138
+
139
+ d_stream.next_in = Bytef.new(compr)
140
+ d_stream.avail_in = 0
141
+ d_stream.next_out = Bytef.new(uncompr)
142
+
143
+ err = inflateInit(d_stream)
144
+ CHECK_ERR(err, "inflateInit")
145
+
146
+ while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen)
147
+ d_stream.avail_in = d_stream.avail_out = 1
148
+ err = inflate(d_stream, Z_NO_FLUSH)
149
+ break if (err == Z_STREAM_END)
150
+ CHECK_ERR(err, "inflate")
151
+ end
152
+
153
+ err = inflateEnd(d_stream)
154
+ CHECK_ERR(err, "inflateEnd")
155
+ uncompr = uncompr[0,d_stream.total_out]
156
+ if uncompr != @@hello
157
+ puts("bad inflate")
158
+ exit(1)
159
+ else
160
+ puts("inflate(): #{uncompr}")
161
+ end
162
+ end
163
+
164
+ def test_large_deflate(compr, comprLen, uncompr, uncomprLen)
165
+ c_stream = Z_stream.new
166
+ err = deflateInit(c_stream, Z_BEST_SPEED)
167
+ CHECK_ERR(err, "deflateInit")
168
+
169
+ c_stream.next_out = Bytef.new(compr)
170
+ c_stream.avail_out = comprLen
171
+ c_stream.next_in = Bytef.new(uncompr)
172
+ c_stream.avail_in = uncomprLen
173
+ err = deflate(c_stream, Z_NO_FLUSH)
174
+ CHECK_ERR(err, "deflate")
175
+ if c_stream.avail_in.nonzero?
176
+ puts("deflate not greedy")
177
+ exit(1)
178
+ end
179
+
180
+ deflateParams(c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY)
181
+ c_stream.next_in = Bytef.new(compr)
182
+ c_stream.avail_in = comprLen/2
183
+ err = deflate(c_stream, Z_NO_FLUSH)
184
+ CHECK_ERR(err, "deflate")
185
+
186
+ deflateParams(c_stream, Z_BEST_COMPRESSION, Z_FILTERED)
187
+ c_stream.next_in = Bytef.new(uncompr)
188
+ c_stream.avail_in = uncomprLen
189
+ err = deflate(c_stream, Z_NO_FLUSH)
190
+ CHECK_ERR(err, "deflate")
191
+
192
+ err = deflate(c_stream, Z_FINISH)
193
+ if (err != Z_STREAM_END)
194
+ puts("deflate should report Z_STREAM_END")
195
+ exit(1)
196
+ end
197
+ err = deflateEnd(c_stream)
198
+ CHECK_ERR(err, "deflateEnd")
199
+ end
200
+
201
+ def test_large_inflate(compr, comprLen, uncompr, uncomprLen)
202
+ d_stream = Z_stream.new
203
+ uncompr[0,7] = "garbage"
204
+
205
+ d_stream.next_in = Bytef.new(compr)
206
+ d_stream.avail_in = comprLen
207
+
208
+ err = inflateInit(d_stream)
209
+ CHECK_ERR(err, "inflateInit")
210
+
211
+ while true
212
+ d_stream.next_out = Bytef.new(uncompr)
213
+ d_stream.avail_out = uncomprLen
214
+ err = inflate(d_stream, Z_NO_FLUSH)
215
+ break if (err == Z_STREAM_END)
216
+ CHECK_ERR(err, "large inflate")
217
+ end
218
+
219
+ err = inflateEnd(d_stream)
220
+ CHECK_ERR(err, "inflateEnd")
221
+
222
+ if (d_stream.total_out != 2*uncomprLen + comprLen/2)
223
+ puts("bad large inflate: #{d_stream.total_out}")
224
+ exit(1)
225
+ else
226
+ puts("large_inflate(): OK")
227
+ end
228
+ end
229
+
230
+ def test_flush(compr, comprLen)
231
+ c_stream = Z_stream.new
232
+ len = @@hello.length
233
+
234
+ err = deflateInit(c_stream, Z_DEFAULT_COMPRESSION)
235
+ CHECK_ERR(err, "deflateInit")
236
+
237
+ c_stream.next_in = Bytef.new(@@hello)
238
+ c_stream.next_out = Bytef.new(compr)
239
+ c_stream.avail_in = 3
240
+ c_stream.avail_out = comprLen
241
+ err = deflate(c_stream, Z_FULL_FLUSH)
242
+ CHECK_ERR(err, "deflate")
243
+
244
+ compr[3]=(compr[3].ord+1).chr
245
+ c_stream.avail_in = len - 3
246
+
247
+ err = deflate(c_stream, Z_FINISH)
248
+ if (err != Z_STREAM_END)
249
+ CHECK_ERR(err, "deflate")
250
+ end
251
+ err = deflateEnd(c_stream)
252
+ CHECK_ERR(err, "deflateEnd")
253
+
254
+ comprLen = c_stream.total_out
255
+ end
256
+
257
+ def test_sync(compr, comprLen, uncompr, uncomprLen)
258
+ d_stream = Z_stream.new
259
+ uncompr[0,7] = "garbage"
260
+
261
+ d_stream.next_in = Bytef.new(compr)
262
+ d_stream.avail_in = 2
263
+
264
+ err = inflateInit(d_stream)
265
+ CHECK_ERR(err, "inflateInit")
266
+
267
+ d_stream.next_out = Bytef.new(uncompr)
268
+ d_stream.avail_out = uncomprLen
269
+
270
+ inflate(d_stream, Z_NO_FLUSH)
271
+ CHECK_ERR(err, "inflate")
272
+
273
+ d_stream.avail_in = comprLen-2
274
+ err = inflateSync(d_stream)
275
+ CHECK_ERR(err, "inflateSync")
276
+
277
+ err = inflate(d_stream, Z_FINISH)
278
+ if (err != Z_DATA_ERROR)
279
+ puts("inflate should report DATA_ERROR")
280
+ exit(1)
281
+ end
282
+ err = inflateEnd(d_stream)
283
+ uncompr = uncompr[0,d_stream.total_out]
284
+ CHECK_ERR(err, "inflateEnd")
285
+ puts("after inflateSync(): hel#{uncompr}")
286
+ end
287
+
288
+ def test_dict_deflate(compr, comprLen)
289
+ c_stream = Z_stream.new
290
+ err = deflateInit(c_stream, Z_BEST_COMPRESSION)
291
+ CHECK_ERR(err, "deflateInit")
292
+
293
+ err = deflateSetDictionary(c_stream,@@dictionary, @@dictionary.length)
294
+ CHECK_ERR(err, "deflateSetDictionary")
295
+
296
+ @@dictId = c_stream.adler
297
+ c_stream.next_out = Bytef.new(compr)
298
+ c_stream.avail_out = comprLen
299
+
300
+ c_stream.next_in = Bytef.new(@@hello)
301
+ c_stream.avail_in = @@hello.length
302
+
303
+ err = deflate(c_stream, Z_FINISH)
304
+ if (err != Z_STREAM_END)
305
+ puts("deflate should report Z_STREAM_END")
306
+ exit(1)
307
+ end
308
+ err = deflateEnd(c_stream)
309
+ CHECK_ERR(err, "deflateEnd")
310
+ end
311
+
312
+ def test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
313
+ d_stream = Z_stream.new
314
+ uncompr[0,7] = "garbage"
315
+
316
+ d_stream.next_in = Bytef.new(compr)
317
+ d_stream.avail_in = comprLen
318
+
319
+ err = inflateInit(d_stream)
320
+ CHECK_ERR(err, "inflateInit")
321
+ d_stream.next_out = Bytef.new(uncompr)
322
+ d_stream.avail_out = uncomprLen
323
+
324
+ while true
325
+ err = inflate(d_stream, Z_NO_FLUSH)
326
+ break if (err == Z_STREAM_END)
327
+ if (err == Z_NEED_DICT)
328
+ if (d_stream.adler != @@dictId)
329
+ puts("unexpected dictionary")
330
+ exit(1)
331
+ end
332
+ err = inflateSetDictionary(d_stream, @@dictionary,@@dictionary.length)
333
+
334
+ end
335
+ CHECK_ERR(err, "inflate with dict")
336
+ end
337
+
338
+ err = inflateEnd(d_stream)
339
+ CHECK_ERR(err, "inflateEnd")
340
+ uncompr = uncompr[0,d_stream.total_out]
341
+ if uncompr != @@hello
342
+ puts("bad inflate with dict")
343
+ exit(1)
344
+ else
345
+ puts("inflate with dictionary: #{uncompr}")
346
+ end
347
+ end
348
+
349
+ comprLen = 10000*4
350
+ uncomprLen = comprLen
351
+ myVersion = ZLIB_VERSION
352
+
353
+ if (zlibVersion[0] != myVersion[0])
354
+ puts("incompatible zlib version")
355
+ exit(1)
356
+ elsif (zlibVersion != ZLIB_VERSION)
357
+ puts("warning: different zlib version")
358
+ end
359
+
360
+ compr = 0.chr * comprLen
361
+ uncompr = 0.chr * uncomprLen
362
+ if (compr.nil? || uncompr.nil?)
363
+ puts("out of memory")
364
+ exit(1)
365
+ end
366
+ test_compress(compr, comprLen, uncompr, uncomprLen)
367
+
368
+ test_gzio((ARGV.length > 0 ? ARGV[0] : TESTFILE),
369
+ uncompr, uncomprLen)
370
+
371
+ test_deflate(compr, comprLen)
372
+ test_inflate(compr, comprLen, uncompr, uncomprLen)
373
+ test_large_deflate(compr, comprLen, uncompr, uncomprLen)
374
+ test_large_inflate(compr, comprLen, uncompr, uncomprLen)
375
+
376
+ test_flush(compr, comprLen)
377
+
378
+ test_sync(compr, comprLen, uncompr, uncomprLen)
379
+ comprLen = uncomprLen
380
+
381
+ test_dict_deflate(compr, comprLen)
382
+ test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
383
+
384
+
385
+