zlib 3.2.3 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 336dc64dd83cbbd9a74ca0494fd75bc2324ff0a615baa20b3c45c232ea6ee35a
4
- data.tar.gz: f23633ed1053a62b9423628edf0ad19149bf1785371f5c5bc3b08900dabeb56e
3
+ metadata.gz: cdea88e6b41721dab02ff9b8ac05dfc3783481d34346e5427415c3003be6fa1a
4
+ data.tar.gz: b4ae4a54e2eaaef1027e1c0419fd877785067ed1dd2959351df611ed7b770538
5
5
  SHA512:
6
- metadata.gz: d6b289bcde42b602690993bd4dc087928f1d50542ed9ddeffb148071f3e5eb9e37bd3891cfa9ec435648a62e01b6cffcfdc1dd92995674efe39c88c434843c6f
7
- data.tar.gz: 00764a29cf1ecbe3a0db97919a911e98ec21d1690599381c12e34a736469f1be8643997964575b4782aa5a100b0f219e645ca067bd22ad222e34b723f886d2f0
6
+ metadata.gz: 3e83994264b03d29caa738cd3d6bd339a8280a178aaf93281fdd016ee23b126bd99d54d462d9b08d62bd7bf795d0162db55352601b1d4910310c7d7481917a5d
7
+ data.tar.gz: c39631c36f0ca93bdbca32d4a8087811e0d7a1fe9cc3374eaae594a530409291157bbaaf533bd60837458ddfe5b44f9b46f25c532768f9c9f6691c47570855c1
data/ext/zlib/extconf.rb CHANGED
@@ -82,9 +82,7 @@ if have_zlib
82
82
  Logging::message 'checking for kind of operating system... '
83
83
  os_code = with_config('os-code') ||
84
84
  case RUBY_PLATFORM.split('-',2)[1]
85
- when 'amigaos' then
86
- os_code = 'AMIGA'
87
- when /mswin|mingw|bccwin/ then
85
+ when /mswin|mingw/ then
88
86
  # NOTE: cygwin should be regarded as Unix.
89
87
  os_code = 'WIN32'
90
88
  else
@@ -114,7 +112,7 @@ if have_zlib
114
112
  Logging::message "#{OS_NAMES[os_code]}\n"
115
113
  defines << "OS_CODE=#{os_code}"
116
114
 
117
- $defs.concat(defines.collect{|d|' -D'+d})
115
+ $defs.concat(defines.collect{|d|'-D'+d})
118
116
 
119
117
  if zsrc
120
118
  $defs << "-DHAVE_CRC32_COMBINE"
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.3"
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 */
@@ -651,6 +682,7 @@ zstream_init(struct zstream *z, const struct zstream_funcs *func)
651
682
  z->stream.avail_in = 0;
652
683
  z->stream.next_out = Z_NULL;
653
684
  z->stream.avail_out = 0;
685
+ z->stream.state = Z_NULL;
654
686
  z->func = func;
655
687
  }
656
688
 
@@ -666,7 +698,7 @@ zstream_expand_buffer(struct zstream *z)
666
698
  }
667
699
 
