-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlib.c
3536 lines (3123 loc) · 85.9 KB
/
zlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* zlib.c - An interface for zlib.
*
* Copyright (C) UENO Katsuhiro 2000-2003
*
* $Id: zlib.c,v 1.7.2.17 2005/12/14 16:37:56 akr Exp $
*/
#include <ruby.h>
#include <zlib.h>
#include <time.h>
#define RUBY_ZLIB_VERSION "0.6.0"
#define OBJ_IS_FREED(val) (RBASIC(val)->flags == 0)
#ifndef GZIP_SUPPORT
#define GZIP_SUPPORT 1
#endif
/* from zutil.h */
#ifndef DEF_MEM_LEVEL
#if MAX_MEM_LEVEL >= 8
#define DEF_MEM_LEVEL 8
#else
#define DEF_MEM_LEVEL MAX_MEM_LEVEL
#endif
#endif
/*--------- Prototypes --------*/
static NORETURN(void raise_zlib_error _((int, const char *)));
static VALUE rb_zlib_version _((VALUE));
static VALUE do_checksum _((int, VALUE*, uLong (*) _((uLong, const Bytef*, uInt))));
static VALUE rb_zlib_adler32 _((int, VALUE*, VALUE));
static VALUE rb_zlib_crc32 _((int, VALUE*, VALUE));
static VALUE rb_zlib_crc_table _((VALUE));
static voidpf zlib_mem_alloc _((voidpf, uInt, uInt));
static void zlib_mem_free _((voidpf, voidpf));
struct zstream;
struct zstream_funcs;
static void zstream_init _((struct zstream*, const struct zstream_funcs *));
static void zstream_expand_buffer _((struct zstream*));
static void zstream_expand_buffer_into _((struct zstream*, int));
static void zstream_append_buffer _((struct zstream*, const char*, int));
static VALUE zstream_detach_buffer _((struct zstream*));
static VALUE zstream_shift_buffer _((struct zstream*, int));
static void zstream_buffer_ungetc _((struct zstream*, int));
static void zstream_append_input _((struct zstream*, const char*, unsigned int));
static void zstream_discard_input _((struct zstream*, unsigned int));
static void zstream_reset_input _((struct zstream*));
static void zstream_passthrough_input _((struct zstream*));
static VALUE zstream_detach_input _((struct zstream*));
static void zstream_reset _((struct zstream*));
static VALUE zstream_end _((struct zstream*));
static void zstream_run _((struct zstream*, Bytef*, uInt, int));
static VALUE zstream_sync _((struct zstream*, Bytef*, uInt));
static void zstream_mark _((struct zstream*));
static void zstream_free _((struct zstream*));
static VALUE zstream_new _((VALUE, const struct zstream_funcs*));
static struct zstream *get_zstream _((VALUE));
static VALUE rb_zstream_end _((VALUE));
static VALUE rb_zstream_reset _((VALUE));
static VALUE rb_zstream_finish _((VALUE));
static VALUE rb_zstream_flush_next_in _((VALUE));
static VALUE rb_zstream_flush_next_out _((VALUE));
static VALUE rb_zstream_avail_out _((VALUE));
static VALUE rb_zstream_set_avail_out _((VALUE, VALUE));
static VALUE rb_zstream_avail_in _((VALUE));
static VALUE rb_zstream_total_in _((VALUE));
static VALUE rb_zstream_total_out _((VALUE));
static VALUE rb_zstream_data_type _((VALUE));
static VALUE rb_zstream_adler _((VALUE));
static VALUE rb_zstream_finished_p _((VALUE));
static VALUE rb_zstream_closed_p _((VALUE));
static VALUE rb_deflate_s_allocate _((VALUE));
static VALUE rb_deflate_initialize _((int, VALUE*, VALUE));
static VALUE rb_deflate_init_copy _((VALUE, VALUE));
static VALUE deflate_run _((VALUE));
static VALUE rb_deflate_s_deflate _((int, VALUE*, VALUE));
static void do_deflate _((struct zstream*, VALUE, int));
static VALUE rb_deflate_deflate _((int, VALUE*, VALUE));
static VALUE rb_deflate_addstr _((VALUE, VALUE));
static VALUE rb_deflate_flush _((int, VALUE*, VALUE));
static VALUE rb_deflate_params _((VALUE, VALUE, VALUE));
static VALUE rb_deflate_set_dictionary _((VALUE, VALUE));
static VALUE inflate_run _((VALUE));
static VALUE rb_inflate_s_allocate _((VALUE));
static VALUE rb_inflate_initialize _((int, VALUE*, VALUE));
static VALUE rb_inflate_s_inflate _((VALUE, VALUE));
static void do_inflate _((struct zstream*, VALUE));
static VALUE rb_inflate_inflate _((VALUE, VALUE));
static VALUE rb_inflate_addstr _((VALUE, VALUE));
static VALUE rb_inflate_sync _((VALUE, VALUE));
static VALUE rb_inflate_sync_point_p _((VALUE));
static VALUE rb_inflate_set_dictionary _((VALUE, VALUE));
#if GZIP_SUPPORT
struct gzfile;
static void gzfile_mark _((struct gzfile*));
static void gzfile_free _((struct gzfile*));
static VALUE gzfile_new _((VALUE, const struct zstream_funcs*, void (*) _((struct gzfile*))));
static void gzfile_reset _((struct gzfile*));
static void gzfile_close _((struct gzfile*, int));
static void gzfile_write_raw _((struct gzfile*));
static VALUE gzfile_read_raw _((struct gzfile*));
static int gzfile_read_raw_ensure _((struct gzfile*, int));
static char *gzfile_read_raw_until_zero _((struct gzfile*, long));
static unsigned int gzfile_get16 _((const unsigned char*));
static unsigned long gzfile_get32 _((const unsigned char*));
static void gzfile_set32 _((unsigned long n, unsigned char*));
static void gzfile_make_header _((struct gzfile*));
static void gzfile_make_footer _((struct gzfile*));
static void gzfile_read_header _((struct gzfile*));
static void gzfile_check_footer _((struct gzfile*));
static void gzfile_write _((struct gzfile*, Bytef*, uInt));
static long gzfile_read_more _((struct gzfile*));
static void gzfile_calc_crc _((struct gzfile*, VALUE));
static VALUE gzfile_read _((struct gzfile*, int));
static VALUE gzfile_read_all _((struct gzfile*));
static void gzfile_ungetc _((struct gzfile*, int));
static VALUE gzfile_finalize _((VALUE));
static void gzfile_writer_end _((struct gzfile*));
static void gzfile_reader_end _((struct gzfile*));
static void gzfile_reader_rewind _((struct gzfile*));
static VALUE gzfile_reader_get_unused _((struct gzfile*));
static struct gzfile *get_gzfile _((VALUE));
static VALUE gzfile_ensure_close _((VALUE));
static VALUE rb_gzfile_s_wrap _((int, VALUE*, VALUE));
static VALUE gzfile_s_open _((int, VALUE*, VALUE, const char*));
static VALUE rb_gzfile_to_io _((VALUE));
static VALUE rb_gzfile_crc _((VALUE));
static VALUE rb_gzfile_mtime _((VALUE));
static VALUE rb_gzfile_level _((VALUE));
static VALUE rb_gzfile_os_code _((VALUE));
static VALUE rb_gzfile_orig_name _((VALUE));
static VALUE rb_gzfile_comment _((VALUE));
static VALUE rb_gzfile_lineno _((VALUE));
static VALUE rb_gzfile_set_lineno _((VALUE, VALUE));
static VALUE rb_gzfile_set_mtime _((VALUE, VALUE));
static VALUE rb_gzfile_set_orig_name _((VALUE, VALUE));
static VALUE rb_gzfile_set_comment _((VALUE, VALUE));
static VALUE rb_gzfile_close _((VALUE));
static VALUE rb_gzfile_finish _((VALUE));
static VALUE rb_gzfile_closed_p _((VALUE));
static VALUE rb_gzfile_eof_p _((VALUE));
static VALUE rb_gzfile_sync _((VALUE));
static VALUE rb_gzfile_set_sync _((VALUE, VALUE));
static VALUE rb_gzfile_total_in _((VALUE));
static VALUE rb_gzfile_total_out _((VALUE));
static VALUE rb_gzwriter_s_allocate _((VALUE));
static VALUE rb_gzwriter_s_open _((int, VALUE*, VALUE));
static VALUE rb_gzwriter_initialize _((int, VALUE*, VALUE));
static VALUE rb_gzwriter_flush _((int, VALUE*, VALUE));
static VALUE rb_gzwriter_write _((VALUE, VALUE));
static VALUE rb_gzwriter_putc _((VALUE, VALUE));
static VALUE rb_gzreader_s_allocate _((VALUE));
static VALUE rb_gzreader_s_open _((int, VALUE*, VALUE));
static VALUE rb_gzreader_initialize _((VALUE, VALUE));
static VALUE rb_gzreader_rewind _((VALUE));
static VALUE rb_gzreader_unused _((VALUE));
static VALUE rb_gzreader_read _((int, VALUE*, VALUE));
static VALUE rb_gzreader_getc _((VALUE));
static VALUE rb_gzreader_readchar _((VALUE));
static VALUE rb_gzreader_each_byte _((VALUE));
static VALUE rb_gzreader_ungetc _((VALUE, VALUE));
static void gzreader_skip_linebreaks _((struct gzfile*));
static VALUE gzreader_gets _((int, VALUE*, VALUE));
static VALUE rb_gzreader_gets _((int, VALUE*, VALUE));
static VALUE rb_gzreader_readline _((int, VALUE*, VALUE));
static VALUE rb_gzreader_each _((int, VALUE*, VALUE));
static VALUE rb_gzreader_readlines _((int, VALUE*, VALUE));
#endif /* GZIP_SUPPORT */
void Init_zlib _((void));
/*--------- Exceptions --------*/
static VALUE cZError, cStreamEnd, cNeedDict;
static VALUE cStreamError, cDataError, cMemError, cBufError, cVersionError;
static void
raise_zlib_error(err, msg)
int err;
const char *msg;
{
VALUE exc;
if (!msg) {
msg = zError(err);
}
switch(err) {
case Z_STREAM_END:
exc = rb_exc_new2(cStreamEnd, msg);
break;
case Z_NEED_DICT:
exc = rb_exc_new2(cNeedDict, msg);
break;
case Z_STREAM_ERROR:
exc = rb_exc_new2(cStreamError, msg);
break;
case Z_DATA_ERROR:
exc = rb_exc_new2(cDataError, msg);
break;
case Z_BUF_ERROR:
exc = rb_exc_new2(cBufError, msg);
break;
case Z_VERSION_ERROR:
exc = rb_exc_new2(cVersionError, msg);
break;
case Z_MEM_ERROR:
exc = rb_exc_new2(cMemError, msg);
break;
case Z_ERRNO:
rb_sys_fail(msg);
/* no return */
default:
{
char buf[BUFSIZ];
snprintf(buf, BUFSIZ, "unknown zlib error %d: %s", err, msg);
exc = rb_exc_new2(cZError, buf);
}
}
rb_exc_raise(exc);
}
/*-------- module Zlib --------*/
/*
* Returns the string which represents the version of zlib library.
*/
static VALUE
rb_zlib_version(klass)
VALUE klass;
{
VALUE str;
str = rb_str_new2(zlibVersion());
OBJ_TAINT(str); /* for safe */
return str;
}
static VALUE
do_checksum(argc, argv, func)
int argc;
VALUE *argv;
uLong (*func) _((uLong, const Bytef *, uInt));
{
VALUE str, vsum;
unsigned long sum;
rb_scan_args(argc, argv, "02", &str, &vsum);
if (!NIL_P(vsum)) {
sum = NUM2ULONG(vsum);
}
else if (NIL_P(str)) {
sum = 0;
}
else {
sum = func(0, Z_NULL, 0);
}
if (NIL_P(str)) {
sum = func(sum, Z_NULL, 0);
}
else {
StringValue(str);
sum = func(sum, RSTRING(str)->ptr, RSTRING(str)->len);
}
return rb_uint2inum(sum);
}
/*
* call-seq: Zlib.adler32(string, adler)
*
* Calculates Alder-32 checksum for +string+, and returns updated value of
* +adler+. If +string+ is omitted, it returns the Adler-32 initial value. If
* +adler+ is omitted, it assumes that the initial value is given to +adler+.
*
* FIXME: expression.
*/
static VALUE
rb_zlib_adler32(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
return do_checksum(argc, argv, adler32);
}
/*
* call-seq: Zlib.crc32(string, adler)
*
* Calculates CRC checksum for +string+, and returns updated value of +crc+. If
* +string+ is omitted, it returns the CRC initial value. If +crc+ is omitted, it
* assumes that the initial value is given to +crc+.
*
* FIXME: expression.
*/
static VALUE
rb_zlib_crc32(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
return do_checksum(argc, argv, crc32);
}
/*
* Returns the table for calculating CRC checksum as an array.
*/
static VALUE
rb_zlib_crc_table(obj)
VALUE obj;
{
const unsigned long *crctbl;
VALUE dst;
int i;
crctbl = get_crc_table();
dst = rb_ary_new2(256);
for (i = 0; i < 256; i++) {
rb_ary_push(dst, rb_uint2inum(crctbl[i]));
}
return dst;
}
/*-------- zstream - internal APIs --------*/
struct zstream {
unsigned long flags;
VALUE buf;
long buf_filled;
VALUE input;
z_stream stream;
const struct zstream_funcs {
int (*reset) _((z_streamp));
int (*end) _((z_streamp));
int (*run) _((z_streamp, int));
} *func;
};
#define ZSTREAM_FLAG_READY 0x1
#define ZSTREAM_FLAG_IN_STREAM 0x2
#define ZSTREAM_FLAG_FINISHED 0x4
#define ZSTREAM_FLAG_FINALIZE 0x8
#define ZSTREAM_FLAG_CLOSED 0x10
#define ZSTREAM_FLAG_UNUSED 0x20
#define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY)
#define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY)
#define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED)
#define ZSTREAM_IS_FINALIZE(z) ((z)->flags & ZSTREAM_FLAG_FINALIZE)
#define ZSTREAM_IS_CLOSED(z) ((z)->flags & ZSTREAM_FLAG_CLOSED)
/* I think that more better value should be found,
but I gave up finding it. B) */
#define ZSTREAM_INITIAL_BUFSIZE 1024
#define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
#define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
static const struct zstream_funcs deflate_funcs = {
deflateReset, deflateEnd, deflate,
};
static const struct zstream_funcs inflate_funcs = {
inflateReset, inflateEnd, inflate,
};
static voidpf
zlib_mem_alloc(opaque, items, size)
voidpf opaque;
uInt items, size;
{
return xmalloc(items * size);
}
static void
zlib_mem_free(opaque, address)
voidpf opaque, address;
{
free(address);
}
static void
zstream_init(z, func)
struct zstream *z;
const struct zstream_funcs *func;
{
z->flags = 0;
z->buf = Qnil;
z->buf_filled = 0;
z->input = Qnil;
z->stream.zalloc = zlib_mem_alloc;
z->stream.zfree = zlib_mem_free;
z->stream.opaque = Z_NULL;
z->stream.msg = Z_NULL;
z->stream.next_in = Z_NULL;
z->stream.avail_in = 0;
z->stream.next_out = Z_NULL;
z->stream.avail_out = 0;
z->func = func;
}
#define zstream_init_deflate(z) zstream_init((z), &deflate_funcs)
#define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
static void
zstream_expand_buffer(z)
struct zstream *z;
{
long inc;
if (NIL_P(z->buf)) {
/* I uses rb_str_new here not rb_str_buf_new because
rb_str_buf_new makes a zero-length string. */
z->buf = rb_str_new(0, ZSTREAM_INITIAL_BUFSIZE);
z->buf_filled = 0;
z->stream.next_out = RSTRING(z->buf)->ptr;
z->stream.avail_out = ZSTREAM_INITIAL_BUFSIZE;
RBASIC(z->buf)->klass = 0;
return;
}
if (RSTRING(z->buf)->len - z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
/* to keep other threads from freezing */
z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
}
else {
inc = z->buf_filled / 2;
if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) {
inc = ZSTREAM_AVAIL_OUT_STEP_MIN;
}
rb_str_resize(z->buf, z->buf_filled + inc);
z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ?
inc : ZSTREAM_AVAIL_OUT_STEP_MAX;
}
z->stream.next_out = RSTRING(z->buf)->ptr + z->buf_filled;
}
static void
zstream_expand_buffer_into(z, size)
struct zstream *z;
int size;
{
if (NIL_P(z->buf)) {
/* I uses rb_str_new here not rb_str_buf_new because
rb_str_buf_new makes a zero-length string. */
z->buf = rb_str_new(0, size);
z->buf_filled = 0;
z->stream.next_out = RSTRING(z->buf)->ptr;
z->stream.avail_out = size;
RBASIC(z->buf)->klass = 0;
}
else if (z->stream.avail_out != size) {
rb_str_resize(z->buf, z->buf_filled + size);
z->stream.next_out = RSTRING(z->buf)->ptr + z->buf_filled;
z->stream.avail_out = size;
}
}
static void
zstream_append_buffer(z, src, len)
struct zstream *z;
const char *src;
int len;
{
if (NIL_P(z->buf)) {
z->buf = rb_str_buf_new(len);
rb_str_buf_cat(z->buf, src, len);
z->buf_filled = len;
z->stream.next_out = RSTRING(z->buf)->ptr;
z->stream.avail_out = 0;
RBASIC(z->buf)->klass = 0;
return;
}
if (RSTRING(z->buf)->len < z->buf_filled + len) {
rb_str_resize(z->buf, z->buf_filled + len);
z->stream.avail_out = 0;
}
else {
if (z->stream.avail_out >= len) {
z->stream.avail_out -= len;
}
else {
z->stream.avail_out = 0;
}
}
memcpy(RSTRING(z->buf)->ptr + z->buf_filled, src, len);
z->buf_filled += len;
z->stream.next_out = RSTRING(z->buf)->ptr + z->buf_filled;
}
#define zstream_append_buffer2(z,v) \
zstream_append_buffer((z),RSTRING(v)->ptr,RSTRING(v)->len)
static VALUE
zstream_detach_buffer(z)
struct zstream *z;
{
VALUE dst;
if (NIL_P(z->buf)) {
dst = rb_str_new(0, 0);
}
else {
dst = z->buf;
rb_str_resize(dst, z->buf_filled);
RBASIC(dst)->klass = rb_cString;
}
z->buf = Qnil;
z->buf_filled = 0;
z->stream.next_out = 0;
z->stream.avail_out = 0;
return dst;
}
static VALUE
zstream_shift_buffer(z, len)
struct zstream *z;
int len;
{
VALUE dst;
if (z->buf_filled <= len) {
return zstream_detach_buffer(z);
}
dst = rb_str_substr(z->buf, 0, len);
RBASIC(dst)->klass = rb_cString;
z->buf_filled -= len;
memmove(RSTRING(z->buf)->ptr, RSTRING(z->buf)->ptr + len,
z->buf_filled);
z->stream.next_out = RSTRING(z->buf)->ptr + z->buf_filled;
z->stream.avail_out = RSTRING(z->buf)->len - z->buf_filled;
if (z->stream.avail_out > ZSTREAM_AVAIL_OUT_STEP_MAX) {
z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
}
return dst;
}
static void
zstream_buffer_ungetc(z, c)
struct zstream *z;
int c;
{
if (NIL_P(z->buf) || RSTRING(z->buf)->len - z->buf_filled == 0) {
zstream_expand_buffer(z);
}
memmove(RSTRING(z->buf)->ptr + 1, RSTRING(z->buf)->ptr, z->buf_filled);
RSTRING(z->buf)->ptr[0] = (char)c;
z->buf_filled++;
if (z->stream.avail_out > 0) {
z->stream.next_out++;
z->stream.avail_out--;
}
}
static void
zstream_append_input(z, src, len)
struct zstream *z;
const char *src;
unsigned int len;
{
if (len <= 0) return;
if (NIL_P(z->input)) {
z->input = rb_str_buf_new(len);
rb_str_buf_cat(z->input, src, len);
RBASIC(z->input)->klass = 0;
}
else {
rb_str_buf_cat(z->input, src, len);
}
}
#define zstream_append_input2(z,v)\
zstream_append_input((z), RSTRING(v)->ptr, RSTRING(v)->len)
static void
zstream_discard_input(z, len)
struct zstream *z;
unsigned int len;
{
if (NIL_P(z->input) || RSTRING(z->input)->len <= len) {
z->input = Qnil;
}
else {
memmove(RSTRING(z->input)->ptr, RSTRING(z->input)->ptr + len,
RSTRING(z->input)->len - len);
rb_str_resize(z->input, RSTRING(z->input)->len - len);
}
}
static void
zstream_reset_input(z)
struct zstream *z;
{
z->input = Qnil;
}
static void
zstream_passthrough_input(z)
struct zstream *z;
{
if (!NIL_P(z->input)) {
zstream_append_buffer2(z, z->input);
z->input = Qnil;
}
}
static VALUE
zstream_detach_input(z)
struct zstream *z;
{
VALUE dst;
if (NIL_P(z->input)) {
dst = rb_str_new(0, 0);
}
else {
dst = z->input;
RBASIC(dst)->klass = rb_cString;
}
z->input = Qnil;
return dst;
}
static void
zstream_reset(z)
struct zstream *z;
{
int err;
err = z->func->reset(&z->stream);
if (err != Z_OK && !ZSTREAM_IS_FINALIZE(z)) {
raise_zlib_error(err, z->stream.msg);
}
z->flags = ZSTREAM_FLAG_READY;
z->buf = Qnil;
z->buf_filled = 0;
z->stream.next_out = 0;
z->stream.avail_out = 0;
zstream_reset_input(z);
}
static VALUE
zstream_end(z)
struct zstream *z;
{
int err;
if (!ZSTREAM_IS_READY(z) && !ZSTREAM_IS_FINALIZE(z)) {
if (RTEST(ruby_debug)) {
rb_warning("attempt to close uninitialized zstream; ignored.");
}
return Qnil;
}
if (z->flags & ZSTREAM_FLAG_IN_STREAM) {
if (RTEST(ruby_debug)) {
rb_warning("attempt to close unfinished zstream; reset forced.");
}
zstream_reset(z);
}
zstream_reset_input(z);
err = z->func->end(&z->stream);
if (err != Z_OK && !ZSTREAM_IS_FINALIZE(z)) {
raise_zlib_error(err, z->stream.msg);
}
z->flags = 0;
return Qnil;
}
static void
zstream_run(z, src, len, flush)
struct zstream *z;
Bytef *src;
uInt len;
int flush;
{
uInt n;
int err;
volatile VALUE guard;
if (NIL_P(z->input) && len == 0) {
z->stream.next_in = "";
z->stream.avail_in = 0;
}
else {
zstream_append_input(z, src, len);
z->stream.next_in = RSTRING(z->input)->ptr;
z->stream.avail_in = RSTRING(z->input)->len;
/* keep reference to `z->input' so as not to be garbage collected
after zstream_reset_input() and prevent `z->stream.next_in'
from dangling. */
guard = z->input;
}
if (z->stream.avail_out == 0) {
zstream_expand_buffer(z);
}
for (;;) {
n = z->stream.avail_out;
err = z->func->run(&z->stream, flush);
z->buf_filled += n - z->stream.avail_out;
rb_thread_schedule();
if (err == Z_STREAM_END) {
z->flags &= ~ZSTREAM_FLAG_IN_STREAM;
z->flags |= ZSTREAM_FLAG_FINISHED;
break;
}
if (err != Z_OK) {
if (flush != Z_FINISH && err == Z_BUF_ERROR
&& z->stream.avail_out > 0) {
z->flags |= ZSTREAM_FLAG_IN_STREAM;
break;
}
zstream_reset_input(z);
if (z->stream.avail_in > 0) {
zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
}
raise_zlib_error(err, z->stream.msg);
}
if (z->stream.avail_out > 0) {
z->flags |= ZSTREAM_FLAG_IN_STREAM;
break;
}
zstream_expand_buffer(z);
}
zstream_reset_input(z);
if (z->stream.avail_in > 0) {
zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
guard = Qnil; /* prevent tail call to make guard effective */
}
}
static VALUE
zstream_sync(z, src, len)
struct zstream *z;
Bytef *src;
uInt len;
{
VALUE rest;
int err;
if (!NIL_P(z->input)) {
z->stream.next_in = RSTRING(z->input)->ptr;
z->stream.avail_in = RSTRING(z->input)->len;
err = inflateSync(&z->stream);
if (err == Z_OK) {
zstream_discard_input(z,
RSTRING(z->input)->len - z->stream.avail_in);
zstream_append_input(z, src, len);
return Qtrue;
}
zstream_reset_input(z);
if (err != Z_DATA_ERROR) {
rest = rb_str_new(z->stream.next_in, z->stream.avail_in);
raise_zlib_error(err, z->stream.msg);
}
}
if (len <= 0) return Qfalse;
z->stream.next_in = src;
z->stream.avail_in = len;
err = inflateSync(&z->stream);
if (err == Z_OK) {
zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
return Qtrue;
}
if (err != Z_DATA_ERROR) {
rest = rb_str_new(z->stream.next_in, z->stream.avail_in);
raise_zlib_error(err, z->stream.msg);
}
return Qfalse;
}
static void
zstream_mark(z)
struct zstream *z;
{
rb_gc_mark(z->buf);
rb_gc_mark(z->input);
}
static void
zstream_free(z)
struct zstream *z;
{
z->flags |= ZSTREAM_FLAG_FINALIZE;
zstream_end(z);
free(z);
}
static VALUE
zstream_new(klass, funcs)
VALUE klass;
const struct zstream_funcs *funcs;
{
VALUE obj;
struct zstream *z;
obj = Data_Make_Struct(klass, struct zstream,
zstream_mark, zstream_free, z);
zstream_init(z, funcs);
return obj;
}
#define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
#define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
static struct zstream *
get_zstream(obj)
VALUE obj;
{
struct zstream *z;
Data_Get_Struct(obj, struct zstream, z);
if (!ZSTREAM_IS_READY(z)) {
rb_raise(cZError, "stream is not ready");
}
return z;
}
/* ------------------------------------------------------------------------- */
/*
* Document-class: Zlib::ZStream
*
* Zlib::ZStream is the abstract class for the stream which handles the
* compressed data. The operations are defined in the subclasses:
* Zlib::Deflate for compression, and Zlib::Inflate for decompression.
*
* An instance of Zlib::ZStream has one stream (struct zstream in the source)
* and two variable-length buffers which associated to the input (next_in) of
* the stream and the output (next_out) of the stream. In this document,
* "input buffer" means the buffer for input, and "output buffer" means the
* buffer for output.
*
* Data input into an instance of Zlib::ZStream are temporally stored into
* the end of input buffer, and then data in input buffer are processed from
* the beginning of the buffer until no more output from the stream is
* produced (i.e. until avail_out > 0 after processing). During processing,
* output buffer is allocated and expanded automatically to hold all output
* data.
*
* Some particular instance methods consume the data in output buffer and
* return them as a String.
*
* Here is an ascii art for describing above:
*
* +================ an instance of Zlib::ZStream ================+
* || ||
* || +--------+ +-------+ +--------+ ||
* || +--| output |<---------|zstream|<---------| input |<--+ ||
* || | | buffer | next_out+-------+next_in | buffer | | ||
* || | +--------+ +--------+ | ||
* || | | ||
* +===|======================================================|===+
* | |
* v |
* "output data" "input data"
*
* If an error occurs during processing input buffer, an exception which is a
* subclass of Zlib::Error is raised. At that time, both input and output
* buffer keep their conditions at the time when the error occurs.
*
* == Method Catalogue
*
* Many of the methods in this class are fairly low-level and unlikely to be
* of interest to users. In fact, users are unlikely to use this class
* directly; rather they will be interested in Zlib::Inflate and
* Zlib::Deflate.
*
* The higher level methods are listed below.
*
* - #total_in
* - #total_out
* - #data_type
* - #adler
* - #reset
* - #finish
* - #finished?
* - #close
* - #closed?
*/
/*
* Closes the stream. All operations on the closed stream will raise an
* exception.
*/
static VALUE
rb_zstream_end(obj)
VALUE obj;
{
zstream_end(get_zstream(obj));
return Qnil;
}
/*
* Resets and initializes the stream. All data in both input and output buffer
* are discarded.
*/
static VALUE
rb_zstream_reset(obj)
VALUE obj;
{
zstream_reset(get_zstream(obj));
return Qnil;
}
/*
* Finishes the stream and flushes output buffer. See Zlib::Deflate#finish and
* Zlib::Inflate#finish for details of this behavior.
*/
static VALUE
rb_zstream_finish(obj)
VALUE obj;
{
struct zstream *z = get_zstream(obj);
VALUE dst;
zstream_run(z, "", 0, Z_FINISH);
dst = zstream_detach_buffer(z);
OBJ_INFECT(dst, obj);
return dst;
}
/*
* Flushes input buffer and returns all data in that buffer.
*/
static VALUE
rb_zstream_flush_next_in(obj)
VALUE obj;
{
struct zstream *z;
VALUE dst;
Data_Get_Struct(obj, struct zstream, z);
dst = zstream_detach_input(z);
OBJ_INFECT(dst, obj);
return dst;
}
/*
* Flushes output buffer and returns all data in that buffer.
*/
static VALUE
rb_zstream_flush_next_out(obj)
VALUE obj;
{
struct zstream *z;
VALUE dst;
Data_Get_Struct(obj, struct zstream, z);
dst = zstream_detach_buffer(z);
OBJ_INFECT(dst, obj);
return dst;
}
/*
* Returns number of bytes of free spaces in output buffer. Because the free
* space is allocated automatically, this method returns 0 normally.
*/
static VALUE
rb_zstream_avail_out(obj)
VALUE obj;
{