README.adoc: fix plural "lengths"
[normand.git] / README.adoc
CommitLineData
bb2f9e9c
PP
1// Show ToC at a specific location for a GitHub rendering
2ifdef::env-github[]
3:toc: macro
4endif::env-github[]
5
6ifndef::env-github[]
71aaa3f7 7:toc: left
bb2f9e9c
PP
8endif::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
19Philippe Proulx
20
df0f8552
PP
21image::normand-logo.png[]
22
71aaa3f7
PP
23[.normal]
24image: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
29This package offers both a portable {py3} module and a command-line
30tool.
31
2acde24a 32WARNING: This version of Normand is 0.23, meaning both the Normand
71aaa3f7
PP
33language and the module/CLI interface aren't stable.
34
bb2f9e9c
PP
35ifdef::env-github[]
36// ToC location for a GitHub rendering
37toc::[]
38endif::env-github[]
39
71aaa3f7
PP
40== Introduction
41
42The purpose of Normand is to consume human-readable text representing
43bytes and to produce the corresponding binary data.
44
45.Simple bytes input.
46====
47Consider the following Normand input:
48
49----
504f 55 32 bb $167 fe %10100111 a9 $-32
51----
52
53The generated nine bytes are:
54
55----
564f 55 32 bb a7 fe a7 a9 e0
57----
58====
59
60As you can see in the last example, the fundamental unit of the Normand
61language is the _byte_. The order in which you list bytes will be the
62order of the generated data.
63
64The Normand language is more than simple lists of bytes, though. Its
65main features are:
66
67Comments, including a bunch of insignificant symbols which may improve readability::
68+
69Input:
70+
71----
72ff bb %1101:0010 # This is a comment
7378 29 af $192 # This too # 99 $-80
74fe80::6257:18ff:fea3:4229
7560:57:18:a3:42:29
7610839636-5d65-4a68-8e6a-21608ddf7258
77----
78+
79Output:
80+
81----
82ff bb d2 78 29 af c0 99 b0 fe 80 62 57 18 ff fe
83a3 42 29 60 57 18 a3 42 29 10 83 96 36 5d 65 4a
8468 8e 6a 21 60 8d df 72 58
85----
86
87Hexadecimal, decimal, and binary byte constants::
88+
89Input:
90+
91----
92aa bb $247 $-89 %0011_0010 %11.01= 10/10
93----
94+
95Output:
96+
97----
98aa bb f7 a7 32 da
99----
100
7a7b31e8 101Strings::
71aaa3f7
PP
102+
103Input:
104+
105----
106"hello world!" 00
107u16le"stress\nverdict 🤣"
7a7b31e8 108s:latin3{hex(ICITTE)}
71aaa3f7
PP
109----
110+
111Output:
112+
113----
11468 65 6c 6c 6f 20 77 6f 72 6c 64 21 00 73 00 74 ┆ hello world!•s•t
11500 72 00 65 00 73 00 73 00 0a 00 76 00 65 00 72 ┆ •r•e•s•s•••v•e•r
7a7b31e8
PP
11600 64 00 69 00 63 00 74 00 20 00 3e d8 23 dd 30 ┆ •d•i•c•t• •>•#•0
11778 32 66 ┆ x2f
71aaa3f7
PP
118----
119
120Labels: special variables holding the offset where they're defined::
121+
122----
123<beg> b2 52 e3 bc 91 05
124$100 $50 <chair> 33 9f fe
12525 e9 89 8a <end>
126----
127
128Variables::
129+
130----
1315e 65 {tower = 47} c6 7f f2 c4
13244 {hurl = tower - 14} b5 {tower = hurl} 26 2d
133----
134+
135The value of a variable assignment is the evaluation of a valid {py3}
136expression which may include label and variable names.
137
269f6eb3 138Fixed-length number with a given length (8{nbsp}bits to 64{nbsp}bits) and byte order::
71aaa3f7
PP
139+
140Input:
141+
142----
143{strength = 4}
ee724c95
PP
144!be 67 <lbl> 44 $178 [(end - lbl) * 8 + strength : 16] $99 <end>
145!le [-1993 : 32]
56996d34 146[-3.141593 : 64be]
71aaa3f7
PP
147----
148+
149Output:
150+
151----
56996d34
PP
15267 44 b2 00 2c 63 37 f8 ff ff c0 09 21 fb 82 c2
153bd 7f
71aaa3f7
PP
154----
155+
269f6eb3 156The encoded number is the evaluation of a valid {py3} expression which
05f81895
PP
157may include label and variable names.
158
159https://en.wikipedia.org/wiki/LEB128[LEB128] integer::
160+
161Input:
162+
163----
ee724c95
PP
164aa bb cc [-1993 : sleb128] <meow> dd ee ff
165[meow * 199 : uleb128]
05f81895
PP
166----
167+
168Output:
169+
170----
171aa bb cc b7 70 dd ee ff e3 07
172----
173+
174The encoded integer is the evaluation of a valid {py3} expression which
71aaa3f7
PP
175may include label and variable names.
176
27d52a19
PP
177Conditional::
178+
179Input:
180+
181----
182aa bb cc
183
184(
185 "foo"
186
187 !if {ICITTE > 10}
188 "bar"
12b5dbc0
PP
189 !else
190 "fight"
27d52a19
PP
191 !end
192) * 4
193----
194+
195Output:
196+
197----
12b5dbc0
PP
198aa bb cc 66 6f 6f 66 69 67 68 74 66 6f 6f 66 69 ┆ •••foofightfoofi
19967 68 74 66 6f 6f 62 61 72 66 6f 6f 62 61 72 ┆ ghtfoobarfoobar
27d52a19
PP
200----
201
71aaa3f7
PP
202Repetition::
203+
204Input:
205+
206----
2adf4336 207aa bb * 5 cc <zoom> "yeah\0" * {zoom * 3}
e57a18e1
PP
208
209!repeat 3
210 ff ee "juice"
211!end
71aaa3f7
PP
212----
213+
214Output:
215+
216----
2adf4336
PP
217aa bb bb bb bb bb cc 79 65 61 68 00 79 65 61 68 ┆ •••••••yeah•yeah
21800 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah•
21979 65 61 68 00 79 65 61 68 00 79 65 61 68 00 79 ┆ yeah•yeah•yeah•y
22065 61 68 00 79 65 61 68 00 79 65 61 68 00 79 65 ┆ eah•yeah•yeah•ye
22161 68 00 79 65 61 68 00 79 65 61 68 00 79 65 61 ┆ ah•yeah•yeah•yea
22268 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 ┆ h•yeah•yeah•yeah
71aaa3f7 22300 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah•
e57a18e1
PP
224ff ee 6a 75 69 63 65 ff ee 6a 75 69 63 65 ff ee ┆ ••juice••juice••
2256a 75 69 63 65 ┆ juice
71aaa3f7
PP
226----
227
676f6189
PP
228Alignment::
229+
230Input:
231+
232----
ee724c95 233!be
676f6189 234
ee724c95
PP
235 [199:32]
236@64 [43:64]
237@16 [-123:16]
238@32~255 [5584:32]
676f6189
PP
239----
240+
241Output:
242+
243----
24400 00 00 c7 00 00 00 00 00 00 00 00 00 00 00 2b
245ff 85 ff ff 00 00 15 d0
246----
71aaa3f7 247
25ca454b
PP
248Filling::
249+
250Input:
251+
252----
ee724c95
PP
253!le
254[0xdeadbeef:32]
255[-1993:16]
256[9:16]
25ca454b 257+0x40
ee724c95 258[ICITTE:8]
25ca454b 259"meow mix"
fc21bb27 260+200~FFh
ee724c95 261[ICITTE:8]
25ca454b
PP
262----
263+
264Output:
265+
266----
267ef be ad de 37 f8 09 00 00 00 00 00 00 00 00 00 ┆ ••••7•••••••••••
26800 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
26900 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
27000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
27140 6d 65 6f 77 20 6d 69 78 ff ff ff ff ff ff ff ┆ @meow mix•••••••
272ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
273ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
274ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
275ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
276ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
277ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
278ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
279ff ff ff ff ff ff ff ff c8 ┆ •••••••••
280----
281
cd33dfe6
PP
282Transformation::
283+
284Input:
285+
286----
ee724c95 287"end of file @ " [end:8]
cd33dfe6
PP
288
289!transform gzip
290 "this part will be gzipped"
291!end
292
293<end>
294----
295+
296Output:
297+
298----
29965 6e 64 20 6f 66 20 66 69 6c 65 20 40 20 3c 1f ┆ end of file @ <•
3008b 08 00 7b 7b 26 65 02 ff 2b c9 c8 2c 56 28 48 ┆ •••{{&e••+••,V(H
3012c 2a 51 28 cf cc c9 51 48 4a 55 48 af ca 2c 28 ┆ ,*Q(•••QHJUH••,(
30248 4d 01 00 d4 cc 5b 8a 19 00 00 00 ┆ HM••••[•••••
303----
304
71aaa3f7
PP
305Multilevel grouping::
306+
307Input:
308+
309----
310ff ((aa bb "zoom" cc) * 5) * 3 $-34 * 4
311----
312+
313Output:
314+
315----
316ff aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa ┆ •••zoom•••zoom••
317bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a ┆ •zoom•••zoom•••z
3186f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f ┆ oom•••zoom•••zoo
3196d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc ┆ m•••zoom•••zoom•
320aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb ┆ ••zoom•••zoom•••
3217a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f ┆ zoom•••zoom•••zo
3226f 6d cc aa bb 7a 6f 6f 6d cc de de de de ┆ om•••zoom•••••
323----
324
320644e2
PP
325Macros::
326+
327Input:
328+
329----
330!macro hello(world)
331 "hello"
332 !if world " world" !end
333!end
334
335!repeat 17
336 ff ff ff ff
337 m:hello({ICITTE > 15 and ICITTE < 60})
338!end
339----
340+
341Output:
342+
343----
344ff ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c ┆ ••••hello••••hel
3456c 6f ff ff ff ff 68 65 6c 6c 6f 20 77 6f 72 6c ┆ lo••••hello worl
34664 ff ff ff ff 68 65 6c 6c 6f 20 77 6f 72 6c 64 ┆ d••••hello world
347ff ff ff ff 68 65 6c 6c 6f 20 77 6f 72 6c 64 ff ┆ ••••hello world•
348ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c 6c ┆ •••hello••••hell
3496f ff ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 ┆ o••••hello••••he
3506c 6c 6f ff ff ff ff 68 65 6c 6c 6f ff ff ff ff ┆ llo••••hello••••
35168 65 6c 6c 6f ff ff ff ff 68 65 6c 6c 6f ff ff ┆ hello••••hello••
352ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c 6c 6f ┆ ••hello••••hello
353ff ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c ┆ ••••hello••••hel
3546c 6f ff ff ff ff 68 65 6c 6c 6f ┆ lo••••hello
355----
356
71aaa3f7
PP
357Precise error reporting::
358+
359----
360/tmp/meow.normand:10:24 - Expecting a bit (`0` or `1`).
361----
362+
363----
364/tmp/meow.normand:32:6 - Unexpected character `k`.
365----
366+
367----
320644e2 368/tmp/meow.normand:24:19 - Illegal (unknown or unreachable) variable/label name `meow` in expression `(meow - 45) // 8`; the legal names are {`ICITTE`, `mix`, `zoom`}.
71aaa3f7
PP
369----
370+
371----
f5dcb24c
PP
372/tmp/meow.normand:32:19 - While expanding the macro `meow`:
373/tmp/meow.normand:35:5 - While expanding the macro `zzz`:
320644e2 374/tmp/meow.normand:18:9 - Value 315 is outside the 8-bit range when evaluating expression `end - ICITTE`.
71aaa3f7
PP
375----
376
377You can use Normand to track data source files in your favorite VCS
378instead of raw binary files. The binary files that Normand generates can
379be used to test file format decoding, including malformatted data, for
380example, as well as for education.
381
382See <<learn-normand>> to explore all the Normand features.
383
384== Install Normand
385
386Normand requires Python ≥ 3.4.
387
388To install Normand:
389
390----
391$ python3 -m pip install --user normand
392----
393
394See
395https://packaging.python.org/en/latest/tutorials/installing-packages/#installing-to-the-user-site[Installing to the User Site]
396to learn more about a user site installation.
397
398[NOTE]
399====
400Normand has a single module file, `normand.py`, which you can copy as is
af3cf417 401to your project to use it (both the <<python3-api,`normand.parse()`>>
71aaa3f7
PP
402function and the <<command-line-tool,command-line tool>>).
403
404`normand.py` has _no external dependencies_, but if you're using
405Python{nbsp}3.4, you'll need a local copy of the standard `typing`
406module.
407====
408
43937a34
PP
409== Design goals
410
411The design goals of Normand are:
412
413Portability::
414 We're making sure `normand.py` works with Python{nbsp}≥{nbsp}3.4 and
415 doesn't have any external dependencies so that you may just copy the
416 module as is to your own project.
417
418Ease of use::
419 The most basic Normand input is a sequence of hexadecimal constants
420 (for example, `4e6f726d616e64`) which produce exactly what you'd
421 expect.
422+
423Most Normand features map to programming language concepts you already
424know and understand: constant integers, literal strings, variables,
425conditionals, repetitions/loops, and the rest.
426
427Concise and readable input::
428 We could have chosen XML or YAML as the input format, but having a
429 DSL here makes a Normand input compact and easy to read, two
430 important traits when using Normand to write tests, for example.
431+
432Compare the following Normand input and some hypothetical XML
433equivalent, for example:
434+
435.Actual normand input.
436----
437ff dd 01 ab $192 $-128 %1101:0011
438
ee724c95 439[end:8]
43937a34
PP
440
441{iter = 1}
442
443!if {not something}
444 # five times because xyz
445 !repeat 5
ee724c95 446 "hello world " [iter:8]
43937a34
PP
447 {iter = iter + 1}
448 !end
449!end
450
451<end>
452----
453+
454.Hypothetical Normand XML input.
455[source,xml]
456----
457<?xml version="1.0" encoding="utf-8" ?>
458<group>
459 <byte base="x" val="ff" />
460 <byte base="x" val="dd" />
461 <byte base="x" val="1" />
462 <byte base="x" val="ab" />
463 <byte base="d" val="192" />
464 <byte base="d" val="-128" />
465 <byte base="b" val="11010011" />
466 <fixed-len-num expr="end" len="8" />
467 <var-assign name="iter" expr="1" />
468 <cond expr="not something">
469 <!-- five times because xyz -->
470 <repeat expr="5">
471 <str>hello world </str>
472 <fixed-len-num expr="iter" len="8" />
473 <var-assign name="iter" expr="iter + 1" />
474 </repeat>
475 </cond>
476 <label name="end" />
477</group>
478----
479
71aaa3f7
PP
480== Learn Normand
481
482A Normand text input is a sequence of items which represent a sequence
483of raw bytes.
484
485[[state]] During the processing of items to data, Normand relies on a
486current state:
487
488[%header%autowidth]
489|===
af3cf417 490|State variable |Description |Initial value: <<python3-api,{py3} API>> |Initial value: <<command-line-tool,CLI>>
71aaa3f7
PP
491
492|[[cur-offset]] Current offset
493|
05f81895 494The current offset has an effect on the value of <<label,labels>> and of
269f6eb3 495the special `ICITTE` name in <<fixed-length-number,fixed-length
95e254bd 496number>>, <<leb128-integer,LEB128 integer>>, <<string,string>>,
f63f4a5d 497<<filling,filling>>, <<variable-assignment,variable assignment>>,
27d52a19 498<<conditional-block,conditional block>>, <<repetition-block,repetition
320644e2
PP
499block>>, <<macro-expansion,macro expansion>>, and
500<<post-item-repetition,post-item repetition>> expression evaluation.
71aaa3f7
PP
501
502Each generated byte increments the current offset.
503
504A <<current-offset-setting,current offset setting>> may change the
676f6189
PP
505current offset without generating data.
506
507An <<current-offset-alignment,current offset alignment>> generates
508padding bytes to make the current offset satisfy a given alignment.
71aaa3f7
PP
509|`init_offset` parameter of the `parse()` function.
510|`--offset` option.
511
512|[[cur-bo]] Current byte order
513|
56996d34 514The current byte order can have an effect on the encoding of
269f6eb3 515<<fixed-length-number,fixed-length numbers>>.
71aaa3f7
PP
516
517A <<current-byte-order-setting,current byte order setting>> may change
518the current byte order.
519|`init_byte_order` parameter of the `parse()` function.
520|`--byte-order` option.
521
522|<<label,Labels>>
523|Mapping of label names to integral values.
524|`init_labels` parameter of the `parse()` function.
525|One or more `--label` options.
526
527|<<variable-assignment,Variables>>
27d52a19 528|Mapping of variable names to integral or floating point number values.
71aaa3f7 529|`init_variables` parameter of the `parse()` function.
7a7b31e8 530|One or more `--var` or `--var-str` options.
71aaa3f7
PP
531|===
532
533The available items are:
534
6dd69a2a
PP
535* A <<byte-constant,constant integer>> representing one or more
536 constant bytes.
71aaa3f7 537
7a7b31e8
PP
538* A <<literal-string,literal string>> representing a constant sequence
539 of bytes encoding UTF-8, UTF-16, UTF-32, or Latin-1 to Latin-10 data.
71aaa3f7
PP
540
541* A <<current-byte-order-setting,current byte order setting>> (big or
542 little endian).
543
269f6eb3 544* A <<fixed-length-number,fixed-length number>> (integer or
56996d34
PP
545 floating point), possibly using the <<cur-bo,current byte order>>, and
546 of which the value is the result of a {py3} expression.
05f81895
PP
547
548* An <<leb128-integer,LEB128 integer>> of which the value is the result
549 of a {py3} expression.
71aaa3f7 550
7a7b31e8
PP
551* A <<string,string>> representing a sequence of bytes encoding UTF-8,
552 UTF-16, UTF-32, or Latin-1 to Latin-10 data, and of which the value is
553 the result of a {py3} expression.
554
71aaa3f7
PP
555* A <<current-offset-setting,current offset setting>>.
556
676f6189
PP
557* A <<current-offset-alignment,current offset alignment>>.
558
25ca454b
PP
559* A <<filling,filling>>.
560
71aaa3f7
PP
561* A <<label,label>>, that is, a named constant holding the current
562 offset.
563+
564This is similar to an assembly label.
565
566* A <<variable-assignment,variable assignment>> associating a name to
567 the integral result of an evaluated {py3} expression.
568
569* A <<group,group>>, that is, a scoped sequence of items.
570
27d52a19
PP
571* A <<conditional-block,conditional block>>.
572
e57a18e1
PP
573* A <<repetition-block,repetition block>>.
574
cd33dfe6
PP
575* A <<transformation-block,transformation block>>.
576
320644e2
PP
577* A <<macro-definition-block,macro definition block>>.
578
579* A <<macro-expansion,macro expansion>>.
580
e57a18e1
PP
581Moreover, you can repeat many items above a constant or variable number
582of times with the ``pass:[*]`` operator _after_ the item to repeat. This
583is called a <<post-item-repetition,post-item repetition>>.
71aaa3f7 584
ba11fb1d 585A Normand comment may exist pretty much anywhere between tokens.
71aaa3f7
PP
586
587A comment is anything between two ``pass:[#]`` characters on the same
ba11fb1d
PP
588line, or from ``pass:[#]`` until the end of the line. Whitespaces are
589also considered comments. The following symbols are also considered
590comments around and between items, as well as between hexadecimal
591nibbles and binary bits of <<byte-constant,byte constants>>:
71aaa3f7
PP
592
593----
ee724c95 594& , - . / : ; = ? \ _ |
71aaa3f7
PP
595----
596
597The latter serve to improve readability so that you may write, for
598example, a MAC address or a UUID as is.
599
fc21bb27
PP
600[[const-int]] Many items require a _constant integer_, possibly
601negative, in which case it may start with `-` for a negative integer. A
602positive constant integer is any of:
603
604Decimal::
605 One or mode digits (`0` to `9`).
606
607Hexadecimal::
608 One of:
609+
610* The `0x` or `0X` prefix followed with one or more hexadecimal digits
611 (`0` to `9`, `a` to `f`, or `A` to `F`).
612* One or more hexadecimal digits followed with the `h` or `H` suffix.
613
614Octal::
615 One of:
616+
617* The `0o` or `0O` prefix followed with one or more octal digits
618 (`0` to `7`).
619* One or more octal digits followed with the `o`, `O`, `q`, or `Q`
620 suffix.
621
622Binary::
623 One of:
624+
625* The `0b` or `0B` prefix followed with one or more bits (`0` or `1`).
626* One or more bits followed with the `b` or `B` suffix.
627
ee724c95
PP
628In general, anything between `pass:[{]` and `}` is a {py3} expression.
629
71aaa3f7
PP
630You can test the examples of this section with the `normand`
631<<command-line-tool,command-line tool>> as such:
632
633----
634$ normand file | hexdump -C
635----
636
637where `file` is the name of a file containing the Normand input.
638
639=== Byte constant
640
6dd69a2a 641A _byte constant_ represents one or more constant bytes.
71aaa3f7
PP
642
643A byte constant is:
644
645Hexadecimal form::
6dd69a2a 646 Two consecutive hexadecimal digits representing a single byte.
71aaa3f7
PP
647
648Decimal form::
6dd69a2a 649 One or more digits after the `$` prefix representing a single byte.
71aaa3f7 650
6dd69a2a
PP
651Binary form:: {empty}
652+
653--
654. __**N**__ `%` prefixes (at least one).
655+
656The number of `%` characters is the number of subsequent expected bytes.
657
658. __**N**__{nbsp}×{nbsp}8 bits (`0` or `1`).
659--
71aaa3f7
PP
660
661====
662Input:
663
664----
ee724c95 665ab cd (3d 8F) CC
71aaa3f7
PP
666----
667
668Output:
669
670----
671ab cd 3d 8f cc
672----
673====
674
675====
676Input:
677
678----
679$192 %1100/0011 $ -77
680----
681
682Output:
683
684----
685c0 c3 b3
686----
687====
688
689====
690Input:
691
692----
69358f64689-6316-4d55-8a1a-04cada366172
694fe80::6257:18ff:fea3:4229
695----
696
697Output:
698
699----
70058 f6 46 89 63 16 4d 55 8a 1a 04 ca da 36 61 72 ┆ X•F•c•MU•••••6ar
701fe 80 62 57 18 ff fe a3 42 29 ┆ ••bW••••B)
702----
703====
704
705====
706Input:
707
708----
709%01110011 %01100001 %01101100 %01110101 %01110100
6dd69a2a 710%%%1101:0010 11111111 #A#11 #B#00 #C#011 #D#1
71aaa3f7
PP
711----
712
713Output:
714
715----
6dd69a2a 71673 61 6c 75 74 d2 ff c7 ┆ salut•••
71aaa3f7
PP
717----
718====
719
720=== Literal string
721
7a7b31e8
PP
722A _literal string_ represents the encoded bytes of a literal string
723using the UTF-8, UTF-16, UTF-32, or Latin-1 to Latin-10 encoding.
71aaa3f7
PP
724
725The string to encode isn't implicitly null-terminated: use `\0` at the
726end of the string to add a null character.
727
728A literal string is:
729
7a7b31e8
PP
730. **Optional**: one of the following encodings instead of the default
731 UTF-8:
71aaa3f7
PP
732+
733--
734[horizontal]
7a7b31e8
PP
735`s:u8`::
736`u8`::
737 UTF-8.
738
739`s:u16be`::
740`u16be`::
741 UTF-16BE.
742
743`s:u16le`::
744`u16le`::
745 UTF-16LE.
746
747`s:u32be`::
748`u32be`::
749 UTF-32BE.
750
751`s:u32le`::
752`u32le`::
753 UTF-32LE.
754
755`s:latin1`::
756 ISO/IEC 8859-1.
757
758`s:latin2`::
759 ISO/IEC 8859-2.
760
761`s:latin3`::
762 ISO/IEC 8859-3.
763
764`s:latin4`::
765 ISO/IEC 8859-4.
766
767`s:latin5`::
768 ISO/IEC 8859-9.
769
770`s:latin6`::
771 ISO/IEC 8859-10.
772
773`s:latin7`::
774 ISO/IEC 8859-13.
775
776`s:latin8`::
777 ISO/IEC 8859-14.
778
779`s:latin9`::
780 ISO/IEC 8859-15.
781
782`s:latin10`::
783 ISO/IEC 8859-16.
71aaa3f7
PP
784--
785
786. The ``pass:["]`` prefix.
787
788. A sequence of zero or more characters, possibly containing escape
789 sequences.
790+
791An escape sequence is the ``\`` character followed by one of:
792+
793--
794[horizontal]
795`0`:: Null (U+0000)
796`a`:: Alert (U+0007)
797`b`:: Backspace (U+0008)
798`e`:: Escape (U+001B)
799`f`:: Form feed (U+000C)
800`n`:: End of line (U+000A)
801`r`:: Carriage return (U+000D)
802`t`:: Character tabulation (U+0009)
803`v`:: Line tabulation (U+000B)
804``\``:: Reverse solidus (U+005C)
805``pass:["]``:: Quotation mark (U+0022)
806--
807
808. The ``pass:["]`` suffix.
809
810====
811Input:
812
813----
814"coucou tout le monde!"
815----
816
817Output:
818
819----
82063 6f 75 63 6f 75 20 74 6f 75 74 20 6c 65 20 6d ┆ coucou tout le m
8216f 6e 64 65 21 ┆ onde!
822----
823====
824
825====
826Input:
827
828----
829u16le"I am not young enough to know everything."
830----
831
832Output:
833
834----
83549 00 20 00 61 00 6d 00 20 00 6e 00 6f 00 74 00 ┆ I• •a•m• •n•o•t•
83620 00 79 00 6f 00 75 00 6e 00 67 00 20 00 65 00 ┆ •y•o•u•n•g• •e•
8376e 00 6f 00 75 00 67 00 68 00 20 00 74 00 6f 00 ┆ n•o•u•g•h• •t•o•
83820 00 6b 00 6e 00 6f 00 77 00 20 00 65 00 76 00 ┆ •k•n•o•w• •e•v•
83965 00 72 00 79 00 74 00 68 00 69 00 6e 00 67 00 ┆ e•r•y•t•h•i•n•g•
8402e 00 ┆ .•
841----
842====
843
844====
845Input:
846
847----
7a7b31e8 848s:u32be "\"illusion is the first\nof all pleasures\" 🦉"
71aaa3f7
PP
849----
850
851Output:
852
853----
85400 00 00 22 00 00 00 69 00 00 00 6c 00 00 00 6c ┆ •••"•••i•••l•••l
85500 00 00 75 00 00 00 73 00 00 00 69 00 00 00 6f ┆ •••u•••s•••i•••o
85600 00 00 6e 00 00 00 20 00 00 00 69 00 00 00 73 ┆ •••n••• •••i•••s
85700 00 00 20 00 00 00 74 00 00 00 68 00 00 00 65 ┆ ••• •••t•••h•••e
85800 00 00 20 00 00 00 66 00 00 00 69 00 00 00 72 ┆ ••• •••f•••i•••r
85900 00 00 73 00 00 00 74 00 00 00 0a 00 00 00 6f ┆ •••s•••t•••••••o
86000 00 00 66 00 00 00 20 00 00 00 61 00 00 00 6c ┆ •••f••• •••a•••l
86100 00 00 6c 00 00 00 20 00 00 00 70 00 00 00 6c ┆ •••l••• •••p•••l
86200 00 00 65 00 00 00 61 00 00 00 73 00 00 00 75 ┆ •••e•••a•••s•••u
86300 00 00 72 00 00 00 65 00 00 00 73 00 00 00 22 ┆ •••r•••e•••s•••"
86400 00 00 20 00 01 f9 89 ┆ ••• ••••
865----
866====
867
7a7b31e8
PP
868====
869Input:
870
871----
872s:latin1 "Paul Piché"
873----
874
875Output:
876
877----
87850 61 75 6c 20 50 69 63 68 e9 ┆ Paul Pich•
879----
880====
881
71aaa3f7
PP
882=== Current byte order setting
883
884This special item sets the <<cur-bo,_current byte order_>>.
885
886The two accepted forms are:
887
888[horizontal]
ee724c95
PP
889`!be`:: Set the current byte order to big endian.
890`!le`:: Set the current byte order to little endian.
71aaa3f7 891
269f6eb3 892=== Fixed-length number
71aaa3f7 893
269f6eb3
PP
894A _fixed-length number_ represents a fixed number of bytes encoding
895either:
896
897* An unsigned or signed integer (two's complement).
898+
899The available lengths are 8, 16, 24, 32, 40, 48, 56, and 64.
900
901* A floating point number
b87a3aa2 902 (https://standards.ieee.org/standard/754-2008.html[IEEE{nbsp}754-2008]).
269f6eb3 903+
9403947b 904The available lengths are 32 (_binary32_) and 64 (_binary64_).
71aaa3f7 905
56996d34
PP
906The value is the result of evaluating a {py3} expression.
907
908The byte order to use to encode the value is either directly specified
909or is the <<cur-bo,current byte order>>.
269f6eb3
PP
910
911A fixed-length number is:
71aaa3f7 912
ee724c95 913. The `[` prefix.
71aaa3f7
PP
914
915. A valid {py3} expression.
05f81895 916+
269f6eb3 917For a fixed-length number at some source location{nbsp}__**L**__, this
05f81895
PP
918expression may contain the name of any accessible <<label,label>> (not
919within a nested group), including the name of a label defined
6740649d
PP
920after{nbsp}__**L**__ (except within a
921<<transformation-block,transformation block>>), as well as the name of
922any <<variable-assignment,variable>> known at{nbsp}__**L**__.
05f81895 923+
269f6eb3
PP
924The value of the special name `ICITTE` (`int` type) in this expression
925is the <<cur-offset,current offset>> (before encoding the number).
71aaa3f7
PP
926
927. The `:` character.
928
269f6eb3
PP
929. An encoding length in bits amongst:
930+
931--
27d52a19 932The expression evaluates to an `int` or `bool` value::
269f6eb3 933 `8`, `16`, `24`, `32`, `40`, `48`, `56`, and `64`.
27d52a19
PP
934+
935NOTE: Normand automatically converts a `bool` value to `int`.
269f6eb3
PP
936
937The expression evaluates to a `float` value::
938 `32` and `64`.
939--
71aaa3f7 940
56996d34
PP
941. **Optional**: a suffix of the previous encoding length, without
942 any whitespace, amongst:
943+
944--
945[horizontal]
946`be`:: Encode in big endian.
947`le`:: Encode in little endian.
948--
949+
950Without this suffix, the encoding byte order is the <<cur-bo,current
951byte order>> which must be defined if the encoding length is greater
952than eight.
953
ee724c95 954. The `]` suffix.
71aaa3f7
PP
955
956====
957Input:
958
959----
56996d34
PP
960[345:16le]
961[-0xabcd:32be]
71aaa3f7
PP
962----
963
964Output:
965
966----
96759 01 ff ff 54 33
968----
969====
970
971====
972Input:
973
974----
ee724c95 975!be
71aaa3f7
PP
976
977# String length in bits
ee724c95 978[8 * (str_end - str_beg) : 16]
71aaa3f7
PP
979
980# String
981<str_beg>
982 "hello world!"
983<str_end>
984----
985
986Output:
987
988----
98900 60 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 ┆ •`hello world!
990----
991====
992
993====
994Input:
995
996----
ee724c95 997[20 - ICITTE : 8] * 10
71aaa3f7
PP
998----
999
1000Output:
1001
1002----
100314 13 12 11 10 0f 0e 0d 0c 0b
1004----
1005====
1006
269f6eb3
PP
1007====
1008Input:
1009
1010----
56996d34 1011[2 * 0.0529 : 32le]
269f6eb3
PP
1012----
1013
1014Output:
1015
1016----
1017ac ad d8 3d
1018----
1019====
1020
05f81895
PP
1021=== LEB128 integer
1022
1023An _LEB128 integer_ represents a variable number of bytes encoding an
1024unsigned or signed integer which is the result of evaluating a {py3}
1025expression following the https://en.wikipedia.org/wiki/LEB128[LEB128]
1026format.
1027
1028An LEB128 integer is:
1029
ee724c95 1030. The `[` prefix.
05f81895 1031
27d52a19
PP
1032. A valid {py3} expression of which the evaluation result type
1033 is `int` or `bool` (automatically converted to `int`).
05f81895
PP
1034+
1035For an LEB128 integer at some source location{nbsp}__**L**__, this
1036expression may contain:
1037+
1038--
fc21bb27
PP
1039* The name of any <<label,label>> defined before{nbsp}__**L**__
1040 which isn't within a nested group.
320644e2
PP
1041* The name of any <<variable-assignment,variable>> known
1042 at{nbsp}__**L**__.
05f81895
PP
1043--
1044+
269f6eb3
PP
1045The value of the special name `ICITTE` (`int` type) in this expression
1046is the <<cur-offset,current offset>> (before encoding the integer).
05f81895
PP
1047
1048. The `:` character.
1049
1050. One of:
1051+
1052--
1053[horizontal]
1054`uleb128`:: Use the unsigned LEB128 format.
1055`sleb128`:: Use the signed LEB128 format.
1056--
1057
ee724c95 1058. The `]` suffix.
05f81895
PP
1059
1060====
1061Input:
1062
1063----
ee724c95 1064[624485 : uleb128]
05f81895
PP
1065----
1066
1067Output:
1068
1069----
1070e5 8e 26
1071----
1072====
1073
1074====
1075Input:
1076
1077----
1078aa bb cc dd
1079<meow>
1080ee ff
ee724c95 1081[-981238311 + (meow * -23) : sleb128]
05f81895
PP
1082"hello"
1083----
1084
c2b79cf6
PP
1085Output:
1086
05f81895
PP
1087----
1088aa bb cc dd ee ff fd fa 8d ac 7c 68 65 6c 6c 6f ┆ ••••••••••|hello
1089----
1090====
1091
7a7b31e8
PP
1092=== String
1093
1094A _string_ represents a variable number of bytes encoding a string which
1095is the result of evaluating a {py3} expression using the UTF-8, UTF-16,
1096UTF-32, or Latin-1 to Latin-10 encoding.
1097
1098A string has two possible forms:
1099
1100Encoding prefix form:: {empty}
1101+
1102. An encoding amongst:
1103+
1104--
1105[horizontal]
1106`s:u8`::
1107`u8`::
1108 UTF-8.
1109
1110`s:u16be`::
1111`u16be`::
1112 UTF-16BE.
1113
1114`s:u16le`::
1115`u16le`::
1116 UTF-16LE.
1117
1118`s:u32be`::
1119`u32be`::
1120 UTF-32BE.
1121
1122`s:u32le`::
1123`u32le`::
1124 UTF-32LE.
1125
1126`s:latin1`::
1127 ISO/IEC 8859-1.
1128
1129`s:latin2`::
1130 ISO/IEC 8859-2.
1131
1132`s:latin3`::
1133 ISO/IEC 8859-3.
1134
1135`s:latin4`::
1136 ISO/IEC 8859-4.
1137
1138`s:latin5`::
1139 ISO/IEC 8859-9.
1140
1141`s:latin6`::
1142 ISO/IEC 8859-10.
1143
1144`s:latin7`::
1145 ISO/IEC 8859-13.
1146
1147`s:latin8`::
1148 ISO/IEC 8859-14.
1149
1150`s:latin9`::
1151 ISO/IEC 8859-15.
1152
1153`s:latin10`::
1154 ISO/IEC 8859-16.
1155--
1156
1157. The ``pass:[{]`` prefix.
1158
1159. A valid {py3} expression of which the evaluation result type
1160 is `bool`, `int`, `float`, or `str` (the first three automatically
1161 converted to `str`).
1162+
1163For a string at some source location{nbsp}__**L**__, this expression may
1164contain:
1165+
1166--
1167* The name of any <<label,label>> defined before{nbsp}__**L**__
1168 which isn't within a nested group.
1169* The name of any <<variable-assignment,variable>> known
1170 at{nbsp}__**L**__.
1171--
1172+
1173The value of the special name `ICITTE` (`int` type) in this expression
1174is the <<cur-offset,current offset>> (before encoding the string).
1175
1176. The `}` suffix.
1177
1178Encoding suffix form:: {empty}
1179+
ee724c95 1180. The `[` prefix.
7a7b31e8
PP
1181
1182. A valid {py3} expression of which the evaluation result type
1183 is `bool`, `int`, `float`, or `str` (the first three automatically
1184 converted to `str`).
1185+
1186For a string at some source location{nbsp}__**L**__, this expression may
1187contain:
1188+
1189--
1190* The name of any <<label,label>> defined before{nbsp}__**L**__
1191 which isn't within a nested group.
1192* The name of any <<variable-assignment,variable>> known
1193 at{nbsp}__**L**__.
1194--
1195+
1196The value of the special name `ICITTE` (`int` type) in this expression
1197is the <<cur-offset,current offset>> (before encoding the string).
1198
1199. The `:` character.
1200
1201. A string encoding amongst:
1202+
1203--
1204[horizontal]
1205`s:u8`::
1206 UTF-8.
1207
1208`s:u16be`::
1209 UTF-16BE.
1210
1211`s:u16le`::
1212 UTF-16LE.
1213
1214`s:u32be`::
1215 UTF-32BE.
1216
1217`s:u32le`::
1218 UTF-32LE.
1219
1220`s:latin1`::
1221 ISO/IEC 8859-1.
1222
1223`s:latin2`::
1224 ISO/IEC 8859-2.
1225
1226`s:latin3`::
1227 ISO/IEC 8859-3.
1228
1229`s:latin4`::
1230 ISO/IEC 8859-4.
1231
1232`s:latin5`::
1233 ISO/IEC 8859-9.
1234
1235`s:latin6`::
1236 ISO/IEC 8859-10.
1237
1238`s:latin7`::
1239 ISO/IEC 8859-13.
1240
1241`s:latin8`::
1242 ISO/IEC 8859-14.
1243
1244`s:latin9`::
1245 ISO/IEC 8859-15.
1246
1247`s:latin10`::
1248 ISO/IEC 8859-16.
1249--
1250
ee724c95 1251. The `]` suffix.
7a7b31e8
PP
1252
1253====
1254Input:
1255
1256----
1257{iter = 1}
1258
1259!repeat 10
ee724c95 1260 u8{iter} " "
7a7b31e8
PP
1261 {iter = iter + 1}
1262!end
1263----
1264
1265Output:
1266
1267----
126831 20 32 20 33 20 34 20 35 20 36 20 37 20 38 20 ┆ 1 2 3 4 5 6 7 8
126939 20 31 30 20 ┆ 9 10
1270----
1271====
1272
1273====
1274Input:
1275
1276----
1277{meow = 'salut jérémie'}
ee724c95 1278[meow.upper() : s:latin1]
7a7b31e8
PP
1279----
1280
1281Output:
1282
1283----
128453 41 4c 55 54 20 4a c9 52 c9 4d 49 45 ┆ SALUT J•R•MIE
1285----
1286====
1287
71aaa3f7
PP
1288=== Current offset setting
1289
1290This special item sets the <<cur-offset,_current offset_>>.
1291
1292A current offset setting is:
1293
1294. The `<` prefix.
1295
fc21bb27
PP
1296. A <<const-int,positive constant integer>> which is the new current
1297 offset.
71aaa3f7
PP
1298
1299. The `>` suffix.
1300
1301====
1302Input:
1303
1304----
ee724c95
PP
1305 [ICITTE : 8] * 8
1306<0x61> [ICITTE : 8] * 8
71aaa3f7
PP
1307----
1308
1309Output:
1310
1311----
131200 01 02 03 04 05 06 07 61 62 63 64 65 66 67 68 ┆ ••••••••abcdefgh
1313----
1314====
1315
1316====
1317Input:
1318
1319----
1320aa bb cc dd <meow> ee ff
1321<12> 11 22 33 <mix> 44 55
ee724c95 1322[meow : 8] [mix : 8]
71aaa3f7
PP
1323----
1324
1325Output:
1326
1327----
1328aa bb cc dd ee ff 11 22 33 44 55 04 0f ┆ •••••••"3DU••
1329----
1330====
1331
676f6189
PP
1332=== Current offset alignment
1333
00deb9fa 1334A _current offset alignment_ represents zero or more padding bytes to
676f6189
PP
1335make the <<cur-offset,current offset>> meet a given
1336https://en.wikipedia.org/wiki/Data_structure_alignment[alignment] value.
1337
1338More specifically, for an alignment value of{nbsp}__**N**__{nbsp}bits,
1339a current offset alignment represents the required padding bytes until
1340the current offset is a multiple of __**N**__{nbsp}/{nbsp}8.
1341
1342A current offset alignment is:
1343
1344. The `@` prefix.
1345
fc21bb27
PP
1346. A <<const-int,positive constant integer>> which is the alignment value
1347 in _bits_.
676f6189
PP
1348+
1349This value must be greater than zero and a multiple of{nbsp}8.
1350
1351. **Optional**:
1352+
1353--
1354. The ``pass:[~]`` prefix.
fc21bb27
PP
1355. A <<const-int,positive constant integer>> which is the value of the
1356 byte to use as padding to align the <<cur-offset,current offset>>.
676f6189
PP
1357--
1358+
1359Without this section, the padding byte value is zero.
1360
1361====
1362Input:
1363
1364----
136511 22 (@32 aa bb cc) * 3
1366----
1367
1368Output:
1369
1370----
137111 22 00 00 aa bb cc 00 aa bb cc 00 aa bb cc
1372----
1373====
1374
1375====
1376Input:
1377
1378----
ee724c95 1379!le
676f6189 138077 88
ee724c95 1381@32~0xcc [-893.5:32]
676f6189
PP
1382@128~0x55 "meow"
1383----
1384
1385Output:
1386
1387----
138877 88 cc cc 00 60 5f c4 55 55 55 55 55 55 55 55 ┆ w••••`_•UUUUUUUU
13896d 65 6f 77 ┆ meow
1390----
1391====
1392
1393====
1394Input:
1395
1396----
1397aa bb cc <29> @64~255 "zoom"
1398----
1399
1400Output:
1401
1402----
1403aa bb cc ff ff ff 7a 6f 6f 6d ┆ ••••••zoom
1404----
1405====
1406
25ca454b
PP
1407=== Filling
1408
1409A _filling_ represents zero or more padding bytes to make the
1410<<cur-offset,current offset>> reach a given value.
1411
1412A filling is:
1413
1414. The ``pass:[+]`` prefix.
1415
1416. One of:
1417
fc21bb27
PP
1418** A <<const-int,positive constant integer>> which is the current offset
1419 target.
25ca454b
PP
1420
1421** The ``pass:[{]`` prefix, a valid {py3} expression of which the
1422 evaluation result type is `int` or `bool` (automatically converted to
ee724c95 1423 `int`), and the `}` suffix.
25ca454b
PP
1424+
1425For a filling at some source location{nbsp}__**L**__, this expression
1426may contain:
1427+
1428--
1429* The name of any <<label,label>> defined before{nbsp}__**L**__
1430 which isn't within a nested group.
1431* The name of any <<variable-assignment,variable>> known
1432 at{nbsp}__**L**__.
1433--
1434+
1435The value of the special name `ICITTE` (`int` type) in this expression
1436is the <<cur-offset,current offset>> (before handling the items to
1437repeat).
1438
1439** A valid {py3} name.
1440+
1441For the name `__NAME__`, this is equivalent to the
ee724c95 1442`pass:[{]__NAME__}` form above.
25ca454b
PP
1443
1444+
1445This value must be greater than or equal to the current offset where
1446it's used.
1447
1448. **Optional**:
1449+
1450--
1451. The ``pass:[~]`` prefix.
fc21bb27
PP
1452. A <<const-int,positive constant integer>> which is the value of the
1453 byte to use as padding to reach the current offset target.
25ca454b
PP
1454--
1455+
1456Without this section, the padding byte value is zero.
1457
1458====
1459Input:
1460
1461----
1462aa bb cc dd
1463+0x40
1464"hello world"
1465----
1466
1467Output:
1468
1469----
1470aa bb cc dd 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
147100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
147200 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
147300 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
147468 65 6c 6c 6f 20 77 6f 72 6c 64 ┆ hello world
1475----
1476====
1477
1478====
1479Input:
1480
1481----
1482!macro part(iter, fill)
ee724c95 1483 <0> "particular security " [ord('0') + iter : 8] +fill~0x80
25ca454b
PP
1484!end
1485
1486{iter = 1}
1487
1488!repeat 5
1489 m:part(iter, {32 + 4 * iter})
1490 {iter = iter + 1}
1491!end
1492----
1493
1494Output:
1495
1496----
149770 61 72 74 69 63 75 6c 61 72 20 73 65 63 75 72 ┆ particular secur
149869 74 79 20 31 80 80 80 80 80 80 80 80 80 80 80 ┆ ity 1•••••••••••
149980 80 80 80 70 61 72 74 69 63 75 6c 61 72 20 73 ┆ ••••particular s
150065 63 75 72 69 74 79 20 32 80 80 80 80 80 80 80 ┆ ecurity 2•••••••
150180 80 80 80 80 80 80 80 80 80 80 80 70 61 72 74 ┆ ••••••••••••part
150269 63 75 6c 61 72 20 73 65 63 75 72 69 74 79 20 ┆ icular security
150333 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 ┆ 3•••••••••••••••
150480 80 80 80 80 80 80 80 70 61 72 74 69 63 75 6c ┆ ••••••••particul
150561 72 20 73 65 63 75 72 69 74 79 20 34 80 80 80 ┆ ar security 4•••
150680 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 ┆ ••••••••••••••••
150780 80 80 80 80 80 80 80 70 61 72 74 69 63 75 6c ┆ ••••••••particul
150861 72 20 73 65 63 75 72 69 74 79 20 35 80 80 80 ┆ ar security 5•••
150980 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 ┆ ••••••••••••••••
151080 80 80 80 80 80 80 80 80 80 80 80 ┆ ••••••••••••
1511----
1512====
1513
71aaa3f7
PP
1514=== Label
1515
1516A _label_ associates a name to the <<cur-offset,current offset>>.
1517
1518All the labels of a whole Normand input must have unique names.
1519
05f81895 1520A label must not share the name of a <<variable-assignment,variable>>
71aaa3f7
PP
1521name.
1522
71aaa3f7
PP
1523A label is:
1524
1525. The `<` prefix.
1526
27d52a19 1527. A valid {py3} name which is not `ICITTE`.
71aaa3f7
PP
1528
1529. The `>` suffix.
1530
1531=== Variable assignment
1532
1533A _variable assignment_ associates a name to the integral result of an
1534evaluated {py3} expression.
1535
05f81895 1536A variable assignment is:
71aaa3f7
PP
1537
1538. The ``pass:[{]`` prefix.
1539
27d52a19 1540. A valid {py3} name which is not `ICITTE`.
71aaa3f7
PP
1541
1542. The `=` character.
1543
7a7b31e8
PP
1544. A valid {py3} expression of which the evaluation result type is `int`,
1545 `float`, or `bool` (automatically converted to `int`), or `str`.
05f81895
PP
1546+
1547For a variable assignment at some source location{nbsp}__**L**__, this
320644e2
PP
1548expression may contain:
1549+
1550--
1551* The name of any <<label,label>> defined before{nbsp}__**L**__
1552 which isn't within a nested group.
1553* The name of any <<variable-assignment,variable>> known
1554 at{nbsp}__**L**__.
1555--
05f81895 1556+
269f6eb3
PP
1557The value of the special name `ICITTE` (`int` type) in this expression
1558is the <<cur-offset,current offset>>.
71aaa3f7
PP
1559
1560. The `}` suffix.
1561
1562====
1563Input:
1564
1565----
ee724c95
PP
1566{mix = 101} !le
1567{meow = 42} 11 22 [meow:8] 33 {meow = ICITTE + 17}
1568"yooo" [meow + mix : 16]
71aaa3f7
PP
1569----
1570
1571Output:
1572
1573----
157411 22 2a 33 79 6f 6f 6f 7a 00 ┆ •"*3yoooz•
1575----
1576====
1577
1578=== Group
1579
1580A _group_ is a scoped sequence of items.
1581
1582The <<label,labels>> within a group aren't visible outside of it.
1583
e57a18e1
PP
1584The main purpose of a group is to <<post-item-repetition,repeat>> more
1585than a single item and to isolate labels.
71aaa3f7
PP
1586
1587A group is:
1588
261c5ecf 1589. The `(`, `!group`, or `!g` opening.
71aaa3f7 1590
cd33dfe6 1591. Zero or more items except, recursively, a macro definition block.
71aaa3f7 1592
261c5ecf
PP
1593. Depending on the group opening:
1594+
1595--
1596`(`::
1597 The `)` closing.
1598
1599`!group`::
1600`!g`::
1601 The `!end` closing.
1602--
71aaa3f7
PP
1603
1604====
1605Input:
1606
1607----
1608((aa bb cc) dd () ee) "leclerc"
1609----
1610
1611Output:
1612
1613----
1614aa bb cc dd ee 6c 65 63 6c 65 72 63 ┆ •••••leclerc
1615----
1616====
1617
1618====
1619Input:
1620
1621----
261c5ecf
PP
1622!group
1623 (aa bb cc) * 3 dd ee
1624!end * 5
71aaa3f7
PP
1625----
1626
1627Output:
1628
1629----
1630aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa bb
1631cc aa bb cc dd ee aa bb cc aa bb cc aa bb cc dd
1632ee aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa
1633bb cc aa bb cc dd ee
1634----
1635====
1636
1637====
1638Input:
1639
1640----
ee724c95 1641!be
71aaa3f7
PP
1642(
1643 <str_beg> u16le"sébastien diaz" <str_end>
ee724c95
PP
1644 [ICITTE - str_beg : 8]
1645 [(end - str_beg) * 5 : 24]
71aaa3f7
PP
1646) * 3
1647<end>
1648----
1649
1650Output:
1651
1652----
165373 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e•
16546e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 e0 ┆ n• •d•i•a•z•••••
165573 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e•
16566e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 40 ┆ n• •d•i•a•z••••@
165773 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e•
16586e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 00 a0 ┆ n• •d•i•a•z•••••
1659----
1660====
1661
27d52a19
PP
1662=== Conditional block
1663
12b5dbc0
PP
1664A _conditional block_ represents either the bytes of zero or more items
1665if some expression is true, or the bytes of zero or more other items if
1666it's false.
27d52a19
PP
1667
1668A conditional block is:
1669
261c5ecf 1670. The `!if` opening.
27d52a19
PP
1671
1672. One of:
1673
1674** The ``pass:[{]`` prefix, a valid {py3} expression of which the
1675 evaluation result type is `int` or `bool` (automatically converted to
ee724c95 1676 `int`), and the `}` suffix.
27d52a19 1677+
320644e2
PP
1678For a conditional block at some source location{nbsp}__**L**__, this
1679expression may contain:
27d52a19
PP
1680+
1681--
1682* The name of any <<label,label>> defined before{nbsp}__**L**__
1683 which isn't within a nested group.
1684* The name of any <<variable-assignment,variable>> known
320644e2 1685 at{nbsp}__**L**__.
27d52a19
PP
1686--
1687+
1688The value of the special name `ICITTE` (`int` type) in this expression
1689is the <<cur-offset,current offset>> (before handling the contained
1690items).
1691
1692** A valid {py3} name.
1693+
1694For the name `__NAME__`, this is equivalent to the
ee724c95 1695`pass:[{]__NAME__}` form above.
27d52a19 1696
cd33dfe6
PP
1697. Zero or more items to be handled when the condition is true
1698 except, recursively, a macro definition block.
12b5dbc0
PP
1699
1700. **Optional**:
1701
1702.. The `!else` opening.
cd33dfe6
PP
1703.. Zero or more items to be handled when the condition is false
1704 except, recursively, a macro definition block
27d52a19 1705
261c5ecf 1706. The `!end` closing.
27d52a19
PP
1707
1708====
1709Input:
1710
1711----
1712{at = 1}
1713{rep_count = 9}
1714
1715!repeat rep_count
1716 "meow "
1717
1718 !if {ICITTE > 25}
1719 "mix"
12b5dbc0
PP
1720 !else
1721 "zoom"
27d52a19
PP
1722 !end
1723
12b5dbc0
PP
1724 !if {at < rep_count} 20 !end
1725
27d52a19
PP
1726 {at = at + 1}
1727!end
1728----
1729
1730Output:
1731
1732----
12b5dbc0
PP
17336d 65 6f 77 20 7a 6f 6f 6d 20 6d 65 6f 77 20 7a ┆ meow zoom meow z
17346f 6f 6d 20 6d 65 6f 77 20 7a 6f 6f 6d 20 6d 65 ┆ oom meow zoom me
17356f 77 20 6d 69 78 20 6d 65 6f 77 20 6d 69 78 20 ┆ ow mix meow mix
17366d 65 6f 77 20 6d 69 78 20 6d 65 6f 77 20 6d 69 ┆ meow mix meow mi
27d52a19 173778 20 6d 65 6f 77 20 6d 69 78 20 6d 65 6f 77 20 ┆ x meow mix meow
12b5dbc0 17386d 69 78 ┆ mix
27d52a19
PP
1739----
1740====
1741
1742====
1743Input:
1744
1745----
1746<str_beg>
1747u16le"meow mix!"
1748<str_end>
1749
1750!if {str_end - str_beg > 10}
1751 " BIG"
1752!end
1753----
1754
1755Output:
1756
1757----
17586d 00 65 00 6f 00 77 00 20 00 6d 00 69 00 78 00 ┆ m•e•o•w• •m•i•x•
175921 00 20 42 49 47 ┆ !• BIG
1760----
1761====
1762
e57a18e1 1763=== Repetition block
71aaa3f7 1764
e57a18e1
PP
1765A _repetition block_ represents the bytes of one or more items repeated
1766a given number of times.
676f6189 1767
e57a18e1 1768A repetition block is:
71aaa3f7 1769
261c5ecf 1770. The `!repeat` or `!r` opening.
71aaa3f7 1771
2adf4336
PP
1772. One of:
1773
fc21bb27
PP
1774** A <<const-int,positive constant integer>> which is the number of
1775 times to repeat the previous item.
2adf4336 1776
27d52a19
PP
1777** The ``pass:[{]`` prefix, a valid {py3} expression of which the
1778 evaluation result type is `int` or `bool` (automatically converted to
ee724c95 1779 `int`), and the `}` suffix.
05f81895 1780+
320644e2
PP
1781For a repetition block at some source location{nbsp}__**L**__, this
1782expression may contain:
05f81895
PP
1783+
1784--
27d52a19
PP
1785* The name of any <<label,label>> defined before{nbsp}__**L**__
1786 which isn't within a nested group.
05f81895 1787* The name of any <<variable-assignment,variable>> known
320644e2 1788 at{nbsp}__**L**__.
05f81895
PP
1789--
1790+
e57a18e1
PP
1791The value of the special name `ICITTE` (`int` type) in this expression
1792is the <<cur-offset,current offset>> (before handling the items to
1793repeat).
1794
1795** A valid {py3} name.
1796+
1797For the name `__NAME__`, this is equivalent to the
ee724c95 1798`pass:[{]__NAME__}` form above.
e57a18e1 1799
cd33dfe6 1800. Zero or more items except, recursively, a macro definition block.
e57a18e1 1801
261c5ecf 1802. The `!end` closing.
e57a18e1
PP
1803
1804You may also use a <<post-item-repetition,post-item repetition>> after
1805some items. The form ``!repeat{nbsp}__X__{nbsp}__ITEMS__{nbsp}!end``
1806is equivalent to ``(__ITEMS__){nbsp}pass:[*]{nbsp}__X__``.
71aaa3f7
PP
1807
1808====
1809Input:
1810
1811----
fc21bb27 1812!repeat 0o400
ee724c95 1813 [end - ICITTE - 1 : 8]
e57a18e1
PP
1814!end
1815
1816<end>
71aaa3f7
PP
1817----
1818
1819Output:
1820
1821----
1822ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ ••••••••••••••••
1823ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ ••••••••••••••••
1824df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ ••••••••••••••••
1825cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ ••••••••••••••••
1826bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ ••••••••••••••••
1827af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ ••••••••••••••••
18289f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ ••••••••••••••••
18298f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ ••••••••••••••••
18307f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp
18316f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba`
18325f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP
18334f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@
18343f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210
18352f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"!
18361f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ ••••••••••••••••
18370f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ ••••••••••••••••
1838----
1839====
1840
2adf4336
PP
1841====
1842Input:
1843
1844----
1845{times = 1}
e57a18e1 1846
2adf4336 1847aa bb cc dd
e57a18e1
PP
1848
1849!repeat 3
2adf4336 1850 <here>
e57a18e1
PP
1851
1852 !repeat {here + 1}
1853 ee ff
1854 !end
1855
1856 11 22 !repeat times 33 !end
1857
2adf4336 1858 {times = times + 1}
e57a18e1
PP
1859!end
1860
2adf4336
PP
1861"coucou!"
1862----
1863
1864Output:
1865
1866----
1867aa bb cc dd ee ff ee ff ee ff ee ff ee ff 11 22 ┆ •••••••••••••••"
186833 ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ 3•••••••••••••••
1869ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1870ff ee ff ee ff 11 22 33 33 ee ff ee ff ee ff ee ┆ ••••••"33•••••••
1871ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1872ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1873ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1874ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1875ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1876ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1877ff ee ff ee ff ee ff ee ff ee ff ee ff 11 22 33 ┆ ••••••••••••••"3
187833 33 63 6f 75 63 6f 75 21 ┆ 33coucou!
1879----
1880====
1881
cd33dfe6
PP
1882=== Transformation block
1883
1884A _transformation block_ represents the bytes of one or more items
1885transformed into other bytes by a function.
1886
1887As of this version, Normand only offers a predetermined set of
1888transformation functions.
1889
1890An encoded block is:
1891
1892. The `!transform` or `!t` opening.
1893
1894. A transformation function name amongst:
1895+
1896--
1897[horizontal]
1898`base64`::
1899`b64`::
1900 Standard https://datatracker.ietf.org/doc/html/rfc4648.html#section-4[Base64].
1901
1902`base64u`::
1903`b64u`::
1904 URL-safe Base64, using `-` instead of `pass:[+]` and `_` instead of
1905 `/`.
1906
1907`base32`::
1908`b32`::
1909 Standard https://datatracker.ietf.org/doc/html/rfc4648.html#section-6[Base32].
1910
1911`base16`::
1912`b16`::
1913 Standard https://datatracker.ietf.org/doc/html/rfc4648.html#section-8[Base16].
1914
1915`ascii85`::
1916`a85`::
1917 https://en.wikipedia.org/wiki/Ascii85[Ascii85] without padding.
1918
1919`ascii85p`::
1920`a85p`::
1921 Ascii85 with padding.
1922
1923`base85`::
1924`b85`::
1925 https://en.wikipedia.org/wiki/Ascii85[Base85] (like Git-style binary
1926 diffs) without padding.
1927
1928`base85p`::
1929`b85p`::
1930 Base85 with padding.
1931
1932`quopri`::
1933`qp`::
1934 MIME
1935 https://datatracker.ietf.org/doc/html/rfc2045#section-6.7[quoted-printable]
1936 without quoted whitespaces.
1937
1938`quoprit`::
1939`qpt`::
1940 MIME quoted-printable with quoted whitespaces.
1941
1942`gzip`::
1943`gz`::
1944 https://en.wikipedia.org/wiki/Gzip[gzip].
1945
1946`bzip2`::
1947`bz2`::
1948 https://en.wikipedia.org/wiki/Bzip2[bzip2].
1949--
1950
1951. Zero or more items except, recursively, a macro definition block.
1952+
1953Any {py3} expression within any of those items may not refer to a future
1954<<label,label>>.
1955+
1956The value of the special name `ICITTE` in any {py3} expression within
1957any of those items is the <<cur-offset,current offset>> _before_ Normand
1958applies the transformation function. Therefore, labels defined within
1959those items also have the current offset value _before_ Normand applies
1960the transformation function.
1961
1962. The `!end` closing.
1963
1964The <<cur-offset,current offset>> after having handled the last item of
1965a transformation block is the value of the current offset before
1966handling the first item plus the size of the generated (transformed)
1967bytes. In other words, <<current-offset-setting,current offset
1968settings>> within the items of the block have no impact outside said
1969block.
1970
1971====
1972Input:
1973
1974----
1975aa bb cc dd
1976
ee724c95 1977"size of compressed section: " [end - start : 8]
cd33dfe6
PP
1978
1979<start>
1980
1981!transform bzip2
1982 "this will be compressed!"
1983 89*100 00*5000
1984!end
1985
1986<end>
1987
1988"yes!"
1989----
1990
1991Output:
1992
1993----
1994aa bb cc dd 73 69 7a 65 20 6f 66 20 63 6f 6d 70 ┆ ••••size of comp
199572 65 73 73 65 64 20 73 65 63 74 69 6f 6e 3a 20 ┆ ressed section:
199652 42 5a 68 39 31 41 59 26 53 59 68 e1 8c fc 00 ┆ RBZh91AY&SYh••••
199700 33 d1 e0 c0 00 60 00 5e 66 dc 80 00 20 00 80 ┆ •3••••`•^f••• ••
199800 08 20 00 31 40 d3 43 23 26 20 ca 87 a9 a1 e8 ┆ •• •1@•C#& •••••
199918 29 44 80 9c 80 49 bf cc b3 e8 45 ed e2 76 ad ┆ •)D•••I••••E••v•
20000f 12 8b 8a d6 cd 40 04 7e 2e e4 8a 70 a1 20 d1 ┆ ••••••@•~.••p• •
2001c3 19 f8 79 65 73 21 ┆ •••yes!
2002----
2003====
2004
2005====
2006Input:
2007
2008----
200988*16
2010
2011!t a85
2012 "I am determined to be cheerful and happy in whatever situation "
2013 "I may find myself. For I have learned that the greater part of "
2014 "our misery or unhappiness is determined not by our circumstance "
2015 "but by our disposition."
2016!end
2017
2018@128~99h
2019
ee724c95 2020!t qp <beg> [ICITTE - beg : 8] * 50 !end
cd33dfe6
PP
2021----
2022
2023Output:
2024
2025----
202688 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 ┆ ••••••••••••••••
202738 4b 5f 47 59 2b 43 6f 26 2a 41 54 44 58 25 44 ┆ 8K_GY+Co&*ATDX%D
202849 6d 3f 24 46 44 69 3a 32 41 4b 59 4a 72 41 53 ┆ Im?$FDi:2AKYJrAS
202923 6d 6f 46 5f 69 31 2f 44 49 61 6c 27 40 3b 70 ┆ #moF_i1/DIal'@;p
203031 32 2b 44 47 5e 39 47 41 28 45 2c 41 54 68 58 ┆ 12+DG^9GA(E,AThX
20312a 2b 45 4d 37 3d 46 5e 5d 42 2b 44 66 2d 5b 68 ┆ *+EM7=F^]B+Df-[h
20322b 44 6b 50 34 2b 44 2c 3e 2a 41 30 3e 60 37 46 ┆ +DkP4+D,>*A0>`7F
203328 4b 30 22 2f 67 2a 57 25 45 5a 64 70 72 42 4f ┆ (K0"/g*W%EZdprBO
203451 27 71 2b 44 62 55 74 45 63 2c 48 21 2b 45 56 ┆ Q'q+DbUtEc,H!+EV
20353a 2a 46 3c 47 5b 3d 41 4b 59 57 2b 41 52 54 5b ┆ :*F<G[=AKYW+ART[
20366c 45 5a 66 3d 30 45 63 60 46 42 41 66 75 23 37 ┆ lEZf=0Ec`FBAfu#7
203745 5a 66 34 35 46 28 4b 42 3b 2b 45 29 39 43 46 ┆ EZf45F(KB;+E)9CF
203860 28 6c 24 45 2c 5d 4e 2f 41 54 4d 6f 38 42 6c ┆ `(l$E,]N/ATMo8Bl
203962 44 2d 41 54 56 4c 28 44 2f 21 6d 21 41 30 3e ┆ bD-ATVL(D/!m!A0>
204063 2e 46 3c 47 25 3c 2b 45 29 43 43 2b 43 66 2c ┆ c.F<G%<+E)CC+Cf,
20412b 40 73 29 58 30 46 43 42 26 73 41 4b 59 48 29 ┆ +@s)X0FCB&sAKYH)
204246 3c 47 25 3c 2b 45 29 43 43 2b 43 6f 32 2d 45 ┆ F<G%<+E)CC+Co2-E
20432c 54 66 33 46 44 35 5a 32 2f 63 99 99 99 99 99 ┆ ,Tf3FD5Z2/c•••••
20443d 30 30 3d 30 31 3d 30 32 3d 30 33 3d 30 34 3d ┆ =00=01=02=03=04=
204530 35 3d 30 36 3d 30 37 3d 30 38 3d 30 39 0a 3d ┆ 05=06=07=08=09•=
204630 42 3d 30 43 0d 3d 30 45 3d 30 46 3d 31 30 3d ┆ 0B=0C•=0E=0F=10=
204731 31 3d 31 32 3d 31 33 3d 31 34 3d 31 35 3d 31 ┆ 11=12=13=14=15=1
204836 3d 31 37 3d 31 38 3d 31 39 3d 31 41 3d 31 42 ┆ 6=17=18=19=1A=1B
20493d 31 43 3d 31 44 3d 31 45 3d 31 46 20 21 22 23 ┆ =1C=1D=1E=1F !"#
205024 25 26 27 28 29 2a 2b 2c 2d 3d 0a 2e 2f 30 31 ┆ $%&'()*+,-=•./01
2051----
2052====
2053
320644e2
PP
2054=== Macro definition block
2055
2056A _macro definition block_ associates a name and parameter names to
2057a group of items.
2058
2059A macro definition block doesn't lead to generated bytes itself: a
2060<<macro-expansion,macro expansion>> does so.
2061
2062A macro definition may only exist at the root level, that is, not within
2063a <<group,group>>, a <<repetition-block,repetition block>>, a
2064<<conditional-block,conditional block>>, or another
2065<<macro-definition-block,macro definition block>>.
2066
2067All macro definitions must have unique names.
2068
2069A macro definition is:
2070
2071. The `!macro` or `!m` opening.
2072
2073. A valid {py3} name (the macro name).
2074
2075. The `(` parameter name list prefix.
2076
2077. A comma-separated list of zero or more unique parameter names,
2078 each one being a valid {py3} name.
2079
2080. The `)` parameter name list suffix.
2081
2082. Zero or more items except, recursively, a macro definition block.
2083
2084. The `!end` closing.
2085
2086====
2087----
2088!macro bake()
ee724c95 2089 !le [ICITTE * 8 : 16]
320644e2
PP
2090 u16le"predict explode"
2091!end
2092----
2093====
2094
2095====
2096----
2097!macro nail(rep, with_extra, val)
2098 {iter = 1}
2099
2100 !repeat rep
ee724c95
PP
2101 [val + iter : uleb128]
2102 [0xdeadbeef : 32]
320644e2
PP
2103 {iter = iter + 1}
2104 !end
2105
2106 !if with_extra
2107 "meow mix\0"
2108 !end
2109!end
2110----
2111====
2112
2113=== Macro expansion
2114
2115A _macro expansion_ expands the items of a defined
2116<<macro-definition-block,macro>>.
2117
2118The macro to expand must be defined _before_ the expansion.
2119
2120The <<state,state>> before handling the first item of the chosen macro
2121is:
2122
2123<<cur-offset,Current offset>>::
2124 Unchanged.
2125
2126<<cur-bo,Current byte order>>::
2127 Unchanged.
2128
2129Variables::
2130 The only available variables initially are the macro parameters.
2131
2132Labels::
2133 None.
2134
2135The state after having handled the last item of the chosen macro is:
2136
2137Current offset::
2138 The one before handling the first item of the macro plus the size
2139 of the generated data of the macro expansion.
2140+
2141IMPORTANT: This means <<current-offset-setting,current offset setting>>
2142items within the expanded macro don't impact the final current offset.
2143
2144Current byte order::
2145 The one before handling the first item of the macro.
2146
2147Variables::
2148 The ones before handling the first item of the macro.
2149
2150Labels::
2151 The ones before handling the first item of the macro.
2152
2153A macro expansion is:
2154
2155. The `m:` prefix.
2156
2157. A valid {py3} name (the name of the macro to expand).
2158
2159. The `(` parameter value list prefix.
2160
2161. A comma-separated list of zero or more unique parameter values.
2162+
2163The number of parameter values must match the number of parameter
2164names of the definition of the chosen macro.
2165+
2166A parameter value is one of:
2167+
2168--
fc21bb27 2169* A <<const-int,constant integer>>, possibly negative.
320644e2 2170
dbd84e74
PP
2171* A constant floating point number.
2172
320644e2
PP
2173* The ``pass:[{]`` prefix, a valid {py3} expression of which the
2174 evaluation result type is `int` or `bool` (automatically converted to
ee724c95 2175 `int`), and the `}` suffix.
320644e2
PP
2176+
2177For a macro expansion at some source location{nbsp}__**L**__, this
2178expression may contain:
2179
2180** The name of any <<label,label>> defined before{nbsp}__**L**__
2181 which isn't within a nested group.
2182** The name of any <<variable-assignment,variable>> known
2183 at{nbsp}__**L**__.
2184
2185+
2186The value of the special name `ICITTE` (`int` type) in this expression
2187is the <<cur-offset,current offset>> (before handling the items of the
2188chosen macro).
2189
2190* A valid {py3} name.
2191+
2192For the name `__NAME__`, this is equivalent to the
2193`pass:[{]__NAME__pass:[}]` form above.
2194--
2195
2196. The `)` parameter value list suffix.
2197
2198====
2199Input:
2200
2201----
2202!macro bake()
ee724c95 2203 !le [ICITTE * 8 : 16]
320644e2
PP
2204 u16le"predict explode"
2205!end
2206
2207"hello [" m:bake() "] world"
2208
2209m:bake() * 5
2210----
2211
2212Output:
2213
2214----
221568 65 6c 6c 6f 20 5b 38 00 70 00 72 00 65 00 64 ┆ hello [8•p•r•e•d
221600 69 00 63 00 74 00 20 00 65 00 78 00 70 00 6c ┆ •i•c•t• •e•x•p•l
221700 6f 00 64 00 65 00 5d 20 77 6f 72 6c 64 70 01 ┆ •o•d•e•] worldp•
221870 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
221965 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 02 ┆ e•x•p•l•o•d•e•p•
222070 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
222165 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 03 ┆ e•x•p•l•o•d•e•p•
222270 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
222365 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 04 ┆ e•x•p•l•o•d•e•p•
222470 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
222565 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 05 ┆ e•x•p•l•o•d•e•p•
222670 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
222765 00 78 00 70 00 6c 00 6f 00 64 00 65 00 ┆ e•x•p•l•o•d•e•
2228----
2229====
2230
2231====
2232Input:
2233
2234----
2235!macro A(val, is_be)
ee724c95 2236 !le
320644e2
PP
2237
2238 !if is_be
ee724c95 2239 !be
320644e2
PP
2240 !end
2241
ee724c95 2242 [val : 16]
320644e2
PP
2243!end
2244
2245!macro B(rep, is_be)
2246 {iter = 1}
2247
2248 !repeat rep
2249 m:A({iter * 3}, is_be)
2250 {iter = iter + 1}
2251 !end
2252!end
2253
2254m:B(5, 1)
2255m:B(3, 0)
2256----
2257
2258Output:
2259
2260----
226100 03 00 06 00 09 00 0c 00 0f 03 00 06 00 09 00
2262----
2263====
2264
dbd84e74
PP
2265====
2266Input:
2267
2268----
ee724c95 2269!macro flt32be(val) !be [val : 32] !end
dbd84e74
PP
2270
2271"CHEETOS"
2272m:flt32be(-42.17)
2273m:flt32be(56.23e-4)
2274----
2275
2276Output:
2277
2278----
227943 48 45 45 54 4f 53 c2 28 ae 14 3b b8 41 25 ┆ CHEETOS•(••;•A%
2280----
2281====
2282
e57a18e1
PP
2283=== Post-item repetition
2284
2285A _post-item repetition_ represents the bytes of an item repeated a
2286given number of times.
2287
2288A post-item repetition is:
2289
27d52a19 2290. One of those items:
e57a18e1 2291
27d52a19
PP
2292** A <<byte-constant,byte constant>>.
2293** A <<literal-string,literal string>>.
2294** A <<fixed-length-number,fixed-length number>>.
2295** An <<leb128-integer,LEB128 integer>>.
7a7b31e8 2296** A <<string,string>>.
320644e2 2297** A <<macro-expansion,macro-expansion>>.
cd33dfe6 2298** A <<transformation-block,transformation block>>.
27d52a19 2299** A <<group,group>>.
e57a18e1
PP
2300
2301. The ``pass:[*]`` character.
2302
2303. One of:
2304
2305** A positive integer (hexadecimal starting with `0x` or `0X` accepted)
2306 which is the number of times to repeat the previous item.
2307
27d52a19
PP
2308** The ``pass:[{]`` prefix, a valid {py3} expression of which the
2309 evaluation result type is `int` or `bool` (automatically converted to
ee724c95 2310 `int`), and the `}` suffix.
e57a18e1 2311+
320644e2
PP
2312For a post-item repetition at some source location{nbsp}__**L**__, this
2313expression may contain:
e57a18e1
PP
2314+
2315--
27d52a19
PP
2316* The name of any <<label,label>> defined before{nbsp}__**L**__
2317 which isn't within a nested group and
2318 which isn't part of the repeated item.
e57a18e1
PP
2319* The name of any <<variable-assignment,variable>> known
2320 at{nbsp}__**L**__, which isn't part of its repeated item, and which
320644e2 2321 doesn't.
e57a18e1
PP
2322--
2323+
2324The value of the special name `ICITTE` (`int` type) in this expression
2325is the <<cur-offset,current offset>> (before handling the items to
2326repeat).
2327
2328** A valid {py3} name.
2329+
2330For the name `__NAME__`, this is equivalent to the
2331`pass:[{]__NAME__pass:[}]` form above.
2332
2333You may also use a <<repetition-block,repetition block>>. The form
2334``__ITEM__{nbsp}pass:[*]{nbsp}__X__`` is equivalent to
2335``!repeat{nbsp}__X__{nbsp}__ITEM__{nbsp}!end``.
2336
2337====
2338Input:
2339
2340----
ee724c95 2341[end - ICITTE - 1 : 8] * 0x100 <end>
e57a18e1
PP
2342----
2343
2344Output:
2345
2346----
2347ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ ••••••••••••••••
2348ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ ••••••••••••••••
2349df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ ••••••••••••••••
2350cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ ••••••••••••••••
2351bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ ••••••••••••••••
2352af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ ••••••••••••••••
23539f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ ••••••••••••••••
23548f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ ••••••••••••••••
23557f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp
23566f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba`
23575f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP
23584f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@
23593f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210
23602f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"!
23611f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ ••••••••••••••••
23620f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ ••••••••••••••••
2363----
2364====
2365
2366====
2367Input:
2368
2369----
2370{times = 1}
2371aa bb cc dd
2372(
2373 <here>
2374 (ee ff) * {here + 1}
2375 11 22 33 * {times}
2376 {times = times + 1}
2377) * 3
2378"coucou!"
2379----
2380
2381Output:
2382
2383----
2384aa bb cc dd ee ff ee ff ee ff ee ff ee ff 11 22 ┆ •••••••••••••••"
238533 ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ 3•••••••••••••••
2386ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2387ff ee ff ee ff 11 22 33 33 ee ff ee ff ee ff ee ┆ ••••••"33•••••••
2388ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2389ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2390ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2391ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2392ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2393ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2394ff ee ff ee ff ee ff ee ff ee ff ee ff 11 22 33 ┆ ••••••••••••••"3
239533 33 63 6f 75 63 6f 75 21 ┆ 33coucou!
2396----
2397====
2398
71aaa3f7
PP
2399== Command-line tool
2400
2401If you <<install-normand,installed>> the `normand` package, then you
2402can use the `normand` command-line tool:
2403
2404----
2405$ normand <<< '"ma gang de malades"' | hexdump -C
2406----
2407
2408----
240900000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad|
241000000010 65 73 |es|
2411----
2412
2413If you copy the `normand.py` module to your own project, then you can
2414run the module itself:
2415
2416----
2417$ python3 -m normand <<< '"ma gang de malades"' | hexdump -C
2418----
2419
2420----
242100000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad|
242200000010 65 73 |es|
2423----
2424
2425Without a path argument, the `normand` tool reads from the standard
2426input.
2427
2428The `normand` tool prints the generated binary data to the standard
2429output.
2430
2431Various options control the initial <<state,state>> of the processor:
2432use the `--help` option to learn more.
2433
2434== {py3} API
2435
e57a18e1 2436The whole `normand` package/module public API is:
71aaa3f7
PP
2437
2438[source,python]
2439----
e57a18e1 2440# Byte order.
71aaa3f7
PP
2441class ByteOrder(enum.Enum):
2442 # Big endian.
2443 BE = ...
2444
2445 # Little endian.
2446 LE = ...
2447
2448
e57a18e1
PP
2449# Text location.
2450class TextLocation:
71aaa3f7
PP
2451 # Line number.
2452 @property
2453 def line_no(self) -> int:
2454 ...
2455
2456 # Column number.
2457 @property
2458 def col_no(self) -> int:
2459 ...
2460
2461
f5dcb24c
PP
2462# Parsing error message.
2463class ParseErrorMessage:
2464 # Message text.
2465 @property
2466 def text(self):
2467 ...
2468
2469 # Source text location.
2470 @property
2471 def text_location(self):
2472 ...
2473
2474
e57a18e1 2475# Parsing error.
71aaa3f7 2476class ParseError(RuntimeError):
f5dcb24c
PP
2477 # Parsing error messages.
2478 #
2479 # The first message is the most _specific_ one.
71aaa3f7 2480 @property
f5dcb24c 2481 def messages(self):
71aaa3f7
PP
2482 ...
2483
2484
e57a18e1
PP
2485# Variables dictionary type (for type hints).
2486VariablesT = typing.Dict[str, typing.Union[int, float]]
2487
2488
2489# Labels dictionary type (for type hints).
2490LabelsT = typing.Dict[str, int]
1b8aa84a
PP
2491
2492
e57a18e1 2493# Parsing result.
71aaa3f7
PP
2494class ParseResult:
2495 # Generated data.
2496 @property
2497 def data(self) -> bytearray:
2498 ...
2499
2500 # Updated variable values.
2501 @property
1b8aa84a 2502 def variables(self) -> SymbolsT:
71aaa3f7
PP
2503 ...
2504
2505 # Updated main group label values.
2506 @property
1b8aa84a 2507 def labels(self) -> SymbolsT:
71aaa3f7
PP
2508 ...
2509
2510 # Final offset.
2511 @property
2512 def offset(self) -> int:
2513 ...
2514
2515 # Final byte order.
2516 @property
1b8aa84a 2517 def byte_order(self) -> typing.Optional[ByteOrder]:
71aaa3f7
PP
2518 ...
2519
1b8aa84a 2520
e57a18e1
PP
2521# Parses the `normand` input using the initial state defined by
2522# `init_variables`, `init_labels`, `init_offset`, and `init_byte_order`,
2523# and returns the corresponding parsing result.
71aaa3f7 2524def parse(normand: str,
1b8aa84a
PP
2525 init_variables: typing.Optional[SymbolsT] = None,
2526 init_labels: typing.Optional[SymbolsT] = None,
71aaa3f7
PP
2527 init_offset: int = 0,
2528 init_byte_order: typing.Optional[ByteOrder] = None) -> ParseResult:
2529 ...
2530----
2531
2532The `normand` parameter is the actual <<learn-normand,Normand input>>
2533while the other parameters control the initial <<state,state>>.
2534
2535The `parse()` function raises a `ParseError` instance should it fail to
2536parse the `normand` string for any reason.
bf8f3b38
PP
2537
2538== Development
2539
2540Normand is a https://python-poetry.org/[Poetry] project.
2541
2542To develop it, install it through Poetry and enter the virtual
2543environment:
2544
2545----
2546$ poetry install
2547$ poetry shell
2548$ normand <<< '"lol" * 10 0a'
2549----
2550
2551`normand.py` is processed by:
2552
2553* https://microsoft.github.io/pyright/[Pyright]
2554* https://github.com/psf/black[Black]
2555* https://pycqa.github.io/isort/[isort]
2556
2557=== Testing
2558
2559Use https://docs.pytest.org/[pytest] to test Normand once the package is
2560part of your virtual environment, for example:
2561
2562----
2563$ poetry install
2564$ poetry run pip3 install pytest
2565$ poetry run pytest
2566----
2567
2568The `pytest` project is currently not a development dependency in
2569`pyproject.toml` due to backward compatibiliy issues with
2570Python{nbsp}3.4.
2571
2572In the `tests` directory, each `*.nt` file is a test. The file name
2573prefix indicates what it's meant to test:
2574
2575`pass-`::
2576 Everything above the `---` line is the valid Normand input
2577 to test.
2578+
2579Everything below the `---` line is the expected data
2580(whitespace-separated hexadecimal bytes).
2581
2582`fail-`::
2583 Everything above the `---` line is the invalid Normand input
2584 to test.
2585+
2586Everything below the `---` line is the expected error message having
2587this form:
2588+
2589----
2590LINE:COL - MESSAGE
2591----
2592
2593=== Contributing
2594
2595Normand uses https://review.lttng.org/admin/repos/normand,general[Gerrit]
2596for code review.
2597
2598To report a bug, https://github.com/efficios/normand/issues/new[create a
2599GitHub issue].
This page took 0.125153 seconds and 4 git commands to generate.