|
| 1 | +_CONTROL_CHARS = b'\n\r\t\f\b' |
| 2 | +_PRINTABLE_ASCII = _CONTROL_CHARS + bytes(range(32, 127)) |
| 3 | +_PRINTABLE_HIGH_ASCII = bytes(range(127, 256)) |
| 4 | + |
| 5 | +# BOM signatures for encodings that legitimately contain null bytes |
| 6 | +_BOM_ENCODINGS = ( |
| 7 | + (b'\xff\xfe\x00\x00', 'utf-32-le'), |
| 8 | + (b'\x00\x00\xfe\xff', 'utf-32-be'), |
| 9 | + (b'\xff\xfe', 'utf-16-le'), |
| 10 | + (b'\xfe\xff', 'utf-16-be'), |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def _has_bom_encoding(bytes_to_check: bytes) -> bool: |
| 15 | + """Check if bytes start with a BOM and can be decoded as that encoding.""" |
| 16 | + for bom, encoding in _BOM_ENCODINGS: |
| 17 | + if bytes_to_check.startswith(bom): |
| 18 | + try: |
| 19 | + bytes_to_check.decode(encoding) |
| 20 | + return True |
| 21 | + except (UnicodeDecodeError, LookupError): |
| 22 | + pass |
| 23 | + return False |
| 24 | + |
| 25 | + |
| 26 | +def _is_decodable_as_utf8(bytes_to_check: bytes) -> bool: |
| 27 | + """Try to decode bytes as UTF-8.""" |
| 28 | + try: |
| 29 | + bytes_to_check.decode('utf-8') |
| 30 | + return True |
| 31 | + except UnicodeDecodeError: |
| 32 | + return False |
| 33 | + |
| 34 | + |
| 35 | +def is_binary_string(bytes_to_check: bytes) -> bool: |
| 36 | + """Check if a chunk of bytes appears to be binary content. |
| 37 | +
|
| 38 | + Uses a simplified version of the Perl detection algorithm, matching |
| 39 | + the structure of binaryornot's is_binary_string. |
| 40 | + """ |
| 41 | + if not bytes_to_check: |
| 42 | + return False |
| 43 | + |
| 44 | + # Binary if control chars are > 30% of the string |
| 45 | + low_chars = bytes_to_check.translate(None, _PRINTABLE_ASCII) |
| 46 | + nontext_ratio1 = len(low_chars) / len(bytes_to_check) |
| 47 | + |
| 48 | + # Binary if high ASCII chars are < 5% of the string |
| 49 | + high_chars = bytes_to_check.translate(None, _PRINTABLE_HIGH_ASCII) |
| 50 | + nontext_ratio2 = len(high_chars) / len(bytes_to_check) |
| 51 | + |
| 52 | + is_likely_binary = (nontext_ratio1 > 0.3 and nontext_ratio2 < 0.05) or ( |
| 53 | + nontext_ratio1 > 0.8 and nontext_ratio2 > 0.8 |
| 54 | + ) |
| 55 | + |
| 56 | + # BOM-marked UTF-16/32 files legitimately contain null bytes. |
| 57 | + # Check this first so they aren't misdetected as binary. |
| 58 | + if _has_bom_encoding(bytes_to_check): |
| 59 | + return False |
| 60 | + |
| 61 | + has_null_or_xff = b'\x00' in bytes_to_check or b'\xff' in bytes_to_check |
| 62 | + |
| 63 | + if is_likely_binary: |
| 64 | + # Only let UTF-8 rescue data that doesn't contain null bytes. |
| 65 | + # Null bytes are valid UTF-8 but almost never appear in real text files, |
| 66 | + # whereas binary formats (e.g. .DS_Store) are full of them. |
| 67 | + if has_null_or_xff: |
| 68 | + return True |
| 69 | + return not _is_decodable_as_utf8(bytes_to_check) |
| 70 | + |
| 71 | + # Null bytes or 0xff in otherwise normal-looking data indicate binary |
| 72 | + return bool(has_null_or_xff) |
0 commit comments