annotate src/zlib-1.2.8/doc/rfc1951.txt @ 169:223a55898ab9 tip default

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