zlib 3.2.4 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/ext/zlib/zlib.c +314 -129
  3. metadata +24 -26
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b4aedd29da471f6f4ade362844bf7dfd791422d5c835e5df3d5ce2d830b62b3
4
- data.tar.gz: a3619742be0ba61d02eb10a5c8bc0c0c25a282b8e119dfa892488a41560e9f0a
3
+ metadata.gz: cdea88e6b41721dab02ff9b8ac05dfc3783481d34346e5427415c3003be6fa1a
4
+ data.tar.gz: b4ae4a54e2eaaef1027e1c0419fd877785067ed1dd2959351df611ed7b770538
5
5
  SHA512:
6
- metadata.gz: b8c1fa15bb6ef5e3888224f860a13eaa8b422e43975df76f44f091bf145bfa35998aacbed000106e1a0c5a9d9a1e693c721433a68061b4b0cf5bdd6df42ca208
7
- data.tar.gz: 1aa4924a7b35dec704c2cf50e75306d876e02d7a16f5bcc834d4d3616eed16be9a20980435501bfdb26859ad14b23d6855062f6cf6ca3b9d435f9b6364f490e4
6
+ metadata.gz: 3e83994264b03d29caa738cd3d6bd339a8280a178aaf93281fdd016ee23b126bd99d54d462d9b08d62bd7bf795d0162db55352601b1d4910310c7d7481917a5d
7
+ data.tar.gz: c39631c36f0ca93bdbca32d4a8087811e0d7a1fe9cc3374eaae594a530409291157bbaaf533bd60837458ddfe5b44f9b46f25c532768f9c9f6691c47570855c1
data/ext/zlib/zlib.c CHANGED
@@ -12,6 +12,29 @@
12
12
  #include <ruby/io.h>
13
13
  #include <ruby/thread.h>
14
14
 
15
+ #ifndef HAVE_RB_LEN_T
16
+ typedef long rb_len_t;
17
+ typedef unsigned long rb_ulen_t;
18
+ #endif
19
+ #ifndef SIZEOF_RB_LEN_T
20
+ # define SIZEOF_RB_LEN_T SIZEOF_LONG
21
+ #endif
22
+ #ifndef LEN2NUM
23
+ # define LEN2NUM LONG2NUM
24
+ #endif
25
+ #ifndef ULEN2NUM
26
+ # define ULEN2NUM ULONG2NUM
27
+ #endif
28
+ #ifndef NUM2LEN
29
+ # define NUM2LEN NUM2LONG
30
+ #endif
31
+ #ifndef NUM2ULEN
32
+ # define NUM2ULEN NUM2ULONG
33
+ #endif
34
+ #ifndef PRIdLEN
35
+ # define PRIdLEN "ld"
36
+ #endif
37
+
15
38
  #ifdef HAVE_VALGRIND_MEMCHECK_H
16
39
  # include <valgrind/memcheck.h>
17
40
  # ifndef VALGRIND_MAKE_MEM_DEFINED
@@ -25,7 +48,7 @@
25
48
  # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) 0
26
49
  #endif
27
50
 
28
- #define RUBY_ZLIB_VERSION "3.2.4"
51
+ #define RUBY_ZLIB_VERSION "3.3.0"
29
52
 
30
53
  #ifndef RB_PASS_CALLED_KEYWORDS
31
54
  # define rb_class_new_instance_kw(argc, argv, klass, kw_splat) rb_class_new_instance(argc, argv, klass)
@@ -52,9 +75,9 @@ typedef uLong (*checksum_func)(uLong, const Bytef*, z_size_t);
52
75
  typedef uLong (*checksum_func)(uLong, const Bytef*, uInt);
53
76
  #endif
54
77
 
55
- #if SIZEOF_LONG > SIZEOF_INT
78
+ #if SIZEOF_RB_LEN_T > SIZEOF_INT
56
79
  static inline uInt
57
- max_uint(long n)
80
+ max_uint(rb_len_t n)
58
81
  {
59
82
  if (n > UINT_MAX) n = UINT_MAX;
60
83
  return (uInt)n;
@@ -86,23 +109,23 @@ struct zstream_funcs;
86
109
  struct zstream_run_args;
87
110
  static void zstream_init(struct zstream*, const struct zstream_funcs*);
88
111
  static void zstream_expand_buffer(struct zstream*);
89
- static void zstream_expand_buffer_into(struct zstream*, unsigned long);
112
+ static void zstream_expand_buffer_into(struct zstream*, rb_ulen_t);
90
113
  static int zstream_expand_buffer_non_stream(struct zstream *z);
91
- static void zstream_append_buffer(struct zstream*, const Bytef*, long);
114
+ static void zstream_append_buffer(struct zstream*, const Bytef*, rb_len_t);
92
115
  static VALUE zstream_detach_buffer(struct zstream*);
93
- static VALUE zstream_shift_buffer(struct zstream*, long, VALUE);
94
- static void zstream_buffer_ungets(struct zstream*, const Bytef*, unsigned long);
116
+ static VALUE zstream_shift_buffer(struct zstream*, rb_len_t, VALUE);
117
+ static void zstream_buffer_ungets(struct zstream*, const Bytef*, rb_ulen_t);
95
118
  static void zstream_buffer_ungetbyte(struct zstream*, int);
96
- static void zstream_append_input(struct zstream*, const Bytef*, long);
97
- static void zstream_discard_input(struct zstream*, long);
119
+ static void zstream_append_input(struct zstream*, const Bytef*, rb_len_t);
120
+ static void zstream_discard_input(struct zstream*, rb_len_t);
98
121
  static void zstream_reset_input(struct zstream*);
99
122
  static void zstream_passthrough_input(struct zstream*);
100
123
  static VALUE zstream_detach_input(struct zstream*);
101
124
  static void zstream_reset(struct zstream*);
102
125
  static VALUE zstream_end(struct zstream*);
103
126
  static VALUE zstream_ensure_end(VALUE v);
104
- static void zstream_run(struct zstream*, Bytef*, long, int);
105
- static VALUE zstream_sync(struct zstream*, Bytef*, long);
127
+ static void zstream_run(struct zstream*, Bytef*, rb_len_t, int);
128
+ static VALUE zstream_sync(struct zstream*, Bytef*, rb_len_t);
106
129
  static void zstream_mark(void*);
107
130
  static void zstream_free(void*);
108
131
  static VALUE zstream_new(VALUE, const struct zstream_funcs*);
@@ -158,8 +181,8 @@ static void gzfile_write_raw(struct gzfile*);
158
181
  static VALUE gzfile_read_raw_partial(VALUE);
159
182
  static VALUE gzfile_read_raw_rescue(VALUE,VALUE);
160
183
  static VALUE gzfile_read_raw(struct gzfile*, VALUE outbuf);
161
- static int gzfile_read_raw_ensure(struct gzfile*, long, VALUE outbuf);
162
- static char *gzfile_read_raw_until_zero(struct gzfile*, long);
184
+ static int gzfile_read_raw_ensure(struct gzfile*, rb_len_t, VALUE outbuf);
185
+ static char *gzfile_read_raw_until_zero(struct gzfile*, rb_len_t);
163
186
  static unsigned int gzfile_get16(const unsigned char*);
164
187
  static unsigned long gzfile_get32(const unsigned char*);
165
188
  static void gzfile_set32(unsigned long n, unsigned char*);
@@ -167,12 +190,12 @@ static void gzfile_make_header(struct gzfile*);
167
190
  static void gzfile_make_footer(struct gzfile*);
168
191
  static void gzfile_read_header(struct gzfile*, VALUE outbuf);
169
192
  static void gzfile_check_footer(struct gzfile*, VALUE outbuf);
170
- static void gzfile_write(struct gzfile*, Bytef*, long);
171
- static long gzfile_read_more(struct gzfile*, VALUE outbuf);
193
+ static void gzfile_write(struct gzfile*, Bytef*, rb_len_t);
194
+ static rb_len_t gzfile_read_more(struct gzfile*, VALUE outbuf);
172
195
  static void gzfile_calc_crc(struct gzfile*, VALUE);
173
- static VALUE gzfile_read(struct gzfile*, long, VALUE);
196
+ static VALUE gzfile_read(struct gzfile*, rb_len_t, VALUE);
174
197
  static VALUE gzfile_read_all(struct gzfile*, VALUE);
175
- static void gzfile_ungets(struct gzfile*, const Bytef*, long);
198
+ static void gzfile_ungets(struct gzfile*, const Bytef*, rb_len_t);
176
199
  static void gzfile_ungetbyte(struct gzfile*, int);
177
200
  static VALUE gzfile_writer_end_run(VALUE);
178
201
  static void gzfile_writer_end(struct gzfile*);
@@ -388,9 +411,9 @@ rb_zlib_version(VALUE klass)
388
411
  # define mask32(x) (x)
389
412
  #endif
390
413
 
391
- #if SIZEOF_LONG > SIZEOF_INT && !defined(HAVE_ZLIB_SIZE_T_FUNCS)
414
+ #if SIZEOF_RB_LEN_T > SIZEOF_INT && !defined(HAVE_ZLIB_SIZE_T_FUNCS)
392
415
  static uLong
393
- checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, long len)
416
+ checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, rb_len_t len)
394
417
  {
395
418
  if (len > UINT_MAX) {
396
419
  do {
@@ -523,8 +546,15 @@ rb_zlib_crc32(int argc, VALUE *argv, VALUE klass)
523
546
  static VALUE
524
547
  rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2)
525
548
  {
526
- return ULONG2NUM(
527
- crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2LONG(len2)));
549
+ z_off_t len = (z_off_t)NUM2ULEN(len2);
550
+
551
+ /* zlib 1.3.1 loops forever on a negative length. NUM2ULEN wraps a
552
+ * negative len2, which stays positive when z_off_t is the wider type. */
553
+ if (len < 0 || RTEST(rb_funcall(len2, '<', 1, INT2FIX(0)))) {
554
+ rb_raise(rb_eArgError, "negative length");
555
+ }
556
+ return ULEN2NUM(
557
+ crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), len));
528
558
  }
