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 | ||
32 | WARNING: This version of Normand is 0.1, meaning both the Normand | |
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 | ||
136 | Value encoding with a specific length (8{nbsp}bits to 64{nbsp}bits) and byte order:: | |
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} | |
144 | ---- | |
145 | + | |
146 | Output: | |
147 | + | |
148 | ---- | |
149 | 67 44 b2 00 2c 63 37 f8 ff ff | |
150 | ---- | |
151 | + | |
152 | The encoded value is the evaluation of a valid {py3} expression which | |
153 | may include label and variable names. | |
154 | ||
155 | Repetition:: | |
156 | + | |
157 | Input: | |
158 | + | |
159 | ---- | |
160 | aa bb * 5 cc "yeah\0" * 8 | |
161 | ---- | |
162 | + | |
163 | Output: | |
164 | + | |
165 | ---- | |
166 | aa bb bb bb bb bb cc 79 65 61 68 00 79 65 61 68 ┆ •••••••yeah.yeah | |
167 | 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah• | |
168 | 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ yeah•yeah•yeah• | |
169 | ---- | |
170 | ||
171 | ||
172 | Multilevel grouping:: | |
173 | + | |
174 | Input: | |
175 | + | |
176 | ---- | |
177 | ff ((aa bb "zoom" cc) * 5) * 3 $-34 * 4 | |
178 | ---- | |
179 | + | |
180 | Output: | |
181 | + | |
182 | ---- | |
183 | ff aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa ┆ •••zoom•••zoom•• | |
184 | bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a ┆ •zoom•••zoom•••z | |
185 | 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f ┆ oom•••zoom•••zoo | |
186 | 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc ┆ m•••zoom•••zoom• | |
187 | aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb ┆ ••zoom•••zoom••• | |
188 | 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f ┆ zoom•••zoom•••zo | |
189 | 6f 6d cc aa bb 7a 6f 6f 6d cc de de de de ┆ om•••zoom••••• | |
190 | ---- | |
191 | ||
192 | Precise error reporting:: | |
193 | + | |
194 | ---- | |
195 | /tmp/meow.normand:10:24 - Expecting a bit (`0` or `1`). | |
196 | ---- | |
197 | + | |
198 | ---- | |
199 | /tmp/meow.normand:32:6 - Unexpected character `k`. | |
200 | ---- | |
201 | + | |
202 | ---- | |
203 | /tmp/meow.normand:24:19 - Unknown variable/label name `meow` in expression `(meow - 45) // 8`. | |
204 | ---- | |
205 | + | |
206 | ---- | |
207 | /tmp/meow.normand:18:9 - Value 315 is outside the 8-bit range when evaluating expression `end - ICITTE` at byte offset 45. | |
208 | ---- | |
209 | ||
210 | You can use Normand to track data source files in your favorite VCS | |
211 | instead of raw binary files. The binary files that Normand generates can | |
212 | be used to test file format decoding, including malformatted data, for | |
213 | example, as well as for education. | |
214 | ||
215 | See <<learn-normand>> to explore all the Normand features. | |
216 | ||
217 | == Install Normand | |
218 | ||
219 | Normand requires Python ≥ 3.4. | |
220 | ||
221 | To install Normand: | |
222 | ||
223 | ---- | |
224 | $ python3 -m pip install --user normand | |
225 | ---- | |
226 | ||
227 | See | |
228 | https://packaging.python.org/en/latest/tutorials/installing-packages/#installing-to-the-user-site[Installing to the User Site] | |
229 | to learn more about a user site installation. | |
230 | ||
231 | [NOTE] | |
232 | ==== | |
233 | Normand has a single module file, `normand.py`, which you can copy as is | |
234 | to your project to use it (both the <<python-3-api,`normand.parse()`>> | |
235 | function and the <<command-line-tool,command-line tool>>). | |
236 | ||
237 | `normand.py` has _no external dependencies_, but if you're using | |
238 | Python{nbsp}3.4, you'll need a local copy of the standard `typing` | |
239 | module. | |
240 | ==== | |
241 | ||
242 | == Learn Normand | |
243 | ||
244 | A Normand text input is a sequence of items which represent a sequence | |
245 | of raw bytes. | |
246 | ||
247 | [[state]] During the processing of items to data, Normand relies on a | |
248 | current state: | |
249 | ||
250 | [%header%autowidth] | |
251 | |=== | |
252 | |State variable |Description |Initial value: <<python-3-api,{py3} API>> |Initial value: <<command-line-tool,CLI>> | |
253 | ||
254 | |[[cur-offset]] Current offset | |
255 | | | |
256 | The current offset has an effect on the value of | |
257 | <<label,labels>> and of the special `ICITTE` name in <<value,value>> and | |
258 | <<variable-assignment,variable assignment>> expression evaluation. | |
259 | ||
260 | Each generated byte increments the current offset. | |
261 | ||
262 | A <<current-offset-setting,current offset setting>> may change the | |
263 | current offset. | |
264 | |`init_offset` parameter of the `parse()` function. | |
265 | |`--offset` option. | |
266 | ||
267 | |[[cur-bo]] Current byte order | |
268 | | | |
269 | The current byte order has an effect on the encoding of <<value,values>>. | |
270 | ||
271 | A <<current-byte-order-setting,current byte order setting>> may change | |
272 | the current byte order. | |
273 | |`init_byte_order` parameter of the `parse()` function. | |
274 | |`--byte-order` option. | |
275 | ||
276 | |<<label,Labels>> | |
277 | |Mapping of label names to integral values. | |
278 | |`init_labels` parameter of the `parse()` function. | |
279 | |One or more `--label` options. | |
280 | ||
281 | |<<variable-assignment,Variables>> | |
282 | |Mapping of variable names to integral values. | |
283 | |`init_variables` parameter of the `parse()` function. | |
284 | |One or more `--var` options. | |
285 | |=== | |
286 | ||
287 | The available items are: | |
288 | ||
289 | * A <<byte-constant,constant integer>> representing a single byte. | |
290 | ||
291 | * A <<literal-string,literal string>> representing a sequence of bytes | |
292 | encoding UTF-8, UTF-16, or UTF-32 data. | |
293 | ||
294 | * A <<current-byte-order-setting,current byte order setting>> (big or | |
295 | little endian). | |
296 | ||
297 | * A <<value,{py3} expression to be evaluated>> as an unsigned or signed | |
298 | integer to be encoded on one or more bytes using the current byte | |
299 | order. | |
300 | ||
301 | * A <<current-offset-setting,current offset setting>>. | |
302 | ||
303 | * A <<label,label>>, that is, a named constant holding the current | |
304 | offset. | |
305 | + | |
306 | This is similar to an assembly label. | |
307 | ||
308 | * A <<variable-assignment,variable assignment>> associating a name to | |
309 | the integral result of an evaluated {py3} expression. | |
310 | ||
311 | * A <<group,group>>, that is, a scoped sequence of items. | |
312 | ||
313 | Moreover, you can <<repetition,repeat>> any item above, except an offset | |
314 | or a label, a given number of times. This is called a repetition. | |
315 | ||
316 | A Normand comment may exist: | |
317 | ||
318 | * Between items, possibly within a group. | |
319 | * Between the nibbles of a constant hexadecimal byte. | |
320 | * Between the bits of a constant binary byte. | |
321 | * Between the last item and the ``pass:[*]`` character of a repetition, | |
322 | and between that ``pass:[*]`` character and the following number. | |
323 | ||
324 | A comment is anything between two ``pass:[#]`` characters on the same | |
325 | line, or from ``pass:[#]`` until the end of the line. Whitespaces and | |
326 | the following symbol characters are also considered comments where a | |
327 | comment may exist: | |
328 | ||
329 | ---- | |
330 | ! @ / \ ? & : ; . , + [ ] _ = | - | |
331 | ---- | |
332 | ||
333 | The latter serve to improve readability so that you may write, for | |
334 | example, a MAC address or a UUID as is. | |
335 | ||
336 | You can test the examples of this section with the `normand` | |
337 | <<command-line-tool,command-line tool>> as such: | |
338 | ||
339 | ---- | |
340 | $ normand file | hexdump -C | |
341 | ---- | |
342 | ||
343 | where `file` is the name of a file containing the Normand input. | |
344 | ||
345 | === Byte constant | |
346 | ||
347 | A _byte constant_ represents a single byte. | |
348 | ||
349 | A byte constant is: | |
350 | ||
351 | Hexadecimal form:: | |
352 | Two consecutive hexits. | |
353 | ||
354 | Decimal form:: | |
355 | A decimal number after the `$` prefix. | |
356 | ||
357 | Binary form:: | |
358 | Eight bits after the `%` prefix. | |
359 | ||
360 | ==== | |
361 | Input: | |
362 | ||
363 | ---- | |
364 | ab cd [3d 8F] CC | |
365 | ---- | |
366 | ||
367 | Output: | |
368 | ||
369 | ---- | |
370 | ab cd 3d 8f cc | |
371 | ---- | |
372 | ==== | |
373 | ||
374 | ==== | |
375 | Input: | |
376 | ||
377 | ---- | |
378 | $192 %1100/0011 $ -77 | |
379 | ---- | |
380 | ||
381 | Output: | |
382 | ||
383 | ---- | |
384 | c0 c3 b3 | |
385 | ---- | |
386 | ==== | |
387 | ||
388 | ==== | |
389 | Input: | |
390 | ||
391 | ---- | |
392 | 58f64689-6316-4d55-8a1a-04cada366172 | |
393 | fe80::6257:18ff:fea3:4229 | |
394 | ---- | |
395 | ||
396 | Output: | |
397 | ||
398 | ---- | |
399 | 58 f6 46 89 63 16 4d 55 8a 1a 04 ca da 36 61 72 ┆ X•F•c•MU•••••6ar | |
400 | fe 80 62 57 18 ff fe a3 42 29 ┆ ••bW••••B) | |
401 | ---- | |
402 | ==== | |
403 | ||
404 | ==== | |
405 | Input: | |
406 | ||
407 | ---- | |
408 | %01110011 %01100001 %01101100 %01110101 %01110100 | |
409 | ---- | |
410 | ||
411 | Output: | |
412 | ||
413 | ---- | |
414 | 73 61 6c 75 74 ┆ salut | |
415 | ---- | |
416 | ==== | |
417 | ||
418 | === Literal string | |
419 | ||
420 | A _literal string_ represents the UTF-8-, UTF-16-, or UTF-32-encoded | |
421 | bytes of a string. | |
422 | ||
423 | The string to encode isn't implicitly null-terminated: use `\0` at the | |
424 | end of the string to add a null character. | |
425 | ||
426 | A literal string is: | |
427 | ||
428 | . **Optional**: one of the following encodings instead of UTF-8: | |
429 | + | |
430 | -- | |
431 | [horizontal] | |
432 | `u16be`:: UTF-16BE. | |
433 | `u16le`:: UTF-16LE. | |
434 | `u32be`:: UTF-32BE. | |
435 | `u32le`:: UTF-32LE. | |
436 | -- | |
437 | ||
438 | . The ``pass:["]`` prefix. | |
439 | ||
440 | . A sequence of zero or more characters, possibly containing escape | |
441 | sequences. | |
442 | + | |
443 | An escape sequence is the ``\`` character followed by one of: | |
444 | + | |
445 | -- | |
446 | [horizontal] | |
447 | `0`:: Null (U+0000) | |
448 | `a`:: Alert (U+0007) | |
449 | `b`:: Backspace (U+0008) | |
450 | `e`:: Escape (U+001B) | |
451 | `f`:: Form feed (U+000C) | |
452 | `n`:: End of line (U+000A) | |
453 | `r`:: Carriage return (U+000D) | |
454 | `t`:: Character tabulation (U+0009) | |
455 | `v`:: Line tabulation (U+000B) | |
456 | ``\``:: Reverse solidus (U+005C) | |
457 | ``pass:["]``:: Quotation mark (U+0022) | |
458 | -- | |
459 | ||
460 | . The ``pass:["]`` suffix. | |
461 | ||
462 | ==== | |
463 | Input: | |
464 | ||
465 | ---- | |
466 | "coucou tout le monde!" | |
467 | ---- | |
468 | ||
469 | Output: | |
470 | ||
471 | ---- | |
472 | 63 6f 75 63 6f 75 20 74 6f 75 74 20 6c 65 20 6d ┆ coucou tout le m | |
473 | 6f 6e 64 65 21 ┆ onde! | |
474 | ---- | |
475 | ==== | |
476 | ||
477 | ==== | |
478 | Input: | |
479 | ||
480 | ---- | |
481 | u16le"I am not young enough to know everything." | |
482 | ---- | |
483 | ||
484 | Output: | |
485 | ||
486 | ---- | |
487 | 49 00 20 00 61 00 6d 00 20 00 6e 00 6f 00 74 00 ┆ I• •a•m• •n•o•t• | |
488 | 20 00 79 00 6f 00 75 00 6e 00 67 00 20 00 65 00 ┆ •y•o•u•n•g• •e• | |
489 | 6e 00 6f 00 75 00 67 00 68 00 20 00 74 00 6f 00 ┆ n•o•u•g•h• •t•o• | |
490 | 20 00 6b 00 6e 00 6f 00 77 00 20 00 65 00 76 00 ┆ •k•n•o•w• •e•v• | |
491 | 65 00 72 00 79 00 74 00 68 00 69 00 6e 00 67 00 ┆ e•r•y•t•h•i•n•g• | |
492 | 2e 00 ┆ .• | |
493 | ---- | |
494 | ==== | |
495 | ||
496 | ==== | |
497 | Input: | |
498 | ||
499 | ---- | |
500 | u32be "\"illusion is the first\nof all pleasures\" 🦉" | |
501 | ---- | |
502 | ||
503 | Output: | |
504 | ||
505 | ---- | |
506 | 00 00 00 22 00 00 00 69 00 00 00 6c 00 00 00 6c ┆ •••"•••i•••l•••l | |
507 | 00 00 00 75 00 00 00 73 00 00 00 69 00 00 00 6f ┆ •••u•••s•••i•••o | |
508 | 00 00 00 6e 00 00 00 20 00 00 00 69 00 00 00 73 ┆ •••n••• •••i•••s | |
509 | 00 00 00 20 00 00 00 74 00 00 00 68 00 00 00 65 ┆ ••• •••t•••h•••e | |
510 | 00 00 00 20 00 00 00 66 00 00 00 69 00 00 00 72 ┆ ••• •••f•••i•••r | |
511 | 00 00 00 73 00 00 00 74 00 00 00 0a 00 00 00 6f ┆ •••s•••t•••••••o | |
512 | 00 00 00 66 00 00 00 20 00 00 00 61 00 00 00 6c ┆ •••f••• •••a•••l | |
513 | 00 00 00 6c 00 00 00 20 00 00 00 70 00 00 00 6c ┆ •••l••• •••p•••l | |
514 | 00 00 00 65 00 00 00 61 00 00 00 73 00 00 00 75 ┆ •••e•••a•••s•••u | |
515 | 00 00 00 72 00 00 00 65 00 00 00 73 00 00 00 22 ┆ •••r•••e•••s•••" | |
516 | 00 00 00 20 00 01 f9 89 ┆ ••• •••• | |
517 | ---- | |
518 | ==== | |
519 | ||
520 | === Current byte order setting | |
521 | ||
522 | This special item sets the <<cur-bo,_current byte order_>>. | |
523 | ||
524 | The two accepted forms are: | |
525 | ||
526 | [horizontal] | |
527 | ``pass:[{be}]``:: Set the current byte order to big endian. | |
528 | ``pass:[{le}]``:: Set the current byte order to little endian. | |
529 | ||
530 | === Value | |
531 | ||
532 | A _value_ represents a fixed number of bytes encoding an unsigned or | |
533 | signed integer which is the result of evaluating a {py3} expression | |
534 | using the <<cur-bo,current byte order>>. | |
535 | ||
536 | For a value at some source location{nbsp}__**L**__, its {py3} expression | |
537 | may contain the name of any accessible <<label,label>>, including the | |
538 | name of a label defined after{nbsp}__**L**__, as well as the name of any | |
539 | <<variable-assignment,variable>> known at{nbsp}__**L**__. | |
540 | ||
541 | An accessible label is either: | |
542 | ||
543 | * Outside of the current <<group,group>>. | |
544 | * Within the same immediate group (not within a nested group). | |
545 | ||
546 | In the {py3} expression of a value, the value of the special name | |
547 | `ICITTE` is the <<cur-offset,current offset>> (before encoding the | |
548 | value). | |
549 | ||
550 | A value is: | |
551 | ||
552 | . The ``pass:[{]`` prefix. | |
553 | ||
554 | . A valid {py3} expression. | |
555 | ||
556 | . The `:` character. | |
557 | ||
558 | . An encoding length in bits amongst `8`, `16`, `24`, `32`, `40`, | |
559 | `48`, `56`, and `64`. | |
560 | ||
561 | . The `}` suffix. | |
562 | ||
563 | ==== | |
564 | Input: | |
565 | ||
566 | ---- | |
567 | {le} {345:16} | |
568 | {be} {-0xabcd:32} | |
569 | ---- | |
570 | ||
571 | Output: | |
572 | ||
573 | ---- | |
574 | 59 01 ff ff 54 33 | |
575 | ---- | |
576 | ==== | |
577 | ||
578 | ==== | |
579 | Input: | |
580 | ||
581 | ---- | |
582 | {be} | |
583 | ||
584 | # String length in bits | |
585 | {8 * (str_end - str_beg) : 16} | |
586 | ||
587 | # String | |
588 | <str_beg> | |
589 | "hello world!" | |
590 | <str_end> | |
591 | ---- | |
592 | ||
593 | Output: | |
594 | ||
595 | ---- | |
596 | 00 60 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 ┆ •`hello world! | |
597 | ---- | |
598 | ==== | |
599 | ||
600 | ==== | |
601 | Input: | |
602 | ||
603 | ---- | |
604 | {20 - ICITTE : 8} * 10 | |
605 | ---- | |
606 | ||
607 | Output: | |
608 | ||
609 | ---- | |
610 | 14 13 12 11 10 0f 0e 0d 0c 0b | |
611 | ---- | |
612 | ==== | |
613 | ||
614 | === Current offset setting | |
615 | ||
616 | This special item sets the <<cur-offset,_current offset_>>. | |
617 | ||
618 | A current offset setting is: | |
619 | ||
620 | . The `<` prefix. | |
621 | ||
622 | . A positive integer (hexadecimal starting with `0x` or `0X` accepted) | |
623 | which is the new current offset. | |
624 | ||
625 | . The `>` suffix. | |
626 | ||
627 | ==== | |
628 | Input: | |
629 | ||
630 | ---- | |
631 | {ICITTE : 8} * 8 | |
632 | <0x61> {ICITTE : 8} * 8 | |
633 | ---- | |
634 | ||
635 | Output: | |
636 | ||
637 | ---- | |
638 | 00 01 02 03 04 05 06 07 61 62 63 64 65 66 67 68 ┆ ••••••••abcdefgh | |
639 | ---- | |
640 | ==== | |
641 | ||
642 | ==== | |
643 | Input: | |
644 | ||
645 | ---- | |
646 | aa bb cc dd <meow> ee ff | |
647 | <12> 11 22 33 <mix> 44 55 | |
648 | {meow : 8} {mix : 8} | |
649 | ---- | |
650 | ||
651 | Output: | |
652 | ||
653 | ---- | |
654 | aa bb cc dd ee ff 11 22 33 44 55 04 0f ┆ •••••••"3DU•• | |
655 | ---- | |
656 | ==== | |
657 | ||
658 | === Label | |
659 | ||
660 | A _label_ associates a name to the <<cur-offset,current offset>>. | |
661 | ||
662 | All the labels of a whole Normand input must have unique names. | |
663 | ||
664 | A label may not share the name of a <<variable-assignment,variable>> | |
665 | name. | |
666 | ||
667 | A label name may not be `ICITTE` (see <<value>> and | |
668 | <<variable-assignment>> to learn more). | |
669 | ||
670 | A label is: | |
671 | ||
672 | . The `<` prefix. | |
673 | ||
674 | . A valid {py3} name which is not `ICITTE`. | |
675 | ||
676 | . The `>` suffix. | |
677 | ||
678 | === Variable assignment | |
679 | ||
680 | A _variable assignment_ associates a name to the integral result of an | |
681 | evaluated {py3} expression. | |
682 | ||
683 | For a variable assignment at some source location{nbsp}__**L**__, its | |
684 | {py3} expression may contain the name of any accessible <<label,label>>, | |
685 | including the name of a label defined after{nbsp}__**L**__, as well as | |
686 | the name of any variable known at{nbsp}__**L**__. | |
687 | ||
688 | An accessible label is either: | |
689 | ||
690 | * Outside of the current <<group,group>>. | |
691 | * Within the same immediate group (not within a nested group). | |
692 | ||
1f182c43 | 693 | A variable name may not be `ICITTE`. |
71aaa3f7 PP |
694 | |
695 | In the {py3} expression of a variable assignment, the special name | |
696 | `ICITTE` is the <<cur-offset,current offset>>. | |
697 | ||
698 | A variable is: | |
699 | ||
700 | . The ``pass:[{]`` prefix. | |
701 | ||
702 | . A valid {py3} name which is not `ICITTE`. | |
703 | ||
704 | . The `=` character. | |
705 | ||
706 | . A valid {py3} expression. | |
707 | ||
708 | . The `}` suffix. | |
709 | ||
710 | ==== | |
711 | Input: | |
712 | ||
713 | ---- | |
714 | {mix = 101} {le} | |
715 | {meow = 42} 11 22 {meow:8} 33 {meow = ICITTE + 17} | |
716 | "yooo" {meow + mix : 16} | |
717 | ---- | |
718 | ||
719 | Output: | |
720 | ||
721 | ---- | |
722 | 11 22 2a 33 79 6f 6f 6f 7a 00 ┆ •"*3yoooz• | |
723 | ---- | |
724 | ==== | |
725 | ||
726 | === Group | |
727 | ||
728 | A _group_ is a scoped sequence of items. | |
729 | ||
730 | The <<label,labels>> within a group aren't visible outside of it. | |
731 | ||
732 | The main purpose of a group is to <<repetition,repeat>> more than a | |
733 | single item. | |
734 | ||
735 | A group is: | |
736 | ||
737 | . The `(` prefix. | |
738 | ||
739 | . Zero or more items. | |
740 | ||
741 | . The `)` suffix. | |
742 | ||
743 | ==== | |
744 | Input: | |
745 | ||
746 | ---- | |
747 | ((aa bb cc) dd () ee) "leclerc" | |
748 | ---- | |
749 | ||
750 | Output: | |
751 | ||
752 | ---- | |
753 | aa bb cc dd ee 6c 65 63 6c 65 72 63 ┆ •••••leclerc | |
754 | ---- | |
755 | ==== | |
756 | ||
757 | ==== | |
758 | Input: | |
759 | ||
760 | ---- | |
761 | ((aa bb cc) * 3 dd ee) * 5 | |
762 | ---- | |
763 | ||
764 | Output: | |
765 | ||
766 | ---- | |
767 | aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa bb | |
768 | cc aa bb cc dd ee aa bb cc aa bb cc aa bb cc dd | |
769 | ee aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa | |
770 | bb cc aa bb cc dd ee | |
771 | ---- | |
772 | ==== | |
773 | ||
774 | ==== | |
775 | Input: | |
776 | ||
777 | ---- | |
778 | {be} | |
779 | ( | |
780 | <str_beg> u16le"sébastien diaz" <str_end> | |
781 | {ICITTE - str_beg : 8} | |
782 | {(end - str_beg) * 5 : 24} | |
783 | ) * 3 | |
784 | <end> | |
785 | ---- | |
786 | ||
787 | Output: | |
788 | ||
789 | ---- | |
790 | 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e• | |
791 | 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 e0 ┆ n• •d•i•a•z••••• | |
792 | 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e• | |
793 | 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 40 ┆ n• •d•i•a•z••••@ | |
794 | 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e• | |
795 | 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 00 a0 ┆ n• •d•i•a•z••••• | |
796 | ---- | |
797 | ==== | |
798 | ||
799 | === Repetition | |
800 | ||
801 | A _repetition_ represents the bytes of an item repeated a given number | |
802 | of times. | |
803 | ||
804 | A repetition is: | |
805 | ||
806 | . Any item. | |
807 | ||
808 | . The ``pass:[*]`` character. | |
809 | ||
810 | . A positive integer (hexadecimal starting with `0x` or `0X` accepted) | |
811 | which is the number of times to repeat the previous item. | |
812 | ||
813 | ==== | |
814 | Input: | |
815 | ||
816 | ---- | |
817 | {end - ICITTE - 1 : 8} * 0x100 <end> | |
818 | ---- | |
819 | ||
820 | Output: | |
821 | ||
822 | ---- | |
823 | ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ •••••••••••••••• | |
824 | ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ •••••••••••••••• | |
825 | df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ •••••••••••••••• | |
826 | cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ •••••••••••••••• | |
827 | bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ •••••••••••••••• | |
828 | af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ •••••••••••••••• | |
829 | 9f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ •••••••••••••••• | |
830 | 8f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ •••••••••••••••• | |
831 | 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp | |
832 | 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba` | |
833 | 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP | |
834 | 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@ | |
835 | 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210 | |
836 | 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"! | |
837 | 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ •••••••••••••••• | |
838 | 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ •••••••••••••••• | |
839 | ---- | |
840 | ==== | |
841 | ||
842 | == Command-line tool | |
843 | ||
844 | If you <<install-normand,installed>> the `normand` package, then you | |
845 | can use the `normand` command-line tool: | |
846 | ||
847 | ---- | |
848 | $ normand <<< '"ma gang de malades"' | hexdump -C | |
849 | ---- | |
850 | ||
851 | ---- | |
852 | 00000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad| | |
853 | 00000010 65 73 |es| | |
854 | ---- | |
855 | ||
856 | If you copy the `normand.py` module to your own project, then you can | |
857 | run the module itself: | |
858 | ||
859 | ---- | |
860 | $ python3 -m normand <<< '"ma gang de malades"' | hexdump -C | |
861 | ---- | |
862 | ||
863 | ---- | |
864 | 00000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad| | |
865 | 00000010 65 73 |es| | |
866 | ---- | |
867 | ||
868 | Without a path argument, the `normand` tool reads from the standard | |
869 | input. | |
870 | ||
871 | The `normand` tool prints the generated binary data to the standard | |
872 | output. | |
873 | ||
874 | Various options control the initial <<state,state>> of the processor: | |
875 | use the `--help` option to learn more. | |
876 | ||
877 | == {py3} API | |
878 | ||
879 | The whole `normand` package/module API is: | |
880 | ||
881 | [source,python] | |
882 | ---- | |
883 | class ByteOrder(enum.Enum): | |
884 | # Big endian. | |
885 | BE = ... | |
886 | ||
887 | # Little endian. | |
888 | LE = ... | |
889 | ||
890 | ||
891 | VarsT = typing.Dict[str, int] | |
892 | ||
893 | ||
894 | class TextLoc: | |
895 | # Line number. | |
896 | @property | |
897 | def line_no(self) -> int: | |
898 | ... | |
899 | ||
900 | # Column number. | |
901 | @property | |
902 | def col_no(self) -> int: | |
903 | ... | |
904 | ||
905 | ||
906 | class ParseError(RuntimeError): | |
907 | # Source text location. | |
908 | @property | |
909 | def text_loc(self) -> TextLoc: | |
910 | ... | |
911 | ||
912 | ||
913 | class ParseResult: | |
914 | # Generated data. | |
915 | @property | |
916 | def data(self) -> bytearray: | |
917 | ... | |
918 | ||
919 | # Updated variable values. | |
920 | @property | |
921 | def variables(self) -> VarsT: | |
922 | ... | |
923 | ||
924 | # Updated main group label values. | |
925 | @property | |
926 | def labels(self) -> VarsT: | |
927 | ... | |
928 | ||
929 | # Final offset. | |
930 | @property | |
931 | def offset(self) -> int: | |
932 | ... | |
933 | ||
934 | # Final byte order. | |
935 | @property | |
936 | def byte_order(self) -> typing.Optional[int]: | |
937 | ... | |
938 | ||
939 | def parse(normand: str, | |
940 | init_variables: typing.Optional[VarsT] = None, | |
941 | init_labels: typing.Optional[VarsT] = None, | |
942 | init_offset: int = 0, | |
943 | init_byte_order: typing.Optional[ByteOrder] = None) -> ParseResult: | |
944 | ... | |
945 | ---- | |
946 | ||
947 | The `normand` parameter is the actual <<learn-normand,Normand input>> | |
948 | while the other parameters control the initial <<state,state>>. | |
949 | ||
950 | The `parse()` function raises a `ParseError` instance should it fail to | |
951 | parse the `normand` string for any reason. |