Previous Next
70
CHAPTER 3 Syntax
Specifically, ASCII base-85 encoding produces 5 ASCII characters for every 4
bytes of binary data. Each group of 4 binary input bytes, (b1 b2 b3 b4 ), is converted
to a group of 5 output bytes, (c1 c2 c3 c4 c5 ), using the relation
3 2 1
( b 1 × 256 ) + ( b 2 × 256 ) + ( b 3 × 256 ) + b 4 =
4 3 2 1
( c 1 × 85 ) + ( c 2 × 85 ) + ( c 3 × 85 ) + ( c 4 × 85 ) + c 5
In other words, 4 bytes of binary data are interpreted as a base-256 number and
then converted to a base-85 number. The five bytes of the base-85 number are
then converted to ASCII characters by adding 33 (the ASCII code for the
character ! ) to each. The resulting encoded data contains only printable ASCII
characters with codes in the range 33 ( ! ) to 117 (u). As a special case, if all five
bytes are 0, they are represented by the character with code 122 (z) instead of by
five exclamation points ( ! ! ! ! ! ).
If the length of the binary data to be encoded is not a multiple of 4 bytes, the last,
partial group of 4 is used to produce a last, partial group of 5 output characters.
Given n (1, 2, or 3) bytes of binary data, the encoder first appends 4 − n zero bytes
to make a complete group of 4. It then encodes this group in the usual way, but
without applying the special z case. Finally, it writes only the first n + 1 characters
of the resulting group of 5. These characters are immediately followed by the ~>
EOD marker.
The following conditions (which never occur in a correctly encoded byte
sequence) cause errors during decoding:
• The value represented by a group of 5 characters is greater than 232 − 1.
• A z character occurs in the middle of a group.
• A final partial group contains only one character.
Previous Next