Skip to content

[ISSUE #425]🐛Fix the value of the body CRC32 does not match the version in Java #426

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

Merged
merged 1 commit into from
Jun 4, 2024
Merged
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
13 changes: 8 additions & 5 deletions rocketmq-common/src/utils/crc32_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,36 @@

// Calculate the CRC32 checksum for the given byte array
pub fn crc32(buf: &[u8]) -> u32 {
if buf.is_empty() {
return 0;

Check warning on line 23 in rocketmq-common/src/utils/crc32_utils.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/utils/crc32_utils.rs#L23

Added line #L23 was not covered by tests
}
//crc32fast::hash(buf)
let mut hasher = Hasher::new();
hasher.update(buf);
hasher.finalize()
hasher.finalize() & 0x7FFFFFFF
}

pub fn crc32_bytes_offset(array: &[u8], offset: usize, length: usize) -> u32 {
if !array.is_empty() && offset < array.len() && offset + length <= array.len() {
let mut hasher = Hasher::new();
hasher.update(&array[offset..offset + length]);
return hasher.finalize();
return hasher.finalize() & 0x7FFFFFFF;
}
0
}

pub fn crc32_bytebuffer(byte_buffer: &mut Vec<u8>) -> u32 {
let mut hasher = Hasher::new();
hasher.update(byte_buffer.as_slice());
hasher.finalize()
hasher.finalize() & 0x7FFFFFFF
}

pub fn crc32_bytebuffers(byte_buffers: &mut Vec<Vec<u8>>) -> u32 {
let mut hasher = Hasher::new();
for buffer in byte_buffers {
hasher.update(buffer.as_slice());
}
hasher.finalize()
hasher.finalize() & 0x7FFFFFFF
}

#[cfg(test)]
Expand All @@ -61,7 +64,7 @@
#[test]
fn crc32_bytes_offset_calculates_correct_checksum() {
let buf = [1, 2, 3, 4, 5];
assert_eq!(crc32_bytes_offset(&buf, 1, 3), 3498416806);
assert_eq!(crc32_bytes_offset(&buf, 1, 3), 1350933158);
}

#[test]
Expand Down
Loading