The SDL Component Suite is an industry leading collection of components supporting scientific and engineering computing. Please visit the SDL Web site for more information....



EncodeASCII85


Unit:SDL_dstruct
Class:none
Declaration:procedure EncodeASCII85 (InStream, OutStream: TStream);

The procedure EncodeASCII85 encodes the data of the stream InStream into a base-85 encoded data stream OutStream. Base-85 encoded data is primarily used in PDF and PostScript files. The encoding process starts at the current position of the input stream InStream and writes the resulting data to the current position of Outstream (overwriting all data after the current position).

Detailed description of ASCII85 encoding [1]:

ASCII base-85 encoding produces five ASCII printing characters from every four bytes of binary data. Each group of four binary bytes (b1 b2 b3 b4) is converted to a group of five encoded characters (c1 c2 c3 c4 c5) using the relation

b1*2563 + b2*2562 + b3*2561 + b4 =
c1*854 + c2*853 + c3*852 + c4*851 + c5


The five digits of the encoded base-85 number are converted to printable ASCII characters by adding 33 (the ASCII code for !) to each. The resulting data contains only printable ASCII characters with codes in the range 33 (!) to 117 (u). Two special cases occur during encoding. First, if all five encoded digits are zero, they are represented by the character code 122 (z), instead of by a series of five exclamation points (!!!!!). In addition, if the length of the binary data to be encoded is not a multiple of four bytes, the last partial 4-tuple is used to produce a last, partial output 5-tuple. Given n (1, 2, or 3) bytes of binary data, the encoding first appends 4 -n zero bytes to make a complete 4-tuple. This 4-tuple is encoded in the usual way, but without applying the special z case. Finally, only the first n + 1 characters of the resulting 5-tuple are written out. Those characters are immediately followed by the EOD marker, which is the two-character sequence ~>.

[1] Adobe Systems Inc.: "Portable Document Format Reference Manual", Version 1.3, March 1999



Last Update: 2012-Oct-20