|(require xenomorph)||package: xenomorph|
This package is in development. I make no commitment to maintaining the public interface documented below.
Hands up: who likes parsing and writing binary formats?
OK, just a few of you, in the back. You’re free to go.
Everyone else: Xenomorph eases the pain of working with binary formats. Instead of fiddling with counting bytes:
This encoding can then be used as a binary compiler, converting Racket values to binary and writing them out to a file.
But wait, there’s more: this encoding can also be used as a binary parser, reading bytes and parsing them into Racket values. So one encoding definition can be used for both input and output.
Derived principally from Devon Govett’s restructure library for Node. Thanks for doing the heavy lifting, dude.
At the command line:
raco pkg install xenomorph
After that, you can update the package from the command line:
raco pkg update xenomorph
Invoke the library in a source file by importing it in the usual way:
> (define four-ints ( 4)) > (decode four-ints #"\1\2\3\4")
'(1 2 3 4)
> (decode four-ints #"\1\2\3")
bytes->list: contract violation
> (decode four-ints #"\1\2\3\4\5\6")
'(1 2 3 4)
> (define op (open-output-string)) > (encode four-ints '(1 2 3 4) op) > (get-output-bytes op)
Suppose we have a file on disk. What’s in the file? Without knowing anything else, we can at least say the file contains a sequence of bytes. A byte is the smallest unit of data storage. It’s not, however, the smallest unit of information storage — that would be a bit. But when we read (or write) from disk (or other source, like memory), we work with bytes.
A byte holds eight bits, so it can take on values between 0 and 255, inclusive. In Racket, a sequence of bytes is also known as a byte string. It prints as a series of values between quotation marks, prefixed with #:
Caution: though this looks similar to the ordinary string "ABC", we’re better off thinking of it as a sequence of integers that are sometimes displayed as characters for convenience. For instance, the byte string above represents three bytes valued 65, 66, and 67. This byte string could also be written in hexadecimal like so:
Or octal like so:
Both of these mean the same thing. (If you like, confirm this by trying them on the REPL.)
We can also make an equivalent byte string with bytes. As above, Racket doesn’t care how we notate the values, as long as they’re between 0 and 255:
TODO: escape the chars below
Byte values between 32 and 127 are printed as characters. Other values are printed in octal:
> (bytes 65 66 67 154 206 255)
> (bytes->list #"ABC\232\316\377")
'(65 66 67 154 206 255)
> (list->bytes '(65 66 67 154 206 255))
The important thing is that when we see the #" prefix, we know we’re looking at a byte string, not an ordinary string.
Back to files. Typically, files on disk are classified as being either binary or text. (A distinction observed by Racket functions such as write-to-file.) When we speak of binary vs. text, we’re saying something about the internal structure of the byte sequence — what values those bytes represent. This internal structure is also called an encoding. An encoding is a way of representing a sequence of arbitrary values as a sequence of bytes.
But since we all have experience with text files, let’s use text encoding as a way of starting to understand what’s happening under the hood with binary encodings.
For example, ASCII is a familiar encoding that stores each character in seven bits, so it can describe 128 distinct characters. Because every ASCII code is less than 255, we can store ASCII text with one byte per character.
But if we want to use more than 128 distinct characters, we’re stuck. That’s why Racket instead uses the UTF-8 text encoding by default. UTF-8 uses between one and three bytes to encode each character, and can thus represent up to 1,112,064 distinct characters. We can see how this works by converting a string into an encoded byte sequence using string->bytes/utf-8:
> (string->bytes/utf-8 "ABCD")
> (bytes->list (string->bytes/utf-8 "ABCD"))
'(65 66 67 68)
> (string->bytes/utf-8 "ABÇ战")
> (bytes->list (string->bytes/utf-8 "ABÇ战"))
'(65 66 195 135 230 136 152)
For ASCII-compatible characters, UTF-8 uses one byte for each character. Thus, the string "ABCD" is four bytes long in UTF-8.
Now consider the string "ABÇ战", which has four characters, but the second two aren’t ASCII-compatible. In UTF-8, it’s encoded as seven bytes: the first two characters are one byte each, the "Ç" takes two bytes, and the "战" takes three.
Moreover, for further simplicity, text files typically rely on a small set of pre-defined encodings, like ASCII or UTF-8 or Latin-1, so that those who write programs that manipulate text only have to support a smallish set of encodings.
Three corollaries follow:
A given sequence of bytes can mean different things, depending on what encoding we use.
We can only make sense of a sequence of bytes if we know its encoding.
A byte sequence does not describe its own encoding.
For those familiar with programming-language lingo, an encoding somewhat resembles a grammar, which is a tool for describing the syntactic structure of a program. A grammar doesn’t describe one particular program. Rather, it describes all possible programs that are consistent with the grammar, and therefore can be used to parse any particular one. Likewise for an encoding.
Can a grammar work as a binary encoding? In limited cases, but not enough to be practical. Most grammars have to assume the target program is context free, meaning that the grammar rules apply the same way everywhere. By contrast, binary files are nonrecursive and contextual.
template : (is-a?/c ) byte-source : (or/c bytes? input-port?) = (current-input-port)
template : (is-a?/c ) v : any/c byte-dest : (or/c output-port? #f) = (current-output-port)
|(require xenomorph/number)||package: xenomorph|
|(require xenomorph/string)||package: xenomorph|
|(require xenomorph/array)||package: xenomorph|
|(require xenomorph/lazy-array)||package: xenomorph|
|(require xenomorph/struct)||package: xenomorph|
|(require xenomorph/versioned-struct)||package: xenomorph|
|(require xenomorph/pointer)||package: xenomorph|
|(require xenomorph/bitfield)||package: xenomorph|
|(require xenomorph/enum)||package: xenomorph|
|(require xenomorph/optional)||package: xenomorph|
|(require xenomorph/reserved)||package: xenomorph|
This module is licensed under the MIT license.
Source repository at http://github.com/mbutterick/xenomorph. Suggestions & corrections welcome.