668
700
  if (!ZSTREAM_IS_GZFILE(z) && rb_block_given_p()) {
669
- long buf_filled = ZSTREAM_BUF_FILLED(z);
701
+ rb_len_t buf_filled = ZSTREAM_BUF_FILLED(z);
670
702
  if (buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
671
703
  int state = 0;
672
704
 
@@ -701,7 +733,7 @@ zstream_expand_buffer(struct zstream *z)
701
733
  }
702
734
 
703
735
  static void
704
- zstream_expand_buffer_into(struct zstream *z, unsigned long size)
736
+ zstream_expand_buffer_into(struct zstream *z, rb_ulen_t size)
705
737
  {
706
738
  if (NIL_P(z->buf)) {
707
739
  /* I uses rb_str_new here not rb_str_buf_new because
@@ -731,7 +763,7 @@ zstream_expand_buffer_protect(struct zstream *z)
731
763
  static int
732
764
  zstream_expand_buffer_non_stream(struct zstream *z)
733
765
  {
734
- long inc, len = ZSTREAM_BUF_FILLED(z);
766
+ rb_len_t inc, len = ZSTREAM_BUF_FILLED(z);
735
767
 
736
768
  if (rb_str_capacity(z->buf) - len >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
737
769
  z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
@@ -752,7 +784,7 @@ zstream_expand_buffer_non_stream(struct zstream *z)
752
784
  }
753
785
 
754
786
  static void
755
- 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)
756
788
  {
757
789
  if (NIL_P(z->buf)) {
758
790
  z->buf = rb_str_buf_new(len);
@@ -763,7 +795,7 @@ zstream_append_buffer(struct zstream *z, const Bytef *src, long len)
763
795
  return;
764
796
  }
765
797
 
766
- 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) {
767
799
  rb_str_modify_expand(z->buf, len);
768
800
  z->stream.avail_out = 0;
769
801
  }
@@ -805,6 +837,7 @@ zstream_detach_buffer(struct zstream *z)
805
837
  }
806
838
 
807
839
  z->buf = Qnil;
840
+ z->flags &= ~ZSTREAM_REUSE_BUFFER;
808
841
  z->stream.next_out = 0;
809
842
  z->stream.avail_out = 0;
810
843
 
@@ -817,10 +850,10 @@ zstream_detach_buffer(struct zstream *z)
817
850
  }
818
851
 
819
852
  static VALUE
820
- zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
853
+ zstream_shift_buffer(struct zstream *z, rb_len_t len, VALUE dst)
821
854
  {
822
855
  char *bufptr;
823
- long buflen = ZSTREAM_BUF_FILLED(z);
856
+ rb_len_t buflen = ZSTREAM_BUF_FILLED(z);
824
857
 
825
858
  if (buflen <= len) {
826
859
  if (NIL_P(dst) || (!ZSTREAM_IS_FINISHED(z) && !ZSTREAM_IS_GZFILE(z) &&
@@ -829,6 +862,9 @@ zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
829
862
  } else {
830
863
  bufptr = RSTRING_PTR(z->buf);
831
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);
832
868
  memcpy(RSTRING_PTR(dst), bufptr, buflen);
833
869
  }
834
870
  buflen = 0;
@@ -838,6 +874,7 @@ zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
838
874
  dst = rb_str_new(bufptr, len);
839
875
  } else {
840
876
  rb_str_resize(dst, len);
877
+ rb_str_modify(dst);
841
878
  memcpy(RSTRING_PTR(dst), bufptr, len);
842
879
  }
843
880
  buflen -= len;
@@ -845,7 +882,7 @@ zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
845
882
  memmove(bufptr, bufptr + len, buflen);
846
883
  rb_str_set_len(z->buf, buflen);
847
884
  z->stream.next_out = (Bytef*)RSTRING_END(z->buf);
848
- 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);
849
886
  if (buflen > ZSTREAM_AVAIL_OUT_STEP_MAX) {
850
887
  buflen = ZSTREAM_AVAIL_OUT_STEP_MAX;
851
888
  }
@@ -855,10 +892,10 @@ zstream_shift_buffer(struct zstream *z, long len, VALUE dst)
855
892
  }
856
893
 
857
894
  static void
858
- 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)
859
896
  {
860
897
  char *bufptr;
861
- long filled;
898
+ rb_len_t filled;
862
899
 
863
900
  zstream_expand_buffer_into(z, len);
864
901
 
@@ -881,7 +918,7 @@ zstream_buffer_ungetbyte(struct zstream *z, int c)
881
918
  }
882
919
 
883
920
  static void
884
- 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)
885
922
  {
886
923
  if (len <= 0) return;
887
924
 
@@ -900,14 +937,14 @@ zstream_append_input(struct zstream *z, const Bytef *src, long len)
900
937
  zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
901
938
 
902
939
  static void
903
- zstream_discard_input(struct zstream *z, long len)
940
+ zstream_discard_input(struct zstream *z, rb_len_t len)
904
941
  {
905
942
  if (NIL_P(z->input)) {
906
943
  }
907
944
  else if (RBASIC_CLASS(z->input) == 0) {
908
945
  /* hidden, we created z->input and have complete control */
909
946
  char *ptr;
910
- long oldlen, newlen;
947
+ rb_len_t oldlen, newlen;
911
948
 
912
949
  RSTRING_GETMEM(z->input, ptr, oldlen);
913
950
  newlen = oldlen - len;
@@ -1022,8 +1059,10 @@ zstream_run_once(void *_arguments)
1022
1059
  {
1023
1060
  struct zstream_run_args *arguments = (struct zstream_run_args *)_arguments;
1024
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;
1025
1064
 
1026
- uintptr_t error = z->func->run(&z->stream, arguments->flush);
1065
+ uintptr_t error = z->func->run(&z->stream, flush);
1027
1066
 
1028
1067
  return (void*)error;
1029
1068
  }
@@ -1049,9 +1088,6 @@ static VALUE
1049
1088
  zstream_run_once_begin(VALUE _arguments)
1050
1089
  {
1051
1090
  struct zstream_run_args *arguments = (struct zstream_run_args *)_arguments;
1052
- struct zstream *z = arguments->z;
1053
-
1054
- rb_str_locktmp(z->buf);
1055
1091
 
1056
1092
  #ifndef RB_NOGVL_UBF_ASYNC_SAFE
1057
1093
  return (VALUE)rb_thread_call_without_gvl(zstream_run_once, (void *)arguments, zstream_unblock_func, (void *)arguments);
@@ -1082,6 +1118,15 @@ zstream_run_func(struct zstream_run_args *args)
1082
1118
  while (!args->interrupt) {
1083
1119
  n = z->stream.avail_out;
1084
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
+ }
1085
1130
  err = (int)(VALUE)rb_ensure(zstream_run_once_begin, (VALUE)args, zstream_run_once_ensure, (VALUE)args);
1086
1131
 
1087
1132
  rb_str_set_len(z->buf, ZSTREAM_BUF_FILLED(z) + (n - z->stream.avail_out));
@@ -1092,8 +1137,15 @@ zstream_run_func(struct zstream_run_args *args)
1092
1137
  break;
1093
1138
  }
1094
1139
 
1095
- if (err != Z_OK && err != Z_BUF_ERROR)
1140
+ if (err != Z_OK && err != Z_BUF_ERROR) {
1096
1141
  break;
1142
+ }
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
+ }
1097
1149
 
1098
1150
  if (z->stream.avail_out > 0) {
1099
1151
  z->flags |= ZSTREAM_FLAG_IN_STREAM;
@@ -1103,7 +1155,7 @@ zstream_run_func(struct zstream_run_args *args)
1103
1155
  if (z->stream.avail_in == 0 && z->func == &inflate_funcs) {
1104
1156
  /* break here because inflate() return Z_BUF_ERROR when avail_in == 0. */
1105
1157
  /* but deflate() could be called with avail_in == 0 (there's hidden buffer
1106
- in zstream->state) */
1158
+ in zstream->state) */
1107
1159
  z->flags |= ZSTREAM_FLAG_IN_STREAM;
1108
1160
  break;
1109
1161
  }
@@ -1131,24 +1183,33 @@ zstream_run_try(VALUE value_arg)
1131
1183
  struct zstream_run_args *args = (struct zstream_run_args *)value_arg;
1132
1184
  struct zstream *z = args->z;
1133
1185
  Bytef *src = args->src;
1134
- long len = args->len;
1186
+ rb_len_t len = args->len;
1135
1187
  int flush = args->flush;
1136
1188
 
1137
1189
  int err;
1138
1190
  VALUE old_input = Qnil;
1139
1191
 
1140
- /* Cannot start zstream while it is in progress. */
1141
- if (z->flags & ZSTREAM_IN_PROGRESS) {
1142
- rb_raise(cInProgressError, "zlib stream is in progress");
1143
- }
1144
1192
  z->flags |= ZSTREAM_IN_PROGRESS;
1145
1193
 
1146
- 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)) {
1147
1209
  z->stream.next_in = (Bytef*)"";
1148
1210
  z->stream.avail_in = 0;
1149
1211
  }
