dissect.cstruct 4.0.dev3__tar.gz → 4.0.dev4__tar.gz

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.
Files changed (66) hide show
  1. {dissect_cstruct-4.0.dev3/dissect.cstruct.egg-info → dissect_cstruct-4.0.dev4}/PKG-INFO +1 -1
  2. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/compiler.py +8 -3
  3. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4/dissect.cstruct.egg-info}/PKG-INFO +1 -1
  4. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_bitfield.py +30 -0
  5. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_compiler.py +78 -1
  6. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/COPYRIGHT +0 -0
  7. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/LICENSE +0 -0
  8. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/MANIFEST.in +0 -0
  9. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/README.md +0 -0
  10. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/__init__.py +0 -0
  11. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/bitbuffer.py +0 -0
  12. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/cstruct.py +0 -0
  13. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/exceptions.py +0 -0
  14. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/expression.py +0 -0
  15. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/parser.py +0 -0
  16. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/__init__.py +0 -0
  17. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/base.py +0 -0
  18. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/char.py +0 -0
  19. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/enum.py +0 -0
  20. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/flag.py +0 -0
  21. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/int.py +0 -0
  22. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/leb128.py +0 -0
  23. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/packed.py +0 -0
  24. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/pointer.py +0 -0
  25. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/structure.py +0 -0
  26. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/void.py +0 -0
  27. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/types/wchar.py +0 -0
  28. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect/cstruct/utils.py +0 -0
  29. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect.cstruct.egg-info/SOURCES.txt +0 -0
  30. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect.cstruct.egg-info/dependency_links.txt +0 -0
  31. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/dissect.cstruct.egg-info/top_level.txt +0 -0
  32. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/examples/disk.py +0 -0
  33. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/examples/mirai.py +0 -0
  34. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/examples/pe.py +0 -0
  35. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/examples/protobuf.py +0 -0
  36. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/examples/secdesc.py +0 -0
  37. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/pyproject.toml +0 -0
  38. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/setup.cfg +0 -0
  39. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/__init__.py +0 -0
  40. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/conftest.py +0 -0
  41. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/data/testdef.txt +0 -0
  42. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/docs/Makefile +0 -0
  43. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/docs/conf.py +0 -0
  44. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/docs/index.rst +0 -0
  45. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_align.py +0 -0
  46. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_basic.py +0 -0
  47. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_bitbuffer.py +0 -0
  48. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_ctypes.py +0 -0
  49. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_expression.py +0 -0
  50. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_parser.py +0 -0
  51. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_base.py +0 -0
  52. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_char.py +0 -0
  53. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_custom.py +0 -0
  54. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_enum.py +0 -0
  55. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_flag.py +0 -0
  56. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_int.py +0 -0
  57. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_leb128.py +0 -0
  58. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_packed.py +0 -0
  59. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_pointer.py +0 -0
  60. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_structure.py +0 -0
  61. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_union.py +0 -0
  62. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_void.py +0 -0
  63. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_types_wchar.py +0 -0
  64. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/test_utils.py +0 -0
  65. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tests/utils.py +0 -0
  66. {dissect_cstruct-4.0.dev3 → dissect_cstruct-4.0.dev4}/tox.ini +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dissect.cstruct
3
- Version: 4.0.dev3
3
+ Version: 4.0.dev4
4
4
  Summary: A Dissect module implementing a parser for C-like structures: structure parsing in Python made easy
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
6
  License: Apache License 2.0
@@ -26,6 +26,7 @@ from dissect.cstruct.types import (
26
26
  Wchar,
27
27
  WcharArray,
28
28
  )
29
+ from dissect.cstruct.types.enum import EnumMetaType
29
30
  from dissect.cstruct.types.packed import _struct
30
31
 
31
32
  if TYPE_CHECKING:
@@ -108,6 +109,7 @@ class _ReadSourceGenerator:
108
109
  preamble = """
109
110
  r = {}
110
111
  s = {}
112
+ o = stream.tell()
111
113
  """
112
114
 
113
115
  if any(field.bits for field in self.fields):
@@ -149,7 +151,7 @@ class _ReadSourceGenerator:
149
151
 
150
152
  if field.offset is not None and field.offset != current_offset:
151
153
  # If a field has a set offset and it's not the same as the current tracked offset, seek to it
152
- yield f"stream.seek({field.offset})"
154
+ yield f"stream.seek(o + {field.offset})"
153
155
  current_offset = field.offset
154
156
 
155
157
  if self.align and field.offset is None:
@@ -158,6 +160,9 @@ class _ReadSourceGenerator:
158
160
  for field in self.fields:
159
161
  field_type = self.cs.resolve(field.type)
160
162
 
163
+ if isinstance(field_type, EnumMetaType):
164
+ field_type = field_type.type
165
+
161
166
  if not issubclass(field_type, SUPPORTED_TYPES):
162
167
  raise TypeError(f"Unsupported type for compiler: {field_type}")
163
168
 
@@ -190,10 +195,10 @@ class _ReadSourceGenerator:
190
195
  # Bit fields
191
196
  elif field.bits:
192
197
  if not prev_was_bits:
