|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Network Working Group P. Deutsch
|
|
kusano |
fc6ab3 |
Request for Comments: 1951 Aladdin Enterprises
|
|
kusano |
fc6ab3 |
Category: Informational May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
DEFLATE Compressed Data Format Specification version 1.3
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Status of This Memo
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
This memo provides information for the Internet community. This memo
|
|
kusano |
fc6ab3 |
does not specify an Internet standard of any kind. Distribution of
|
|
kusano |
fc6ab3 |
this memo is unlimited.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
IESG Note:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The IESG takes no position on the validity of any Intellectual
|
|
kusano |
fc6ab3 |
Property Rights statements contained in this document.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Notices
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Copyright (c) 1996 L. Peter Deutsch
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Permission is granted to copy and distribute this document for any
|
|
kusano |
fc6ab3 |
purpose and without charge, including translations into other
|
|
kusano |
fc6ab3 |
languages and incorporation into compilations, provided that the
|
|
kusano |
fc6ab3 |
copyright notice and this notice are preserved, and that any
|
|
kusano |
fc6ab3 |
substantive changes or deletions from the original are clearly
|
|
kusano |
fc6ab3 |
marked.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
A pointer to the latest version of this and related documentation in
|
|
kusano |
fc6ab3 |
HTML format can be found at the URL
|
|
kusano |
fc6ab3 |
<ftp: documents="" ftp.uu.net="" graphics="" png="" zdoc-index.html="" zlib="">.</ftp:>
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Abstract
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
This specification defines a lossless compressed data format that
|
|
kusano |
fc6ab3 |
compresses data using a combination of the LZ77 algorithm and Huffman
|
|
kusano |
fc6ab3 |
coding, with efficiency comparable to the best currently available
|
|
kusano |
fc6ab3 |
general-purpose compression methods. The data can be produced or
|
|
kusano |
fc6ab3 |
consumed, even for an arbitrarily long sequentially presented input
|
|
kusano |
fc6ab3 |
data stream, using only an a priori bounded amount of intermediate
|
|
kusano |
fc6ab3 |
storage. The format can be implemented readily in a manner not
|
|
kusano |
fc6ab3 |
covered by patents.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 1]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Table of Contents
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
1. Introduction ................................................... 2
|
|
kusano |
fc6ab3 |
1.1. Purpose ................................................... 2
|
|
kusano |
fc6ab3 |
1.2. Intended audience ......................................... 3
|
|
kusano |
fc6ab3 |
1.3. Scope ..................................................... 3
|
|
kusano |
fc6ab3 |
1.4. Compliance ................................................ 3
|
|
kusano |
fc6ab3 |
1.5. Definitions of terms and conventions used ................ 3
|
|
kusano |
fc6ab3 |
1.6. Changes from previous versions ............................ 4
|
|
kusano |
fc6ab3 |
2. Compressed representation overview ............................. 4
|
|
kusano |
fc6ab3 |
3. Detailed specification ......................................... 5
|
|
kusano |
fc6ab3 |
3.1. Overall conventions ....................................... 5
|
|
kusano |
fc6ab3 |
3.1.1. Packing into bytes .................................. 5
|
|
kusano |
fc6ab3 |
3.2. Compressed block format ................................... 6
|
|
kusano |
fc6ab3 |
3.2.1. Synopsis of prefix and Huffman coding ............... 6
|
|
kusano |
fc6ab3 |
3.2.2. Use of Huffman coding in the "deflate" format ....... 7
|
|
kusano |
fc6ab3 |
3.2.3. Details of block format ............................. 9
|
|
kusano |
fc6ab3 |
3.2.4. Non-compressed blocks (BTYPE=00) ................... 11
|
|
kusano |
fc6ab3 |
3.2.5. Compressed blocks (length and distance codes) ...... 11
|
|
kusano |
fc6ab3 |
3.2.6. Compression with fixed Huffman codes (BTYPE=01) .... 12
|
|
kusano |
fc6ab3 |
3.2.7. Compression with dynamic Huffman codes (BTYPE=10) .. 13
|
|
kusano |
fc6ab3 |
3.3. Compliance ............................................... 14
|
|
kusano |
fc6ab3 |
4. Compression algorithm details ................................. 14
|
|
kusano |
fc6ab3 |
5. References .................................................... 16
|
|
kusano |
fc6ab3 |
6. Security Considerations ....................................... 16
|
|
kusano |
fc6ab3 |
7. Source code ................................................... 16
|
|
kusano |
fc6ab3 |
8. Acknowledgements .............................................. 16
|
|
kusano |
fc6ab3 |
9. Author's Address .............................................. 17
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
1. Introduction
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
1.1. Purpose
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The purpose of this specification is to define a lossless
|
|
kusano |
fc6ab3 |
compressed data format that:
|
|
kusano |
fc6ab3 |
* Is independent of CPU type, operating system, file system,
|
|
kusano |
fc6ab3 |
and character set, and hence can be used for interchange;
|
|
kusano |
fc6ab3 |
* Can be produced or consumed, even for an arbitrarily long
|
|
kusano |
fc6ab3 |
sequentially presented input data stream, using only an a
|
|
kusano |
fc6ab3 |
priori bounded amount of intermediate storage, and hence
|
|
kusano |
fc6ab3 |
can be used in data communications or similar structures
|
|
kusano |
fc6ab3 |
such as Unix filters;
|
|
kusano |
fc6ab3 |
* Compresses data with efficiency comparable to the best
|
|
kusano |
fc6ab3 |
currently available general-purpose compression methods,
|
|
kusano |
fc6ab3 |
and in particular considerably better than the "compress"
|
|
kusano |
fc6ab3 |
program;
|
|
kusano |
fc6ab3 |
* Can be implemented readily in a manner not covered by
|
|
kusano |
fc6ab3 |
patents, and hence can be practiced freely;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 2]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
* Is compatible with the file format produced by the current
|
|
kusano |
fc6ab3 |
widely used gzip utility, in that conforming decompressors
|
|
kusano |
fc6ab3 |
will be able to read data produced by the existing gzip
|
|
kusano |
fc6ab3 |
compressor.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The data format defined by this specification does not attempt to:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
* Allow random access to compressed data;
|
|
kusano |
fc6ab3 |
* Compress specialized data (e.g., raster graphics) as well
|
|
kusano |
fc6ab3 |
as the best currently available specialized algorithms.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
A simple counting argument shows that no lossless compression
|
|
kusano |
fc6ab3 |
algorithm can compress every possible input data set. For the
|
|
kusano |
fc6ab3 |
format defined here, the worst case expansion is 5 bytes per 32K-
|
|
kusano |
fc6ab3 |
byte block, i.e., a size increase of 0.015% for large data sets.
|
|
kusano |
fc6ab3 |
English text usually compresses by a factor of 2.5 to 3;
|
|
kusano |
fc6ab3 |
executable files usually compress somewhat less; graphical data
|
|
kusano |
fc6ab3 |
such as raster images may compress much more.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
1.2. Intended audience
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
This specification is intended for use by implementors of software
|
|
kusano |
fc6ab3 |
to compress data into "deflate" format and/or decompress data from
|
|
kusano |
fc6ab3 |
"deflate" format.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The text of the specification assumes a basic background in
|
|
kusano |
fc6ab3 |
programming at the level of bits and other primitive data
|
|
kusano |
fc6ab3 |
representations. Familiarity with the technique of Huffman coding
|
|
kusano |
fc6ab3 |
is helpful but not required.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
1.3. Scope
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The specification specifies a method for representing a sequence
|
|
kusano |
fc6ab3 |
of bytes as a (usually shorter) sequence of bits, and a method for
|
|
kusano |
fc6ab3 |
packing the latter bit sequence into bytes.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
1.4. Compliance
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Unless otherwise indicated below, a compliant decompressor must be
|
|
kusano |
fc6ab3 |
able to accept and decompress any data set that conforms to all
|
|
kusano |
fc6ab3 |
the specifications presented here; a compliant compressor must
|
|
kusano |
fc6ab3 |
produce data sets that conform to all the specifications presented
|
|
kusano |
fc6ab3 |
here.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
1.5. Definitions of terms and conventions used
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Byte: 8 bits stored or transmitted as a unit (same as an octet).
|
|
kusano |
fc6ab3 |
For this specification, a byte is exactly 8 bits, even on machines
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 3]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
which store a character on a number of bits different from eight.
|
|
kusano |
fc6ab3 |
See below, for the numbering of bits within a byte.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
String: a sequence of arbitrary bytes.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
1.6. Changes from previous versions
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
There have been no technical changes to the deflate format since
|
|
kusano |
fc6ab3 |
version 1.1 of this specification. In version 1.2, some
|
|
kusano |
fc6ab3 |
terminology was changed. Version 1.3 is a conversion of the
|
|
kusano |
fc6ab3 |
specification to RFC style.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
2. Compressed representation overview
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
A compressed data set consists of a series of blocks, corresponding
|
|
kusano |
fc6ab3 |
to successive blocks of input data. The block sizes are arbitrary,
|
|
kusano |
fc6ab3 |
except that non-compressible blocks are limited to 65,535 bytes.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Each block is compressed using a combination of the LZ77 algorithm
|
|
kusano |
fc6ab3 |
and Huffman coding. The Huffman trees for each block are independent
|
|
kusano |
fc6ab3 |
of those for previous or subsequent blocks; the LZ77 algorithm may
|
|
kusano |
fc6ab3 |
use a reference to a duplicated string occurring in a previous block,
|
|
kusano |
fc6ab3 |
up to 32K input bytes before.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Each block consists of two parts: a pair of Huffman code trees that
|
|
kusano |
fc6ab3 |
describe the representation of the compressed data part, and a
|
|
kusano |
fc6ab3 |
compressed data part. (The Huffman trees themselves are compressed
|
|
kusano |
fc6ab3 |
using Huffman encoding.) The compressed data consists of a series of
|
|
kusano |
fc6ab3 |
elements of two types: literal bytes (of strings that have not been
|
|
kusano |
fc6ab3 |
detected as duplicated within the previous 32K input bytes), and
|
|
kusano |
fc6ab3 |
pointers to duplicated strings, where a pointer is represented as a
|
|
kusano |
fc6ab3 |
pair <length, backward="" distance="">. The representation used in the</length,>
|
|
kusano |
fc6ab3 |
"deflate" format limits distances to 32K bytes and lengths to 258
|
|
kusano |
fc6ab3 |
bytes, but does not limit the size of a block, except for
|
|
kusano |
fc6ab3 |
uncompressible blocks, which are limited as noted above.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Each type of value (literals, distances, and lengths) in the
|
|
kusano |
fc6ab3 |
compressed data is represented using a Huffman code, using one code
|
|
kusano |
fc6ab3 |
tree for literals and lengths and a separate code tree for distances.
|
|
kusano |
fc6ab3 |
The code trees for each block appear in a compact form just before
|
|
kusano |
fc6ab3 |
the compressed data for that block.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 4]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3. Detailed specification
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.1. Overall conventions In the diagrams below, a box like this:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
+---+
|
|
kusano |
fc6ab3 |
| | <-- the vertical bars might be missing
|
|
kusano |
fc6ab3 |
+---+
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
represents one byte; a box like this:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
+==============+
|
|
kusano |
fc6ab3 |
| |
|
|
kusano |
fc6ab3 |
+==============+
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
represents a variable number of bytes.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Bytes stored within a computer do not have a "bit order", since
|
|
kusano |
fc6ab3 |
they are always treated as a unit. However, a byte considered as
|
|
kusano |
fc6ab3 |
an integer between 0 and 255 does have a most- and least-
|
|
kusano |
fc6ab3 |
significant bit, and since we write numbers with the most-
|
|
kusano |
fc6ab3 |
significant digit on the left, we also write bytes with the most-
|
|
kusano |
fc6ab3 |
significant bit on the left. In the diagrams below, we number the
|
|
kusano |
fc6ab3 |
bits of a byte so that bit 0 is the least-significant bit, i.e.,
|
|
kusano |
fc6ab3 |
the bits are numbered:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
+--------+
|
|
kusano |
fc6ab3 |
|76543210|
|
|
kusano |
fc6ab3 |
+--------+
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Within a computer, a number may occupy multiple bytes. All
|
|
kusano |
fc6ab3 |
multi-byte numbers in the format described here are stored with
|
|
kusano |
fc6ab3 |
the least-significant byte first (at the lower memory address).
|
|
kusano |
fc6ab3 |
For example, the decimal number 520 is stored as:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
0 1
|
|
kusano |
fc6ab3 |
+--------+--------+
|
|
kusano |
fc6ab3 |
|00001000|00000010|
|
|
kusano |
fc6ab3 |
+--------+--------+
|
|
kusano |
fc6ab3 |
^ ^
|
|
kusano |
fc6ab3 |
| |
|
|
kusano |
fc6ab3 |
| + more significant byte = 2 x 256
|
|
kusano |
fc6ab3 |
+ less significant byte = 8
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.1.1. Packing into bytes
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
This document does not address the issue of the order in which
|
|
kusano |
fc6ab3 |
bits of a byte are transmitted on a bit-sequential medium,
|
|
kusano |
fc6ab3 |
since the final data format described here is byte- rather than
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 5]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
bit-oriented. However, we describe the compressed block format
|
|
kusano |
fc6ab3 |
in below, as a sequence of data elements of various bit
|
|
kusano |
fc6ab3 |
lengths, not a sequence of bytes. We must therefore specify
|
|
kusano |
fc6ab3 |
how to pack these data elements into bytes to form the final
|
|
kusano |
fc6ab3 |
compressed byte sequence:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
* Data elements are packed into bytes in order of
|
|
kusano |
fc6ab3 |
increasing bit number within the byte, i.e., starting
|
|
kusano |
fc6ab3 |
with the least-significant bit of the byte.
|
|
kusano |
fc6ab3 |
* Data elements other than Huffman codes are packed
|
|
kusano |
fc6ab3 |
starting with the least-significant bit of the data
|
|
kusano |
fc6ab3 |
element.
|
|
kusano |
fc6ab3 |
* Huffman codes are packed starting with the most-
|
|
kusano |
fc6ab3 |
significant bit of the code.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
In other words, if one were to print out the compressed data as
|
|
kusano |
fc6ab3 |
a sequence of bytes, starting with the first byte at the
|
|
kusano |
fc6ab3 |
*right* margin and proceeding to the *left*, with the most-
|
|
kusano |
fc6ab3 |
significant bit of each byte on the left as usual, one would be
|
|
kusano |
fc6ab3 |
able to parse the result from right to left, with fixed-width
|
|
kusano |
fc6ab3 |
elements in the correct MSB-to-LSB order and Huffman codes in
|
|
kusano |
fc6ab3 |
bit-reversed order (i.e., with the first bit of the code in the
|
|
kusano |
fc6ab3 |
relative LSB position).
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.2. Compressed block format
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.2.1. Synopsis of prefix and Huffman coding
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Prefix coding represents symbols from an a priori known
|
|
kusano |
fc6ab3 |
alphabet by bit sequences (codes), one code for each symbol, in
|
|
kusano |
fc6ab3 |
a manner such that different symbols may be represented by bit
|
|
kusano |
fc6ab3 |
sequences of different lengths, but a parser can always parse
|
|
kusano |
fc6ab3 |
an encoded string unambiguously symbol-by-symbol.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
We define a prefix code in terms of a binary tree in which the
|
|
kusano |
fc6ab3 |
two edges descending from each non-leaf node are labeled 0 and
|
|
kusano |
fc6ab3 |
1 and in which the leaf nodes correspond one-for-one with (are
|
|
kusano |
fc6ab3 |
labeled with) the symbols of the alphabet; then the code for a
|
|
kusano |
fc6ab3 |
symbol is the sequence of 0's and 1's on the edges leading from
|
|
kusano |
fc6ab3 |
the root to the leaf labeled with that symbol. For example:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 6]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/\ Symbol Code
|
|
kusano |
fc6ab3 |
0 1 ------ ----
|
|
kusano |
fc6ab3 |
/ \ A 00
|
|
kusano |
fc6ab3 |
/\ B B 1
|
|
kusano |
fc6ab3 |
0 1 C 011
|
|
kusano |
fc6ab3 |
/ \ D 010
|
|
kusano |
fc6ab3 |
A /\
|
|
kusano |
fc6ab3 |
0 1
|
|
kusano |
fc6ab3 |
/ \
|
|
kusano |
fc6ab3 |
D C
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
A parser can decode the next symbol from an encoded input
|
|
kusano |
fc6ab3 |
stream by walking down the tree from the root, at each step
|
|
kusano |
fc6ab3 |
choosing the edge corresponding to the next input bit.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Given an alphabet with known symbol frequencies, the Huffman
|
|
kusano |
fc6ab3 |
algorithm allows the construction of an optimal prefix code
|
|
kusano |
fc6ab3 |
(one which represents strings with those symbol frequencies
|
|
kusano |
fc6ab3 |
using the fewest bits of any possible prefix codes for that
|
|
kusano |
fc6ab3 |
alphabet). Such a code is called a Huffman code. (See
|
|
kusano |
fc6ab3 |
reference [1] in Chapter 5, references for additional
|
|
kusano |
fc6ab3 |
information on Huffman codes.)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Note that in the "deflate" format, the Huffman codes for the
|
|
kusano |
fc6ab3 |
various alphabets must not exceed certain maximum code lengths.
|
|
kusano |
fc6ab3 |
This constraint complicates the algorithm for computing code
|
|
kusano |
fc6ab3 |
lengths from symbol frequencies. Again, see Chapter 5,
|
|
kusano |
fc6ab3 |
references for details.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.2.2. Use of Huffman coding in the "deflate" format
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The Huffman codes used for each alphabet in the "deflate"
|
|
kusano |
fc6ab3 |
format have two additional rules:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
* All codes of a given bit length have lexicographically
|
|
kusano |
fc6ab3 |
consecutive values, in the same order as the symbols
|
|
kusano |
fc6ab3 |
they represent;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
* Shorter codes lexicographically precede longer codes.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 7]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
We could recode the example above to follow this rule as
|
|
kusano |
fc6ab3 |
follows, assuming that the order of the alphabet is ABCD:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Symbol Code
|
|
kusano |
fc6ab3 |
------ ----
|
|
kusano |
fc6ab3 |
A 10
|
|
kusano |
fc6ab3 |
B 0
|
|
kusano |
fc6ab3 |
C 110
|
|
kusano |
fc6ab3 |
D 111
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
I.e., 0 precedes 10 which precedes 11x, and 110 and 111 are
|
|
kusano |
fc6ab3 |
lexicographically consecutive.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Given this rule, we can define the Huffman code for an alphabet
|
|
kusano |
fc6ab3 |
just by giving the bit lengths of the codes for each symbol of
|
|
kusano |
fc6ab3 |
the alphabet in order; this is sufficient to determine the
|
|
kusano |
fc6ab3 |
actual codes. In our example, the code is completely defined
|
|
kusano |
fc6ab3 |
by the sequence of bit lengths (2, 1, 3, 3). The following
|
|
kusano |
fc6ab3 |
algorithm generates the codes as integers, intended to be read
|
|
kusano |
fc6ab3 |
from most- to least-significant bit. The code lengths are
|
|
kusano |
fc6ab3 |
initially in tree[I].Len; the codes are produced in
|
|
kusano |
fc6ab3 |
tree[I].Code.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
1) Count the number of codes for each code length. Let
|
|
kusano |
fc6ab3 |
bl_count[N] be the number of codes of length N, N >= 1.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
2) Find the numerical value of the smallest code for each
|
|
kusano |
fc6ab3 |
code length:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
code = 0;
|
|
kusano |
fc6ab3 |
bl_count[0] = 0;
|
|
kusano |
fc6ab3 |
for (bits = 1; bits <= MAX_BITS; bits++) {
|
|
kusano |
fc6ab3 |
code = (code + bl_count[bits-1]) << 1;
|
|
kusano |
fc6ab3 |
next_code[bits] = code;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3) Assign numerical values to all codes, using consecutive
|
|
kusano |
fc6ab3 |
values for all codes of the same length with the base
|
|
kusano |
fc6ab3 |
values determined at step 2. Codes that are never used
|
|
kusano |
fc6ab3 |
(which have a bit length of zero) must not be assigned a
|
|
kusano |
fc6ab3 |
value.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
for (n = 0; n <= max_code; n++) {
|
|
kusano |
fc6ab3 |
len = tree[n].Len;
|
|
kusano |
fc6ab3 |
if (len != 0) {
|
|
kusano |
fc6ab3 |
tree[n].Code = next_code[len];
|
|
kusano |
fc6ab3 |
next_code[len]++;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 8]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Example:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Consider the alphabet ABCDEFGH, with bit lengths (3, 3, 3, 3,
|
|
kusano |
fc6ab3 |
3, 2, 4, 4). After step 1, we have:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
N bl_count[N]
|
|
kusano |
fc6ab3 |
- -----------
|
|
kusano |
fc6ab3 |
2 1
|
|
kusano |
fc6ab3 |
3 5
|
|
kusano |
fc6ab3 |
4 2
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Step 2 computes the following next_code values:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
N next_code[N]
|
|
kusano |
fc6ab3 |
- ------------
|
|
kusano |
fc6ab3 |
1 0
|
|
kusano |
fc6ab3 |
2 0
|
|
kusano |
fc6ab3 |
3 2
|
|
kusano |
fc6ab3 |
4 14
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Step 3 produces the following code values:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Symbol Length Code
|
|
kusano |
fc6ab3 |
------ ------ ----
|
|
kusano |
fc6ab3 |
A 3 010
|
|
kusano |
fc6ab3 |
B 3 011
|
|
kusano |
fc6ab3 |
C 3 100
|
|
kusano |
fc6ab3 |
D 3 101
|
|
kusano |
fc6ab3 |
E 3 110
|
|
kusano |
fc6ab3 |
F 2 00
|
|
kusano |
fc6ab3 |
G 4 1110
|
|
kusano |
fc6ab3 |
H 4 1111
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.2.3. Details of block format
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Each block of compressed data begins with 3 header bits
|
|
kusano |
fc6ab3 |
containing the following data:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
first bit BFINAL
|
|
kusano |
fc6ab3 |
next 2 bits BTYPE
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Note that the header bits do not necessarily begin on a byte
|
|
kusano |
fc6ab3 |
boundary, since a block does not necessarily occupy an integral
|
|
kusano |
fc6ab3 |
number of bytes.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 9]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
BFINAL is set if and only if this is the last block of the data
|
|
kusano |
fc6ab3 |
set.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
BTYPE specifies how the data are compressed, as follows:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
00 - no compression
|
|
kusano |
fc6ab3 |
01 - compressed with fixed Huffman codes
|
|
kusano |
fc6ab3 |
10 - compressed with dynamic Huffman codes
|
|
kusano |
fc6ab3 |
11 - reserved (error)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The only difference between the two compressed cases is how the
|
|
kusano |
fc6ab3 |
Huffman codes for the literal/length and distance alphabets are
|
|
kusano |
fc6ab3 |
defined.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
In all cases, the decoding algorithm for the actual data is as
|
|
kusano |
fc6ab3 |
follows:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
do
|
|
kusano |
fc6ab3 |
read block header from input stream.
|
|
kusano |
fc6ab3 |
if stored with no compression
|
|
kusano |
fc6ab3 |
skip any remaining bits in current partially
|
|
kusano |
fc6ab3 |
processed byte
|
|
kusano |
fc6ab3 |
read LEN and NLEN (see next section)
|
|
kusano |
fc6ab3 |
copy LEN bytes of data to output
|
|
kusano |
fc6ab3 |
otherwise
|
|
kusano |
fc6ab3 |
if compressed with dynamic Huffman codes
|
|
kusano |
fc6ab3 |
read representation of code trees (see
|
|
kusano |
fc6ab3 |
subsection below)
|
|
kusano |
fc6ab3 |
loop (until end of block code recognized)
|
|
kusano |
fc6ab3 |
decode literal/length value from input stream
|
|
kusano |
fc6ab3 |
if value < 256
|
|
kusano |
fc6ab3 |
copy value (literal byte) to output stream
|
|
kusano |
fc6ab3 |
otherwise
|
|
kusano |
fc6ab3 |
if value = end of block (256)
|
|
kusano |
fc6ab3 |
break from loop
|
|
kusano |
fc6ab3 |
otherwise (value = 257..285)
|
|
kusano |
fc6ab3 |
decode distance from input stream
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
move backwards distance bytes in the output
|
|
kusano |
fc6ab3 |
stream, and copy length bytes from this
|
|
kusano |
fc6ab3 |
position to the output stream.
|
|
kusano |
fc6ab3 |
end loop
|
|
kusano |
fc6ab3 |
while not last block
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Note that a duplicated string reference may refer to a string
|
|
kusano |
fc6ab3 |
in a previous block; i.e., the backward distance may cross one
|
|
kusano |
fc6ab3 |
or more block boundaries. However a distance cannot refer past
|
|
kusano |
fc6ab3 |
the beginning of the output stream. (An application using a
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 10]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
preset dictionary might discard part of the output stream; a
|
|
kusano |
fc6ab3 |
distance can refer to that part of the output stream anyway)
|
|
kusano |
fc6ab3 |
Note also that the referenced string may overlap the current
|
|
kusano |
fc6ab3 |
position; for example, if the last 2 bytes decoded have values
|
|
kusano |
fc6ab3 |
X and Y, a string reference with <length 5,="" =="" distance="2"></length>
|
|
kusano |
fc6ab3 |
adds X,Y,X,Y,X to the output stream.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
We now specify each compression method in turn.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.2.4. Non-compressed blocks (BTYPE=00)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Any bits of input up to the next byte boundary are ignored.
|
|
kusano |
fc6ab3 |
The rest of the block consists of the following information:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
0 1 2 3 4...
|
|
kusano |
fc6ab3 |
+---+---+---+---+================================+
|
|
kusano |
fc6ab3 |
| LEN | NLEN |... LEN bytes of literal data...|
|
|
kusano |
fc6ab3 |
+---+---+---+---+================================+
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
LEN is the number of data bytes in the block. NLEN is the
|
|
kusano |
fc6ab3 |
one's complement of LEN.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.2.5. Compressed blocks (length and distance codes)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
As noted above, encoded data blocks in the "deflate" format
|
|
kusano |
fc6ab3 |
consist of sequences of symbols drawn from three conceptually
|
|
kusano |
fc6ab3 |
distinct alphabets: either literal bytes, from the alphabet of
|
|
kusano |
fc6ab3 |
byte values (0..255), or <length, backward="" distance=""> pairs,</length,>
|
|
kusano |
fc6ab3 |
where the length is drawn from (3..258) and the distance is
|
|
kusano |
fc6ab3 |
drawn from (1..32,768). In fact, the literal and length
|
|
kusano |
fc6ab3 |
alphabets are merged into a single alphabet (0..285), where
|
|
kusano |
fc6ab3 |
values 0..255 represent literal bytes, the value 256 indicates
|
|
kusano |
fc6ab3 |
end-of-block, and values 257..285 represent length codes
|
|
kusano |
fc6ab3 |
(possibly in conjunction with extra bits following the symbol
|
|
kusano |
fc6ab3 |
code) as follows:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 11]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Extra Extra Extra
|
|
kusano |
fc6ab3 |
Code Bits Length(s) Code Bits Lengths Code Bits Length(s)
|
|
kusano |
fc6ab3 |
---- ---- ------ ---- ---- ------- ---- ---- -------
|
|
kusano |
fc6ab3 |
257 0 3 267 1 15,16 277 4 67-82
|
|
kusano |
fc6ab3 |
258 0 4 268 1 17,18 278 4 83-98
|
|
kusano |
fc6ab3 |
259 0 5 269 2 19-22 279 4 99-114
|
|
kusano |
fc6ab3 |
260 0 6 270 2 23-26 280 4 115-130
|
|
kusano |
fc6ab3 |
261 0 7 271 2 27-30 281 5 131-162
|
|
kusano |
fc6ab3 |
262 0 8 272 2 31-34 282 5 163-194
|
|
kusano |
fc6ab3 |
263 0 9 273 3 35-42 283 5 195-226
|
|
kusano |
fc6ab3 |
264 0 10 274 3 43-50 284 5 227-257
|
|
kusano |
fc6ab3 |
265 1 11,12 275 3 51-58 285 0 258
|
|
kusano |
fc6ab3 |
266 1 13,14 276 3 59-66
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The extra bits should be interpreted as a machine integer
|
|
kusano |
fc6ab3 |
stored with the most-significant bit first, e.g., bits 1110
|
|
kusano |
fc6ab3 |
represent the value 14.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Extra Extra Extra
|
|
kusano |
fc6ab3 |
Code Bits Dist Code Bits Dist Code Bits Distance
|
|
kusano |
fc6ab3 |
---- ---- ---- ---- ---- ------ ---- ---- --------
|
|
kusano |
fc6ab3 |
0 0 1 10 4 33-48 20 9 1025-1536
|
|
kusano |
fc6ab3 |
1 0 2 11 4 49-64 21 9 1537-2048
|
|
kusano |
fc6ab3 |
2 0 3 12 5 65-96 22 10 2049-3072
|
|
kusano |
fc6ab3 |
3 0 4 13 5 97-128 23 10 3073-4096
|
|
kusano |
fc6ab3 |
4 1 5,6 14 6 129-192 24 11 4097-6144
|
|
kusano |
fc6ab3 |
5 1 7,8 15 6 193-256 25 11 6145-8192
|
|
kusano |
fc6ab3 |
6 2 9-12 16 7 257-384 26 12 8193-12288
|
|
kusano |
fc6ab3 |
7 2 13-16 17 7 385-512 27 12 12289-16384
|
|
kusano |
fc6ab3 |
8 3 17-24 18 8 513-768 28 13 16385-24576
|
|
kusano |
fc6ab3 |
9 3 25-32 19 8 769-1024 29 13 24577-32768
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.2.6. Compression with fixed Huffman codes (BTYPE=01)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The Huffman codes for the two alphabets are fixed, and are not
|
|
kusano |
fc6ab3 |
represented explicitly in the data. The Huffman code lengths
|
|
kusano |
fc6ab3 |
for the literal/length alphabet are:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Lit Value Bits Codes
|
|
kusano |
fc6ab3 |
--------- ---- -----
|
|
kusano |
fc6ab3 |
0 - 143 8 00110000 through
|
|
kusano |
fc6ab3 |
10111111
|
|
kusano |
fc6ab3 |
144 - 255 9 110010000 through
|
|
kusano |
fc6ab3 |
111111111
|
|
kusano |
fc6ab3 |
256 - 279 7 0000000 through
|
|
kusano |
fc6ab3 |
0010111
|
|
kusano |
fc6ab3 |
280 - 287 8 11000000 through
|
|
kusano |
fc6ab3 |
11000111
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 12]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The code lengths are sufficient to generate the actual codes,
|
|
kusano |
fc6ab3 |
as described above; we show the codes in the table for added
|
|
kusano |
fc6ab3 |
clarity. Literal/length values 286-287 will never actually
|
|
kusano |
fc6ab3 |
occur in the compressed data, but participate in the code
|
|
kusano |
fc6ab3 |
construction.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Distance codes 0-31 are represented by (fixed-length) 5-bit
|
|
kusano |
fc6ab3 |
codes, with possible additional bits as shown in the table
|
|
kusano |
fc6ab3 |
shown in Paragraph 3.2.5, above. Note that distance codes 30-
|
|
kusano |
fc6ab3 |
31 will never actually occur in the compressed data.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.2.7. Compression with dynamic Huffman codes (BTYPE=10)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The Huffman codes for the two alphabets appear in the block
|
|
kusano |
fc6ab3 |
immediately after the header bits and before the actual
|
|
kusano |
fc6ab3 |
compressed data, first the literal/length code and then the
|
|
kusano |
fc6ab3 |
distance code. Each code is defined by a sequence of code
|
|
kusano |
fc6ab3 |
lengths, as discussed in Paragraph 3.2.2, above. For even
|
|
kusano |
fc6ab3 |
greater compactness, the code length sequences themselves are
|
|
kusano |
fc6ab3 |
compressed using a Huffman code. The alphabet for code lengths
|
|
kusano |
fc6ab3 |
is as follows:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
0 - 15: Represent code lengths of 0 - 15
|
|
kusano |
fc6ab3 |
16: Copy the previous code length 3 - 6 times.
|
|
kusano |
fc6ab3 |
The next 2 bits indicate repeat length
|
|
kusano |
fc6ab3 |
(0 = 3, ... , 3 = 6)
|
|
kusano |
fc6ab3 |
Example: Codes 8, 16 (+2 bits 11),
|
|
kusano |
fc6ab3 |
16 (+2 bits 10) will expand to
|
|
kusano |
fc6ab3 |
12 code lengths of 8 (1 + 6 + 5)
|
|
kusano |
fc6ab3 |
17: Repeat a code length of 0 for 3 - 10 times.
|
|
kusano |
fc6ab3 |
(3 bits of length)
|
|
kusano |
fc6ab3 |
18: Repeat a code length of 0 for 11 - 138 times
|
|
kusano |
fc6ab3 |
(7 bits of length)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
A code length of 0 indicates that the corresponding symbol in
|
|
kusano |
fc6ab3 |
the literal/length or distance alphabet will not occur in the
|
|
kusano |
fc6ab3 |
block, and should not participate in the Huffman code
|
|
kusano |
fc6ab3 |
construction algorithm given earlier. If only one distance
|
|
kusano |
fc6ab3 |
code is used, it is encoded using one bit, not zero bits; in
|
|
kusano |
fc6ab3 |
this case there is a single code length of one, with one unused
|
|
kusano |
fc6ab3 |
code. One distance code of zero bits means that there are no
|
|
kusano |
fc6ab3 |
distance codes used at all (the data is all literals).
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
We can now define the format of the block:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286)
|
|
kusano |
fc6ab3 |
5 Bits: HDIST, # of Distance codes - 1 (1 - 32)
|
|
kusano |
fc6ab3 |
4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19)
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 13]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
(HCLEN + 4) x 3 bits: code lengths for the code length
|
|
kusano |
fc6ab3 |
alphabet given just above, in the order: 16, 17, 18,
|
|
kusano |
fc6ab3 |
0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
These code lengths are interpreted as 3-bit integers
|
|
kusano |
fc6ab3 |
(0-7); as above, a code length of 0 means the
|
|
kusano |
fc6ab3 |
corresponding symbol (literal/length or distance code
|
|
kusano |
fc6ab3 |
length) is not used.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
HLIT + 257 code lengths for the literal/length alphabet,
|
|
kusano |
fc6ab3 |
encoded using the code length Huffman code
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
HDIST + 1 code lengths for the distance alphabet,
|
|
kusano |
fc6ab3 |
encoded using the code length Huffman code
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The actual compressed data of the block,
|
|
kusano |
fc6ab3 |
encoded using the literal/length and distance Huffman
|
|
kusano |
fc6ab3 |
codes
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The literal/length symbol 256 (end of data),
|
|
kusano |
fc6ab3 |
encoded using the literal/length Huffman code
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The code length repeat codes can cross from HLIT + 257 to the
|
|
kusano |
fc6ab3 |
HDIST + 1 code lengths. In other words, all code lengths form
|
|
kusano |
fc6ab3 |
a single sequence of HLIT + HDIST + 258 values.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
3.3. Compliance
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
A compressor may limit further the ranges of values specified in
|
|
kusano |
fc6ab3 |
the previous section and still be compliant; for example, it may
|
|
kusano |
fc6ab3 |
limit the range of backward pointers to some value smaller than
|
|
kusano |
fc6ab3 |
32K. Similarly, a compressor may limit the size of blocks so that
|
|
kusano |
fc6ab3 |
a compressible block fits in memory.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
A compliant decompressor must accept the full range of possible
|
|
kusano |
fc6ab3 |
values defined in the previous section, and must accept blocks of
|
|
kusano |
fc6ab3 |
arbitrary size.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
4. Compression algorithm details
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
While it is the intent of this document to define the "deflate"
|
|
kusano |
fc6ab3 |
compressed data format without reference to any particular
|
|
kusano |
fc6ab3 |
compression algorithm, the format is related to the compressed
|
|
kusano |
fc6ab3 |
formats produced by LZ77 (Lempel-Ziv 1977, see reference [2] below);
|
|
kusano |
fc6ab3 |
since many variations of LZ77 are patented, it is strongly
|
|
kusano |
fc6ab3 |
recommended that the implementor of a compressor follow the general
|
|
kusano |
fc6ab3 |
algorithm presented here, which is known not to be patented per se.
|
|
kusano |
fc6ab3 |
The material in this section is not part of the definition of the
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 14]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
specification per se, and a compressor need not follow it in order to
|
|
kusano |
fc6ab3 |
be compliant.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The compressor terminates a block when it determines that starting a
|
|
kusano |
fc6ab3 |
new block with fresh trees would be useful, or when the block size
|
|
kusano |
fc6ab3 |
fills up the compressor's block buffer.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The compressor uses a chained hash table to find duplicated strings,
|
|
kusano |
fc6ab3 |
using a hash function that operates on 3-byte sequences. At any
|
|
kusano |
fc6ab3 |
given point during compression, let XYZ be the next 3 input bytes to
|
|
kusano |
fc6ab3 |
be examined (not necessarily all different, of course). First, the
|
|
kusano |
fc6ab3 |
compressor examines the hash chain for XYZ. If the chain is empty,
|
|
kusano |
fc6ab3 |
the compressor simply writes out X as a literal byte and advances one
|
|
kusano |
fc6ab3 |
byte in the input. If the hash chain is not empty, indicating that
|
|
kusano |
fc6ab3 |
the sequence XYZ (or, if we are unlucky, some other 3 bytes with the
|
|
kusano |
fc6ab3 |
same hash function value) has occurred recently, the compressor
|
|
kusano |
fc6ab3 |
compares all strings on the XYZ hash chain with the actual input data
|
|
kusano |
fc6ab3 |
sequence starting at the current point, and selects the longest
|
|
kusano |
fc6ab3 |
match.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
The compressor searches the hash chains starting with the most recent
|
|
kusano |
fc6ab3 |
strings, to favor small distances and thus take advantage of the
|
|
kusano |
fc6ab3 |
Huffman encoding. The hash chains are singly linked. There are no
|
|
kusano |
fc6ab3 |
deletions from the hash chains; the algorithm simply discards matches
|
|
kusano |
fc6ab3 |
that are too old. To avoid a worst-case situation, very long hash
|
|
kusano |
fc6ab3 |
chains are arbitrarily truncated at a certain length, determined by a
|
|
kusano |
fc6ab3 |
run-time parameter.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
To improve overall compression, the compressor optionally defers the
|
|
kusano |
fc6ab3 |
selection of matches ("lazy matching"): after a match of length N has
|
|
kusano |
fc6ab3 |
been found, the compressor searches for a longer match starting at
|
|
kusano |
fc6ab3 |
the next input byte. If it finds a longer match, it truncates the
|
|
kusano |
fc6ab3 |
previous match to a length of one (thus producing a single literal
|
|
kusano |
fc6ab3 |
byte) and then emits the longer match. Otherwise, it emits the
|
|
kusano |
fc6ab3 |
original match, and, as described above, advances N bytes before
|
|
kusano |
fc6ab3 |
continuing.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Run-time parameters also control this "lazy match" procedure. If
|
|
kusano |
fc6ab3 |
compression ratio is most important, the compressor attempts a
|
|
kusano |
fc6ab3 |
complete second search regardless of the length of the first match.
|
|
kusano |
fc6ab3 |
In the normal case, if the current match is "long enough", the
|
|
kusano |
fc6ab3 |
compressor reduces the search for a longer match, thus speeding up
|
|
kusano |
fc6ab3 |
the process. If speed is most important, the compressor inserts new
|
|
kusano |
fc6ab3 |
strings in the hash table only when no match was found, or when the
|
|
kusano |
fc6ab3 |
match is not "too long". This degrades the compression ratio but
|
|
kusano |
fc6ab3 |
saves time since there are both fewer insertions and fewer searches.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 15]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
5. References
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
[1] Huffman, D. A., "A Method for the Construction of Minimum
|
|
kusano |
fc6ab3 |
Redundancy Codes", Proceedings of the Institute of Radio
|
|
kusano |
fc6ab3 |
Engineers, September 1952, Volume 40, Number 9, pp. 1098-1101.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
[2] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data
|
|
kusano |
fc6ab3 |
Compression", IEEE Transactions on Information Theory, Vol. 23,
|
|
kusano |
fc6ab3 |
No. 3, pp. 337-343.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
[3] Gailly, J.-L., and Adler, M., ZLIB documentation and sources,
|
|
kusano |
fc6ab3 |
available in ftp://ftp.uu.net/pub/archiving/zip/doc/
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
[4] Gailly, J.-L., and Adler, M., GZIP documentation and sources,
|
|
kusano |
fc6ab3 |
available as gzip-*.tar in ftp://prep.ai.mit.edu/pub/gnu/
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
[5] Schwartz, E. S., and Kallick, B. "Generating a canonical prefix
|
|
kusano |
fc6ab3 |
encoding." Comm. ACM, 7,3 (Mar. 1964), pp. 166-169.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
[6] Hirschberg and Lelewer, "Efficient decoding of prefix codes,"
|
|
kusano |
fc6ab3 |
Comm. ACM, 33,4, April 1990, pp. 449-459.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
6. Security Considerations
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Any data compression method involves the reduction of redundancy in
|
|
kusano |
fc6ab3 |
the data. Consequently, any corruption of the data is likely to have
|
|
kusano |
fc6ab3 |
severe effects and be difficult to correct. Uncompressed text, on
|
|
kusano |
fc6ab3 |
the other hand, will probably still be readable despite the presence
|
|
kusano |
fc6ab3 |
of some corrupted bytes.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
It is recommended that systems using this data format provide some
|
|
kusano |
fc6ab3 |
means of validating the integrity of the compressed data. See
|
|
kusano |
fc6ab3 |
reference [3], for example.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
7. Source code
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Source code for a C language implementation of a "deflate" compliant
|
|
kusano |
fc6ab3 |
compressor and decompressor is available within the zlib package at
|
|
kusano |
fc6ab3 |
ftp://ftp.uu.net/pub/archiving/zip/zlib/.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
8. Acknowledgements
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Trademarks cited in this document are the property of their
|
|
kusano |
fc6ab3 |
respective owners.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Phil Katz designed the deflate format. Jean-Loup Gailly and Mark
|
|
kusano |
fc6ab3 |
Adler wrote the related software described in this specification.
|
|
kusano |
fc6ab3 |
Glenn Randers-Pehrson converted this document to RFC and HTML format.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 16]
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
9. Author's Address
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
L. Peter Deutsch
|
|
kusano |
fc6ab3 |
Aladdin Enterprises
|
|
kusano |
fc6ab3 |
203 Santa Margarita Ave.
|
|
kusano |
fc6ab3 |
Menlo Park, CA 94025
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Phone: (415) 322-0103 (AM only)
|
|
kusano |
fc6ab3 |
FAX: (415) 322-1734
|
|
kusano |
fc6ab3 |
EMail: <ghost@aladdin.com></ghost@aladdin.com>
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Questions about the technical content of this specification can be
|
|
kusano |
fc6ab3 |
sent by email to:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Jean-Loup Gailly <gzip@prep.ai.mit.edu> and</gzip@prep.ai.mit.edu>
|
|
kusano |
fc6ab3 |
Mark Adler <madler@alumni.caltech.edu></madler@alumni.caltech.edu>
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Editorial comments on this specification can be sent by email to:
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
L. Peter Deutsch <ghost@aladdin.com> and</ghost@aladdin.com>
|
|
kusano |
fc6ab3 |
Glenn Randers-Pehrson <randeg@alumni.rpi.edu></randeg@alumni.rpi.edu>
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
Deutsch Informational [Page 17]
|
|
kusano |
fc6ab3 |
|