annotate src/zlib-1.2.7/doc/rfc1951.txt @ 23:619f715526df sv_v2.1

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