193
- prev_bits_type = field.type
198
+ prev_bits_type = field_type
194
199
  prev_was_bits = True
195
200
 
196
- if bits_remaining == 0 or prev_bits_type != field.type:
201
+ if bits_remaining == 0 or prev_bits_type != field_type:
197
202
  bits_remaining = (size * 8) - field.bits
198
203
  bits_rollover = True
199
204
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dissect.cstruct
3
- Version: 4.0.dev3
3
+ Version: 4.0.dev4
4
4
  Summary: A Dissect module implementing a parser for C-like structures: structure parsing in Python made easy
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
6
  License: Apache License 2.0
@@ -1,3 +1,5 @@
1
+ import io
2
+
1
3
  import pytest
2
4
 
3
5
  from dissect.cstruct.cstruct import cstruct
@@ -265,3 +267,31 @@ def test_bitfield_char(cs: cstruct, compiled: bool) -> None:
265
267
  assert obj.d == b"i420"
266
268
 
267
269
  assert obj.dumps() == buf
270
+
271
+
272
+ def test_bitfield_dynamic(cs: cstruct, compiled: bool) -> None:
273
+ cdef = """
274
+ enum A : uint16 {
275
+ A = 0x0
276
+ };
277
+
278
+ struct test {
279
+ uint16 size : 4;
280
+ A b : 4;
281
+ char d[size];
282
+ };
283
+ """
284
+
285
+ cs.load(cdef, compiled=compiled)
286
+ assert verify_compiled(cs.test, compiled)
287
+
288
+ buf = io.BytesIO(b"\x00\x00\xF4\x00help")
289
+ buf.seek(2)
290
+ obj = cs.test(buf)
291
+
292
+ assert obj.size == 4
293
+ assert obj.b == 0xF
294
+ assert obj.d == b"help"
295
+
296
+ buf.seek(2)
297
+ assert obj.dumps() == buf.read()
@@ -323,4 +323,81 @@ def test_generate_bits_read(cs: cstruct, TestEnum: type[Enum]) -> None:
323
323
  assert code == dedent(expected)
324
324
 
325
325
 
326
- # TODO: the rest of the compiler
326
+ @pytest.mark.parametrize("other_type", ["int8", "uint64"])
327
+ def test_generate_fields_dynamic_after_bitfield(cs: cstruct, TestEnum: Enum, other_type: str) -> None:
328
+ _type = getattr(cs, other_type)
329
+
330
+ fields = [
331
+ Field("size", cs.uint16, offset=0),
332
+ Field("a", TestEnum, 4, offset=2),
333
+ Field("b", _type, 4),
334
+ Field("c", cs.char["size"], offset=3),
335
+ ]
336
+
337
+ output = "\n".join(compiler._ReadSourceGenerator(cs, fields)._generate_fields())
338
+
339
+ expected = """
340
+ buf = stream.read(2)
341
+ if len(buf) != 2: raise EOFError()
342
+ data = _struct(cls.cs.endian, "H").unpack(buf)
343
+
344
+ r["size"] = type.__call__(_0, data[0])
345
+ s["size"] = 2
346
+
347
+
348
+ _t = _1
349
+ r["a"] = type.__call__(_t, bit_reader.read(_t.type, 4))
350
+
351
+
352
+ _t = _2
353
+ r["b"] = type.__call__(_t, bit_reader.read(_t, 4))
354
+
355
+ bit_reader.reset()
356
+ stream.seek(o + 3)
357
+
358
+ _s = stream.tell()
359
+ r["c"] = _3._read(stream, context=r)
360
+ s["c"] = stream.tell() - _s
361
+ """
362
+
363
+ assert output.strip() == dedent(expected).strip()
364
+
365
+
366
+ @pytest.mark.parametrize("other_type", ["int8", "uint64"])
367
+ def test_generate_fields_dynamic_before_bitfield(cs: cstruct, TestEnum: Enum, other_type: str) -> None:
368
+ _type = getattr(cs, other_type)
369
+
370
+ fields = [
371
+ Field("size", cs.uint16, offset=0),
372
+ Field("a", _type, 4, offset=2),
373
+ Field("b", TestEnum, 4),
374
+ Field("c", cs.char["size"], offset=3),
375
+ ]
376
+
377
+ output = "\n".join(compiler._ReadSourceGenerator(cs, fields)._generate_fields())
378
+
379
+ expected = """
380
+ buf = stream.read(2)
381
+ if len(buf) != 2: raise EOFError()
382
+ data = _struct(cls.cs.endian, "H").unpack(buf)
383
+
384
+ r["size"] = type.__call__(_0, data[0])
385
+ s["size"] = 2
386
+
387
+
388
+ _t = _1
389
+ r["a"] = type.__call__(_t, bit_reader.read(_t, 4))
390
+
391
+
392
+ _t = _2
393
+ r["b"] = type.__call__(_t, bit_reader.read(_t.type, 4))
394
+
395
+ bit_reader.reset()
396
+ stream.seek(o + 3)
397
+
398
+ _s = stream.tell()
399
+ r["c"] = _3._read(stream, context=r)
400
+ s["c"] = stream.tell() - _s
401
+ """
402
+
403
+ assert output.strip() == dedent(expected).strip()