1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncNetwork Working Group P. Deutsch
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRequest for Comments: 1950 Aladdin Enterprises
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncCategory: Informational J-L. Gailly
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync ZLIB Compressed Data Format Specification version 3.3
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncStatus of This Memo
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync This memo provides information for the Internet community. This memo
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync does not specify an Internet standard of any kind. Distribution of
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync this memo is unlimited.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The IESG takes no position on the validity of any Intellectual
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Property Rights statements contained in this document.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Copyright (c) 1996 L. Peter Deutsch and Jean-Loup Gailly
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Permission is granted to copy and distribute this document for any
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync purpose and without charge, including translations into other
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync languages and incorporation into compilations, provided that the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync copyright notice and this notice are preserved, and that any
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync substantive changes or deletions from the original are clearly
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync A pointer to the latest version of this and related documentation in
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync HTML format can be found at the URL
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync This specification defines a lossless compressed data format. The
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync data can be produced or consumed, even for an arbitrarily long
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync sequentially presented input data stream, using only an a priori
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bounded amount of intermediate storage. The format presently uses
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the DEFLATE compression method but can be easily extended to use
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync other compression methods. It can be implemented readily in a manner
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync not covered by patents. This specification also defines the ADLER-32
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync checksum (an extension and improvement of the Fletcher checksum),
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync used for detection of data corruption, and provides an algorithm for
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync computing it.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 1]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncTable of Contents
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1. Introduction ................................................... 2
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.1. Purpose ................................................... 2
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.2. Intended audience ......................................... 3
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.3. Scope ..................................................... 3
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.4. Compliance ................................................ 3
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.5. Definitions of terms and conventions used ................ 3
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.6. Changes from previous versions ............................ 3
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 2. Detailed specification ......................................... 3
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 2.1. Overall conventions ....................................... 3
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 2.2. Data format ............................................... 4
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 2.3. Compliance ................................................ 7
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 3. References ..................................................... 7
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 4. Source code .................................................... 8
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 5. Security Considerations ........................................ 8
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 6. Acknowledgements ............................................... 8
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 7. Authors' Addresses ............................................. 8
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 8. Appendix: Rationale ............................................ 9
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 9. Appendix: Sample code ..........................................10
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync1. Introduction
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.1. Purpose
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The purpose of this specification is to define a lossless
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync compressed data format that:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * Is independent of CPU type, operating system, file system,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync and character set, and hence can be used for interchange;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * Can be produced or consumed, even for an arbitrarily long
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync sequentially presented input data stream, using only an a
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync priori bounded amount of intermediate storage, and hence can
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync be used in data communications or similar structures such as
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Unix filters;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * Can use a number of different compression methods;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * Can be implemented readily in a manner not covered by
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync patents, and hence can be practiced freely.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The data format defined by this specification does not attempt to
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync allow random access to compressed data.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 2]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.2. Intended audience
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync This specification is intended for use by implementors of software
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync to compress data into zlib format and/or decompress data from zlib
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The text of the specification assumes a basic background in
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync programming at the level of bits and other primitive data
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync representations.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The specification specifies a compressed data format that can be
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync used for in-memory compression of a sequence of arbitrary bytes.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.4. Compliance
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Unless otherwise indicated below, a compliant decompressor must be
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync able to accept and decompress any data set that conforms to all
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the specifications presented here; a compliant compressor must
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync produce data sets that conform to all the specifications presented
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.5. Definitions of terms and conventions used
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync byte: 8 bits stored or transmitted as a unit (same as an octet).
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync (For this specification, a byte is exactly 8 bits, even on
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync machines which store a character on a number of bits different
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync from 8.) See below, for the numbering of bits within a byte.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1.6. Changes from previous versions
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Version 3.1 was the first public release of this specification.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync In version 3.2, some terminology was changed and the Adler-32
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync sample code was rewritten for clarity. In version 3.3, the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync support for a preset dictionary was introduced, and the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync specification was converted to RFC style.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync2. Detailed specification
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 2.1. Overall conventions
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync In the diagrams below, a box like this:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync | | <-- the vertical bars might be missing
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 3]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync represents one byte; a box like this:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync +==============+
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync +==============+
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync represents a variable number of bytes.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Bytes stored within a computer do not have a "bit order", since
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync they are always treated as a unit. However, a byte considered as
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync an integer between 0 and 255 does have a most- and least-
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync significant bit, and since we write numbers with the most-
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync significant digit on the left, we also write bytes with the most-
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync significant bit on the left. In the diagrams below, we number the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bits of a byte so that bit 0 is the least-significant bit, i.e.,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the bits are numbered:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Within a computer, a number may occupy multiple bytes. All
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync multi-byte numbers in the format described here are stored with
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the MOST-significant byte first (at the lower memory address).
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync For example, the decimal number 520 is stored as:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync +--------+--------+
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync |00000010|00001000|
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync +--------+--------+
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync | + less significant byte = 8
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync + more significant byte = 2 x 256
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 2.2. Data format
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync A zlib stream has the following structure:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync |CMF|FLG| (more-->)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 4]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync +---+---+---+---+
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync | DICTID | (more-->)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync +---+---+---+---+
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync +=====================+---+---+---+---+
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync |...compressed data...| ADLER32 |
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync +=====================+---+---+---+---+
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Any data which may appear after ADLER32 are not part of the zlib
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync CMF (Compression Method and flags)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync This byte is divided into a 4-bit compression method and a 4-
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bit information field depending on the compression method.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bits 0 to 3 CM Compression method
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bits 4 to 7 CINFO Compression info
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync CM (Compression method)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync This identifies the compression method used in the file. CM = 8
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync denotes the "deflate" compression method with a window size up
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync to 32K. This is the method used by gzip and PNG (see
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync references [1] and [2] in Chapter 3, below, for the reference
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync documents). CM = 15 is reserved. It might be used in a future
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync version of this specification to indicate the presence of an
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync extra field before the compressed data.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync CINFO (Compression info)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync For CM = 8, CINFO is the base-2 logarithm of the LZ77 window
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync size, minus eight (CINFO=7 indicates a 32K window size). Values
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync of CINFO above 7 are not allowed in this version of the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync specification. CINFO is not defined in this specification for
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync CM not equal to 8.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync FLG (FLaGs)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync This flag byte is divided as follows:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bits 0 to 4 FCHECK (check bits for CMF and FLG)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bit 5 FDICT (preset dictionary)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bits 6 to 7 FLEVEL (compression level)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The FCHECK value must be such that CMF and FLG, when viewed as
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG),
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync is a multiple of 31.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 5]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync FDICT (Preset dictionary)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync If FDICT is set, a DICT dictionary identifier is present
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync immediately after the FLG byte. The dictionary is a sequence of
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bytes which are initially fed to the compressor without
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync producing any compressed output. DICT is the Adler-32 checksum
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync of this sequence of bytes (see the definition of ADLER32
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync below). The decompressor can use this identifier to determine
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync which dictionary has been used by the compressor.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync FLEVEL (Compression level)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync These flags are available for use by specific compression
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync methods. The "deflate" method (CM = 8) sets these flags as
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 0 - compressor used fastest algorithm
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 1 - compressor used fast algorithm
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 2 - compressor used default algorithm
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 3 - compressor used maximum compression, slowest algorithm
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The information in FLEVEL is not needed for decompression; it
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync is there to indicate if recompression might be worthwhile.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync compressed data
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync For compression method 8, the compressed data is stored in the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync deflate compressed data format as described in the document
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync "DEFLATE Compressed Data Format Specification" by L. Peter
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Deutsch. (See reference [3] in Chapter 3, below)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Other compressed data formats are not specified in this version
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync of the zlib specification.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync ADLER32 (Adler-32 checksum)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync This contains a checksum value of the uncompressed data
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync (excluding any dictionary data) computed according to Adler-32
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync algorithm. This algorithm is a 32-bit extension and improvement
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync standard. See references [4] and [5] in Chapter 3, below)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Adler-32 is composed of two sums accumulated per byte: s1 is
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the sum of all bytes, s2 is the sum of all s1 values. Both sums
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync are done modulo 65521. s1 is initialized to 1, s2 to zero. The
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Adler-32 checksum is stored as s2*65536 + s1 in most-
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync significant-byte first (network) order.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 6]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 2.3. Compliance
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync A compliant compressor must produce streams with correct CMF, FLG
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync and ADLER32, but need not support preset dictionaries. When the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync zlib data format is used as part of another standard data format,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the compressor may use only preset dictionaries that are specified
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync by this other data format. If this other format does not use the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync preset dictionary feature, the compressor must not set the FDICT
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync A compliant decompressor must check CMF, FLG, and ADLER32, and
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync provide an error indication if any of these have incorrect values.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync A compliant decompressor must give an error indication if CM is
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync not one of the values defined in this specification (only the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync value 8 is permitted in this version), since another value could
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync indicate the presence of new features that would cause subsequent
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync data to be interpreted incorrectly. A compliant decompressor must
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync give an error indication if FDICT is set and DICTID is not the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync identifier of a known preset dictionary. A decompressor may
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync ignore FLEVEL and still be compliant. When the zlib data format
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync is being used as a part of another standard format, a compliant
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync decompressor must support all the preset dictionaries specified by
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the other format. When the other format does not use the preset
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync dictionary feature, a compliant decompressor must reject any
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync stream in which the FDICT flag is set.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync3. References
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync [1] Deutsch, L.P.,"GZIP Compressed Data Format Specification",
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync available in ftp://ftp.uu.net/pub/archiving/zip/doc/
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync [2] Thomas Boutell, "PNG (Portable Network Graphics) specification",
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync available in ftp://ftp.uu.net/graphics/png/documents/
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync [3] Deutsch, L.P.,"DEFLATE Compressed Data Format Specification",
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync available in ftp://ftp.uu.net/pub/archiving/zip/doc/
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync [4] Fletcher, J. G., "An Arithmetic Checksum for Serial
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Transmissions," IEEE Transactions on Communications, Vol. COM-30,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync No. 1, January 1982, pp. 247-252.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync [5] ITU-T Recommendation X.224, Annex D, "Checksum Algorithms,"
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync November, 1993, pp. 144, 145. (Available from
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync gopher://info.itu.ch). ITU-T X.244 is also the same as ISO 8073.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 7]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync4. Source code
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Source code for a C language implementation of a "zlib" compliant
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync library is available at ftp://ftp.uu.net/pub/archiving/zip/zlib/.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync5. Security Considerations
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync A decoder that fails to check the ADLER32 checksum value may be
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync subject to undetected data corruption.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync6. Acknowledgements
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Trademarks cited in this document are the property of their
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync respective owners.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Jean-Loup Gailly and Mark Adler designed the zlib format and wrote
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the related software described in this specification. Glenn
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Randers-Pehrson converted this document to RFC and HTML format.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync7. Authors' Addresses
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync L. Peter Deutsch
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Aladdin Enterprises
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 203 Santa Margarita Ave.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Menlo Park, CA 94025
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Phone: (415) 322-0103 (AM only)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync FAX: (415) 322-1734
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync EMail: <ghost@aladdin.com>
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Jean-Loup Gailly
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync EMail: <gzip@prep.ai.mit.edu>
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Questions about the technical content of this specification can be
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync sent by email to
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Jean-Loup Gailly <gzip@prep.ai.mit.edu> and
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Mark Adler <madler@alumni.caltech.edu>
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Editorial comments on this specification can be sent by email to
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync L. Peter Deutsch <ghost@aladdin.com> and
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Glenn Randers-Pehrson <randeg@alumni.rpi.edu>
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 8]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync8. Appendix: Rationale
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 8.1. Preset dictionaries
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync A preset dictionary is specially useful to compress short input
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync sequences. The compressor can take advantage of the dictionary
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync context to encode the input in a more compact manner. The
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync decompressor can be initialized with the appropriate context by
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync virtually decompressing a compressed version of the dictionary
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync without producing any output. However for certain compression
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync algorithms such as the deflate algorithm this operation can be
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync achieved without actually performing any decompression.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The compressor and the decompressor must use exactly the same
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync dictionary. The dictionary may be fixed or may be chosen among a
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync certain number of predefined dictionaries, according to the kind
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync of input data. The decompressor can determine which dictionary has
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync been chosen by the compressor by checking the dictionary
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync identifier. This document does not specify the contents of
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync predefined dictionaries, since the optimal dictionaries are
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync application specific. Standard data formats using this feature of
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync the zlib specification must precisely define the allowed
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync dictionaries.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 8.2. The Adler-32 algorithm
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The Adler-32 algorithm is much faster than the CRC32 algorithm yet
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync still provides an extremely low probability of undetected errors.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The modulo on unsigned long accumulators can be delayed for 5552
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bytes, so the modulo operation time is negligible. If the bytes
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync and order sensitive, unlike the first sum, which is just a
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync checksum. That 65521 is prime is important to avoid a possible
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync large class of two-byte errors that leave the check unchanged.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync (The Fletcher checksum uses 255, which is not prime and which also
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync makes the Fletcher check insensitive to single byte changes 0 <->
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The sum s1 is initialized to 1 instead of zero to make the length
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync of the sequence part of s2, so that the length does not have to be
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync checked separately. (Any sequence of zeroes has a Fletcher
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync checksum of zero.)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 9]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync9. Appendix: Sample code
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync The following C code computes the Adler-32 checksum of a data buffer.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync It is written for clarity, not for speed. The sample code is in the
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync ANSI C programming language. Non C users may find it easier to read
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync with these hints:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync & Bitwise AND operator.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync >> Bitwise right shift operator. When applied to an
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync unsigned quantity, as here, right shift inserts zero bit(s)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync at the left.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync << Bitwise left shift operator. Left shift inserts zero
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync bit(s) at the right.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync ++ "n++" increments the variable n.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync % modulo operator: a % b is the remainder of a divided by b.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync #define BASE 65521 /* largest prime smaller than 65536 */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Update a running Adler-32 checksum with the bytes buf[0..len-1]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync and return the updated checksum. The Adler-32 checksum should be
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync initialized to 1.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Usage example:
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync unsigned long adler = 1L;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync while (read_buffer(buffer, length) != EOF) {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync adler = update_adler32(adler, buffer, length);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync if (adler != original_adler) error();
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync unsigned long update_adler32(unsigned long adler,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync unsigned char *buf, int len)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync unsigned long s1 = adler & 0xffff;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync unsigned long s2 = (adler >> 16) & 0xffff;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync for (n = 0; n < len; n++) {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync s1 = (s1 + buf[n]) % BASE;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync s2 = (s2 + s1) % BASE;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync return (s2 << 16) + s1;
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* Return the adler32 of the bytes buf[0..len-1] */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 10]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncRFC 1950 ZLIB Compressed Data Format Specification May 1996
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync unsigned long adler32(unsigned char *buf, int len)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync return update_adler32(1L, buf, len);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncDeutsch & Gailly Informational [Page 11]