idnits 2.17.00 (12 Aug 2021) /tmp/idnits12427/draft-deutsch-deflate-spec-02.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- ** Cannot find the required boilerplate sections (Copyright, IPR, etc.) in this document. Expected boilerplate is as follows today (2022-05-20) according to https://trustee.ietf.org/license-info : IETF Trust Legal Provisions of 28-dec-2009, Section 6.a: This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79. IETF Trust Legal Provisions of 28-dec-2009, Section 6.b(i), paragraph 2: Copyright (c) 2022 IETF Trust and the persons identified as the document authors. All rights reserved. IETF Trust Legal Provisions of 28-dec-2009, Section 6.b(i), paragraph 3: This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (https://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- ** Missing expiration date. The document expiration date should appear on the first and last page. ** The document seems to lack a 1id_guidelines paragraph about Internet-Drafts being working documents. ** The document seems to lack a 1id_guidelines paragraph about 6 months document validity -- however, there's a paragraph with a matching beginning. Boilerplate error? ** The document seems to lack a 1id_guidelines paragraph about the list of current Internet-Drafts. ** The document seems to lack a 1id_guidelines paragraph about the list of Shadow Directories. == No 'Intended status' indicated for this document; assuming Proposed Standard == The page length should not exceed 58 lines per page, but there was 15 longer pages, the longest (page 2) being 59 lines Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** The document seems to lack an IANA Considerations section. (See Section 2.2 of https://www.ietf.org/id-info/checklist for how to handle the case when there are no actions for IANA.) ** The document seems to lack separate sections for Informative/Normative References. All references will be assumed normative when checking for downward references. Miscellaneous warnings: ---------------------------------------------------------------------------- -- The document seems to lack a disclaimer for pre-RFC5378 work, but may have content which was first submitted before 10 November 2008. If you have contacted all the original authors and they are all willing to grant the BCP78 rights to the IETF Trust, then this is fine, and you can ignore this comment. If not, you may need to add the pre-RFC5378 disclaimer. (See the Legal Provisions document at https://trustee.ietf.org/license-info for more information.) -- The document date (11 Mar 1996) is 9566 days in the past. Is this intentional? Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) -- Looks like a reference, but probably isn't: 'I' on line 349 -- Looks like a reference, but probably isn't: 'N' on line 389 == Missing Reference: '0' is mentioned on line 359, but not defined == Unused Reference: '4' is defined on line 719, but no explicit reference was found in the text == Unused Reference: '5' is defined on line 722, but no explicit reference was found in the text == Unused Reference: '6' is defined on line 725, but no explicit reference was found in the text -- Possible downref: Non-RFC (?) normative reference: ref. '1' -- Possible downref: Non-RFC (?) normative reference: ref. '2' -- Possible downref: Non-RFC (?) normative reference: ref. '3' -- Possible downref: Non-RFC (?) normative reference: ref. '4' -- Possible downref: Non-RFC (?) normative reference: ref. '5' -- Possible downref: Non-RFC (?) normative reference: ref. '6' Summary: 8 errors (**), 0 flaws (~~), 6 warnings (==), 10 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 INTERNET-DRAFT L. Peter Deutsch 3 DEFLATE 1.3 Aladdin Enterprises 4 Expires: 16 Sep 1996 11 Mar 1996 6 DEFLATE Compressed Data Format Specification version 1.3 8 File draft-deutsch-deflate-spec-02.txt 10 Status of this Memo 12 This document is an Internet-Draft. Internet-Drafts are working 13 documents of the Internet Engineering Task Force (IETF), its areas, 14 and its working groups. Note that other groups may also distribute 15 working documents as Internet-Drafts. 17 Internet-Drafts are draft documents valid for a maximum of six months 18 and may be updated, replaced, or obsoleted by other documents at any 19 time. It is inappropriate to use Internet- Drafts as reference 20 material or to cite them other than as ``work in progress.'' 22 To learn the current status of any Internet-Draft, please check the 23 ``1id-abstracts.txt'' listing contained in the Internet- Drafts 24 Shadow Directories on ftp.is.co.za (Africa), nic.nordu.net (Europe), 25 munnari.oz.au (Pacific Rim), ds.internic.net (US East Coast), or 26 ftp.isi.edu (US West Coast). 28 Distribution of this memo is unlimited. 30 Notices 32 Copyright (c) 1996 L. Peter Deutsch 34 Permission is granted to copy and distribute this document for any 35 purpose and without charge, including translations into other 36 languages and incorporation into compilations, provided that it is 37 copied as a whole (including the copyright notice and this notice) 38 and with no changes. 40 Deutsch [Page 1] 41 Abstract 43 This specification defines a lossless compressed data format that 44 compresses data using a combination of the LZ77 algorithm and Huffman 45 coding, with efficiency comparable to the best currently available 46 general-purpose compression methods. The data can be produced or 47 consumed, even for an arbitrarily long sequentially presented input 48 data stream, using only an a priori bounded amount of intermediate 49 storage. The format can be implemented readily in a manner not 50 covered by patents. 52 Table of Contents 54 1. Introduction ................................................... 2 55 1.1. Purpose ................................................... 3 56 1.2. Intended audience ......................................... 3 57 1.3. Scope ..................................................... 3 58 1.4. Compliance ................................................ 4 59 1.5. Definitions of terms and conventions used ................ 4 60 1.6. Changes from previous versions ............................ 4 61 2. Compressed representation overview ............................. 4 62 3. Detailed specification ......................................... 5 63 3.1. Overall conventions ....................................... 5 64 3.1.1. Packing into bytes .................................. 5 65 3.2. Compressed block format ................................... 6 66 3.2.1. Synopsis of prefix and Huffman coding ............... 6 67 3.2.2. Use of Huffman coding in the 'deflate' format ....... 7 68 3.2.3. Details of block format ............................. 9 69 3.2.4. Non-compressed blocks (BTYPE=00) ................... 10 70 3.2.5. Compressed blocks (length and distance codes) ...... 11 71 3.2.6. Compression with fixed Huffman codes (BTYPE=01) .... 11 72 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) .. 12 73 3.3. Compliance ............................................... 13 74 4. Compression algorithm details ................................. 14 75 5. References .................................................... 15 76 6. Security considerations ....................................... 15 77 7. Source code ................................................... 15 78 8. Acknowledgements .............................................. 16 79 9. Author's address .............................................. 16 81 1. Introduction 83 Deutsch [Page 2] 84 1.1. Purpose 86 The purpose of this specification is to define a lossless 87 compressed data format that: 89 * Is independent of CPU type, operating system, file system, 90 and character set, and hence can be used for interchange; 91 * Can be produced or consumed, even for an arbitrarily long 92 sequentially presented input data stream, using only an a 93 priori bounded amount of intermediate storage, and hence can 94 be used in data communications or similar structures such as 95 Unix filters; 96 * Compresses data with efficiency comparable to the best 97 currently available general-purpose compression methods, and 98 in particular considerably better than the 'compress' 99 program; 100 * Can be implemented readily in a manner not covered by 101 patents, and hence can be practiced freely; 102 * Is compatible with the file format produced by the current 103 widely used gzip utility, in that conforming decompressors 104 will be able to read data produced by the existing gzip 105 compressor. 107 The data format defined by this specification does not attempt to: 109 * Allow random access to compressed data; 110 * Compress specialized data (e.g., raster graphics) as well as 111 the best currently available specialized algorithms. 113 A simple counting argument shows that no lossless compression 114 algorithm can compress every possible input data set. For the 115 format defined here, the worst case expansion is 5 bytes per 32K- 116 byte block, i.e., a size increase of 0.015% for large data sets. 117 English text usually compresses by a factor of 2.5 to 3; 118 executable files usually compress somewhat less; graphical data 119 such as raster images may compress much more. 121 1.2. Intended audience 123 This specification is intended for use by implementors of software 124 to compress data into 'deflate' format and/or decompress data from 125 'deflate' format. 127 The text of the specification assumes a basic background in 128 programming at the level of bits and other primitive data 129 representations. Familiarity with the technique of Huffman coding 130 is helpful but not required. 132 1.3. Scope 134 The specification specifies a method for representing a sequence 135 of bytes as a (usually shorter) sequence of bits, and a method for 137 Deutsch [Page 3] 138 packing the latter bit sequence into bytes. 140 1.4. Compliance 142 Unless otherwise indicated below, a compliant decompressor must be 143 able to accept and decompress any data set that conforms to all 144 the specifications presented here; a compliant compressor must 145 produce data sets that conform to all the specifications presented 146 here. 148 1.5. Definitions of terms and conventions used 150 Byte: 8 bits stored or transmitted as a unit (same as an octet). 151 For this specification, a byte is exactly 8 bits, even on machines 152 which store a character on a number of bits different from eight. 153 See below, for the numbering of bits within a byte. 155 String: a sequence of arbitrary bytes. 157 1.6. Changes from previous versions 159 There have been no technical changes to the deflate format since 160 version 1.1 of this specification. In version 1.2, some 161 terminology was changed. Version 1.3 is a conversion of the 162 specification to Internet Draft style. 164 2. Compressed representation overview 166 A compressed data set consists of a series of blocks, corresponding 167 to successive blocks of input data. The block sizes are arbitrary, 168 except that non-compressible blocks are limited to 65,535 bytes. 170 Each block is compressed using a combination of the LZ77 algorithm 171 and Huffman coding. The Huffman trees for each block are independant 172 of those for previous or subsequent blocks; the LZ77 algorithm may 173 use a reference to a duplicated string occurring in a previous block, 174 up to 32K input bytes before. 176 Each block consists of two parts: a pair of Huffman code trees that 177 describe the representation of the compressed data part, and a 178 compressed data part. (The Huffman trees themselves are compressed 179 using Huffman encoding.) The compressed data consists of a series of 180 elements of two types: literal bytes (of strings that have not been 181 detected as duplicated within the previous 32K input bytes), and 182 pointers to duplicated strings, where a pointer is represented as a 183 pair . The representation used in the 184 'deflate' format limits distances to 32K bytes and lengths to 258 185 bytes, but does not limit the size of a block, except for 186 uncompressible blocks, which are limited as noted above. 188 Each type of value (literals, distances, and lengths) in the 189 compressed data is represented using a Huffman code, using one code 191 Deutsch [Page 4] 192 tree for literals and lengths and a separate code tree for distances. 193 The code trees for each block appear in a compact form just before 194 the compressed data for that block. 196 3. Detailed specification 198 3.1. Overall conventions In the diagrams below, a box like this: 200 +---+ 201 | | <-- the vertical bars might be missing 202 +---+ 204 represents one byte; a box like this: 206 +==============+ 207 | | 208 +==============+ 210 represents a variable number of bytes. 212 Bytes stored within a computer do not have a 'bit order', since 213 they are always treated as a unit. However, a byte considered as 214 an integer between 0 and 255 does have a most- and least- 215 significant bit, and since we write numbers with the most- 216 significant digit on the left, we also write bytes with the most- 217 significant bit on the left. In the diagrams below, we number the 218 bits of a byte so that bit 0 is the least-significant bit, i.e., 219 the bits are numbered: 221 +--------+ 222 |76543210| 223 +--------+ 225 Within a computer, a number may occupy multiple bytes. All 226 multi-byte numbers in the format described here are stored with 227 the least-significant byte first (at the lower memory address). 228 For example, the decimal number 520 is stored as: 230 0 1 231 +--------+--------+ 232 |00001000|00000010| 233 +--------+--------+ 234 ^ ^ 235 | | 236 | + more significant byte = 2 x 256 237 + less significant byte = 8 239 3.1.1. Packing into bytes 241 This document does not address the issue of the order in which 242 bits of a byte are transmitted on a bit-sequential medium, 243 since the final data format described here is byte- rather than 245 Deutsch [Page 5] 246 bit-oriented. However, we describe the compressed block format 247 in below, as a sequence of data elements of various bit 248 lengths, not a sequence of bytes. We must therefore specify 249 how to pack these data elements into bytes to form the final 250 compressed byte sequence: 252 * Data elements are packed into bytes in order of 253 increasing bit number within the byte, i.e., starting 254 with the least- significant bit of the byte. 255 * Data elements other than Huffman codes are packed 256 starting with the least-significant bit of the data 257 element. 258 * Huffman codes are packed starting with the most- 259 significant bit of the code. 261 In other words, if one were to print out the compressed data as 262 a sequence of bytes, starting with the first byte at the 263 *right* margin and proceeding to the *left*, with the most- 264 significant bit of each byte on the left as usual, one would be 265 able to parse the result from right to left, with fixed-width 266 elements in the correct MSB-to-LSB order and Huffman codes in 267 bit-reversed order (i.e., with the first bit of the code in the 268 relative LSB position). 270 3.2. Compressed block format 272 3.2.1. Synopsis of prefix and Huffman coding 274 Prefix coding represents symbols from an a priori known 275 alphabet by bit sequences (codes), one code for each symbol, in 276 a manner such that different symbols may be represented by bit 277 sequences of different lengths, but a parser can always parse 278 an encoded string unambiguously symbol-by-symbol. 280 We define a prefix code in terms of a binary tree in which the 281 two edges descending from each non-leaf node are labeled 0 and 282 1 and in which the leaf nodes correspond one-for-one with (are 283 labeled with) the symbols of the alphabet; then the code for a 284 symbol is the sequence of 0's and 1's on the edges leading from 285 the root to the leaf labeled with that symbol. For example: 287 /\ Symbol Code 288 0 1 ------ ---- 289 / \ A 00 290 /\ B B 1 291 0 1 C 011 292 / \ D 010 293 A /\ 294 0 1 295 / \ 296 D C 298 Deutsch [Page 6] 299 A parser can decode the next symbol from an encoded input 300 stream by walking down the tree from the root, at each step 301 choosing the edge corresponding to the next input bit. 303 Given an alphabet with known symbol frequencies, the Huffman 304 algorithm allows the construction of an optimal prefix code 305 (one which represents strings with those symbol frequencies 306 using the fewest bits of any possible prefix codes for that 307 alphabet). Such a code is called a Huffman code. (See 308 reference [1] in Chapter 5, references for additional 309 information on Huffman codes.) 311 Note that in the 'deflate' format, the Huffman codes for the 312 various alphabets must not exceed certain maximum code lengths. 313 This constraint complicates the algorithm for computing code 314 lengths from symbol frequencies. Again, see Chapter 5, 315 references for details. 317 3.2.2. Use of Huffman coding in the 'deflate' format 319 The Huffman codes used for each alphabet in the 'deflate' 320 format have two additional rules: 322 * All codes of a given bit length have lexicographically 323 consecutive values, in the same order as the symbols they 324 represent; 326 * Shorter codes lexicographically precede longer codes. 328 We could recode the example above to follow this rule as 329 follows, assuming that the order of the alphabet is ABCD: 331 Symbol Code 332 ------ ---- 333 A 10 334 B 0 335 C 110 336 D 111 338 I.e., 0 precedes 10 which precedes 11x, and 110 and 111 are 339 lexicographically consecutive. 341 Given this rule, we can define the Huffman code for an alphabet 342 just by giving the bit lengths of the codes for each symbol of 343 the alphabet in order; this is sufficient to determine the 344 actual codes. In our example, the code is completely defined 345 by the sequence of bit lengths (2, 1, 3, 3). The following 346 algorithm generates the codes as integers, intended to be read 347 from most- to least-significant bit. The code lengths are 348 initially in tree[I].Len; the codes are produced in 349 tree[I].Code. 351 Deutsch [Page 7] 352 1) Count the number of codes for each code length. Let 353 bl_count[N] be the number of codes of length N, N >= 1. 355 2) Find the numerical value of the smallest code for each code 356 length: 358 code = 0; 359 bl_count[0] = 0; 360 for (bits = 1; bits <= MAX_BITS; bits++) { 361 next_code[bits] = code 362 = (code + bl_count[bits-1]) << 1; 363 } 365 3) Assign numerical values to all codes, using consecutive 366 values for all codes of the same length with the base values 367 determined at step 2. Codes that are never used (which have a 368 bit length of zero) must not be assigned a value. 370 for (n = 0; n <= max_code; n++) { 371 len = tree[n].Len; 372 if (len == 0) continue; 373 tree[n].Code = next_code[len]++; 374 } 376 Example: 378 Consider the alphabet ABCDEFGH, with bit lengths (3, 3, 3, 3, 379 3, 2, 4, 4). After step 1, we have: 381 N bl_count[N] 382 - ----------- 383 2 1 384 3 5 385 4 2 387 Step 2 computes the following next_code values: 389 N next_code[N] 390 - ------------ 391 1 0 392 2 0 393 3 2 394 4 14 396 Step 3 produces the following code values: 398 Deutsch [Page 8] 399 Symbol Length Code 400 ------ ------ ---- 401 A 3 010 402 B 3 011 403 C 3 100 404 D 3 101 405 E 3 110 406 F 2 00 407 G 4 1110 408 H 4 1111 410 3.2.3. Details of block format 412 Each block of compressed data begins with 3 header bits 413 containing the following data: 415 first bit BFINAL 416 next 2 bits BTYPE 418 Note that the header bits do not necessarily begin on a byte 419 boundary, since a block does not necessarily occupy an integral 420 number of bytes. 422 BFINAL is set iff this is the last block of the data set. 424 BTYPE specifies how the data are compressed, as follows: 426 00 - no compression 427 01 - compressed with fixed Huffman codes 428 10 - compressed with dynamic Huffman codes 429 11 - reserved (error) 431 The only difference between the two compressed cases is how the 432 Huffman codes for the literal/length and distance alphabets are 433 defined. 435 In all cases, the decoding algorithm for the actual data is as 436 follows: 438 Deutsch [Page 9] 439 do 440 read block header from input stream. 441 if stored with no compression 442 skip any remaining bits in current partially 443 processed byte 444 read LEN and NLEN (see next section) 445 copy LEN bytes of data to output 446 otherwise 447 if compressed with dynamic Huffman codes 448 read representation of code trees (see 449 subsection below) 450 loop (until end of block code recognized) 451 decode literal/length value from input stream 452 if value < 256 453 copy value (literal byte) to output stream 454 otherwise 455 if value = end of block (256) 456 break from loop 457 otherwise (value = 257..285) 458 decode distance from input stream 460 move backwards distance bytes in the output 461 stream, and copy length bytes from this 462 position to the output stream. 463 end loop 464 while not last block 466 Note that a duplicated string reference may refer to a string 467 in a previous block; i.e., the backward distance may cross one 468 or more block boundaries. However a distance cannot refer past 469 the beginning of the output stream. (An application using a 470 preset dictionary might discard part of the output stream; a 471 distance can refer to that part of the output stream anyway) 472 Note also that the referenced string may overlap the current 473 position; for example, if the last 2 bytes decoded have values 474 X and Y, a string reference with 475 adds X,Y,X,Y,X to the output stream. 477 We now specify each compression method in turn. 479 3.2.4. Non-compressed blocks (BTYPE=00) 481 Any bits of input up to the next byte boundary are ignored. 482 The rest of the block consists of the following information: 484 0 1 2 3 4... 485 +---+---+---+---+=================================+ 486 | LEN | NLEN |... LEN bytes of literal data...| 487 +---+---+---+---+=================================+ 489 LEN is the number of data bytes in the block. NLEN is the 490 one's complement of LEN. 492 Deutsch [Page 10] 493 3.2.5. Compressed blocks (length and distance codes) 495 As noted above, encoded data blocks in the 'deflate' format 496 consist of sequences of symbols drawn from three conceptually 497 distinct alphabets: either literal bytes, from the alphabet of 498 byte values (0..255), or pairs, 499 where the length is drawn from (3..258) and the distance is 500 drawn from (1..32,768). In fact, the literal and length 501 alphabets are merged into a single alphabet (0..285), where 502 values 0..255 represent literal bytes, the value 256 indicates 503 end-of-block, and values 257..285 represent length codes 504 (possibly in conjunction with extra bits following the symbol 505 code) as follows: 507 Extra Extra Extra 508 Code Bits Length(s) Code Bits Lengths Code Bits Length(s) 509 ---- ---- ------ ---- ---- ------- ---- ---- ------- 510 257 0 3 267 1 15,16 277 4 67-82 511 258 0 4 268 1 17,18 278 4 83-98 512 259 0 5 269 2 19-22 279 4 99-114 513 260 0 6 270 2 23-26 280 4 115-130 514 261 0 7 271 2 27-30 281 5 131-162 515 262 0 8 272 2 31-34 282 5 163-194 516 263 0 9 273 3 35-42 283 5 195-226 517 264 0 10 274 3 43-50 284 5 227-257 518 265 1 11,12 275 3 51-58 285 0 258 519 266 1 13,14 276 3 59-66 521 The extra bits should be interpreted as a machine integer 522 stored with the most-significant bit first, e.g., bits 1110 523 represent the value 14. 525 Extra Extra Extra 526 Code Bits Dist Code Bits Dist Code Bits Distance 527 ---- ---- ---- ---- ---- ------ ---- ---- -------- 528 0 0 1 10 4 33-48 20 9 1025-1536 529 1 0 2 11 4 49-64 21 9 1537-2048 530 2 0 3 12 5 65-96 22 10 2049-3072 531 3 0 4 13 5 97-128 23 10 3073-4096 532 4 1 5,6 14 6 129-192 24 11 4097-6144 533 5 1 7,8 15 6 193-256 25 11 6145-8192 534 6 2 9-12 16 7 257-384 26 12 8193-12288 535 7 2 13-16 17 7 385-512 27 12 12289-16384 536 8 3 17-24 18 8 513-768 28 13 16385-24576 537 9 3 25-32 19 8 769-1024 29 13 24577-32768 539 3.2.6. Compression with fixed Huffman codes (BTYPE=01) 541 The Huffman codes for the two alphabets are fixed, and are not 542 represented explicitly in the data. The Huffman code lengths 543 for the literal/length alphabet are: 545 Deutsch [Page 11] 546 Lit Value Bits Codes 547 --------- ---- ----- 548 0 - 143 8 00110000 through 549 10111111 550 144 - 255 9 110010000 through 551 111111111 552 256 - 279 7 0000000 through 553 0010111 554 280 - 287 8 11000000 through 555 11000111 557 The code lengths are sufficient to generate the actual codes, 558 as described above; we show the codes in the table for added 559 clarity. Literal/length values 286-287 will never actually 560 occur in the compressed data, but participate in the code 561 construction. 563 Distance codes 0-31 are represented by (fixed-length) 5-bit 564 codes, with possible additional bits as shown in the table 565 shown in Paragraph 3.2.5, above. Note that distance codes 30- 566 31 will never actually occur in the compressed data. 568 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) 570 The Huffman codes for the two alphabets appear in the block 571 immediately after the header bits and before the actual 572 compressed data, first the literal/length code and then the 573 distance code. Each code is defined by a sequence of code 574 lengths, as discussed in Paragraph 3.2.2, above. For even 575 greater compactness, the code length sequences themselves are 576 compressed using a Huffman code. The alphabet for code lengths 577 is as follows: 579 0 - 15: Represent code lengths of 0 - 15 580 16: Copy the previous code length 3 - 6 times. 581 The next 2 bits indicate repeat length 582 (0 = 3, ... , 3 = 6) 583 Example: Codes 8, 16 (+2 bits 11), 584 16 (+2 bits 10) will expand to 585 12 code lengths of 8 (1 + 6 + 5) 586 17: Repeat a code length of 0 for 3 - 10 times. 587 (3 bits of length) 588 18: Repeat a code length of 0 for 11 - 138 times 589 (7 bits of length) 591 A code length of 0 indicates that the corresponding symbol in 592 the literal/length or distance alphabet will not occur in the 593 block, and should not participate in the Huffman code 594 construction algorithm given earlier. If only one distance 595 code is used, it is encoded using one bit, not zero bits; in 596 this case there is a single code length of one, with one unused 597 code. One distance code of zero bits means that there are no 599 Deutsch [Page 12] 600 distance codes used at all (the data is all literals). 602 We can now define the format of the block: 604 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286) 605 5 Bits: HDIST, # of Distance codes - 1 (1 - 32) 606 4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19) 608 (HCLEN + 4) x 3 bits: code lengths for the code length 609 alphabet given just above, in the order: 16, 17, 18, 610 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 612 These code lengths are interpreted as 3-bit integers 613 (0-7); as above, a code length of 0 means the 614 corresponding symbol (literal/length or distance code 615 length) is not used. 617 HLIT + 257 code lengths for the literal/length alphabet, 618 encoded using the code length Huffman code 620 HDIST + 1 code lengths for the distance alphabet, 621 encoded using the code length Huffman code 623 The actual compressed data of the block, 624 encoded using the literal/length and distance Huffman 625 codes 627 The literal/length symbol 256 (end of data), 628 encoded using the literal/length Huffman code 630 The code length repeat codes can cross from HLIT + 257 to the 631 HDIST + 1 code lengths. In other words, all code lengths form 632 a single sequence of HLIT + HDIST + 258 values. 634 3.3. Compliance 636 A compressor may limit further the ranges of values specified in 637 the previous section and still be compliant; for example, it may 638 limit the range of backward pointers to some value smaller than 639 32K. Similarly, a compressor may limit the size of blocks so that 640 a compressible block fits in memory. 642 A compliant decompressor must accept the full range of possible 643 values defined in the previous section, and must accept blocks of 644 arbitrary size. 646 Deutsch [Page 13] 647 4. Compression algorithm details 649 While it is the intent of this document to define the 'deflate' 650 compressed data format without reference to any particular 651 compression algorithm, the format is related to the compressed 652 formats produced by LZ77 (Lempel-Ziv 1977, see reference [2] below); 653 since many variations of LZ77 are patented, it is strongly 654 recommended that the implementor of a compressor follow the general 655 algorithm presented here, which is known not to be patented per se. 656 The material in this section is not part of the definition of the 657 specification per se, and a compressor need not follow it in order to 658 be compliant. 660 The compressor terminates a block when it determines that starting a 661 new block with fresh trees would be useful, or when the block size 662 fills up the compressor's block buffer. 664 The compressor uses a chained hash table to find duplicated strings, 665 using a hash function that operates on 3-byte sequences. At any 666 given point during compression, let XYZ be the next 3 input bytes to 667 be examined (not necessarily all different, of course). First, the 668 compressor examines the hash chain for XYZ. If the chain is empty, 669 the compressor simply writes out X as a literal byte and advances one 670 byte in the input. If the hash chain is not empty, indicating that 671 the sequence XYZ (or, if we are unlucky, some other 3 bytes with the 672 same hash function value) has occurred recently, the compressor 673 compares all strings on the XYZ hash chain with the actual input data 674 sequence starting at the current point, and selects the longest 675 match. 677 The compressor searches the hash chains starting with the most recent 678 strings, to favor small distances and thus take advantage of the 679 Huffman encoding. The hash chains are singly linked. There are no 680 deletions from the hash chains; the algorithm simply discards matches 681 that are too old. To avoid a worst-case situation, very long hash 682 chains are arbitrarily truncated at a certain length, determined by a 683 run-time parameter. 685 To improve overall compression, the compressor optionally defers the 686 selection of matches ("lazy matching"): after a match of length N has 687 been found, the compressor searches for a longer match starting at 688 the next input byte. If it finds a longer match, it truncates the 689 previous match to a length of one (thus producing a single literal 690 byte) and then emits the longer match. Otherwise, it emits the 691 original match, and, as described above, advances N bytes before 692 continuing. 694 Run-time parameters also control this "lazy match" procedure. If 695 compression ratio is most important, the compressor attempts a 696 complete second search regardless of the length of the first match. 697 In the normal case, if the current match is "long enough", the 698 compressor reduces the search for a longer match, thus speeding up 700 Deutsch [Page 14] 701 the process. If speed is most important, the compressor inserts new 702 strings in the hash table only when no match was found, or when the 703 match is not "too long". This degrades the compression ratio but 704 saves time since there are both fewer insertions and fewer searches. 706 5. References 708 [1] Huffman, D. A., "A Method for the Construction of Minimum 709 Redundancy Codes", Proceedings of the Institute of Radio 710 Engineers, September 1952, Volume 40, Number 9, pp. 1098-1101. 712 [2] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data 713 Compression", IEEE Transactions on Information Theory, Vol. 23, 714 No. 3, pp. 337-343. 716 [3] Gailly, J.-L., and Adler, M., zlib documentation and sources, 717 available in ftp.uu.net:/pub/archiving/zip/doc/zlib* 719 [4] Gailly, J.-L., and Adler, M., gzip documentation and sources, 720 available in prep.ai.mit.edu:/pub/gnu/gzip-*.tar 722 [5] Schwartz, E. S., and Kallick, B. "Generating a canonical prefix 723 encoding." Comm. ACM, 7,3 (Mar. 1964), pp. 166-169. 725 [6] "Efficient decoding of prefix codes", Hirschberg and Lelewer, 726 Comm. ACM, 33,4, April 1990, pp. 449-459. 728 6. Security considerations 730 Any data compression method involves the reduction of redundancy in 731 the data. Consequently, any corruption of the data is likely to have 732 severe effects and be difficult to correct. Uncompressed text, on 733 the other hand, will probably still be readable despite the presence 734 of some corrupted bytes. 736 It is recommended that systems using this data format provide some 737 means of validating the integrity of the compressed data. See 738 reference [3], for example. 740 7. Source code 742 Source code for a C language implementation of a 'deflate' compliant 743 compressor and decompressor is available within the zlib package at 744 ftp.uu.net:/pub/archiving/zip/zlib/zlib*. 746 Deutsch [Page 15] 747 8. Acknowledgements 749 Trademarks cited in this document are the property of their 750 respective owners. 752 Phil Katz designed the deflate format. Jean-Loup Gailly and Mark 753 Adler wrote the related software described in this specification. 754 Glenn Randers-Pehrson converted this document to Internet Draft and 755 HTML format. 757 9. Author's address 759 L. Peter Deutsch 761 Aladdin Enterprises 762 203 Santa Margarita Ave. 763 Menlo Park, CA 94025 765 Phone: (415) 322-0103 (AM only) 766 FAX: (415) 322-1734 767 EMail: 769 Questions about the technical content of this specification can be 770 sent by email to 772 Jean-loup Gailly and 773 Mark Adler 775 Editorial comments on this specification can be sent by email to 777 L. Peter Deutsch and 778 Glenn Randers-Pehrson 780 Deutsch [Page 16]