1150
1212
  else {
1151
- zstream_append_input(z, src, len);
1152
1213
  /* keep reference to `z->input' so as not to be garbage collected
1153
1214
  after zstream_reset_input() and prevent `z->stream.next_in'
1154
1215
  from dangling. */
@@ -1156,10 +1217,7 @@ zstream_run_try(VALUE value_arg)
1156
1217
  rb_obj_hide(old_input); /* for GVL release and later recycle */
1157
1218
  z->stream.next_in = (Bytef*)RSTRING_PTR(old_input);
1158
1219
  z->stream.avail_in = MAX_UINT(RSTRING_LEN(old_input));
1159
- }
1160
-
1161
- if (z->stream.avail_out == 0) {
1162
- zstream_expand_buffer(z);
1220
+ args->rest_in = RSTRING_LEN(old_input) - z->stream.avail_in;
1163
1221
  }
1164
1222
 
1165
1223
  loop:
@@ -1168,20 +1226,30 @@ loop:
1168
1226
  /* retry if no exception is thrown */
1169
1227
  if (err == Z_OK && args->interrupt) {
1170
1228
  args->interrupt = 0;
1171
- goto loop;
1229
+
1230
+ /* Retry only if both avail_in > 0 (more input to process) and avail_out > 0
1231
+ * (output buffer has space). If avail_out == 0, the buffer is full and should
1232
+ * be consumed by the caller first. If avail_in == 0, there's nothing more to process. */
1233
+ if (z->stream.avail_in > 0 && z->stream.avail_out > 0) {
1234
+ goto loop;
1235
+ }
1172
1236
  }
1173
1237
 
1174
- if (flush != Z_FINISH && err == Z_BUF_ERROR
1175
- && z->stream.avail_out > 0) {
1176
- z->flags |= ZSTREAM_FLAG_IN_STREAM;
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
+
1245
+ if (flush != Z_FINISH && err == Z_BUF_ERROR && z->stream.avail_out > 0) {
1246
+ z->flags |= ZSTREAM_FLAG_IN_STREAM;
1177
1247
  }
1178
1248
 
1179
1249
  zstream_reset_input(z);
1180
1250
 
1181
1251
  if (err != Z_OK && err != Z_STREAM_END) {
1182
- if (z->stream.avail_in > 0) {
1183
- zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
1184
- }
1252
+ zstream_append_input(z, z->stream.next_in, z->stream.avail_in + args->rest_in);
1185
1253
  if (err == Z_NEED_DICT) {
1186
1254
  VALUE self = (VALUE)z->stream.opaque;
1187
1255
  if (self) {
@@ -1189,19 +1257,27 @@ loop:
1189
1257
  VALUE dict = rb_hash_aref(dicts, rb_uint2inum(z->stream.adler));
1190
1258
  if (!NIL_P(dict)) {
1191
1259
  rb_inflate_set_dictionary(self, dict);
1192
- 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;
1193
1264
  }
1194
1265
  }
1195
1266
  }
1196
- 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
+ }
1197
1272
  }
1198
1273
 
1199
- if (z->stream.avail_in > 0) {
1200
- zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
1201
- }
1274
+ zstream_append_input(z, z->stream.next_in, z->stream.avail_in + args->rest_in);
1202
1275
  if (!NIL_P(old_input)) {
1203
1276
  rb_str_resize(old_input, 0);
1204
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;
1205
1281
 
1206
1282
  return Qnil;
1207
1283
  }
@@ -1219,36 +1295,62 @@ zstream_run_ensure(VALUE value_arg)
1219
1295
  }
1220
1296
 
1221
1297
  static void
1222
- 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)
1223
1299
  {
1224
1300
  struct zstream_run_args args = {
1225
1301
  .z = z,
1226
1302
  .src = src,
1227
1303
  .len = len,
1304
+ .rest_in = 0,
1228
1305
  .flush = flush,
1229
1306
  .interrupt = 0,
1230
1307
  .jump_state = 0,
1231
1308
  .stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p(),
1232
1309
  };
1233
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
+
1234
1316
  rb_ensure(zstream_run_try, (VALUE)&args, zstream_run_ensure, (VALUE)&args);
1235
1317
  if (args.jump_state)
1236
1318
  rb_jump_tag(args.jump_state);
1237
1319
  }
1238
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
+
1239
1338
  static VALUE
