-
Notifications
You must be signed in to change notification settings - Fork 59
/
helpers.py
1240 lines (1023 loc) · 45.3 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""This modules contains helper functions for the construct library."""
import os
import io
import re
import string
import uuid
import zlib
from typing import Iterable
from mwcp.utils import custombase64, elffileutils, pefileutils
# Patch with version 2.8 changes.
from mwcp.utils.construct import core as construct
from .core import *
BYTE = Byte
WORD = Int16ul
DWORD = ULONG = Int32ul
QWORD = ULONGLONG = Int64ul
def chunk(seq, size):
"""
Returns an iterator that yields full chunks seq into size chunks.
>>> list(chunk('hello', 2))
[('h', 'e'), ('l', 'l')]
>>> list(chunk('hello!', 2))
[('h', 'e'), ('l', 'l'), ('o', '!')]
"""
return zip(*([iter(seq)] * size))
class Boolean(Adapter):
r"""
Adapter used to convert parsed value into a boolean.
NOTE: While similar to construct.Flag, this adapter accepts any value other than 0 or '' as true.
And will work with more than just construct.Byte.
WARNING: Due to the lossy nature, this can't be used to build.
e.g.
>>> Boolean(Int32ul).parse(b'\x01\x02\x03\x04')
True
>>> Boolean(Int32ul).parse(b'\x00\x00\x00\x00')
False
>>> Boolean(CString()).parse(b'hello\x00')
True
>>> Boolean(CString()).parse(b'\x00')
False
"""
def _decode(self, obj, context, path):
return bool(obj)
class ErrorMessage(Construct):
r"""
Raises an exception when triggered by parse or build. Can be used as a sentinel that blows a whistle when a conditional branch goes the wrong way, or to raise an error explicitly the declarative way.
This modification allows the ability to supply a custom message.
Example::
>>> d = "x"/Int8sb >> IfThenElse(this.x > 0, Int8sb, ErrorMessage('Failed if statement'))
>>> d.parse(b"\xff\x05")
Traceback (most recent call last):
...
construct.core.ExplicitError: Failed if statement
"""
def __init__(self, message="Error field was activated."):
super().__init__()
self.message = message
def _parse(self, stream, context, path):
message = self.message(context) if callable(self.message) else self.message
raise ExplicitError(message)
def _build(self, obj, stream, context, path):
message = self.message(context) if callable(self.message) else self.message
raise ExplicitError(message)
def String16(length):
r"""
Creates UTF-16 (little endian) encoded string.
>>> String16(10).build(u'hello')
b'h\x00e\x00l\x00l\x00o\x00'
>>> String16(10).parse(b'h\x00e\x00l\x00l\x00o\x00')
'hello'
>>> String16(16).parse(b'h\x00e\x00l\x00l\x00o\x00\x00\x00\x00\x00\x00\x00')
'hello'
"""
return String(length, encoding='utf-16-le')
def String32(length):
r"""
Creates UTF-32 (little endian) encoded string.
>>> String32(20).build(u'hello')
b'h\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00'
>>> String32(20).parse(b'h\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00')
'hello'
"""
return String(length, encoding='utf-32-le')
class Printable(Validator):
r"""
Validator used to validate that a parsed String (or Bytes) is a printable (ascii) string.
NOTE: A ValidationError is a type of ConstructError and will be cause if catching ConstructError.
>>> Printable(String(5)).parse(b'hello')
'hello'
>>> Printable(String(5)).parse(b'he\x11o!')
Traceback (most recent call last):
...
construct.core.ValidationError: Error in path (parsing)
object failed validation: heo!
>>> Printable(Bytes(3)).parse(b'\x01NO')
Traceback (most recent call last):
...
construct.core.ValidationError: Error in path (parsing)
object failed validation: b'\x01NO'
>>> Printable(Bytes(3)).parse(b'YES')
b'YES'
"""
def _validate(self, obj, context, path):
if isinstance(obj, bytes):
return all(chr(byte) in string.printable for byte in obj)
return isinstance(obj, str) and all(char in string.printable for char in obj)
# Continuously parses until it hits the first non-zero byte.
SkipNull = Const(b'\x00')[:]
# Continuously parses until it hits the first zero byte (consumed).
# Use this instead of CString() if you can't guarantee it won't fail to decode.
CBytes = NullTerminated(GreedyBytes)
class BytesTerminated(NullTerminated):
r"""
BytesTerminated is the same as NullTerminated except that it is targeted for binary data and not strings, and
therefore the terminator can be an arbitrary length (as opposed to having length equal to the character width).
See the NullTerminated documentation for the remainder of the functionality and options.
>>> BytesTerminated(GreedyBytes, term=b'TERM').parse(b'helloTERM')
b'hello'
"""
# The only method we need to override is _parse. Everything else from NullTerminated works as-is.
def _parse(self, stream, context, path):
term = self.term
term_len = len(term)
if term_len < 1:
raise PaddingError("BytesTerminated term must be at least 1 byte")
data = b''
while True:
pos = stream_tell(stream, path)
try:
b = stream_read(stream, term_len)
stream_seek(stream, pos, 0, path)
except StreamError:
if self.require:
raise
else:
stream_seek(stream, pos, 0, path)
data += stream_read_entire(stream)
break
if b == term:
if self.include:
data += b
if self.consume:
stream_read(stream, term_len, path)
break
else:
data += stream_read(stream, 1, path)
if self.subcon is GreedyBytes:
return data
if type(self.subcon) is GreedyString:
return data.decode(self.subcon.encoding)
return self.subcon._parsereport(io.BytesIO(data), context, path)
# TODO: Make a LStripped?
class Stripped(Adapter):
r"""
An adapter that strips characters/bytes from the right of the parsed results.
NOTE: While this may look similar to Padded() this is different because this
doesn't take a length and instead strips out the nulls from within the already parsed subconstruct.
:param subcon: The sub-construct to wrap.
:param pad: The character/bytes to use for stripping. Defaults to null character.
>>> Stripped(GreedyBytes).parse(b'hello\x00\x00\x00')
b'hello'
>>> Stripped(Bytes(10)).parse(b'hello\x00\x00\x00\x00\x00')
b'hello'
>>> Stripped(Bytes(14), pad=b'PAD').parse(b'helloPADPADPAD')
b'hello'
>>> Stripped(Bytes(14), pad=b'PAD').build(b'hello')
b'helloPADPADPAD'
>>> Stripped(CString(), pad=u'PAD').parse(b'helloPADPAD\x00')
'hello'
>>> Stripped(String(14), pad=u'PAD').parse(b'helloPADPAD\x00\x00\x00')
'hello'
# WARNING: If padding doesn't fit in the perscribed data it will not strip it!
>>> Stripped(Bytes(13), pad=b'PAD').parse(b'helloPADPADPA')
b'helloPADPADPA'
>>> Stripped(Bytes(13), pad=b'PAD').build(b'hello')
Traceback (most recent call last):
...
construct.core.StreamError: Error in path (building)
bytes object of wrong length, expected 13, found 5
# If the wrapped subconstruct's size can't be determined, if defaults to not providing a pad.
>>> Stripped(CString(), pad=u'PAD').build(u'hello')
b'hello\x00'
"""
def __init__(self, subcon, pad=None):
super().__init__(subcon)
self.pad = pad
def _decode(self, obj, context, path):
pad = self.pad
if pad is None:
pad = u'\0' if isinstance(obj, unicodestringtype) else b'\x00'
if not isinstance(pad, type(obj)):
raise PaddingError(f"NullStripped pad must be of the same type: {type(pad)} vs {type(obj)}")
unit = len(pad)
if unit < 1:
raise PaddingError("NullStripped pad must be at least 1 byte")
obj = obj
if unit == 1:
obj = obj.rstrip(pad)
else:
tailunit = len(obj) % unit
end = len(obj)
if tailunit and obj[-tailunit:] == pad[:tailunit]:
end -= tailunit
while end-unit >= 0 and obj[end-unit:end] == pad:
end -= unit
obj = obj[:end]
return obj
def _encode(self, obj, context, path):
pad = self.pad
if pad is None:
pad = u'\0' if isinstance(self.subcon, StringEncoded) else b'\x00'
try:
size = self.subcon._sizeof(context, path)
except SizeofError:
return obj # Don't pad if we can't figure out size.
unit = len(pad)
if unit == 1:
obj = obj.ljust(size, pad)
# Only pad if it fits in nicely.
elif (size - len(obj)) % unit == 0:
obj = (obj + (pad * (size - len(obj))))[:size]
return obj
class HexString(Adapter):
r"""
Adapter used to convert an int into a hex string equivalent.
e.g.
>>> HexString(Int32ul).build('0x123')
b'#\x01\x00\x00'
>>> HexString(Int32ul).parse(b'\x20\x01\x00\x00')
'0x120'
>>> HexString(Int16ub).parse(b'\x12\x34')
'0x1234'
>>> HexString(BytesInteger(20)).parse(b'\x01' * 20)
'0x101010101010101010101010101010101010101'
"""
def _encode(self, obj, context, path):
return int(obj, 16)
def _decode(self, obj, context, path):
hex_string = hex(obj)
if hex_string.endswith('L'):
hex_string = hex_string[:-1]
return hex_string
class Base64(Adapter):
r"""
Adapter used to Base64 encoded/decode a value.
WARNING: This adapter must be used on a unicode string value.
:param subcon: the construct to wrap
:param custom_alpha: optional custom alphabet to use
e.g.
>>> Base64(GreedyString()).build(b'hello')
b'aGVsbG8='
>>> Base64(GreedyString()).parse(b'aGVsbG8=')
b'hello'
>>> Base64(GreedyBytes).build(b'\x01\x02\x03\x04')
b'AQIDBA=='
>>> Base64(GreedyBytes).parse(b'AQIDBA==')
b'\x01\x02\x03\x04'
NOTE: String size is based on the encoded version.
>>> Base64(String(16)).build('hello world')
b'aGVsbG8gd29ybGQ='
>>> Base64(String(16)).parse(b'aGVsbG8gd29ybGQ=')
b'hello world'
Supplying a custom alphabet is also supported.
>>> spec = Base64(String(16), custom_alpha=b'EFGHQRSTUVWefghijklmnopIJKLMNOPABCDqrstuvwxyXYZabcdz0123456789+/=')
>>> spec.build('hello world')
b'LSoXMS8BO29dMSj='
>>> spec.parse(b'LSoXMS8BO29dMSj=')
b'hello world'
"""
def __init__(self, subcon, custom_alpha=None):
super().__init__(subcon)
self.custom_alpha = custom_alpha
def _encode(self, obj, context, path):
obj = custombase64.b64encode(obj, alphabet=self.custom_alpha)
# Convert to unicode if wrapped subcon expects it.
if isinstance(self.subcon, StringEncoded):
obj = obj.decode('utf-8')
return obj
def _decode(self, obj, context, path):
if isinstance(obj, str):
obj = obj.encode('utf-8')
try:
return custombase64.b64decode(obj, alphabet=self.custom_alpha)
except binascii.Error as e:
raise ConstructError(f"[{path}] {e}")
class ZLIB(Adapter):
r"""
Adapter used to zlib compress/decompress a data buffer
:param subcon: The construct to wrap
:param int level: The zlib compression level
:param int wbits: The zlib decompression window size
:param int bufsize: The initial output buffer size
>>> ZLIB(Bytes(12)).build(b'data')
b'x\x9cKI,I\x04\x00\x04\x00\x01\x9b'
>>> ZLIB(GreedyBytes, level=0).build(b'data')
b'x\x01\x01\x04\x00\xfb\xffdata\x04\x00\x01\x9b'
>>> ZLIB(GreedyBytes).parse(b'x^KI,I\x04\x00\x04\x00\x01\x9b')
b'data'
"""
def __init__(self, subcon, wbits=None, bufsize=None, level=None):
super().__init__(subcon)
self.wbits = wbits
self.bufsize = bufsize
self.level = level
def _encode(self, obj, context, path):
level = self.level(context) if callable(self.level) else self.level
if level is not None:
return zlib.compress(obj, level)
return zlib.compress(obj)
def _decode(self, obj, context, path):
"""
ZLIB decompress a buffer, cannot use bufsize if wbits is not set
:param obj:
:param context:
:return:
"""
wbits = self.wbits(context) if callable(self.wbits) else self.wbits
bufsize = self.bufsize(context) if callable(self.bufsize) else self.bufsize
if wbits is not None and bufsize is not None:
return zlib.decompress(obj, wbits, bufsize)
elif wbits is not None:
return zlib.decompress(obj, wbits)
return zlib.decompress(obj)
class UUIDAdapter(Adapter):
r"""
Adapter used to convert parsed bytes to a string representing the UUID.
Adapter can decode 16 bytes straight or in little-endian order if you set le=True.
e.g.
>>> UUIDAdapter(Bytes(16)).build('{12345678-1234-5678-1234-567812345678}')
b'xV4\x124\x12xV\x124Vx\x124Vx'
>>> UUIDAdapter(Bytes(16), le=False).build('{12345678-1234-5678-1234-567812345678}')
b'\x124Vx\x124Vx\x124Vx\x124Vx'
>>> UUIDAdapter(Bytes(16)).parse(b'xV4\x124\x12xV\x124Vx\x124Vx')
'{12345678-1234-5678-1234-567812345678}'
"""
def __init__(self, subcon, le=True):
super().__init__(subcon)
self.le = le
def _encode(self, obj, context, path):
obj = uuid.UUID(obj)
if self.le:
return obj.bytes_le
else:
return obj.bytes
def _decode(self, obj, context, path):
if self.le:
_uuid = uuid.UUID(bytes_le=obj)
else:
_uuid = uuid.UUID(bytes=obj)
return '{' + str(_uuid) + '}'
def UUID(le=True):
r"""A convenience function for using the UUIDAdapter with 16 bytes.
:param le: Whether to use "bytes_le" or "bytes" when constructing the UUID.
e.g.
>>> UUID().build('{12345678-1234-5678-1234-567812345678}')
b'xV4\x124\x12xV\x124Vx\x124Vx'
>>> UUID(le=False).build('{12345678-1234-5678-1234-567812345678}')
b'\x124Vx\x124Vx\x124Vx\x124Vx'
>>> UUID().parse(b'xV4\x124\x12xV\x124Vx\x124Vx')
'{12345678-1234-5678-1234-567812345678}'
>>> UUID(le=False).parse(b'\x124Vx\x124Vx\x124Vx\x124Vx')
'{12345678-1234-5678-1234-567812345678}'
"""
return UUIDAdapter(Bytes(16), le=le)
def ELFPointer(mem_off, subcon, elf=None):
r"""
Pointer for ELF files. This works for both memory sizes.
NOTE: This only works for x86 instructions. For other architectures,
please see the "ELFPointer" within their respective submodules. (e.g. construct.ARM.ELFPointer)
spec.parse(file_data, pe=elf_object)
:param mem_off: an int or a function that represents the memory offset for the equivalent physical offset.
:param subcon: the subcon to use at the offset
:param elf: Optional elftools.ELFFile file object.
(if not supplied here, this must be supplied during parse()/build()
"""
def _obtain_physical_offset(ctx):
_elf = elf or ctx._params.elf
_mem_off = mem_off(ctx) if callable(mem_off) else mem_off
phy_off = elffileutils.obtain_physical_offset(_mem_off, elf=_elf)
if phy_off is None:
raise ConstructError('Unable to decode virtual address')
return phy_off
return Pointer(_obtain_physical_offset, subcon)
class PEPhysicalAddress(Adapter):
r"""
Adapter used to convert an int representing a PE memory address into a physical address.
The PE object can either be passed into the specific construct, or as a keyword arument in
the parse()/build() functions.
If passed in through parse()/build(), the same PE object will be used for all instances.
This Adapter is useful when used along-side the Pointer construct:
spec = Struct(
'offset' / PEPhysicalAddress(Int32ul),
'data' / Pointer(this.offset, Bytes(100))
)
e.g.
>> with open(r'C:\32bit_exe', 'rb') as fo:
... file_data = fo.read()
>> pe = pefileutils.obtain_pe(file_data)
>> PEPhysicalAddress(Int32ul, pe=pe).build(100)
'd\x00@\x00'
>> PEPhysicalAddress(Int32ul, pe=pe).parse(b'd\x00@\x00')
100
>> PEPhysicalAddress(Int32ul).build(100, pe=pe)
'd\x00@\x00'
>> PEPhysicalAddress(Int32ul).parse(b'd\x00@\x00', pe=pe)
100
"""
def __init__(self, subcon, pe=None):
"""
:param pe: Optional PE file object. (if not supplied here, this must be supplied during parse()/build()
:param subcon: subcon to parse memory offset.
"""
super().__init__(subcon)
self._pe = pe
def _encode(self, obj, context, path):
pe = self._pe or context._params.pe
address = pefileutils.obtain_memory_offset(obj, pe=pe)
if address is None:
raise ConstructError('Unable to encode physical address.')
return address
def _decode(self, obj, context, path):
pe = self._pe or context._params.pe
address = pefileutils.obtain_physical_offset(obj, pe=pe)
if address is None:
raise ConstructError('Unable to decode virtual address.')
return address
def PEPointer(mem_off, subcon, pe=None):
r"""
This is an alternative to PEPhysicalAddress when you are using the address along with Pointer
Simplifies:
spec = Struct(
'offset' / PEPhysicalAddress(Int32ul),
'data' / Pointer(this.offset, Bytes(100))
)
to:
spec = Struct(
'offset' / Int32ul,
'data' / PEPointer(this.offset, Bytes(100))
)
spec.parse(file_data, pe=pe_object)
:param mem_off: an int or a function that represents the memory offset for the equivalent physical offset.
:param subcon: the subcon to use at the offset
:param pe: Optional PE file object. (if not supplied here, this must be supplied during parse()/build()
"""
def _obtain_physical_offset(ctx):
_pe = pe or ctx._params.pe
_mem_off = mem_off(ctx) if callable(mem_off) else mem_off
phy_off = pefileutils.obtain_physical_offset(_mem_off, pe=_pe)
if phy_off is None:
raise ConstructError('Unable to decode virtual address')
return phy_off
return Pointer(_obtain_physical_offset, subcon)
def PEPointer64(mem_off, inst_end, subcon, pe=None):
r"""
This is the 64-bit version of PEPointer.
This subconstruct takes an extra argument which specifies
the location of the end of the instruction for which the memory_offset was used.
(A parameter necessary for 64-bit)
Example:
spec = Struct(
'offset' / Int32ul,
Padding(2),
'inst_end' / Tell,
'data' / PEPointer64(this.offset, this.inst_end, Byte(100))
)
spec = Struct(
'instruction' / Regex(
'\x01\x03(?P<data_ptr>.{4})\x04\x05(?P<end>)\x06\x07', data_ptr=DWORD, end=Tell),
'data' / PEPointer64(this.instruction.data_ptr, this.instruction.end, Bytes(100))
)
spec.parse(file_data, pe=pe_object)
:param mem_off: an int or a function that represents the memory offset for the equivelent physical offset.
:param inst_end: an int or a function that represents the location of the end of the instruction to be relative to.
:param subcon: the subcon to use at the offset
:param pe: Optional PE file object. (if not supplied here, this must be supplied during parse()/build()
"""
def _obtain_physical_offset(ctx):
_pe = pe or ctx._params.pe
_mem_off = mem_off(ctx) if callable(mem_off) else mem_off
_inst_end = inst_end(ctx) if callable(inst_end) else inst_end
phy_off = pefileutils.obtain_physical_offset_x64(_mem_off, _inst_end, pe=_pe)
if phy_off is None:
raise ConstructError('Unable to decode virtual address')
return phy_off
return Pointer(_obtain_physical_offset, subcon)
class Delimited(Construct):
r"""
A construct used to parse delimited data.
NOTE: The parsed constructs will be buffered
>>> spec = Delimited(b'|',
... 'first' / CString(),
... 'second' / Int32ul,
... # When using a Greedy construct, either all data till EOF or the next delimiter will be consumed.
... 'third' / GreedyBytes,
... 'fourth' / Byte
... )
>>> spec.parse(b'Hello\x00\x00|\x01\x00\x00\x00|world!!\x01\x02|\xff')
Container(first=u'Hello', second=1, third=b'world!!\x01\x02', fourth=255)
>>> spec.build(dict(first=u'Hello', second=1, third=b'world!!\x01\x02', fourth=255))
b'Hello\x00|\x01\x00\x00\x00|world!!\x01\x02|\xff'
If you don't care about a particular element, you can leave it nameless just like in Structs.
# NOTE: You can't build unless you have supplied every attribute.
>>> spec = Delimited(b'|',
... 'first' / CString(),
... 'second' / Int32ul,
... Pass,
... 'fourth' / Byte
... )
>>> spec.parse(b'Hello\x00\x00|\x01\x00\x00\x00|world!!\x01\x02|\xff')
Container(first=u'Hello', second=1, fourth=255)
It may also be useful to use Pass or Optional for fields that may not exist.
>>> spec = Delimited(b'|',
... 'first' / CString(),
... 'second' / Pass,
... 'third' / Optional(Int32ul)
... )
>>> spec.parse(b'Hello\x00\x00|dont care|\x01\x00\x00\x00')
Container(first=u'Hello', second=None, third=1)
>>> spec.parse(b'Hello\x00\x00||')
Container(first=u'Hello', second=None, third=None)
delimiters may have a length > 1
>>> spec = Delimited(b'YOYO',
... 'first' / CString(),
... 'second' / Int32ul,
... # When using a Greedy construct, either all data till EOF or the next delimiter will be consumed.
... 'third' / GreedyBytes,
... 'fourth' / Byte
... )
>>> spec.parse(b'Hello\x00\x00YOYO\x01\x00\x00\x00YOYOworld!!YO!!\x01\x02YOYO\xff')
Container(first=u'Hello', second=1, third=b'world!!YO!!\x01\x02', fourth=255)
>>> spec.build(dict(first=u'Hello', second=1, third=b'world!!YO!!\x01\x02', fourth=255))
b'Hello\x00YOYO\x01\x00\x00\x00YOYOworld!!YO!!\x01\x02YOYO\xff'
# TODO: Add support for using a single construct for parsing an unknown number of times
# (or within a min, max, or exact)
# (Perhaps call it "Split" to avoid overloading too much functionality.)
# e.g.
# >>> spec = Delimited(b'|', GreedyString())
# >>> spec.parse(b'hello|world')
# ['hello', 'world']
# >>> spec.parse(b'hello|world|hi|bob')
# ['hello', 'world', 'hi', 'bob']
# >>> spec.parse(b'hello')
# ['hello']
"""
def __init__(self, delimiter, *subcons):
"""
:param delimiter: single character or a function that takes context and returns the delimiter
:param subcons: constructs to use to parse each element.
NOTE: The number of constructs will be the number of elements delimited.
(ie. len(subcons) == number of delimiters + 1)
:raises ValueError: If no subcons are defined.
"""
super().__init__()
self.delimiter = delimiter
self.subcons = subcons
if len(subcons) < 2:
raise ValueError('At least two subconstruct must be defined.')
def _find_delimiter(self, stream, delimiter):
"""
Finds given delimiter in stream.
:returns: Stream offset for delimiter.
:raises ConstructError: If delimiter isn't found.
"""
fallback = stream.tell()
try:
for byte in iter(lambda: stream.read(1), b''):
if delimiter[0] == ord(byte):
delimiter_offset = stream.seek(-1, os.SEEK_CUR)
if stream.read(len(delimiter)) == delimiter:
return delimiter_offset
else:
stream.seek(delimiter_offset + 1)
raise ConstructError(f'Unable to find delimiter: {delimiter}')
finally:
stream.seek(fallback)
def _parse_subcon(self, subcon, stream, obj, context, path):
"""Parses and fills obj and context."""
subobj = subcon._parsereport(stream, context, path)
if getattr(subcon, "flagembedded", False):
if subobj is not None:
obj.update(subobj.items())
context.update(subobj.items())
else:
if subcon.name is not None:
obj[subcon.name] = subobj
context[subcon.name] = subobj
def _parse(self, stream, context, path):
delimiter = self.delimiter(context) if callable(self.delimiter) else self.delimiter
if not isinstance(delimiter, bytestringtype) or not delimiter:
raise ValueError('Invalid delimiter.')
obj = Container()
context = Container(_=context)
# Parse all but the last element.
for sc in self.subcons[:-1]:
# Don't count probes as an element.
if isinstance(sc, Probe):
sc._parsereport(stream, context, path)
continue
delimiter_offset = self._find_delimiter(stream, delimiter)
# Temporarily fake the read() so that we can force EOF before delimiter.
orig_read = stream.read
def new_read(size=None):
max_size = delimiter_offset - stream.tell()
if size is None:
size = max_size
else:
size = min(max_size, size)
return orig_read(size)
try:
stream.read = new_read
self._parse_subcon(sc, stream, obj, context, path)
finally:
stream.read = orig_read
# Align to after delimiter
stream.seek(delimiter_offset + len(delimiter))
# Parse the last element.
self._parse_subcon(self.subcons[-1], stream, obj, context, path)
return obj
def _build(self, obj, stream, context, path):
delimiter = self.delimiter(context) if callable(self.delimiter) else self.delimiter
if not isinstance(delimiter, bytestringtype) or not delimiter:
raise ValueError('Invalid delimiter.')
context = Container(_=context)
context.update(obj)
for i, sc in enumerate(self.subcons):
if getattr(sc, "flagembedded", False):
subobj = obj
elif sc.flagbuildnone:
subobj = obj.get(sc.name, None)
else:
subobj = obj[sc.name]
buildret = sc._build(subobj, stream, context, path)
if buildret is not None:
if getattr(sc, "flagembedded", False):
context.update(buildret)
if sc.name is not None:
context[sc.name] = buildret
# Add delimiter if not last element and not Probe.
if i < len(self.subcons) - 1 and not isinstance(sc, Probe):
stream.write(delimiter)
return context
class Regex(Construct):
r"""
A construct designed look for the first match for the given regex, then parse the data collected in the groups.
Returns the matched capture groups in attributes based on their respective names.
If a subconstruct is defined for a group, it will run that construct on that particular piece of data.
NOTE: The subconstruct will run on the data as if is the only data that exists. Therefore, using Seek and Tell
will be purely relative to that piece of data only. This was done to ensure you are only parsing what has been
captured. (If you need to use Seek or Tell, you will have to instead make a capture group that collects no data.)
NOTE: If you supply a string as the regular expression, the re.DOTALL flag will be automatically specified.
If you need to use different flags, you must past a compiled regex.
The seek position is left at the end of the successful match (match.end()).
>>> regex = re.compile(b'\x01\x02(?P<size>.{4})\x03\x04(?P<path>[A-Za-z].*\x00)', re.DOTALL)
>>> data = b'GARBAGE!\x01\x02\x0A\x00\x00\x00\x03\x04C:\Windows\x00MORE GARBAGE!'
>>> r = Regex(regex, size=Int32ul, path=CString()).parse(data)
>>> r == Container(path='C:\\Windows', size=10)
True
>>> r = Regex(regex).parse(data)
>>> r == Container(path=b'C:\\Windows\x00', size=b'\n\x00\x00\x00')
True
>>> r = Struct(
... 're' / Regex(regex, size=Int32ul, path=CString()),
... 'after_re' / Tell,
... 'garbage' / GreedyBytes
... ).parse(data)
>>> r == Container(re=Container(path='C:\\Windows', size=10), after_re=27, garbage=b'MORE GARBAGE!')
True
>>> Struct(
... *Regex(regex, size=Int32ul, path=CString()),
... 'after_re' / Tell,
... 'garbage' / GreedyBytes
... ).parse(data)
Container(size=10, path=u'C:\\Windows', after_re=27, garbage=b'MORE GARBAGE!')
You can use Regex as a trigger to find a particular piece of data before you start parsing.
>>> Struct(
... Regex(b'TRIGGER'),
... 'greeting' / CString()
... ).parse(b'\x01\x02\x04GARBAGE\x05TRIGGERhello world\x00')
Container(greeting=u'hello world')
If no data is captured, the associated subcon will received a stream with the position set at the location
of that captured group. Thus, allowing you to use it as an anchor point.
>>> r = Regex(b'hello (?P<anchor>)world(?P<extra_data>.*)', anchor=Tell).parse(b'hello world!!!!')
>>> r == Container(extra_data=b'!!!!', anchor=6)
True
If no named capture groups are used, you can instead parse the entire matched string by supplying
a subconstruct as a positional argument. (If no subcon is provided, the raw bytes are returned instead.
>>> Regex(b'hello world\x00', CString()).parse(b'GARBAGE\x01\x03hello world\x00\x04')
'hello world'
>>> Regex(b'hello world\x00').parse(b'GARBAGE\x01\x03hello world\x00\x04')
b'hello world\x00'
You can also set the regular expression to match in-place (instead of searching the data)
by setting the keyword argument _match to True.
>>> Regex('hello', _match=True).parse(b'hello world!')
b'hello'
>>> Regex('hello').parse(b'bogus hello world')
b'hello'
>>> Regex('hello', _match=True).parse(b'bogus hello world')
Traceback (most recent call last):
...
construct.core.ConstructError: [(parsing)] regex did not match
"""
__slots__ = ['regex', 'subcon', 'subcons', 'match']
def __init__(self, regex, *subcon, **group_subcons):
"""
Initializes regex construct.
:param regex: A regex to use (can be a string or compiled).
:param subcon:
A subcon to use on the entire matching string when there are no named capture groups.
(NOTE: This is only used if there are no capture groups.
If you want to use capture groups AND this then have a capture group encapsulating the entire regex.)
:param group_subcons:
Keyword argument dictionary that contains the constructs to use for the corresponding capture group.
If a subcon is not supplied for a capture group, it will default to returning bytes
(equivalent to setting construct.Bytes() for that group.)
:raises ValueError: If arguments are invalid.
"""
super().__init__()
if isinstance(regex, str):
regex = regex.encode() # force byte strings
if isinstance(regex, bytestringtype):
regex = re.compile(regex, re.DOTALL)
self.regex = regex
self.match = group_subcons.pop('_match', False)
self.subcons = [Renamed(sc, name) for name, sc in group_subcons.items()]
self._subcons = Container((sc.name, sc) for sc in self.subcons)
if subcon and len(subcon) > 1:
raise ValueError('Only one subcon can be supplied for the entire match.')
if subcon and group_subcons:
raise ValueError('subcon and group_subcons arguments cannot be used at the same time.')
self.subcon = subcon[0] if subcon else None
def __getattr__(self, name):
if name in self._subcons:
return self._subcons[name]
raise AttributeError
def _parse(self, stream, context, path):
start = stream.tell()
# NOTE: we are going to have to read the entire stream due to regex requirements.
# However, that's okay in this case since we are parsing ByteIO anyway.
if self.match:
match = self.regex.match(stream.read())
else:
match = self.regex.search(stream.read())
if not match:
raise ConstructError(f'[{path}] regex did not match')
try:
group_dict = match.groupdict()
# If there are no named groups. Return parsed full match instead.
if not group_dict:
if self.subcon:
sub_stream = io.BytesIO(match.group())
return self.subcon._parsereport(sub_stream, context, path)
else:
return match.group()
# Otherwise, we are going to parse each named capture group.
obj = Container()
obj._io = stream
context = Container(_=context, _params=context._params, _root=None, _parsing=context._parsing,
_building=context._building, _sizing=context._sizing, _subcons=self.subcons,
_io=stream, _index=context.get("_index", None))
context._root = context._.get("_root", context)
# Default to displaying matched data as pure bytes.
obj.update(group_dict)
context.update(group_dict)
# Parse groups using supplied constructs.
for subcon in self.subcons:
name = subcon.name
try:
data = match.group(name)
except IndexError:
continue
# If data is None, then we are most likely dealing with an optional capture group.
if data is None:
obj[name] = None
context[name] = None
continue
# If we have an empty capture group, the user would like to use it as an anchor.
if not data:
stream.seek(start + match.start(name))
sub_stream = stream
else:
sub_stream = io.BytesIO(data)
try:
subobj = subcon._parsereport(sub_stream, context, path)
except ConstructError as e:
# Raise a more useful error message.
# TODO: Remove when path is provided in exception messages.
raise ConstructError('Failed to parse {} capture group with error: {}'.format(name, e))
obj[name] = subobj
context[name] = subobj
return obj
finally:
# Reset position to right after the matched regex.
stream.seek(start + match.end())
def _build(self, obj, stream, context, path):
raise ConstructError('Building for Regex is not supported.')
def _sizeof(self, context, path):
raise SizeofError('sizeof() for Regex is not supported.')
def _emitparse(self, code):
raise NotImplementedError
def _emitseq(self, ksy, bitwise):
raise NotImplementedError
def RegexSearch(regex, *subcon, **group_subcons) -> Regex:
"""Performs search of given regex pattern starting at current stream position and then parses match groups."""
return Regex(regex, *subcon, _match=False, **group_subcons)
def RegexMatch(regex, *subcon, **group_subcons) -> Regex:
"""Peforms match of given regex pattern at current stream position and then parses match groups."""
return Regex(regex, *subcon, _match=True, **group_subcons)
class IterError(ConstructError):
pass
# TODO: Should this be renamed to Map?
class Iter(Construct):
r"""
Class that allows iterating over an object and acting on each item.
e.g.
>>> spec = Struct(
... 'types' / Byte[3],
... 'entries' / Iter(this.types, {