-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinaryiotools.py
More file actions
888 lines (733 loc) · 20.5 KB
/
binaryiotools.py
File metadata and controls
888 lines (733 loc) · 20.5 KB
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
"""
Base binary I/O helper.
Does boilerplate things like reading the next uint32 from a file or binary stream
Create a new IO object and initialize/ set data
Example:
-------
```python
f = open(fileName, "rb")
data = f.read()
f.close()
io = IO(data)
width = io.u32
height = io.u32
```
The example above reads a file in binary and sets the variables width and height
as the first two unsigned integer 32s
For a file starting with the bytearray:
```none
00 00 00 C8 00 01 90
```
The values for width and height would be 200, 400
"""
from __future__ import annotations
import struct
from typing import Any
# ruff: noqa: ANN401
class IO:
"""Class to handle i/o to a byte buffer or file-like object."""
def __init__(
self,
data: bytearray | bytes | None = None,
idx: int = 0,
littleEndian: bool = False,
boolSize: int = 8,
stringEncoding: str = "U",
) -> None:
"""Class to handle i/o to a byte buffer or file-like object.
:param data: can be a data buffer or a file-like object
:param idx: start reading/writing the data at the given index
:param littleEndian: whether the default is big-endian or little-endian
:param boolSize: how many default bits to use for a bool (8,16,32,or 64)
:param stringEncoding: default string encoding A=Ascii, U=UTF-8, W-Unicode wide
"""
if data is None:
data = bytearray()
self._data = data
self._index = idx
self._contexts = []
self.littleEndian = littleEndian
self.boolSize = boolSize
self.stringEncoding = stringEncoding # A=Ascii, U=UTF-8, W-Unicode wide
def __len__(self) -> int:
"""Length of data."""
return len(self.data)
def __getitem__(self, idx: int):
"""Get data at a specific idx."""
return self.data[idx]
@property
def data(self) -> bytearray:
"""Return data as a mutable bytearray."""
if isinstance(self._data, bytearray):
return self._data
if not hasattr(self, "_converted_data"): # Cache converted bytearray
self._converted_data = bytearray(self._data)
return self._converted_data
@data.setter
def data(self, data: bytearray) -> None:
"""Set data."""
if not hasattr(data, "__getitem__"):
raise RuntimeError("ERR: incorrect type for data buffer" + str(type(data)))
self._data = data
@property
def index(self) -> int:
"""Return data."""
return self._index
@index.setter
def index(self, index: int) -> None:
"""Set index."""
self._index = index
def beginContext(self, newIndex: int) -> None:
"""Start a new context where the index can be changed all you want...
and when endContext() is called, it will be restored to the current position
"""
self._contexts.append(newIndex)
def endContext(self) -> None:
"""Restore the index to the previous location where it was when beginContext() was called."""
self.index = self._contexts.pop()
def _write(self, size: int, fmt: str, data: Any) -> None:
"""General formatted write."""
if self.index + size >= len(self.data):
self.data.extend(bytearray((self.index + size) - len(self.data)))
try:
struct.pack_into(fmt, self.data, self.index, data)
self.index += size
except (DeprecationWarning, struct.error) as upstream:
raise RuntimeError(type(data), fmt, size, data) from upstream
def _read(self, size: int, fmt: str) -> Any:
"""General formatted read."""
try:
data = struct.unpack(fmt, self.data[self.index : self.index + size])[0]
except (DeprecationWarning, struct.error) as upstream:
rep: bytes = self.index.to_bytes(8, "big")
msg = (
f"{fmt} {size} {self.index} ({rep}) "
f"\n{len(self.data)} {self.data[self.index : self.index + size]}"
)
raise RuntimeError(msg) from upstream
# Move the pointer forward
self.index += size
return data
@property
def boolean(self) -> bool:
"""Return bool."""
if self.boolSize == 8:
return self.bool8
if self.boolSize == 16:
return self.bool16
if self.boolSize == 32:
return self.bool32
if self.boolSize == 64:
return self.bool64
raise RuntimeError("Unknown bool size " + str(self.boolSize))
@boolean.setter
def boolean(self, ioBool: bool) -> None:
"""Set bool."""
if self.boolSize == 8:
self.bool8 = ioBool
elif self.boolSize == 16:
self.bool16 = ioBool
elif self.boolSize == 32:
self.bool32 = ioBool
elif self.boolSize == 64:
self.bool64 = ioBool
else:
raise RuntimeError("Unknown bool size " + str(self.boolSize))
@property
def bool8(self) -> bool:
"""Get bool8."""
return self.u8 != 0
@bool8.setter
def bool8(self, ioBool: bool) -> None:
"""Set a bool8."""
self.u8 = ioBool
@property
def bool16(self) -> bool:
"""Get bool16."""
return self.u16 != 0
@bool16.setter
def bool16(self, ioBool: bool) -> None:
"""Set bool16."""
self.u16 = ioBool
@property
def bool32(self) -> bool:
"""Get bool32."""
return self.u32 != 0
@bool32.setter
def bool32(self, ioBool: bool) -> None:
"""Set bool32."""
self.u32 = ioBool
@property
def bool64(self) -> bool:
"""Get bool64."""
return self.u64 != 0
@bool64.setter
def bool64(self, ioBool: bool) -> None:
"""Set bool64."""
self.u64 = ioBool
@property
def byte(self) -> Any:
"""Get byte."""
return self.i8
@byte.setter
def byte(self, byte: Any) -> None:
"""Set byte."""
self.i8 = byte
@property
def unsignedByte(self) -> Any:
"""Get unsigned byte."""
return self.u8
@unsignedByte.setter
def unsignedByte(self, byte: Any) -> None:
"""Set unsigned byte."""
self.u8 = byte
@property
def word(self) -> Any:
"""Get a word."""
return self.i16
@word.setter
def word(self, word: Any) -> None:
"""Set a word."""
self.i16 = word
@property
def unsignedWord(self) -> Any:
"""Get an unsigned word."""
return self.u16
@unsignedWord.setter
def unsignedWord(self, unsignedWord: Any) -> None:
"""Set an unsigned word."""
self.u16 = unsignedWord
@property
def dword(self) -> Any:
"""Get a dword."""
return self.i32
@dword.setter
def dword(self, dword: Any) -> None:
"""Set a dword."""
self.i32 = dword
@property
def unsignedDword(self) -> Any:
"""Get a unsigned dword."""
return self.u32
@unsignedDword.setter
def unsignedDword(self, unsignedDword: Any) -> None:
"""Set an unsigned dword."""
self.u32 = unsignedDword
@property
def qword(self) -> Any:
"""Get a qword."""
return self.i64
@qword.setter
def qword(self, qword: Any) -> None:
"""Set a qword."""
self.i64 = qword
@property
def unsignedQword(self) -> Any:
"""Get an unsigned qword."""
return self.u64
@unsignedQword.setter
def unsignedQword(self, unsignedQword: Any) -> None:
"""Set an unsigned qword."""
self.u64 = unsignedQword
@property
def i8(self) -> int:
"""Get an int8."""
if self.littleEndian:
return self.i8le
return self.i8be
@i8.setter
def i8(self, i8: int) -> None:
"""Set an int8."""
if self.littleEndian:
self.i8le = i8
else:
self.i8be = i8
@property
def u8(self) -> int:
"""Get an unsigned int."""
if self.littleEndian:
return self.u8le
return self.u8be
@u8.setter
def u8(self, u8: int) -> None:
"""Set an unsigned int."""
if self.littleEndian:
self.u8le = u8
else:
self.u8be = u8
@property
def i16(self) -> int:
"""Get an int16."""
if self.littleEndian:
return self.i16le
return self.i16be
@i16.setter
def i16(self, i16: int) -> None:
"""Set an int16."""
if self.littleEndian:
self.i16le = i16
else:
self.i16be = i16
@property
def u16(self) -> int:
"""Get an uint16."""
if self.littleEndian:
return self.u16le
return self.u16be
@u16.setter
def u16(self, u16: int) -> None:
"""Set an unint16."""
if self.littleEndian:
self.u16le = u16
else:
self.u16be = u16
@property
def i32(self) -> int:
"""Get an int32."""
if self.littleEndian:
return self.i32le
return self.i32be
@i32.setter
def i32(self, i32: int) -> None:
"""Set an int32."""
if self.littleEndian:
self.i32le = i32
else:
self.i32be = i32
@property
def u32(self) -> int:
"""Get a uint32."""
if self.littleEndian:
return self.u32le
return self.u32be
@u32.setter
def u32(self, u32: int) -> None:
"""Set a unint32."""
if self.littleEndian:
self.u32le = u32
else:
self.u32be = u32
@property
def i64(self) -> int:
"""Get an int64."""
if self.littleEndian:
return self.i64le
return self.i64be
@i64.setter
def i64(self, i64: int) -> None:
"""Set an int64."""
if self.littleEndian:
self.i64le = i64
else:
self.i64be = i64
@property
def u64(self) -> int:
"""Get a uint64."""
if self.littleEndian:
return self.u64le
return self.u64be
@u64.setter
def u64(self, u64: int) -> None:
"""Set a uint64."""
if self.littleEndian:
self.u64le = u64
else:
self.u64be = u64
@property
def float32(self) -> float:
"""Get a float32."""
if self.littleEndian:
return self.float32le
return self.float32be
@float32.setter
def float32(self, float32: float) -> None:
"""Set a float32."""
if self.littleEndian:
self.float32le = float32
else:
self.float32be = float32
@property
def float64(self) -> float:
"""Get a float64."""
if self.littleEndian:
return self.float64le
return self.float64be
@float64.setter
def float64(self, float64: float) -> None:
"""Set a float64."""
if self.littleEndian:
self.float64le = float64
else:
self.float64be = float64
@property
def u8be(self) -> int:
"""Read the next uint8 and advance the index."""
return self._read(1, ">B")
@u8be.setter
def u8be(self, u8be: int) -> None:
"""Set the uint8."""
self._write(1, ">B", u8be)
@property
def u8le(self) -> int:
"""Read the next uint8 and advance the index."""
return self._read(1, "<B")
@u8le.setter
def u8le(self, u8le: int) -> None:
"""Set the uint8."""
self._write(1, "<B", u8le)
@property
def i8le(self) -> int:
"""Read the next signed int8 and advance the index."""
return self._read(1, "<b")
@i8le.setter
def i8le(self, i8le: int) -> None:
"""Set the int8."""
self._write(1, "<b", i8le)
@property
def i8be(self) -> int:
"""Read the next signed int8 and advance the index."""
return self._read(1, ">b")
@i8be.setter
def i8be(self, i8be: int) -> None:
"""Set the int8."""
self._write(1, ">b", i8be)
@property
def u16be(self) -> int:
"""Read the next uint16 and advance the index."""
return self._read(2, ">H")
@u16be.setter
def u16be(self, u16be: int) -> None:
"""Set the uint16."""
self._write(2, ">H", u16be)
@property
def u16le(self) -> int:
"""Read the next uint16 and advance the index."""
return self._read(2, "<H")
@u16le.setter
def u16le(self, u16le: int) -> None:
"""Set the uint16."""
self._write(2, "<H", u16le)
@property
def i16le(self) -> int:
"""Read the next signed int16 and advance the index."""
return self._read(2, "<h")
@i16le.setter
def i16le(self, i16le: int) -> None:
"""Set the int16."""
self._write(2, "<h", i16le)
@property
def i16be(self) -> int:
"""Read the next signed int16 and advance the index."""
return self._read(2, ">h")
@i16be.setter
def i16be(self, i16be: int) -> None:
"""Set the int16."""
self._write(2, ">h", i16be)
@property
def u32be(self) -> int:
"""Read the next uint32 and advance the index."""
return self._read(4, ">I")
@u32be.setter
def u32be(self, u32be: int) -> None:
"""Set the uint32."""
self._write(4, ">I", u32be)
@property
def u32le(self) -> int:
"""Read the next uint32 and advance the index."""
return self._read(4, "<I")
@u32le.setter
def u32le(self, u32le: int) -> None:
"""Set the uint32."""
self._write(4, "<I", u32le)
@property
def i32le(self) -> int:
"""Read the next signed int32 and advance the index."""
return self._read(4, "<i")
@i32le.setter
def i32le(self, i32le: int) -> None:
"""Set the int32."""
self._write(4, "<i", i32le)
@property
def i32be(self) -> int:
"""Read the next signed int32 and advance the index."""
return self._read(4, ">i")
@i32be.setter
def i32be(self, i32be: int) -> None:
"""Set the int32."""
self._write(4, ">i", i32be)
@property
def u64be(self) -> int:
"""Read the next uint64 and advance the index."""
return self._read(8, ">Q")
@u64be.setter
def u64be(self, u64be: int) -> None:
"""Set the uint64."""
self._write(8, ">Q", u64be)
@property
def u64le(self) -> int:
"""Read the next uint64 and advance the index."""
return self._read(8, "<Q")
@u64le.setter
def u64le(self, u64le: int) -> None:
"""Set the uint64."""
self._write(8, "<Q", u64le)
@property
def i64le(self) -> int:
"""Read the next signed int64 and advance the index."""
return self._read(8, "<q")
@i64le.setter
def i64le(self, i64le: int) -> None:
"""Set the int64."""
self._write(8, "<q", i64le)
@property
def i64be(self) -> int:
"""Read the next signed int64 and advance the index."""
return self._read(8, ">q")
@i64be.setter
def i64be(self, i64be: int) -> None:
"""Set the int64."""
self._write(8, ">q", i64be)
@property
def floating(self) -> float:
"""Get a float."""
return self.float32
@floating.setter
def floating(self, floating: float) -> None:
"""Set a float."""
self.float32 = floating
@property
def double(self) -> float:
"""Get a double."""
return self.float64
@double.setter
def double(self, floating: float) -> None:
"""Set a double."""
self.float64 = floating
@property
def float32be(self) -> float:
"""Read the next 32 bit float and advance the index."""
return self._read(4, ">f")
@float32be.setter
def float32be(self, float32be: float) -> None:
"""Set a 32 bit float."""
self._write(4, ">f", float32be)
@property
def float32le(self) -> float:
"""Read the next 32 bit float and advance the index."""
return self._read(4, "<f")
@float32le.setter
def float32le(self, float32le: float) -> None:
"""Set a 32 bit float."""
self._write(4, "<f", float32le)
@property
def float64be(self) -> float:
"""Read the next 64 bit float and advance the index."""
return self._read(8, ">d")
@float64be.setter
def float64be(self, float64be: float) -> None:
"""Set a 64 bit float."""
self._write(8, ">d", float64be)
@property
def float64le(self) -> float:
"""Read the next 64 bit float and advance the index."""
return self._read(8, "<d")
@float64le.setter
def float64le(self, float64le: float) -> None:
"""Set a 64 bit float."""
self._write(8, "<d", float64le)
def getbytearray(self, nbytearray: int):
"""Grab some raw bytearray and advance the index."""
data = self.data[self.index : self.index + nbytearray]
self.index += nbytearray
return data
def addbytearray(self, iobytearray: Any) -> None:
"""Add some raw bytearray and advance the index.
:param iobytearray: Can be a string, bytearray, bytes, or another IOBuffer object.
"""
if isinstance(iobytearray, IO):
iobytearray = iobytearray.data
if isinstance(iobytearray, str):
iobytearray = iobytearray.encode("utf-8")
if isinstance(iobytearray, bytes):
iobytearray = bytearray(iobytearray)
if not isinstance(iobytearray, bytearray):
raise TypeError(f"Unsupported type {type(iobytearray)} for addbytearray")
new_size = self.index + len(iobytearray)
# # Expand buffer if needed
if new_size > len(self.data):
self.data.extend(bytearray(new_size - len(self.data)))
# Insert data at the correct index
self.data[self.index : new_size] = iobytearray
self.index = new_size # Move index forward\
def _sz754(self, encoding: str) -> str:
"""Read the next string conforming to IEEE 754 and advance the index.
Note, string format is:
uint32 n+1 Number of bytearray that follow, including the zero byte
byte[n] ... String data in Unicode, encoded using UTF-8
byte 0 Zero marks the end of the string.
or simply uint32=0 for empty string
"""
nchars = self.u32
if nchars == 0:
return ""
data = self.data[self.index : self.index + nchars - 1]
self.index += nchars
if encoding == "A":
return data
if encoding == "U":
return data.decode("UTF-8", errors="replace")
if encoding == "W":
return data.decode("UTF-16", errors="replace")
raise RuntimeError
def _sz754set(self, sz754: Any, _encoding: str) -> None:
"""_sz754set."""
self.u32 = len(sz754)
self.addbytearray(sz754)
self.u8 = 0
@property
def sz754(self) -> Any:
"""sz754."""
return self._sz754(self.stringEncoding)
@sz754.setter
def sz754(self, sz754: Any) -> None:
"""Set sz754."""
return self._sz754set(sz754, self.stringEncoding)
@property
def sz754A(self) -> Any:
"""sz754A."""
return self._sz754("A")
@sz754A.setter
def sz754A(self, sz754: Any) -> None:
"""Set sz754A."""
return self._sz754set(sz754, "A")
@property
def sz754W(self) -> Any:
"""sz754W."""
return self._sz754("W")
@sz754W.setter
def sz754W(self, sz754: Any) -> None:
"""Set sz754W."""
return self._sz754set(sz754, "W")
@property
def sz754U(self) -> Any:
"""sz754U."""
return self._sz754("U")
@sz754U.setter
def sz754U(self, sz754: Any) -> None:
"""Set sz754U."""
return self._sz754set(sz754, "U")
def _readUntil(self, until: str, encoding: str = "A") -> str:
"""Read a sequence of chars in a set encoding until a set char.
:param until: must be within the ascii character set
:param encoding: one of A (ascii), U (UTF-8) or W (UCS-2)
"""
data = []
if encoding == "A":
encoding = "ascii"
elif encoding == "U":
encoding = "UTF-8"
elif encoding == "W":
encoding = "UCS-2"
else:
msg = "bogus encoding"
raise RuntimeError(msg)
untilB = until.encode("ascii")[0] # always ascii
while True:
char = self.data[self.index]
self.index += 1
if encoding == "W": # get one more byte
data.append(char)
char = self.data[self.index]
self.index += 1
if char == untilB:
break
data.append(char)
return bytearray(data).decode(encoding, errors="replace")
@property
def textLine(self) -> str:
"""Read a sequence of chars until the next new line char."""
ret = self._readUntil("\n", self.stringEncoding)
if ret[-1] == "\r":
ret = ret[-1]
return ret
@textLine.setter
def textLine(self, text: str) -> None:
"""Set a sequence of chars until the next new line char."""
self.addbytearray(text)
if isinstance(text, (int, float)) or text[-1] != "\n":
self.addbytearray("\n")
@property
def textLineA(self) -> str:
"""Read a sequence of chars until the next new line char in ascii."""
ret = self._readUntil("\n", "A")
if ret[-1] == "\r":
ret = ret[-1]
return ret
@textLineA.setter
def textLineA(self, text: str) -> None:
"""Set a sequence of chars until the next new line char in ascii."""
self.addbytearray(text)
if isinstance(text, (int, float)) or text[-1] != "\n":
self.addbytearray("\n")
@property
def textLineW(self) -> str:
"""Read a sequence of chars until the next new line char in ucs-2."""
ret = self._readUntil("\n", "W")
if ret[-1] == "\r":
ret = ret[-1]
return ret
@textLineW.setter
def textLineW(self, text: str) -> None:
"""Set a sequence of chars until the next new line char in ucs-2."""
self.addbytearray(text)
if isinstance(text, (int, float)) or text[-1] != "\n":
self.addbytearray("\0\n")
@property
def textLineU(self) -> str:
"""Read a sequence of chars until the next new line char in utf-8."""
ret = self._readUntil("\n", "U")
if ret[-1] == "\r":
ret = ret[-1]
return ret
@textLineU.setter
def textLineU(self, text: str) -> None:
"""Set a sequence of chars until the next new line char in utf-8."""
self.addbytearray(text)
if isinstance(text, (int, float)) or text[-1] != "\n":
self.addbytearray("\n")
@property
def cString(self) -> str:
"""Read a sequence of chars until the next null byte."""
return self._readUntil("\0", self.stringEncoding)
@cString.setter
def cString(self, text: str) -> None:
"""Set a sequence of chars and add a null byte."""
self.addbytearray(text)
self.addbytearray("\0")
@property
def cStringA(self) -> str:
"""Read a sequence of chars until the next null byte in ascii."""
return self._readUntil("\0", "A")
@cStringA.setter
def cStringA(self, text: str) -> None:
"""Set a sequence of chars and add a null byte in ascii."""
self.addbytearray(text)
self.addbytearray("\0")
@property
def cStringW(self) -> str:
"""Read a sequence of chars until the next null byte in ucs-2."""
return self._readUntil("\0", "W")
@cStringW.setter
def cStringW(self, text: str) -> None:
"""Set a sequence of chars and add a null byte in ucs-2."""
self.addbytearray(text)
self.addbytearray("\0\0")
@property
def cStringU(self) -> str:
"""Read a sequence of chars until the next null byte in utf-8."""
return self._readUntil("\0", "U")
@cStringU.setter
def cStringU(self, text: str) -> None:
"""Set a sequence of chars and add a null byte in utf-8."""
self.addbytearray(text)
self.addbytearray("\0")