1240
- zstream_sync(struct zstream *z, Bytef *src, long len)
1339
+ zstream_sync(struct zstream *z, Bytef *src, rb_len_t len)
1241
1340
  {
1242
1341
  /* VALUE rest; */
1243
1342
  int err;
1244
1343
 
1344
+ if (z->flags & ZSTREAM_IN_PROGRESS) {
1345
+ rb_raise(cInProgressError, "zlib stream is in progress");
1346
+ }
1347
+
1245
1348
  if (!NIL_P(z->input)) {
1246
- z->stream.next_in = (Bytef*)RSTRING_PTR(z->input);
1247
- z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input));
1248
- err = inflateSync(&z->stream);
1349
+ err = zstream_inflate_sync(z, (Bytef*)RSTRING_PTR(z->input),
1350
+ RSTRING_LEN(z->input));
1249
1351
  if (err == Z_OK) {
1250
- zstream_discard_input(z,
1251
- RSTRING_LEN(z->input) - z->stream.avail_in);
1352
+ zstream_discard_input(z, z->stream.next_in -
1353
+ (Bytef*)RSTRING_PTR(z->input));
1252
1354
  zstream_append_input(z, src, len);
1253
1355
  return Qtrue;
1254
1356
  }
@@ -1261,11 +1363,9 @@ zstream_sync(struct zstream *z, Bytef *src, long len)
1261
1363
 
1262
1364
  if (len <= 0) return Qfalse;
1263
1365
 
1264
- z->stream.next_in = src;
1265
- z->stream.avail_in = MAX_UINT(len);
1266
- err = inflateSync(&z->stream);
1366
+ err = zstream_inflate_sync(z, src, len);
1267
1367
  if (err == Z_OK) {
1268
- 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);
1269
1369
  return Qtrue;
1270
1370
  }
1271
1371
  if (err != Z_DATA_ERROR) {
@@ -1315,7 +1415,7 @@ zstream_memsize(const void *p)
1315
1415
  static const rb_data_type_t zstream_data_type = {
1316
1416
  "zstream",
1317
1417
  { zstream_mark, zstream_free, zstream_memsize, },
1318
- 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1418
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1319
1419
  };
1320
1420
 
1321
1421
  static VALUE
@@ -1454,6 +1554,7 @@ rb_zstream_finish(VALUE obj)
1454
1554
  * call-seq:
1455
1555
  * flush_next_in -> input
1456
1556
  *
1557
+ * Flushes input buffer and returns all data in that buffer.
1457
1558
  */
1458
1559
  static VALUE
1459
1560
  rb_zstream_flush_next_in(VALUE obj)
@@ -1507,8 +1608,11 @@ static VALUE
1507
1608
  rb_zstream_set_avail_out(VALUE obj, VALUE size)
1508
1609
  {
1509
1610
  struct zstream *z = get_zstream(obj);
1611
+ int len = FIX2INT(size);
1612
+
1613
+ if (len < 0) rb_raise(rb_eArgError, "negative size");
1510
1614
 
1511
- zstream_expand_buffer_into(z, FIX2INT(size));
1615
+ zstream_expand_buffer_into(z, len);
1512
1616
  return size;
1513
1617
  }
1514
1618
 
@@ -1520,7 +1624,7 @@ rb_zstream_avail_in(VALUE obj)
1520
1624
  {
1521
1625
  struct zstream *z;
1522
1626
  TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1523
- 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));
1524
1628
  }
1525
1629
 
1526
1630
  /*
@@ -1722,7 +1826,13 @@ rb_deflate_init_copy(VALUE self, VALUE orig)
1722
1826
  raise_zlib_error(err, 0);
1723
1827
  }
1724
1828
  z1->input = NIL_P(z2->input) ? Qnil : rb_str_dup(z2->input);
1725
- 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
+ }
1726
1836
  z1->flags = z2->flags;
1727
1837
 
1728
1838
  return self;
@@ -1918,18 +2028,21 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy)
1918
2028
  int level, strategy;
1919
2029
  int err;
1920
2030
  uInt n;
1921
- long filled;
2031
+ rb_len_t filled;
1922
2032
 
1923
2033
  level = ARG_LEVEL(v_level);
1924
2034
  strategy = ARG_STRATEGY(v_strategy);
1925
2035
 
2036
+ if (NIL_P(z->buf) || z->stream.avail_out == 0) {
2037
+ zstream_expand_buffer(z);
2038
+ }
1926
2039
  n = z->stream.avail_out;
1927
2040
  err = deflateParams(&z->stream, level, strategy);
1928
2041
  filled = n - z->stream.avail_out;
1929
2042
  while (err == Z_BUF_ERROR) {
2043
+ rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled);
1930
2044
  rb_warning("deflateParams() returned Z_BUF_ERROR");
1931
2045
  zstream_expand_buffer(z);
1932
- rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled);
1933
2046
  n = z->stream.avail_out;
1934
2047
  err = deflateParams(&z->stream, level, strategy);
1935
2048
  filled = n - z->stream.avail_out;
@@ -2120,7 +2233,8 @@ do_inflate(struct zstream *z, VALUE src)
2120
2233
  return;
2121
2234
  }
2122
2235
  StringValue(src);
2123
- 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 */
2124
2238
  zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH);
2125
2239
  }
2126
2240
  }
@@ -2169,7 +2283,7 @@ rb_inflate_inflate_body(VALUE _arguments)
2169
2283
  }
2170
2284
  if (buffer != Qnil) {
2171
2285
  if (!(ZSTREAM_REUSE_BUFFER_P(z) && z->buf == buffer)) {
2172
- long len = RSTRING_LEN(buffer);
2286
+ rb_len_t len = RSTRING_LEN(buffer);
2173
2287
  if (len >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
2174
2288
  rb_str_modify(buffer);
2175
2289
  }
@@ -2178,12 +2292,24 @@ rb_inflate_inflate_body(VALUE _arguments)
2178
2292
  rb_str_modify_expand(buffer, len);
2179
2293
  }
2180
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
+ }
2181
2299
  z->flags |= ZSTREAM_REUSE_BUFFER;
