Project

General

Profile

Bug #5929 ยป zlib.c.set_dictionary.patch

drbrain (Eric Hodel), 01/26/2012 11:59 AM

View differences:

ext/zlib/zlib.c (working copy)
1732 1732
	return;
1733 1733
    }
1734 1734
    StringValue(src);
1735
    if (RSTRING_LEN(src) > 0) { /* prevent Z_BUF_ERROR */
1735
    if (RSTRING_LEN(src) > 0 || z->stream.avail_in > 0) { /* prevent Z_BUF_ERROR */
1736 1736
	zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH);
1737 1737
    }
1738 1738
}
......
1749 1749
 *
1750 1750
 * Raises a Zlib::NeedDict exception if a preset dictionary is needed to
1751 1751
 * decompress.  Set the dictionary by Zlib::Inflate#set_dictionary and then
1752
 * call this method again with an empty string.  (<i>???</i>)
1752
 * call this method again with an empty string to flush the stream:
1753
 *
1754
 *   inflater = Zlib::Inflate.new
1755
 *
1756
 *   begin
1757
 *     out = inflater.inflate compressed
1758
 *   rescue Zlib::NeedDict
1759
 *     # ensure the dictionary matches the stream's required dictionary
1760
 *     raise unless inflater.adler == Zlib.adler32(dictionary)
1761
 *
1762
 *     inflater.set_dictionary dictionary
1763
 *     inflater.inflate ''
1764
 *   end
1765
 *
1766
 *   # ...
1767
 *
1768
 *   inflater.close
1753 1769
 *
1754 1770
 * See also Zlib::Inflate.new
1755 1771
 */
test/zlib/test_zlib.rb (working copy)
195 195
      z << "foo" # ???
196 196
    end
197 197

  
198
    def test_inflate_dictionary
199
      dictionary = "foo"
200

  
201
      deflate = Zlib::Deflate.new
202
      deflate.set_dictionary dictionary
203
      compressed = deflate.deflate "foofoofoo", Zlib::FINISH
204
      deflate.close
205

  
206
      out = nil
207
      inflate = Zlib::Inflate.new
208

  
209
      begin
210
        out = inflate.inflate compressed
211

  
212
        flunk "Zlib::NeedDict was not raised"
213
      rescue Zlib::NeedDict
214
        inflate.set_dictionary dictionary
215
        out = inflate.inflate ""
216
      end
217

  
218
      assert_equal "foofoofoo", out
219
    end
220

  
198 221
    def test_sync
199 222
      z = Zlib::Deflate.new
200 223
      s = z.deflate("foo" * 1000, Zlib::FULL_FLUSH)