annotate src/zlib-1.2.8/doc/rfc1951.txt @ 81:7029a4916348

Merge build update
author Chris Cannam
date Thu, 31 Oct 2019 13:36:58 +0000
parents 5ea0608b923f
children
rev   line source
Chris@43 1
Chris@43 2
Chris@43 3
Chris@43 4
Chris@43 5
Chris@43 6
Chris@43 7 Network Working Group P. Deutsch
Chris@43 8 Request for Comments: 1951 Aladdin Enterprises
Chris@43 9 Category: Informational May 1996
Chris@43 10
Chris@43 11
Chris@43 12 DEFLATE Compressed Data Format Specification version 1.3
Chris@43 13
Chris@43 14 Status of This Memo
Chris@43 15
Chris@43 16 This memo provides information for the Internet community. This memo
Chris@43 17 does not specify an Internet standard of any kind. Distribution of
Chris@43 18 this memo is unlimited.
Chris@43 19
Chris@43 20 IESG Note:
Chris@43 21
Chris@43 22 The IESG takes no position on the validity of any Intellectual
Chris@43 23 Property Rights statements contained in this document.
Chris@43 24
Chris@43 25 Notices
Chris@43 26
Chris@43 27 Copyright (c) 1996 L. Peter Deutsch
Chris@43 28
Chris@43 29 Permission is granted to copy and distribute this document for any
Chris@43 30 purpose and without charge, including translations into other
Chris@43 31 languages and incorporation into compilations, provided that the
Chris@43 32 copyright notice and this notice are preserved, and that any
Chris@43 33 substantive changes or deletions from the original are clearly
Chris@43 34 marked.
Chris@43 35
Chris@43 36 A pointer to the latest version of this and related documentation in
Chris@43 37 HTML format can be found at the URL
Chris@43 38 <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>.
Chris@43 39
Chris@43 40 Abstract
Chris@43 41
Chris@43 42 This specification defines a lossless compressed data format that
Chris@43 43 compresses data using a combination of the LZ77 algorithm and Huffman
Chris@43 44 coding, with efficiency comparable to the best currently available
Chris@43 45 general-purpose compression methods. The data can be produced or
Chris@43 46 consumed, even for an arbitrarily long sequentially presented input
Chris@43 47 data stream, using only an a priori bounded amount of intermediate
Chris@43 48 storage. The format can be implemented readily in a manner not
Chris@43 49 covered by patents.
Chris@43 50
Chris@43 51
Chris@43 52
Chris@43 53
Chris@43 54
Chris@43 55
Chris@43 56
Chris@43 57
Chris@43 58 Deutsch Informational [Page 1]
Chris@43 59
Chris@43 60 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 61
Chris@43 62
Chris@43 63 Table of Contents
Chris@43 64
Chris@43 65 1. Introduction ................................................... 2
Chris@43 66 1.1. Purpose ................................................... 2
Chris@43 67 1.2. Intended audience ......................................... 3
Chris@43 68 1.3. Scope ..................................................... 3
Chris@43 69 1.4. Compliance ................................................ 3
Chris@43 70 1.5. Definitions of terms and conventions used ................ 3
Chris@43 71 1.6. Changes from previous versions ............................ 4
Chris@43 72 2. Compressed representation overview ............................. 4
Chris@43 73 3. Detailed specification ......................................... 5
Chris@43 74 3.1. Overall conventions ....................................... 5
Chris@43 75 3.1.1. Packing into bytes .................................. 5
Chris@43 76 3.2. Compressed block format ................................... 6
Chris@43 77 3.2.1. Synopsis of prefix and Huffman coding ............... 6
Chris@43 78 3.2.2. Use of Huffman coding in the "deflate" format ....... 7
Chris@43 79 3.2.3. Details of block format ............................. 9
Chris@43 80 3.2.4. Non-compressed blocks (BTYPE=00) ................... 11
Chris@43 81 3.2.5. Compressed blocks (length and distance codes) ...... 11
Chris@43 82 3.2.6. Compression with fixed Huffman codes (BTYPE=01) .... 12
Chris@43 83 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) .. 13
Chris@43 84 3.3. Compliance ............................................... 14
Chris@43 85 4. Compression algorithm details ................................. 14
Chris@43 86 5. References .................................................... 16
Chris@43 87 6. Security Considerations ....................................... 16
Chris@43 88 7. Source code ................................................... 16
Chris@43 89 8. Acknowledgements .............................................. 16
Chris@43 90 9. Author's Address .............................................. 17
Chris@43 91
Chris@43 92 1. Introduction
Chris@43 93
Chris@43 94 1.1. Purpose
Chris@43 95
Chris@43 96 The purpose of this specification is to define a lossless
Chris@43 97 compressed data format that:
Chris@43 98 * Is independent of CPU type, operating system, file system,
Chris@43 99 and character set, and hence can be used for interchange;
Chris@43 100 * Can be produced or consumed, even for an arbitrarily long
Chris@43 101 sequentially presented input data stream, using only an a
Chris@43 102 priori bounded amount of intermediate storage, and hence
Chris@43 103 can be used in data communications or similar structures
Chris@43 104 such as Unix filters;
Chris@43 105 * Compresses data with efficiency comparable to the best
Chris@43 106 currently available general-purpose compression methods,
Chris@43 107 and in particular considerably better than the "compress"
Chris@43 108 program;
Chris@43 109 * Can be implemented readily in a manner not covered by
Chris@43 110 patents, and hence can be practiced freely;
Chris@43 111
Chris@43 112
Chris@43 113
Chris@43 114 Deutsch Informational [Page 2]
Chris@43 115
Chris@43 116 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 117
Chris@43 118
Chris@43 119 * Is compatible with the file format produced by the current
Chris@43 120 widely used gzip utility, in that conforming decompressors
Chris@43 121 will be able to read data produced by the existing gzip
Chris@43 122 compressor.
Chris@43 123
Chris@43 124 The data format defined by this specification does not attempt to:
Chris@43 125
Chris@43 126 * Allow random access to compressed data;
Chris@43 127 * Compress specialized data (e.g., raster graphics) as well
Chris@43 128 as the best currently available specialized algorithms.
Chris@43 129
Chris@43 130 A simple counting argument shows that no lossless compression
Chris@43 131 algorithm can compress every possible input data set. For the
Chris@43 132 format defined here, the worst case expansion is 5 bytes per 32K-
Chris@43 133 byte block, i.e., a size increase of 0.015% for large data sets.
Chris@43 134 English text usually compresses by a factor of 2.5 to 3;
Chris@43 135 executable files usually compress somewhat less; graphical data
Chris@43 136 such as raster images may compress much more.
Chris@43 137
Chris@43 138 1.2. Intended audience
Chris@43 139
Chris@43 140 This specification is intended for use by implementors of software
Chris@43 141 to compress data into "deflate" format and/or decompress data from
Chris@43 142 "deflate" format.
Chris@43 143
Chris@43 144 The text of the specification assumes a basic background in
Chris@43 145 programming at the level of bits and other primitive data
Chris@43 146 representations. Familiarity with the technique of Huffman coding
Chris@43 147 is helpful but not required.
Chris@43 148
Chris@43 149 1.3. Scope
Chris@43 150
Chris@43 151 The specification specifies a method for representing a sequence
Chris@43 152 of bytes as a (usually shorter) sequence of bits, and a method for
Chris@43 153 packing the latter bit sequence into bytes.
Chris@43 154
Chris@43 155 1.4. Compliance
Chris@43 156
Chris@43 157 Unless otherwise indicated below, a compliant decompressor must be
Chris@43 158 able to accept and decompress any data set that conforms to all
Chris@43 159 the specifications presented here; a compliant compressor must
Chris@43 160 produce data sets that conform to all the specifications presented
Chris@43 161 here.
Chris@43 162
Chris@43 163 1.5. Definitions of terms and conventions used
Chris@43 164
Chris@43 165 Byte: 8 bits stored or transmitted as a unit (same as an octet).
Chris@43 166 For this specification, a byte is exactly 8 bits, even on machines
Chris@43 167
Chris@43 168
Chris@43 169
Chris@43 170 Deutsch Informational [Page 3]
Chris@43 171
Chris@43 172 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 173
Chris@43 174
Chris@43 175 which store a character on a number of bits different from eight.
Chris@43 176 See below, for the numbering of bits within a byte.
Chris@43 177
Chris@43 178 String: a sequence of arbitrary bytes.
Chris@43 179
Chris@43 180 1.6. Changes from previous versions
Chris@43 181
Chris@43 182 There have been no technical changes to the deflate format since
Chris@43 183 version 1.1 of this specification. In version 1.2, some
Chris@43 184 terminology was changed. Version 1.3 is a conversion of the
Chris@43 185 specification to RFC style.
Chris@43 186
Chris@43 187 2. Compressed representation overview
Chris@43 188
Chris@43 189 A compressed data set consists of a series of blocks, corresponding
Chris@43 190 to successive blocks of input data. The block sizes are arbitrary,
Chris@43 191 except that non-compressible blocks are limited to 65,535 bytes.
Chris@43 192
Chris@43 193 Each block is compressed using a combination of the LZ77 algorithm
Chris@43 194 and Huffman coding. The Huffman trees for each block are independent
Chris@43 195 of those for previous or subsequent blocks; the LZ77 algorithm may
Chris@43 196 use a reference to a duplicated string occurring in a previous block,
Chris@43 197 up to 32K input bytes before.
Chris@43 198
Chris@43 199 Each block consists of two parts: a pair of Huffman code trees that
Chris@43 200 describe the representation of the compressed data part, and a
Chris@43 201 compressed data part. (The Huffman trees themselves are compressed
Chris@43 202 using Huffman encoding.) The compressed data consists of a series of
Chris@43 203 elements of two types: literal bytes (of strings that have not been
Chris@43 204 detected as duplicated within the previous 32K input bytes), and
Chris@43 205 pointers to duplicated strings, where a pointer is represented as a
Chris@43 206 pair <length, backward distance>. The representation used in the
Chris@43 207 "deflate" format limits distances to 32K bytes and lengths to 258
Chris@43 208 bytes, but does not limit the size of a block, except for
Chris@43 209 uncompressible blocks, which are limited as noted above.
Chris@43 210
Chris@43 211 Each type of value (literals, distances, and lengths) in the
Chris@43 212 compressed data is represented using a Huffman code, using one code
Chris@43 213 tree for literals and lengths and a separate code tree for distances.
Chris@43 214 The code trees for each block appear in a compact form just before
Chris@43 215 the compressed data for that block.
Chris@43 216
Chris@43 217
Chris@43 218
Chris@43 219
Chris@43 220
Chris@43 221
Chris@43 222
Chris@43 223
Chris@43 224
Chris@43 225
Chris@43 226 Deutsch Informational [Page 4]
Chris@43 227
Chris@43 228 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 229
Chris@43 230
Chris@43 231 3. Detailed specification
Chris@43 232
Chris@43 233 3.1. Overall conventions In the diagrams below, a box like this:
Chris@43 234
Chris@43 235 +---+
Chris@43 236 | | <-- the vertical bars might be missing
Chris@43 237 +---+
Chris@43 238
Chris@43 239 represents one byte; a box like this:
Chris@43 240
Chris@43 241 +==============+
Chris@43 242 | |
Chris@43 243 +==============+
Chris@43 244
Chris@43 245 represents a variable number of bytes.
Chris@43 246
Chris@43 247 Bytes stored within a computer do not have a "bit order", since
Chris@43 248 they are always treated as a unit. However, a byte considered as
Chris@43 249 an integer between 0 and 255 does have a most- and least-
Chris@43 250 significant bit, and since we write numbers with the most-
Chris@43 251 significant digit on the left, we also write bytes with the most-
Chris@43 252 significant bit on the left. In the diagrams below, we number the
Chris@43 253 bits of a byte so that bit 0 is the least-significant bit, i.e.,
Chris@43 254 the bits are numbered:
Chris@43 255
Chris@43 256 +--------+
Chris@43 257 |76543210|
Chris@43 258 +--------+
Chris@43 259
Chris@43 260 Within a computer, a number may occupy multiple bytes. All
Chris@43 261 multi-byte numbers in the format described here are stored with
Chris@43 262 the least-significant byte first (at the lower memory address).
Chris@43 263 For example, the decimal number 520 is stored as:
Chris@43 264
Chris@43 265 0 1
Chris@43 266 +--------+--------+
Chris@43 267 |00001000|00000010|
Chris@43 268 +--------+--------+
Chris@43 269 ^ ^
Chris@43 270 | |
Chris@43 271 | + more significant byte = 2 x 256
Chris@43 272 + less significant byte = 8
Chris@43 273
Chris@43 274 3.1.1. Packing into bytes
Chris@43 275
Chris@43 276 This document does not address the issue of the order in which
Chris@43 277 bits of a byte are transmitted on a bit-sequential medium,
Chris@43 278 since the final data format described here is byte- rather than
Chris@43 279
Chris@43 280
Chris@43 281
Chris@43 282 Deutsch Informational [Page 5]
Chris@43 283
Chris@43 284 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 285
Chris@43 286
Chris@43 287 bit-oriented. However, we describe the compressed block format
Chris@43 288 in below, as a sequence of data elements of various bit
Chris@43 289 lengths, not a sequence of bytes. We must therefore specify
Chris@43 290 how to pack these data elements into bytes to form the final
Chris@43 291 compressed byte sequence:
Chris@43 292
Chris@43 293 * Data elements are packed into bytes in order of
Chris@43 294 increasing bit number within the byte, i.e., starting
Chris@43 295 with the least-significant bit of the byte.
Chris@43 296 * Data elements other than Huffman codes are packed
Chris@43 297 starting with the least-significant bit of the data
Chris@43 298 element.
Chris@43 299 * Huffman codes are packed starting with the most-
Chris@43 300 significant bit of the code.
Chris@43 301
Chris@43 302 In other words, if one were to print out the compressed data as
Chris@43 303 a sequence of bytes, starting with the first byte at the
Chris@43 304 *right* margin and proceeding to the *left*, with the most-
Chris@43 305 significant bit of each byte on the left as usual, one would be
Chris@43 306 able to parse the result from right to left, with fixed-width
Chris@43 307 elements in the correct MSB-to-LSB order and Huffman codes in
Chris@43 308 bit-reversed order (i.e., with the first bit of the code in the
Chris@43 309 relative LSB position).
Chris@43 310
Chris@43 311 3.2. Compressed block format
Chris@43 312
Chris@43 313 3.2.1. Synopsis of prefix and Huffman coding
Chris@43 314
Chris@43 315 Prefix coding represents symbols from an a priori known
Chris@43 316 alphabet by bit sequences (codes), one code for each symbol, in
Chris@43 317 a manner such that different symbols may be represented by bit
Chris@43 318 sequences of different lengths, but a parser can always parse
Chris@43 319 an encoded string unambiguously symbol-by-symbol.
Chris@43 320
Chris@43 321 We define a prefix code in terms of a binary tree in which the
Chris@43 322 two edges descending from each non-leaf node are labeled 0 and
Chris@43 323 1 and in which the leaf nodes correspond one-for-one with (are
Chris@43 324 labeled with) the symbols of the alphabet; then the code for a
Chris@43 325 symbol is the sequence of 0's and 1's on the edges leading from
Chris@43 326 the root to the leaf labeled with that symbol. For example:
Chris@43 327
Chris@43 328
Chris@43 329
Chris@43 330
Chris@43 331
Chris@43 332
Chris@43 333
Chris@43 334
Chris@43 335
Chris@43 336
Chris@43 337
Chris@43 338 Deutsch Informational [Page 6]
Chris@43 339
Chris@43 340 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 341
Chris@43 342
Chris@43 343 /\ Symbol Code
Chris@43 344 0 1 ------ ----
Chris@43 345 / \ A 00
Chris@43 346 /\ B B 1
Chris@43 347 0 1 C 011
Chris@43 348 / \ D 010
Chris@43 349 A /\
Chris@43 350 0 1
Chris@43 351 / \
Chris@43 352 D C
Chris@43 353
Chris@43 354 A parser can decode the next symbol from an encoded input
Chris@43 355 stream by walking down the tree from the root, at each step
Chris@43 356 choosing the edge corresponding to the next input bit.
Chris@43 357
Chris@43 358 Given an alphabet with known symbol frequencies, the Huffman
Chris@43 359 algorithm allows the construction of an optimal prefix code
Chris@43 360 (one which represents strings with those symbol frequencies
Chris@43 361 using the fewest bits of any possible prefix codes for that
Chris@43 362 alphabet). Such a code is called a Huffman code. (See
Chris@43 363 reference [1] in Chapter 5, references for additional
Chris@43 364 information on Huffman codes.)
Chris@43 365
Chris@43 366 Note that in the "deflate" format, the Huffman codes for the
Chris@43 367 various alphabets must not exceed certain maximum code lengths.
Chris@43 368 This constraint complicates the algorithm for computing code
Chris@43 369 lengths from symbol frequencies. Again, see Chapter 5,
Chris@43 370 references for details.
Chris@43 371
Chris@43 372 3.2.2. Use of Huffman coding in the "deflate" format
Chris@43 373
Chris@43 374 The Huffman codes used for each alphabet in the "deflate"
Chris@43 375 format have two additional rules:
Chris@43 376
Chris@43 377 * All codes of a given bit length have lexicographically
Chris@43 378 consecutive values, in the same order as the symbols
Chris@43 379 they represent;
Chris@43 380
Chris@43 381 * Shorter codes lexicographically precede longer codes.
Chris@43 382
Chris@43 383
Chris@43 384
Chris@43 385
Chris@43 386
Chris@43 387
Chris@43 388
Chris@43 389
Chris@43 390
Chris@43 391
Chris@43 392
Chris@43 393
Chris@43 394 Deutsch Informational [Page 7]
Chris@43 395
Chris@43 396 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 397
Chris@43 398
Chris@43 399 We could recode the example above to follow this rule as
Chris@43 400 follows, assuming that the order of the alphabet is ABCD:
Chris@43 401
Chris@43 402 Symbol Code
Chris@43 403 ------ ----
Chris@43 404 A 10
Chris@43 405 B 0
Chris@43 406 C 110
Chris@43 407 D 111
Chris@43 408
Chris@43 409 I.e., 0 precedes 10 which precedes 11x, and 110 and 111 are
Chris@43 410 lexicographically consecutive.
Chris@43 411
Chris@43 412 Given this rule, we can define the Huffman code for an alphabet
Chris@43 413 just by giving the bit lengths of the codes for each symbol of
Chris@43 414 the alphabet in order; this is sufficient to determine the
Chris@43 415 actual codes. In our example, the code is completely defined
Chris@43 416 by the sequence of bit lengths (2, 1, 3, 3). The following
Chris@43 417 algorithm generates the codes as integers, intended to be read
Chris@43 418 from most- to least-significant bit. The code lengths are
Chris@43 419 initially in tree[I].Len; the codes are produced in
Chris@43 420 tree[I].Code.
Chris@43 421
Chris@43 422 1) Count the number of codes for each code length. Let
Chris@43 423 bl_count[N] be the number of codes of length N, N >= 1.
Chris@43 424
Chris@43 425 2) Find the numerical value of the smallest code for each
Chris@43 426 code length:
Chris@43 427
Chris@43 428 code = 0;
Chris@43 429 bl_count[0] = 0;
Chris@43 430 for (bits = 1; bits <= MAX_BITS; bits++) {
Chris@43 431 code = (code + bl_count[bits-1]) << 1;
Chris@43 432 next_code[bits] = code;
Chris@43 433 }
Chris@43 434
Chris@43 435 3) Assign numerical values to all codes, using consecutive
Chris@43 436 values for all codes of the same length with the base
Chris@43 437 values determined at step 2. Codes that are never used
Chris@43 438 (which have a bit length of zero) must not be assigned a
Chris@43 439 value.
Chris@43 440
Chris@43 441 for (n = 0; n <= max_code; n++) {
Chris@43 442 len = tree[n].Len;
Chris@43 443 if (len != 0) {
Chris@43 444 tree[n].Code = next_code[len];
Chris@43 445 next_code[len]++;
Chris@43 446 }
Chris@43 447
Chris@43 448
Chris@43 449
Chris@43 450 Deutsch Informational [Page 8]
Chris@43 451
Chris@43 452 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 453
Chris@43 454
Chris@43 455 }
Chris@43 456
Chris@43 457 Example:
Chris@43 458
Chris@43 459 Consider the alphabet ABCDEFGH, with bit lengths (3, 3, 3, 3,
Chris@43 460 3, 2, 4, 4). After step 1, we have:
Chris@43 461
Chris@43 462 N bl_count[N]
Chris@43 463 - -----------
Chris@43 464 2 1
Chris@43 465 3 5
Chris@43 466 4 2
Chris@43 467
Chris@43 468 Step 2 computes the following next_code values:
Chris@43 469
Chris@43 470 N next_code[N]
Chris@43 471 - ------------
Chris@43 472 1 0
Chris@43 473 2 0
Chris@43 474 3 2
Chris@43 475 4 14
Chris@43 476
Chris@43 477 Step 3 produces the following code values:
Chris@43 478
Chris@43 479 Symbol Length Code
Chris@43 480 ------ ------ ----
Chris@43 481 A 3 010
Chris@43 482 B 3 011
Chris@43 483 C 3 100
Chris@43 484 D 3 101
Chris@43 485 E 3 110
Chris@43 486 F 2 00
Chris@43 487 G 4 1110
Chris@43 488 H 4 1111
Chris@43 489
Chris@43 490 3.2.3. Details of block format
Chris@43 491
Chris@43 492 Each block of compressed data begins with 3 header bits
Chris@43 493 containing the following data:
Chris@43 494
Chris@43 495 first bit BFINAL
Chris@43 496 next 2 bits BTYPE
Chris@43 497
Chris@43 498 Note that the header bits do not necessarily begin on a byte
Chris@43 499 boundary, since a block does not necessarily occupy an integral
Chris@43 500 number of bytes.
Chris@43 501
Chris@43 502
Chris@43 503
Chris@43 504
Chris@43 505
Chris@43 506 Deutsch Informational [Page 9]
Chris@43 507
Chris@43 508 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 509
Chris@43 510
Chris@43 511 BFINAL is set if and only if this is the last block of the data
Chris@43 512 set.
Chris@43 513
Chris@43 514 BTYPE specifies how the data are compressed, as follows:
Chris@43 515
Chris@43 516 00 - no compression
Chris@43 517 01 - compressed with fixed Huffman codes
Chris@43 518 10 - compressed with dynamic Huffman codes
Chris@43 519 11 - reserved (error)
Chris@43 520
Chris@43 521 The only difference between the two compressed cases is how the
Chris@43 522 Huffman codes for the literal/length and distance alphabets are
Chris@43 523 defined.
Chris@43 524
Chris@43 525 In all cases, the decoding algorithm for the actual data is as
Chris@43 526 follows:
Chris@43 527
Chris@43 528 do
Chris@43 529 read block header from input stream.
Chris@43 530 if stored with no compression
Chris@43 531 skip any remaining bits in current partially
Chris@43 532 processed byte
Chris@43 533 read LEN and NLEN (see next section)
Chris@43 534 copy LEN bytes of data to output
Chris@43 535 otherwise
Chris@43 536 if compressed with dynamic Huffman codes
Chris@43 537 read representation of code trees (see
Chris@43 538 subsection below)
Chris@43 539 loop (until end of block code recognized)
Chris@43 540 decode literal/length value from input stream
Chris@43 541 if value < 256
Chris@43 542 copy value (literal byte) to output stream
Chris@43 543 otherwise
Chris@43 544 if value = end of block (256)
Chris@43 545 break from loop
Chris@43 546 otherwise (value = 257..285)
Chris@43 547 decode distance from input stream
Chris@43 548
Chris@43 549 move backwards distance bytes in the output
Chris@43 550 stream, and copy length bytes from this
Chris@43 551 position to the output stream.
Chris@43 552 end loop
Chris@43 553 while not last block
Chris@43 554
Chris@43 555 Note that a duplicated string reference may refer to a string
Chris@43 556 in a previous block; i.e., the backward distance may cross one
Chris@43 557 or more block boundaries. However a distance cannot refer past
Chris@43 558 the beginning of the output stream. (An application using a
Chris@43 559
Chris@43 560
Chris@43 561
Chris@43 562 Deutsch Informational [Page 10]
Chris@43 563
Chris@43 564 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 565
Chris@43 566
Chris@43 567 preset dictionary might discard part of the output stream; a
Chris@43 568 distance can refer to that part of the output stream anyway)
Chris@43 569 Note also that the referenced string may overlap the current
Chris@43 570 position; for example, if the last 2 bytes decoded have values
Chris@43 571 X and Y, a string reference with <length = 5, distance = 2>
Chris@43 572 adds X,Y,X,Y,X to the output stream.
Chris@43 573
Chris@43 574 We now specify each compression method in turn.
Chris@43 575
Chris@43 576 3.2.4. Non-compressed blocks (BTYPE=00)
Chris@43 577
Chris@43 578 Any bits of input up to the next byte boundary are ignored.
Chris@43 579 The rest of the block consists of the following information:
Chris@43 580
Chris@43 581 0 1 2 3 4...
Chris@43 582 +---+---+---+---+================================+
Chris@43 583 | LEN | NLEN |... LEN bytes of literal data...|
Chris@43 584 +---+---+---+---+================================+
Chris@43 585
Chris@43 586 LEN is the number of data bytes in the block. NLEN is the
Chris@43 587 one's complement of LEN.
Chris@43 588
Chris@43 589 3.2.5. Compressed blocks (length and distance codes)
Chris@43 590
Chris@43 591 As noted above, encoded data blocks in the "deflate" format
Chris@43 592 consist of sequences of symbols drawn from three conceptually
Chris@43 593 distinct alphabets: either literal bytes, from the alphabet of
Chris@43 594 byte values (0..255), or <length, backward distance> pairs,
Chris@43 595 where the length is drawn from (3..258) and the distance is
Chris@43 596 drawn from (1..32,768). In fact, the literal and length
Chris@43 597 alphabets are merged into a single alphabet (0..285), where
Chris@43 598 values 0..255 represent literal bytes, the value 256 indicates
Chris@43 599 end-of-block, and values 257..285 represent length codes
Chris@43 600 (possibly in conjunction with extra bits following the symbol
Chris@43 601 code) as follows:
Chris@43 602
Chris@43 603
Chris@43 604
Chris@43 605
Chris@43 606
Chris@43 607
Chris@43 608
Chris@43 609
Chris@43 610
Chris@43 611
Chris@43 612
Chris@43 613
Chris@43 614
Chris@43 615
Chris@43 616
Chris@43 617
Chris@43 618 Deutsch Informational [Page 11]
Chris@43 619
Chris@43 620 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 621
Chris@43 622
Chris@43 623 Extra Extra Extra
Chris@43 624 Code Bits Length(s) Code Bits Lengths Code Bits Length(s)
Chris@43 625 ---- ---- ------ ---- ---- ------- ---- ---- -------
Chris@43 626 257 0 3 267 1 15,16 277 4 67-82
Chris@43 627 258 0 4 268 1 17,18 278 4 83-98
Chris@43 628 259 0 5 269 2 19-22 279 4 99-114
Chris@43 629 260 0 6 270 2 23-26 280 4 115-130
Chris@43 630 261 0 7 271 2 27-30 281 5 131-162
Chris@43 631 262 0 8 272 2 31-34 282 5 163-194
Chris@43 632 263 0 9 273 3 35-42 283 5 195-226
Chris@43 633 264 0 10 274 3 43-50 284 5 227-257
Chris@43 634 265 1 11,12 275 3 51-58 285 0 258
Chris@43 635 266 1 13,14 276 3 59-66
Chris@43 636
Chris@43 637 The extra bits should be interpreted as a machine integer
Chris@43 638 stored with the most-significant bit first, e.g., bits 1110
Chris@43 639 represent the value 14.
Chris@43 640
Chris@43 641 Extra Extra Extra
Chris@43 642 Code Bits Dist Code Bits Dist Code Bits Distance
Chris@43 643 ---- ---- ---- ---- ---- ------ ---- ---- --------
Chris@43 644 0 0 1 10 4 33-48 20 9 1025-1536
Chris@43 645 1 0 2 11 4 49-64 21 9 1537-2048
Chris@43 646 2 0 3 12 5 65-96 22 10 2049-3072
Chris@43 647 3 0 4 13 5 97-128 23 10 3073-4096
Chris@43 648 4 1 5,6 14 6 129-192 24 11 4097-6144
Chris@43 649 5 1 7,8 15 6 193-256 25 11 6145-8192
Chris@43 650 6 2 9-12 16 7 257-384 26 12 8193-12288
Chris@43 651 7 2 13-16 17 7 385-512 27 12 12289-16384
Chris@43 652 8 3 17-24 18 8 513-768 28 13 16385-24576
Chris@43 653 9 3 25-32 19 8 769-1024 29 13 24577-32768
Chris@43 654
Chris@43 655 3.2.6. Compression with fixed Huffman codes (BTYPE=01)
Chris@43 656
Chris@43 657 The Huffman codes for the two alphabets are fixed, and are not
Chris@43 658 represented explicitly in the data. The Huffman code lengths
Chris@43 659 for the literal/length alphabet are:
Chris@43 660
Chris@43 661 Lit Value Bits Codes
Chris@43 662 --------- ---- -----
Chris@43 663 0 - 143 8 00110000 through
Chris@43 664 10111111
Chris@43 665 144 - 255 9 110010000 through
Chris@43 666 111111111
Chris@43 667 256 - 279 7 0000000 through
Chris@43 668 0010111
Chris@43 669 280 - 287 8 11000000 through
Chris@43 670 11000111
Chris@43 671
Chris@43 672
Chris@43 673
Chris@43 674 Deutsch Informational [Page 12]
Chris@43 675
Chris@43 676 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 677
Chris@43 678
Chris@43 679 The code lengths are sufficient to generate the actual codes,
Chris@43 680 as described above; we show the codes in the table for added
Chris@43 681 clarity. Literal/length values 286-287 will never actually
Chris@43 682 occur in the compressed data, but participate in the code
Chris@43 683 construction.
Chris@43 684
Chris@43 685 Distance codes 0-31 are represented by (fixed-length) 5-bit
Chris@43 686 codes, with possible additional bits as shown in the table
Chris@43 687 shown in Paragraph 3.2.5, above. Note that distance codes 30-
Chris@43 688 31 will never actually occur in the compressed data.
Chris@43 689
Chris@43 690 3.2.7. Compression with dynamic Huffman codes (BTYPE=10)
Chris@43 691
Chris@43 692 The Huffman codes for the two alphabets appear in the block
Chris@43 693 immediately after the header bits and before the actual
Chris@43 694 compressed data, first the literal/length code and then the
Chris@43 695 distance code. Each code is defined by a sequence of code
Chris@43 696 lengths, as discussed in Paragraph 3.2.2, above. For even
Chris@43 697 greater compactness, the code length sequences themselves are
Chris@43 698 compressed using a Huffman code. The alphabet for code lengths
Chris@43 699 is as follows:
Chris@43 700
Chris@43 701 0 - 15: Represent code lengths of 0 - 15
Chris@43 702 16: Copy the previous code length 3 - 6 times.
Chris@43 703 The next 2 bits indicate repeat length
Chris@43 704 (0 = 3, ... , 3 = 6)
Chris@43 705 Example: Codes 8, 16 (+2 bits 11),
Chris@43 706 16 (+2 bits 10) will expand to
Chris@43 707 12 code lengths of 8 (1 + 6 + 5)
Chris@43 708 17: Repeat a code length of 0 for 3 - 10 times.
Chris@43 709 (3 bits of length)
Chris@43 710 18: Repeat a code length of 0 for 11 - 138 times
Chris@43 711 (7 bits of length)
Chris@43 712
Chris@43 713 A code length of 0 indicates that the corresponding symbol in
Chris@43 714 the literal/length or distance alphabet will not occur in the
Chris@43 715 block, and should not participate in the Huffman code
Chris@43 716 construction algorithm given earlier. If only one distance
Chris@43 717 code is used, it is encoded using one bit, not zero bits; in
Chris@43 718 this case there is a single code length of one, with one unused
Chris@43 719 code. One distance code of zero bits means that there are no
Chris@43 720 distance codes used at all (the data is all literals).
Chris@43 721
Chris@43 722 We can now define the format of the block:
Chris@43 723
Chris@43 724 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286)
Chris@43 725 5 Bits: HDIST, # of Distance codes - 1 (1 - 32)
Chris@43 726 4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19)
Chris@43 727
Chris@43 728
Chris@43 729
Chris@43 730 Deutsch Informational [Page 13]
Chris@43 731
Chris@43 732 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 733
Chris@43 734
Chris@43 735 (HCLEN + 4) x 3 bits: code lengths for the code length
Chris@43 736 alphabet given just above, in the order: 16, 17, 18,
Chris@43 737 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
Chris@43 738
Chris@43 739 These code lengths are interpreted as 3-bit integers
Chris@43 740 (0-7); as above, a code length of 0 means the
Chris@43 741 corresponding symbol (literal/length or distance code
Chris@43 742 length) is not used.
Chris@43 743
Chris@43 744 HLIT + 257 code lengths for the literal/length alphabet,
Chris@43 745 encoded using the code length Huffman code
Chris@43 746
Chris@43 747 HDIST + 1 code lengths for the distance alphabet,
Chris@43 748 encoded using the code length Huffman code
Chris@43 749
Chris@43 750 The actual compressed data of the block,
Chris@43 751 encoded using the literal/length and distance Huffman
Chris@43 752 codes
Chris@43 753
Chris@43 754 The literal/length symbol 256 (end of data),
Chris@43 755 encoded using the literal/length Huffman code
Chris@43 756
Chris@43 757 The code length repeat codes can cross from HLIT + 257 to the
Chris@43 758 HDIST + 1 code lengths. In other words, all code lengths form
Chris@43 759 a single sequence of HLIT + HDIST + 258 values.
Chris@43 760
Chris@43 761 3.3. Compliance
Chris@43 762
Chris@43 763 A compressor may limit further the ranges of values specified in
Chris@43 764 the previous section and still be compliant; for example, it may
Chris@43 765 limit the range of backward pointers to some value smaller than
Chris@43 766 32K. Similarly, a compressor may limit the size of blocks so that
Chris@43 767 a compressible block fits in memory.
Chris@43 768
Chris@43 769 A compliant decompressor must accept the full range of possible
Chris@43 770 values defined in the previous section, and must accept blocks of
Chris@43 771 arbitrary size.
Chris@43 772
Chris@43 773 4. Compression algorithm details
Chris@43 774
Chris@43 775 While it is the intent of this document to define the "deflate"
Chris@43 776 compressed data format without reference to any particular
Chris@43 777 compression algorithm, the format is related to the compressed
Chris@43 778 formats produced by LZ77 (Lempel-Ziv 1977, see reference [2] below);
Chris@43 779 since many variations of LZ77 are patented, it is strongly
Chris@43 780 recommended that the implementor of a compressor follow the general
Chris@43 781 algorithm presented here, which is known not to be patented per se.
Chris@43 782 The material in this section is not part of the definition of the
Chris@43 783
Chris@43 784
Chris@43 785
Chris@43 786 Deutsch Informational [Page 14]
Chris@43 787
Chris@43 788 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 789
Chris@43 790
Chris@43 791 specification per se, and a compressor need not follow it in order to
Chris@43 792 be compliant.
Chris@43 793
Chris@43 794 The compressor terminates a block when it determines that starting a
Chris@43 795 new block with fresh trees would be useful, or when the block size
Chris@43 796 fills up the compressor's block buffer.
Chris@43 797
Chris@43 798 The compressor uses a chained hash table to find duplicated strings,
Chris@43 799 using a hash function that operates on 3-byte sequences. At any
Chris@43 800 given point during compression, let XYZ be the next 3 input bytes to
Chris@43 801 be examined (not necessarily all different, of course). First, the
Chris@43 802 compressor examines the hash chain for XYZ. If the chain is empty,
Chris@43 803 the compressor simply writes out X as a literal byte and advances one
Chris@43 804 byte in the input. If the hash chain is not empty, indicating that
Chris@43 805 the sequence XYZ (or, if we are unlucky, some other 3 bytes with the
Chris@43 806 same hash function value) has occurred recently, the compressor
Chris@43 807 compares all strings on the XYZ hash chain with the actual input data
Chris@43 808 sequence starting at the current point, and selects the longest
Chris@43 809 match.
Chris@43 810
Chris@43 811 The compressor searches the hash chains starting with the most recent
Chris@43 812 strings, to favor small distances and thus take advantage of the
Chris@43 813 Huffman encoding. The hash chains are singly linked. There are no
Chris@43 814 deletions from the hash chains; the algorithm simply discards matches
Chris@43 815 that are too old. To avoid a worst-case situation, very long hash
Chris@43 816 chains are arbitrarily truncated at a certain length, determined by a
Chris@43 817 run-time parameter.
Chris@43 818
Chris@43 819 To improve overall compression, the compressor optionally defers the
Chris@43 820 selection of matches ("lazy matching"): after a match of length N has
Chris@43 821 been found, the compressor searches for a longer match starting at
Chris@43 822 the next input byte. If it finds a longer match, it truncates the
Chris@43 823 previous match to a length of one (thus producing a single literal
Chris@43 824 byte) and then emits the longer match. Otherwise, it emits the
Chris@43 825 original match, and, as described above, advances N bytes before
Chris@43 826 continuing.
Chris@43 827
Chris@43 828 Run-time parameters also control this "lazy match" procedure. If
Chris@43 829 compression ratio is most important, the compressor attempts a
Chris@43 830 complete second search regardless of the length of the first match.
Chris@43 831 In the normal case, if the current match is "long enough", the
Chris@43 832 compressor reduces the search for a longer match, thus speeding up
Chris@43 833 the process. If speed is most important, the compressor inserts new
Chris@43 834 strings in the hash table only when no match was found, or when the
Chris@43 835 match is not "too long". This degrades the compression ratio but
Chris@43 836 saves time since there are both fewer insertions and fewer searches.
Chris@43 837
Chris@43 838
Chris@43 839
Chris@43 840
Chris@43 841
Chris@43 842 Deutsch Informational [Page 15]
Chris@43 843
Chris@43 844 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 845
Chris@43 846
Chris@43 847 5. References
Chris@43 848
Chris@43 849 [1] Huffman, D. A., "A Method for the Construction of Minimum
Chris@43 850 Redundancy Codes", Proceedings of the Institute of Radio
Chris@43 851 Engineers, September 1952, Volume 40, Number 9, pp. 1098-1101.
Chris@43 852
Chris@43 853 [2] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data
Chris@43 854 Compression", IEEE Transactions on Information Theory, Vol. 23,
Chris@43 855 No. 3, pp. 337-343.
Chris@43 856
Chris@43 857 [3] Gailly, J.-L., and Adler, M., ZLIB documentation and sources,
Chris@43 858 available in ftp://ftp.uu.net/pub/archiving/zip/doc/
Chris@43 859
Chris@43 860 [4] Gailly, J.-L., and Adler, M., GZIP documentation and sources,
Chris@43 861 available as gzip-*.tar in ftp://prep.ai.mit.edu/pub/gnu/
Chris@43 862
Chris@43 863 [5] Schwartz, E. S., and Kallick, B. "Generating a canonical prefix
Chris@43 864 encoding." Comm. ACM, 7,3 (Mar. 1964), pp. 166-169.
Chris@43 865
Chris@43 866 [6] Hirschberg and Lelewer, "Efficient decoding of prefix codes,"
Chris@43 867 Comm. ACM, 33,4, April 1990, pp. 449-459.
Chris@43 868
Chris@43 869 6. Security Considerations
Chris@43 870
Chris@43 871 Any data compression method involves the reduction of redundancy in
Chris@43 872 the data. Consequently, any corruption of the data is likely to have
Chris@43 873 severe effects and be difficult to correct. Uncompressed text, on
Chris@43 874 the other hand, will probably still be readable despite the presence
Chris@43 875 of some corrupted bytes.
Chris@43 876
Chris@43 877 It is recommended that systems using this data format provide some
Chris@43 878 means of validating the integrity of the compressed data. See
Chris@43 879 reference [3], for example.
Chris@43 880
Chris@43 881 7. Source code
Chris@43 882
Chris@43 883 Source code for a C language implementation of a "deflate" compliant
Chris@43 884 compressor and decompressor is available within the zlib package at
Chris@43 885 ftp://ftp.uu.net/pub/archiving/zip/zlib/.
Chris@43 886
Chris@43 887 8. Acknowledgements
Chris@43 888
Chris@43 889 Trademarks cited in this document are the property of their
Chris@43 890 respective owners.
Chris@43 891
Chris@43 892 Phil Katz designed the deflate format. Jean-Loup Gailly and Mark
Chris@43 893 Adler wrote the related software described in this specification.
Chris@43 894 Glenn Randers-Pehrson converted this document to RFC and HTML format.
Chris@43 895
Chris@43 896
Chris@43 897
Chris@43 898 Deutsch Informational [Page 16]
Chris@43 899
Chris@43 900 RFC 1951 DEFLATE Compressed Data Format Specification May 1996
Chris@43 901
Chris@43 902
Chris@43 903 9. Author's Address
Chris@43 904
Chris@43 905 L. Peter Deutsch
Chris@43 906 Aladdin Enterprises
Chris@43 907 203 Santa Margarita Ave.
Chris@43 908 Menlo Park, CA 94025
Chris@43 909
Chris@43 910 Phone: (415) 322-0103 (AM only)
Chris@43 911 FAX: (415) 322-1734
Chris@43 912 EMail: <ghost@aladdin.com>
Chris@43 913
Chris@43 914 Questions about the technical content of this specification can be
Chris@43 915 sent by email to:
Chris@43 916
Chris@43 917 Jean-Loup Gailly <gzip@prep.ai.mit.edu> and
Chris@43 918 Mark Adler <madler@alumni.caltech.edu>
Chris@43 919
Chris@43 920 Editorial comments on this specification can be sent by email to:
Chris@43 921
Chris@43 922 L. Peter Deutsch <ghost@aladdin.com> and
Chris@43 923 Glenn Randers-Pehrson <randeg@alumni.rpi.edu>
Chris@43 924
Chris@43 925
Chris@43 926
Chris@43 927
Chris@43 928
Chris@43 929
Chris@43 930
Chris@43 931
Chris@43 932
Chris@43 933
Chris@43 934
Chris@43 935
Chris@43 936
Chris@43 937
Chris@43 938
Chris@43 939
Chris@43 940
Chris@43 941
Chris@43 942
Chris@43 943
Chris@43 944
Chris@43 945
Chris@43 946
Chris@43 947
Chris@43 948
Chris@43 949
Chris@43 950
Chris@43 951
Chris@43 952
Chris@43 953
Chris@43 954 Deutsch Informational [Page 17]
Chris@43 955