zlib 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -0
  3. data/README.md +32 -3
  4. data/ext/zlib/zlib.c +61 -24
  5. data/zlib.gemspec +3 -3
  6. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c89a988315358942a700fcaad47423267840ffe6
4
- data.tar.gz: '0967f4982f0a1c36f43815420f05e6ef674c1252'
3
+ metadata.gz: e2d28dc0bcac8345db3a2f65229fc3ac5d3130e7
4
+ data.tar.gz: 23ac52950f8d7417ad3505300fb706a908eeda97
5
5
  SHA512:
6
- metadata.gz: 6e1a0fee0c0f38e9fccf904f70b3f3d71753ce537bf7bee59aa390d57e548588ecc2d5253b13904e7c61c4983475e8603759e51f6bd0bed1795c7f852f9dc2aa
7
- data.tar.gz: 92b5a9d994aa80fa3b7fc15064b3753e604ba024f37ed6258772c7e38ea84aeadb49fd2ea9054707cff0c564d873aa3cf836f46c174ce2e4adaee1e7539fd395
6
+ metadata.gz: fb489611bdaf16c1c20804d28fae2c3f28c0a9a5a9cecc54996cad555653d447a74060ed087b0edfe186fc8de06de65d61b93fe657f8ed8c5364329606061193
7
+ data.tar.gz: 10f5154d4e3f5f085b2aa32cb4d933842fc528892ed671031e9b35e818f0af9d427b0187de43d44c1b59873fef8dc4c8441ca431f3de78c0e25f6a2aa80503d0
data/.travis.yml CHANGED
@@ -1,5 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
+ - 2.3.5
5
+ - 2.4.2
4
6
  - ruby-head
5
7
  before_install: gem install bundler