529
559
  #else
530
560
  #define rb_zlib_crc32_combine rb_f_notimplement
@@ -577,7 +607,7 @@ struct zstream {
577
607
  #define ZSTREAM_FLAG_FINISHED (1 << 2)
578
608
  #define ZSTREAM_FLAG_CLOSING (1 << 3)
579
609
  #define ZSTREAM_FLAG_GZFILE (1 << 4) /* disallows yield from expand_buffer for
580
- gzip*/
610
+ gzip*/
581
611
  #define ZSTREAM_REUSE_BUFFER (1 << 5)
582
612
  #define ZSTREAM_IN_PROGRESS (1 << 6)
583
613
  #define ZSTREAM_FLAG_UNUSED (1 << 7)
@@ -611,7 +641,8 @@ static const struct zstream_funcs inflate_funcs = {
611
641
  struct zstream_run_args {
612
642
  struct zstream *const z;
613
643
  Bytef *src;
614
- long len;
644
+ rb_len_t len;
645
+ rb_len_t rest_in; /* input after next_in + avail_in, as avail_in is a uInt */
615
646
  int flush; /* stream flush value for inflate() or deflate() */
616
647
  int interrupt; /* stop processing the stream and return to ruby */
617
648
  int jump_state; /* for buffer expansion block break or exception */
@@ -667,7 +698,7 @@ zstream_expand_buffer(struct zstream *z)
667
698
  }
668
699
 
669
700
  if (!ZSTREAM_IS_GZFILE(z) && rb_block_given_p()) {
670
- long buf_filled = ZSTREAM_BUF_FILLED(z);
701
+ rb_len_t buf_filled = ZSTREAM_BUF_FILLED(z);
671
702
  if (buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
672
703
  int state = 0;
673
704
 
@@ -702,7 +733,7 @@ zstream_expand_buffer(struct zstream *z)
702
733
  }
703
734
 
704
735
  static void
705
- zstream_expand_buffer_into(struct zstream *z, unsigned long size)
736
+ zstream_expand_buffer_into(struct zstream *z, rb_ulen_t size)
706
737
  {
707
738
  if (NIL_P(z->buf)) {
708
739
  /* I uses rb_str_new here not rb_str_buf_new because
@@ -732,7 +763,7 @@ zstream_expand_buffer_protect(struct zstream *z)
732
763
  static int
733
764
  zstream_expand_buffer_non_stream(struct zstream *z)
734
765
  {
735
- long inc, len = ZSTREAM_BUF_FILLED(z);
766
+ rb_len_t inc, len = ZSTREAM_BUF_FILLED(z);
736
767
 
737
768
  if (rb_str_capacity(z->buf) - len >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
738
769
  z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
@@ -753,7 +784,7 @@ zstream_expand_buffer_non_stream(struct zstream *z)
753
784
  }
754
785
 
755
786
  static void
756
- zstream_append_buffer(struct zstream *z, const Bytef *src, long len)
787
+ zstream_append_buffer(struct zstream *z, const Bytef *src, rb_len_t len)
757
788
  {
758
789
  if (NIL_P(z->buf)) {
759
790
  z->buf = rb_str_buf_new(len);
@@ -764,7 +795,7 @@ zstream_append_buffer(struct zstream *z, const Bytef *src, long len)
764
795
  return;
765
796
  }
766
797
 
767
- if ((long)rb_str_capacity(z->buf) < ZSTREAM_BUF_FILLED(z) + len) {
798
+ if ((rb_len_t)rb_str_capacity(z->buf) < ZSTREAM_BUF_FILLED(z) + len) {
768
799
  rb_str_modify_expand(z->buf, len);
769
800
  z->stream.avail_out = 0;
770
801
  }
@@ -806,6 +837,7 @@ zstream_detach_buffer(struct zstream *z)
806
837
  }
807
838
 
808
839
  z->buf = Qnil;
840
+ z->flags &= ~ZSTREAM_REUSE_BUFFER;
809
841
  z->stream.next_out = 0;
810
842
  z->stream.avail_out = 0;
811
843
 
@@ -818,10 +850,10 @@ zstream_detach_buffer(struct zstream *z)
818
850
  }
819
851
 
820
852
  static VALUE
821
- zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
853
+ zstream_shift_buffer(struct zstream *z, rb_len_t len, VALUE dst)
822
854
  {
823
855
  char *bufptr;
824
- long buflen = ZSTREAM_BUF_FILLED(z);
856
+ rb_len_t buflen = ZSTREAM_BUF_FILLED(z);
825
857
 
826
858
  if (buflen <= len) {
827
859
  if (NIL_P(dst) || (!ZSTREAM_IS_FINISHED(z) && !ZSTREAM_IS_GZFILE(z) &&
@@ -830,6 +862,9 @@ zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
830
862
  } else {
831
863
  bufptr = RSTRING_PTR(z->buf);
832
864
  rb_str_resize(dst, buflen);
865
+ /* dst may still share its memory with other strings, and
866
+ * its coderange describes the old contents */
867
+ rb_str_modify(dst);
833
868
  memcpy(RSTRING_PTR(dst), bufptr, buflen);
834
869
  }
835
870
  buflen = 0;
@@ -839,6 +874,7 @@ zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
839
874
  dst = rb_str_new(bufptr, len);
840
875
  } else {
841
876
  rb_str_resize(dst, len);
877
+ rb_str_modify(dst);
842
878
  memcpy(RSTRING_PTR(dst), bufptr, len);
843
879
  }
844
880
  buflen -= len;
@@ -846,7 +882,7 @@ zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
846
882
  memmove(bufptr, bufptr + len, buflen);
847
883
  rb_str_set_len(z->buf, buflen);
848
884
  z->stream.next_out = (Bytef*)RSTRING_END(z->buf);
849
- buflen = (long)rb_str_capacity(z->buf) - ZSTREAM_BUF_FILLED(z);
885
+ buflen = (rb_len_t)rb_str_capacity(z->buf) - ZSTREAM_BUF_FILLED(z);
850
886
  if (buflen > ZSTREAM_AVAIL_OUT_STEP_MAX) {
851
887
  buflen = ZSTREAM_AVAIL_OUT_STEP_MAX;
852
888
  }
@@ -856,10 +892,10 @@ zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
856
892
  }
857
893
 
858
894
  static void
859
- zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len)
895
+ zstream_buffer_ungets(struct zstream *z, const Bytef *b, rb_ulen_t len)
860
896
  {
861
897
  char *bufptr;
862
- long filled;
898
+ rb_len_t filled;
863
899
 
864
900
  zstream_expand_buffer_into(z, len);
865
901
 
@@ -882,7 +918,7 @@ zstream_buffer_ungetbyte(struct zstream *z, int c)
882
918
  }
883
919
 
884
920
  static void
885
- zstream_append_input(struct zstream *z, const Bytef *src, long len)
921
+ zstream_append_input(struct zstream *z, const Bytef *src, rb_len_t len)
886
922
  {
887
923
  if (len <= 0) return;
888
924
 
@@ -901,14 +937,14 @@ zstream_append_input(struct zstream *z, const Bytef *src, long len)
901
937
  zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
902
938
 
903
939
  static void
904
- zstream_discard_input(struct zstream *z, long len)
940
+ zstream_discard_input(struct zstream *z, rb_len_t len)
905
941
  {
906
942
  if (NIL_P(z->input)) {
907
943
  }
908
944
  else if (RBASIC_CLASS(z->input) == 0) {
909
945
  /* hidden, we created z->input and have complete control */
910
946
  char *ptr;
911
- long oldlen, newlen;
947
+ rb_len_t oldlen, newlen;
912
948
 
913
949
  RSTRING_GETMEM(z->input, ptr, oldlen);
914
950
  newlen = oldlen - len;
@@ -1023,8 +1059,10 @@ zstream_run_once(void *_arguments)
1023
1059
  {
1024
1060
  struct zstream_run_args *arguments = (struct zstream_run_args *)_arguments;
1025
1061
  struct zstream *z = arguments->z;
1062
+ /* Z_FINISH would end the stream before rest_in is given */
1063
+ int flush = arguments->rest_in > 0 ? Z_NO_FLUSH : arguments->flush;
1026
1064
 
1027
- uintptr_t error = z->func->run(&z->stream, arguments->flush);
1065
+ uintptr_t error = z->func->run(&z->stream, flush);
1028
1066
 
1029
1067
  return (void*)error;
1030
1068
  }
@@ -1050,9 +1088,6 @@ static VALUE
1050
1088
  zstream_run_once_begin(VALUE _arguments)
1051
1089
  {
1052
1090
  struct zstream_run_args *arguments = (struct zstream_run_args *)_arguments;
1053
- struct zstream *z = arguments->z;
1054
-
1055
- rb_str_locktmp(z->buf);
1056
1091
 
1057
1092
  #ifndef RB_NOGVL_UBF_ASYNC_SAFE
1058
1093
  return (VALUE)rb_thread_call_without_gvl(zstream_run_once, (void *)arguments, zstream_unblock_func, (void *)arguments);
@@ -1083,6 +1118,15 @@ zstream_run_func(struct zstream_run_args *args)
1083
1118
  while (!args->interrupt) {
1084
1119
  n = z->stream.avail_out;
1085
1120
 
1121
+ /* lock outside rb_ensure, so that a lock held by someone else is not
1122
+ released when rb_str_locktmp fails, and raise the failure after
1123
+ zstream_run_try has put back the pending input */
1124
+ rb_protect(rb_str_locktmp, z->buf, &state);
1125
+ if (state) {
1126
+ err = Z_OK;
1127
+ args->jump_state = state;
1128
+ break;
1129
+ }
1086
1130
  err = (int)(VALUE)rb_ensure(zstream_run_once_begin, (VALUE)args, zstream_run_once_ensure, (VALUE)args);
1087
1131
 
1088
1132
  rb_str_set_len(z->buf, ZSTREAM_BUF_FILLED(z) + (n - z->stream.avail_out));
@@ -1097,6 +1141,12 @@ zstream_run_func(struct zstream_run_args *args)
1097
1141
  break;
1098
1142
  }
1099
1143
 
1144
+ if (z->stream.avail_in == 0 && args->rest_in > 0) {
1145
+ z->stream.avail_in = MAX_UINT(args->rest_in);
1146
+ args->rest_in -= z->stream.avail_in;
1147
+ if (z->stream.avail_out > 0) continue;
1148
+ }
1149
+
1100
1150
  if (z->stream.avail_out > 0) {
1101
1151
  z->flags |= ZSTREAM_FLAG_IN_STREAM;
1102
1152
  break;
@@ -1105,7 +1155,7 @@ zstream_run_func(struct zstream_run_args *args)
1105
1155
  if (z->stream.avail_in == 0 && z->func == &inflate_funcs) {
1106
1156
  /* break here because inflate() return Z_BUF_ERROR when avail_in == 0. */
1107
1157
  /* but deflate() could be called with avail_in == 0 (there's hidden buffer
1108
- in zstream->state) */
1158
+ in zstream->state) */
1109
1159
  z->flags |= ZSTREAM_FLAG_IN_STREAM;
1110
1160
  break;
1111
1161
  }
@@ -1133,24 +1183,33 @@ zstream_run_try(VALUE value_arg)
1133
1183
  struct zstream_run_args *args = (struct zstream_run_args *)value_arg;
1134
1184
  struct zstream *z = args->z;
1135
1185
  Bytef *src = args->src;
1136
- long len = args->len;
1186
+ rb_len_t len = args->len;
1137
1187
  int flush = args->flush;
1138
1188
 
1139
1189
  int err;
1140
1190
  VALUE old_input = Qnil;
1141
1191
 
1142
- /* Cannot start zstream while it is in progress. */
1143
- if (z->flags & ZSTREAM_IN_PROGRESS) {
1144
- rb_raise(cInProgressError, "zlib stream is in progress");
1145
- }
1146
1192
  z->flags |= ZSTREAM_IN_PROGRESS;
1147
1193
 
1148
- if (NIL_P(z->input) && len == 0) {
1194
+ if (ZSTREAM_REUSE_BUFFER_P(z)) {
1195
+ /* always re-derive next_out; the user may have modified z->buf */
1196
+ z->stream.next_out = NULL;
1197
+ z->stream.avail_out = 0;
1198
+ }
1199
+
1200
+ zstream_append_input(z, src, len);
1201
+
1202
+ /* expanding the buffer may yield the pending output, and the block
1203
+ may exit there, so keep the input in `z->input' until then. */
1204
+ if (z->stream.avail_out == 0) {
1205
+ zstream_expand_buffer(z);
1206
+ }
1207
+
1208
+ if (NIL_P(z->input)) {
1149
1209
  z->stream.next_in = (Bytef*)"";
1150
1210
  z->stream.avail_in = 0;
1151
1211
  }
1152
1212
  else {
1153
- zstream_append_input(z, src, len);
1154
1213
  /* keep reference to `z->input' so as not to be garbage collected
1155
1214
  after zstream_reset_input() and prevent `z->stream.next_in'
1156
1215
  from dangling. */
@@ -1158,10 +1217,7 @@ zstream_run_try(VALUE value_arg)
1158
1217
  rb_obj_hide(old_input); /* for GVL release and later recycle */
1159
1218
  z->stream.next_in = (Bytef*)RSTRING_PTR(old_input);
1160
1219
  z->stream.avail_in = MAX_UINT(RSTRING_LEN(old_input));
1161
- }
1162
-
1163
- if (z->stream.avail_out == 0) {
1164
- zstream_expand_buffer(z);
1220
+ args->rest_in = RSTRING_LEN(old_input) - z->stream.avail_in;
1165
1221
  }
1166
1222
 
1167
1223
  loop:
@@ -1179,6 +1235,13 @@ loop:
1179
1235
  }
1180
1236
  }
1181
1237
 
1238
+ if (err == Z_BUF_ERROR && z->func == &deflate_funcs && z->stream.avail_in == 0) {
1239
+ /* deflate() reports Z_BUF_ERROR for a flush when no input was consumed
1240
+ * since the previous flush of the same or higher rank. Nothing is lost,
1241
+ * so treat it as a no-op like zlib's own gzflush() does. */
1242
+ err = Z_OK;
1243
+ }
1244
+
1182
1245
  if (flush != Z_FINISH && err == Z_BUF_ERROR && z->stream.avail_out > 0) {
1183
1246
  z->flags |= ZSTREAM_FLAG_IN_STREAM;
1184
1247
  }
@@ -1186,9 +1249,7 @@ loop:
1186
1249
  zstream_reset_input(z);
1187
1250
 
1188
1251
  if (err != Z_OK && err != Z_STREAM_END) {
1189
- if (z->stream.avail_in > 0) {
1190
- zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
1191
- }
1252
+ zstream_append_input(z, z->stream.next_in, z->stream.avail_in + args->rest_in);
1192
1253
  if (err == Z_NEED_DICT) {
1193
1254
  VALUE self = (VALUE)z->stream.opaque;
1194
1255
  if (self) {
@@ -1196,19 +1257,27 @@ loop:
1196
1257
  VALUE dict = rb_hash_aref(dicts, rb_uint2inum(z->stream.adler));
1197
1258
  if (!NIL_P(dict)) {
1198
1259
  rb_inflate_set_dictionary(self, dict);
1199
- goto loop;
1260
+ if (z->stream.avail_in > 0) goto loop;
1261
+ /* the dictionary id was the last byte available; inflate()
1262
+ * would only report Z_BUF_ERROR now */
1263
+ err = Z_OK;
1200
1264
  }
1201
1265
  }
1202
1266
  }
1203
- raise_zlib_error(err, z->stream.msg);
1267
+ if (err != Z_OK) {
1268
+ z->stream.next_in = Z_NULL;
1269
+ z->stream.avail_in = 0;
1270
+ raise_zlib_error(err, z->stream.msg);
1271
+ }
1204
1272
  }
1205
1273
 
1206
- if (z->stream.avail_in > 0) {
1207
- zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
1208
- }
1274
+ zstream_append_input(z, z->stream.next_in, z->stream.avail_in + args->rest_in);
1209
1275
  if (!NIL_P(old_input)) {
1210
1276
  rb_str_resize(old_input, 0);
1211
1277
  }
1278
+ /* the rest is in z->input now; do not leave next_in in old_input */
1279
+ z->stream.next_in = Z_NULL;
1280
+ z->stream.avail_in = 0;
1212
1281
 
1213
1282
  return Qnil;
1214
1283
  }
@@ -1226,36 +1295,62 @@ zstream_run_ensure(VALUE value_arg)
1226
1295
  }
1227
1296
 
1228
1297
  static void
1229
- zstream_run(struct zstream *z, Bytef *src, long len, int flush)
1298
+ zstream_run(struct zstream *z, Bytef *src, rb_len_t len, int flush)
1230
1299
  {
1231
1300
  struct zstream_run_args args = {
1232
1301
  .z = z,
1233
1302
  .src = src,
1234
1303
  .len = len,
1304
+ .rest_in = 0,
1235
1305
  .flush = flush,
1236
1306
  .interrupt = 0,
1237
1307
  .jump_state = 0,
1238
1308
  .stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p(),
1239
1309
  };
1240
1310
 
1311
+ /* Cannot start zstream while it is in progress. */
1312
+ if (z->flags & ZSTREAM_IN_PROGRESS) {
1313
+ rb_raise(cInProgressError, "zlib stream is in progress");
1314
+ }
1315
+
1241
1316
  rb_ensure(zstream_run_try, (VALUE)&args, zstream_run_ensure, (VALUE)&args);
1242
1317
  if (args.jump_state)
1243
1318
  rb_jump_tag(args.jump_state);
1244
1319
  }
1245
1320
 
1321
+ static int
1322
+ zstream_inflate_sync(struct zstream *z, Bytef *src, rb_len_t len)
1323
+ {
1324
+ int err;
1325
+
1326
+ z->stream.next_in = src;
1327
+ do {
1328
+ z->stream.avail_in = MAX_UINT(len);
1329
+ len -= z->stream.avail_in;
1330
+ err = inflateSync(&z->stream);
1331
+ } while (err == Z_DATA_ERROR && len > 0);
1332
+ /* do_inflate() looks at avail_in for the input left after the sync point */
1333
+ z->stream.avail_in = MAX_UINT(z->stream.avail_in + len);
1334
+
1335
+ return err;
1336
+ }
1337
+
1246
1338
  static VALUE
1247
- zstream_sync(struct zstream *z, Bytef *src, long len)
1339
+ zstream_sync(struct zstream *z, Bytef *src, rb_len_t len)
1248
1340
  {
1249
1341
  /* VALUE rest; */
1250
1342
  int err;
1251
1343
 
1344
+ if (z->flags & ZSTREAM_IN_PROGRESS) {
1345
+ rb_raise(cInProgressError, "zlib stream is in progress");
1346
+ }
1347
+
1252
1348
  if (!NIL_P(z->input)) {
1253
- z->stream.next_in = (Bytef*)RSTRING_PTR(z->input);
1254
- z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input));
1255
- err = inflateSync(&z->stream);
1349
+ err = zstream_inflate_sync(z, (Bytef*)RSTRING_PTR(z->input),
1350
+ RSTRING_LEN(z->input));
1256
1351
  if (err == Z_OK) {
1257
- zstream_discard_input(z,
1258
- RSTRING_LEN(z->input) - z->stream.avail_in);
1352
+ zstream_discard_input(z, z->stream.next_in -
1353
+ (Bytef*)RSTRING_PTR(z->input));
1259
1354
  zstream_append_input(z, src, len);
1260
1355
  return Qtrue;
1261
1356
  }
@@ -1268,11 +1363,9 @@ zstream_sync(struct zstream *z, Bytef *src, long len)
1268
1363
 
1269
1364
  if (len <= 0) return Qfalse;
1270
1365
 
1271
- z->stream.next_in = src;
1272
- z->stream.avail_in = MAX_UINT(len);
1273
- err = inflateSync(&z->stream);
1366
+ err = zstream_inflate_sync(z, src, len);
1274
1367
  if (err == Z_OK) {
1275
- zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
1368
+ zstream_append_input(z, z->stream.next_in, src + len - z->stream.next_in);
1276
1369
  return Qtrue;
1277
1370
  }
1278
1371
  if (err != Z_DATA_ERROR) {
@@ -1322,7 +1415,7 @@ zstream_memsize(const void *p)
1322
1415
  static const rb_data_type_t zstream_data_type = {
1323
1416
  "zstream",
1324
1417
  { zstream_mark, zstream_free, zstream_memsize, },
1325
- 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1418
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1326
1419
  };
1327
1420
 
1328
1421
  static VALUE
@@ -1515,8 +1608,11 @@ static VALUE
1515
1608
  rb_zstream_set_avail_out(VALUE obj, VALUE size)
1516
1609
  {
1517
1610
  struct zstream *z = get_zstream(obj);
1611
+ int len = FIX2INT(size);
1612
+
1613
+ if (len < 0) rb_raise(rb_eArgError, "negative size");
1518
1614
 
1519
- zstream_expand_buffer_into(z, FIX2INT(size));
1615
+ zstream_expand_buffer_into(z, len);
1520
1616
  return size;
1521
1617
  }
1522
1618
 
@@ -1528,7 +1624,7 @@ rb_zstream_avail_in(VALUE obj)
1528
1624
  {
1529
1625
  struct zstream *z;
1530
1626
  TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1531
- return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING_LEN(z->input)));
1627
+ return NIL_P(z->input) ? INT2FIX(0) : LEN2NUM(RSTRING_LEN(z->input));
1532
1628
  }
1533
1629
 
1534
1630
  /*
@@ -1730,7 +1826,13 @@ rb_deflate_init_copy(VALUE self, VALUE orig)
1730
1826
  raise_zlib_error(err, 0);
1731
1827
  }
1732
1828
  z1->input = NIL_P(z2->input) ? Qnil : rb_str_dup(z2->input);
1733
- z1->buf = NIL_P(z2->buf) ? Qnil : rb_str_dup(z2->buf);
1829
+ z1->buf = Qnil;
1830
+ if (!NIL_P(z2->buf)) {
1831
+ /* next_out was copied from orig; point it into our own buffer */
1832
+ z1->buf = rb_obj_hide(rb_str_new(RSTRING_PTR(z2->buf), RSTRING_LEN(z2->buf)));
1833
+ z1->stream.next_out = (Bytef*)RSTRING_END(z1->buf);
1834
+ z1->stream.avail_out = 0;
1835
+ }
1734
1836
  z1->flags = z2->flags;
