|
13 | 13 | Adapter, |
14 | 14 | BitsSwapped, |
15 | 15 | BitStruct, |
| 16 | + Construct, |
16 | 17 | Container, |
17 | 18 | Flag, |
18 | 19 | GreedyBytes, |
19 | 20 | Int32ul, |
20 | 21 | ListContainer, |
21 | 22 | Mapping, |
22 | 23 | Padding, |
| 24 | + SizeofError, |
23 | 25 | Switch, |
| 26 | + stream_read_entire, |
| 27 | + stream_write, |
24 | 28 | ) |
25 | 29 | from Cryptodome.Cipher import AES, ChaCha20, Salsa20 |
| 30 | +from Cryptodome.Random import get_random_bytes |
26 | 31 | from Cryptodome.Util import Padding as CryptoPadding |
27 | 32 | from lxml import etree |
28 | 33 |
|
@@ -81,6 +86,28 @@ def _encode(self, obj, context, path): |
81 | 86 |
|
82 | 87 | return ListContainer(l) |
83 | 88 |
|
| 89 | + def _build(self, obj, stream, context, path): |
| 90 | + obj2 = self._encode(obj, context, path) |
| 91 | + buildret = self.subcon._build(obj2, stream, context, path) |
| 92 | + if buildret is not None: |
| 93 | + return self._decode(buildret, context, path) |
| 94 | + return obj |
| 95 | + |
| 96 | + |
| 97 | +class RandomGreedyBytes(Construct): |
| 98 | + """Parses like GreedyBytes, but generates random bytes of same length during build.""" |
| 99 | + |
| 100 | + def _parse(self, stream, context, path): |
| 101 | + return stream_read_entire(stream, path) |
| 102 | + |
| 103 | + def _build(self, obj, stream, context, path): |
| 104 | + data = get_random_bytes(len(obj)) |
| 105 | + stream_write(stream, data, len(data), path) |
| 106 | + return data |
| 107 | + |
| 108 | + def _sizeof(self, context, path): |
| 109 | + raise SizeofError(path=path) |
| 110 | + |
84 | 111 |
|
85 | 112 | def Reparsed(subcon_out): |
86 | 113 | class Reparsed(Adapter): |
|
0 commit comments