data/README.md CHANGED
@@ -1,8 +1,18 @@
1
1
  # Zlib
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zlib`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Build Status](https://travis-ci.org/ruby/zlib.svg?branch=master)](https://travis-ci.org/ruby/zlib)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ This module provides access to the {zlib library}[http://zlib.net]. Zlib is designed to be a portable, free, general-purpose, legally unencumbered -- that is, not covered by any patents -- lossless data-compression library for use on virtually any computer hardware and operating system.
6
+
7
+ The zlib compression library provides in-memory compression and decompression functions, including integrity checks of the uncompressed data.
8
+
9
+ The zlib compressed data format is described in RFC 1950, which is a wrapper around a deflate stream which is described in RFC 1951.
10
+
11
+ The library also supports reading and writing files in gzip (.gz) format with an interface similar to that of IO. The gzip format is described in RFC 1952 which is also a wrapper around a deflate stream.
12
+
13
+ The zlib format was designed to be compact and fast for use in memory and on communications channels. The gzip format was designed for single-file compression on file systems, has a larger header than zlib to maintain directory information, and uses a different, slower check method than zlib.
14
+
15
+ See your system's zlib.h for further information about zlib
6
16
 
7
17
  ## Installation
8
18
 
@@ -22,7 +32,26 @@ Or install it yourself as:
22
32
 
23
33
  ## Usage
24
34
 
25
- TODO: Write usage instructions here
35
+ Using the wrapper to compress strings with default parameters is quite simple:
36
+
37
+ ```
38
+ require "zlib"
39
+
40
+ data_to_compress = File.read("don_quixote.txt")
41
+
42
+ puts "Input size: #{data_to_compress.size}"
43
+ #=> Input size: 2347740
44
+
45
+ data_compressed = Zlib::Deflate.deflate(data_to_compress)
46
+
47
+ puts "Compressed size: #{data_compressed.size}"
48
+ #=> Compressed size: 887238
49
+
50
+ uncompressed_data = Zlib::Inflate.inflate(data_compressed)
51
+
52
+ puts "Uncompressed data is: #{uncompressed_data}"
53
+ #=> Uncompressed data is: The Project Gutenberg EBook of Don Quixote...
54
+ ```
26
55
 
27
56
  ## Development
28
57
 
data/ext/zlib/zlib.c CHANGED
@@ -197,7 +197,7 @@ static VALUE rb_gzwriter_s_allocate(VALUE);
197
197
  static VALUE rb_gzwriter_s_open(int, VALUE*, VALUE);
198
198
  static VALUE rb_gzwriter_initialize(int, VALUE*, VALUE);
199
199
  static VALUE rb_gzwriter_flush(int, VALUE*, VALUE);
200
- static VALUE rb_gzwriter_write(VALUE, VALUE);
200
+ static VALUE rb_gzwriter_write(int, VALUE*, VALUE);
201
201
  static VALUE rb_gzwriter_putc(VALUE, VALUE);
202
202
 
203
203
  static VALUE rb_gzreader_s_allocate(VALUE);
@@ -451,7 +451,7 @@ rb_zlib_adler32(int argc, VALUE *argv, VALUE klass)
451
451
  static VALUE
452
452
  rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2)
453
453
  {
454
- return ULONG2NUM(
454
+ return ULONG2NUM(
455
455
  adler32_combine(NUM2ULONG(adler1), NUM2ULONG(adler2), NUM2LONG(len2)));
456
456
  }
457
457
  #else
@@ -489,7 +489,7 @@ rb_zlib_crc32(int argc, VALUE *argv, VALUE klass)
489
489
  static VALUE
490
490
  rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2)
491
491
  {
492
- return ULONG2NUM(
492
+ return ULONG2NUM(
493
493
  crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2LONG(len2)));
494
494
  }
495
495
  #else
@@ -644,7 +644,7 @@ zstream_expand_buffer(struct zstream *z)
644
644
  }
645
645
  else {
646
646
  zstream_expand_buffer_into(z,
647
- ZSTREAM_AVAIL_OUT_STEP_MAX - buf_filled);
647
+ ZSTREAM_AVAIL_OUT_STEP_MAX - buf_filled);
648
648
  }
649
649
  }
650
650
  else {
@@ -1381,7 +1381,7 @@ rb_zstream_data_type(VALUE obj)
1381
1381
  static VALUE
1382
1382
  rb_zstream_adler(VALUE obj)
1383
1383
  {
1384
- return rb_uint2inum(get_zstream(obj)->stream.adler);
1384
+ return rb_uint2inum(get_zstream(obj)->stream.adler);
1385
1385
  }
1386
1386
 
1387
1387
  /*
@@ -2673,7 +2673,7 @@ gzfile_calc_crc(struct gzfile *gz, VALUE str)
2673
2673
  }
2674
2674
  else {
2675
2675
  gz->crc = checksum_long(crc32, gz->crc, (Bytef*)RSTRING_PTR(str) + gz->ungetc,
2676
- RSTRING_LEN(str) - gz->ungetc);
2676
+ RSTRING_LEN(str) - gz->ungetc);
2677
2677
  gz->ungetc = 0;
2678
2678
  }
2679
2679
  }
@@ -3566,18 +3566,23 @@ rb_gzwriter_flush(int argc, VALUE *argv, VALUE obj)
3566
3566
  * Same as IO.
3567
3567
  */
3568
3568
  static VALUE
3569
- rb_gzwriter_write(VALUE obj, VALUE str)
3569
+ rb_gzwriter_write(int argc, VALUE *argv, VALUE obj)
3570
3570
  {
3571
3571
  struct gzfile *gz = get_gzfile(obj);
3572
-
3573
- if (!RB_TYPE_P(str, T_STRING))
3574
- str = rb_obj_as_string(str);
3575
- if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) {
3576
- str = rb_str_conv_enc(str, rb_enc_get(str), gz->enc2);
3572
+ size_t total = 0;
3573
+
3574
+ while (argc-- > 0) {
3575
+ VALUE str = *argv++;
3576
+ if (!RB_TYPE_P(str, T_STRING))
3577
+ str = rb_obj_as_string(str);
3578
+ if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) {
3579
+ str = rb_str_conv_enc(str, rb_enc_get(str), gz->enc2);
3580
+ }
3581
+ gzfile_write(gz, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str));
3582
+ total += RSTRING_LEN(str);
3583
+ RB_GC_GUARD(str);
3577
3584
  }
3578
- gzfile_write(gz, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str));
3579
- RB_GC_GUARD(str);
3580
- return INT2FIX(RSTRING_LEN(str));
3585
+ return SIZET2NUM(total);
3581
3586
  }
3582
3587
 
3583
3588
  /*
@@ -4245,6 +4250,14 @@ rb_gzreader_external_encoding(VALUE self)
4245
4250
  return rb_enc_from_encoding(get_gzfile(self)->enc);
4246
4251
  }
4247
4252
 
4253
+ static VALUE
4254
+ zlib_gzip_ensure(VALUE arg)
4255
+ {
4256
+ struct gzfile *gz = (struct gzfile *)arg;
4257
+ rb_rescue((VALUE(*)())gz->end, arg, NULL, Qnil);
4258
+ return Qnil;
4259
+ }
4260
+
4248
4261
  static void
4249
4262
  zlib_gzip_end(struct gzfile *gz)
4250
4263
  {
@@ -4257,6 +4270,7 @@ zlib_gzip_end(struct gzfile *gz)
4257
4270
  #define OPTHASH_GIVEN_P(opts) \
4258
4271
  (argc > 0 && !NIL_P((opts) = rb_check_hash_type(argv[argc-1])) && (--argc, 1))
4259
4272
  static ID id_level, id_strategy;
4273
+ static VALUE zlib_gzip_run(VALUE arg);
4260
4274
 
4261
4275
  /*
4262
4276
  * call-seq:
@@ -4285,9 +4299,8 @@ zlib_s_gzip(int argc, VALUE *argv, VALUE klass)
4285
4299
  {
4286
4300
  struct gzfile gz0;
4287
4301
  struct gzfile *gz = &gz0;
4288
- long len;
4289
4302
  int err;
4290
- VALUE src, opts, level=Qnil, strategy=Qnil;
4303
+ VALUE src, opts, level=Qnil, strategy=Qnil, args[2];
4291
4304
 
4292
4305
  if (OPTHASH_GIVEN_P(opts)) {
4293
4306
  ID keyword_ids[2];
@@ -4309,9 +4322,23 @@ zlib_s_gzip(int argc, VALUE *argv, VALUE klass)
4309
4322
  err = deflateInit2(&gz->z.stream, gz->level, Z_DEFLATED,
4310
4323
  -MAX_WBITS, DEF_MEM_LEVEL, ARG_STRATEGY(strategy));
4311
4324
  if (err != Z_OK) {
4325
+ zlib_gzip_end(gz);
4312
4326
  raise_zlib_error(err, gz->z.stream.msg);
4313
4327
  }
4314
4328
  ZSTREAM_READY(&gz->z);
4329
+ args[0] = (VALUE)gz;
4330
+ args[1] = src;
4331
+ return rb_ensure(zlib_gzip_run, (VALUE)args, zlib_gzip_ensure, (VALUE)gz);
4332
+ }
4333
+
4334
+ static VALUE
4335
+ zlib_gzip_run(VALUE arg)
4336
+ {
4337
+ VALUE *args = (VALUE *)arg;
4338
+ struct gzfile *gz = (struct gzfile *)args[0];
4339
+ VALUE src = args[1];
4340
+ long len;
4341
+
4315
4342
  gzfile_make_header(gz);
4316
4343
  len = RSTRING_LEN(src);
4317
4344
  if (len > 0) {
@@ -4327,10 +4354,11 @@ static void
4327
4354
  zlib_gunzip_end(struct gzfile *gz)
4328
4355
  {
4329
4356
  gz->z.flags |= ZSTREAM_FLAG_CLOSING;
4330
- gzfile_check_footer(gz);
4331
4357
  zstream_end(&gz->z);
4332
4358
  }
4333
4359
 
4360
+ static VALUE zlib_gunzip_run(VALUE arg);
4361
+
4334
4362
  /*
4335
4363
  * call-seq:
4336
4364
  * Zlib.gunzip(src) -> String
@@ -4355,7 +4383,6 @@ zlib_gunzip(VALUE klass, VALUE src)
4355
4383
  struct gzfile gz0;
4356
4384
  struct gzfile *gz = &gz0;
4357
4385
  int err;
4358
- VALUE dst;
4359
4386
 
4360
4387
  StringValue(src);
4361
4388
 
@@ -4367,14 +4394,24 @@ zlib_gunzip(VALUE klass, VALUE src)
4367
4394
  gz->io = Qundef;
4368
4395
  gz->z.input = src;
4369
4396
  ZSTREAM_READY(&gz->z);
4397
+ return rb_ensure(zlib_gunzip_run, (VALUE)gz, zlib_gzip_ensure, (VALUE)gz);
4398
+ }
4399
+
4400
+ static VALUE
4401
+ zlib_gunzip_run(VALUE arg)
4402
+ {
4403
+ struct gzfile *gz = (struct gzfile *)arg;
4404
+ VALUE dst;
4405
+
4370
4406
  gzfile_read_header(gz);
4371
4407
  dst = zstream_detach_buffer(&gz->z);
4372
4408
  gzfile_calc_crc(gz, dst);
4373
- if (!ZSTREAM_IS_FINISHED(&gz->z)) {
4374
- rb_raise(cGzError, "unexpected end of file");
4375
- }
4376
- if (NIL_P(gz->z.input))
4409
+ if (!ZSTREAM_IS_FINISHED(&gz->z)) {
4410
+ rb_raise(cGzError, "unexpected end of file");
4411
+ }
4412
+ if (NIL_P(gz->z.input)) {
4377
4413
  rb_raise(cNoFooter, "footer is not found");
4414
+ }
4378
4415
  gzfile_check_footer(gz);
4379
4416
  return dst;
4380
4417
  }
@@ -4618,7 +4655,7 @@ Init_zlib(void)
4618
4655
  rb_define_alloc_func(cGzipWriter, rb_gzwriter_s_allocate);
4619
4656
  rb_define_method(cGzipWriter, "initialize", rb_gzwriter_initialize,-1);
4620
4657
  rb_define_method(cGzipWriter, "flush", rb_gzwriter_flush, -1);
4621
- rb_define_method(cGzipWriter, "write", rb_gzwriter_write, 1);
4658
+ rb_define_method(cGzipWriter, "write", rb_gzwriter_write, -1);
4622
4659
  rb_define_method(cGzipWriter, "putc", rb_gzwriter_putc, 1);
4623
4660
  rb_define_method(cGzipWriter, "<<", rb_gzwriter_addstr, 1);
4624
4661
  rb_define_method(cGzipWriter, "printf", rb_gzwriter_printf, -1);
data/zlib.gemspec CHANGED
@@ -2,8 +2,8 @@
2
2
  # frozen_string_literal: true
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "zlib"
5
- spec.version = "0.1.0"
6
- spec.date = '2017-09-13'
5
+ spec.version = "1.0.0"
6
+ spec.date = '2017-12-11'
7
7
  spec.authors = ["Yukihiro Matsumoto", "UENO Katsuhiro"]
8
8
  spec.email = ["matz@ruby-lang.org", nil]
9
9
 
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
  spec.extensions = "ext/zlib/extconf.rb"
20
- spec.required_ruby_version = ">= 2.5.0dev"
20
+ spec.required_ruby_version = ">= 2.3.0"
21
21
 
22
22
  spec.add_development_dependency "bundler"
23
23
  spec.add_development_dependency "rake"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-09-13 00:00:00.000000000 Z
12
+ date: 2017-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -85,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
- version: 2.5.0dev
88
+ version: 2.3.0
89
89
  required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  requirements:
91
91
  - - ">="
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubyforge_project:
96
- rubygems_version: 2.6.13
96
+ rubygems_version: 2.5.2.1
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Ruby interface for the zlib compression/decompression library