dissect.cstruct 3.14.dev2__py3-none-any.whl → 3.14.dev3__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.
dissect/cstruct/parser.py CHANGED
@@ -2,12 +2,21 @@ from __future__ import annotations
2
2
 
3
3
  import ast
4
4
  import re
5
- from typing import TYPE_CHECKING, Dict, List
5
+ from typing import TYPE_CHECKING, Dict, List, Optional
6
6
 
7
7
  from dissect.cstruct.compiler import Compiler
8
8
  from dissect.cstruct.exceptions import ParserError
9
9
  from dissect.cstruct.expression import Expression
10
- from dissect.cstruct.types import Array, Enum, Field, Flag, Pointer, Structure, Union
10
+ from dissect.cstruct.types import (
11
+ Array,
12
+ BaseType,
13
+ Enum,
14
+ Field,
15
+ Flag,
16
+ Pointer,
17
+ Structure,
18
+ Union,
19
+ )
11
20
 
12
21
  if TYPE_CHECKING:
13
22
  from dissect.cstruct import cstruct
@@ -158,6 +167,9 @@ class TokenParser(Parser):
158
167
 
159
168
  names = self._names(tokens)
160
169
  for name in names:
170
+ type_, name, bits = self._parse_field_type(type_, name)
171
+ if bits is not None:
172
+ raise ParserError(f"line {self._lineno(tokens.previous)}: typedefs cannot have bitfields")
161
173
  self.cstruct.addtype(name, type_)
162
174
 
163
175
  def _struct(self, tokens: TokenConsumer, register: bool = False) -> None:
@@ -236,9 +248,15 @@ class TokenParser(Parser):
236
248
  raise ParserError(f"line {self._lineno(tokens.next)}: expected name")
237
249
  nametok = tokens.consume()
238
250
 
251
+ type_, name, bits = self._parse_field_type(type_, nametok.value)
252
+
253
+ tokens.eol()
254
+ return Field(name.strip(), type_, bits)
255
+
256
+ def _parse_field_type(self, type_: BaseType, name: str) -> tuple[BaseType, str, Optional[int]]:
239
257
  pattern = self.TOK.patterns[self.TOK.NAME]
240
258
  # Dirty trick because the regex expects a ; but we don't want it to be part of the value
241
- d = pattern.match(nametok.value + ";").groupdict()
259
+ d = pattern.match(name + ";").groupdict()
242
260
 
243
261
  name = d["name"]
244
262
  count_expression = d["count"]
@@ -269,8 +287,7 @@ class TokenParser(Parser):
269
287
 
270
288
  type_ = Array(self.cstruct, type_, count)
271
289
 
272
- tokens.eol()
273
- return Field(name, type_, int(d["bits"]) if d["bits"] else None)
290
+ return type_, name, int(d["bits"]) if d["bits"] else None
274
291
 
275
292
  def _names(self, tokens: TokenConsumer) -> List[str]:
276
293
  names = []
@@ -564,6 +581,7 @@ class TokenConsumer:
564
581
  def __init__(self, tokens: List[Token]):
565
582
  self.tokens = tokens
566
583
  self.flags = []
584
+ self.previous = None
567
585
 
568
586
  def __contains__(self, token) -> bool:
569
587
  return token in self.tokens
@@ -582,7 +600,8 @@ class TokenConsumer:
582
600
  return None
583
601
 
584
602
  def consume(self) -> Token:
585
- return self.tokens.pop(0)
603
+ self.previous = self.tokens.pop(0)
604
+ return self.previous
586
605
 
587
606
  def reset_flags(self) -> None:
588
607
  self.flags = []
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dissect.cstruct
3
- Version: 3.14.dev2
3
+ Version: 3.14.dev3
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
@@ -4,7 +4,7 @@ dissect/cstruct/compiler.py,sha256=htgYvaAQiH_e9XFdAH4M1nMzwwfizycorathMz6p5Z4,1
4
4
  dissect/cstruct/cstruct.py,sha256=koqYcltVplqz7sEi6DrOXwm1bBiYDqoORyCsQ9x0smk,9728
5
5
  dissect/cstruct/exceptions.py,sha256=WqsUW4OiIpRLQLvixfLrfKl0rtvU1qx7pvfBrz9Sz-I,293
6
6
  dissect/cstruct/expression.py,sha256=Thd_TuEOz_46LLRVIZI4ePcSnXYJp1PUeuIKx7AJ8h0,10659
7
- dissect/cstruct/parser.py,sha256=Zc-y5NkfOi4By6wAZKffpcyfqFtYHrSyHs8veMMuaBQ,19489
7
+ dissect/cstruct/parser.py,sha256=Mt4PLT5MGtEq__UyRK9HLL2Z0kxi7q16cw4dZayrdos,20028
8
8
  dissect/cstruct/utils.py,sha256=z5cGtQ5IRMf9KfI67vFx-ZiFDoU6JFmRNn-7avgHkCA,9752
9
9
  dissect/cstruct/types/__init__.py,sha256=b6DoyHXJ38yNa2Ku7tYI3kpIsC-SB7wCXBBdx0Yp1IU,948
10
10
  dissect/cstruct/types/base.py,sha256=UPG1Djfo3IPM11Zy87yI2XNS_rT82xbpofSuD9wvZmk,6377
@@ -18,9 +18,9 @@ dissect/cstruct/types/pointer.py,sha256=ghfZ9y638KCzem0rXgYeeTPXbYhacHfWx8uD003G
18
18
  dissect/cstruct/types/structure.py,sha256=epjVmMUCxoMNCi5xMoMIyw9Ujp_g2cSEPcZl7eJN0bE,13442
19
19
  dissect/cstruct/types/voidtype.py,sha256=JaKEIjOsGTARn8g30vv7WCH_OAdEkKt4Mw6Oneo_3Mw,301
20
20
  dissect/cstruct/types/wchartype.py,sha256=pVp_xlu4xHdxzQG6FB30IxNSwnKRC183jleeUG4nzQ4,1693
21
- dissect.cstruct-3.14.dev2.dist-info/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
22
- dissect.cstruct-3.14.dev2.dist-info/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
23
- dissect.cstruct-3.14.dev2.dist-info/METADATA,sha256=KGpVIvqtLpwzTBgdUYqag78cI8ufFv_4pUMW1NoHYq8,8303
24
- dissect.cstruct-3.14.dev2.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
25
- dissect.cstruct-3.14.dev2.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
26
- dissect.cstruct-3.14.dev2.dist-info/RECORD,,
21
+ dissect.cstruct-3.14.dev3.dist-info/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
22
+ dissect.cstruct-3.14.dev3.dist-info/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
23
+ dissect.cstruct-3.14.dev3.dist-info/METADATA,sha256=ndcjqZiVqb63BCmy55Uw4T32El_XdVDmEWNrL3oYqds,8303
24
+ dissect.cstruct-3.14.dev3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
25
+ dissect.cstruct-3.14.dev3.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
26
+ dissect.cstruct-3.14.dev3.dist-info/RECORD,,