ruby-changes:71608
From: Nobuyoshi <ko1@a...>
Date: Sat, 2 Apr 2022 17:01:49 +0900 (JST)
Subject: [ruby-changes:71608] 1cbdedec89 (master): [ruby/zlib] Mask checksums to lower 32bits
https://git.ruby-lang.org/ruby.git/commit/?id=1cbdedec89 From 1cbdedec895070df1df96d05370cf8da084ab6fa Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sat, 2 Apr 2022 16:06:11 +0900 Subject: [ruby/zlib] Mask checksums to lower 32bits Upper bits affect the result of `crc32` in zlib 1.2.12. https://github.com/ruby/zlib/commit/9ab6d04af1 --- ext/zlib/zlib.c | 12 +++++++++--- test/zlib/test_zlib.rb | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 5cd45c9b26..168b3d0a0a 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -374,18 +374,24 @@ rb_zlib_version(VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L374 return rb_str_new2(zlibVersion()); } +#if SIZEOF_LONG * CHAR_BIT > 32 +# define mask32(x) ((x) & 0xffffffff) +#else +# define mask32(x) (x) +#endif + #if SIZEOF_LONG > SIZEOF_INT static uLong checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, long len) { if (len > UINT_MAX) { do { - sum = func(sum, ptr, UINT_MAX); + sum = func(mask32(sum), ptr, UINT_MAX); ptr += UINT_MAX; len -= UINT_MAX; } while (len >= UINT_MAX); } - if (len > 0) sum = func(sum, ptr, (uInt)len); + if (len > 0) sum = func(mask32(sum), ptr, (uInt)len); return sum; } #else @@ -411,7 +417,7 @@ do_checksum(int argc, VALUE *argv, uLong (*func)(uLong, const Bytef*, uInt)) https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L417 } if (NIL_P(str)) { - sum = func(sum, Z_NULL, 0); + sum = func(mask32(sum), Z_NULL, 0); } else if (rb_obj_is_kind_of(str, rb_cIO)) { VALUE buf; diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index a9de827ca8..00a13af95b 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -1303,6 +1303,7 @@ if defined? Zlib https://github.com/ruby/ruby/blob/trunk/test/zlib/test_zlib.rb#L1303 assert_equal(0x02820145, Zlib.adler32("foo")) assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo"))) assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000)) + assert_equal(0x97d1a9f7, Zlib.adler32("p", -305419897)) Tempfile.create("test_zlib_gzip_file_to_io") {|t| File.binwrite(t.path, "foo") t.rewind @@ -1338,6 +1339,7 @@ if defined? Zlib https://github.com/ruby/ruby/blob/trunk/test/zlib/test_zlib.rb#L1339 assert_equal(0x8c736521, Zlib.crc32("foo")) assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo"))) assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000)) + assert_equal(0xf136439b, Zlib.crc32("p", -305419897)) Tempfile.create("test_zlib_gzip_file_to_io") {|t| File.binwrite(t.path, "foo") t.rewind -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/