Commit | Line | Data |
---|---|---|
bb2f9e9c PP |
1 | // Show ToC at a specific location for a GitHub rendering |
2 | ifdef::env-github[] | |
3 | :toc: macro | |
4 | endif::env-github[] | |
5 | ||
6 | ifndef::env-github[] | |
71aaa3f7 | 7 | :toc: left |
bb2f9e9c PP |
8 | endif::env-github[] |
9 | ||
10 | // This is to mimic what GitHub does so that anchors work in an offline | |
11 | // rendering too. | |
12 | :idprefix: | |
13 | :idseparator: - | |
71aaa3f7 | 14 | |
bb2f9e9c | 15 | // Other attributes |
71aaa3f7 PP |
16 | :py3: Python{nbsp}3 |
17 | ||
bb2f9e9c PP |
18 | = Normand |
19 | Philippe Proulx | |
20 | ||
df0f8552 PP |
21 | image::normand-logo.png[] |
22 | ||
71aaa3f7 PP |
23 | [.normal] |
24 | image:https://img.shields.io/pypi/v/normand.svg?label=Latest%20version[link="https://pypi.python.org/pypi/normand"] | |
25 | ||
26 | [.lead] | |
27 | _**Normand**_ is a text-to-binary processor with its own language. | |
28 | ||
29 | This package offers both a portable {py3} module and a command-line | |
30 | tool. | |
31 | ||
27d52a19 | 32 | WARNING: This version of Normand is 0.9, meaning both the Normand |
71aaa3f7 PP |
33 | language and the module/CLI interface aren't stable. |
34 | ||
bb2f9e9c PP |
35 | ifdef::env-github[] |
36 | // ToC location for a GitHub rendering | |
37 | toc::[] | |
38 | endif::env-github[] | |
39 | ||
71aaa3f7 PP |
40 | == Introduction |
41 | ||
42 | The purpose of Normand is to consume human-readable text representing | |
43 | bytes and to produce the corresponding binary data. | |
44 | ||
45 | .Simple bytes input. | |
46 | ==== | |
47 | Consider the following Normand input: | |
48 | ||
49 | ---- | |
50 | 4f 55 32 bb $167 fe %10100111 a9 $-32 | |
51 | ---- | |
52 | ||
53 | The generated nine bytes are: | |
54 | ||
55 | ---- | |
56 | 4f 55 32 bb a7 fe a7 a9 e0 | |
57 | ---- | |
58 | ==== | |
59 | ||
60 | As you can see in the last example, the fundamental unit of the Normand | |
61 | language is the _byte_. The order in which you list bytes will be the | |
62 | order of the generated data. | |
63 | ||
64 | The Normand language is more than simple lists of bytes, though. Its | |
65 | main features are: | |
66 | ||
67 | Comments, including a bunch of insignificant symbols which may improve readability:: | |
68 | + | |
69 | Input: | |
70 | + | |
71 | ---- | |
72 | ff bb %1101:0010 # This is a comment | |
73 | 78 29 af $192 # This too # 99 $-80 | |
74 | fe80::6257:18ff:fea3:4229 | |
75 | 60:57:18:a3:42:29 | |
76 | 10839636-5d65-4a68-8e6a-21608ddf7258 | |
77 | ---- | |
78 | + | |
79 | Output: | |
80 | + | |
81 | ---- | |
82 | ff bb d2 78 29 af c0 99 b0 fe 80 62 57 18 ff fe | |
83 | a3 42 29 60 57 18 a3 42 29 10 83 96 36 5d 65 4a | |
84 | 68 8e 6a 21 60 8d df 72 58 | |
85 | ---- | |
86 | ||
87 | Hexadecimal, decimal, and binary byte constants:: | |
88 | + | |
89 | Input: | |
90 | + | |
91 | ---- | |
92 | aa bb $247 $-89 %0011_0010 %11.01= 10/10 | |
93 | ---- | |
94 | + | |
95 | Output: | |
96 | + | |
97 | ---- | |
98 | aa bb f7 a7 32 da | |
99 | ---- | |
100 | ||
101 | UTF-8, UTF-16, and UTF-32 literal strings:: | |
102 | + | |
103 | Input: | |
104 | + | |
105 | ---- | |
106 | "hello world!" 00 | |
107 | u16le"stress\nverdict 🤣" | |
108 | ---- | |
109 | + | |
110 | Output: | |
111 | + | |
112 | ---- | |
113 | 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 00 73 00 74 ┆ hello world!•s•t | |
114 | 00 72 00 65 00 73 00 73 00 0a 00 76 00 65 00 72 ┆ •r•e•s•s•••v•e•r | |
115 | 00 64 00 69 00 63 00 74 00 20 00 3e d8 23 dd ┆ •d•i•c•t• •>•#• | |
116 | ---- | |
117 | ||
118 | Labels: special variables holding the offset where they're defined:: | |
119 | + | |
120 | ---- | |
121 | <beg> b2 52 e3 bc 91 05 | |
122 | $100 $50 <chair> 33 9f fe | |
123 | 25 e9 89 8a <end> | |
124 | ---- | |
125 | ||
126 | Variables:: | |
127 | + | |
128 | ---- | |
129 | 5e 65 {tower = 47} c6 7f f2 c4 | |
130 | 44 {hurl = tower - 14} b5 {tower = hurl} 26 2d | |
131 | ---- | |
132 | + | |
133 | The value of a variable assignment is the evaluation of a valid {py3} | |
134 | expression which may include label and variable names. | |
135 | ||
269f6eb3 | 136 | Fixed-length number with a given length (8{nbsp}bits to 64{nbsp}bits) and byte order:: |
71aaa3f7 PP |
137 | + |
138 | Input: | |
139 | + | |
140 | ---- | |
141 | {strength = 4} | |
142 | {be} 67 <lbl> 44 $178 {(end - lbl) * 8 + strength : 16} $99 <end> | |
143 | {le} {-1993 : 32} | |
269f6eb3 | 144 | {-3.141593 : 64} |
71aaa3f7 PP |
145 | ---- |
146 | + | |
147 | Output: | |
148 | + | |
149 | ---- | |
269f6eb3 PP |
150 | 67 44 b2 00 2c 63 37 f8 ff ff 7f bd c2 82 fb 21 |
151 | 09 c0 | |
71aaa3f7 PP |
152 | ---- |
153 | + | |
269f6eb3 | 154 | The encoded number is the evaluation of a valid {py3} expression which |
05f81895 PP |
155 | may include label and variable names. |
156 | ||
157 | https://en.wikipedia.org/wiki/LEB128[LEB128] integer:: | |
158 | + | |
159 | Input: | |
160 | + | |
161 | ---- | |
162 | aa bb cc {-1993 : sleb128} <meow> dd ee ff | |
163 | {meow * 199 : uleb128} | |
164 | ---- | |
165 | + | |
166 | Output: | |
167 | + | |
168 | ---- | |
169 | aa bb cc b7 70 dd ee ff e3 07 | |
170 | ---- | |
171 | + | |
172 | The encoded integer is the evaluation of a valid {py3} expression which | |
71aaa3f7 PP |
173 | may include label and variable names. |
174 | ||
27d52a19 PP |
175 | Conditional:: |
176 | + | |
177 | Input: | |
178 | + | |
179 | ---- | |
180 | aa bb cc | |
181 | ||
182 | ( | |
183 | "foo" | |
184 | ||
185 | !if {ICITTE > 10} | |
186 | "bar" | |
187 | !end | |
188 | ) * 4 | |
189 | ---- | |
190 | + | |
191 | Output: | |
192 | + | |
193 | ---- | |
194 | aa bb cc 66 6f 6f 66 6f 6f 66 6f 6f 62 61 72 66 ┆ •••foofoofoobarf | |
195 | 6f 6f 62 61 72 ┆ oobar | |
196 | ---- | |
197 | ||
71aaa3f7 PP |
198 | Repetition:: |
199 | + | |
200 | Input: | |
201 | + | |
202 | ---- | |
2adf4336 | 203 | aa bb * 5 cc <zoom> "yeah\0" * {zoom * 3} |
e57a18e1 PP |
204 | |
205 | !repeat 3 | |
206 | ff ee "juice" | |
207 | !end | |
71aaa3f7 PP |
208 | ---- |
209 | + | |
210 | Output: | |
211 | + | |
212 | ---- | |
2adf4336 PP |
213 | aa bb bb bb bb bb cc 79 65 61 68 00 79 65 61 68 ┆ •••••••yeah•yeah |
214 | 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah• | |
215 | 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 79 ┆ yeah•yeah•yeah•y | |
216 | 65 61 68 00 79 65 61 68 00 79 65 61 68 00 79 65 ┆ eah•yeah•yeah•ye | |
217 | 61 68 00 79 65 61 68 00 79 65 61 68 00 79 65 61 ┆ ah•yeah•yeah•yea | |
218 | 68 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 ┆ h•yeah•yeah•yeah | |
71aaa3f7 | 219 | 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah• |
e57a18e1 PP |
220 | ff ee 6a 75 69 63 65 ff ee 6a 75 69 63 65 ff ee ┆ ••juice••juice•• |
221 | 6a 75 69 63 65 ┆ juice | |
71aaa3f7 PP |
222 | ---- |
223 | ||
676f6189 PP |
224 | Alignment:: |
225 | + | |
226 | Input: | |
227 | + | |
228 | ---- | |
229 | {be} | |
230 | ||
231 | {199:32} | |
232 | @64 {43:64} | |
233 | @16 {-123:16} | |
234 | @32~255 {5584:32} | |
235 | ---- | |
236 | + | |
237 | Output: | |
238 | + | |
239 | ---- | |
240 | 00 00 00 c7 00 00 00 00 00 00 00 00 00 00 00 2b | |
241 | ff 85 ff ff 00 00 15 d0 | |
242 | ---- | |
71aaa3f7 PP |
243 | |
244 | Multilevel grouping:: | |
245 | + | |
246 | Input: | |
247 | + | |
248 | ---- | |
249 | ff ((aa bb "zoom" cc) * 5) * 3 $-34 * 4 | |
250 | ---- | |
251 | + | |
252 | Output: | |
253 | + | |
254 | ---- | |
255 | ff aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa ┆ •••zoom•••zoom•• | |
256 | bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a ┆ •zoom•••zoom•••z | |
257 | 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f ┆ oom•••zoom•••zoo | |
258 | 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc ┆ m•••zoom•••zoom• | |
259 | aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb ┆ ••zoom•••zoom••• | |
260 | 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f ┆ zoom•••zoom•••zo | |
261 | 6f 6d cc aa bb 7a 6f 6f 6d cc de de de de ┆ om•••zoom••••• | |
262 | ---- | |
263 | ||
264 | Precise error reporting:: | |
265 | + | |
266 | ---- | |
267 | /tmp/meow.normand:10:24 - Expecting a bit (`0` or `1`). | |
268 | ---- | |
269 | + | |
270 | ---- | |
271 | /tmp/meow.normand:32:6 - Unexpected character `k`. | |
272 | ---- | |
273 | + | |
274 | ---- | |
2adf4336 | 275 | /tmp/meow.normand:24:19 - Illegal (unknown or unreachable) variable/label name `meow` in expression `(meow - 45) // 8`; the legal names are {`mix`, `zoom`}. |
71aaa3f7 PP |
276 | ---- |
277 | + | |
278 | ---- | |
279 | /tmp/meow.normand:18:9 - Value 315 is outside the 8-bit range when evaluating expression `end - ICITTE` at byte offset 45. | |
280 | ---- | |
281 | ||
282 | You can use Normand to track data source files in your favorite VCS | |
283 | instead of raw binary files. The binary files that Normand generates can | |
284 | be used to test file format decoding, including malformatted data, for | |
285 | example, as well as for education. | |
286 | ||
287 | See <<learn-normand>> to explore all the Normand features. | |
288 | ||
289 | == Install Normand | |
290 | ||
291 | Normand requires Python ≥ 3.4. | |
292 | ||
293 | To install Normand: | |
294 | ||
295 | ---- | |
296 | $ python3 -m pip install --user normand | |
297 | ---- | |
298 | ||
299 | See | |
300 | https://packaging.python.org/en/latest/tutorials/installing-packages/#installing-to-the-user-site[Installing to the User Site] | |
301 | to learn more about a user site installation. | |
302 | ||
303 | [NOTE] | |
304 | ==== | |
305 | Normand has a single module file, `normand.py`, which you can copy as is | |
af3cf417 | 306 | to your project to use it (both the <<python3-api,`normand.parse()`>> |
71aaa3f7 PP |
307 | function and the <<command-line-tool,command-line tool>>). |
308 | ||
309 | `normand.py` has _no external dependencies_, but if you're using | |
310 | Python{nbsp}3.4, you'll need a local copy of the standard `typing` | |
311 | module. | |
312 | ==== | |
313 | ||
314 | == Learn Normand | |
315 | ||
316 | A Normand text input is a sequence of items which represent a sequence | |
317 | of raw bytes. | |
318 | ||
319 | [[state]] During the processing of items to data, Normand relies on a | |
320 | current state: | |
321 | ||
322 | [%header%autowidth] | |
323 | |=== | |
af3cf417 | 324 | |State variable |Description |Initial value: <<python3-api,{py3} API>> |Initial value: <<command-line-tool,CLI>> |
71aaa3f7 PP |
325 | |
326 | |[[cur-offset]] Current offset | |
327 | | | |
05f81895 | 328 | The current offset has an effect on the value of <<label,labels>> and of |
269f6eb3 | 329 | the special `ICITTE` name in <<fixed-length-number,fixed-length |
27d52a19 PP |
330 | number>>, <<leb-128-integer,LEB128 integer>>, |
331 | <<variable-assignment,variable assignment>>, | |
332 | <<conditional-block,conditional block>>, <<repetition-block,repetition | |
333 | block>>, and <<post-item-repetition,post-item repetition>> expression | |
334 | evaluation. | |
71aaa3f7 PP |
335 | |
336 | Each generated byte increments the current offset. | |
337 | ||
338 | A <<current-offset-setting,current offset setting>> may change the | |
676f6189 PP |
339 | current offset without generating data. |
340 | ||
341 | An <<current-offset-alignment,current offset alignment>> generates | |
342 | padding bytes to make the current offset satisfy a given alignment. | |
71aaa3f7 PP |
343 | |`init_offset` parameter of the `parse()` function. |
344 | |`--offset` option. | |
345 | ||
346 | |[[cur-bo]] Current byte order | |
347 | | | |
05f81895 | 348 | The current byte order has an effect on the encoding of |
269f6eb3 | 349 | <<fixed-length-number,fixed-length numbers>>. |
71aaa3f7 PP |
350 | |
351 | A <<current-byte-order-setting,current byte order setting>> may change | |
352 | the current byte order. | |
353 | |`init_byte_order` parameter of the `parse()` function. | |
354 | |`--byte-order` option. | |
355 | ||
356 | |<<label,Labels>> | |
357 | |Mapping of label names to integral values. | |
358 | |`init_labels` parameter of the `parse()` function. | |
359 | |One or more `--label` options. | |
360 | ||
361 | |<<variable-assignment,Variables>> | |
27d52a19 | 362 | |Mapping of variable names to integral or floating point number values. |
71aaa3f7 PP |
363 | |`init_variables` parameter of the `parse()` function. |
364 | |One or more `--var` options. | |
365 | |=== | |
366 | ||
367 | The available items are: | |
368 | ||
369 | * A <<byte-constant,constant integer>> representing a single byte. | |
370 | ||
371 | * A <<literal-string,literal string>> representing a sequence of bytes | |
372 | encoding UTF-8, UTF-16, or UTF-32 data. | |
373 | ||
374 | * A <<current-byte-order-setting,current byte order setting>> (big or | |
375 | little endian). | |
376 | ||
269f6eb3 PP |
377 | * A <<fixed-length-number,fixed-length number>> (integer or |
378 | floating point) using the <<cur-bo,current byte order>> and of which | |
379 | the value is the result of a {py3} expression. | |
05f81895 PP |
380 | |
381 | * An <<leb128-integer,LEB128 integer>> of which the value is the result | |
382 | of a {py3} expression. | |
71aaa3f7 PP |
383 | |
384 | * A <<current-offset-setting,current offset setting>>. | |
385 | ||
676f6189 PP |
386 | * A <<current-offset-alignment,current offset alignment>>. |
387 | ||
71aaa3f7 PP |
388 | * A <<label,label>>, that is, a named constant holding the current |
389 | offset. | |
390 | + | |
391 | This is similar to an assembly label. | |
392 | ||
393 | * A <<variable-assignment,variable assignment>> associating a name to | |
394 | the integral result of an evaluated {py3} expression. | |
395 | ||
396 | * A <<group,group>>, that is, a scoped sequence of items. | |
397 | ||
27d52a19 PP |
398 | * A <<conditional-block,conditional block>>. |
399 | ||
e57a18e1 PP |
400 | * A <<repetition-block,repetition block>>. |
401 | ||
402 | Moreover, you can repeat many items above a constant or variable number | |
403 | of times with the ``pass:[*]`` operator _after_ the item to repeat. This | |
404 | is called a <<post-item-repetition,post-item repetition>>. | |
71aaa3f7 PP |
405 | |
406 | A Normand comment may exist: | |
407 | ||
408 | * Between items, possibly within a group. | |
409 | * Between the nibbles of a constant hexadecimal byte. | |
410 | * Between the bits of a constant binary byte. | |
e57a18e1 PP |
411 | * Between the last item and the ``pass:[*]`` character of a post-item |
412 | repetition, and between that ``pass:[*]`` character and the following | |
413 | number or expression. | |
414 | * Between the ``!repeat``/``!r`` prefix and the following constant | |
415 | integer, name, or expression of a repetition block. | |
27d52a19 PP |
416 | * Between the ``!if`` prefix and the following name or expression of a |
417 | conditional block. | |
71aaa3f7 PP |
418 | |
419 | A comment is anything between two ``pass:[#]`` characters on the same | |
420 | line, or from ``pass:[#]`` until the end of the line. Whitespaces and | |
421 | the following symbol characters are also considered comments where a | |
422 | comment may exist: | |
423 | ||
424 | ---- | |
e57a18e1 | 425 | / \ ? & : ; . , + [ ] _ = | - |
71aaa3f7 PP |
426 | ---- |
427 | ||
428 | The latter serve to improve readability so that you may write, for | |
429 | example, a MAC address or a UUID as is. | |
430 | ||
431 | You can test the examples of this section with the `normand` | |
432 | <<command-line-tool,command-line tool>> as such: | |
433 | ||
434 | ---- | |
435 | $ normand file | hexdump -C | |
436 | ---- | |
437 | ||
438 | where `file` is the name of a file containing the Normand input. | |
439 | ||
440 | === Byte constant | |
441 | ||
442 | A _byte constant_ represents a single byte. | |
443 | ||
444 | A byte constant is: | |
445 | ||
446 | Hexadecimal form:: | |
447 | Two consecutive hexits. | |
448 | ||
449 | Decimal form:: | |
450 | A decimal number after the `$` prefix. | |
451 | ||
452 | Binary form:: | |
453 | Eight bits after the `%` prefix. | |
454 | ||
455 | ==== | |
456 | Input: | |
457 | ||
458 | ---- | |
459 | ab cd [3d 8F] CC | |
460 | ---- | |
461 | ||
462 | Output: | |
463 | ||
464 | ---- | |
465 | ab cd 3d 8f cc | |
466 | ---- | |
467 | ==== | |
468 | ||
469 | ==== | |
470 | Input: | |
471 | ||
472 | ---- | |
473 | $192 %1100/0011 $ -77 | |
474 | ---- | |
475 | ||
476 | Output: | |
477 | ||
478 | ---- | |
479 | c0 c3 b3 | |
480 | ---- | |
481 | ==== | |
482 | ||
483 | ==== | |
484 | Input: | |
485 | ||
486 | ---- | |
487 | 58f64689-6316-4d55-8a1a-04cada366172 | |
488 | fe80::6257:18ff:fea3:4229 | |
489 | ---- | |
490 | ||
491 | Output: | |
492 | ||
493 | ---- | |
494 | 58 f6 46 89 63 16 4d 55 8a 1a 04 ca da 36 61 72 ┆ X•F•c•MU•••••6ar | |
495 | fe 80 62 57 18 ff fe a3 42 29 ┆ ••bW••••B) | |
496 | ---- | |
497 | ==== | |
498 | ||
499 | ==== | |
500 | Input: | |
501 | ||
502 | ---- | |
503 | %01110011 %01100001 %01101100 %01110101 %01110100 | |
504 | ---- | |
505 | ||
506 | Output: | |
507 | ||
508 | ---- | |
509 | 73 61 6c 75 74 ┆ salut | |
510 | ---- | |
511 | ==== | |
512 | ||
513 | === Literal string | |
514 | ||
515 | A _literal string_ represents the UTF-8-, UTF-16-, or UTF-32-encoded | |
516 | bytes of a string. | |
517 | ||
518 | The string to encode isn't implicitly null-terminated: use `\0` at the | |
519 | end of the string to add a null character. | |
520 | ||
521 | A literal string is: | |
522 | ||
523 | . **Optional**: one of the following encodings instead of UTF-8: | |
524 | + | |
525 | -- | |
526 | [horizontal] | |
527 | `u16be`:: UTF-16BE. | |
528 | `u16le`:: UTF-16LE. | |
529 | `u32be`:: UTF-32BE. | |
530 | `u32le`:: UTF-32LE. | |
531 | -- | |
532 | ||
533 | . The ``pass:["]`` prefix. | |
534 | ||
535 | . A sequence of zero or more characters, possibly containing escape | |
536 | sequences. | |
537 | + | |
538 | An escape sequence is the ``\`` character followed by one of: | |
539 | + | |
540 | -- | |
541 | [horizontal] | |
542 | `0`:: Null (U+0000) | |
543 | `a`:: Alert (U+0007) | |
544 | `b`:: Backspace (U+0008) | |
545 | `e`:: Escape (U+001B) | |
546 | `f`:: Form feed (U+000C) | |
547 | `n`:: End of line (U+000A) | |
548 | `r`:: Carriage return (U+000D) | |
549 | `t`:: Character tabulation (U+0009) | |
550 | `v`:: Line tabulation (U+000B) | |
551 | ``\``:: Reverse solidus (U+005C) | |
552 | ``pass:["]``:: Quotation mark (U+0022) | |
553 | -- | |
554 | ||
555 | . The ``pass:["]`` suffix. | |
556 | ||
557 | ==== | |
558 | Input: | |
559 | ||
560 | ---- | |
561 | "coucou tout le monde!" | |
562 | ---- | |
563 | ||
564 | Output: | |
565 | ||
566 | ---- | |
567 | 63 6f 75 63 6f 75 20 74 6f 75 74 20 6c 65 20 6d ┆ coucou tout le m | |
568 | 6f 6e 64 65 21 ┆ onde! | |
569 | ---- | |
570 | ==== | |
571 | ||
572 | ==== | |
573 | Input: | |
574 | ||
575 | ---- | |
576 | u16le"I am not young enough to know everything." | |
577 | ---- | |
578 | ||
579 | Output: | |
580 | ||
581 | ---- | |
582 | 49 00 20 00 61 00 6d 00 20 00 6e 00 6f 00 74 00 ┆ I• •a•m• •n•o•t• | |
583 | 20 00 79 00 6f 00 75 00 6e 00 67 00 20 00 65 00 ┆ •y•o•u•n•g• •e• | |
584 | 6e 00 6f 00 75 00 67 00 68 00 20 00 74 00 6f 00 ┆ n•o•u•g•h• •t•o• | |
585 | 20 00 6b 00 6e 00 6f 00 77 00 20 00 65 00 76 00 ┆ •k•n•o•w• •e•v• | |
586 | 65 00 72 00 79 00 74 00 68 00 69 00 6e 00 67 00 ┆ e•r•y•t•h•i•n•g• | |
587 | 2e 00 ┆ .• | |
588 | ---- | |
589 | ==== | |
590 | ||
591 | ==== | |
592 | Input: | |
593 | ||
594 | ---- | |
595 | u32be "\"illusion is the first\nof all pleasures\" 🦉" | |
596 | ---- | |
597 | ||
598 | Output: | |
599 | ||
600 | ---- | |
601 | 00 00 00 22 00 00 00 69 00 00 00 6c 00 00 00 6c ┆ •••"•••i•••l•••l | |
602 | 00 00 00 75 00 00 00 73 00 00 00 69 00 00 00 6f ┆ •••u•••s•••i•••o | |
603 | 00 00 00 6e 00 00 00 20 00 00 00 69 00 00 00 73 ┆ •••n••• •••i•••s | |
604 | 00 00 00 20 00 00 00 74 00 00 00 68 00 00 00 65 ┆ ••• •••t•••h•••e | |
605 | 00 00 00 20 00 00 00 66 00 00 00 69 00 00 00 72 ┆ ••• •••f•••i•••r | |
606 | 00 00 00 73 00 00 00 74 00 00 00 0a 00 00 00 6f ┆ •••s•••t•••••••o | |
607 | 00 00 00 66 00 00 00 20 00 00 00 61 00 00 00 6c ┆ •••f••• •••a•••l | |
608 | 00 00 00 6c 00 00 00 20 00 00 00 70 00 00 00 6c ┆ •••l••• •••p•••l | |
609 | 00 00 00 65 00 00 00 61 00 00 00 73 00 00 00 75 ┆ •••e•••a•••s•••u | |
610 | 00 00 00 72 00 00 00 65 00 00 00 73 00 00 00 22 ┆ •••r•••e•••s•••" | |
611 | 00 00 00 20 00 01 f9 89 ┆ ••• •••• | |
612 | ---- | |
613 | ==== | |
614 | ||
615 | === Current byte order setting | |
616 | ||
617 | This special item sets the <<cur-bo,_current byte order_>>. | |
618 | ||
619 | The two accepted forms are: | |
620 | ||
621 | [horizontal] | |
622 | ``pass:[{be}]``:: Set the current byte order to big endian. | |
623 | ``pass:[{le}]``:: Set the current byte order to little endian. | |
624 | ||
269f6eb3 | 625 | === Fixed-length number |
71aaa3f7 | 626 | |
269f6eb3 PP |
627 | A _fixed-length number_ represents a fixed number of bytes encoding |
628 | either: | |
629 | ||
630 | * An unsigned or signed integer (two's complement). | |
631 | + | |
632 | The available lengths are 8, 16, 24, 32, 40, 48, 56, and 64. | |
633 | ||
634 | * A floating point number | |
635 | ([IEEE{nbsp}754-2008[https://standards.ieee.org/standard/754-2008.html]). | |
636 | + | |
637 | The available length are 32 (_binary32_) and 64 (_binary64_). | |
71aaa3f7 | 638 | |
269f6eb3 PP |
639 | The value is the result of evaluating a {py3} expression using the |
640 | <<cur-bo,current byte order>>. | |
641 | ||
642 | A fixed-length number is: | |
71aaa3f7 PP |
643 | |
644 | . The ``pass:[{]`` prefix. | |
645 | ||
646 | . A valid {py3} expression. | |
05f81895 | 647 | + |
269f6eb3 | 648 | For a fixed-length number at some source location{nbsp}__**L**__, this |
05f81895 PP |
649 | expression may contain the name of any accessible <<label,label>> (not |
650 | within a nested group), including the name of a label defined | |
651 | after{nbsp}__**L**__, as well as the name of any | |
652 | <<variable-assignment,variable>> known at{nbsp}__**L**__. | |
653 | + | |
269f6eb3 PP |
654 | The value of the special name `ICITTE` (`int` type) in this expression |
655 | is the <<cur-offset,current offset>> (before encoding the number). | |
71aaa3f7 PP |
656 | |
657 | . The `:` character. | |
658 | ||
269f6eb3 PP |
659 | . An encoding length in bits amongst: |
660 | + | |
661 | -- | |
27d52a19 | 662 | The expression evaluates to an `int` or `bool` value:: |
269f6eb3 | 663 | `8`, `16`, `24`, `32`, `40`, `48`, `56`, and `64`. |
27d52a19 PP |
664 | + |
665 | NOTE: Normand automatically converts a `bool` value to `int`. | |
269f6eb3 PP |
666 | |
667 | The expression evaluates to a `float` value:: | |
668 | `32` and `64`. | |
669 | -- | |
71aaa3f7 PP |
670 | |
671 | . The `}` suffix. | |
672 | ||
673 | ==== | |
674 | Input: | |
675 | ||
676 | ---- | |
677 | {le} {345:16} | |
678 | {be} {-0xabcd:32} | |
679 | ---- | |
680 | ||
681 | Output: | |
682 | ||
683 | ---- | |
684 | 59 01 ff ff 54 33 | |
685 | ---- | |
686 | ==== | |
687 | ||
688 | ==== | |
689 | Input: | |
690 | ||
691 | ---- | |
692 | {be} | |
693 | ||
694 | # String length in bits | |
695 | {8 * (str_end - str_beg) : 16} | |
696 | ||
697 | # String | |
698 | <str_beg> | |
699 | "hello world!" | |
700 | <str_end> | |
701 | ---- | |
702 | ||
703 | Output: | |
704 | ||
705 | ---- | |
706 | 00 60 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 ┆ •`hello world! | |
707 | ---- | |
708 | ==== | |
709 | ||
710 | ==== | |
711 | Input: | |
712 | ||
713 | ---- | |
714 | {20 - ICITTE : 8} * 10 | |
715 | ---- | |
716 | ||
717 | Output: | |
718 | ||
719 | ---- | |
720 | 14 13 12 11 10 0f 0e 0d 0c 0b | |
721 | ---- | |
722 | ==== | |
723 | ||
269f6eb3 PP |
724 | ==== |
725 | Input: | |
726 | ||
727 | ---- | |
728 | {le} | |
729 | {2 * 0.0529 : 32} | |
730 | ---- | |
731 | ||
732 | Output: | |
733 | ||
734 | ---- | |
735 | ac ad d8 3d | |
736 | ---- | |
737 | ==== | |
738 | ||
05f81895 PP |
739 | === LEB128 integer |
740 | ||
741 | An _LEB128 integer_ represents a variable number of bytes encoding an | |
742 | unsigned or signed integer which is the result of evaluating a {py3} | |
743 | expression following the https://en.wikipedia.org/wiki/LEB128[LEB128] | |
744 | format. | |
745 | ||
746 | An LEB128 integer is: | |
747 | ||
748 | . The ``pass:[{]`` prefix. | |
749 | ||
27d52a19 PP |
750 | . A valid {py3} expression of which the evaluation result type |
751 | is `int` or `bool` (automatically converted to `int`). | |
05f81895 PP |
752 | + |
753 | For an LEB128 integer at some source location{nbsp}__**L**__, this | |
754 | expression may contain: | |
755 | + | |
756 | -- | |
757 | * The name of any <<label,label>> defined before{nbsp}__**L**__. | |
758 | * The name of any <<variable-assignment,variable>> known at{nbsp}__**L**__ | |
759 | which doesn't, directly or indirectly, refer to a label | |
760 | defined after{nbsp}__**L**__. | |
761 | -- | |
762 | + | |
269f6eb3 PP |
763 | The value of the special name `ICITTE` (`int` type) in this expression |
764 | is the <<cur-offset,current offset>> (before encoding the integer). | |
05f81895 PP |
765 | |
766 | . The `:` character. | |
767 | ||
768 | . One of: | |
769 | + | |
770 | -- | |
771 | [horizontal] | |
772 | `uleb128`:: Use the unsigned LEB128 format. | |
773 | `sleb128`:: Use the signed LEB128 format. | |
774 | -- | |
775 | ||
776 | . The `}` suffix. | |
777 | ||
778 | ==== | |
779 | Input: | |
780 | ||
781 | ---- | |
782 | {624485 : uleb128} | |
783 | ---- | |
784 | ||
785 | Output: | |
786 | ||
787 | ---- | |
788 | e5 8e 26 | |
789 | ---- | |
790 | ==== | |
791 | ||
792 | ==== | |
793 | Input: | |
794 | ||
795 | ---- | |
796 | aa bb cc dd | |
797 | <meow> | |
798 | ee ff | |
799 | {-981238311 + (meow * -23) : sleb128} | |
800 | "hello" | |
801 | ---- | |
802 | ||
c2b79cf6 PP |
803 | Output: |
804 | ||
05f81895 PP |
805 | ---- |
806 | aa bb cc dd ee ff fd fa 8d ac 7c 68 65 6c 6c 6f ┆ ••••••••••|hello | |
807 | ---- | |
808 | ==== | |
809 | ||
71aaa3f7 PP |
810 | === Current offset setting |
811 | ||
812 | This special item sets the <<cur-offset,_current offset_>>. | |
813 | ||
814 | A current offset setting is: | |
815 | ||
816 | . The `<` prefix. | |
817 | ||
818 | . A positive integer (hexadecimal starting with `0x` or `0X` accepted) | |
819 | which is the new current offset. | |
820 | ||
821 | . The `>` suffix. | |
822 | ||
823 | ==== | |
824 | Input: | |
825 | ||
826 | ---- | |
827 | {ICITTE : 8} * 8 | |
828 | <0x61> {ICITTE : 8} * 8 | |
829 | ---- | |
830 | ||
831 | Output: | |
832 | ||
833 | ---- | |
834 | 00 01 02 03 04 05 06 07 61 62 63 64 65 66 67 68 ┆ ••••••••abcdefgh | |
835 | ---- | |
836 | ==== | |
837 | ||
838 | ==== | |
839 | Input: | |
840 | ||
841 | ---- | |
842 | aa bb cc dd <meow> ee ff | |
843 | <12> 11 22 33 <mix> 44 55 | |
844 | {meow : 8} {mix : 8} | |
845 | ---- | |
846 | ||
847 | Output: | |
848 | ||
849 | ---- | |
850 | aa bb cc dd ee ff 11 22 33 44 55 04 0f ┆ •••••••"3DU•• | |
851 | ---- | |
852 | ==== | |
853 | ||
676f6189 PP |
854 | === Current offset alignment |
855 | ||
00deb9fa | 856 | A _current offset alignment_ represents zero or more padding bytes to |
676f6189 PP |
857 | make the <<cur-offset,current offset>> meet a given |
858 | https://en.wikipedia.org/wiki/Data_structure_alignment[alignment] value. | |
859 | ||
860 | More specifically, for an alignment value of{nbsp}__**N**__{nbsp}bits, | |
861 | a current offset alignment represents the required padding bytes until | |
862 | the current offset is a multiple of __**N**__{nbsp}/{nbsp}8. | |
863 | ||
864 | A current offset alignment is: | |
865 | ||
866 | . The `@` prefix. | |
867 | ||
868 | . A positive integer (hexadecimal starting with `0x` or `0X` accepted) | |
869 | which is the alignment value in _bits_. | |
870 | + | |
871 | This value must be greater than zero and a multiple of{nbsp}8. | |
872 | ||
873 | . **Optional**: | |
874 | + | |
875 | -- | |
876 | . The ``pass:[~]`` prefix. | |
877 | . A positive integer (hexadecimal starting with `0x` or `0X` accepted) | |
878 | which is the value of the byte to use as padding to align the | |
879 | <<cur-offset,current offset>>. | |
880 | -- | |
881 | + | |
882 | Without this section, the padding byte value is zero. | |
883 | ||
884 | ==== | |
885 | Input: | |
886 | ||
887 | ---- | |
888 | 11 22 (@32 aa bb cc) * 3 | |
889 | ---- | |
890 | ||
891 | Output: | |
892 | ||
893 | ---- | |
894 | 11 22 00 00 aa bb cc 00 aa bb cc 00 aa bb cc | |
895 | ---- | |
896 | ==== | |
897 | ||
898 | ==== | |
899 | Input: | |
900 | ||
901 | ---- | |
902 | {le} | |
903 | 77 88 | |
904 | @32~0xcc {-893.5:32} | |
905 | @128~0x55 "meow" | |
906 | ---- | |
907 | ||
908 | Output: | |
909 | ||
910 | ---- | |
911 | 77 88 cc cc 00 60 5f c4 55 55 55 55 55 55 55 55 ┆ w••••`_•UUUUUUUU | |
912 | 6d 65 6f 77 ┆ meow | |
913 | ---- | |
914 | ==== | |
915 | ||
916 | ==== | |
917 | Input: | |
918 | ||
919 | ---- | |
920 | aa bb cc <29> @64~255 "zoom" | |
921 | ---- | |
922 | ||
923 | Output: | |
924 | ||
925 | ---- | |
926 | aa bb cc ff ff ff 7a 6f 6f 6d ┆ ••••••zoom | |
927 | ---- | |
928 | ==== | |
929 | ||
71aaa3f7 PP |
930 | === Label |
931 | ||
932 | A _label_ associates a name to the <<cur-offset,current offset>>. | |
933 | ||
934 | All the labels of a whole Normand input must have unique names. | |
935 | ||
05f81895 | 936 | A label must not share the name of a <<variable-assignment,variable>> |
71aaa3f7 PP |
937 | name. |
938 | ||
71aaa3f7 PP |
939 | A label is: |
940 | ||
941 | . The `<` prefix. | |
942 | ||
27d52a19 | 943 | . A valid {py3} name which is not `ICITTE`. |
71aaa3f7 PP |
944 | |
945 | . The `>` suffix. | |
946 | ||
947 | === Variable assignment | |
948 | ||
949 | A _variable assignment_ associates a name to the integral result of an | |
950 | evaluated {py3} expression. | |
951 | ||
05f81895 | 952 | A variable assignment is: |
71aaa3f7 PP |
953 | |
954 | . The ``pass:[{]`` prefix. | |
955 | ||
27d52a19 | 956 | . A valid {py3} name which is not `ICITTE`. |
71aaa3f7 PP |
957 | |
958 | . The `=` character. | |
959 | ||
27d52a19 PP |
960 | . A valid {py3} expression of which the evaluation result type |
961 | is `int`, `float`, or `bool` (automatically converted to `int`). | |
05f81895 PP |
962 | + |
963 | For a variable assignment at some source location{nbsp}__**L**__, this | |
964 | expression may contain the name of any accessible <<label,label>> (not | |
965 | within a nested group), including the name of a label defined | |
966 | after{nbsp}__**L**__, as well as the name of any | |
967 | <<variable-assignment,variable>> known at{nbsp}__**L**__. | |
968 | + | |
269f6eb3 PP |
969 | The value of the special name `ICITTE` (`int` type) in this expression |
970 | is the <<cur-offset,current offset>>. | |
71aaa3f7 PP |
971 | |
972 | . The `}` suffix. | |
973 | ||
974 | ==== | |
975 | Input: | |
976 | ||
977 | ---- | |
978 | {mix = 101} {le} | |
979 | {meow = 42} 11 22 {meow:8} 33 {meow = ICITTE + 17} | |
980 | "yooo" {meow + mix : 16} | |
981 | ---- | |
982 | ||
983 | Output: | |
984 | ||
985 | ---- | |
986 | 11 22 2a 33 79 6f 6f 6f 7a 00 ┆ •"*3yoooz• | |
987 | ---- | |
988 | ==== | |
989 | ||
990 | === Group | |
991 | ||
992 | A _group_ is a scoped sequence of items. | |
993 | ||
994 | The <<label,labels>> within a group aren't visible outside of it. | |
995 | ||
e57a18e1 PP |
996 | The main purpose of a group is to <<post-item-repetition,repeat>> more |
997 | than a single item and to isolate labels. | |
71aaa3f7 PP |
998 | |
999 | A group is: | |
1000 | ||
1001 | . The `(` prefix. | |
1002 | ||
1003 | . Zero or more items. | |
1004 | ||
1005 | . The `)` suffix. | |
1006 | ||
1007 | ==== | |
1008 | Input: | |
1009 | ||
1010 | ---- | |
1011 | ((aa bb cc) dd () ee) "leclerc" | |
1012 | ---- | |
1013 | ||
1014 | Output: | |
1015 | ||
1016 | ---- | |
1017 | aa bb cc dd ee 6c 65 63 6c 65 72 63 ┆ •••••leclerc | |
1018 | ---- | |
1019 | ==== | |
1020 | ||
1021 | ==== | |
1022 | Input: | |
1023 | ||
1024 | ---- | |
1025 | ((aa bb cc) * 3 dd ee) * 5 | |
1026 | ---- | |
1027 | ||
1028 | Output: | |
1029 | ||
1030 | ---- | |
1031 | aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa bb | |
1032 | cc aa bb cc dd ee aa bb cc aa bb cc aa bb cc dd | |
1033 | ee aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa | |
1034 | bb cc aa bb cc dd ee | |
1035 | ---- | |
1036 | ==== | |
1037 | ||
1038 | ==== | |
1039 | Input: | |
1040 | ||
1041 | ---- | |
1042 | {be} | |
1043 | ( | |
1044 | <str_beg> u16le"sébastien diaz" <str_end> | |
1045 | {ICITTE - str_beg : 8} | |
1046 | {(end - str_beg) * 5 : 24} | |
1047 | ) * 3 | |
1048 | <end> | |
1049 | ---- | |
1050 | ||
1051 | Output: | |
1052 | ||
1053 | ---- | |
1054 | 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e• | |
1055 | 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 e0 ┆ n• •d•i•a•z••••• | |
1056 | 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e• | |
1057 | 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 40 ┆ n• •d•i•a•z••••@ | |
1058 | 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e• | |
1059 | 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 00 a0 ┆ n• •d•i•a•z••••• | |
1060 | ---- | |
1061 | ==== | |
1062 | ||
27d52a19 PP |
1063 | === Conditional block |
1064 | ||
1065 | A _conditional block_ represents either the bytes of one or more items | |
1066 | if some expression is true, or no bytes at all if it's false. | |
1067 | ||
1068 | A conditional block is: | |
1069 | ||
1070 | . The `!if` prefix. | |
1071 | ||
1072 | . One of: | |
1073 | ||
1074 | ** The ``pass:[{]`` prefix, a valid {py3} expression of which the | |
1075 | evaluation result type is `int` or `bool` (automatically converted to | |
1076 | `int`), and the ``pass:[}]`` suffix. | |
1077 | + | |
1078 | For a repetition at some source location{nbsp}__**L**__, this expression | |
1079 | may contain: | |
1080 | + | |
1081 | -- | |
1082 | * The name of any <<label,label>> defined before{nbsp}__**L**__ | |
1083 | which isn't within a nested group. | |
1084 | * The name of any <<variable-assignment,variable>> known | |
1085 | at{nbsp}__**L**__ which doesn't, directly or indirectly, refer to a | |
1086 | label defined after{nbsp}__**L**__. | |
1087 | -- | |
1088 | + | |
1089 | The value of the special name `ICITTE` (`int` type) in this expression | |
1090 | is the <<cur-offset,current offset>> (before handling the contained | |
1091 | items). | |
1092 | ||
1093 | ** A valid {py3} name. | |
1094 | + | |
1095 | For the name `__NAME__`, this is equivalent to the | |
1096 | `pass:[{]__NAME__pass:[}]` form above. | |
1097 | ||
1098 | . Zero or more items. | |
1099 | ||
1100 | . The `!end` suffix. | |
1101 | ||
1102 | ==== | |
1103 | Input: | |
1104 | ||
1105 | ---- | |
1106 | {at = 1} | |
1107 | {rep_count = 9} | |
1108 | ||
1109 | !repeat rep_count | |
1110 | "meow " | |
1111 | ||
1112 | !if {ICITTE > 25} | |
1113 | "mix" | |
1114 | ||
1115 | !if {at < rep_count} 20 !end | |
1116 | !end | |
1117 | ||
1118 | {at = at + 1} | |
1119 | !end | |
1120 | ---- | |
1121 | ||
1122 | Output: | |
1123 | ||
1124 | ---- | |
1125 | 6d 65 6f 77 20 6d 65 6f 77 20 6d 65 6f 77 20 6d ┆ meow meow meow m | |
1126 | 65 6f 77 20 6d 65 6f 77 20 6d 65 6f 77 20 6d 69 ┆ eow meow meow mi | |
1127 | 78 20 6d 65 6f 77 20 6d 69 78 20 6d 65 6f 77 20 ┆ x meow mix meow | |
1128 | 6d 69 78 20 6d 65 6f 77 20 6d 69 78 ┆ mix meow mix | |
1129 | ---- | |
1130 | ==== | |
1131 | ||
1132 | ==== | |
1133 | Input: | |
1134 | ||
1135 | ---- | |
1136 | <str_beg> | |
1137 | u16le"meow mix!" | |
1138 | <str_end> | |
1139 | ||
1140 | !if {str_end - str_beg > 10} | |
1141 | " BIG" | |
1142 | !end | |
1143 | ---- | |
1144 | ||
1145 | Output: | |
1146 | ||
1147 | ---- | |
1148 | 6d 00 65 00 6f 00 77 00 20 00 6d 00 69 00 78 00 ┆ m•e•o•w• •m•i•x• | |
1149 | 21 00 20 42 49 47 ┆ !• BIG | |
1150 | ---- | |
1151 | ==== | |
1152 | ||
e57a18e1 | 1153 | === Repetition block |
71aaa3f7 | 1154 | |
e57a18e1 PP |
1155 | A _repetition block_ represents the bytes of one or more items repeated |
1156 | a given number of times. | |
676f6189 | 1157 | |
e57a18e1 | 1158 | A repetition block is: |
71aaa3f7 | 1159 | |
e57a18e1 | 1160 | . The `!repeat` or `!r` prefix. |
71aaa3f7 | 1161 | |
2adf4336 PP |
1162 | . One of: |
1163 | ||
1164 | ** A positive integer (hexadecimal starting with `0x` or `0X` accepted) | |
1165 | which is the number of times to repeat the previous item. | |
1166 | ||
27d52a19 PP |
1167 | ** The ``pass:[{]`` prefix, a valid {py3} expression of which the |
1168 | evaluation result type is `int` or `bool` (automatically converted to | |
1169 | `int`), and the ``pass:[}]`` suffix. | |
05f81895 PP |
1170 | + |
1171 | For a repetition at some source location{nbsp}__**L**__, this expression | |
1172 | may contain: | |
1173 | + | |
1174 | -- | |
27d52a19 PP |
1175 | * The name of any <<label,label>> defined before{nbsp}__**L**__ |
1176 | which isn't within a nested group. | |
05f81895 | 1177 | * The name of any <<variable-assignment,variable>> known |
e57a18e1 PP |
1178 | at{nbsp}__**L**__ which doesn't, directly or indirectly, refer to a |
1179 | label defined after{nbsp}__**L**__. | |
05f81895 PP |
1180 | -- |
1181 | + | |
e57a18e1 PP |
1182 | The value of the special name `ICITTE` (`int` type) in this expression |
1183 | is the <<cur-offset,current offset>> (before handling the items to | |
1184 | repeat). | |
1185 | ||
1186 | ** A valid {py3} name. | |
1187 | + | |
1188 | For the name `__NAME__`, this is equivalent to the | |
1189 | `pass:[{]__NAME__pass:[}]` form above. | |
1190 | ||
1191 | . Zero or more items. | |
1192 | ||
1193 | . The `!end` suffix. | |
1194 | ||
1195 | You may also use a <<post-item-repetition,post-item repetition>> after | |
1196 | some items. The form ``!repeat{nbsp}__X__{nbsp}__ITEMS__{nbsp}!end`` | |
1197 | is equivalent to ``(__ITEMS__){nbsp}pass:[*]{nbsp}__X__``. | |
71aaa3f7 PP |
1198 | |
1199 | ==== | |
1200 | Input: | |
1201 | ||
1202 | ---- | |
e57a18e1 PP |
1203 | !repeat 0x100 |
1204 | {end - ICITTE - 1 : 8} | |
1205 | !end | |
1206 | ||
1207 | <end> | |
71aaa3f7 PP |
1208 | ---- |
1209 | ||
1210 | Output: | |
1211 | ||
1212 | ---- | |
1213 | ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ •••••••••••••••• | |
1214 | ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ •••••••••••••••• | |
1215 | df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ •••••••••••••••• | |
1216 | cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ •••••••••••••••• | |
1217 | bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ •••••••••••••••• | |
1218 | af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ •••••••••••••••• | |
1219 | 9f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ •••••••••••••••• | |
1220 | 8f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ •••••••••••••••• | |
1221 | 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp | |
1222 | 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba` | |
1223 | 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP | |
1224 | 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@ | |
1225 | 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210 | |
1226 | 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"! | |
1227 | 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ •••••••••••••••• | |
1228 | 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ •••••••••••••••• | |
1229 | ---- | |
1230 | ==== | |
1231 | ||
2adf4336 PP |
1232 | ==== |
1233 | Input: | |
1234 | ||
1235 | ---- | |
1236 | {times = 1} | |
e57a18e1 | 1237 | |
2adf4336 | 1238 | aa bb cc dd |
e57a18e1 PP |
1239 | |
1240 | !repeat 3 | |
2adf4336 | 1241 | <here> |
e57a18e1 PP |
1242 | |
1243 | !repeat {here + 1} | |
1244 | ee ff | |
1245 | !end | |
1246 | ||
1247 | 11 22 !repeat times 33 !end | |
1248 | ||
2adf4336 | 1249 | {times = times + 1} |
e57a18e1 PP |
1250 | !end |
1251 | ||
2adf4336 PP |
1252 | "coucou!" |
1253 | ---- | |
1254 | ||
1255 | Output: | |
1256 | ||
1257 | ---- | |
1258 | aa bb cc dd ee ff ee ff ee ff ee ff ee ff 11 22 ┆ •••••••••••••••" | |
1259 | 33 ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ 3••••••••••••••• | |
1260 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1261 | ff ee ff ee ff 11 22 33 33 ee ff ee ff ee ff ee ┆ ••••••"33••••••• | |
1262 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1263 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1264 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1265 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1266 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1267 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1268 | ff ee ff ee ff ee ff ee ff ee ff ee ff 11 22 33 ┆ ••••••••••••••"3 | |
1269 | 33 33 63 6f 75 63 6f 75 21 ┆ 33coucou! | |
1270 | ---- | |
1271 | ==== | |
1272 | ||
e57a18e1 PP |
1273 | === Post-item repetition |
1274 | ||
1275 | A _post-item repetition_ represents the bytes of an item repeated a | |
1276 | given number of times. | |
1277 | ||
1278 | A post-item repetition is: | |
1279 | ||
27d52a19 | 1280 | . One of those items: |
e57a18e1 | 1281 | |
27d52a19 PP |
1282 | ** A <<byte-constant,byte constant>>. |
1283 | ** A <<literal-string,literal string>>. | |
1284 | ** A <<fixed-length-number,fixed-length number>>. | |
1285 | ** An <<leb128-integer,LEB128 integer>>. | |
1286 | ** A <<group,group>>. | |
e57a18e1 PP |
1287 | |
1288 | . The ``pass:[*]`` character. | |
1289 | ||
1290 | . One of: | |
1291 | ||
1292 | ** A positive integer (hexadecimal starting with `0x` or `0X` accepted) | |
1293 | which is the number of times to repeat the previous item. | |
1294 | ||
27d52a19 PP |
1295 | ** The ``pass:[{]`` prefix, a valid {py3} expression of which the |
1296 | evaluation result type is `int` or `bool` (automatically converted to | |
1297 | `int`), and the ``pass:[}]`` suffix. | |
e57a18e1 PP |
1298 | + |
1299 | For a repetition at some source location{nbsp}__**L**__, this expression | |
1300 | may contain: | |
1301 | + | |
1302 | -- | |
27d52a19 PP |
1303 | * The name of any <<label,label>> defined before{nbsp}__**L**__ |
1304 | which isn't within a nested group and | |
1305 | which isn't part of the repeated item. | |
e57a18e1 PP |
1306 | * The name of any <<variable-assignment,variable>> known |
1307 | at{nbsp}__**L**__, which isn't part of its repeated item, and which | |
1308 | doesn't, directly or indirectly, refer to a label defined | |
1309 | after{nbsp}__**L**__. | |
1310 | -- | |
1311 | + | |
1312 | The value of the special name `ICITTE` (`int` type) in this expression | |
1313 | is the <<cur-offset,current offset>> (before handling the items to | |
1314 | repeat). | |
1315 | ||
1316 | ** A valid {py3} name. | |
1317 | + | |
1318 | For the name `__NAME__`, this is equivalent to the | |
1319 | `pass:[{]__NAME__pass:[}]` form above. | |
1320 | ||
1321 | You may also use a <<repetition-block,repetition block>>. The form | |
1322 | ``__ITEM__{nbsp}pass:[*]{nbsp}__X__`` is equivalent to | |
1323 | ``!repeat{nbsp}__X__{nbsp}__ITEM__{nbsp}!end``. | |
1324 | ||
1325 | ==== | |
1326 | Input: | |
1327 | ||
1328 | ---- | |
1329 | {end - ICITTE - 1 : 8} * 0x100 <end> | |
1330 | ---- | |
1331 | ||
1332 | Output: | |
1333 | ||
1334 | ---- | |
1335 | ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ •••••••••••••••• | |
1336 | ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ •••••••••••••••• | |
1337 | df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ •••••••••••••••• | |
1338 | cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ •••••••••••••••• | |
1339 | bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ •••••••••••••••• | |
1340 | af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ •••••••••••••••• | |
1341 | 9f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ •••••••••••••••• | |
1342 | 8f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ •••••••••••••••• | |
1343 | 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp | |
1344 | 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba` | |
1345 | 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP | |
1346 | 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@ | |
1347 | 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210 | |
1348 | 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"! | |
1349 | 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ •••••••••••••••• | |
1350 | 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ •••••••••••••••• | |
1351 | ---- | |
1352 | ==== | |
1353 | ||
1354 | ==== | |
1355 | Input: | |
1356 | ||
1357 | ---- | |
1358 | {times = 1} | |
1359 | aa bb cc dd | |
1360 | ( | |
1361 | <here> | |
1362 | (ee ff) * {here + 1} | |
1363 | 11 22 33 * {times} | |
1364 | {times = times + 1} | |
1365 | ) * 3 | |
1366 | "coucou!" | |
1367 | ---- | |
1368 | ||
1369 | Output: | |
1370 | ||
1371 | ---- | |
1372 | aa bb cc dd ee ff ee ff ee ff ee ff ee ff 11 22 ┆ •••••••••••••••" | |
1373 | 33 ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ 3••••••••••••••• | |
1374 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1375 | ff ee ff ee ff 11 22 33 33 ee ff ee ff ee ff ee ┆ ••••••"33••••••• | |
1376 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1377 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1378 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1379 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1380 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1381 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• | |
1382 | ff ee ff ee ff ee ff ee ff ee ff ee ff 11 22 33 ┆ ••••••••••••••"3 | |
1383 | 33 33 63 6f 75 63 6f 75 21 ┆ 33coucou! | |
1384 | ---- | |
1385 | ==== | |
1386 | ||
71aaa3f7 PP |
1387 | == Command-line tool |
1388 | ||
1389 | If you <<install-normand,installed>> the `normand` package, then you | |
1390 | can use the `normand` command-line tool: | |
1391 | ||
1392 | ---- | |
1393 | $ normand <<< '"ma gang de malades"' | hexdump -C | |
1394 | ---- | |
1395 | ||
1396 | ---- | |
1397 | 00000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad| | |
1398 | 00000010 65 73 |es| | |
1399 | ---- | |
1400 | ||
1401 | If you copy the `normand.py` module to your own project, then you can | |
1402 | run the module itself: | |
1403 | ||
1404 | ---- | |
1405 | $ python3 -m normand <<< '"ma gang de malades"' | hexdump -C | |
1406 | ---- | |
1407 | ||
1408 | ---- | |
1409 | 00000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad| | |
1410 | 00000010 65 73 |es| | |
1411 | ---- | |
1412 | ||
1413 | Without a path argument, the `normand` tool reads from the standard | |
1414 | input. | |
1415 | ||
1416 | The `normand` tool prints the generated binary data to the standard | |
1417 | output. | |
1418 | ||
1419 | Various options control the initial <<state,state>> of the processor: | |
1420 | use the `--help` option to learn more. | |
1421 | ||
1422 | == {py3} API | |
1423 | ||
e57a18e1 | 1424 | The whole `normand` package/module public API is: |
71aaa3f7 PP |
1425 | |
1426 | [source,python] | |
1427 | ---- | |
e57a18e1 | 1428 | # Byte order. |
71aaa3f7 PP |
1429 | class ByteOrder(enum.Enum): |
1430 | # Big endian. | |
1431 | BE = ... | |
1432 | ||
1433 | # Little endian. | |
1434 | LE = ... | |
1435 | ||
1436 | ||
e57a18e1 PP |
1437 | # Text location. |
1438 | class TextLocation: | |
71aaa3f7 PP |
1439 | # Line number. |
1440 | @property | |
1441 | def line_no(self) -> int: | |
1442 | ... | |
1443 | ||
1444 | # Column number. | |
1445 | @property | |
1446 | def col_no(self) -> int: | |
1447 | ... | |
1448 | ||
1449 | ||
e57a18e1 | 1450 | # Parsing error. |
71aaa3f7 PP |
1451 | class ParseError(RuntimeError): |
1452 | # Source text location. | |
1453 | @property | |
e57a18e1 | 1454 | def text_loc(self) -> TextLocation: |
71aaa3f7 PP |
1455 | ... |
1456 | ||
1457 | ||
e57a18e1 PP |
1458 | # Variables dictionary type (for type hints). |
1459 | VariablesT = typing.Dict[str, typing.Union[int, float]] | |
1460 | ||
1461 | ||
1462 | # Labels dictionary type (for type hints). | |
1463 | LabelsT = typing.Dict[str, int] | |
1b8aa84a PP |
1464 | |
1465 | ||
e57a18e1 | 1466 | # Parsing result. |
71aaa3f7 PP |
1467 | class ParseResult: |
1468 | # Generated data. | |
1469 | @property | |
1470 | def data(self) -> bytearray: | |
1471 | ... | |
1472 | ||
1473 | # Updated variable values. | |
1474 | @property | |
1b8aa84a | 1475 | def variables(self) -> SymbolsT: |
71aaa3f7 PP |
1476 | ... |
1477 | ||
1478 | # Updated main group label values. | |
1479 | @property | |
1b8aa84a | 1480 | def labels(self) -> SymbolsT: |
71aaa3f7 PP |
1481 | ... |
1482 | ||
1483 | # Final offset. | |
1484 | @property | |
1485 | def offset(self) -> int: | |
1486 | ... | |
1487 | ||
1488 | # Final byte order. | |
1489 | @property | |
1b8aa84a | 1490 | def byte_order(self) -> typing.Optional[ByteOrder]: |
71aaa3f7 PP |
1491 | ... |
1492 | ||
1b8aa84a | 1493 | |
e57a18e1 PP |
1494 | # Parses the `normand` input using the initial state defined by |
1495 | # `init_variables`, `init_labels`, `init_offset`, and `init_byte_order`, | |
1496 | # and returns the corresponding parsing result. | |
71aaa3f7 | 1497 | def parse(normand: str, |
1b8aa84a PP |
1498 | init_variables: typing.Optional[SymbolsT] = None, |
1499 | init_labels: typing.Optional[SymbolsT] = None, | |
71aaa3f7 PP |
1500 | init_offset: int = 0, |
1501 | init_byte_order: typing.Optional[ByteOrder] = None) -> ParseResult: | |
1502 | ... | |
1503 | ---- | |
1504 | ||
1505 | The `normand` parameter is the actual <<learn-normand,Normand input>> | |
1506 | while the other parameters control the initial <<state,state>>. | |
1507 | ||
1508 | The `parse()` function raises a `ParseError` instance should it fail to | |
1509 | parse the `normand` string for any reason. | |
bf8f3b38 PP |
1510 | |
1511 | == Development | |
1512 | ||
1513 | Normand is a https://python-poetry.org/[Poetry] project. | |
1514 | ||
1515 | To develop it, install it through Poetry and enter the virtual | |
1516 | environment: | |
1517 | ||
1518 | ---- | |
1519 | $ poetry install | |
1520 | $ poetry shell | |
1521 | $ normand <<< '"lol" * 10 0a' | |
1522 | ---- | |
1523 | ||
1524 | `normand.py` is processed by: | |
1525 | ||
1526 | * https://microsoft.github.io/pyright/[Pyright] | |
1527 | * https://github.com/psf/black[Black] | |
1528 | * https://pycqa.github.io/isort/[isort] | |
1529 | ||
1530 | === Testing | |
1531 | ||
1532 | Use https://docs.pytest.org/[pytest] to test Normand once the package is | |
1533 | part of your virtual environment, for example: | |
1534 | ||
1535 | ---- | |
1536 | $ poetry install | |
1537 | $ poetry run pip3 install pytest | |
1538 | $ poetry run pytest | |
1539 | ---- | |
1540 | ||
1541 | The `pytest` project is currently not a development dependency in | |
1542 | `pyproject.toml` due to backward compatibiliy issues with | |
1543 | Python{nbsp}3.4. | |
1544 | ||
1545 | In the `tests` directory, each `*.nt` file is a test. The file name | |
1546 | prefix indicates what it's meant to test: | |
1547 | ||
1548 | `pass-`:: | |
1549 | Everything above the `---` line is the valid Normand input | |
1550 | to test. | |
1551 | + | |
1552 | Everything below the `---` line is the expected data | |
1553 | (whitespace-separated hexadecimal bytes). | |
1554 | ||
1555 | `fail-`:: | |
1556 | Everything above the `---` line is the invalid Normand input | |
1557 | to test. | |
1558 | + | |
1559 | Everything below the `---` line is the expected error message having | |
1560 | this form: | |
1561 | + | |
1562 | ---- | |
1563 | LINE:COL - MESSAGE | |
1564 | ---- | |
1565 | ||
1566 | === Contributing | |
1567 | ||
1568 | Normand uses https://review.lttng.org/admin/repos/normand,general[Gerrit] | |
1569 | for code review. | |
1570 | ||
1571 | To report a bug, https://github.com/efficios/normand/issues/new[create a | |
1572 | GitHub issue]. |