1735
1837
 
1736
1838
  return self;
@@ -1926,18 +2028,21 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy)
1926
2028
  int level, strategy;
1927
2029
  int err;
1928
2030
  uInt n;
1929
- long filled;
2031
+ rb_len_t filled;
1930
2032
 
1931
2033
  level = ARG_LEVEL(v_level);
1932
2034
  strategy = ARG_STRATEGY(v_strategy);
1933
2035
 
2036
+ if (NIL_P(z->buf) || z->stream.avail_out == 0) {
2037
+ zstream_expand_buffer(z);
2038
+ }
1934
2039
  n = z->stream.avail_out;
1935
2040
  err = deflateParams(&z->stream, level, strategy);
1936
2041
  filled = n - z->stream.avail_out;
1937
2042
  while (err == Z_BUF_ERROR) {
2043
+ rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled);
1938
2044
  rb_warning("deflateParams() returned Z_BUF_ERROR");
1939
2045
  zstream_expand_buffer(z);
1940
- rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled);
1941
2046
  n = z->stream.avail_out;
1942
2047
  err = deflateParams(&z->stream, level, strategy);
1943
2048
  filled = n - z->stream.avail_out;
@@ -2128,7 +2233,8 @@ do_inflate(struct zstream *z, VALUE src)
2128
2233
  return;
