|
| 1 | +import os |
| 2 | +import struct |
| 3 | + |
| 4 | +from puremagic.scanners.helpers import Match |
| 5 | + |
| 6 | +match_bytes = b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" |
| 7 | +match_bytes_short = b"\xd0\xcf\x11\xe0" |
| 8 | + |
| 9 | +# Stream names that identify specific CFBF-based formats, checked in priority order. |
| 10 | +# Each entry: (stream_name, extension, name, mime_type) |
| 11 | +# Using startswith for prefix matching where noted. |
| 12 | +_STREAM_MATCHES = [ |
| 13 | + ("__nameid_version1.0", ".msg", "Outlook Message", "application/vnd.ms-outlook"), |
| 14 | + ("PowerPoint Document", ".ppt", "PowerPoint Presentation", "application/vnd.ms-powerpoint"), |
| 15 | + ("Current User", ".ppt", "PowerPoint Presentation", "application/vnd.ms-powerpoint"), |
| 16 | + ("Workbook", ".xls", "Excel Spreadsheet", "application/vnd.ms-excel"), |
| 17 | + ("Book", ".xls", "Excel Spreadsheet", "application/vnd.ms-excel"), |
| 18 | + ("WordDocument", ".doc", "Word Document", "application/msword"), |
| 19 | + ("VisioDocument", ".vsd", "Visio Drawing", "application/x-visio"), |
| 20 | + ("Quill", ".pub", "Publisher Document", "application/x-mspublisher"), |
| 21 | +] |
| 22 | + |
| 23 | +_PREFIX_MATCHES = [ |
| 24 | + ("__substg1.0_", ".msg", "Outlook Message", "application/vnd.ms-outlook"), |
| 25 | +] |
| 26 | + |
| 27 | + |
| 28 | +def _extract_stream_names(dir_data: bytes) -> set[str]: |
| 29 | + """Parse CFBF directory entries and return the set of stream/storage names.""" |
| 30 | + names: set[str] = set() |
| 31 | + for i in range(0, len(dir_data), 128): |
| 32 | + entry = dir_data[i : i + 128] |
| 33 | + if len(entry) < 128: |
| 34 | + break |
| 35 | + name_size = struct.unpack_from("<H", entry, 64)[0] |
| 36 | + if name_size < 2 or name_size > 64: |
| 37 | + continue |
| 38 | + obj_type = entry[66] |
| 39 | + # obj_type: 0=unknown, 1=storage, 2=stream, 5=root |
| 40 | + if obj_type not in (1, 2, 5): |
| 41 | + continue |
| 42 | + name = entry[: name_size - 2].decode("utf-16-le", errors="ignore") |
| 43 | + if name: |
| 44 | + names.add(name) |
| 45 | + return names |
| 46 | + |
| 47 | + |
| 48 | +def _identify_format(stream_names: set[str]) -> Match | None: |
| 49 | + """Match stream names against known CFBF format signatures.""" |
| 50 | + # Check prefix matches first (e.g. __substg1.0_ for MSG) |
| 51 | + for name in stream_names: |
| 52 | + for prefix, ext, fmt_name, mime in _PREFIX_MATCHES: |
| 53 | + if name.startswith(prefix): |
| 54 | + return Match(ext, fmt_name, mime) |
| 55 | + |
| 56 | + # Check exact stream name matches in priority order |
| 57 | + for stream_name, ext, fmt_name, mime in _STREAM_MATCHES: |
| 58 | + if stream_name in stream_names: |
| 59 | + return Match(ext, fmt_name, mime) |
| 60 | + |
| 61 | + return None |
| 62 | + |
| 63 | + |
| 64 | +def main(file_path: os.PathLike, head: bytes, foot: bytes) -> Match | None: |
| 65 | + if len(head) < 76: |
| 66 | + return None |
| 67 | + |
| 68 | + # Verify magic bytes |
| 69 | + if head[:8] != match_bytes: |
| 70 | + if head[:4] != match_bytes_short: |
| 71 | + return None |
| 72 | + |
| 73 | + # Parse CFBF header |
| 74 | + sector_shift = struct.unpack_from("<H", head, 30)[0] |
| 75 | + if sector_shift not in (9, 12): |
| 76 | + return None |
| 77 | + sector_size = 1 << sector_shift |
| 78 | + |
| 79 | + first_dir_secid = struct.unpack_from("<i", head, 48)[0] |
| 80 | + if first_dir_secid < 0: |
| 81 | + return None |
| 82 | + |
| 83 | + # Directory sector offset: header occupies first sector_size bytes |
| 84 | + dir_offset = (first_dir_secid + 1) * sector_size |
| 85 | + |
| 86 | + try: |
| 87 | + with open(file_path, "rb") as f: |
| 88 | + f.seek(dir_offset) |
| 89 | + dir_data = f.read(sector_size) |
| 90 | + except (OSError, ValueError): |
| 91 | + return None |
| 92 | + |
| 93 | + if not dir_data: |
| 94 | + return None |
| 95 | + |
| 96 | + stream_names = _extract_stream_names(dir_data) |
| 97 | + return _identify_format(stream_names) |
0 commit comments