Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,24 @@ rb_zlib_version(VALUE klass)
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
Expand All @@ -411,7 +417,7 @@ do_checksum(int argc, VALUE *argv, uLong (*func)(uLong, const Bytef*, uInt))
}

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;
Expand Down
2 changes: 2 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,7 @@ def test_adler32
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
Expand Down Expand Up @@ -1338,6 +1339,7 @@ def test_crc32
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
Expand Down