2182
2300
  z->buf = buffer;
2183
2301
  }
2184
2302
  } else if (ZSTREAM_REUSE_BUFFER_P(z)) {
2303
+ VALUE pending = z->buf;
2185
2304
  z->flags &= ~ZSTREAM_REUSE_BUFFER;
2186
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);
2187
2313
  }
2188
2314
  rb_scan_args(argc, argv, "10", &src);
2189
2315
 
@@ -2195,7 +2321,7 @@ rb_inflate_inflate_body(VALUE _arguments)
2195
2321
  StringValue(src);
2196
2322
  zstream_append_buffer2(z, src);
2197
2323
  if (ZSTREAM_REUSE_BUFFER_P(z)) {
2198
- dst = rb_str_resize(buffer, 0);
2324
+ dst = zstream_detach_buffer(z);
2199
2325
  } else {
2200
2326
  dst = rb_str_new(0, 0);
2201
2327
  }
@@ -2235,6 +2361,12 @@ rb_inflate_inflate_body(VALUE _arguments)
2235
2361
  * :buffer keyword argument.
2236
2362
  * * When passing a block, the yielded chunks will be the same value as the
2237
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.
2238
2370
  *
2239
2371
  * Raises a Zlib::NeedDict exception if a preset dictionary is needed to
2240
2372
  * decompress. Set the dictionary by Zlib::Inflate#set_dictionary and then
@@ -2299,8 +2431,11 @@ rb_inflate_addstr(VALUE obj, VALUE src)
2299
2431
  *
2300
2432
  * Inputs +string+ into the end of input buffer and skips data until a full
2301
2433
  * flush point can be found. If the point is found in the buffer, this method
2302
- * flushes the buffer and returns false. Otherwise it returns +true+ and the
2303
- * 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.
2304
2439
  */
2305
2440
  static VALUE
2306
2441
  rb_inflate_sync(VALUE obj, VALUE src)
@@ -2422,7 +2557,7 @@ struct gzfile {
2422
2557
  unsigned long crc;
2423
2558
  int ecflags;
2424
2559
  int lineno;
2425
- long ungetc;
2560
+ rb_len_t ungetc;
2426
2561
  void (*end)(struct gzfile *);
2427
2562
  rb_encoding *enc;
2428
2563
  rb_encoding *enc2;
@@ -2442,6 +2577,9 @@ struct gzfile {
2442
2577
 
2443
2578
  #define GZFILE_READ_SIZE 2048
2444
2579
 
2580
+ /* the encoding of the data before conversion */
2581
+ #define GZFILE_RAW_ENC(gz) ((gz)->enc2 ? (gz)->enc2 : (gz)->enc)
2582
+
2445
2583
  enum { read_raw_arg_len, read_raw_arg_buf, read_raw_arg__count};
2446
2584
  struct read_raw_arg {
2447
2585
  VALUE io;
@@ -2477,6 +2615,9 @@ gzfile_free(void *p)
2477
2615
  }
2478
2616
  zstream_finalize(z);
2479
2617
  }
2618
+ if (gz->ec) {
2619
+ rb_econv_close(gz->ec);
2620
+ }
2480
2621
  xfree(gz);
2481
2622
  }
2482
2623
 
@@ -2489,7 +2630,7 @@ gzfile_memsize(const void *p)
2489
2630
  static const rb_data_type_t gzfile_data_type = {
2490
2631
  "gzfile",
2491
2632
  { gzfile_mark, gzfile_free, gzfile_memsize, },
2492
- 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2633
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2493
2634
  };
2494
2635
 
2495
2636
  static void
@@ -2611,7 +2752,7 @@ gzfile_read_raw(struct gzfile *gz, VALUE outbuf)
2611
2752
  }
2612
2753
 
2613
2754
  static int
2614
- 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)
2615
2756
  {
2616
2757
  VALUE str;
2617
2758
 
@@ -2628,7 +2769,7 @@ gzfile_read_raw_ensure(struct gzfile *gz, long size, VALUE outbuf)
2628
2769
  }
2629
2770
 
2630
2771
  static char *
2631
- gzfile_read_raw_until_zero(struct gzfile *gz, long offset)
2772
+ gzfile_read_raw_until_zero(struct gzfile *gz, rb_len_t offset)
2632
2773
  {
2633
2774
  VALUE str;
2634
2775
  char *p;
@@ -2637,6 +2778,9 @@ gzfile_read_raw_until_zero(struct gzfile *gz, long offset)
2637
2778
  p = memchr(RSTRING_PTR(gz->z.input) + offset, '\0',
2638
2779
  RSTRING_LEN(gz->z.input) - offset);
2639
2780
  if (p) break;
2781
+ if (gz->io == Qundef) {
2782
+ rb_raise(cGzError, "unexpected end of string");
2783
+ }
2640
2784
  str = gzfile_read_raw(gz, Qnil);
2641
2785
  if (NIL_P(str)) {
2642
2786
  rb_raise(cGzError, "unexpected end of file");
@@ -2765,7 +2909,7 @@ static void
2765
2909
  gzfile_read_header(struct gzfile *gz, VALUE outbuf)
2766
2910
  {
2767
2911
  const unsigned char *head;
2768
- long len;
2912
+ rb_len_t len;
2769
2913
  char flags, *p;
2770
2914
 
2771
2915
  /* 10 is the size of gzip header */
@@ -2868,7 +3012,7 @@ gzfile_check_footer(struct gzfile *gz, VALUE outbuf)
2868
3012
  }
2869
3013
 
2870
3014
  static void
2871
- gzfile_write(struct gzfile *gz, Bytef *str, long len)
3015
+ gzfile_write(struct gzfile *gz, Bytef *str, rb_len_t len)
2872
3016
  {
2873
3017
  if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) {
2874
3018
  gzfile_make_header(gz);
@@ -2882,7 +3026,7 @@ gzfile_write(struct gzfile *gz, Bytef *str, long len)
2882
3026
  gzfile_write_raw(gz);
2883
3027
  }
2884
3028
 
2885
- static long
3029
+ static rb_len_t
2886
3030
  gzfile_read_more(struct gzfile *gz, VALUE outbuf)
2887
3031
  {
2888
3032
  VALUE str;
@@ -2934,11 +3078,11 @@ gzfile_newstr(struct gzfile *gz, VALUE str)
2934
3078
  gz->ecflags, gz->ecopts);
2935
3079
  }
2936
3080
 
2937
- static long
2938
- 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)
2939
3083
  {
2940
3084
  if (len < 0)
2941
- rb_raise(rb_eArgError, "negative length %ld given", len);
3085
+ rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
2942
3086
  if (len == 0)
2943
3087
  return 0;
2944
3088
  while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) < len) {
@@ -2954,7 +3098,7 @@ gzfile_fill(struct gzfile *gz, long len, VALUE outbuf)
2954
3098
  }