2129
2234
  }
2130
2235
  StringValue(src);
2131
- if (RSTRING_LEN(src) > 0 || z->stream.avail_in > 0) { /* prevent Z_BUF_ERROR */
2236
+ if (RSTRING_LEN(src) > 0 ||
2237
+ (!NIL_P(z->input) && RSTRING_LEN(z->input) > 0)) { /* prevent Z_BUF_ERROR */
2132
2238
  zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH);
2133
2239
  }
2134
2240
  }
@@ -2177,7 +2283,7 @@ rb_inflate_inflate_body(VALUE _arguments)
2177
2283
  }
2178
2284
  if (buffer != Qnil) {
2179
2285
  if (!(ZSTREAM_REUSE_BUFFER_P(z) && z->buf == buffer)) {
2180
- long len = RSTRING_LEN(buffer);
2286
+ rb_len_t len = RSTRING_LEN(buffer);
2181
2287
  if (len >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
2182
2288
  rb_str_modify(buffer);
2183
2289
  }
@@ -2186,12 +2292,24 @@ rb_inflate_inflate_body(VALUE _arguments)
2186
2292
  rb_str_modify_expand(buffer, len);
2187
2293
  }
2188
2294
  rb_str_set_len(buffer, 0);
2295
+ if (!NIL_P(z->buf) && RSTRING_LEN(z->buf) > 0) {
2296
+ /* carry over the output pending in the previous buffer */
2297
+ rb_str_buf_cat(buffer, RSTRING_PTR(z->buf), RSTRING_LEN(z->buf));
2298
+ }
2189
2299
  z->flags |= ZSTREAM_REUSE_BUFFER;
2190
2300
  z->buf = buffer;
2191
2301
  }
