1 // Show ToC at a specific location for a GitHub rendering
10 // This is to mimic what GitHub does so that anchors work in an offline
21 image::normand-logo.png[]
24 image:https://img.shields.io/pypi/v/normand.svg?label=Latest%20version[link="https://pypi.python.org/pypi/normand"]
27 _**Normand**_ is a text-to-binary processor with its own language.
29 This package offers both a portable {py3} module and a command-line
32 WARNING: This version of Normand is 0.23, meaning both the Normand
33 language and the module/CLI interface aren't stable.
36 // ToC location for a GitHub rendering
42 The purpose of Normand is to consume human-readable text representing
43 bytes and to produce the corresponding binary data.
47 Consider the following Normand input:
50 4f 55 32 bb $167 fe %10100111 a9 $-32
53 The generated nine bytes are:
56 4f 55 32 bb a7 fe a7 a9 e0
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.
64 The Normand language is more than simple lists of bytes, though. Its
67 Comments, including a bunch of insignificant symbols which may improve readability::
72 ff bb %1101:0010 # This is a comment
73 78 29 af $192 # This too # 99 $-80
74 fe80::6257:18ff:fea3:4229
76 10839636-5d65-4a68-8e6a-21608ddf7258
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
87 Hexadecimal, decimal, and binary byte constants::
92 aa bb $247 $-89 %0011_0010 %11.01= 10/10
107 u16le"stress\nverdict 🤣"
108 s:latin3{hex(ICITTE)}
114 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 00 73 00 74 ┆ hello world!•s•t
115 00 72 00 65 00 73 00 73 00 0a 00 76 00 65 00 72 ┆ •r•e•s•s•••v•e•r
116 00 64 00 69 00 63 00 74 00 20 00 3e d8 23 dd 30 ┆ •d•i•c•t• •>•#•0
120 Labels: special variables holding the offset where they're defined::
123 <beg> b2 52 e3 bc 91 05
124 $100 $50 <chair> 33 9f fe
131 5e 65 {tower = 47} c6 7f f2 c4
132 44 {hurl = tower - 14} b5 {tower = hurl} 26 2d
135 The value of a variable assignment is the evaluation of a valid {py3}
136 expression which may include label and variable names.
138 Fixed-length number with a given length (8{nbsp}bits to 64{nbsp}bits) and byte order::
144 !be 67 <lbl> 44 $178 [(end - lbl) * 8 + strength : 16] $99 <end>
152 67 44 b2 00 2c 63 37 f8 ff ff c0 09 21 fb 82 c2
156 The encoded number is the evaluation of a valid {py3} expression which
157 may include label and variable names.
159 https://en.wikipedia.org/wiki/LEB128[LEB128] integer::
164 aa bb cc [-1993 : sleb128] <meow> dd ee ff
165 [meow * 199 : uleb128]
171 aa bb cc b7 70 dd ee ff e3 07
174 The encoded integer is the evaluation of a valid {py3} expression which
175 may include label and variable names.
198 aa bb cc 66 6f 6f 66 69 67 68 74 66 6f 6f 66 69 ┆ •••foofightfoofi
199 67 68 74 66 6f 6f 62 61 72 66 6f 6f 62 61 72 ┆ ghtfoobarfoobar
207 aa bb * 5 cc <zoom> "yeah\0" * {zoom * 3}
217 aa bb bb bb bb bb cc 79 65 61 68 00 79 65 61 68 ┆ •••••••yeah•yeah
218 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah•
219 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 79 ┆ yeah•yeah•yeah•y
220 65 61 68 00 79 65 61 68 00 79 65 61 68 00 79 65 ┆ eah•yeah•yeah•ye
221 61 68 00 79 65 61 68 00 79 65 61 68 00 79 65 61 ┆ ah•yeah•yeah•yea
222 68 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 ┆ h•yeah•yeah•yeah
223 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah•
224 ff ee 6a 75 69 63 65 ff ee 6a 75 69 63 65 ff ee ┆ ••juice••juice••
225 6a 75 69 63 65 ┆ juice
244 00 00 00 c7 00 00 00 00 00 00 00 00 00 00 00 2b
245 ff 85 ff ff 00 00 15 d0
267 ef be ad de 37 f8 09 00 00 00 00 00 00 00 00 00 ┆ ••••7•••••••••••
268 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
269 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
270 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
271 40 6d 65 6f 77 20 6d 69 78 ff ff ff ff ff ff ff ┆ @meow mix•••••••
272 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
273 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
274 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
275 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
276 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
277 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
278 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ ••••••••••••••••
279 ff ff ff ff ff ff ff ff c8 ┆ •••••••••
287 "end of file @ " [end:8]
290 "this part will be gzipped"
299 65 6e 64 20 6f 66 20 66 69 6c 65 20 40 20 3c 1f ┆ end of file @ <•
300 8b 08 00 7b 7b 26 65 02 ff 2b c9 c8 2c 56 28 48 ┆ •••{{&e••+••,V(H
301 2c 2a 51 28 cf cc c9 51 48 4a 55 48 af ca 2c 28 ┆ ,*Q(•••QHJUH••,(
302 48 4d 01 00 d4 cc 5b 8a 19 00 00 00 ┆ HM••••[•••••
305 Multilevel grouping::
310 ff ((aa bb "zoom" cc) * 5) * 3 $-34 * 4
316 ff aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa ┆ •••zoom•••zoom••
317 bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a ┆ •zoom•••zoom•••z
318 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f ┆ oom•••zoom•••zoo
319 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc ┆ m•••zoom•••zoom•
320 aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb ┆ ••zoom•••zoom•••
321 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f ┆ zoom•••zoom•••zo
322 6f 6d cc aa bb 7a 6f 6f 6d cc de de de de ┆ om•••zoom•••••
332 !if world " world" !end
337 m:hello({ICITTE > 15 and ICITTE < 60})
344 ff ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c ┆ ••••hello••••hel
345 6c 6f ff ff ff ff 68 65 6c 6c 6f 20 77 6f 72 6c ┆ lo••••hello worl
346 64 ff ff ff ff 68 65 6c 6c 6f 20 77 6f 72 6c 64 ┆ d••••hello world
347 ff ff ff ff 68 65 6c 6c 6f 20 77 6f 72 6c 64 ff ┆ ••••hello world•
348 ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c 6c ┆ •••hello••••hell
349 6f ff ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 ┆ o••••hello••••he
350 6c 6c 6f ff ff ff ff 68 65 6c 6c 6f ff ff ff ff ┆ llo••••hello••••
351 68 65 6c 6c 6f ff ff ff ff 68 65 6c 6c 6f ff ff ┆ hello••••hello••
352 ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c 6c 6f ┆ ••hello••••hello
353 ff ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c ┆ ••••hello••••hel
354 6c 6f ff ff ff ff 68 65 6c 6c 6f ┆ lo••••hello
357 Precise error reporting::
360 /tmp/meow.normand:10:24 - Expecting a bit (`0` or `1`).
364 /tmp/meow.normand:32:6 - Unexpected character `k`.
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`}.
372 /tmp/meow.normand:32:19 - While expanding the macro `meow`:
373 /tmp/meow.normand:35:5 - While expanding the macro `zzz`:
374 /tmp/meow.normand:18:9 - Value 315 is outside the 8-bit range when evaluating expression `end - ICITTE`.
377 You can use Normand to track data source files in your favorite VCS
378 instead of raw binary files. The binary files that Normand generates can
379 be used to test file format decoding, including malformatted data, for
380 example, as well as for education.
382 See <<learn-normand>> to explore all the Normand features.
386 Normand requires Python ≥ 3.4.
391 $ python3 -m pip install --user normand
395 https://packaging.python.org/en/latest/tutorials/installing-packages/#installing-to-the-user-site[Installing to the User Site]
396 to learn more about a user site installation.
400 Normand has a single module file, `normand.py`, which you can copy as is
401 to your project to use it (both the <<python3-api,`normand.parse()`>>
402 function and the <<command-line-tool,command-line tool>>).
404 `normand.py` has _no external dependencies_, but if you're using
405 Python{nbsp}3.4, you'll need a local copy of the standard `typing`
411 The design goals of Normand are:
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.
419 The most basic Normand input is a sequence of hexadecimal constants
420 (for example, `4e6f726d616e64`) which produce exactly what you'd
423 Most Normand features map to programming language concepts you already
424 know and understand: constant integers, literal strings, variables,
425 conditionals, repetitions/loops, and the rest.
427 Concise 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.
432 Compare the following Normand input and some hypothetical XML
433 equivalent, for example:
435 .Actual normand input.
437 ff dd 01 ab $192 $-128 %1101:0011
444 # five times because xyz
446 "hello world " [iter:8]
454 .Hypothetical Normand XML input.
457 <?xml version="1.0" encoding="utf-8" ?>
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 -->
471 <str>hello world </str>
472 <fixed-len-num expr="iter" len="8" />
473 <var-assign name="iter" expr="iter + 1" />
482 A Normand text input is a sequence of items which represent a sequence
485 [[state]] During the processing of items to data, Normand relies on a
490 |State variable |Description |Initial value: <<python3-api,{py3} API>> |Initial value: <<command-line-tool,CLI>>
492 |[[cur-offset]] Current offset
494 The current offset has an effect on the value of <<label,labels>> and of
495 the special `ICITTE` name in <<fixed-length-number,fixed-length
496 number>>, <<leb-128-integer,LEB128 integer>>, <<string,string>>,
497 <<filling,filling>>, <<variable-assignment,variable assignment>>,
498 <<conditional-block,conditional block>>, <<repetition-block,repetition
499 block>>, <<macro-expansion,macro expansion>>, and
500 <<post-item-repetition,post-item repetition>> expression evaluation.
502 Each generated byte increments the current offset.
504 A <<current-offset-setting,current offset setting>> may change the
505 current offset without generating data.
507 An <<current-offset-alignment,current offset alignment>> generates
508 padding bytes to make the current offset satisfy a given alignment.
509 |`init_offset` parameter of the `parse()` function.
512 |[[cur-bo]] Current byte order
514 The current byte order can have an effect on the encoding of
515 <<fixed-length-number,fixed-length numbers>>.
517 A <<current-byte-order-setting,current byte order setting>> may change
518 the current byte order.
519 |`init_byte_order` parameter of the `parse()` function.
520 |`--byte-order` option.
523 |Mapping of label names to integral values.
524 |`init_labels` parameter of the `parse()` function.
525 |One or more `--label` options.
527 |<<variable-assignment,Variables>>
528 |Mapping of variable names to integral or floating point number values.
529 |`init_variables` parameter of the `parse()` function.
530 |One or more `--var` or `--var-str` options.
533 The available items are:
535 * A <<byte-constant,constant integer>> representing one or more
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.
541 * A <<current-byte-order-setting,current byte order setting>> (big or
544 * A <<fixed-length-number,fixed-length number>> (integer or
545 floating point), possibly using the <<cur-bo,current byte order>>, and
546 of which the value is the result of a {py3} expression.
548 * An <<leb128-integer,LEB128 integer>> of which the value is the result
549 of a {py3} expression.
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.
555 * A <<current-offset-setting,current offset setting>>.
557 * A <<current-offset-alignment,current offset alignment>>.
559 * A <<filling,filling>>.
561 * A <<label,label>>, that is, a named constant holding the current
564 This is similar to an assembly label.
566 * A <<variable-assignment,variable assignment>> associating a name to
567 the integral result of an evaluated {py3} expression.
569 * A <<group,group>>, that is, a scoped sequence of items.
571 * A <<conditional-block,conditional block>>.
573 * A <<repetition-block,repetition block>>.
575 * A <<transformation-block,transformation block>>.
577 * A <<macro-definition-block,macro definition block>>.
579 * A <<macro-expansion,macro expansion>>.
581 Moreover, you can repeat many items above a constant or variable number
582 of times with the ``pass:[*]`` operator _after_ the item to repeat. This
583 is called a <<post-item-repetition,post-item repetition>>.
585 A Normand comment may exist pretty much anywhere between tokens.
587 A comment is anything between two ``pass:[#]`` characters on the same
588 line, or from ``pass:[#]`` until the end of the line. Whitespaces are
589 also considered comments. The following symbols are also considered
590 comments around and between items, as well as between hexadecimal
591 nibbles and binary bits of <<byte-constant,byte constants>>:
594 & , - . / : ; = ? \ _ |
597 The latter serve to improve readability so that you may write, for
598 example, a MAC address or a UUID as is.
600 [[const-int]] Many items require a _constant integer_, possibly
601 negative, in which case it may start with `-` for a negative integer. A
602 positive constant integer is any of:
605 One or mode digits (`0` to `9`).
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.
617 * The `0o` or `0O` prefix followed with one or more octal digits
619 * One or more octal digits followed with the `o`, `O`, `q`, or `Q`
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.
628 In general, anything between `pass:[{]` and `}` is a {py3} expression.
630 You can test the examples of this section with the `normand`
631 <<command-line-tool,command-line tool>> as such:
634 $ normand file | hexdump -C
637 where `file` is the name of a file containing the Normand input.
641 A _byte constant_ represents one or more constant bytes.
646 Two consecutive hexadecimal digits representing a single byte.
649 One or more digits after the `$` prefix representing a single byte.
651 Binary form:: {empty}
654 . __**N**__ `%` prefixes (at least one).
656 The number of `%` characters is the number of subsequent expected bytes.
658 . __**N**__{nbsp}×{nbsp}8 bits (`0` or `1`).
679 $192 %1100/0011 $ -77
693 58f64689-6316-4d55-8a1a-04cada366172
694 fe80::6257:18ff:fea3:4229
700 58 f6 46 89 63 16 4d 55 8a 1a 04 ca da 36 61 72 ┆ X•F•c•MU•••••6ar
701 fe 80 62 57 18 ff fe a3 42 29 ┆ ••bW••••B)
709 %01110011 %01100001 %01101100 %01110101 %01110100
710 %%%1101:0010 11111111 #A#11 #B#00 #C#011 #D#1
716 73 61 6c 75 74 d2 ff c7 ┆ salut•••
722 A _literal string_ represents the encoded bytes of a literal string
723 using the UTF-8, UTF-16, UTF-32, or Latin-1 to Latin-10 encoding.
725 The string to encode isn't implicitly null-terminated: use `\0` at the
726 end of the string to add a null character.
730 . **Optional**: one of the following encodings instead of the default
786 . The ``pass:["]`` prefix.
788 . A sequence of zero or more characters, possibly containing escape
791 An escape sequence is the ``\`` character followed by one of:
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)
808 . The ``pass:["]`` suffix.
814 "coucou tout le monde!"
820 63 6f 75 63 6f 75 20 74 6f 75 74 20 6c 65 20 6d ┆ coucou tout le m
821 6f 6e 64 65 21 ┆ onde!
829 u16le"I am not young enough to know everything."
835 49 00 20 00 61 00 6d 00 20 00 6e 00 6f 00 74 00 ┆ I• •a•m• •n•o•t•
836 20 00 79 00 6f 00 75 00 6e 00 67 00 20 00 65 00 ┆ •y•o•u•n•g• •e•
837 6e 00 6f 00 75 00 67 00 68 00 20 00 74 00 6f 00 ┆ n•o•u•g•h• •t•o•
838 20 00 6b 00 6e 00 6f 00 77 00 20 00 65 00 76 00 ┆ •k•n•o•w• •e•v•
839 65 00 72 00 79 00 74 00 68 00 69 00 6e 00 67 00 ┆ e•r•y•t•h•i•n•g•
848 s:u32be "\"illusion is the first\nof all pleasures\" 🦉"
854 00 00 00 22 00 00 00 69 00 00 00 6c 00 00 00 6c ┆ •••"•••i•••l•••l
855 00 00 00 75 00 00 00 73 00 00 00 69 00 00 00 6f ┆ •••u•••s•••i•••o
856 00 00 00 6e 00 00 00 20 00 00 00 69 00 00 00 73 ┆ •••n••• •••i•••s
857 00 00 00 20 00 00 00 74 00 00 00 68 00 00 00 65 ┆ ••• •••t•••h•••e
858 00 00 00 20 00 00 00 66 00 00 00 69 00 00 00 72 ┆ ••• •••f•••i•••r
859 00 00 00 73 00 00 00 74 00 00 00 0a 00 00 00 6f ┆ •••s•••t•••••••o
860 00 00 00 66 00 00 00 20 00 00 00 61 00 00 00 6c ┆ •••f••• •••a•••l
861 00 00 00 6c 00 00 00 20 00 00 00 70 00 00 00 6c ┆ •••l••• •••p•••l
862 00 00 00 65 00 00 00 61 00 00 00 73 00 00 00 75 ┆ •••e•••a•••s•••u
863 00 00 00 72 00 00 00 65 00 00 00 73 00 00 00 22 ┆ •••r•••e•••s•••"
864 00 00 00 20 00 01 f9 89 ┆ ••• ••••
872 s:latin1 "Paul Piché"
878 50 61 75 6c 20 50 69 63 68 e9 ┆ Paul Pich•
882 === Current byte order setting
884 This special item sets the <<cur-bo,_current byte order_>>.
886 The two accepted forms are:
889 `!be`:: Set the current byte order to big endian.
890 `!le`:: Set the current byte order to little endian.
892 === Fixed-length number
894 A _fixed-length number_ represents a fixed number of bytes encoding
897 * An unsigned or signed integer (two's complement).
899 The available lengths are 8, 16, 24, 32, 40, 48, 56, and 64.
901 * A floating point number
902 (https://standards.ieee.org/standard/754-2008.html[IEEE{nbsp}754-2008]).
904 The available length are 32 (_binary32_) and 64 (_binary64_).
906 The value is the result of evaluating a {py3} expression.
908 The byte order to use to encode the value is either directly specified
909 or is the <<cur-bo,current byte order>>.
911 A fixed-length number is:
915 . A valid {py3} expression.
917 For a fixed-length number at some source location{nbsp}__**L**__, this
918 expression may contain the name of any accessible <<label,label>> (not
919 within a nested group), including the name of a label defined
920 after{nbsp}__**L**__ (except within a
921 <<transformation-block,transformation block>>), as well as the name of
922 any <<variable-assignment,variable>> known at{nbsp}__**L**__.
924 The value of the special name `ICITTE` (`int` type) in this expression
925 is the <<cur-offset,current offset>> (before encoding the number).
929 . An encoding length in bits amongst:
932 The expression evaluates to an `int` or `bool` value::
933 `8`, `16`, `24`, `32`, `40`, `48`, `56`, and `64`.
935 NOTE: Normand automatically converts a `bool` value to `int`.
937 The expression evaluates to a `float` value::
941 . **Optional**: a suffix of the previous encoding length, without
942 any whitespace, amongst:
946 `be`:: Encode in big endian.
947 `le`:: Encode in little endian.
950 Without this suffix, the encoding byte order is the <<cur-bo,current
951 byte order>> which must be defined if the encoding length is greater
977 # String length in bits
978 [8 * (str_end - str_beg) : 16]
989 00 60 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 ┆ •`hello world!
997 [20 - ICITTE : 8] * 10
1003 14 13 12 11 10 0f 0e 0d 0c 0b
1023 An _LEB128 integer_ represents a variable number of bytes encoding an
1024 unsigned or signed integer which is the result of evaluating a {py3}
1025 expression following the https://en.wikipedia.org/wiki/LEB128[LEB128]
1028 An LEB128 integer is:
1032 . A valid {py3} expression of which the evaluation result type
1033 is `int` or `bool` (automatically converted to `int`).
1035 For an LEB128 integer at some source location{nbsp}__**L**__, this
1036 expression may contain:
1039 * The name of any <<label,label>> defined before{nbsp}__**L**__
1040 which isn't within a nested group.
1041 * The name of any <<variable-assignment,variable>> known
1045 The value of the special name `ICITTE` (`int` type) in this expression
1046 is the <<cur-offset,current offset>> (before encoding the integer).
1048 . The `:` character.
1054 `uleb128`:: Use the unsigned LEB128 format.
1055 `sleb128`:: Use the signed LEB128 format.
1081 [-981238311 + (meow * -23) : sleb128]
1088 aa bb cc dd ee ff fd fa 8d ac 7c 68 65 6c 6c 6f ┆ ••••••••••|hello
1094 A _string_ represents a variable number of bytes encoding a string which
1095 is the result of evaluating a {py3} expression using the UTF-8, UTF-16,
1096 UTF-32, or Latin-1 to Latin-10 encoding.
1098 A string has two possible forms:
1100 Encoding prefix form:: {empty}
1102 . An encoding amongst:
1157 . The ``pass:[{]`` prefix.
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`).
1163 For a string at some source location{nbsp}__**L**__, this expression may
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
1173 The value of the special name `ICITTE` (`int` type) in this expression
1174 is the <<cur-offset,current offset>> (before encoding the string).
1178 Encoding suffix form:: {empty}
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`).
1186 For a string at some source location{nbsp}__**L**__, this expression may
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
1196 The value of the special name `ICITTE` (`int` type) in this expression
1197 is the <<cur-offset,current offset>> (before encoding the string).
1199 . The `:` character.
1201 . A string encoding amongst:
1268 31 20 32 20 33 20 34 20 35 20 36 20 37 20 38 20 ┆ 1 2 3 4 5 6 7 8
1269 39 20 31 30 20 ┆ 9 10
1277 {meow = 'salut jérémie'}
1278 [meow.upper() : s:latin1]
1284 53 41 4c 55 54 20 4a c9 52 c9 4d 49 45 ┆ SALUT J•R•MIE
1288 === Current offset setting
1290 This special item sets the <<cur-offset,_current offset_>>.
1292 A current offset setting is:
1296 . A <<const-int,positive constant integer>> which is the new current
1306 <0x61> [ICITTE : 8] * 8
1312 00 01 02 03 04 05 06 07 61 62 63 64 65 66 67 68 ┆ ••••••••abcdefgh
1320 aa bb cc dd <meow> ee ff
1321 <12> 11 22 33 <mix> 44 55
1322 [meow : 8] [mix : 8]
1328 aa bb cc dd ee ff 11 22 33 44 55 04 0f ┆ •••••••"3DU••
1332 === Current offset alignment
1334 A _current offset alignment_ represents zero or more padding bytes to
1335 make the <<cur-offset,current offset>> meet a given
1336 https://en.wikipedia.org/wiki/Data_structure_alignment[alignment] value.
1338 More specifically, for an alignment value of{nbsp}__**N**__{nbsp}bits,
1339 a current offset alignment represents the required padding bytes until
1340 the current offset is a multiple of __**N**__{nbsp}/{nbsp}8.
1342 A current offset alignment is:
1346 . A <<const-int,positive constant integer>> which is the alignment value
1349 This value must be greater than zero and a multiple of{nbsp}8.
1354 . The ``pass:[~]`` prefix.
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>>.
1359 Without this section, the padding byte value is zero.
1365 11 22 (@32 aa bb cc) * 3
1371 11 22 00 00 aa bb cc 00 aa bb cc 00 aa bb cc
1381 @32~0xcc [-893.5:32]
1388 77 88 cc cc 00 60 5f c4 55 55 55 55 55 55 55 55 ┆ w••••`_•UUUUUUUU
1389 6d 65 6f 77 ┆ meow
1397 aa bb cc <29> @64~255 "zoom"
1403 aa bb cc ff ff ff 7a 6f 6f 6d ┆ ••••••zoom
1409 A _filling_ represents zero or more padding bytes to make the
1410 <<cur-offset,current offset>> reach a given value.
1414 . The ``pass:[+]`` prefix.
1418 ** A <<const-int,positive constant integer>> which is the current offset
1421 ** The ``pass:[{]`` prefix, a valid {py3} expression of which the
1422 evaluation result type is `int` or `bool` (automatically converted to
1423 `int`), and the `}` suffix.
1425 For a filling at some source location{nbsp}__**L**__, this expression
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
1435 The value of the special name `ICITTE` (`int` type) in this expression
1436 is the <<cur-offset,current offset>> (before handling the items to
1439 ** A valid {py3} name.
1441 For the name `__NAME__`, this is equivalent to the
1442 `pass:[{]__NAME__}` form above.
1445 This value must be greater than or equal to the current offset where
1451 . The ``pass:[~]`` prefix.
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.
1456 Without this section, the padding byte value is zero.
1470 aa bb cc dd 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
1471 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
1472 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
1473 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ ••••••••••••••••
1474 68 65 6c 6c 6f 20 77 6f 72 6c 64 ┆ hello world
1482 !macro part(iter, fill)
1483 <0> "particular security " [ord('0') + iter : 8] +fill~0x80
1489 m:part(iter, {32 + 4 * iter})
1497 70 61 72 74 69 63 75 6c 61 72 20 73 65 63 75 72 ┆ particular secur
1498 69 74 79 20 31 80 80 80 80 80 80 80 80 80 80 80 ┆ ity 1•••••••••••
1499 80 80 80 80 70 61 72 74 69 63 75 6c 61 72 20 73 ┆ ••••particular s
1500 65 63 75 72 69 74 79 20 32 80 80 80 80 80 80 80 ┆ ecurity 2•••••••
1501 80 80 80 80 80 80 80 80 80 80 80 80 70 61 72 74 ┆ ••••••••••••part
1502 69 63 75 6c 61 72 20 73 65 63 75 72 69 74 79 20 ┆ icular security
1503 33 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 ┆ 3•••••••••••••••
1504 80 80 80 80 80 80 80 80 70 61 72 74 69 63 75 6c ┆ ••••••••particul
1505 61 72 20 73 65 63 75 72 69 74 79 20 34 80 80 80 ┆ ar security 4•••
1506 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 ┆ ••••••••••••••••
1507 80 80 80 80 80 80 80 80 70 61 72 74 69 63 75 6c ┆ ••••••••particul
1508 61 72 20 73 65 63 75 72 69 74 79 20 35 80 80 80 ┆ ar security 5•••
1509 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 ┆ ••••••••••••••••
1510 80 80 80 80 80 80 80 80 80 80 80 80 ┆ ••••••••••••
1516 A _label_ associates a name to the <<cur-offset,current offset>>.
1518 All the labels of a whole Normand input must have unique names.
1520 A label must not share the name of a <<variable-assignment,variable>>
1527 . A valid {py3} name which is not `ICITTE`.
1531 === Variable assignment
1533 A _variable assignment_ associates a name to the integral result of an
1534 evaluated {py3} expression.
1536 A variable assignment is:
1538 . The ``pass:[{]`` prefix.
1540 . A valid {py3} name which is not `ICITTE`.
1542 . The `=` character.
1544 . A valid {py3} expression of which the evaluation result type is `int`,
1545 `float`, or `bool` (automatically converted to `int`), or `str`.
1547 For a variable assignment at some source location{nbsp}__**L**__, this
1548 expression may contain:
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
1557 The value of the special name `ICITTE` (`int` type) in this expression
1558 is the <<cur-offset,current offset>>.
1567 {meow = 42} 11 22 [meow:8] 33 {meow = ICITTE + 17}
1568 "yooo" [meow + mix : 16]
1574 11 22 2a 33 79 6f 6f 6f 7a 00 ┆ •"*3yoooz•
1580 A _group_ is a scoped sequence of items.
1582 The <<label,labels>> within a group aren't visible outside of it.
1584 The main purpose of a group is to <<post-item-repetition,repeat>> more
1585 than a single item and to isolate labels.
1589 . The `(`, `!group`, or `!g` opening.
1591 . Zero or more items except, recursively, a macro definition block.
1593 . Depending on the group opening:
1608 ((aa bb cc) dd () ee) "leclerc"
1614 aa bb cc dd ee 6c 65 63 6c 65 72 63 ┆ •••••leclerc
1623 (aa bb cc) * 3 dd ee
1630 aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa bb
1631 cc aa bb cc dd ee aa bb cc aa bb cc aa bb cc dd
1632 ee aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa
1633 bb cc aa bb cc dd ee
1643 <str_beg> u16le"sébastien diaz" <str_end>
1644 [ICITTE - str_beg : 8]
1645 [(end - str_beg) * 5 : 24]
1653 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e•
1654 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 e0 ┆ n• •d•i•a•z•••••
1655 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e•
1656 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 40 ┆ n• •d•i•a•z••••@
1657 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e•
1658 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 00 a0 ┆ n• •d•i•a•z•••••
1662 === Conditional block
1664 A _conditional block_ represents either the bytes of zero or more items
1665 if some expression is true, or the bytes of zero or more other items if
1668 A conditional block is:
1670 . The `!if` opening.
1674 ** The ``pass:[{]`` prefix, a valid {py3} expression of which the
1675 evaluation result type is `int` or `bool` (automatically converted to
1676 `int`), and the `}` suffix.
1678 For a conditional block at some source location{nbsp}__**L**__, this
1679 expression may contain:
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
1688 The value of the special name `ICITTE` (`int` type) in this expression
1689 is the <<cur-offset,current offset>> (before handling the contained
1692 ** A valid {py3} name.
1694 For the name `__NAME__`, this is equivalent to the
1695 `pass:[{]__NAME__}` form above.
1697 . Zero or more items to be handled when the condition is true
1698 except, recursively, a macro definition block.
1702 .. The `!else` opening.
1703 .. Zero or more items to be handled when the condition is false
1704 except, recursively, a macro definition block
1706 . The `!end` closing.
1724 !if {at < rep_count} 20 !end
1733 6d 65 6f 77 20 7a 6f 6f 6d 20 6d 65 6f 77 20 7a ┆ meow zoom meow z
1734 6f 6f 6d 20 6d 65 6f 77 20 7a 6f 6f 6d 20 6d 65 ┆ oom meow zoom me
1735 6f 77 20 6d 69 78 20 6d 65 6f 77 20 6d 69 78 20 ┆ ow mix meow mix
1736 6d 65 6f 77 20 6d 69 78 20 6d 65 6f 77 20 6d 69 ┆ meow mix meow mi
1737 78 20 6d 65 6f 77 20 6d 69 78 20 6d 65 6f 77 20 ┆ x meow mix meow
1750 !if {str_end - str_beg > 10}
1758 6d 00 65 00 6f 00 77 00 20 00 6d 00 69 00 78 00 ┆ m•e•o•w• •m•i•x•
1759 21 00 20 42 49 47 ┆ !• BIG
1763 === Repetition block
1765 A _repetition block_ represents the bytes of one or more items repeated
1766 a given number of times.
1768 A repetition block is:
1770 . The `!repeat` or `!r` opening.
1774 ** A <<const-int,positive constant integer>> which is the number of
1775 times to repeat the previous item.
1777 ** The ``pass:[{]`` prefix, a valid {py3} expression of which the
1778 evaluation result type is `int` or `bool` (automatically converted to
1779 `int`), and the `}` suffix.
1781 For a repetition block at some source location{nbsp}__**L**__, this
1782 expression may contain:
1785 * The name of any <<label,label>> defined before{nbsp}__**L**__
1786 which isn't within a nested group.
1787 * The name of any <<variable-assignment,variable>> known
1791 The value of the special name `ICITTE` (`int` type) in this expression
1792 is the <<cur-offset,current offset>> (before handling the items to
1795 ** A valid {py3} name.
1797 For the name `__NAME__`, this is equivalent to the
1798 `pass:[{]__NAME__}` form above.
1800 . Zero or more items except, recursively, a macro definition block.
1802 . The `!end` closing.
1804 You may also use a <<post-item-repetition,post-item repetition>> after
1805 some items. The form ``!repeat{nbsp}__X__{nbsp}__ITEMS__{nbsp}!end``
1806 is equivalent to ``(__ITEMS__){nbsp}pass:[*]{nbsp}__X__``.
1813 [end - ICITTE - 1 : 8]
1822 ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ ••••••••••••••••
1823 ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ ••••••••••••••••
1824 df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ ••••••••••••••••
1825 cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ ••••••••••••••••
1826 bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ ••••••••••••••••
1827 af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ ••••••••••••••••
1828 9f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ ••••••••••••••••
1829 8f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ ••••••••••••••••
1830 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp
1831 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba`
1832 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP
1833 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@
1834 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210
1835 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"!
1836 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ ••••••••••••••••
1837 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ ••••••••••••••••
1856 11 22 !repeat times 33 !end
1867 aa bb cc dd ee ff ee ff ee ff ee ff ee ff 11 22 ┆ •••••••••••••••"
1868 33 ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ 3•••••••••••••••
1869 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1870 ff ee ff ee ff 11 22 33 33 ee ff ee ff ee ff ee ┆ ••••••"33•••••••
1871 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1872 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1873 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1874 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1875 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1876 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
1877 ff ee ff ee ff ee ff ee ff ee ff ee ff 11 22 33 ┆ ••••••••••••••"3
1878 33 33 63 6f 75 63 6f 75 21 ┆ 33coucou!
1882 === Transformation block
1884 A _transformation block_ represents the bytes of one or more items
1885 transformed into other bytes by a function.
1887 As of this version, Normand only offers a predetermined set of
1888 transformation functions.
1890 An encoded block is:
1892 . The `!transform` or `!t` opening.
1894 . A transformation function name amongst:
1900 Standard https://datatracker.ietf.org/doc/html/rfc4648.html#section-4[Base64].
1904 URL-safe Base64, using `-` instead of `pass:[+]` and `_` instead of
1909 Standard https://datatracker.ietf.org/doc/html/rfc4648.html#section-6[Base32].
1913 Standard https://datatracker.ietf.org/doc/html/rfc4648.html#section-8[Base16].
1917 https://en.wikipedia.org/wiki/Ascii85[Ascii85] without padding.
1921 Ascii85 with padding.
1925 https://en.wikipedia.org/wiki/Ascii85[Base85] (like Git-style binary
1926 diffs) without padding.
1930 Base85 with padding.
1935 https://datatracker.ietf.org/doc/html/rfc2045#section-6.7[quoted-printable]
1936 without quoted whitespaces.
1940 MIME quoted-printable with quoted whitespaces.
1944 https://en.wikipedia.org/wiki/Gzip[gzip].
1948 https://en.wikipedia.org/wiki/Bzip2[bzip2].
1951 . Zero or more items except, recursively, a macro definition block.
1953 Any {py3} expression within any of those items may not refer to a future
1956 The value of the special name `ICITTE` in any {py3} expression within
1957 any of those items is the <<cur-offset,current offset>> _before_ Normand
1958 applies the transformation function. Therefore, labels defined within
1959 those items also have the current offset value _before_ Normand applies
1960 the transformation function.
1962 . The `!end` closing.
1964 The <<cur-offset,current offset>> after having handled the last item of
1965 a transformation block is the value of the current offset before
1966 handling the first item plus the size of the generated (transformed)
1967 bytes. In other words, <<current-offset-setting,current offset
1968 settings>> within the items of the block have no impact outside said
1977 "size of compressed section: " [end - start : 8]
1982 "this will be compressed!"
1994 aa bb cc dd 73 69 7a 65 20 6f 66 20 63 6f 6d 70 ┆ ••••size of comp
1995 72 65 73 73 65 64 20 73 65 63 74 69 6f 6e 3a 20 ┆ ressed section:
1996 52 42 5a 68 39 31 41 59 26 53 59 68 e1 8c fc 00 ┆ RBZh91AY&SYh••••
1997 00 33 d1 e0 c0 00 60 00 5e 66 dc 80 00 20 00 80 ┆ •3••••`•^f••• ••
1998 00 08 20 00 31 40 d3 43 23 26 20 ca 87 a9 a1 e8 ┆ •• •1@•C#& •••••
1999 18 29 44 80 9c 80 49 bf cc b3 e8 45 ed e2 76 ad ┆ •)D•••I••••E••v•
2000 0f 12 8b 8a d6 cd 40 04 7e 2e e4 8a 70 a1 20 d1 ┆ ••••••@•~.••p• •
2001 c3 19 f8 79 65 73 21 ┆ •••yes!
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."
2020 !t qp <beg> [ICITTE - beg : 8] * 50 !end
2026 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 ┆ ••••••••••••••••
2027 38 4b 5f 47 59 2b 43 6f 26 2a 41 54 44 58 25 44 ┆ 8K_GY+Co&*ATDX%D
2028 49 6d 3f 24 46 44 69 3a 32 41 4b 59 4a 72 41 53 ┆ Im?$FDi:2AKYJrAS
2029 23 6d 6f 46 5f 69 31 2f 44 49 61 6c 27 40 3b 70 ┆ #moF_i1/DIal'@;p
2030 31 32 2b 44 47 5e 39 47 41 28 45 2c 41 54 68 58 ┆ 12+DG^9GA(E,AThX
2031 2a 2b 45 4d 37 3d 46 5e 5d 42 2b 44 66 2d 5b 68 ┆ *+EM7=F^]B+Df-[h
2032 2b 44 6b 50 34 2b 44 2c 3e 2a 41 30 3e 60 37 46 ┆ +DkP4+D,>*A0>`7F
2033 28 4b 30 22 2f 67 2a 57 25 45 5a 64 70 72 42 4f ┆ (K0"/g*W%EZdprBO
2034 51 27 71 2b 44 62 55 74 45 63 2c 48 21 2b 45 56 ┆ Q'q+DbUtEc,H!+EV
2035 3a 2a 46 3c 47 5b 3d 41 4b 59 57 2b 41 52 54 5b ┆ :*F<G[=AKYW+ART[
2036 6c 45 5a 66 3d 30 45 63 60 46 42 41 66 75 23 37 ┆ lEZf=0Ec`FBAfu#7
2037 45 5a 66 34 35 46 28 4b 42 3b 2b 45 29 39 43 46 ┆ EZf45F(KB;+E)9CF
2038 60 28 6c 24 45 2c 5d 4e 2f 41 54 4d 6f 38 42 6c ┆ `(l$E,]N/ATMo8Bl
2039 62 44 2d 41 54 56 4c 28 44 2f 21 6d 21 41 30 3e ┆ bD-ATVL(D/!m!A0>
2040 63 2e 46 3c 47 25 3c 2b 45 29 43 43 2b 43 66 2c ┆ c.F<G%<+E)CC+Cf,
2041 2b 40 73 29 58 30 46 43 42 26 73 41 4b 59 48 29 ┆ +@s)X0FCB&sAKYH)
2042 46 3c 47 25 3c 2b 45 29 43 43 2b 43 6f 32 2d 45 ┆ F<G%<+E)CC+Co2-E
2043 2c 54 66 33 46 44 35 5a 32 2f 63 99 99 99 99 99 ┆ ,Tf3FD5Z2/c•••••
2044 3d 30 30 3d 30 31 3d 30 32 3d 30 33 3d 30 34 3d ┆ =00=01=02=03=04=
2045 30 35 3d 30 36 3d 30 37 3d 30 38 3d 30 39 0a 3d ┆ 05=06=07=08=09•=
2046 30 42 3d 30 43 0d 3d 30 45 3d 30 46 3d 31 30 3d ┆ 0B=0C•=0E=0F=10=
2047 31 31 3d 31 32 3d 31 33 3d 31 34 3d 31 35 3d 31 ┆ 11=12=13=14=15=1
2048 36 3d 31 37 3d 31 38 3d 31 39 3d 31 41 3d 31 42 ┆ 6=17=18=19=1A=1B
2049 3d 31 43 3d 31 44 3d 31 45 3d 31 46 20 21 22 23 ┆ =1C=1D=1E=1F !"#
2050 24 25 26 27 28 29 2a 2b 2c 2d 3d 0a 2e 2f 30 31 ┆ $%&'()*+,-=•./01
2054 === Macro definition block
2056 A _macro definition block_ associates a name and parameter names to
2059 A macro definition block doesn't lead to generated bytes itself: a
2060 <<macro-expansion,macro expansion>> does so.
2062 A macro definition may only exist at the root level, that is, not within
2063 a <<group,group>>, a <<repetition-block,repetition block>>, a
2064 <<conditional-block,conditional block>>, or another
2065 <<macro-definition-block,macro definition block>>.
2067 All macro definitions must have unique names.
2069 A macro definition is:
2071 . The `!macro` or `!m` opening.
2073 . A valid {py3} name (the macro name).
2075 . The `(` parameter name list prefix.
2077 . A comma-separated list of zero or more unique parameter names,
2078 each one being a valid {py3} name.
2080 . The `)` parameter name list suffix.
2082 . Zero or more items except, recursively, a macro definition block.
2084 . The `!end` closing.
2089 !le [ICITTE * 8 : 16]
2090 u16le"predict explode"
2097 !macro nail(rep, with_extra, val)
2101 [val + iter : uleb128]
2115 A _macro expansion_ expands the items of a defined
2116 <<macro-definition-block,macro>>.
2118 The macro to expand must be defined _before_ the expansion.
2120 The <<state,state>> before handling the first item of the chosen macro
2123 <<cur-offset,Current offset>>::
2126 <<cur-bo,Current byte order>>::
2130 The only available variables initially are the macro parameters.
2135 The state after having handled the last item of the chosen macro is:
2138 The one before handling the first item of the macro plus the size
2139 of the generated data of the macro expansion.
2141 IMPORTANT: This means <<current-offset-setting,current offset setting>>
2142 items within the expanded macro don't impact the final current offset.
2144 Current byte order::
2145 The one before handling the first item of the macro.
2148 The ones before handling the first item of the macro.
2151 The ones before handling the first item of the macro.
2153 A macro expansion is:
2157 . A valid {py3} name (the name of the macro to expand).
2159 . The `(` parameter value list prefix.
2161 . A comma-separated list of zero or more unique parameter values.
2163 The number of parameter values must match the number of parameter
2164 names of the definition of the chosen macro.
2166 A parameter value is one of:
2169 * A <<const-int,constant integer>>, possibly negative.
2171 * A constant floating point number.
2173 * The ``pass:[{]`` prefix, a valid {py3} expression of which the
2174 evaluation result type is `int` or `bool` (automatically converted to
2175 `int`), and the `}` suffix.
2177 For a macro expansion at some source location{nbsp}__**L**__, this
2178 expression may contain:
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
2186 The value of the special name `ICITTE` (`int` type) in this expression
2187 is the <<cur-offset,current offset>> (before handling the items of the
2190 * A valid {py3} name.
2192 For the name `__NAME__`, this is equivalent to the
2193 `pass:[{]__NAME__pass:[}]` form above.
2196 . The `)` parameter value list suffix.
2203 !le [ICITTE * 8 : 16]
2204 u16le"predict explode"
2207 "hello [" m:bake() "] world"
2215 68 65 6c 6c 6f 20 5b 38 00 70 00 72 00 65 00 64 ┆ hello [8•p•r•e•d
2216 00 69 00 63 00 74 00 20 00 65 00 78 00 70 00 6c ┆ •i•c•t• •e•x•p•l
2217 00 6f 00 64 00 65 00 5d 20 77 6f 72 6c 64 70 01 ┆ •o•d•e•] worldp•
2218 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
2219 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 02 ┆ e•x•p•l•o•d•e•p•
2220 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
2221 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 03 ┆ e•x•p•l•o•d•e•p•
2222 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
2223 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 04 ┆ e•x•p•l•o•d•e•p•
2224 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
2225 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 05 ┆ e•x•p•l•o•d•e•p•
2226 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• •
2227 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 ┆ e•x•p•l•o•d•e•
2235 !macro A(val, is_be)
2245 !macro B(rep, is_be)
2249 m:A({iter * 3}, is_be)
2261 00 03 00 06 00 09 00 0c 00 0f 03 00 06 00 09 00
2269 !macro flt32be(val) !be [val : 32] !end
2279 43 48 45 45 54 4f 53 c2 28 ae 14 3b b8 41 25 ┆ CHEETOS•(••;•A%
2283 === Post-item repetition
2285 A _post-item repetition_ represents the bytes of an item repeated a
2286 given number of times.
2288 A post-item repetition is:
2290 . One of those items:
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>>.
2296 ** A <<string,string>>.
2297 ** A <<macro-expansion,macro-expansion>>.
2298 ** A <<transformation-block,transformation block>>.
2299 ** A <<group,group>>.
2301 . The ``pass:[*]`` character.
2305 ** A positive integer (hexadecimal starting with `0x` or `0X` accepted)
2306 which is the number of times to repeat the previous item.
2308 ** The ``pass:[{]`` prefix, a valid {py3} expression of which the
2309 evaluation result type is `int` or `bool` (automatically converted to
2310 `int`), and the `}` suffix.
2312 For a post-item repetition at some source location{nbsp}__**L**__, this
2313 expression may contain:
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.
2319 * The name of any <<variable-assignment,variable>> known
2320 at{nbsp}__**L**__, which isn't part of its repeated item, and which
2324 The value of the special name `ICITTE` (`int` type) in this expression
2325 is the <<cur-offset,current offset>> (before handling the items to
2328 ** A valid {py3} name.
2330 For the name `__NAME__`, this is equivalent to the
2331 `pass:[{]__NAME__pass:[}]` form above.
2333 You 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``.
2341 [end - ICITTE - 1 : 8] * 0x100 <end>
2347 ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ ••••••••••••••••
2348 ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ ••••••••••••••••
2349 df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ ••••••••••••••••
2350 cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ ••••••••••••••••
2351 bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ ••••••••••••••••
2352 af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ ••••••••••••••••
2353 9f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ ••••••••••••••••
2354 8f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ ••••••••••••••••
2355 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp
2356 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba`
2357 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP
2358 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@
2359 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210
2360 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"!
2361 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ ••••••••••••••••
2362 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ ••••••••••••••••
2374 (ee ff) * {here + 1}
2384 aa bb cc dd ee ff ee ff ee ff ee ff ee ff 11 22 ┆ •••••••••••••••"
2385 33 ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ 3•••••••••••••••
2386 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2387 ff ee ff ee ff 11 22 33 33 ee ff ee ff ee ff ee ┆ ••••••"33•••••••
2388 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2389 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2390 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2391 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2392 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2393 ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ ••••••••••••••••
2394 ff ee ff ee ff ee ff ee ff ee ff ee ff 11 22 33 ┆ ••••••••••••••"3
2395 33 33 63 6f 75 63 6f 75 21 ┆ 33coucou!
2399 == Command-line tool
2401 If you <<install-normand,installed>> the `normand` package, then you
2402 can use the `normand` command-line tool:
2405 $ normand <<< '"ma gang de malades"' | hexdump -C
2409 00000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad|
2413 If you copy the `normand.py` module to your own project, then you can
2414 run the module itself:
2417 $ python3 -m normand <<< '"ma gang de malades"' | hexdump -C
2421 00000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad|
2425 Without a path argument, the `normand` tool reads from the standard
2428 The `normand` tool prints the generated binary data to the standard
2431 Various options control the initial <<state,state>> of the processor:
2432 use the `--help` option to learn more.
2436 The whole `normand` package/module public API is:
2441 class ByteOrder(enum.Enum):
2453 def line_no(self) -> int:
2458 def col_no(self) -> int:
2462 # Parsing error message.
2463 class ParseErrorMessage:
2469 # Source text location.
2471 def text_location(self):
2476 class ParseError(RuntimeError):
2477 # Parsing error messages.
2479 # The first message is the most _specific_ one.
2485 # Variables dictionary type (for type hints).
2486 VariablesT = typing.Dict[str, typing.Union[int, float]]
2489 # Labels dictionary type (for type hints).
2490 LabelsT = typing.Dict[str, int]
2497 def data(self) -> bytearray:
2500 # Updated variable values.
2502 def variables(self) -> SymbolsT:
2505 # Updated main group label values.
2507 def labels(self) -> SymbolsT:
2512 def offset(self) -> int:
2517 def byte_order(self) -> typing.Optional[ByteOrder]:
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.
2524 def parse(normand: str,
2525 init_variables: typing.Optional[SymbolsT] = None,
2526 init_labels: typing.Optional[SymbolsT] = None,
2527 init_offset: int = 0,
2528 init_byte_order: typing.Optional[ByteOrder] = None) -> ParseResult:
2532 The `normand` parameter is the actual <<learn-normand,Normand input>>
2533 while the other parameters control the initial <<state,state>>.
2535 The `parse()` function raises a `ParseError` instance should it fail to
2536 parse the `normand` string for any reason.
2540 Normand is a https://python-poetry.org/[Poetry] project.
2542 To develop it, install it through Poetry and enter the virtual
2548 $ normand <<< '"lol" * 10 0a'
2551 `normand.py` is processed by:
2553 * https://microsoft.github.io/pyright/[Pyright]
2554 * https://github.com/psf/black[Black]
2555 * https://pycqa.github.io/isort/[isort]
2559 Use https://docs.pytest.org/[pytest] to test Normand once the package is
2560 part of your virtual environment, for example:
2564 $ poetry run pip3 install pytest
2568 The `pytest` project is currently not a development dependency in
2569 `pyproject.toml` due to backward compatibiliy issues with
2572 In the `tests` directory, each `*.nt` file is a test. The file name
2573 prefix indicates what it's meant to test:
2576 Everything above the `---` line is the valid Normand input
2579 Everything below the `---` line is the expected data
2580 (whitespace-separated hexadecimal bytes).
2583 Everything above the `---` line is the invalid Normand input
2586 Everything below the `---` line is the expected error message having
2595 Normand uses https://review.lttng.org/admin/repos/normand,general[Gerrit]
2598 To report a bug, https://github.com/efficios/normand/issues/new[create a