2955
3099
 
2956
3100
  static VALUE
2957
- gzfile_read(struct gzfile *gz, long len, VALUE outbuf)
3101
+ gzfile_read(struct gzfile *gz, rb_len_t len, VALUE outbuf)
2958
3102
  {
2959
3103
  VALUE dst;
2960
3104
 
@@ -2980,12 +3124,12 @@ gzfile_read(struct gzfile *gz, long len, VALUE outbuf)
2980
3124
  }
2981
3125
 
2982
3126
  static VALUE
2983
- gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf)
3127
+ gzfile_readpartial(struct gzfile *gz, rb_len_t len, VALUE outbuf)
2984
3128
  {
2985
3129
  VALUE dst;
2986
3130
 
2987
3131
  if (len < 0)
2988
- rb_raise(rb_eArgError, "negative length %ld given", len);
3132
+ rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
2989
3133
 
2990
3134
  if (len == 0) {
2991
3135
  if (NIL_P(outbuf))
@@ -3016,6 +3160,9 @@ gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf)
3016
3160
  static VALUE
3017
3161
  gzfile_read_all(struct gzfile *gz, VALUE dst)
3018
3162
  {
3163
+ VALUE str;
3164
+
3165
+ if (!NIL_P(dst)) rb_str_modify(dst);
3019
3166
  while (!ZSTREAM_IS_FINISHED(&gz->z)) {
3020
3167
  gzfile_read_more(gz, dst);
3021
3168
  }
@@ -3025,15 +3172,18 @@ gzfile_read_all(struct gzfile *gz, VALUE dst)
3025
3172
  }
3026
3173
  if (!NIL_P(dst)) {
3027
3174
  rb_str_resize(dst, 0);
3175
+ rb_enc_associate(dst, gz->enc);
3028
3176
  return dst;
3029
3177
  }
3030
- return rb_str_new(0, 0);
3178
+ return rb_enc_str_new(0, 0, gz->enc);
3031
3179
  }
3032
3180
 
3033
- dst = zstream_detach_buffer(&gz->z);
3034
- if (NIL_P(dst)) return dst;
3035
- gzfile_calc_crc(gz, dst);
3036
- 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);
3037
3187
  }
3038
3188
 
3039
3189
  static VALUE
@@ -3042,7 +3192,7 @@ gzfile_getc(struct gzfile *gz)
3042
3192
  VALUE buf, dst = 0;
3043
3193
  int len;
3044
3194
 
3045
- len = rb_enc_mbmaxlen(gz->enc);
3195
+ len = rb_enc_mbmaxlen(GZFILE_RAW_ENC(gz));
3046
3196
  while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) < len) {
3047
3197
  gzfile_read_more(gz, Qnil);
3048
3198
  }
@@ -3071,7 +3221,7 @@ gzfile_getc(struct gzfile *gz)
3071
3221
  }
3072
3222
  else {
3073
3223
  buf = gz->z.buf;
3074
- 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));
3075
3225
  dst = gzfile_read(gz, len, Qnil);
3076
3226
  if (NIL_P(dst)) return dst;
3077
3227
  return gzfile_newstr(gz, dst);
@@ -3079,7 +3229,7 @@ gzfile_getc(struct gzfile *gz)
3079
3229
  }
3080
3230
 
3081
3231
  static void
