dissect.cstruct 5.0.dev7__py3-none-any.whl → 5.0.dev8__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.
@@ -30,12 +30,18 @@ from dissect.cstruct.types import (
30
30
  )
31
31
  from dissect.cstruct.util import (
32
32
  dumpstruct,
33
+ f16,
34
+ f32,
35
+ f64,
33
36
  hexdump,
34
37
  p8,
35
38
  p16,
36
39
  p32,
37
40
  p64,
38
41
  pack,
42
+ pf16,
43
+ pf32,
44
+ pf64,
39
45
  swap,
40
46
  swap16,
41
47
  swap32,
@@ -75,12 +81,18 @@ __all__ = [
75
81
  "ctypes",
76
82
  "ctypes_type",
77
83
  "dumpstruct",
84
+ "f16",
85
+ "f32",
86
+ "f64",
78
87
  "hexdump",
79
88
  "p8",
80
89
  "p16",
81
90
  "p32",
82
91
  "p64",
83
92
  "pack",
93
+ "pf16",
94
+ "pf32",
95
+ "pf64",
84
96
  "swap",
85
97
  "swap16",
86
98
  "swap32",
@@ -11,6 +11,7 @@ from typing import TYPE_CHECKING
11
11
  from dissect.cstruct.bitbuffer import BitBuffer
12
12
  from dissect.cstruct.types import (
13
13
  Array,
14
+ BaseArray,
14
15
  Char,
15
16
  CharArray,
16
17
  Flag,
@@ -24,18 +25,15 @@ from dissect.cstruct.types import (
24
25
  Wchar,
25
26
  WcharArray,
26
27
  )
27
- from dissect.cstruct.types.base import BaseArray
28
28
  from dissect.cstruct.types.enum import EnumMetaType
29
- from dissect.cstruct.types.packed import _struct
29
+ from dissect.cstruct.util import _struct
30
30
 
31
31
  if TYPE_CHECKING:
32
32
  from collections.abc import Iterator
33
33
  from types import MethodType
34
34
 
35
35
  from dissect.cstruct.cstruct import cstruct
36
- from dissect.cstruct.types import (
37
- BaseType,
38
- )
36
+ from dissect.cstruct.types import BaseType
39
37
  from dissect.cstruct.types.structure import Field
40
38
 
41
39
  SUPPORTED_TYPES = (
@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Any, BinaryIO, ClassVar, TypeVar
6
6
 
7
7
  from dissect.cstruct.exception import ArraySizeError
8
8
  from dissect.cstruct.expression import Expression
9
+ from dissect.cstruct.util import normalize_endianness
9
10
 
10
11
  if TYPE_CHECKING:
11
12
  from collections.abc import Callable
@@ -328,27 +329,5 @@ def _is_eof(stream: BinaryIO) -> bool:
328
329
  return False
329
330
 
330
331
 
331
- ENDIANNESS_MAP: dict[AllowedEndianness, Endianness] = {
332
- "<": "<",
333
- ">": ">",
334
- "!": "!",
335
- "@": "@",
336
- "=": "=",
337
- "network": "!",
338
- "little": "<",
339
- "big": ">",
340
- }
341
-
342
-
343
- def normalize_endianness(endian: AllowedEndianness) -> Endianness:
344
- """Normalize an endianness string to one of the standard format characters."""
345
- try:
346
- return ENDIANNESS_MAP[endian]
347
- except KeyError:
348
- raise ValueError(
349
- f"Invalid endianness: {endian!r}, expected one of {', '.join(ENDIANNESS_MAP.keys())}"
350
- ) from None
351
-
352
-
353
332
  # As mentioned in the BaseType class, we correctly set the type here
354
333
  MetaType.ArrayType = Array
@@ -1,10 +1,9 @@
1
1
  from __future__ import annotations
2
2
 
3
- from functools import lru_cache
4
- from struct import Struct
5
3
  from typing import TYPE_CHECKING, Any, BinaryIO, Generic, TypeVar
6
4
 
7
5
  from dissect.cstruct.types.base import EOF, BaseType
6
+ from dissect.cstruct.util import _struct
8
7
 
9
8
  if TYPE_CHECKING:
10
9
  from typing_extensions import Self
@@ -12,11 +11,6 @@ if TYPE_CHECKING:
12
11
  from dissect.cstruct.cstruct import Endianness
13
12
 
14
13
 
15
- @lru_cache(1024)
16
- def _struct(endian: str, packchar: str) -> Struct:
17
- return Struct(f"{endian}{packchar}")
18
-
19
-
20
14
  T = TypeVar("T", int, float)
21
15
 
22
16
 
dissect/cstruct/util.py CHANGED
@@ -3,15 +3,17 @@ from __future__ import annotations
3
3
  import os
4
4
  import pprint
5
5
  import string
6
+ import struct
6
7
  import sys
7
8
  from enum import Enum
9
+ from functools import lru_cache
8
10
  from typing import TYPE_CHECKING
9
11
 
10
12
  if TYPE_CHECKING:
11
13
  from collections.abc import Iterator
12
14
  from typing import Literal
13
15
 
14
- from dissect.cstruct.cstruct import AllowedEndianness
16
+ from dissect.cstruct.cstruct import AllowedEndianness, Endianness
15
17
  from dissect.cstruct.types.base import BaseType
16
18
  from dissect.cstruct.types.structure import Structure
17
19
 
@@ -64,6 +66,28 @@ ENDIANNESS_TO_BYTEORDER_MAP: dict[AllowedEndianness, Literal["big", "little"]] =
64
66
  "big": "big",
65
67
  }
66
68
 
69
+ ENDIANNESS_MAP: dict[AllowedEndianness, Endianness] = {
70
+ "<": "<",
71
+ ">": ">",
72
+ "!": "!",
73
+ "@": "@",
74
+ "=": "=",
75
+ "network": "!",
76
+ "little": "<",
77
+ "big": ">",
78
+ }
79
+
80
+
81
+ def normalize_endianness(endian: AllowedEndianness) -> Endianness:
82
+ """Normalize an endianness string to one of the standard format characters."""
83
+ try:
84
+ return ENDIANNESS_MAP[endian]
85
+ except KeyError:
86
+ raise ValueError(
87
+ f"Invalid endianness: {endian!r}, expected one of {', '.join(ENDIANNESS_MAP.keys())}"
88
+ ) from None
89
+
90
+
67
91
  Palette = list[tuple[int, str]]
68
92
 
69
93
 
@@ -327,6 +351,11 @@ def dumpstruct(
327
351
  return _dumpstruct(obj, obj.dumps(), offset, color, output, autoskip)
328
352
 
329
353
 
354
+ @lru_cache(1024)
355
+ def _struct(endian: str, packchar: str) -> struct.Struct:
356
+ return struct.Struct(f"{endian}{packchar}")
357
+
358
+
330
359
  def pack(value: int, size: int | None = None, endian: AllowedEndianness = "little") -> bytes:
331
360
  """Pack an integer value to a given bit size, endianness.
332
361
 
@@ -400,6 +429,36 @@ def p64(value: int, endian: AllowedEndianness = "little") -> bytes:
400
429
  return pack(value, 64, endian)
401
430
 
402
431
 
432
+ def pf16(value: float, endian: AllowedEndianness = "little") -> bytes:
433
+ """Pack a 16 bit float.
434
+
435
+ Arguments:
436
+ value: Value to pack.
437
+ endian: Endianness to use (little, big, network, <, >, !, @ or =).
438
+ """
439
+ return _struct(normalize_endianness(endian), "e").pack(value)
440
+
441
+
442
+ def pf32(value: float, endian: AllowedEndianness = "little") -> bytes:
443
+ """Pack a 32 bit float.
444
+
445
+ Arguments:
446
+ value: Value to pack.
447
+ endian: Endianness to use (little, big, network, <, >, !, @ or =).
448
+ """
449
+ return _struct(normalize_endianness(endian), "f").pack(value)
450
+
451
+
452
+ def pf64(value: float, endian: AllowedEndianness = "little") -> bytes:
453
+ """Pack a 64 bit float.
454
+
455
+ Arguments:
456
+ value: Value to pack.
457
+ endian: Endianness to use (little, big, network, <, >, !, @ or =).
458
+ """
459
+ return _struct(normalize_endianness(endian), "d").pack(value)
460
+
461
+
403
462
  def u8(value: bytes, endian: AllowedEndianness = "little", sign: bool = False) -> int:
404
463
  """Unpack an 8 bit integer.
405
464
 
@@ -444,6 +503,36 @@ def u64(value: bytes, endian: AllowedEndianness = "little", sign: bool = False)
444
503
  return unpack(value, 64, endian, sign)
445
504
 
446
505
 
506
+ def f16(value: bytes, endian: AllowedEndianness = "little") -> float:
507
+ """Unpack a 16 bit float.
508
+
509
+ Arguments:
510
+ value: Value to unpack.
511
+ endian: Endianness to use (little, big, network, <, >, !, @ or =).
512
+ """
513
+ return _struct(normalize_endianness(endian), "e").unpack(value)[0]
514
+
515
+
516
+ def f32(value: bytes, endian: AllowedEndianness = "little") -> float:
517
+ """Unpack a 32 bit float.
518
+
519
+ Arguments:
520
+ value: Value to unpack.
521
+ endian: Endianness to use (little, big, network, <, >, !, @ or =).
522
+ """
523
+ return _struct(normalize_endianness(endian), "f").unpack(value)[0]
524
+
525
+
526
+ def f64(value: bytes, endian: AllowedEndianness = "little") -> float:
527
+ """Unpack a 64 bit float.
528
+
529
+ Arguments:
530
+ value: Value to unpack.
531
+ endian: Endianness to use (little, big, network, <, >, !, @ or =).
532
+ """
533
+ return _struct(normalize_endianness(endian), "d").unpack(value)[0]
534
+
535
+
447
536
  def swap(value: int, size: int) -> int:
448
537
  """Swap the endianness of an integer with a given bit size.
449
538
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.cstruct
3
- Version: 5.0.dev7
3
+ Version: 5.0.dev8
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-Expression: Apache-2.0
@@ -1,32 +1,32 @@
1
- dissect/cstruct/__init__.py,sha256=8VNVQfPH4aH5V_kD-jvtLPwQ-ag9ppKHTy3dbxIR6ik,1378
1
+ dissect/cstruct/__init__.py,sha256=KOyNeUvu4c0JaAiyOfj7iftGwUIjTmD8MwHtFsLPLSU,1504
2
2
  dissect/cstruct/bitbuffer.py,sha256=lZuLUOluHx4mzFGRp62_FO45_iJwXayyzv9-v8b0Q08,2687
3
- dissect/cstruct/compiler.py,sha256=vVg2H-pi9ugQ8Xvhs1JQ--4BjA5gtO2tW-1W8jr36V4,14283
3
+ dissect/cstruct/compiler.py,sha256=hlCNd7EQml8bJxI-fQB-kLS8CSdU3_YxzgIkxJbSJiU,14224
4
4
  dissect/cstruct/cstruct.py,sha256=bsCesSpbE0H4MUsHwb2iEbeadOBlOSAVENwyoP-UkP8,24929
5
5
  dissect/cstruct/exception.py,sha256=RqD08LuBkbMqa3iQ1A05DNN8XgEmQR_SUnguwe9LGo8,316
6
6
  dissect/cstruct/expression.py,sha256=MEwqHGgqA2fUk4kYk4lRLDWNTAMhB4-saJ5BgTxl7OA,7956
7
7
  dissect/cstruct/lexer.py,sha256=NDUfAWgyxgRNRuVO8ExXQ51LqQXS2mv9bywVa_M7tFQ,19665
8
8
  dissect/cstruct/parser.py,sha256=xCK-1O8c86sVFvjaXXSRgTsJWWd0ncKM6KJf2wbyGG8,21348
9
- dissect/cstruct/util.py,sha256=S__kLEb9HKZg_WZR4CGtXDxkIkERLKbzFTHoU8FT8tU,15313
9
+ dissect/cstruct/util.py,sha256=0khT6G1B7ZjNR7kjD9ox30jDaky8ZxxdUaN5FPMWQ_o,17845
10
10
  dissect/cstruct/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
11
  dissect/cstruct/tools/stubgen.py,sha256=FLQieYE-MeKK31DcrDzwNUropUYHPjwdfcDb1X2L6j4,7651
12
12
  dissect/cstruct/types/__init__.py,sha256=6P71Pu94Dv7RNjZpDZ19pqzx-G0QzmQw4q0-3zWALso,911
13
- dissect/cstruct/types/base.py,sha256=rU1S2FQllx7X7BQG7qMOwNa7waxAJyuTHTNwtkiAz0g,12763
13
+ dissect/cstruct/types/base.py,sha256=JRc5LpEOZwXQD0Wjo_Q_dWc6lEFtLTW1Q56SCGdDgj4,12268
14
14
  dissect/cstruct/types/char.py,sha256=s4zDmnbuOjwHgcsnEihjSqgC6H3t1jsfBTYH2ykwvHU,2997
15
15
  dissect/cstruct/types/enum.py,sha256=YIWyP3H0tdxmuSI82enoULiN0YevpnU0UaY5ioBCYfI,8518
16
16
  dissect/cstruct/types/flag.py,sha256=7xxQjFEDPHSQOxBHgcc75WzPiAxflOquwr7Z7c3erLE,2347
17
17
  dissect/cstruct/types/int.py,sha256=Ewejng5pBYnQHPtzL5W0wQMJVUPYyrY8ALES8mFaabI,1324
18
18
  dissect/cstruct/types/leb128.py,sha256=V97XTJT_Xbw1LpTwTN1OkjPHuQQAQUxd8OGuA6yEDmE,2387
19
- dissect/cstruct/types/packed.py,sha256=78nQWm-yylgfK3858nN-XZhO5jiqyO-piGNEyPD2ps4,2458
19
+ dissect/cstruct/types/packed.py,sha256=NewM2M3CsQKHbByLL42sxaTTOFNoo8gC-dnYe_79qTg,2330
20
20
  dissect/cstruct/types/pointer.py,sha256=eGMrB_tTnKXKmYrcqyA_12IdukXcxRG3WOeQ1dgt7lk,3927
21
21
  dissect/cstruct/types/structure.py,sha256=7pBg0moZbszeRomFEp5CbAHZcd1J8IVTmEG9Mo2eKS0,37571
22
22
  dissect/cstruct/types/void.py,sha256=LCYnTcxsFPaxln36QEUxWuCZVioHRRckMuqtlm6tH54,1218
23
23
  dissect/cstruct/types/wchar.py,sha256=YCYlu91D9AMVpWOLbVMdpvDDURH-EqyppM5UMDU5UQw,3200
24
- dissect_cstruct-5.0.dev7.dist-info/licenses/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
25
- dissect_cstruct-5.0.dev7.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
26
- dissect_cstruct-5.0.dev7.dist-info/METADATA,sha256=IUF-_jFf7_GABYqAww23JWUTr-f73OKwmaICtDcOGc8,9255
27
- dissect_cstruct-5.0.dev7.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
28
- dissect_cstruct-5.0.dev7.dist-info/entry_points.txt,sha256=z53zqZqwD2OLrAkRwrP4wTeiU9CQe7xrMly0T2c0_wQ,71
29
- dissect_cstruct-5.0.dev7.dist-info/scm_file_list.json,sha256=nuFWhDGL6VmjcwOB94CSfjNBblnsa9imUy8X4BLyvhw,2306
30
- dissect_cstruct-5.0.dev7.dist-info/scm_version.json,sha256=wMGNV4ui6dgAUOW0hK23797wTuOlVvhVvvLFz_dXXFk,162
31
- dissect_cstruct-5.0.dev7.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
32
- dissect_cstruct-5.0.dev7.dist-info/RECORD,,
24
+ dissect_cstruct-5.0.dev8.dist-info/licenses/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
25
+ dissect_cstruct-5.0.dev8.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
26
+ dissect_cstruct-5.0.dev8.dist-info/METADATA,sha256=e7oamdNZkbBocBjcD_kkj0T-wSHGf5mPhSgWn-MWHos,9255
27
+ dissect_cstruct-5.0.dev8.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
28
+ dissect_cstruct-5.0.dev8.dist-info/entry_points.txt,sha256=z53zqZqwD2OLrAkRwrP4wTeiU9CQe7xrMly0T2c0_wQ,71
29
+ dissect_cstruct-5.0.dev8.dist-info/scm_file_list.json,sha256=nuFWhDGL6VmjcwOB94CSfjNBblnsa9imUy8X4BLyvhw,2306
30
+ dissect_cstruct-5.0.dev8.dist-info/scm_version.json,sha256=znogn9Q3i6GJ75IR8pSsX582lmewE1Hqievuy3IQk8U,162
31
+ dissect_cstruct-5.0.dev8.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
32
+ dissect_cstruct-5.0.dev8.dist-info/RECORD,,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "tag": "5.0.dev0",
3
- "distance": 7,
4
- "node": "g32253fe9f286383a8bda6b953c7c85571cd89833",
3
+ "distance": 8,
4
+ "node": "g3032ad71f5e15148222c2faef49e47112a36bf12",
5
5
  "dirty": true,
6
6
  "branch": "main",
7
7
  "node_date": "2026-07-20"