2192
2302
  } else if (ZSTREAM_REUSE_BUFFER_P(z)) {
2303
+ VALUE pending = z->buf;
2193
2304
  z->flags &= ~ZSTREAM_REUSE_BUFFER;
2194
2305
  z->buf = Qnil;
2306
+ z->stream.next_out = NULL;
2307
+ z->stream.avail_out = 0;
2308
+ if (RSTRING_LEN(pending) > 0) {
2309
+ /* carry over the output pending in the previous buffer */
2310
+ zstream_append_buffer2(z, pending);
2311
+ }
2312
+ RB_GC_GUARD(pending);
2195
2313
  }
2196
2314
  rb_scan_args(argc, argv, "10", &src);
2197
2315
 
@@ -2203,7 +2321,7 @@ rb_inflate_inflate_body(VALUE _arguments)
2203
2321
  StringValue(src);
2204
2322
  zstream_append_buffer2(z, src);
2205
2323
  if (ZSTREAM_REUSE_BUFFER_P(z)) {
2206
- dst = rb_str_resize(buffer, 0);
2324
+ dst = zstream_detach_buffer(z);
2207
2325
  } else {
2208
2326
  dst = rb_str_new(0, 0);
2209
2327
  }
@@ -2243,6 +2361,12 @@ rb_inflate_inflate_body(VALUE _arguments)
2243
2361
  * :buffer keyword argument.
2244
2362
  * * When passing a block, the yielded chunks will be the same value as the
2245
2363
  * :buffer keyword argument.
2364
+ * * When using the :buffer keyword argument, pass the same String object in
2365
+ * every call to this method on the stream, from the first call until the
2366
+ * stream is finished, and do not modify it other than through this method
2367
+ * until then. Also, do not mix it with other methods that produce output,
2368
+ * such as #<< and Zlib::ZStream#finish. Otherwise, the behavior is
2369
+ * undefined.
2246
2370
  *
2247
2371
  * Raises a Zlib::NeedDict exception if a preset dictionary is needed to
2248
2372
  * decompress. Set the dictionary by Zlib::Inflate#set_dictionary and then
@@ -2307,8 +2431,11 @@ rb_inflate_addstr(VALUE obj, VALUE src)
2307
2431
  *
2308
2432
  * Inputs +string+ into the end of input buffer and skips data until a full
2309
2433
  * flush point can be found. If the point is found in the buffer, this method
2310
- * flushes the buffer and returns false. Otherwise it returns +true+ and the
2311
- * following data of full flush point is preserved in the buffer.
2434
+ * returns +true+ and the following data of full flush point is preserved in
2435
+ * the buffer. Otherwise it flushes the buffer and returns +false+.
2436
+ *
2437
+ * Raises a Zlib::InProgressError exception if called while the stream is
2438
+ * being inflated.
2312
2439
  */
2313
2440
  static VALUE
2314
2441
  rb_inflate_sync(VALUE obj, VALUE src)
@@ -2430,7 +2557,7 @@ struct gzfile {
2430
2557
  unsigned long crc;
2431
2558
  int ecflags;
2432
2559
  int lineno;
2433
- long ungetc;
2560
+ rb_len_t ungetc;
2434
2561
  void (*end)(struct gzfile *);
2435
2562
  rb_encoding *enc;
2436
2563
  rb_encoding *enc2;
@@ -2450,6 +2577,9 @@ struct gzfile {
2450
2577
 
2451
2578
  #define GZFILE_READ_SIZE 2048
2452
2579
 
2580
+ /* the encoding of the data before conversion */
2581
+ #define GZFILE_RAW_ENC(gz) ((gz)->enc2 ? (gz)->enc2 : (gz)->enc)
2582
+
2453
2583
  enum { read_raw_arg_len, read_raw_arg_buf, read_raw_arg__count};
2454
2584
  struct read_raw_arg {
2455
2585
  VALUE io;
@@ -2485,6 +2615,9 @@ gzfile_free(void *p)
2485
2615
  }
2486
2616
  zstream_finalize(z);
2487
2617
  }
2618
+ if (gz->ec) {
2619
+ rb_econv_close(gz->ec);
2620
+ }
2488
2621
  xfree(gz);
2489
2622
  }
