Allow null bytes in UTF16 input files

pull/454/head
Wilfred Hughes 2022-12-13 09:38:24 +07:00
parent 7b31be8adb
commit c5985c88b2
1 changed files with 37 additions and 18 deletions

@ -132,22 +132,6 @@ pub enum ProbableFileKind {
/// Do these bytes look like a binary (non-textual) format?
pub fn guess_content(bytes: &[u8]) -> ProbableFileKind {
// Only consider the first 1,000 bytes, as tree_magic_mini
// considers the entire file, which is very slow on large files.
let mut magic_bytes = bytes;
if magic_bytes.len() > 1000 {
magic_bytes = &magic_bytes[..1000];
}
// Null bytes are rare in typical text files, but common in binary
// files like images. A surprising number of binary files are also
// valid UTF-8/UTF-16, so consider null bytes to be a strong hint
// that it's binary.
let null_byte_count = magic_bytes.iter().filter(|b| **b == 0).count();
if null_byte_count >= 10 {
return ProbableFileKind::Binary;
}
// If the bytes are entirely valid UTF-8, treat them as a string.
if let Ok(valid_utf8_string) = std::str::from_utf8(bytes) {
return ProbableFileKind::Text(valid_utf8_string.to_string());
@ -160,6 +144,13 @@ pub fn guess_content(bytes: &[u8]) -> ProbableFileKind {
return ProbableFileKind::Text(valid_utf16_string);
}
// Only consider the first 1,000 bytes, as tree_magic_mini
// considers the entire file, which is very slow on large files.
let mut magic_bytes = bytes;
if magic_bytes.len() > 1000 {
magic_bytes = &magic_bytes[..1000];
}
// Use MIME type detection to guess whether a file is binary. This
// has false positives and false negatives, so only check the MIME
// type after allowing perfect text files (see issue #433).
@ -292,8 +283,36 @@ mod tests {
fn test_png_bytes_are_binary() {
let bytes = vec![
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48,
0x44, 0x52, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x00, 0x01, 0x2c, 0x08, 0x02, 0x00, 0x00,
0x00, 0xf6, 0x1f, 0x19,
0x44, 0x52, 0x00, 0x00, 0x03, 0x76, 0x00, 0x00, 0x01, 0xed, 0x08, 0x06, 0x00, 0x00,
0x01, 0x22, 0x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 0xfd, 0x76, 0x02, 0x42, 0xc4, 0xb5, 0x8d, 0xc5, 0x84,
0x44, 0x06, 0x7c, 0x09, 0xc9, 0x42, 0x09, 0xbe, 0xc6, 0x58, 0x43, 0x20, 0x8e, 0x8d,
0xfc, 0x0c, 0x56, 0x1e, 0x93, 0xcb, 0x71, 0x92, 0x82, 0x60, 0x16, 0x37,
];
assert_eq!(guess_content(&bytes), ProbableFileKind::Binary);
}