| 1 | // Show ToC at a specific location for a GitHub rendering |
| 2 | ifdef::env-github[] |
| 3 | :toc: macro |
| 4 | endif::env-github[] |
| 5 | |
| 6 | ifndef::env-github[] |
| 7 | :toc: left |
| 8 | endif::env-github[] |
| 9 | |
| 10 | // This is to mimic what GitHub does so that anchors work in an offline |
| 11 | // rendering too. |
| 12 | :idprefix: |
| 13 | :idseparator: - |
| 14 | |
| 15 | // Other attributes |
| 16 | :py3: Python{nbsp}3 |
| 17 | |
| 18 | = Normand |
| 19 | Philippe Proulx |
| 20 | |
| 21 | image::normand-logo.png[] |
| 22 | |
| 23 | [.normal] |
| 24 | image:https://img.shields.io/pypi/v/normand.svg?label=Latest%20version[link="https://pypi.python.org/pypi/normand"] |
| 25 | |
| 26 | [.lead] |
| 27 | _**Normand**_ is a text-to-binary processor with its own language. |
| 28 | |
| 29 | This package offers both a portable {py3} module and a command-line |
| 30 | tool. |
| 31 | |
| 32 | WARNING: This version of Normand is 0.16, meaning both the Normand |
| 33 | language and the module/CLI interface aren't stable. |
| 34 | |
| 35 | ifdef::env-github[] |
| 36 | // ToC location for a GitHub rendering |
| 37 | toc::[] |
| 38 | endif::env-github[] |
| 39 | |
| 40 | == Introduction |
| 41 | |
| 42 | The purpose of Normand is to consume human-readable text representing |
| 43 | bytes and to produce the corresponding binary data. |
| 44 | |
| 45 | .Simple bytes input. |
| 46 | ==== |
| 47 | Consider the following Normand input: |
| 48 | |
| 49 | ---- |
| 50 | 4f 55 32 bb $167 fe %10100111 a9 $-32 |
| 51 | ---- |
| 52 | |
| 53 | The generated nine bytes are: |
| 54 | |
| 55 | ---- |
| 56 | 4f 55 32 bb a7 fe a7 a9 e0 |
| 57 | ---- |
| 58 | ==== |
| 59 | |
| 60 | As you can see in the last example, the fundamental unit of the Normand |
| 61 | language is the _byte_. The order in which you list bytes will be the |
| 62 | order of the generated data. |
| 63 | |
| 64 | The Normand language is more than simple lists of bytes, though. Its |
| 65 | main features are: |
| 66 | |
| 67 | Comments, including a bunch of insignificant symbols which may improve readability:: |
| 68 | + |
| 69 | Input: |
| 70 | + |
| 71 | ---- |
| 72 | ff bb %1101:0010 # This is a comment |
| 73 | 78 29 af $192 # This too # 99 $-80 |
| 74 | fe80::6257:18ff:fea3:4229 |
| 75 | 60:57:18:a3:42:29 |
| 76 | 10839636-5d65-4a68-8e6a-21608ddf7258 |
| 77 | ---- |
| 78 | + |
| 79 | Output: |
| 80 | + |
| 81 | ---- |
| 82 | ff bb d2 78 29 af c0 99 b0 fe 80 62 57 18 ff fe |
| 83 | a3 42 29 60 57 18 a3 42 29 10 83 96 36 5d 65 4a |
| 84 | 68 8e 6a 21 60 8d df 72 58 |
| 85 | ---- |
| 86 | |
| 87 | Hexadecimal, decimal, and binary byte constants:: |
| 88 | + |
| 89 | Input: |
| 90 | + |
| 91 | ---- |
| 92 | aa bb $247 $-89 %0011_0010 %11.01= 10/10 |
| 93 | ---- |
| 94 | + |
| 95 | Output: |
| 96 | + |
| 97 | ---- |
| 98 | aa bb f7 a7 32 da |
| 99 | ---- |
| 100 | |
| 101 | UTF-8, UTF-16, and UTF-32 literal strings:: |
| 102 | + |
| 103 | Input: |
| 104 | + |
| 105 | ---- |
| 106 | "hello world!" 00 |
| 107 | u16le"stress\nverdict 🤣" |
| 108 | ---- |
| 109 | + |
| 110 | Output: |
| 111 | + |
| 112 | ---- |
| 113 | 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 00 73 00 74 ┆ hello world!•s•t |
| 114 | 00 72 00 65 00 73 00 73 00 0a 00 76 00 65 00 72 ┆ •r•e•s•s•••v•e•r |
| 115 | 00 64 00 69 00 63 00 74 00 20 00 3e d8 23 dd ┆ •d•i•c•t• •>•#• |
| 116 | ---- |
| 117 | |
| 118 | Labels: special variables holding the offset where they're defined:: |
| 119 | + |
| 120 | ---- |
| 121 | <beg> b2 52 e3 bc 91 05 |
| 122 | $100 $50 <chair> 33 9f fe |
| 123 | 25 e9 89 8a <end> |
| 124 | ---- |
| 125 | |
| 126 | Variables:: |
| 127 | + |
| 128 | ---- |
| 129 | 5e 65 {tower = 47} c6 7f f2 c4 |
| 130 | 44 {hurl = tower - 14} b5 {tower = hurl} 26 2d |
| 131 | ---- |
| 132 | + |
| 133 | The value of a variable assignment is the evaluation of a valid {py3} |
| 134 | expression which may include label and variable names. |
| 135 | |
| 136 | Fixed-length number with a given length (8{nbsp}bits to 64{nbsp}bits) and byte order:: |
| 137 | + |
| 138 | Input: |
| 139 | + |
| 140 | ---- |
| 141 | {strength = 4} |
| 142 | {be} 67 <lbl> 44 $178 {(end - lbl) * 8 + strength : 16} $99 <end> |
| 143 | {le} {-1993 : 32} |
| 144 | {-3.141593 : 64} |
| 145 | ---- |
| 146 | + |
| 147 | Output: |
| 148 | + |
| 149 | ---- |
| 150 | 67 44 b2 00 2c 63 37 f8 ff ff 7f bd c2 82 fb 21 |
| 151 | 09 c0 |
| 152 | ---- |
| 153 | + |
| 154 | The encoded number is the evaluation of a valid {py3} expression which |
| 155 | may include label and variable names. |
| 156 | |
| 157 | https://en.wikipedia.org/wiki/LEB128[LEB128] integer:: |
| 158 | + |
| 159 | Input: |
| 160 | + |
| 161 | ---- |
| 162 | aa bb cc {-1993 : sleb128} <meow> dd ee ff |
| 163 | {meow * 199 : uleb128} |
| 164 | ---- |
| 165 | + |
| 166 | Output: |
| 167 | + |
| 168 | ---- |
| 169 | aa bb cc b7 70 dd ee ff e3 07 |
| 170 | ---- |
| 171 | + |
| 172 | The encoded integer is the evaluation of a valid {py3} expression which |
| 173 | may include label and variable names. |
| 174 | |
| 175 | Conditional:: |
| 176 | + |
| 177 | Input: |
| 178 | + |
| 179 | ---- |
| 180 | aa bb cc |
| 181 | |
| 182 | ( |
| 183 | "foo" |
| 184 | |
| 185 | !if {ICITTE > 10} |
| 186 | "bar" |
| 187 | !else |
| 188 | "fight" |
| 189 | !end |
| 190 | ) * 4 |
| 191 | ---- |
| 192 | + |
| 193 | Output: |
| 194 | + |
| 195 | ---- |
| 196 | aa bb cc 66 6f 6f 66 69 67 68 74 66 6f 6f 66 69 ┆ •••foofightfoofi |
| 197 | 67 68 74 66 6f 6f 62 61 72 66 6f 6f 62 61 72 ┆ ghtfoobarfoobar |
| 198 | ---- |
| 199 | |
| 200 | Repetition:: |
| 201 | + |
| 202 | Input: |
| 203 | + |
| 204 | ---- |
| 205 | aa bb * 5 cc <zoom> "yeah\0" * {zoom * 3} |
| 206 | |
| 207 | !repeat 3 |
| 208 | ff ee "juice" |
| 209 | !end |
| 210 | ---- |
| 211 | + |
| 212 | Output: |
| 213 | + |
| 214 | ---- |
| 215 | aa bb bb bb bb bb cc 79 65 61 68 00 79 65 61 68 ┆ •••••••yeah•yeah |
| 216 | 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah• |
| 217 | 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 79 ┆ yeah•yeah•yeah•y |
| 218 | 65 61 68 00 79 65 61 68 00 79 65 61 68 00 79 65 ┆ eah•yeah•yeah•ye |
| 219 | 61 68 00 79 65 61 68 00 79 65 61 68 00 79 65 61 ┆ ah•yeah•yeah•yea |
| 220 | 68 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 ┆ h•yeah•yeah•yeah |
| 221 | 00 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah• |
| 222 | ff ee 6a 75 69 63 65 ff ee 6a 75 69 63 65 ff ee ┆ ••juice••juice•• |
| 223 | 6a 75 69 63 65 ┆ juice |
| 224 | ---- |
| 225 | |
| 226 | Alignment:: |
| 227 | + |
| 228 | Input: |
| 229 | + |
| 230 | ---- |
| 231 | {be} |
| 232 | |
| 233 | {199:32} |
| 234 | @64 {43:64} |
| 235 | @16 {-123:16} |
| 236 | @32~255 {5584:32} |
| 237 | ---- |
| 238 | + |
| 239 | Output: |
| 240 | + |
| 241 | ---- |
| 242 | 00 00 00 c7 00 00 00 00 00 00 00 00 00 00 00 2b |
| 243 | ff 85 ff ff 00 00 15 d0 |
| 244 | ---- |
| 245 | |
| 246 | Filling:: |
| 247 | + |
| 248 | Input: |
| 249 | + |
| 250 | ---- |
| 251 | {le} |
| 252 | {0xdeadbeef:32} |
| 253 | {-1993:16} |
| 254 | {9:16} |
| 255 | +0x40 |
| 256 | {ICITTE:8} |
| 257 | "meow mix" |
| 258 | +200~FFh |
| 259 | {ICITTE:8} |
| 260 | ---- |
| 261 | + |
| 262 | Output: |
| 263 | + |
| 264 | ---- |
| 265 | ef be ad de 37 f8 09 00 00 00 00 00 00 00 00 00 ┆ ••••7••••••••••• |
| 266 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ •••••••••••••••• |
| 267 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ •••••••••••••••• |
| 268 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ •••••••••••••••• |
| 269 | 40 6d 65 6f 77 20 6d 69 78 ff ff ff ff ff ff ff ┆ @meow mix••••••• |
| 270 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ •••••••••••••••• |
| 271 | ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ┆ •••••••••••••••• |
| 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 c8 ┆ ••••••••• |
| 278 | ---- |
| 279 | |
| 280 | Multilevel grouping:: |
| 281 | + |
| 282 | Input: |
| 283 | + |
| 284 | ---- |
| 285 | ff ((aa bb "zoom" cc) * 5) * 3 $-34 * 4 |
| 286 | ---- |
| 287 | + |
| 288 | Output: |
| 289 | + |
| 290 | ---- |
| 291 | ff aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa ┆ •••zoom•••zoom•• |
| 292 | bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a ┆ •zoom•••zoom•••z |
| 293 | 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f ┆ oom•••zoom•••zoo |
| 294 | 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc ┆ m•••zoom•••zoom• |
| 295 | aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb ┆ ••zoom•••zoom••• |
| 296 | 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f ┆ zoom•••zoom•••zo |
| 297 | 6f 6d cc aa bb 7a 6f 6f 6d cc de de de de ┆ om•••zoom••••• |
| 298 | ---- |
| 299 | |
| 300 | Macros:: |
| 301 | + |
| 302 | Input: |
| 303 | + |
| 304 | ---- |
| 305 | !macro hello(world) |
| 306 | "hello" |
| 307 | !if world " world" !end |
| 308 | !end |
| 309 | |
| 310 | !repeat 17 |
| 311 | ff ff ff ff |
| 312 | m:hello({ICITTE > 15 and ICITTE < 60}) |
| 313 | !end |
| 314 | ---- |
| 315 | + |
| 316 | Output: |
| 317 | + |
| 318 | ---- |
| 319 | ff ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c ┆ ••••hello••••hel |
| 320 | 6c 6f ff ff ff ff 68 65 6c 6c 6f 20 77 6f 72 6c ┆ lo••••hello worl |
| 321 | 64 ff ff ff ff 68 65 6c 6c 6f 20 77 6f 72 6c 64 ┆ d••••hello world |
| 322 | ff ff ff ff 68 65 6c 6c 6f 20 77 6f 72 6c 64 ff ┆ ••••hello world• |
| 323 | ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c 6c ┆ •••hello••••hell |
| 324 | 6f ff ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 ┆ o••••hello••••he |
| 325 | 6c 6c 6f ff ff ff ff 68 65 6c 6c 6f ff ff ff ff ┆ llo••••hello•••• |
| 326 | 68 65 6c 6c 6f ff ff ff ff 68 65 6c 6c 6f ff ff ┆ hello••••hello•• |
| 327 | ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c 6c 6f ┆ ••hello••••hello |
| 328 | ff ff ff ff 68 65 6c 6c 6f ff ff ff ff 68 65 6c ┆ ••••hello••••hel |
| 329 | 6c 6f ff ff ff ff 68 65 6c 6c 6f ┆ lo••••hello |
| 330 | ---- |
| 331 | |
| 332 | Precise error reporting:: |
| 333 | + |
| 334 | ---- |
| 335 | /tmp/meow.normand:10:24 - Expecting a bit (`0` or `1`). |
| 336 | ---- |
| 337 | + |
| 338 | ---- |
| 339 | /tmp/meow.normand:32:6 - Unexpected character `k`. |
| 340 | ---- |
| 341 | + |
| 342 | ---- |
| 343 | /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`}. |
| 344 | ---- |
| 345 | + |
| 346 | ---- |
| 347 | /tmp/meow.normand:32:19 - While expanding the macro `meow`: |
| 348 | /tmp/meow.normand:35:5 - While expanding the macro `zzz`: |
| 349 | /tmp/meow.normand:18:9 - Value 315 is outside the 8-bit range when evaluating expression `end - ICITTE`. |
| 350 | ---- |
| 351 | |
| 352 | You can use Normand to track data source files in your favorite VCS |
| 353 | instead of raw binary files. The binary files that Normand generates can |
| 354 | be used to test file format decoding, including malformatted data, for |
| 355 | example, as well as for education. |
| 356 | |
| 357 | See <<learn-normand>> to explore all the Normand features. |
| 358 | |
| 359 | == Install Normand |
| 360 | |
| 361 | Normand requires Python ≥ 3.4. |
| 362 | |
| 363 | To install Normand: |
| 364 | |
| 365 | ---- |
| 366 | $ python3 -m pip install --user normand |
| 367 | ---- |
| 368 | |
| 369 | See |
| 370 | https://packaging.python.org/en/latest/tutorials/installing-packages/#installing-to-the-user-site[Installing to the User Site] |
| 371 | to learn more about a user site installation. |
| 372 | |
| 373 | [NOTE] |
| 374 | ==== |
| 375 | Normand has a single module file, `normand.py`, which you can copy as is |
| 376 | to your project to use it (both the <<python3-api,`normand.parse()`>> |
| 377 | function and the <<command-line-tool,command-line tool>>). |
| 378 | |
| 379 | `normand.py` has _no external dependencies_, but if you're using |
| 380 | Python{nbsp}3.4, you'll need a local copy of the standard `typing` |
| 381 | module. |
| 382 | ==== |
| 383 | |
| 384 | == Design goals |
| 385 | |
| 386 | The design goals of Normand are: |
| 387 | |
| 388 | Portability:: |
| 389 | We're making sure `normand.py` works with Python{nbsp}≥{nbsp}3.4 and |
| 390 | doesn't have any external dependencies so that you may just copy the |
| 391 | module as is to your own project. |
| 392 | |
| 393 | Ease of use:: |
| 394 | The most basic Normand input is a sequence of hexadecimal constants |
| 395 | (for example, `4e6f726d616e64`) which produce exactly what you'd |
| 396 | expect. |
| 397 | + |
| 398 | Most Normand features map to programming language concepts you already |
| 399 | know and understand: constant integers, literal strings, variables, |
| 400 | conditionals, repetitions/loops, and the rest. |
| 401 | |
| 402 | Concise and readable input:: |
| 403 | We could have chosen XML or YAML as the input format, but having a |
| 404 | DSL here makes a Normand input compact and easy to read, two |
| 405 | important traits when using Normand to write tests, for example. |
| 406 | + |
| 407 | Compare the following Normand input and some hypothetical XML |
| 408 | equivalent, for example: |
| 409 | + |
| 410 | .Actual normand input. |
| 411 | ---- |
| 412 | ff dd 01 ab $192 $-128 %1101:0011 |
| 413 | |
| 414 | {end:8} |
| 415 | |
| 416 | {iter = 1} |
| 417 | |
| 418 | !if {not something} |
| 419 | # five times because xyz |
| 420 | !repeat 5 |
| 421 | "hello world " {iter:8} |
| 422 | {iter = iter + 1} |
| 423 | !end |
| 424 | !end |
| 425 | |
| 426 | <end> |
| 427 | ---- |
| 428 | + |
| 429 | .Hypothetical Normand XML input. |
| 430 | [source,xml] |
| 431 | ---- |
| 432 | <?xml version="1.0" encoding="utf-8" ?> |
| 433 | <group> |
| 434 | <byte base="x" val="ff" /> |
| 435 | <byte base="x" val="dd" /> |
| 436 | <byte base="x" val="1" /> |
| 437 | <byte base="x" val="ab" /> |
| 438 | <byte base="d" val="192" /> |
| 439 | <byte base="d" val="-128" /> |
| 440 | <byte base="b" val="11010011" /> |
| 441 | <fixed-len-num expr="end" len="8" /> |
| 442 | <var-assign name="iter" expr="1" /> |
| 443 | <cond expr="not something"> |
| 444 | <!-- five times because xyz --> |
| 445 | <repeat expr="5"> |
| 446 | <str>hello world </str> |
| 447 | <fixed-len-num expr="iter" len="8" /> |
| 448 | <var-assign name="iter" expr="iter + 1" /> |
| 449 | </repeat> |
| 450 | </cond> |
| 451 | <label name="end" /> |
| 452 | </group> |
| 453 | ---- |
| 454 | |
| 455 | == Learn Normand |
| 456 | |
| 457 | A Normand text input is a sequence of items which represent a sequence |
| 458 | of raw bytes. |
| 459 | |
| 460 | [[state]] During the processing of items to data, Normand relies on a |
| 461 | current state: |
| 462 | |
| 463 | [%header%autowidth] |
| 464 | |=== |
| 465 | |State variable |Description |Initial value: <<python3-api,{py3} API>> |Initial value: <<command-line-tool,CLI>> |
| 466 | |
| 467 | |[[cur-offset]] Current offset |
| 468 | | |
| 469 | The current offset has an effect on the value of <<label,labels>> and of |
| 470 | the special `ICITTE` name in <<fixed-length-number,fixed-length |
| 471 | number>>, <<leb-128-integer,LEB128 integer>>, |
| 472 | <<filling,filling>>, <<variable-assignment,variable assignment>>, |
| 473 | <<conditional-block,conditional block>>, <<repetition-block,repetition |
| 474 | block>>, <<macro-expansion,macro expansion>>, and |
| 475 | <<post-item-repetition,post-item repetition>> expression evaluation. |
| 476 | |
| 477 | Each generated byte increments the current offset. |
| 478 | |
| 479 | A <<current-offset-setting,current offset setting>> may change the |
| 480 | current offset without generating data. |
| 481 | |
| 482 | An <<current-offset-alignment,current offset alignment>> generates |
| 483 | padding bytes to make the current offset satisfy a given alignment. |
| 484 | |`init_offset` parameter of the `parse()` function. |
| 485 | |`--offset` option. |
| 486 | |
| 487 | |[[cur-bo]] Current byte order |
| 488 | | |
| 489 | The current byte order has an effect on the encoding of |
| 490 | <<fixed-length-number,fixed-length numbers>>. |
| 491 | |
| 492 | A <<current-byte-order-setting,current byte order setting>> may change |
| 493 | the current byte order. |
| 494 | |`init_byte_order` parameter of the `parse()` function. |
| 495 | |`--byte-order` option. |
| 496 | |
| 497 | |<<label,Labels>> |
| 498 | |Mapping of label names to integral values. |
| 499 | |`init_labels` parameter of the `parse()` function. |
| 500 | |One or more `--label` options. |
| 501 | |
| 502 | |<<variable-assignment,Variables>> |
| 503 | |Mapping of variable names to integral or floating point number values. |
| 504 | |`init_variables` parameter of the `parse()` function. |
| 505 | |One or more `--var` options. |
| 506 | |=== |
| 507 | |
| 508 | The available items are: |
| 509 | |
| 510 | * A <<byte-constant,constant integer>> representing one or more |
| 511 | constant bytes. |
| 512 | |
| 513 | * A <<literal-string,literal string>> representing a sequence of bytes |
| 514 | encoding UTF-8, UTF-16, or UTF-32 data. |
| 515 | |
| 516 | * A <<current-byte-order-setting,current byte order setting>> (big or |
| 517 | little endian). |
| 518 | |
| 519 | * A <<fixed-length-number,fixed-length number>> (integer or |
| 520 | floating point) using the <<cur-bo,current byte order>> and of which |
| 521 | the value is the result of a {py3} expression. |
| 522 | |
| 523 | * An <<leb128-integer,LEB128 integer>> of which the value is the result |
| 524 | of a {py3} expression. |
| 525 | |
| 526 | * A <<current-offset-setting,current offset setting>>. |
| 527 | |
| 528 | * A <<current-offset-alignment,current offset alignment>>. |
| 529 | |
| 530 | * A <<filling,filling>>. |
| 531 | |
| 532 | * A <<label,label>>, that is, a named constant holding the current |
| 533 | offset. |
| 534 | + |
| 535 | This is similar to an assembly label. |
| 536 | |
| 537 | * A <<variable-assignment,variable assignment>> associating a name to |
| 538 | the integral result of an evaluated {py3} expression. |
| 539 | |
| 540 | * A <<group,group>>, that is, a scoped sequence of items. |
| 541 | |
| 542 | * A <<conditional-block,conditional block>>. |
| 543 | |
| 544 | * A <<repetition-block,repetition block>>. |
| 545 | |
| 546 | * A <<macro-definition-block,macro definition block>>. |
| 547 | |
| 548 | * A <<macro-expansion,macro expansion>>. |
| 549 | |
| 550 | Moreover, you can repeat many items above a constant or variable number |
| 551 | of times with the ``pass:[*]`` operator _after_ the item to repeat. This |
| 552 | is called a <<post-item-repetition,post-item repetition>>. |
| 553 | |
| 554 | A Normand comment may exist: |
| 555 | |
| 556 | * Between items, possibly within a group. |
| 557 | * Between the nibbles of a constant hexadecimal byte. |
| 558 | * Between the bits of a constant binary byte. |
| 559 | * Between the last item and the ``pass:[*]`` character of a post-item |
| 560 | repetition, and between that ``pass:[*]`` character and the following |
| 561 | number or expression. |
| 562 | * Between the ``!repeat``/``!r`` block opening and the following |
| 563 | constant integer, name, or expression of a repetition block. |
| 564 | * Between the ``!if`` block opening and the following name or expression |
| 565 | of a conditional block. |
| 566 | |
| 567 | A comment is anything between two ``pass:[#]`` characters on the same |
| 568 | line, or from ``pass:[#]`` until the end of the line. Whitespaces and |
| 569 | the following symbol characters are also considered comments where a |
| 570 | comment may exist: |
| 571 | |
| 572 | ---- |
| 573 | / \ ? & : ; . , [ ] _ = | - |
| 574 | ---- |
| 575 | |
| 576 | The latter serve to improve readability so that you may write, for |
| 577 | example, a MAC address or a UUID as is. |
| 578 | |
| 579 | [[const-int]] Many items require a _constant integer_, possibly |
| 580 | negative, in which case it may start with `-` for a negative integer. A |
| 581 | positive constant integer is any of: |
| 582 | |
| 583 | Decimal:: |
| 584 | One or mode digits (`0` to `9`). |
| 585 | |
| 586 | Hexadecimal:: |
| 587 | One of: |
| 588 | + |
| 589 | * The `0x` or `0X` prefix followed with one or more hexadecimal digits |
| 590 | (`0` to `9`, `a` to `f`, or `A` to `F`). |
| 591 | * One or more hexadecimal digits followed with the `h` or `H` suffix. |
| 592 | |
| 593 | Octal:: |
| 594 | One of: |
| 595 | + |
| 596 | * The `0o` or `0O` prefix followed with one or more octal digits |
| 597 | (`0` to `7`). |
| 598 | * One or more octal digits followed with the `o`, `O`, `q`, or `Q` |
| 599 | suffix. |
| 600 | |
| 601 | Binary:: |
| 602 | One of: |
| 603 | + |
| 604 | * The `0b` or `0B` prefix followed with one or more bits (`0` or `1`). |
| 605 | * One or more bits followed with the `b` or `B` suffix. |
| 606 | |
| 607 | You can test the examples of this section with the `normand` |
| 608 | <<command-line-tool,command-line tool>> as such: |
| 609 | |
| 610 | ---- |
| 611 | $ normand file | hexdump -C |
| 612 | ---- |
| 613 | |
| 614 | where `file` is the name of a file containing the Normand input. |
| 615 | |
| 616 | === Byte constant |
| 617 | |
| 618 | A _byte constant_ represents one or more constant bytes. |
| 619 | |
| 620 | A byte constant is: |
| 621 | |
| 622 | Hexadecimal form:: |
| 623 | Two consecutive hexadecimal digits representing a single byte. |
| 624 | |
| 625 | Decimal form:: |
| 626 | One or more digits after the `$` prefix representing a single byte. |
| 627 | |
| 628 | Binary form:: {empty} |
| 629 | + |
| 630 | -- |
| 631 | . __**N**__ `%` prefixes (at least one). |
| 632 | + |
| 633 | The number of `%` characters is the number of subsequent expected bytes. |
| 634 | |
| 635 | . __**N**__{nbsp}×{nbsp}8 bits (`0` or `1`). |
| 636 | -- |
| 637 | |
| 638 | ==== |
| 639 | Input: |
| 640 | |
| 641 | ---- |
| 642 | ab cd [3d 8F] CC |
| 643 | ---- |
| 644 | |
| 645 | Output: |
| 646 | |
| 647 | ---- |
| 648 | ab cd 3d 8f cc |
| 649 | ---- |
| 650 | ==== |
| 651 | |
| 652 | ==== |
| 653 | Input: |
| 654 | |
| 655 | ---- |
| 656 | $192 %1100/0011 $ -77 |
| 657 | ---- |
| 658 | |
| 659 | Output: |
| 660 | |
| 661 | ---- |
| 662 | c0 c3 b3 |
| 663 | ---- |
| 664 | ==== |
| 665 | |
| 666 | ==== |
| 667 | Input: |
| 668 | |
| 669 | ---- |
| 670 | 58f64689-6316-4d55-8a1a-04cada366172 |
| 671 | fe80::6257:18ff:fea3:4229 |
| 672 | ---- |
| 673 | |
| 674 | Output: |
| 675 | |
| 676 | ---- |
| 677 | 58 f6 46 89 63 16 4d 55 8a 1a 04 ca da 36 61 72 ┆ X•F•c•MU•••••6ar |
| 678 | fe 80 62 57 18 ff fe a3 42 29 ┆ ••bW••••B) |
| 679 | ---- |
| 680 | ==== |
| 681 | |
| 682 | ==== |
| 683 | Input: |
| 684 | |
| 685 | ---- |
| 686 | %01110011 %01100001 %01101100 %01110101 %01110100 |
| 687 | %%%1101:0010 11111111 #A#11 #B#00 #C#011 #D#1 |
| 688 | ---- |
| 689 | |
| 690 | Output: |
| 691 | |
| 692 | ---- |
| 693 | 73 61 6c 75 74 d2 ff c7 ┆ salut••• |
| 694 | ---- |
| 695 | ==== |
| 696 | |
| 697 | === Literal string |
| 698 | |
| 699 | A _literal string_ represents the UTF-8-, UTF-16-, or UTF-32-encoded |
| 700 | bytes of a string. |
| 701 | |
| 702 | The string to encode isn't implicitly null-terminated: use `\0` at the |
| 703 | end of the string to add a null character. |
| 704 | |
| 705 | A literal string is: |
| 706 | |
| 707 | . **Optional**: one of the following encodings instead of UTF-8: |
| 708 | + |
| 709 | -- |
| 710 | [horizontal] |
| 711 | `u16be`:: UTF-16BE. |
| 712 | `u16le`:: UTF-16LE. |
| 713 | `u32be`:: UTF-32BE. |
| 714 | `u32le`:: UTF-32LE. |
| 715 | -- |
| 716 | |
| 717 | . The ``pass:["]`` prefix. |
| 718 | |
| 719 | . A sequence of zero or more characters, possibly containing escape |
| 720 | sequences. |
| 721 | + |
| 722 | An escape sequence is the ``\`` character followed by one of: |
| 723 | + |
| 724 | -- |
| 725 | [horizontal] |
| 726 | `0`:: Null (U+0000) |
| 727 | `a`:: Alert (U+0007) |
| 728 | `b`:: Backspace (U+0008) |
| 729 | `e`:: Escape (U+001B) |
| 730 | `f`:: Form feed (U+000C) |
| 731 | `n`:: End of line (U+000A) |
| 732 | `r`:: Carriage return (U+000D) |
| 733 | `t`:: Character tabulation (U+0009) |
| 734 | `v`:: Line tabulation (U+000B) |
| 735 | ``\``:: Reverse solidus (U+005C) |
| 736 | ``pass:["]``:: Quotation mark (U+0022) |
| 737 | -- |
| 738 | |
| 739 | . The ``pass:["]`` suffix. |
| 740 | |
| 741 | ==== |
| 742 | Input: |
| 743 | |
| 744 | ---- |
| 745 | "coucou tout le monde!" |
| 746 | ---- |
| 747 | |
| 748 | Output: |
| 749 | |
| 750 | ---- |
| 751 | 63 6f 75 63 6f 75 20 74 6f 75 74 20 6c 65 20 6d ┆ coucou tout le m |
| 752 | 6f 6e 64 65 21 ┆ onde! |
| 753 | ---- |
| 754 | ==== |
| 755 | |
| 756 | ==== |
| 757 | Input: |
| 758 | |
| 759 | ---- |
| 760 | u16le"I am not young enough to know everything." |
| 761 | ---- |
| 762 | |
| 763 | Output: |
| 764 | |
| 765 | ---- |
| 766 | 49 00 20 00 61 00 6d 00 20 00 6e 00 6f 00 74 00 ┆ I• •a•m• •n•o•t• |
| 767 | 20 00 79 00 6f 00 75 00 6e 00 67 00 20 00 65 00 ┆ •y•o•u•n•g• •e• |
| 768 | 6e 00 6f 00 75 00 67 00 68 00 20 00 74 00 6f 00 ┆ n•o•u•g•h• •t•o• |
| 769 | 20 00 6b 00 6e 00 6f 00 77 00 20 00 65 00 76 00 ┆ •k•n•o•w• •e•v• |
| 770 | 65 00 72 00 79 00 74 00 68 00 69 00 6e 00 67 00 ┆ e•r•y•t•h•i•n•g• |
| 771 | 2e 00 ┆ .• |
| 772 | ---- |
| 773 | ==== |
| 774 | |
| 775 | ==== |
| 776 | Input: |
| 777 | |
| 778 | ---- |
| 779 | u32be "\"illusion is the first\nof all pleasures\" 🦉" |
| 780 | ---- |
| 781 | |
| 782 | Output: |
| 783 | |
| 784 | ---- |
| 785 | 00 00 00 22 00 00 00 69 00 00 00 6c 00 00 00 6c ┆ •••"•••i•••l•••l |
| 786 | 00 00 00 75 00 00 00 73 00 00 00 69 00 00 00 6f ┆ •••u•••s•••i•••o |
| 787 | 00 00 00 6e 00 00 00 20 00 00 00 69 00 00 00 73 ┆ •••n••• •••i•••s |
| 788 | 00 00 00 20 00 00 00 74 00 00 00 68 00 00 00 65 ┆ ••• •••t•••h•••e |
| 789 | 00 00 00 20 00 00 00 66 00 00 00 69 00 00 00 72 ┆ ••• •••f•••i•••r |
| 790 | 00 00 00 73 00 00 00 74 00 00 00 0a 00 00 00 6f ┆ •••s•••t•••••••o |
| 791 | 00 00 00 66 00 00 00 20 00 00 00 61 00 00 00 6c ┆ •••f••• •••a•••l |
| 792 | 00 00 00 6c 00 00 00 20 00 00 00 70 00 00 00 6c ┆ •••l••• •••p•••l |
| 793 | 00 00 00 65 00 00 00 61 00 00 00 73 00 00 00 75 ┆ •••e•••a•••s•••u |
| 794 | 00 00 00 72 00 00 00 65 00 00 00 73 00 00 00 22 ┆ •••r•••e•••s•••" |
| 795 | 00 00 00 20 00 01 f9 89 ┆ ••• •••• |
| 796 | ---- |
| 797 | ==== |
| 798 | |
| 799 | === Current byte order setting |
| 800 | |
| 801 | This special item sets the <<cur-bo,_current byte order_>>. |
| 802 | |
| 803 | The two accepted forms are: |
| 804 | |
| 805 | [horizontal] |
| 806 | ``pass:[{be}]``:: Set the current byte order to big endian. |
| 807 | ``pass:[{le}]``:: Set the current byte order to little endian. |
| 808 | |
| 809 | === Fixed-length number |
| 810 | |
| 811 | A _fixed-length number_ represents a fixed number of bytes encoding |
| 812 | either: |
| 813 | |
| 814 | * An unsigned or signed integer (two's complement). |
| 815 | + |
| 816 | The available lengths are 8, 16, 24, 32, 40, 48, 56, and 64. |
| 817 | |
| 818 | * A floating point number |
| 819 | (https://standards.ieee.org/standard/754-2008.html[IEEE{nbsp}754-2008]). |
| 820 | + |
| 821 | The available length are 32 (_binary32_) and 64 (_binary64_). |
| 822 | |
| 823 | The value is the result of evaluating a {py3} expression using the |
| 824 | <<cur-bo,current byte order>>. |
| 825 | |
| 826 | A fixed-length number is: |
| 827 | |
| 828 | . The ``pass:[{]`` prefix. |
| 829 | |
| 830 | . A valid {py3} expression. |
| 831 | + |
| 832 | For a fixed-length number at some source location{nbsp}__**L**__, this |
| 833 | expression may contain the name of any accessible <<label,label>> (not |
| 834 | within a nested group), including the name of a label defined |
| 835 | after{nbsp}__**L**__, as well as the name of any |
| 836 | <<variable-assignment,variable>> known at{nbsp}__**L**__. |
| 837 | + |
| 838 | The value of the special name `ICITTE` (`int` type) in this expression |
| 839 | is the <<cur-offset,current offset>> (before encoding the number). |
| 840 | |
| 841 | . The `:` character. |
| 842 | |
| 843 | . An encoding length in bits amongst: |
| 844 | + |
| 845 | -- |
| 846 | The expression evaluates to an `int` or `bool` value:: |
| 847 | `8`, `16`, `24`, `32`, `40`, `48`, `56`, and `64`. |
| 848 | + |
| 849 | NOTE: Normand automatically converts a `bool` value to `int`. |
| 850 | |
| 851 | The expression evaluates to a `float` value:: |
| 852 | `32` and `64`. |
| 853 | -- |
| 854 | |
| 855 | . The `}` suffix. |
| 856 | |
| 857 | ==== |
| 858 | Input: |
| 859 | |
| 860 | ---- |
| 861 | {le} {345:16} |
| 862 | {be} {-0xabcd:32} |
| 863 | ---- |
| 864 | |
| 865 | Output: |
| 866 | |
| 867 | ---- |
| 868 | 59 01 ff ff 54 33 |
| 869 | ---- |
| 870 | ==== |
| 871 | |
| 872 | ==== |
| 873 | Input: |
| 874 | |
| 875 | ---- |
| 876 | {be} |
| 877 | |
| 878 | # String length in bits |
| 879 | {8 * (str_end - str_beg) : 16} |
| 880 | |
| 881 | # String |
| 882 | <str_beg> |
| 883 | "hello world!" |
| 884 | <str_end> |
| 885 | ---- |
| 886 | |
| 887 | Output: |
| 888 | |
| 889 | ---- |
| 890 | 00 60 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 ┆ •`hello world! |
| 891 | ---- |
| 892 | ==== |
| 893 | |
| 894 | ==== |
| 895 | Input: |
| 896 | |
| 897 | ---- |
| 898 | {20 - ICITTE : 8} * 10 |
| 899 | ---- |
| 900 | |
| 901 | Output: |
| 902 | |
| 903 | ---- |
| 904 | 14 13 12 11 10 0f 0e 0d 0c 0b |
| 905 | ---- |
| 906 | ==== |
| 907 | |
| 908 | ==== |
| 909 | Input: |
| 910 | |
| 911 | ---- |
| 912 | {le} |
| 913 | {2 * 0.0529 : 32} |
| 914 | ---- |
| 915 | |
| 916 | Output: |
| 917 | |
| 918 | ---- |
| 919 | ac ad d8 3d |
| 920 | ---- |
| 921 | ==== |
| 922 | |
| 923 | === LEB128 integer |
| 924 | |
| 925 | An _LEB128 integer_ represents a variable number of bytes encoding an |
| 926 | unsigned or signed integer which is the result of evaluating a {py3} |
| 927 | expression following the https://en.wikipedia.org/wiki/LEB128[LEB128] |
| 928 | format. |
| 929 | |
| 930 | An LEB128 integer is: |
| 931 | |
| 932 | . The ``pass:[{]`` prefix. |
| 933 | |
| 934 | . A valid {py3} expression of which the evaluation result type |
| 935 | is `int` or `bool` (automatically converted to `int`). |
| 936 | + |
| 937 | For an LEB128 integer at some source location{nbsp}__**L**__, this |
| 938 | expression may contain: |
| 939 | + |
| 940 | -- |
| 941 | * The name of any <<label,label>> defined before{nbsp}__**L**__ |
| 942 | which isn't within a nested group. |
| 943 | * The name of any <<variable-assignment,variable>> known |
| 944 | at{nbsp}__**L**__. |
| 945 | -- |
| 946 | + |
| 947 | The value of the special name `ICITTE` (`int` type) in this expression |
| 948 | is the <<cur-offset,current offset>> (before encoding the integer). |
| 949 | |
| 950 | . The `:` character. |
| 951 | |
| 952 | . One of: |
| 953 | + |
| 954 | -- |
| 955 | [horizontal] |
| 956 | `uleb128`:: Use the unsigned LEB128 format. |
| 957 | `sleb128`:: Use the signed LEB128 format. |
| 958 | -- |
| 959 | |
| 960 | . The `}` suffix. |
| 961 | |
| 962 | ==== |
| 963 | Input: |
| 964 | |
| 965 | ---- |
| 966 | {624485 : uleb128} |
| 967 | ---- |
| 968 | |
| 969 | Output: |
| 970 | |
| 971 | ---- |
| 972 | e5 8e 26 |
| 973 | ---- |
| 974 | ==== |
| 975 | |
| 976 | ==== |
| 977 | Input: |
| 978 | |
| 979 | ---- |
| 980 | aa bb cc dd |
| 981 | <meow> |
| 982 | ee ff |
| 983 | {-981238311 + (meow * -23) : sleb128} |
| 984 | "hello" |
| 985 | ---- |
| 986 | |
| 987 | Output: |
| 988 | |
| 989 | ---- |
| 990 | aa bb cc dd ee ff fd fa 8d ac 7c 68 65 6c 6c 6f ┆ ••••••••••|hello |
| 991 | ---- |
| 992 | ==== |
| 993 | |
| 994 | === Current offset setting |
| 995 | |
| 996 | This special item sets the <<cur-offset,_current offset_>>. |
| 997 | |
| 998 | A current offset setting is: |
| 999 | |
| 1000 | . The `<` prefix. |
| 1001 | |
| 1002 | . A <<const-int,positive constant integer>> which is the new current |
| 1003 | offset. |
| 1004 | |
| 1005 | . The `>` suffix. |
| 1006 | |
| 1007 | ==== |
| 1008 | Input: |
| 1009 | |
| 1010 | ---- |
| 1011 | {ICITTE : 8} * 8 |
| 1012 | <0x61> {ICITTE : 8} * 8 |
| 1013 | ---- |
| 1014 | |
| 1015 | Output: |
| 1016 | |
| 1017 | ---- |
| 1018 | 00 01 02 03 04 05 06 07 61 62 63 64 65 66 67 68 ┆ ••••••••abcdefgh |
| 1019 | ---- |
| 1020 | ==== |
| 1021 | |
| 1022 | ==== |
| 1023 | Input: |
| 1024 | |
| 1025 | ---- |
| 1026 | aa bb cc dd <meow> ee ff |
| 1027 | <12> 11 22 33 <mix> 44 55 |
| 1028 | {meow : 8} {mix : 8} |
| 1029 | ---- |
| 1030 | |
| 1031 | Output: |
| 1032 | |
| 1033 | ---- |
| 1034 | aa bb cc dd ee ff 11 22 33 44 55 04 0f ┆ •••••••"3DU•• |
| 1035 | ---- |
| 1036 | ==== |
| 1037 | |
| 1038 | === Current offset alignment |
| 1039 | |
| 1040 | A _current offset alignment_ represents zero or more padding bytes to |
| 1041 | make the <<cur-offset,current offset>> meet a given |
| 1042 | https://en.wikipedia.org/wiki/Data_structure_alignment[alignment] value. |
| 1043 | |
| 1044 | More specifically, for an alignment value of{nbsp}__**N**__{nbsp}bits, |
| 1045 | a current offset alignment represents the required padding bytes until |
| 1046 | the current offset is a multiple of __**N**__{nbsp}/{nbsp}8. |
| 1047 | |
| 1048 | A current offset alignment is: |
| 1049 | |
| 1050 | . The `@` prefix. |
| 1051 | |
| 1052 | . A <<const-int,positive constant integer>> which is the alignment value |
| 1053 | in _bits_. |
| 1054 | + |
| 1055 | This value must be greater than zero and a multiple of{nbsp}8. |
| 1056 | |
| 1057 | . **Optional**: |
| 1058 | + |
| 1059 | -- |
| 1060 | . The ``pass:[~]`` prefix. |
| 1061 | . A <<const-int,positive constant integer>> which is the value of the |
| 1062 | byte to use as padding to align the <<cur-offset,current offset>>. |
| 1063 | -- |
| 1064 | + |
| 1065 | Without this section, the padding byte value is zero. |
| 1066 | |
| 1067 | ==== |
| 1068 | Input: |
| 1069 | |
| 1070 | ---- |
| 1071 | 11 22 (@32 aa bb cc) * 3 |
| 1072 | ---- |
| 1073 | |
| 1074 | Output: |
| 1075 | |
| 1076 | ---- |
| 1077 | 11 22 00 00 aa bb cc 00 aa bb cc 00 aa bb cc |
| 1078 | ---- |
| 1079 | ==== |
| 1080 | |
| 1081 | ==== |
| 1082 | Input: |
| 1083 | |
| 1084 | ---- |
| 1085 | {le} |
| 1086 | 77 88 |
| 1087 | @32~0xcc {-893.5:32} |
| 1088 | @128~0x55 "meow" |
| 1089 | ---- |
| 1090 | |
| 1091 | Output: |
| 1092 | |
| 1093 | ---- |
| 1094 | 77 88 cc cc 00 60 5f c4 55 55 55 55 55 55 55 55 ┆ w••••`_•UUUUUUUU |
| 1095 | 6d 65 6f 77 ┆ meow |
| 1096 | ---- |
| 1097 | ==== |
| 1098 | |
| 1099 | ==== |
| 1100 | Input: |
| 1101 | |
| 1102 | ---- |
| 1103 | aa bb cc <29> @64~255 "zoom" |
| 1104 | ---- |
| 1105 | |
| 1106 | Output: |
| 1107 | |
| 1108 | ---- |
| 1109 | aa bb cc ff ff ff 7a 6f 6f 6d ┆ ••••••zoom |
| 1110 | ---- |
| 1111 | ==== |
| 1112 | |
| 1113 | === Filling |
| 1114 | |
| 1115 | A _filling_ represents zero or more padding bytes to make the |
| 1116 | <<cur-offset,current offset>> reach a given value. |
| 1117 | |
| 1118 | A filling is: |
| 1119 | |
| 1120 | . The ``pass:[+]`` prefix. |
| 1121 | |
| 1122 | . One of: |
| 1123 | |
| 1124 | ** A <<const-int,positive constant integer>> which is the current offset |
| 1125 | target. |
| 1126 | |
| 1127 | ** The ``pass:[{]`` prefix, a valid {py3} expression of which the |
| 1128 | evaluation result type is `int` or `bool` (automatically converted to |
| 1129 | `int`), and the ``pass:[}]`` suffix. |
| 1130 | + |
| 1131 | For a filling at some source location{nbsp}__**L**__, this expression |
| 1132 | may contain: |
| 1133 | + |
| 1134 | -- |
| 1135 | * The name of any <<label,label>> defined before{nbsp}__**L**__ |
| 1136 | which isn't within a nested group. |
| 1137 | * The name of any <<variable-assignment,variable>> known |
| 1138 | at{nbsp}__**L**__. |
| 1139 | -- |
| 1140 | + |
| 1141 | The value of the special name `ICITTE` (`int` type) in this expression |
| 1142 | is the <<cur-offset,current offset>> (before handling the items to |
| 1143 | repeat). |
| 1144 | |
| 1145 | ** A valid {py3} name. |
| 1146 | + |
| 1147 | For the name `__NAME__`, this is equivalent to the |
| 1148 | `pass:[{]__NAME__pass:[}]` form above. |
| 1149 | |
| 1150 | + |
| 1151 | This value must be greater than or equal to the current offset where |
| 1152 | it's used. |
| 1153 | |
| 1154 | . **Optional**: |
| 1155 | + |
| 1156 | -- |
| 1157 | . The ``pass:[~]`` prefix. |
| 1158 | . A <<const-int,positive constant integer>> which is the value of the |
| 1159 | byte to use as padding to reach the current offset target. |
| 1160 | -- |
| 1161 | + |
| 1162 | Without this section, the padding byte value is zero. |
| 1163 | |
| 1164 | ==== |
| 1165 | Input: |
| 1166 | |
| 1167 | ---- |
| 1168 | aa bb cc dd |
| 1169 | +0x40 |
| 1170 | "hello world" |
| 1171 | ---- |
| 1172 | |
| 1173 | Output: |
| 1174 | |
| 1175 | ---- |
| 1176 | aa bb cc dd 00 00 00 00 00 00 00 00 00 00 00 00 ┆ •••••••••••••••• |
| 1177 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ •••••••••••••••• |
| 1178 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ •••••••••••••••• |
| 1179 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ┆ •••••••••••••••• |
| 1180 | 68 65 6c 6c 6f 20 77 6f 72 6c 64 ┆ hello world |
| 1181 | ---- |
| 1182 | ==== |
| 1183 | |
| 1184 | ==== |
| 1185 | Input: |
| 1186 | |
| 1187 | ---- |
| 1188 | !macro part(iter, fill) |
| 1189 | <0> "particular security " {ord('0') + iter : 8} +fill~0x80 |
| 1190 | !end |
| 1191 | |
| 1192 | {iter = 1} |
| 1193 | |
| 1194 | !repeat 5 |
| 1195 | m:part(iter, {32 + 4 * iter}) |
| 1196 | {iter = iter + 1} |
| 1197 | !end |
| 1198 | ---- |
| 1199 | |
| 1200 | Output: |
| 1201 | |
| 1202 | ---- |
| 1203 | 70 61 72 74 69 63 75 6c 61 72 20 73 65 63 75 72 ┆ particular secur |
| 1204 | 69 74 79 20 31 80 80 80 80 80 80 80 80 80 80 80 ┆ ity 1••••••••••• |
| 1205 | 80 80 80 80 70 61 72 74 69 63 75 6c 61 72 20 73 ┆ ••••particular s |
| 1206 | 65 63 75 72 69 74 79 20 32 80 80 80 80 80 80 80 ┆ ecurity 2••••••• |
| 1207 | 80 80 80 80 80 80 80 80 80 80 80 80 70 61 72 74 ┆ ••••••••••••part |
| 1208 | 69 63 75 6c 61 72 20 73 65 63 75 72 69 74 79 20 ┆ icular security |
| 1209 | 33 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 ┆ 3••••••••••••••• |
| 1210 | 80 80 80 80 80 80 80 80 70 61 72 74 69 63 75 6c ┆ ••••••••particul |
| 1211 | 61 72 20 73 65 63 75 72 69 74 79 20 34 80 80 80 ┆ ar security 4••• |
| 1212 | 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 ┆ •••••••••••••••• |
| 1213 | 80 80 80 80 80 80 80 80 70 61 72 74 69 63 75 6c ┆ ••••••••particul |
| 1214 | 61 72 20 73 65 63 75 72 69 74 79 20 35 80 80 80 ┆ ar security 5••• |
| 1215 | 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 ┆ •••••••••••••••• |
| 1216 | 80 80 80 80 80 80 80 80 80 80 80 80 ┆ •••••••••••• |
| 1217 | ---- |
| 1218 | ==== |
| 1219 | |
| 1220 | === Label |
| 1221 | |
| 1222 | A _label_ associates a name to the <<cur-offset,current offset>>. |
| 1223 | |
| 1224 | All the labels of a whole Normand input must have unique names. |
| 1225 | |
| 1226 | A label must not share the name of a <<variable-assignment,variable>> |
| 1227 | name. |
| 1228 | |
| 1229 | A label is: |
| 1230 | |
| 1231 | . The `<` prefix. |
| 1232 | |
| 1233 | . A valid {py3} name which is not `ICITTE`. |
| 1234 | |
| 1235 | . The `>` suffix. |
| 1236 | |
| 1237 | === Variable assignment |
| 1238 | |
| 1239 | A _variable assignment_ associates a name to the integral result of an |
| 1240 | evaluated {py3} expression. |
| 1241 | |
| 1242 | A variable assignment is: |
| 1243 | |
| 1244 | . The ``pass:[{]`` prefix. |
| 1245 | |
| 1246 | . A valid {py3} name which is not `ICITTE`. |
| 1247 | |
| 1248 | . The `=` character. |
| 1249 | |
| 1250 | . A valid {py3} expression of which the evaluation result type |
| 1251 | is `int`, `float`, or `bool` (automatically converted to `int`). |
| 1252 | + |
| 1253 | For a variable assignment at some source location{nbsp}__**L**__, this |
| 1254 | expression may contain: |
| 1255 | + |
| 1256 | -- |
| 1257 | * The name of any <<label,label>> defined before{nbsp}__**L**__ |
| 1258 | which isn't within a nested group. |
| 1259 | * The name of any <<variable-assignment,variable>> known |
| 1260 | at{nbsp}__**L**__. |
| 1261 | -- |
| 1262 | + |
| 1263 | The value of the special name `ICITTE` (`int` type) in this expression |
| 1264 | is the <<cur-offset,current offset>>. |
| 1265 | |
| 1266 | . The `}` suffix. |
| 1267 | |
| 1268 | ==== |
| 1269 | Input: |
| 1270 | |
| 1271 | ---- |
| 1272 | {mix = 101} {le} |
| 1273 | {meow = 42} 11 22 {meow:8} 33 {meow = ICITTE + 17} |
| 1274 | "yooo" {meow + mix : 16} |
| 1275 | ---- |
| 1276 | |
| 1277 | Output: |
| 1278 | |
| 1279 | ---- |
| 1280 | 11 22 2a 33 79 6f 6f 6f 7a 00 ┆ •"*3yoooz• |
| 1281 | ---- |
| 1282 | ==== |
| 1283 | |
| 1284 | === Group |
| 1285 | |
| 1286 | A _group_ is a scoped sequence of items. |
| 1287 | |
| 1288 | The <<label,labels>> within a group aren't visible outside of it. |
| 1289 | |
| 1290 | The main purpose of a group is to <<post-item-repetition,repeat>> more |
| 1291 | than a single item and to isolate labels. |
| 1292 | |
| 1293 | A group is: |
| 1294 | |
| 1295 | . The `(`, `!group`, or `!g` opening. |
| 1296 | |
| 1297 | . Zero or more items. |
| 1298 | |
| 1299 | . Depending on the group opening: |
| 1300 | + |
| 1301 | -- |
| 1302 | `(`:: |
| 1303 | The `)` closing. |
| 1304 | |
| 1305 | `!group`:: |
| 1306 | `!g`:: |
| 1307 | The `!end` closing. |
| 1308 | -- |
| 1309 | |
| 1310 | ==== |
| 1311 | Input: |
| 1312 | |
| 1313 | ---- |
| 1314 | ((aa bb cc) dd () ee) "leclerc" |
| 1315 | ---- |
| 1316 | |
| 1317 | Output: |
| 1318 | |
| 1319 | ---- |
| 1320 | aa bb cc dd ee 6c 65 63 6c 65 72 63 ┆ •••••leclerc |
| 1321 | ---- |
| 1322 | ==== |
| 1323 | |
| 1324 | ==== |
| 1325 | Input: |
| 1326 | |
| 1327 | ---- |
| 1328 | !group |
| 1329 | (aa bb cc) * 3 dd ee |
| 1330 | !end * 5 |
| 1331 | ---- |
| 1332 | |
| 1333 | Output: |
| 1334 | |
| 1335 | ---- |
| 1336 | aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa bb |
| 1337 | cc aa bb cc dd ee aa bb cc aa bb cc aa bb cc dd |
| 1338 | ee aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa |
| 1339 | bb cc aa bb cc dd ee |
| 1340 | ---- |
| 1341 | ==== |
| 1342 | |
| 1343 | ==== |
| 1344 | Input: |
| 1345 | |
| 1346 | ---- |
| 1347 | {be} |
| 1348 | ( |
| 1349 | <str_beg> u16le"sébastien diaz" <str_end> |
| 1350 | {ICITTE - str_beg : 8} |
| 1351 | {(end - str_beg) * 5 : 24} |
| 1352 | ) * 3 |
| 1353 | <end> |
| 1354 | ---- |
| 1355 | |
| 1356 | Output: |
| 1357 | |
| 1358 | ---- |
| 1359 | 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e• |
| 1360 | 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 e0 ┆ n• •d•i•a•z••••• |
| 1361 | 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e• |
| 1362 | 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 40 ┆ n• •d•i•a•z••••@ |
| 1363 | 73 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e• |
| 1364 | 6e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 00 a0 ┆ n• •d•i•a•z••••• |
| 1365 | ---- |
| 1366 | ==== |
| 1367 | |
| 1368 | === Conditional block |
| 1369 | |
| 1370 | A _conditional block_ represents either the bytes of zero or more items |
| 1371 | if some expression is true, or the bytes of zero or more other items if |
| 1372 | it's false. |
| 1373 | |
| 1374 | A conditional block is: |
| 1375 | |
| 1376 | . The `!if` opening. |
| 1377 | |
| 1378 | . One of: |
| 1379 | |
| 1380 | ** The ``pass:[{]`` prefix, a valid {py3} expression of which the |
| 1381 | evaluation result type is `int` or `bool` (automatically converted to |
| 1382 | `int`), and the ``pass:[}]`` suffix. |
| 1383 | + |
| 1384 | For a conditional block at some source location{nbsp}__**L**__, this |
| 1385 | expression may contain: |
| 1386 | + |
| 1387 | -- |
| 1388 | * The name of any <<label,label>> defined before{nbsp}__**L**__ |
| 1389 | which isn't within a nested group. |
| 1390 | * The name of any <<variable-assignment,variable>> known |
| 1391 | at{nbsp}__**L**__. |
| 1392 | -- |
| 1393 | + |
| 1394 | The value of the special name `ICITTE` (`int` type) in this expression |
| 1395 | is the <<cur-offset,current offset>> (before handling the contained |
| 1396 | items). |
| 1397 | |
| 1398 | ** A valid {py3} name. |
| 1399 | + |
| 1400 | For the name `__NAME__`, this is equivalent to the |
| 1401 | `pass:[{]__NAME__pass:[}]` form above. |
| 1402 | |
| 1403 | . Zero or more items to be handled when the condition is true. |
| 1404 | |
| 1405 | . **Optional**: |
| 1406 | |
| 1407 | .. The `!else` opening. |
| 1408 | .. Zero or more items to be handled when the condition is false. |
| 1409 | |
| 1410 | . The `!end` closing. |
| 1411 | |
| 1412 | ==== |
| 1413 | Input: |
| 1414 | |
| 1415 | ---- |
| 1416 | {at = 1} |
| 1417 | {rep_count = 9} |
| 1418 | |
| 1419 | !repeat rep_count |
| 1420 | "meow " |
| 1421 | |
| 1422 | !if {ICITTE > 25} |
| 1423 | "mix" |
| 1424 | !else |
| 1425 | "zoom" |
| 1426 | !end |
| 1427 | |
| 1428 | !if {at < rep_count} 20 !end |
| 1429 | |
| 1430 | {at = at + 1} |
| 1431 | !end |
| 1432 | ---- |
| 1433 | |
| 1434 | Output: |
| 1435 | |
| 1436 | ---- |
| 1437 | 6d 65 6f 77 20 7a 6f 6f 6d 20 6d 65 6f 77 20 7a ┆ meow zoom meow z |
| 1438 | 6f 6f 6d 20 6d 65 6f 77 20 7a 6f 6f 6d 20 6d 65 ┆ oom meow zoom me |
| 1439 | 6f 77 20 6d 69 78 20 6d 65 6f 77 20 6d 69 78 20 ┆ ow mix meow mix |
| 1440 | 6d 65 6f 77 20 6d 69 78 20 6d 65 6f 77 20 6d 69 ┆ meow mix meow mi |
| 1441 | 78 20 6d 65 6f 77 20 6d 69 78 20 6d 65 6f 77 20 ┆ x meow mix meow |
| 1442 | 6d 69 78 ┆ mix |
| 1443 | ---- |
| 1444 | ==== |
| 1445 | |
| 1446 | ==== |
| 1447 | Input: |
| 1448 | |
| 1449 | ---- |
| 1450 | <str_beg> |
| 1451 | u16le"meow mix!" |
| 1452 | <str_end> |
| 1453 | |
| 1454 | !if {str_end - str_beg > 10} |
| 1455 | " BIG" |
| 1456 | !end |
| 1457 | ---- |
| 1458 | |
| 1459 | Output: |
| 1460 | |
| 1461 | ---- |
| 1462 | 6d 00 65 00 6f 00 77 00 20 00 6d 00 69 00 78 00 ┆ m•e•o•w• •m•i•x• |
| 1463 | 21 00 20 42 49 47 ┆ !• BIG |
| 1464 | ---- |
| 1465 | ==== |
| 1466 | |
| 1467 | === Repetition block |
| 1468 | |
| 1469 | A _repetition block_ represents the bytes of one or more items repeated |
| 1470 | a given number of times. |
| 1471 | |
| 1472 | A repetition block is: |
| 1473 | |
| 1474 | . The `!repeat` or `!r` opening. |
| 1475 | |
| 1476 | . One of: |
| 1477 | |
| 1478 | ** A <<const-int,positive constant integer>> which is the number of |
| 1479 | times to repeat the previous item. |
| 1480 | |
| 1481 | ** The ``pass:[{]`` prefix, a valid {py3} expression of which the |
| 1482 | evaluation result type is `int` or `bool` (automatically converted to |
| 1483 | `int`), and the ``pass:[}]`` suffix. |
| 1484 | + |
| 1485 | For a repetition block at some source location{nbsp}__**L**__, this |
| 1486 | expression may contain: |
| 1487 | + |
| 1488 | -- |
| 1489 | * The name of any <<label,label>> defined before{nbsp}__**L**__ |
| 1490 | which isn't within a nested group. |
| 1491 | * The name of any <<variable-assignment,variable>> known |
| 1492 | at{nbsp}__**L**__. |
| 1493 | -- |
| 1494 | + |
| 1495 | The value of the special name `ICITTE` (`int` type) in this expression |
| 1496 | is the <<cur-offset,current offset>> (before handling the items to |
| 1497 | repeat). |
| 1498 | |
| 1499 | ** A valid {py3} name. |
| 1500 | + |
| 1501 | For the name `__NAME__`, this is equivalent to the |
| 1502 | `pass:[{]__NAME__pass:[}]` form above. |
| 1503 | |
| 1504 | . Zero or more items. |
| 1505 | |
| 1506 | . The `!end` closing. |
| 1507 | |
| 1508 | You may also use a <<post-item-repetition,post-item repetition>> after |
| 1509 | some items. The form ``!repeat{nbsp}__X__{nbsp}__ITEMS__{nbsp}!end`` |
| 1510 | is equivalent to ``(__ITEMS__){nbsp}pass:[*]{nbsp}__X__``. |
| 1511 | |
| 1512 | ==== |
| 1513 | Input: |
| 1514 | |
| 1515 | ---- |
| 1516 | !repeat 0o400 |
| 1517 | {end - ICITTE - 1 : 8} |
| 1518 | !end |
| 1519 | |
| 1520 | <end> |
| 1521 | ---- |
| 1522 | |
| 1523 | Output: |
| 1524 | |
| 1525 | ---- |
| 1526 | ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ •••••••••••••••• |
| 1527 | ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ •••••••••••••••• |
| 1528 | df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ •••••••••••••••• |
| 1529 | cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ •••••••••••••••• |
| 1530 | bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ •••••••••••••••• |
| 1531 | af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ •••••••••••••••• |
| 1532 | 9f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ •••••••••••••••• |
| 1533 | 8f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ •••••••••••••••• |
| 1534 | 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp |
| 1535 | 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba` |
| 1536 | 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP |
| 1537 | 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@ |
| 1538 | 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210 |
| 1539 | 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"! |
| 1540 | 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ •••••••••••••••• |
| 1541 | 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ •••••••••••••••• |
| 1542 | ---- |
| 1543 | ==== |
| 1544 | |
| 1545 | ==== |
| 1546 | Input: |
| 1547 | |
| 1548 | ---- |
| 1549 | {times = 1} |
| 1550 | |
| 1551 | aa bb cc dd |
| 1552 | |
| 1553 | !repeat 3 |
| 1554 | <here> |
| 1555 | |
| 1556 | !repeat {here + 1} |
| 1557 | ee ff |
| 1558 | !end |
| 1559 | |
| 1560 | 11 22 !repeat times 33 !end |
| 1561 | |
| 1562 | {times = times + 1} |
| 1563 | !end |
| 1564 | |
| 1565 | "coucou!" |
| 1566 | ---- |
| 1567 | |
| 1568 | Output: |
| 1569 | |
| 1570 | ---- |
| 1571 | aa bb cc dd ee ff ee ff ee ff ee ff ee ff 11 22 ┆ •••••••••••••••" |
| 1572 | 33 ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ 3••••••••••••••• |
| 1573 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1574 | ff ee ff ee ff 11 22 33 33 ee ff ee ff ee ff ee ┆ ••••••"33••••••• |
| 1575 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1576 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1577 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1578 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1579 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1580 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1581 | ff ee ff ee ff ee ff ee ff ee ff ee ff 11 22 33 ┆ ••••••••••••••"3 |
| 1582 | 33 33 63 6f 75 63 6f 75 21 ┆ 33coucou! |
| 1583 | ---- |
| 1584 | ==== |
| 1585 | |
| 1586 | === Macro definition block |
| 1587 | |
| 1588 | A _macro definition block_ associates a name and parameter names to |
| 1589 | a group of items. |
| 1590 | |
| 1591 | A macro definition block doesn't lead to generated bytes itself: a |
| 1592 | <<macro-expansion,macro expansion>> does so. |
| 1593 | |
| 1594 | A macro definition may only exist at the root level, that is, not within |
| 1595 | a <<group,group>>, a <<repetition-block,repetition block>>, a |
| 1596 | <<conditional-block,conditional block>>, or another |
| 1597 | <<macro-definition-block,macro definition block>>. |
| 1598 | |
| 1599 | All macro definitions must have unique names. |
| 1600 | |
| 1601 | A macro definition is: |
| 1602 | |
| 1603 | . The `!macro` or `!m` opening. |
| 1604 | |
| 1605 | . A valid {py3} name (the macro name). |
| 1606 | |
| 1607 | . The `(` parameter name list prefix. |
| 1608 | |
| 1609 | . A comma-separated list of zero or more unique parameter names, |
| 1610 | each one being a valid {py3} name. |
| 1611 | |
| 1612 | . The `)` parameter name list suffix. |
| 1613 | |
| 1614 | . Zero or more items except, recursively, a macro definition block. |
| 1615 | |
| 1616 | . The `!end` closing. |
| 1617 | |
| 1618 | ==== |
| 1619 | ---- |
| 1620 | !macro bake() |
| 1621 | {le} {ICITTE * 8 : 16} |
| 1622 | u16le"predict explode" |
| 1623 | !end |
| 1624 | ---- |
| 1625 | ==== |
| 1626 | |
| 1627 | ==== |
| 1628 | ---- |
| 1629 | !macro nail(rep, with_extra, val) |
| 1630 | {iter = 1} |
| 1631 | |
| 1632 | !repeat rep |
| 1633 | {val + iter : uleb128} |
| 1634 | {0xdeadbeef : 32} |
| 1635 | {iter = iter + 1} |
| 1636 | !end |
| 1637 | |
| 1638 | !if with_extra |
| 1639 | "meow mix\0" |
| 1640 | !end |
| 1641 | !end |
| 1642 | ---- |
| 1643 | ==== |
| 1644 | |
| 1645 | === Macro expansion |
| 1646 | |
| 1647 | A _macro expansion_ expands the items of a defined |
| 1648 | <<macro-definition-block,macro>>. |
| 1649 | |
| 1650 | The macro to expand must be defined _before_ the expansion. |
| 1651 | |
| 1652 | The <<state,state>> before handling the first item of the chosen macro |
| 1653 | is: |
| 1654 | |
| 1655 | <<cur-offset,Current offset>>:: |
| 1656 | Unchanged. |
| 1657 | |
| 1658 | <<cur-bo,Current byte order>>:: |
| 1659 | Unchanged. |
| 1660 | |
| 1661 | Variables:: |
| 1662 | The only available variables initially are the macro parameters. |
| 1663 | |
| 1664 | Labels:: |
| 1665 | None. |
| 1666 | |
| 1667 | The state after having handled the last item of the chosen macro is: |
| 1668 | |
| 1669 | Current offset:: |
| 1670 | The one before handling the first item of the macro plus the size |
| 1671 | of the generated data of the macro expansion. |
| 1672 | + |
| 1673 | IMPORTANT: This means <<current-offset-setting,current offset setting>> |
| 1674 | items within the expanded macro don't impact the final current offset. |
| 1675 | |
| 1676 | Current byte order:: |
| 1677 | The one before handling the first item of the macro. |
| 1678 | |
| 1679 | Variables:: |
| 1680 | The ones before handling the first item of the macro. |
| 1681 | |
| 1682 | Labels:: |
| 1683 | The ones before handling the first item of the macro. |
| 1684 | |
| 1685 | A macro expansion is: |
| 1686 | |
| 1687 | . The `m:` prefix. |
| 1688 | |
| 1689 | . A valid {py3} name (the name of the macro to expand). |
| 1690 | |
| 1691 | . The `(` parameter value list prefix. |
| 1692 | |
| 1693 | . A comma-separated list of zero or more unique parameter values. |
| 1694 | + |
| 1695 | The number of parameter values must match the number of parameter |
| 1696 | names of the definition of the chosen macro. |
| 1697 | + |
| 1698 | A parameter value is one of: |
| 1699 | + |
| 1700 | -- |
| 1701 | * A <<const-int,constant integer>>, possibly negative. |
| 1702 | |
| 1703 | * The ``pass:[{]`` prefix, a valid {py3} expression of which the |
| 1704 | evaluation result type is `int` or `bool` (automatically converted to |
| 1705 | `int`), and the ``pass:[}]`` suffix. |
| 1706 | + |
| 1707 | For a macro expansion at some source location{nbsp}__**L**__, this |
| 1708 | expression may contain: |
| 1709 | |
| 1710 | ** The name of any <<label,label>> defined before{nbsp}__**L**__ |
| 1711 | which isn't within a nested group. |
| 1712 | ** The name of any <<variable-assignment,variable>> known |
| 1713 | at{nbsp}__**L**__. |
| 1714 | |
| 1715 | + |
| 1716 | The value of the special name `ICITTE` (`int` type) in this expression |
| 1717 | is the <<cur-offset,current offset>> (before handling the items of the |
| 1718 | chosen macro). |
| 1719 | |
| 1720 | * A valid {py3} name. |
| 1721 | + |
| 1722 | For the name `__NAME__`, this is equivalent to the |
| 1723 | `pass:[{]__NAME__pass:[}]` form above. |
| 1724 | -- |
| 1725 | |
| 1726 | . The `)` parameter value list suffix. |
| 1727 | |
| 1728 | ==== |
| 1729 | Input: |
| 1730 | |
| 1731 | ---- |
| 1732 | !macro bake() |
| 1733 | {le} {ICITTE * 8 : 16} |
| 1734 | u16le"predict explode" |
| 1735 | !end |
| 1736 | |
| 1737 | "hello [" m:bake() "] world" |
| 1738 | |
| 1739 | m:bake() * 5 |
| 1740 | ---- |
| 1741 | |
| 1742 | Output: |
| 1743 | |
| 1744 | ---- |
| 1745 | 68 65 6c 6c 6f 20 5b 38 00 70 00 72 00 65 00 64 ┆ hello [8•p•r•e•d |
| 1746 | 00 69 00 63 00 74 00 20 00 65 00 78 00 70 00 6c ┆ •i•c•t• •e•x•p•l |
| 1747 | 00 6f 00 64 00 65 00 5d 20 77 6f 72 6c 64 70 01 ┆ •o•d•e•] worldp• |
| 1748 | 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• • |
| 1749 | 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 02 ┆ e•x•p•l•o•d•e•p• |
| 1750 | 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• • |
| 1751 | 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 03 ┆ e•x•p•l•o•d•e•p• |
| 1752 | 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• • |
| 1753 | 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 04 ┆ e•x•p•l•o•d•e•p• |
| 1754 | 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• • |
| 1755 | 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 70 05 ┆ e•x•p•l•o•d•e•p• |
| 1756 | 70 00 72 00 65 00 64 00 69 00 63 00 74 00 20 00 ┆ p•r•e•d•i•c•t• • |
| 1757 | 65 00 78 00 70 00 6c 00 6f 00 64 00 65 00 ┆ e•x•p•l•o•d•e• |
| 1758 | ---- |
| 1759 | ==== |
| 1760 | |
| 1761 | ==== |
| 1762 | Input: |
| 1763 | |
| 1764 | ---- |
| 1765 | !macro A(val, is_be) |
| 1766 | {le} |
| 1767 | |
| 1768 | !if is_be |
| 1769 | {be} |
| 1770 | !end |
| 1771 | |
| 1772 | {val : 16} |
| 1773 | !end |
| 1774 | |
| 1775 | !macro B(rep, is_be) |
| 1776 | {iter = 1} |
| 1777 | |
| 1778 | !repeat rep |
| 1779 | m:A({iter * 3}, is_be) |
| 1780 | {iter = iter + 1} |
| 1781 | !end |
| 1782 | !end |
| 1783 | |
| 1784 | m:B(5, 1) |
| 1785 | m:B(3, 0) |
| 1786 | ---- |
| 1787 | |
| 1788 | Output: |
| 1789 | |
| 1790 | ---- |
| 1791 | 00 03 00 06 00 09 00 0c 00 0f 03 00 06 00 09 00 |
| 1792 | ---- |
| 1793 | ==== |
| 1794 | |
| 1795 | === Post-item repetition |
| 1796 | |
| 1797 | A _post-item repetition_ represents the bytes of an item repeated a |
| 1798 | given number of times. |
| 1799 | |
| 1800 | A post-item repetition is: |
| 1801 | |
| 1802 | . One of those items: |
| 1803 | |
| 1804 | ** A <<byte-constant,byte constant>>. |
| 1805 | ** A <<literal-string,literal string>>. |
| 1806 | ** A <<fixed-length-number,fixed-length number>>. |
| 1807 | ** An <<leb128-integer,LEB128 integer>>. |
| 1808 | ** A <<macro-expansion,macro-expansion>>. |
| 1809 | ** A <<group,group>>. |
| 1810 | |
| 1811 | . The ``pass:[*]`` character. |
| 1812 | |
| 1813 | . One of: |
| 1814 | |
| 1815 | ** A positive integer (hexadecimal starting with `0x` or `0X` accepted) |
| 1816 | which is the number of times to repeat the previous item. |
| 1817 | |
| 1818 | ** The ``pass:[{]`` prefix, a valid {py3} expression of which the |
| 1819 | evaluation result type is `int` or `bool` (automatically converted to |
| 1820 | `int`), and the ``pass:[}]`` suffix. |
| 1821 | + |
| 1822 | For a post-item repetition at some source location{nbsp}__**L**__, this |
| 1823 | expression may contain: |
| 1824 | + |
| 1825 | -- |
| 1826 | * The name of any <<label,label>> defined before{nbsp}__**L**__ |
| 1827 | which isn't within a nested group and |
| 1828 | which isn't part of the repeated item. |
| 1829 | * The name of any <<variable-assignment,variable>> known |
| 1830 | at{nbsp}__**L**__, which isn't part of its repeated item, and which |
| 1831 | doesn't. |
| 1832 | -- |
| 1833 | + |
| 1834 | The value of the special name `ICITTE` (`int` type) in this expression |
| 1835 | is the <<cur-offset,current offset>> (before handling the items to |
| 1836 | repeat). |
| 1837 | |
| 1838 | ** A valid {py3} name. |
| 1839 | + |
| 1840 | For the name `__NAME__`, this is equivalent to the |
| 1841 | `pass:[{]__NAME__pass:[}]` form above. |
| 1842 | |
| 1843 | You may also use a <<repetition-block,repetition block>>. The form |
| 1844 | ``__ITEM__{nbsp}pass:[*]{nbsp}__X__`` is equivalent to |
| 1845 | ``!repeat{nbsp}__X__{nbsp}__ITEM__{nbsp}!end``. |
| 1846 | |
| 1847 | ==== |
| 1848 | Input: |
| 1849 | |
| 1850 | ---- |
| 1851 | {end - ICITTE - 1 : 8} * 0x100 <end> |
| 1852 | ---- |
| 1853 | |
| 1854 | Output: |
| 1855 | |
| 1856 | ---- |
| 1857 | ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ •••••••••••••••• |
| 1858 | ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ •••••••••••••••• |
| 1859 | df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ •••••••••••••••• |
| 1860 | cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ •••••••••••••••• |
| 1861 | bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ •••••••••••••••• |
| 1862 | af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ •••••••••••••••• |
| 1863 | 9f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ •••••••••••••••• |
| 1864 | 8f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ •••••••••••••••• |
| 1865 | 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp |
| 1866 | 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba` |
| 1867 | 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP |
| 1868 | 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@ |
| 1869 | 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210 |
| 1870 | 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"! |
| 1871 | 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ •••••••••••••••• |
| 1872 | 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ •••••••••••••••• |
| 1873 | ---- |
| 1874 | ==== |
| 1875 | |
| 1876 | ==== |
| 1877 | Input: |
| 1878 | |
| 1879 | ---- |
| 1880 | {times = 1} |
| 1881 | aa bb cc dd |
| 1882 | ( |
| 1883 | <here> |
| 1884 | (ee ff) * {here + 1} |
| 1885 | 11 22 33 * {times} |
| 1886 | {times = times + 1} |
| 1887 | ) * 3 |
| 1888 | "coucou!" |
| 1889 | ---- |
| 1890 | |
| 1891 | Output: |
| 1892 | |
| 1893 | ---- |
| 1894 | aa bb cc dd ee ff ee ff ee ff ee ff ee ff 11 22 ┆ •••••••••••••••" |
| 1895 | 33 ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ 3••••••••••••••• |
| 1896 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1897 | ff ee ff ee ff 11 22 33 33 ee ff ee ff ee ff ee ┆ ••••••"33••••••• |
| 1898 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1899 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1900 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1901 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1902 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1903 | ff ee ff ee ff ee ff ee ff ee ff ee ff ee ff ee ┆ •••••••••••••••• |
| 1904 | ff ee ff ee ff ee ff ee ff ee ff ee ff 11 22 33 ┆ ••••••••••••••"3 |
| 1905 | 33 33 63 6f 75 63 6f 75 21 ┆ 33coucou! |
| 1906 | ---- |
| 1907 | ==== |
| 1908 | |
| 1909 | == Command-line tool |
| 1910 | |
| 1911 | If you <<install-normand,installed>> the `normand` package, then you |
| 1912 | can use the `normand` command-line tool: |
| 1913 | |
| 1914 | ---- |
| 1915 | $ normand <<< '"ma gang de malades"' | hexdump -C |
| 1916 | ---- |
| 1917 | |
| 1918 | ---- |
| 1919 | 00000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad| |
| 1920 | 00000010 65 73 |es| |
| 1921 | ---- |
| 1922 | |
| 1923 | If you copy the `normand.py` module to your own project, then you can |
| 1924 | run the module itself: |
| 1925 | |
| 1926 | ---- |
| 1927 | $ python3 -m normand <<< '"ma gang de malades"' | hexdump -C |
| 1928 | ---- |
| 1929 | |
| 1930 | ---- |
| 1931 | 00000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad| |
| 1932 | 00000010 65 73 |es| |
| 1933 | ---- |
| 1934 | |
| 1935 | Without a path argument, the `normand` tool reads from the standard |
| 1936 | input. |
| 1937 | |
| 1938 | The `normand` tool prints the generated binary data to the standard |
| 1939 | output. |
| 1940 | |
| 1941 | Various options control the initial <<state,state>> of the processor: |
| 1942 | use the `--help` option to learn more. |
| 1943 | |
| 1944 | == {py3} API |
| 1945 | |
| 1946 | The whole `normand` package/module public API is: |
| 1947 | |
| 1948 | [source,python] |
| 1949 | ---- |
| 1950 | # Byte order. |
| 1951 | class ByteOrder(enum.Enum): |
| 1952 | # Big endian. |
| 1953 | BE = ... |
| 1954 | |
| 1955 | # Little endian. |
| 1956 | LE = ... |
| 1957 | |
| 1958 | |
| 1959 | # Text location. |
| 1960 | class TextLocation: |
| 1961 | # Line number. |
| 1962 | @property |
| 1963 | def line_no(self) -> int: |
| 1964 | ... |
| 1965 | |
| 1966 | # Column number. |
| 1967 | @property |
| 1968 | def col_no(self) -> int: |
| 1969 | ... |
| 1970 | |
| 1971 | |
| 1972 | # Parsing error message. |
| 1973 | class ParseErrorMessage: |
| 1974 | # Message text. |
| 1975 | @property |
| 1976 | def text(self): |
| 1977 | ... |
| 1978 | |
| 1979 | # Source text location. |
| 1980 | @property |
| 1981 | def text_location(self): |
| 1982 | ... |
| 1983 | |
| 1984 | |
| 1985 | # Parsing error. |
| 1986 | class ParseError(RuntimeError): |
| 1987 | # Parsing error messages. |
| 1988 | # |
| 1989 | # The first message is the most _specific_ one. |
| 1990 | @property |
| 1991 | def messages(self): |
| 1992 | ... |
| 1993 | |
| 1994 | |
| 1995 | # Variables dictionary type (for type hints). |
| 1996 | VariablesT = typing.Dict[str, typing.Union[int, float]] |
| 1997 | |
| 1998 | |
| 1999 | # Labels dictionary type (for type hints). |
| 2000 | LabelsT = typing.Dict[str, int] |
| 2001 | |
| 2002 | |
| 2003 | # Parsing result. |
| 2004 | class ParseResult: |
| 2005 | # Generated data. |
| 2006 | @property |
| 2007 | def data(self) -> bytearray: |
| 2008 | ... |
| 2009 | |
| 2010 | # Updated variable values. |
| 2011 | @property |
| 2012 | def variables(self) -> SymbolsT: |
| 2013 | ... |
| 2014 | |
| 2015 | # Updated main group label values. |
| 2016 | @property |
| 2017 | def labels(self) -> SymbolsT: |
| 2018 | ... |
| 2019 | |
| 2020 | # Final offset. |
| 2021 | @property |
| 2022 | def offset(self) -> int: |
| 2023 | ... |
| 2024 | |
| 2025 | # Final byte order. |
| 2026 | @property |
| 2027 | def byte_order(self) -> typing.Optional[ByteOrder]: |
| 2028 | ... |
| 2029 | |
| 2030 | |
| 2031 | # Parses the `normand` input using the initial state defined by |
| 2032 | # `init_variables`, `init_labels`, `init_offset`, and `init_byte_order`, |
| 2033 | # and returns the corresponding parsing result. |
| 2034 | def parse(normand: str, |
| 2035 | init_variables: typing.Optional[SymbolsT] = None, |
| 2036 | init_labels: typing.Optional[SymbolsT] = None, |
| 2037 | init_offset: int = 0, |
| 2038 | init_byte_order: typing.Optional[ByteOrder] = None) -> ParseResult: |
| 2039 | ... |
| 2040 | ---- |
| 2041 | |
| 2042 | The `normand` parameter is the actual <<learn-normand,Normand input>> |
| 2043 | while the other parameters control the initial <<state,state>>. |
| 2044 | |
| 2045 | The `parse()` function raises a `ParseError` instance should it fail to |
| 2046 | parse the `normand` string for any reason. |
| 2047 | |
| 2048 | == Development |
| 2049 | |
| 2050 | Normand is a https://python-poetry.org/[Poetry] project. |
| 2051 | |
| 2052 | To develop it, install it through Poetry and enter the virtual |
| 2053 | environment: |
| 2054 | |
| 2055 | ---- |
| 2056 | $ poetry install |
| 2057 | $ poetry shell |
| 2058 | $ normand <<< '"lol" * 10 0a' |
| 2059 | ---- |
| 2060 | |
| 2061 | `normand.py` is processed by: |
| 2062 | |
| 2063 | * https://microsoft.github.io/pyright/[Pyright] |
| 2064 | * https://github.com/psf/black[Black] |
| 2065 | * https://pycqa.github.io/isort/[isort] |
| 2066 | |
| 2067 | === Testing |
| 2068 | |
| 2069 | Use https://docs.pytest.org/[pytest] to test Normand once the package is |
| 2070 | part of your virtual environment, for example: |
| 2071 | |
| 2072 | ---- |
| 2073 | $ poetry install |
| 2074 | $ poetry run pip3 install pytest |
| 2075 | $ poetry run pytest |
| 2076 | ---- |
| 2077 | |
| 2078 | The `pytest` project is currently not a development dependency in |
| 2079 | `pyproject.toml` due to backward compatibiliy issues with |
| 2080 | Python{nbsp}3.4. |
| 2081 | |
| 2082 | In the `tests` directory, each `*.nt` file is a test. The file name |
| 2083 | prefix indicates what it's meant to test: |
| 2084 | |
| 2085 | `pass-`:: |
| 2086 | Everything above the `---` line is the valid Normand input |
| 2087 | to test. |
| 2088 | + |
| 2089 | Everything below the `---` line is the expected data |
| 2090 | (whitespace-separated hexadecimal bytes). |
| 2091 | |
| 2092 | `fail-`:: |
| 2093 | Everything above the `---` line is the invalid Normand input |
| 2094 | to test. |
| 2095 | + |
| 2096 | Everything below the `---` line is the expected error message having |
| 2097 | this form: |
| 2098 | + |
| 2099 | ---- |
| 2100 | LINE:COL - MESSAGE |
| 2101 | ---- |
| 2102 | |
| 2103 | === Contributing |
| 2104 | |
| 2105 | Normand uses https://review.lttng.org/admin/repos/normand,general[Gerrit] |
| 2106 | for code review. |
| 2107 | |
| 2108 | To report a bug, https://github.com/efficios/normand/issues/new[create a |
| 2109 | GitHub issue]. |