Skip to content

Commit e131ad8

Browse files
authored
Merge pull request #1263 from micahsnyder/lzh-delharc-crc-check-fix
LZH: check CRC after reading file data
2 parents 0d4075e + 1da18af commit e131ad8

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

Diff for: libclamav_rust/src/scanners.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -209,21 +209,6 @@ pub unsafe extern "C" fn scan_lha_lzh(ctx: *mut cli_ctx) -> cl_error_t {
209209
// Get the file header.
210210
let header = decoder.header();
211211

212-
// Verify the CRC check. This is important because LHA/LZH does not have particularly identifiable magic bytes.
213-
match decoder.crc_check() {
214-
Ok(crc) => {
215-
// CRC is valid. Very likely it is indeed an LHA/LZH archive.
216-
debug!("CRC check passed. Very likely this is an LHA or LZH archive. CRC: {crc}");
217-
}
218-
Err(err) => {
219-
// Error checking CRC.
220-
// Use debug-level because may not actually be an LHA/LZH archive.
221-
// LHA/LZH does not have particularly identifiable magic bytes.
222-
debug!("An error occurred when checking the CRC of this LHA or LZH archive: {err}");
223-
// break;
224-
}
225-
}
226-
227212
let filepath = header.parse_pathname();
228213
let filename = filepath.to_string_lossy();
229214
if header.is_directory() {
@@ -254,11 +239,31 @@ pub unsafe extern "C" fn scan_lha_lzh(ctx: *mut cli_ctx) -> cl_error_t {
254239
debug!("err: unsupported compression method");
255240
} else {
256241
// Read the file into a buffer.
257-
let mut file_data = Vec::<u8>::new();
242+
let mut file_data: Vec<u8> = Vec::<u8>::new();
258243

259244
match decoder.read_to_end(&mut file_data) {
260245
Ok(bytes_read) => {
261246
if bytes_read > 0 {
247+
debug!(
248+
"Read {bytes_read} bytes from file {filename} in the LHA archive."
249+
);
250+
251+
// Verify the CRC check *after* reading the file.
252+
match decoder.crc_check() {
253+
Ok(crc) => {
254+
// CRC is valid. Very likely this is an LHA or LZH archive.
255+
debug!("CRC check passed. Very likely this is an LHA or LZH archive. CRC: {crc}");
256+
}
257+
Err(err) => {
258+
// Error checking CRC.
259+
debug!("An error occurred when checking the CRC of this LHA or LZH archive: {err}");
260+
261+
// Allow the scan to continue even with a CRC error, for now.
262+
// break;
263+
}
264+
}
265+
266+
// Scan the file.
262267
let ret =
263268
magic_scan(ctx, &file_data, Some(filename.to_string()));
264269
if ret != cl_error_t_CL_SUCCESS {

0 commit comments

Comments
 (0)