2490
2623
 
@@ -2497,7 +2630,7 @@ gzfile_memsize(const void *p)
2497
2630
  static const rb_data_type_t gzfile_data_type = {
2498
2631
  "gzfile",
2499
2632
  { gzfile_mark, gzfile_free, gzfile_memsize, },
2500
- 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2633
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2501
2634
  };
2502
2635
 
2503
2636
  static void
@@ -2619,7 +2752,7 @@ gzfile_read_raw(struct gzfile *gz, VALUE outbuf)
2619
2752
  }
2620
2753
 
2621
2754
  static int
2622
- gzfile_read_raw_ensure(struct gzfile *gz, long size, VALUE outbuf)
2755
+ gzfile_read_raw_ensure(struct gzfile *gz, rb_len_t size, VALUE outbuf)
2623
2756
  {
2624
2757
  VALUE str;
2625
2758
 
@@ -2636,7 +2769,7 @@ gzfile_read_raw_ensure(struct gzfile *gz, long size, VALUE outbuf)
2636
2769
  }
2637
2770
 
2638
2771
  static char *
2639
- gzfile_read_raw_until_zero(struct gzfile *gz, long offset)
2772
+ gzfile_read_raw_until_zero(struct gzfile *gz, rb_len_t offset)
2640
2773
  {
2641
2774
  VALUE str;
2642
2775
  char *p;
@@ -2645,6 +2778,9 @@ gzfile_read_raw_until_zero(struct gzfile *gz, long offset)
2645
2778
  p = memchr(RSTRING_PTR(gz->z.input) + offset, '\0',
2646
2779
  RSTRING_LEN(gz->z.input) - offset);
2647
2780
  if (p) break;
2781
+ if (gz->io == Qundef) {
2782
+ rb_raise(cGzError, "unexpected end of string");
2783
+ }
2648
2784
  str = gzfile_read_raw(gz, Qnil);
2649
2785
  if (NIL_P(str)) {
2650
2786
  rb_raise(cGzError, "unexpected end of file");
@@ -2773,7 +2909,7 @@ static void
2773
2909
  gzfile_read_header(struct gzfile *gz, VALUE outbuf)
2774
2910
  {
2775
2911
  const unsigned char *head;
2776
- long len;
2912
+ rb_len_t len;
2777
2913
  char flags, *p;
2778
2914
 
2779
2915
  /* 10 is the size of gzip header */
@@ -2876,7 +3012,7 @@ gzfile_check_footer(struct gzfile *gz, VALUE outbuf)
2876
3012
  }
2877
3013
 
2878
3014
  static void
2879
- gzfile_write(struct gzfile *gz, Bytef *str, long len)
3015
+ gzfile_write(struct gzfile *gz, Bytef *str, rb_len_t len)
2880
3016
  {
2881
3017
  if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) {
2882
3018
  gzfile_make_header(gz);
@@ -2890,7 +3026,7 @@ gzfile_write(struct gzfile *gz, Bytef *str, long len)
2890
3026
  gzfile_write_raw(gz);
2891
3027
  }
2892
3028
 
2893
- static long
3029
+ static rb_len_t
2894
3030
  gzfile_read_more(struct gzfile *gz, VALUE outbuf)
2895
3031
  {
2896
3032
  VALUE str;
@@ -2942,11 +3078,11 @@ gzfile_newstr(struct gzfile *gz, VALUE str)
2942
3078
  gz->ecflags, gz->ecopts);
2943
3079
  }
2944
3080
 
2945
- static long
2946
- gzfile_fill(struct gzfile *gz, long len, VALUE outbuf)
3081
+ static rb_len_t
3082
+ gzfile_fill(struct gzfile *gz, rb_len_t len, VALUE outbuf)
2947
3083
  {
2948
3084
  if (len < 0)
2949
- rb_raise(rb_eArgError, "negative length %ld given", len);
3085
+ rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
2950
3086
  if (len == 0)
2951
3087
  return 0;
2952
3088
  while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) < len) {
@@ -2962,7 +3098,7 @@ gzfile_fill(struct gzfile *gz, long len, VALUE outbuf)
2962
3098
  }
2963
3099
 
2964
3100
  static VALUE
2965
- gzfile_read(struct gzfile *gz, long len, VALUE outbuf)
3101
+ gzfile_read(struct gzfile *gz, rb_len_t len, VALUE outbuf)
2966
3102
  {
2967
3103
  VALUE dst;
2968
3104
 
@@ -2988,12 +3124,12 @@ gzfile_read(struct gzfile *gz, long len, VALUE outbuf)
2988
3124
  }
2989
3125
 
2990
3126
  static VALUE
2991
- gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf)
3127
+ gzfile_readpartial(struct gzfile *gz, rb_len_t len, VALUE outbuf)
2992
3128
  {
2993
3129
  VALUE dst;
2994
3130
 
2995
3131
  if (len < 0)
2996
- rb_raise(rb_eArgError, "negative length %ld given", len);
3132
+ rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
2997
3133
 
2998
3134
  if (len == 0) {
2999
3135
  if (NIL_P(outbuf))
@@ -3024,6 +3160,9 @@ gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf)
3024
3160
  static VALUE
3025
3161
  gzfile_read_all(struct gzfile *gz, VALUE dst)
3026
3162
  {
3163
+ VALUE str;
3164
+
3165
+ if (!NIL_P(dst)) rb_str_modify(dst);
3027
3166
  while (!ZSTREAM_IS_FINISHED(&gz->z)) {
3028
3167
  gzfile_read_more(gz, dst);
3029
3168
  }
@@ -3033,15 +3172,18 @@ gzfile_read_all(struct gzfile *gz, VALUE dst)
3033
3172
  }
3034
3173
  if (!NIL_P(dst)) {
3035
3174
  rb_str_resize(dst, 0);
3175
+ rb_enc_associate(dst, gz->enc);
3036
3176
  return dst;
3037
3177
  }
3038
- return rb_str_new(0, 0);
3178
+ return rb_enc_str_new(0, 0, gz->enc);
3039
3179
  }
3040
3180
 
3041
- dst = zstream_detach_buffer(&gz->z);
3042
- if (NIL_P(dst)) return dst;
3043
- gzfile_calc_crc(gz, dst);
3044
- return gzfile_newstr(gz, dst);
3181
+ str = zstream_detach_buffer(&gz->z);
3182
+ if (NIL_P(str)) return str;
3183
+ gzfile_calc_crc(gz, str);
3184
+ str = gzfile_newstr(gz, str);
3185
+ if (NIL_P(dst)) return str;
3186
+ return rb_str_replace(dst, str);
3045
3187
  }
3046
3188
 
3047
3189
  static VALUE
@@ -3050,7 +3192,7 @@ gzfile_getc(struct gzfile *gz)
3050
3192
  VALUE buf, dst = 0;
3051
3193
  int len;
3052
3194
 
3053
- len = rb_enc_mbmaxlen(gz->enc);
3195
+ len = rb_enc_mbmaxlen(GZFILE_RAW_ENC(gz));
3054
3196
  while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) < len) {
3055
3197
  gzfile_read_more(gz, Qnil);
3056
3198
  }
@@ -3079,7 +3221,7 @@ gzfile_getc(struct gzfile *gz)
3079
3221
  }
3080
3222
  else {
3081
3223
  buf = gz->z.buf;
3082
- len = rb_enc_mbclen(RSTRING_PTR(buf), RSTRING_END(buf), gz->enc);
3224
+ len = rb_enc_mbclen(RSTRING_PTR(buf), RSTRING_END(buf), GZFILE_RAW_ENC(gz));
3083
3225
  dst = gzfile_read(gz, len, Qnil);
3084
3226
  if (NIL_P(dst)) return dst;
3085
3227
  return gzfile_newstr(gz, dst);
@@ -3087,7 +3229,7 @@ gzfile_getc(struct gzfile *gz)
3087
3229
  }
