Contents
Ascii85
Ascii85, also called Base85, is a form of binary-to-text encoding developed by Paul E. Rutter for the btoa utility. By using five ASCII characters to represent four bytes of binary data (making the encoded size 1⁄4 larger than the original, assuming eight bits per ASCII character), it is more efficient than uuencode or Base64, which use four characters to represent three bytes of data (1⁄3 increase, assuming eight bits per ASCII character). Its main modern uses are in Adobe's PostScript and Portable Document Format file formats, as well as in the patch encoding for binary files used by Git.
Overview
The basic need for a binary-to-text encoding comes from a need to communicate arbitrary binary data over preexisting communications protocols that were designed to carry only English language human-readable text. Those communication protocols may only be 7-bit safe (and within that avoid certain ASCII control codes), and may require line breaks at certain maximum intervals, and may not maintain whitespace. Thus, only the 94 printable ASCII characters are "safe" to use to convey data. Eighty-five is the minimum integer value of n such that n5 ≥ 2564; so any sequence of 4 bytes can be encoded as 5 symbols, as long as at least 85 distinct symbols are available. (Five radix-85 digits can represent the integers from 0 to 4,437,053,124 inclusive, which suffice to represent all 4,294,967,296 possible 4-byte sequences.)
Encoding
When encoding, each group of 4 bytes is taken as a 32-bit binary number, most significant byte first (Ascii85 uses a big-endian convention). This is converted, by repeatedly dividing by 85 and taking the remainder, into 5 radix-85 digits. Then each digit (again, most significant first) is encoded as an ASCII printable character by adding 33 to it, giving the ASCII characters 33 through 117. Because all-zero data is quite common, an exception is made for the sake of data compression, and an all-zero group is encoded as a single character instead of. Groups of characters that decode to a value greater than 232 − 1 (encoded as ) will cause a decoding error, as will characters in the middle of a group. White space between the characters is ignored and may occur anywhere to accommodate line-length limitations.
Limitations
The original specification only allows a stream that is a multiple of 4 bytes to be encoded. Encoded data may contain characters that have special meaning in many programming languages and in some text-based protocols, such as left-angle-bracket, backslash , and the single and double quotes &. Other base-85 encodings like Z85 and are designed to be safe in source code.
History
btoa version
The original btoa program always encoded full groups (padding the source as necessary), with a prefix line of "xbtoa Begin", and suffix line of "xbtoa End", followed by the original file length (in decimal and hexadecimal) and three 32-bit checksums. The decoder needs to use the file length to see how much of the group was padding. The initial proposal for btoa encoding used an encoding alphabet starting at the ASCII space character through "t" inclusive, but this was replaced with an encoding alphabet of "!" to "u" to avoid "problems with some mailers (stripping off trailing blanks)". This program also introduced the special " " short form for an all-zero group. Version 4.2 added a " " exception for a group of all ASCII space characters (0x20202020).
ZMODEM version
"ZMODEM Pack-7 encoding" encodes groups of 4 octets into groups of 5 printable ASCII characters in a similar, or possibly in the same way as Ascii85 does. When a ZMODEM program sends pre-compressed 8-bit data files over 7-bit data channels, it uses "ZMODEM Pack-7 encoding".
Adobe version
Adobe adopted the basic btoa encoding, but with slight changes, and gave it the name Ascii85. The characters used are the ASCII characters 33 through 117 inclusive (to represent the base-85 digits 0 through 84), together with the letter (as a special case to represent a 32-bit 0 value), and white space is ignored. Adobe uses the delimiter " " to mark the end of an Ascii85-encoded string and represents the length by truncating the final group: If the last block of source bytes contains fewer than 4 bytes, the block is padded with up to 3 null bytes before encoding. After encoding, as many bytes as were added as padding are removed from the end of the output. The reverse is applied when decoding: The last block is padded to 5 bytes with the Ascii85 character, and as many bytes as were added as padding are omitted from the end of the output (see example). The padding is not arbitrary. Converting from binary to base 64 only regroups bits and does not change them or their order (a high bit in binary does not affect the low bits in the base64 representation). In converting a binary number to base85 (85 is not a power of two) high bits do affect the low order base85 digits and conversely. Padding the binary low (with zero bits) while encoding and padding the base85 value high (with s) in decoding assures that the high order bits are preserved (the zero padding in the binary gives enough room so that a small addition is trapped and there is no "carry" to the high bits).
<!-- In sending bits/bytes requiring K bytes for complete/lossless encoding and only having L bytes one can pad with (K-L) padding bytes P send the full set of K bytes and an indicator of how many should be dropped. The recipient can decode and get a result (including the padding bytes) and use the indicator select and drop them. Very easy and it is how I would have done base64 and base85 encoding. Consider the following: Pad the L bytes with padding K-L padding bytes P. Encode that. Don't send them all, but send a shorter version (since you don't need the lower part to calculate the upper part). The recipient can't decode (since he needs the full allocation of material to decode) so he pads with bytes Q (to have enough to decode to something) and decodes this to a string of K bytes. He takes the first L bytes from this string as the decoded version. Is this the same as the original string of L bytes. base64: Encodes three bytes into four base64 digits. Assume one encodes two bytes. Ooops! Too short. Suppose one pads this with an arbitrary byte. In the bit pattern: x x x x x x x x|x x x x x x x x|p p p p p p p p BYTE_1 BYTE_2 BYTE_3 we have the padding bits, p, can be arbitrary. Convert to base64. This is very easy as 64 is a power of two as is 256 (bytes are just base256 numbers). x x x x x x|x x x x x x|x x x x p p|p p p p p p DIGIT_1 DIGIT_2 DIGIT_3 DIGIT_4 <-- base 64 "digits" where the digits are base64 digits (which can be displayed when taken from a selected set of 64 symbols). Send only the first THREE base64 digits of the endoded material: x x x x x x|x x x x x x|x x x x p p DIGIT_1 DIGIT_2 DIGIT_3 The recipient pads with any (arbitrary) base64 digit x x x x x x|x x x x x x|x x x x p p|q q q q q q DIGIT_1 DIGIT_2 DIGIT_3 DIGIT_4 and decodes to binary x x x x x x x x|x x x x x x x x|p p q q q q q q BYTE_1 BYTE_2 BYTE_3 and keeps the first two bytes only. Are they the same as the original? Yes, no matter what the p's and q's are since they are just added and dropped. base85: Encodes four bytes into five base85 digits. Assume one encodes three bytes. Ooops! Too short. Suppose one pads this with an arbitrary byte In the bit pattern: x x x x x x x x|x x x x x x x x|x x x x x x x x|p p p p p p p p BYTE_1 BYTE_2 BYTE_3 BYTE_4 we have the padding bits, p, which can be arbitrary. Convert to base85. This is not so easy as 85 is NOT a power of two as is 256 (bytes are just base256 numbers). DIGIT_1 DIGIT_2 DIGIT_3 DIGIT_4 DIGIT_5 NOT SO EASY NOW! The high bits in the original data can affect the low base85 digits and the low base85 digits can affect the HIGH BITS! Only send the first four base85 digits. DIGIT_1 DIGIT_2 DIGIT_3 DIGIT_4 The recipient pads this with a base85 digit (Q) and decodes this to binary (or base256 if you like to think of bytes instead of bits): DIGIT_1 DIGIT_2 DIGIT_3 DIGIT_4 Q b b b b b b b b|b b b b b b b b|b b b b b b b b|b b b b b b b b and keeps the first three bytes. Are they the same as the original three bytes? NOT NECESSARILY. In base64, as converting from base256 (bytes) to base 64 (bits) just regroups bits, the values of the bits do not change. In base85 a change in the lowest base85 digit CAN affect the high bits if the padding is wrongly done. EXAMPLE: Encoding ASC(128) ASC(0) ASC(0) (three bytes - the two last being nulls) and padding the binary with zero bits: 1 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0 BYTE_1 BYTE_2 BYTE_3 PADDED: 1 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0 WRITTEN IN BASE85: (encoded) DIGIT_1 DIGIT_2 DIGIT_3 DIGIT_4 DIGIT_5 I am not going to specify these but simply note DIGIT_5 CANNOT BE ZERO! (or else the number would be divisible by 85 but it is equal to 2^31) SEND THE FIRST FOUR base85 DIGITS: DIGIT_1 DIGIT_2 DIGIT_3 DIGIT_4 The recipient pads ... let me use ZERO (base85 digit) to pad: DIGIT_1 DIGIT_2 DIGIT_3 DIGIT_4 0 and converts back to binary. b b b b b b b b|b b b b b b b b|b b b b b b b b|b b b b b b b b This is base85 encoding (NOT base64) the low order base85 digit of the number 2^31 is NOT ZERO (DIGIT_5 is not zero) and the recipient replaced it with zero so he has a SMALLER number (smaller than 2^31) and so when he converts to binary, the high order bit is ZERO - NOT ONE! It is not true that one can use arbitrary paddings in encoding/decoding this time. TRICK: Always pad the binary LOW (use zeros) and pad the base85 number HIGH (use 84 as the base85 digit for padding). What happens in that case? In that case the recipient has padded with 84. DIGIT_1 DIGIT_2 DIGIT_3 DIGIT_4 84 and decodes: b b b b b b b b|b b b b b b b b|b b b b b b b b|b b b b b b b b This number is not the original and, in fact, is LARGER THAN THE ORIGINAL BINARY NUMBER. How much larger? At most 84+the_original (you changed the last base85 digit to its maximum). This number is then 1 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0 + X (0 <= X <= 84) - 1 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|t t t t t t t t ^^^^^^^^^^^^^^^ X ONLY AFFECTS THESE (and not even the first one!) where X is at most 84. NOTE THAT WE PADDED THE BINARY WITH ZEROS! THERE WILL BE NO CARRY WHEN WE ADD X (smaller than 256) BEYOND THE LOWEST BYTE DUE TO THE NUMBER OF LOW ORDER ZERO PADDIING BITS IN THE ORIGINAL NUMBER TO WHICH X IS ADDED. As long as we pad the binary with zeros and the base85 with 84s, since 85^n < 256^n we will decode to a larger number (with some low order bits) BUT WE HAVE PADDED WITH ENOUGH ZEROS SO ADDING THE EXTRA TO THE ORIGINAL BINARY ONLY AFFECTS THE PADDED BITS (there is NO CARRY INTO THE BITS WE WANT TO KEEP). You can see similarly that padding the binary high (with 1 bits) and the base85 character high (84s) will cause problems. So ... the padding for decoding has to be matched with the padding for encoding in base85 since high bits affect low base85 digits and conversely - which does not occur with base64 encoding. --> In Ascii85-encoded blocks, whitespace and line-break characters may be present anywhere, including in the middle of a 5-character block, but they must be silently ignored.Adobe's specification does not support the exception.
Example for Ascii85
A quote from Thomas Hobbes's Leviathan: If this is initially encoded using US-ASCII, it can be reencoded in Ascii85 as follows: 9jqo^BlbD-BleB1DJ++F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,O< DJ+.@<K0@<6L(Df-\0Ec5e;DffZ(EZee.Bl.9pF"AGXBPCsi+DGm>@3BB/F&OCAfu2/AKYi( DIb:@FD,)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal( DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G>u D.RTpAKYo'+CT/5+Cei#DII?(E,9)oF2M7/c Since the last 4-tuple is incomplete, it must be padded with three zero bytes: Since three bytes of padding had to be added, the three final characters 'YkO' are omitted from the output. Decoding is done inversely, except that the last 5-tuple is padded with 'u' characters: Since the input had to be padded with three 'u' bytes, the last three bytes of the output are ignored and we end up with the original period. The input sentence does not contain 4 consecutive zero bytes, so the example does not show the use of the 'z' abbreviation.
Compatibility
The Ascii85 encoding is compatible with 7-bit and 8-bit MIME, while having less overhead than Base64. One potential compatibility issue of Ascii85 is that some of the characters it uses are significant in markup languages such as XML or SGML. To include ascii85 data in these documents, it may be necessary to escape the quote, angle brackets, and ampersands.
RFC 1924 version
Published on April 1, 1996, informational : "A Compact Representation of IPv6 Addresses" by Robert Elz suggests a base-85 encoding of IPv6 addresses as an April Fools' Day joke. This differs from the scheme used above in that he proposes a different set of 85 ASCII characters, and proposes to do all arithmetic on the 128-bit number, converting it to a single 20-digit base-85 number (internal whitespace not allowed), rather than breaking it into four 32-bit groups. The proposed character set is, in order, –, – , – , and then the 23 characters. The highest possible representable address, 2128−1 = 74×8519 + 53×8518 + 5×8517 + ..., would be encoded as. This character set excludes the characters, making it suitable for use in JSON strings (where and would require escaping). However, for SGML-based protocols, notably including XML, string escapes may still be required (to accommodate, and ).
This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.
Wikipedia® is a registered trademark of the
Wikimedia Foundation, Inc.
Bliptext is not
affiliated with or endorsed by Wikipedia or the
Wikimedia Foundation.