Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix snappy crc32c checksum #10048

Merged
merged 1 commit into from
Feb 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ static int calculateChecksum(ByteBuf data, int offset, int length) {
Crc32c crc32 = new Crc32c();
try {
crc32.update(data, offset, length);
return maskChecksum((int) crc32.getValue());
return maskChecksum(crc32.getValue());
} finally {
crc32.reset();
}
Expand Down Expand Up @@ -655,7 +655,7 @@ static void validateChecksum(int expectedChecksum, ByteBuf data, int offset, int
* @param checksum The actual checksum of the data
* @return The masked checksum
*/
static int maskChecksum(int checksum) {
return (checksum >> 15 | checksum << 17) + 0xa282ead8;
static int maskChecksum(long checksum) {
return (int) ((checksum >> 15 | checksum << 17) + 0xa282ead8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void testInvalidChecksumDoesNotThrowException() throws Exception {
// checksum here is presented as a282986f (little endian)
ByteBuf in = Unpooled.wrappedBuffer(new byte[]{
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y'
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, 0x2e, -0x47, 'n', 'e', 't', 't', 'y'
});

assertTrue(channel.writeInbound(in));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ public void testSmallAmountOfDataIsUncompressed() throws Exception {

channel.writeOutbound(in);
assertTrue(channel.finish());

ByteBuf expected = Unpooled.wrappedBuffer(new byte[] {
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y'
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, 0x2e, -0x47, 'n', 'e', 't', 't', 'y'
});
ByteBuf actual = channel.readOutbound();
assertEquals(expected, actual);
Expand Down Expand Up @@ -89,8 +88,8 @@ public void testStreamStartIsOnlyWrittenOnce() throws Exception {

ByteBuf expected = Unpooled.wrappedBuffer(new byte[] {
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y',
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y',
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, 0x2e, -0x47, 'n', 'e', 't', 't', 'y',
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, 0x2e, -0x47, 'n', 'e', 't', 't', 'y',
});

CompositeByteBuf actual = Unpooled.compositeBuffer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,19 @@ public void testCalculateChecksum() {
ByteBuf input = Unpooled.wrappedBuffer(new byte[] {
'n', 'e', 't', 't', 'y'
});
assertEquals(maskChecksum(0xd6cb8b55), calculateChecksum(input));

assertEquals(maskChecksum(0xd6cb8b55L), calculateChecksum(input));
input.release();
}

@Test
public void testMaskChecksum() {
ByteBuf input = Unpooled.wrappedBuffer(new byte[] {
0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00,
0x5f, 0x68, 0x65, 0x61, 0x72, 0x74, 0x62, 0x65,
0x61, 0x74, 0x5f,
});
assertEquals(0x44a4301f, calculateChecksum(input));
input.release();
}

Expand Down