3088
3230
 
3089
3231
  static void
3090
- gzfile_ungets(struct gzfile *gz, const Bytef *b, long len)
3232
+ gzfile_ungets(struct gzfile *gz, const Bytef *b, rb_len_t len)
3091
3233
  {
3092
3234
  zstream_buffer_ungets(&gz->z, b, len);
3093
3235
  gz->ungetc+=len;
@@ -3980,9 +4122,9 @@ rb_gzreader_s_zcat(int argc, VALUE *argv, VALUE klass)
3980
4122
  io = argv[0];
3981
4123
 
3982
4124
  do {
3983
- obj = rb_funcallv(klass, rb_intern("new"), argc, argv);
4125
+ obj = rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_CALLED_KEYWORDS);
3984
4126
  if (rb_block_given_p()) {
3985
- rb_gzreader_each(0, 0, obj);
4127
+ rb_gzreader_each(0, 0, obj);
3986
4128
  }
3987
4129
  else {
3988
4130
  if (!buf) {
@@ -3990,6 +4132,7 @@ rb_gzreader_s_zcat(int argc, VALUE *argv, VALUE klass)
3990
4132
  }
3991
4133
  tmpbuf = gzfile_read_all(get_gzfile(obj), Qnil);
3992
4134
  rb_str_cat(buf, RSTRING_PTR(tmpbuf), RSTRING_LEN(tmpbuf));
4135
+ RB_GC_GUARD(tmpbuf);
3993
4136
  }
3994
4137
 
3995
4138
  rb_gzreader_read(0, 0, obj);
@@ -4091,16 +4234,18 @@ rb_gzreader_read(int argc, VALUE *argv, VALUE obj)
4091
4234
  {
4092
4235
  struct gzfile *gz = get_gzfile(obj);
4093
4236
  VALUE vlen, outbuf;
4094
- long len;
4237
+ rb_len_t len;
4095
4238
 
4096
4239
  rb_scan_args(argc, argv, "02", &vlen, &outbuf);
4240
+ if (!NIL_P(outbuf))
4241
+ Check_Type(outbuf, T_STRING);
4097
4242
  if (NIL_P(vlen)) {
4098
4243
  return gzfile_read_all(gz, outbuf);
4099
4244
  }
4100
4245
 
4101
4246
  len = NUM2INT(vlen);
4102
4247
  if (len < 0) {
4103
- rb_raise(rb_eArgError, "negative length %ld given", len);
4248
+ rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
4104
4249
  }
4105
4250
  return gzfile_read(gz, len, outbuf);
4106
4251
  }
@@ -4122,13 +4267,13 @@ rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj)
4122
4267
  {
4123
4268
  struct gzfile *gz = get_gzfile(obj);
4124
4269
  VALUE vlen, outbuf;
4125
- long len;
4270
+ rb_len_t len;
4126
4271
 
4127
4272
  rb_scan_args(argc, argv, "11", &vlen, &outbuf);
4128
4273
 
4129
4274
  len = NUM2INT(vlen);
4130
4275
  if (len < 0) {
4131
- rb_raise(rb_eArgError, "negative length %ld given", len);
4276
+ rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
4132
4277
  }
4133
4278
  if (!NIL_P(outbuf))
4134
4279
  Check_Type(outbuf, T_STRING);
@@ -4302,23 +4447,26 @@ gzreader_skip_linebreaks(struct gzfile *gz)
4302
4447
  }
4303
4448
 
4304
4449
  static void
4305
- rscheck(const char *rsptr, long rslen, VALUE rs)
4450
+ rscheck(const char *rsptr, rb_len_t rslen, VALUE rs)
4306
4451
  {
4307
- if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
4452
+ if (RSTRING_PTR(rs) != rsptr || RSTRING_LEN(rs) != rslen)
4308
4453
  rb_raise(rb_eRuntimeError, "rs modified");
4309
4454
  }
4310
4455
 
4311
- static long
4312
- gzreader_charboundary(struct gzfile *gz, long n)
4456
+ static rb_len_t
4457
+ gzreader_charboundary(struct gzfile *gz, rb_len_t n)
4313
4458
  {
4314
4459
  char *s = RSTRING_PTR(gz->z.buf);
4315
4460
  char *e = s + ZSTREAM_BUF_FILLED(&gz->z);
4316
- char *p = rb_enc_left_char_head(s, s + n - 1, e, gz->enc);
4317
- long l = p - s;
4461
+ rb_encoding *enc = GZFILE_RAW_ENC(gz);
4462
+ char *p = rb_enc_left_char_head(s, s + n - 1, e, enc);
4463
+ rb_len_t l = p - s;
4318
4464
  if (l < n) {
4319
- int n_bytes = rb_enc_precise_mbclen(p, e, gz->enc);
4465
+ int n_bytes = rb_enc_precise_mbclen(p, e, enc);
4320
4466
  if (MBCLEN_NEEDMORE_P(n_bytes)) {
4321
- if ((l = gzfile_fill(gz, n + MBCLEN_NEEDMORE_LEN(n_bytes), Qnil)) > 0) {
4467
+ /* the character needs MBCLEN_NEEDMORE_LEN bytes beyond the END of the
4468
+ * buffer, which may lie past n */
4469
+ if ((l = gzfile_fill(gz, ZSTREAM_BUF_FILLED(&gz->z) + MBCLEN_NEEDMORE_LEN(n_bytes), Qnil)) > 0) {
4322
4470
  return l;
4323
4471
  }
4324
4472
  }
@@ -4337,10 +4485,9 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4337
4485
  VALUE dst;
4338
4486
  const char *rsptr;
4339
4487
  char *p, *res;
4340
- long rslen, n, limit = -1;
4488
+ rb_len_t rslen, n, limit = -1;
4341
4489
  int rspara;
4342
- rb_encoding *enc = gz->enc;
4343
- int maxlen = rb_enc_mbmaxlen(enc);
4490
+ int maxlen = rb_enc_mbmaxlen(GZFILE_RAW_ENC(gz));
4344
4491
 
4345
4492
  if (argc == 0) {
4346
4493
  rs = rb_rs;
@@ -4363,7 +4510,7 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4363
4510
  }
4364
4511
  }
4365
4512
  if (!NIL_P(lim)) {
4366
- limit = NUM2LONG(lim);
4513
+ limit = NUM2LEN(lim);
4367
4514
  if (limit == 0) return rb_str_new(0,0);
4368
4515
  }
4369
4516
  }
@@ -4407,10 +4554,11 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4407
4554
  gzreader_skip_linebreaks(gz);
4408
4555
  }
4409
4556
 