3082
- gzfile_ungets(struct gzfile *gz, const Bytef *b, long len)
3232
+ gzfile_ungets(struct gzfile *gz, const Bytef *b, rb_len_t len)
3083
3233
  {
3084
3234
  zstream_buffer_ungets(&gz->z, b, len);
3085
3235
  gz->ungetc+=len;
@@ -3972,9 +4122,9 @@ rb_gzreader_s_zcat(int argc, VALUE *argv, VALUE klass)
3972
4122
  io = argv[0];
3973
4123
 
3974
4124
  do {
3975
- obj = rb_funcallv(klass, rb_intern("new"), argc, argv);
4125
+ obj = rb_funcallv_kw(klass, rb_intern("new"), argc, argv, RB_PASS_CALLED_KEYWORDS);
3976
4126
  if (rb_block_given_p()) {
3977
- rb_gzreader_each(0, 0, obj);
4127
+ rb_gzreader_each(0, 0, obj);
3978
4128
  }
3979
4129
  else {
3980
4130
  if (!buf) {
@@ -3982,6 +4132,7 @@ rb_gzreader_s_zcat(int argc, VALUE *argv, VALUE klass)
3982
4132
  }
3983
4133
  tmpbuf = gzfile_read_all(get_gzfile(obj), Qnil);
3984
4134
  rb_str_cat(buf, RSTRING_PTR(tmpbuf), RSTRING_LEN(tmpbuf));
4135
+ RB_GC_GUARD(tmpbuf);
3985
4136
  }
3986
4137
 
3987
4138
  rb_gzreader_read(0, 0, obj);
@@ -4083,16 +4234,18 @@ rb_gzreader_read(int argc, VALUE *argv, VALUE obj)
4083
4234
  {
4084
4235
  struct gzfile *gz = get_gzfile(obj);
4085
4236
  VALUE vlen, outbuf;
4086
- long len;
4237
+ rb_len_t len;
4087
4238
 
4088
4239
  rb_scan_args(argc, argv, "02", &vlen, &outbuf);
4240
+ if (!NIL_P(outbuf))
4241
+ Check_Type(outbuf, T_STRING);
4089
4242
  if (NIL_P(vlen)) {
4090
4243
  return gzfile_read_all(gz, outbuf);
4091
4244
  }
4092
4245
 
4093
4246
  len = NUM2INT(vlen);
4094
4247
  if (len < 0) {
4095
- rb_raise(rb_eArgError, "negative length %ld given", len);
4248
+ rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
4096
4249
  }
4097
4250
  return gzfile_read(gz, len, outbuf);
4098
4251
  }
@@ -4114,13 +4267,13 @@ rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj)
4114
4267
  {
4115
4268
  struct gzfile *gz = get_gzfile(obj);
4116
4269
  VALUE vlen, outbuf;
4117
- long len;
4270
+ rb_len_t len;
4118
4271
 
4119
4272
  rb_scan_args(argc, argv, "11", &vlen, &outbuf);
4120
4273
 
4121
4274
  len = NUM2INT(vlen);
4122
4275
  if (len < 0) {
4123
- rb_raise(rb_eArgError, "negative length %ld given", len);
4276
+ rb_raise(rb_eArgError, "negative length %"PRIdLEN" given", len);
4124
4277
  }
4125
4278
  if (!NIL_P(outbuf))
4126
4279
  Check_Type(outbuf, T_STRING);
@@ -4294,23 +4447,26 @@ gzreader_skip_linebreaks(struct gzfile *gz)
4294
4447
  }
4295
4448
 
4296
4449
  static void
4297
- rscheck(const char *rsptr, long rslen, VALUE rs)
4450
+ rscheck(const char *rsptr, rb_len_t rslen, VALUE rs)
4298
4451
  {
4299
- if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
4452
+ if (RSTRING_PTR(rs) != rsptr || RSTRING_LEN(rs) != rslen)
4300
4453
  rb_raise(rb_eRuntimeError, "rs modified");
4301
4454
  }
4302
4455
 
4303
- static long
4304
- gzreader_charboundary(struct gzfile *gz, long n)
4456
+ static rb_len_t
4457
+ gzreader_charboundary(struct gzfile *gz, rb_len_t n)
4305
4458
  {
4306
4459
  char *s = RSTRING_PTR(gz->z.buf);
4307
4460
  char *e = s + ZSTREAM_BUF_FILLED(&gz->z);
4308
- char *p = rb_enc_left_char_head(s, s + n - 1, e, gz->enc);
4309
- 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;
4310
4464
  if (l < n) {
4311
- int n_bytes = rb_enc_precise_mbclen(p, e, gz->enc);
4465
+ int n_bytes = rb_enc_precise_mbclen(p, e, enc);
4312
4466
  if (MBCLEN_NEEDMORE_P(n_bytes)) {
4313
- 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) {
4314
4470
  return l;
4315
4471
  }
4316
4472
  }
@@ -4329,10 +4485,9 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4329
4485
  VALUE dst;
4330
4486
  const char *rsptr;
4331
4487
  char *p, *res;
4332
- long rslen, n, limit = -1;
4488
+ rb_len_t rslen, n, limit = -1;
4333
4489
  int rspara;
4334
- rb_encoding *enc = gz->enc;
4335
- int maxlen = rb_enc_mbmaxlen(enc);
4490
+ int maxlen = rb_enc_mbmaxlen(GZFILE_RAW_ENC(gz));
4336
4491
 
4337
4492
  if (argc == 0) {
4338
4493
  rs = rb_rs;
@@ -4355,7 +4510,7 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4355
4510
  }
4356
4511
  }
4357
4512
  if (!NIL_P(lim)) {
4358
- limit = NUM2LONG(lim);
4513
+ limit = NUM2LEN(lim);
4359
4514
  if (limit == 0) return rb_str_new(0,0);
4360
4515
  }
4361
4516
  }
@@ -4399,10 +4554,11 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4399
4554
  gzreader_skip_linebreaks(gz);
4400
4555
  }
4401
4556
 
4402
- 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) {
4403
4559
  if (ZSTREAM_IS_FINISHED(&gz->z)) {
4404
4560
  if (ZSTREAM_BUF_FILLED(&gz->z) > 0) gz->lineno++;
4405
- return gzfile_read(gz, rslen, Qnil);
4561
+ return gzfile_read(gz, minlen, Qnil);
4406
4562
  }
4407
4563
  gzfile_read_more(gz, Qnil);
4408
4564
  }
@@ -4410,7 +4566,11 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4410
4566
  p = RSTRING_PTR(gz->z.buf);
