Skip to content

Commit

Permalink
fix snappy crc32c checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Xie authored and andyxning committed Feb 27, 2020
1 parent 5c458c9 commit 05c4eb8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
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

0 comments on commit 05c4eb8

Please sign in to comment.