dissect.database 1.2.dev12__py3-none-any.whl → 1.2.dev13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,6 +26,7 @@ enum COMPRESSION_SCHEME {
26
26
  COMPRESS_SCRUB = 0x4,
27
27
  COMPRESS_XPRESS9 = 0x5,
28
28
  COMPRESS_XPRESS10 = 0x6,
29
+ COMPRESS_LZ4 = 0x7,
29
30
  };
30
31
 
31
32
  enum JET_coltyp {
@@ -23,6 +23,7 @@ class _c_ese(__cs__.cstruct):
23
23
  COMPRESS_SCRUB = ...
24
24
  COMPRESS_XPRESS9 = ...
25
25
  COMPRESS_XPRESS10 = ...
26
+ COMPRESS_LZ4 = ...
26
27
 
27
28
  class JET_coltyp(__cs__.Enum):
28
29
  Nil = ...
@@ -1,31 +1,74 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import struct
4
+ from typing import Final
4
5
 
5
- from dissect.util.compression import lzxpress, sevenbit
6
+ from dissect.util.compression import lz4, lzxpress, lzxpress9, sevenbit
7
+ from dissect.util.hash import crc32c, crc64c
6
8
 
7
9
  from dissect.database.ese.c_ese import COMPRESSION_SCHEME
8
10
 
11
+ _H: Final = struct.Struct("<H")
12
+ _I: Final = struct.Struct("<I")
9
13
 
10
- def decompress(buf: bytes) -> bytes:
14
+ _XPRESS_HEADER_SIZE: Final = 3
15
+ """Scheme byte + u16 LE uncompressed size."""
16
+
17
+ _XPRESS9_HEADER_SIZE: Final = 5
18
+ """Scheme byte + u32 LE plaintext CRC-32C (``Xpress9Header``)."""
19
+
20
+ _XPRESS10_HEADER: Final = struct.Struct("<BHIQ")
21
+ _XPRESS10_HEADER_SIZE: Final = 15
22
+ """Scheme byte + u16 LE size + u32 LE CRC-32C + u64 LE CRC-64 (``Xpress10Header``)."""
23
+
24
+ _LZ4_HEADER: Final = struct.Struct("<BH")
25
+ _LZ4_HEADER_SIZE: Final = 3
26
+ """Scheme byte + u16 LE uncompressed size (``Lz4Header``)."""
27
+
28
+
29
+ def decompress(buf: bytes, *, verify: bool = True) -> bytes:
11
30
  """Decompress the given bytes according to the encoded compression scheme.
12
31
 
32
+ Handles all seven ESE record compression formats:
33
+ ``COMPRESS_7BITASCII`` (0x1), ``COMPRESS_7BITUNICODE`` (0x2),
34
+ ``COMPRESS_XPRESS`` (0x3), ``COMPRESS_SCRUB`` (0x4),
35
+ ``COMPRESS_XPRESS9`` (0x5), ``COMPRESS_XPRESS10`` (0x6),
36
+ and ``COMPRESS_LZ4`` (0x7).
37
+
13
38
  Args:
14
39
  buf: The compressed bytes to decompress.
40
+ verify: When True, verify integrity checks on formats that carry them:
41
+ decoded-size match for XPRESS, CRC-32C for XPRESS9, CRC-64 and
42
+ CRC-32C for XPRESS10. LZ4 has no checksum. When False, skip all
43
+ integrity checks for speed or corrupt-data recovery.
15
44
 
16
45
  Raises:
17
- NotImplementedError: If the buffer is compressed with an unsupported compression algorithm (XPRESS9/XPRESS10).
46
+ ValueError: If the buffer is a SCRUB erase marker or an integrity
47
+ check fails (when ``verify`` is True).
18
48
  """
19
49
  identifier = buf[0] >> 3
50
+
20
51
  if identifier == COMPRESSION_SCHEME.COMPRESS_7BITASCII:
21
52
  return sevenbit.decompress(buf[1:])
53
+
22
54
  if identifier == COMPRESSION_SCHEME.COMPRESS_7BITUNICODE:
23
55
  return sevenbit.decompress(buf[1:], wide=True)
56
+
24
57
  if identifier == COMPRESSION_SCHEME.COMPRESS_XPRESS:
25
- return lzxpress.decompress(buf[3:])
26
- if identifier in (COMPRESSION_SCHEME.COMPRESS_XPRESS9, COMPRESSION_SCHEME.COMPRESS_XPRESS10):
27
- raise NotImplementedError(f"Compression not yet implemented: {COMPRESSION_SCHEME(identifier)}")
28
- # Not compressed
58
+ return _decompress_xpress(buf, verify=verify)
59
+
60
+ if identifier == COMPRESSION_SCHEME.COMPRESS_SCRUB:
61
+ raise ValueError("Record is a SCRUB erase marker: no plaintext is recoverable")
62
+
63
+ if identifier == COMPRESSION_SCHEME.COMPRESS_XPRESS9:
64
+ return _decompress_xpress9(buf, verify=verify)
65
+
66
+ if identifier == COMPRESSION_SCHEME.COMPRESS_XPRESS10:
67
+ return _decompress_xpress10(buf, verify=verify)
68
+
69
+ if identifier == COMPRESSION_SCHEME.COMPRESS_LZ4:
70
+ return _decompress_lz4(buf)
71
+
29
72
  return buf
30
73
 
31
74
 
@@ -36,15 +79,97 @@ def decompress_size(buf: bytes) -> int | None:
36
79
  buf: The compressed bytes to return the decompressed size of.
37
80
 
38
81
  Raises:
39
- NotImplementedError: If the buffer is compressed with an unsupported compression algorithm (XPRESS9/XPRESS10).
82
+ ValueError: If the buffer is a SCRUB erase marker.
40
83
  """
41
84
  identifier = buf[0] >> 3
85
+
42
86
  if identifier == COMPRESSION_SCHEME.COMPRESS_7BITASCII:
43
- return ((buf[0] & 7) + (8 * len(buf))) // 7
87
+ # Low 3 header bits hold the valid bit count of the final packed byte; the rest are full 8-bit bytes.
88
+ # https://github.com/microsoft/Extensible-Storage-Engine/blob/7030fe7407615160e54d152e4ef704eede2fdd7e/dev/ese/src/ese/compression.cxx#L2131-L2137
89
+ return ((len(buf) - 2) * 8 + (buf[0] & 7) + 1) // 7
90
+
44
91
  if identifier == COMPRESSION_SCHEME.COMPRESS_7BITUNICODE:
45
- return 2 * (((buf[0] & 7) + (8 * len(buf))) // 7)
46
- if identifier == COMPRESSION_SCHEME.COMPRESS_XPRESS:
47
- return struct.unpack("<H", buf[1:3])[0]
48
- if identifier in (COMPRESSION_SCHEME.COMPRESS_XPRESS9, COMPRESSION_SCHEME.COMPRESS_XPRESS10):
49
- raise NotImplementedError(f"Compression not yet implemented: {COMPRESSION_SCHEME(identifier)}")
92
+ return 2 * (((len(buf) - 2) * 8 + (buf[0] & 7) + 1) // 7)
93
+
94
+ if identifier in (
95
+ COMPRESSION_SCHEME.COMPRESS_XPRESS,
96
+ COMPRESSION_SCHEME.COMPRESS_XPRESS10,
97
+ COMPRESSION_SCHEME.COMPRESS_LZ4,
98
+ ):
99
+ return _H.unpack_from(buf, 1)[0]
100
+
101
+ if identifier == COMPRESSION_SCHEME.COMPRESS_SCRUB:
102
+ raise ValueError("Record is a SCRUB erase marker: no decompressed size")
103
+
104
+ if identifier == COMPRESSION_SCHEME.COMPRESS_XPRESS9:
105
+ return lzxpress9.decompressed_size(buf[_XPRESS9_HEADER_SIZE:])
106
+
50
107
  return None
108
+
109
+
110
+ def _decompress_xpress(buf: bytes, *, verify: bool = True) -> bytes:
111
+ """Decompress an XPRESS (0x3) cell: 3-byte ESE header + Plain LZ77 stream.
112
+
113
+ When ``verify`` is True, checks that the decoded length matches the
114
+ header's declared uncompressed size.
115
+
116
+ References:
117
+ - https://github.com/microsoft/Extensible-Storage-Engine/blob/7030fe7407615160e54d152e4ef704eede2fdd7e/dev/ese/src/ese/compression.cxx#L2316-L2327
118
+ """
119
+ declared = _H.unpack_from(buf, 1)[0]
120
+ plaintext = lzxpress.decompress(buf[_XPRESS_HEADER_SIZE:], declared)
121
+ if verify and len(plaintext) != declared:
122
+ raise ValueError(f"XPRESS decoded {len(plaintext)} bytes but header declares {declared}")
123
+ return plaintext
124
+
125
+
126
+ def _decompress_xpress9(buf: bytes, *, verify: bool = True) -> bytes:
127
+ """Decompress an XPRESS9 (0x5) cell: 5-byte ESE header + XPRESS9 blocks.
128
+
129
+ When ``verify`` is True, checks the CRC-32C of the plaintext against
130
+ the value stored in the header.
131
+
132
+ References:
133
+ - https://github.com/microsoft/Extensible-Storage-Engine/blob/7030fe7407615160e54d152e4ef704eede2fdd7e/dev/ese/src/ese/compression.cxx#L2458-L2465
134
+ """
135
+ stored_crc = _I.unpack_from(buf, 1)[0]
136
+ plaintext = lzxpress9.decompress(buf[_XPRESS9_HEADER_SIZE:])
137
+ if verify and (actual_crc := crc32c.crc32c(plaintext)) != stored_crc:
138
+ raise ValueError(f"XPRESS9 plaintext CRC-32C mismatch: 0x{stored_crc:08x} vs 0x{actual_crc:08x}")
139
+ return plaintext
140
+
141
+
142
+ def _decompress_xpress10(buf: bytes, *, verify: bool = True) -> bytes:
143
+ """Decompress an XPRESS10 (0x6) cell: 15-byte ESE header + LZ4 block.
144
+
145
+ When ``verify`` is True, checks the CRC-64/NVME of the compressed payload
146
+ and the CRC-32C of the plaintext.
147
+
148
+ References:
149
+ - https://github.com/microsoft/Extensible-Storage-Engine/blob/7030fe7407615160e54d152e4ef704eede2fdd7e/dev/ese/src/ese/compression.cxx#L2581-L2593
150
+ - https://github.com/microsoft/Extensible-Storage-Engine/blob/7030fe7407615160e54d152e4ef704eede2fdd7e/dev/ese/src/ese/compression.cxx#L2615-L2619
151
+ """
152
+ _, size, stored_crc32, stored_crc64 = _XPRESS10_HEADER.unpack_from(buf)
153
+ payload = buf[_XPRESS10_HEADER_SIZE:]
154
+
155
+ if verify and (actual_crc64 := crc64c.crc64c(payload)) != stored_crc64:
156
+ raise ValueError(f"XPRESS10 payload CRC-64 mismatch: 0x{stored_crc64:016x} vs 0x{actual_crc64:016x}")
157
+
158
+ plaintext = lz4.decompress(payload, size)
159
+
160
+ if verify and (actual_crc32 := crc32c.crc32c(plaintext)) != stored_crc32:
161
+ raise ValueError(f"XPRESS10 plaintext CRC-32C mismatch: 0x{stored_crc32:08x} vs 0x{actual_crc32:08x}")
162
+
163
+ return plaintext
164
+
165
+
166
+ def _decompress_lz4(buf: bytes) -> bytes:
167
+ """Decompress an LZ4 (0x7) cell: 3-byte ESE header + LZ4 block.
168
+
169
+ LZ4 carries no checksum, so there is nothing to verify.
170
+
171
+ References:
172
+ - https://github.com/microsoft/Extensible-Storage-Engine/blob/7030fe7407615160e54d152e4ef704eede2fdd7e/dev/ese/src/ese/compression.cxx#L2643-L2699
173
+ """
174
+ _, size = _LZ4_HEADER.unpack_from(buf)
175
+ return lz4.decompress(buf[_LZ4_HEADER_SIZE:], size)
@@ -215,7 +215,7 @@ class Table:
215
215
  if len(chunk) != next_chunk_offset - chunk_offset:
216
216
  chunk = compression.decompress(chunk)
217
217
  buf.append(chunk)
218
- chunk_offset += next_chunk_offset
218
+ chunk_offset = next_chunk_offset
219
219
 
220
220
  return b"".join(buf)
221
221
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.database
3
- Version: 1.2.dev12
3
+ Version: 1.2.dev13
4
4
  Summary: A Dissect module implementing parsers for various database formats, including Berkeley DB, Microsofts Extensible Storage Engine (ESE) and SQLite3
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
6
  License-Expression: Apache-2.0
@@ -22,13 +22,13 @@ Description-Content-Type: text/markdown
22
22
  License-File: LICENSE
23
23
  License-File: COPYRIGHT
24
24
  Requires-Dist: dissect.cstruct<5,>=4
25
- Requires-Dist: dissect.util<4,>=3.24
25
+ Requires-Dist: dissect.util<4,>=3.25.dev24
26
26
  Provides-Extra: full
27
27
  Requires-Dist: pycryptodome; extra == "full"
28
28
  Provides-Extra: dev
29
29
  Requires-Dist: dissect.database[full]; extra == "dev"
30
30
  Requires-Dist: dissect.cstruct<5.0.dev,>=4.0.dev; extra == "dev"
31
- Requires-Dist: dissect.util<4.0.dev,>=3.24; extra == "dev"
31
+ Requires-Dist: dissect.util<4.0.dev,>=3.25.dev; extra == "dev"
32
32
  Dynamic: license-file
33
33
 
34
34
  # dissect.database
@@ -13,9 +13,9 @@ dissect/database/bsd/tools/c_rpm.py,sha256=yI9JIerMRPCsO-QZRXzFmY4sAo1fvdwYvLL0S
13
13
  dissect/database/bsd/tools/c_rpm.pyi,sha256=a22dZ8eMGPnj9rsKWjT7ol82Q6OmEcTgWsLNBbBJ3IM,15116
14
14
  dissect/database/bsd/tools/rpm.py,sha256=Wpmhfr4w4_TTFpp94o1RCcjPkwrHxyebIUz-P03r3OE,7699
15
15
  dissect/database/ese/__init__.py,sha256=aF5V41QUz6KMUtzBQ1Mak3PlK-TbidbPxRNRnngNtLc,559
16
- dissect/database/ese/c_ese.py,sha256=kcn2GHTQ_fijY1CEX4gtNAUeTvu5feR-ya4C2_BzTpk,24643
17
- dissect/database/ese/c_ese.pyi,sha256=v-h3If5kUxS8HN8G23m6AHbIQUoGHQ2XZih4zurxWTM,18874
18
- dissect/database/ese/compression.py,sha256=lLrriyvVjUuDEibUl_ECYVfvqmeU9ijKIaBnx38m98Q,2027
16
+ dissect/database/ese/c_ese.py,sha256=dZxmqMzfoE3Ra6UdcH80Pz5UwmSS3epbnBcbdcS8UQQ,24678
17
+ dissect/database/ese/c_ese.pyi,sha256=86IMu4ToXaMmDmpyUWvoajc81DsJZHhK3hLwcSZHS9M,18901
18
+ dissect/database/ese/compression.py,sha256=R8WjYKFFB279EYmCWQU-Xl1NGq8amLkC-ylLnJ4yRc4,6986
19
19
  dissect/database/ese/cursor.py,sha256=ju-mUewhW22bVW0MMdsaKolKCiKnztRcyU0nJw5bNOg,14346
20
20
  dissect/database/ese/ese.py,sha256=MryiOFbbAE7G1spUk0d5liQTt14nLREVAFnfaj7QW18,3575
21
21
  dissect/database/ese/exception.py,sha256=09xbltclWIBsGbk8Ry8RlKG-OwGSQvyLKsgj0d98T0k,210
@@ -24,7 +24,7 @@ dissect/database/ese/lcmapstring.py,sha256=sI0jnSHnuCYwXS3YqBruGixlHXm44L3au5llR
24
24
  dissect/database/ese/page.py,sha256=TbR_pGBKCaYag1SBAYtcQp8EnGOw3QRp3tLMNeuVdCM,8325
25
25
  dissect/database/ese/record.py,sha256=DKBwkyuszsuUaBgu3iD41cidmw4GEpaoNNKPhI0Tnxg,19723
26
26
  dissect/database/ese/sorting_table.py,sha256=NsBchm4-XxyDKZrdt2M_wMuGELUQed5osKbv1RxmIbk,819383
27
- dissect/database/ese/table.py,sha256=BpaI2WHXvXnoe46bumkZn5_HyGqtl0jAnyinrqo81zA,13352
27
+ dissect/database/ese/table.py,sha256=JgjKBnRqPZmCX4DxaSnpJoYAJtGtfI8qyWlUDhyH3qA,13351
28
28
  dissect/database/ese/util.py,sha256=5ZWurLGHahG4LL4iXpYCK9kbMLr53dVh18f1Ox9YHlA,3044
29
29
  dissect/database/ese/ntds/__init__.py,sha256=Fu6_i_7fAlDfJoOgbpsVlPxM4CXpDCNtXmgzNzkmesY,264
30
30
  dissect/database/ese/ntds/c_ds.py,sha256=ZaWdEcpJPJrRjiJFhi1s5KlOIVj0yoix4XAh-D31uLU,3055
@@ -165,10 +165,10 @@ dissect/database/sqlite3/encryption/__init__.py,sha256=kJdFWXD9Z_O_QipC-_A9dlVfR
165
165
  dissect/database/sqlite3/encryption/sqlcipher/__init__.py,sha256=kJdFWXD9Z_O_QipC-_A9dlVfR6AOPSOoT8WBhpFbSsE,238
166
166
  dissect/database/sqlite3/encryption/sqlcipher/exception.py,sha256=GKNtzcnAKlWkvjLluruA8LfzCwjRRWubibbH8WM9l2o,121
167
167
  dissect/database/sqlite3/encryption/sqlcipher/sqlcipher.py,sha256=y_oJRKZqoJBeOQaBbniesZKm1sVTFvXZ466rJYZj2xE,11217
168
- dissect_database-1.2.dev12.dist-info/licenses/COPYRIGHT,sha256=pFH-OBYz6Xj23UB0Odz5IhoTR8nsTbJQNlCRV_wMaiE,317
169
- dissect_database-1.2.dev12.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
170
- dissect_database-1.2.dev12.dist-info/METADATA,sha256=N1-mIZW8jgxOgrh3RZQ9PgLfkmv-XF3sv0N4A4tSp_0,5541
171
- dissect_database-1.2.dev12.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
172
- dissect_database-1.2.dev12.dist-info/entry_points.txt,sha256=ZVVKj3Nzjkgm1kBXGWyGNVUJzTbmVgivv9lgFcuLkpk,343
173
- dissect_database-1.2.dev12.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
174
- dissect_database-1.2.dev12.dist-info/RECORD,,
168
+ dissect_database-1.2.dev13.dist-info/licenses/COPYRIGHT,sha256=pFH-OBYz6Xj23UB0Odz5IhoTR8nsTbJQNlCRV_wMaiE,317
169
+ dissect_database-1.2.dev13.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
170
+ dissect_database-1.2.dev13.dist-info/METADATA,sha256=EDrsAuGVQXZeyytuQCIeGKNSl9pfsa5jssev1UT7zC0,5551
171
+ dissect_database-1.2.dev13.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
172
+ dissect_database-1.2.dev13.dist-info/entry_points.txt,sha256=ZVVKj3Nzjkgm1kBXGWyGNVUJzTbmVgivv9lgFcuLkpk,343
173
+ dissect_database-1.2.dev13.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
174
+ dissect_database-1.2.dev13.dist-info/RECORD,,