4410
- while (ZSTREAM_BUF_FILLED(&gz->z) < rslen) {
4557
+ rb_len_t minlen = (limit > 0 && limit < rslen) ? limit : rslen;
4558
+ while (ZSTREAM_BUF_FILLED(&gz->z) < minlen) {
4411
4559
  if (ZSTREAM_IS_FINISHED(&gz->z)) {
4412
4560
  if (ZSTREAM_BUF_FILLED(&gz->z) > 0) gz->lineno++;
4413
- return gzfile_read(gz, rslen, Qnil);
4561
+ return gzfile_read(gz, minlen, Qnil);
4414
4562
  }
4415
4563
  gzfile_read_more(gz, Qnil);
4416
4564
  }
@@ -4418,7 +4566,11 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4418
4566
  p = RSTRING_PTR(gz->z.buf);
4419
4567
  n = rslen;
4420
4568
  for (;;) {
4421
- long filled;
4569
+ rb_len_t filled;
4570
+ if (limit > 0 && limit < rslen) {
4571
+ n = limit;
4572
+ break;
4573
+ }
4422
4574
  if (n > ZSTREAM_BUF_FILLED(&gz->z)) {
4423
4575
  if (ZSTREAM_IS_FINISHED(&gz->z)) break;
4424
4576
  gzfile_read_more(gz, Qnil);
@@ -4436,8 +4588,15 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4436
4588
  n++;
4437
4589
  }
4438
4590
  else {
4439
- n += (long)(res - p);
4591
+ n += (rb_len_t)(res - p);
4440
4592
  p = res;
4593
+ if (maxlen > 1 &&
4594
+ rb_enc_left_char_head(RSTRING_PTR(gz->z.buf), p, RSTRING_PTR(gz->z.buf) + ZSTREAM_BUF_FILLED(&gz->z),
4595
+ GZFILE_RAW_ENC(gz)) != p) {
4596
+ /* the match starts inside a multibyte character: keep searching */
4597
+ p++, n++;
4598
+ continue;
4599
+ }
4441
4600
  if (rslen == 1 || memcmp(p, rsptr, rslen) == 0) break;
4442
4601
  p++, n++;
4443
4602
  }
@@ -4491,6 +4650,25 @@ rb_gzreader_readline(int argc, VALUE *argv, VALUE obj)
4491
4650
  return dst;
4492
4651
  }
4493
4652
 
4653
+ /*
4654
+ * IO#each_line and IO#readlines reject a zero limit: gets(..., 0) returns ""
4655
+ * without consuming anything, so a loop over it would never terminate.
4656
+ */
4657
+ static void
4658
+ gzreader_check_limit(int argc, VALUE *argv, const char *name)
4659
+ {
4660
+ VALUE rs, lim;
4661
+
4662
+ if (argc == 0) return;
4663
+ rb_scan_args(argc, argv, "11", &rs, &lim);
4664
+ if (NIL_P(lim) && !NIL_P(rs) && NIL_P(rb_check_string_type(rs))) {
4665
+ lim = rs;
4666
+ }
4667
+ if (!NIL_P(lim) && NUM2LEN(lim) == 0) {
4668
+ rb_raise(rb_eArgError, "invalid limit: 0 for %s", name);
4669
+ }
4670
+ }
4671
+
4494
4672
  /*
4495
4673
  * Document-method: Zlib::GzipReader#each
4496
4674
  *
@@ -4501,8 +4679,9 @@ rb_gzreader_each(int argc, VALUE *argv, VALUE obj)
4501
4679
  {
4502
4680
  VALUE str;
4503
4681
 
4504
- RETURN_ENUMERATOR(obj, 0, 0);
4682
+ RETURN_ENUMERATOR(obj, argc, argv);
4505
4683
 
4684
+ gzreader_check_limit(argc, argv, "each_line");
4506
4685
  while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
4507
4686
  rb_yield(str);
4508
4687
  }
@@ -4518,6 +4697,7 @@ static VALUE
4518
4697
  rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj)
4519
4698
  {
4520
4699
  VALUE str, dst;
4700
+ gzreader_check_limit(argc, argv, "readlines");
4521
4701
  dst = rb_ary_new();
4522
4702
  while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
4523
4703
  rb_ary_push(dst, str);
@@ -4533,7 +4713,10 @@ rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj)
4533
4713
  static VALUE
4534
4714
  rb_gzreader_external_encoding(VALUE self)
4535
4715
  {
4536
- return rb_enc_from_encoding(get_gzfile(self)->enc);
4716
+ struct gzfile *gz = get_gzfile(self);
4717
+ /* with a conversion set up, enc is the internal encoding and enc2 the
4718
+ * external one (same convention as rb_io_extract_encoding_option) */
4719
+ return rb_enc_from_encoding(gz->enc2 ? gz->enc2 : gz->enc);
4537
4720
  }
4538
4721
 
4539
4722
  static VALUE
@@ -4627,7 +4810,7 @@ zlib_gzip_run(VALUE arg)
4627
4810
  VALUE *args = (VALUE *)arg;
4628
4811
  struct gzfile *gz = (struct gzfile *)args[0];
4629
4812
  VALUE src = args[1];
4630
- long len;
4813
+ rb_len_t len;
4631
4814
 
4632
4815
  gzfile_make_header(gz);
4633
4816
  len = RSTRING_LEN(src);
@@ -4724,7 +4907,7 @@ Init_zlib(void)
4724
4907
 
4725
4908
  mZlib = rb_define_module("Zlib");
4726
4909
 
4727
- id_dictionaries = rb_intern("@dictionaries");
4910
+ id_dictionaries = rb_intern("dictionaries");
4728
4911
 
4729
4912
  cZError = rb_define_class_under(mZlib, "Error", rb_eStandardError);
4730
4913
  cStreamEnd = rb_define_class_under(mZlib, "StreamEnd", cZError);
@@ -4796,6 +4979,7 @@ Init_zlib(void)
4796
4979
  cDeflate = rb_define_class_under(mZlib, "Deflate", cZStream);
4797
4980
  rb_define_singleton_method(cDeflate, "deflate", rb_deflate_s_deflate, -1);
4798
4981
  rb_define_singleton_method(mZlib, "deflate", rb_deflate_s_deflate, -1);
4982
+ rb_define_alias(rb_singleton_class(mZlib), "dump", "deflate");
4799
4983
  rb_define_alloc_func(cDeflate, rb_deflate_s_allocate);
4800
4984
  rb_define_method(cDeflate, "initialize", rb_deflate_initialize, -1);
4801
4985
  rb_define_method(cDeflate, "initialize_copy", rb_deflate_init_copy, 1);
@@ -4808,6 +4992,7 @@ Init_zlib(void)
4808
4992
  cInflate = rb_define_class_under(mZlib, "Inflate", cZStream);
4809
4993
  rb_define_singleton_method(cInflate, "inflate", rb_inflate_s_inflate, 1);
4810
4994
  rb_define_singleton_method(mZlib, "inflate", rb_inflate_s_inflate, 1);
4995
+ rb_define_alias(rb_singleton_class(mZlib), "load", "inflate");
4811
4996
  rb_define_alloc_func(cInflate, rb_inflate_s_allocate);
4812
4997
  rb_define_method(cInflate, "initialize", rb_inflate_initialize, -1);
4813
4998
  rb_define_method(cInflate, "add_dictionary", rb_inflate_add_dictionary, 1);
metadata CHANGED
@@ -1,52 +1,50 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.4
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
- - Yukihiro Matsumoto
8
- - UENO Katsuhiro
7
+ - Yukihiro Matsumoto
8
+ - UENO Katsuhiro
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-10-08 00:00:00.000000000 Z
11
+ date: 2026-11-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby interface for the zlib compression/decompression library
14
14
  email:
15
- - matz@ruby-lang.org
16
- -
15
+ - matz@ruby-lang.org
16
+ -
17
17
  executables: []
18
18
  extensions:
19
- - ext/zlib/extconf.rb
19
+ - ext/zlib/extconf.rb
20
20
  extra_rdoc_files: []
21
21
  files:
22
- - BSDL
23
- - COPYING
24
- - README.md
25
- - ext/zlib/extconf.rb
26
- - ext/zlib/zlib.c
27
- - zlib.gemspec
28
- homepage: "https://github.com/ruby/zlib"
22
+ - BSDL
23
+ - COPYING
24
+ - README.md
25
+ - ext/zlib/extconf.rb
26
+ - ext/zlib/zlib.c
27
+ - zlib.gemspec
28
+ homepage: https://github.com/ruby/zlib
29
29
  licenses:
30
- - Ruby
31
- - BSD-2-Clause
30
+ - Ruby
31
+ - BSD-2-Clause
32
32
  metadata: {}
33
33
  rdoc_options: []
34
34
  require_paths:
35
- - lib
35
+ - lib
36
36
  required_ruby_version: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- -
39
- - ">="
40
- - !ruby/object:Gem::Version
41
- version: 2.7.0
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.7.0
42
41
  required_rubygems_version: !ruby/object:Gem::Requirement
43
42
  requirements:
44
- -
45
- - ">="
46
- - !ruby/object:Gem::Version
47
- version: "0"
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
48
46
  requirements: []
49
- rubygems_version: 4.1.0.beta1
47
+ rubygems_version: 4.0.20
50
48
  specification_version: 4
51
49
  summary: Ruby interface for the zlib compression/decompression library
52
50
  test_files: []