4411
4567
  n = rslen;
4412
4568
  for (;;) {
4413
- long filled;
4569
+ rb_len_t filled;
4570
+ if (limit > 0 && limit < rslen) {
4571
+ n = limit;
4572
+ break;
4573
+ }
4414
4574
  if (n > ZSTREAM_BUF_FILLED(&gz->z)) {
4415
4575
  if (ZSTREAM_IS_FINISHED(&gz->z)) break;
4416
4576
  gzfile_read_more(gz, Qnil);
@@ -4428,8 +4588,15 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4428
4588
  n++;
4429
4589
  }
4430
4590
  else {
4431
- n += (long)(res - p);
4591
+ n += (rb_len_t)(res - p);
4432
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
+ }
4433
4600
  if (rslen == 1 || memcmp(p, rsptr, rslen) == 0) break;
4434
4601
  p++, n++;
4435
4602
  }
@@ -4483,6 +4650,25 @@ rb_gzreader_readline(int argc, VALUE *argv, VALUE obj)
4483
4650
  return dst;
4484
4651
  }
4485
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
+
4486
4672
  /*
4487
4673
  * Document-method: Zlib::GzipReader#each
4488
4674
  *
@@ -4493,8 +4679,9 @@ rb_gzreader_each(int argc, VALUE *argv, VALUE obj)
4493
4679
  {
4494
4680
  VALUE str;
4495
4681
 
4496
- RETURN_ENUMERATOR(obj, 0, 0);
4682
+ RETURN_ENUMERATOR(obj, argc, argv);
4497
4683
 
4684
+ gzreader_check_limit(argc, argv, "each_line");
4498
4685
  while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
4499
4686
  rb_yield(str);
4500
4687
  }
@@ -4510,6 +4697,7 @@ static VALUE
4510
4697
  rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj)
4511
4698
  {
4512
4699
  VALUE str, dst;
4700
+ gzreader_check_limit(argc, argv, "readlines");
4513
4701
  dst = rb_ary_new();
4514
4702
  while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
4515
4703
  rb_ary_push(dst, str);
@@ -4525,7 +4713,10 @@ rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj)
4525
4713
  static VALUE
4526
4714
  rb_gzreader_external_encoding(VALUE self)
4527
4715
  {
4528
- 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);
4529
4720
  }
4530
4721
 
4531
4722
  static VALUE
@@ -4619,7 +4810,7 @@ zlib_gzip_run(VALUE arg)
4619
4810
  VALUE *args = (VALUE *)arg;
4620
4811
  struct gzfile *gz = (struct gzfile *)args[0];
4621
4812
  VALUE src = args[1];
4622
- long len;
4813
+ rb_len_t len;
4623
4814
 
4624
4815
  gzfile_make_header(gz);
4625
4816
  len = RSTRING_LEN(src);
@@ -4716,7 +4907,7 @@ Init_zlib(void)
4716
4907
 
4717
4908
  mZlib = rb_define_module("Zlib");
4718
4909
 
4719
- id_dictionaries = rb_intern("@dictionaries");
4910
+ id_dictionaries = rb_intern("dictionaries");
4720
4911
 
4721
4912
  cZError = rb_define_class_under(mZlib, "Error", rb_eStandardError);
4722
4913
  cStreamEnd = rb_define_class_under(mZlib, "StreamEnd", cZError);
@@ -4788,6 +4979,7 @@ Init_zlib(void)
4788
4979
  cDeflate = rb_define_class_under(mZlib, "Deflate", cZStream);
4789
4980
  rb_define_singleton_method(cDeflate, "deflate", rb_deflate_s_deflate, -1);
4790
4981
  rb_define_singleton_method(mZlib, "deflate", rb_deflate_s_deflate, -1);
4982
+ rb_define_alias(rb_singleton_class(mZlib), "dump", "deflate");
4791
4983
  rb_define_alloc_func(cDeflate, rb_deflate_s_allocate);
4792
4984
  rb_define_method(cDeflate, "initialize", rb_deflate_initialize, -1);
4793
4985
  rb_define_method(cDeflate, "initialize_copy", rb_deflate_init_copy, 1);
@@ -4800,6 +4992,7 @@ Init_zlib(void)
4800
4992
  cInflate = rb_define_class_under(mZlib, "Inflate", cZStream);
4801
4993
  rb_define_singleton_method(cInflate, "inflate", rb_inflate_s_inflate, 1);
4802
4994
  rb_define_singleton_method(mZlib, "inflate", rb_inflate_s_inflate, 1);
4995
+ rb_define_alias(rb_singleton_class(mZlib), "load", "inflate");
4803
4996
  rb_define_alloc_func(cInflate, rb_inflate_s_allocate);
4804
4997
  rb_define_method(cInflate, "initialize", rb_inflate_initialize, -1);
4805
4998
  rb_define_method(cInflate, "add_dictionary", rb_inflate_add_dictionary, 1);
data/zlib.gemspec CHANGED
@@ -27,5 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = []
28
28
  spec.require_paths = ["lib"]
29
29
  spec.extensions = "ext/zlib/extconf.rb"
30
- spec.required_ruby_version = ">= 2.5.0"
30
+ spec.required_ruby_version = ">= 2.7.0"
31
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.3
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
8
  - UENO Katsuhiro
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 1980-01-02 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:
@@ -37,14 +37,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 2.5.0
40
+ version: 2.7.0
41
41
  required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  requirements:
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  requirements: []
47
- rubygems_version: 3.6.9
47
+ rubygems_version: 4.0.20
48
48
  specification_version: 4
49
49
  summary: Ruby interface for the zlib compression/decompression library
50
50
  test_files: []