kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Network Working Group                                         P. Deutsch
kusano fc6ab3
Request for Comments: 1950                           Aladdin Enterprises
kusano fc6ab3
Category: Informational                                      J-L. Gailly
kusano fc6ab3
                                                                Info-ZIP
kusano fc6ab3
                                                                May 1996
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
         ZLIB Compressed Data Format Specification version 3.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 and Jean-Loup Gailly
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.  The
kusano fc6ab3
   data can be produced or consumed, even for an arbitrarily long
kusano fc6ab3
   sequentially presented input data stream, using only an a priori
kusano fc6ab3
   bounded amount of intermediate storage.  The format presently uses
kusano fc6ab3
   the DEFLATE compression method but can be easily extended to use
kusano fc6ab3
   other compression methods.  It can be implemented readily in a manner
kusano fc6ab3
   not covered by patents.  This specification also defines the ADLER-32
kusano fc6ab3
   checksum (an extension and improvement of the Fletcher checksum),
kusano fc6ab3
   used for detection of data corruption, and provides an algorithm for
kusano fc6ab3
   computing it.
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Deutsch & Gailly             Informational                      [Page 1]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB 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 ............................ 3
kusano fc6ab3
   2. Detailed specification ......................................... 3
kusano fc6ab3
      2.1. Overall conventions ....................................... 3
kusano fc6ab3
      2.2. Data format ............................................... 4
kusano fc6ab3
      2.3. Compliance ................................................ 7
kusano fc6ab3
   3. References ..................................................... 7
kusano fc6ab3
   4. Source code .................................................... 8
kusano fc6ab3
   5. Security Considerations ........................................ 8
kusano fc6ab3
   6. Acknowledgements ............................................... 8
kusano fc6ab3
   7. Authors' Addresses ............................................. 8
kusano fc6ab3
   8. Appendix: Rationale ............................................ 9
kusano fc6ab3
   9. Appendix: Sample code ..........................................10
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
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
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 can
kusano fc6ab3
            be used in data communications or similar structures such as
kusano fc6ab3
            Unix filters;
kusano fc6ab3
kusano fc6ab3
          * Can use a number of different compression methods;
kusano fc6ab3
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
      The data format defined by this specification does not attempt to
kusano fc6ab3
      allow random access to compressed data.
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Deutsch & Gailly             Informational                      [Page 2]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB Compressed Data Format Specification        May 1996
kusano fc6ab3
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 zlib format and/or decompress data from zlib
kusano fc6ab3
      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.
kusano fc6ab3
kusano fc6ab3
   1.3. Scope
kusano fc6ab3
kusano fc6ab3
      The specification specifies a compressed data format that can be
kusano fc6ab3
      used for in-memory compression of a sequence of arbitrary 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
kusano fc6ab3
      machines which store a character on a number of bits different
kusano fc6ab3
      from 8.) See below, for the numbering of bits within a byte.
kusano fc6ab3
kusano fc6ab3
   1.6. Changes from previous versions
kusano fc6ab3
kusano fc6ab3
      Version 3.1 was the first public release of this specification.
kusano fc6ab3
      In version 3.2, some terminology was changed and the Adler-32
kusano fc6ab3
      sample code was rewritten for clarity.  In version 3.3, the
kusano fc6ab3
      support for a preset dictionary was introduced, and the
kusano fc6ab3
      specification was converted to RFC style.
kusano fc6ab3
kusano fc6ab3
2. Detailed specification
kusano fc6ab3
kusano fc6ab3
   2.1. Overall conventions
kusano fc6ab3
kusano fc6ab3
      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
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Deutsch & Gailly             Informational                      [Page 3]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB Compressed Data Format Specification        May 1996
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 MOST-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
         |00000010|00001000|
kusano fc6ab3
         +--------+--------+
kusano fc6ab3
          ^        ^
kusano fc6ab3
          |        |
kusano fc6ab3
          |        + less significant byte = 8
kusano fc6ab3
          + more significant byte = 2 x 256
kusano fc6ab3
kusano fc6ab3
   2.2. Data format
kusano fc6ab3
kusano fc6ab3
      A zlib stream has the following structure:
kusano fc6ab3
kusano fc6ab3
           0   1
kusano fc6ab3
         +---+---+
kusano fc6ab3
         |CMF|FLG|   (more-->)
kusano fc6ab3
         +---+---+
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Deutsch & Gailly             Informational                      [Page 4]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB Compressed Data Format Specification        May 1996
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
      (if FLG.FDICT set)
kusano fc6ab3
kusano fc6ab3
           0   1   2   3
kusano fc6ab3
         +---+---+---+---+
kusano fc6ab3
         |     DICTID    |   (more-->)
kusano fc6ab3
         +---+---+---+---+
kusano fc6ab3
kusano fc6ab3
         +=====================+---+---+---+---+
kusano fc6ab3
         |...compressed data...|    ADLER32    |
kusano fc6ab3
         +=====================+---+---+---+---+
kusano fc6ab3
kusano fc6ab3
      Any data which may appear after ADLER32 are not part of the zlib
kusano fc6ab3
      stream.
kusano fc6ab3
kusano fc6ab3
      CMF (Compression Method and flags)
kusano fc6ab3
         This byte is divided into a 4-bit compression method and a 4-
kusano fc6ab3
         bit information field depending on the compression method.
kusano fc6ab3
kusano fc6ab3
            bits 0 to 3  CM     Compression method
kusano fc6ab3
            bits 4 to 7  CINFO  Compression info
kusano fc6ab3
kusano fc6ab3
      CM (Compression method)
kusano fc6ab3
         This identifies the compression method used in the file. CM = 8
kusano fc6ab3
         denotes the "deflate" compression method with a window size up
kusano fc6ab3
         to 32K.  This is the method used by gzip and PNG (see
kusano fc6ab3
         references [1] and [2] in Chapter 3, below, for the reference
kusano fc6ab3
         documents).  CM = 15 is reserved.  It might be used in a future
kusano fc6ab3
         version of this specification to indicate the presence of an
kusano fc6ab3
         extra field before the compressed data.
kusano fc6ab3
kusano fc6ab3
      CINFO (Compression info)
kusano fc6ab3
         For CM = 8, CINFO is the base-2 logarithm of the LZ77 window
kusano fc6ab3
         size, minus eight (CINFO=7 indicates a 32K window size). Values
kusano fc6ab3
         of CINFO above 7 are not allowed in this version of the
kusano fc6ab3
         specification.  CINFO is not defined in this specification for
kusano fc6ab3
         CM not equal to 8.
kusano fc6ab3
kusano fc6ab3
      FLG (FLaGs)
kusano fc6ab3
         This flag byte is divided as follows:
kusano fc6ab3
kusano fc6ab3
            bits 0 to 4  FCHECK  (check bits for CMF and FLG)
kusano fc6ab3
            bit  5       FDICT   (preset dictionary)
kusano fc6ab3
            bits 6 to 7  FLEVEL  (compression level)
kusano fc6ab3
kusano fc6ab3
         The FCHECK value must be such that CMF and FLG, when viewed as
kusano fc6ab3
         a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG),
kusano fc6ab3
         is a multiple of 31.
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Deutsch & Gailly             Informational                      [Page 5]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB Compressed Data Format Specification        May 1996
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
      FDICT (Preset dictionary)
kusano fc6ab3
         If FDICT is set, a DICT dictionary identifier is present
kusano fc6ab3
         immediately after the FLG byte. The dictionary is a sequence of
kusano fc6ab3
         bytes which are initially fed to the compressor without
kusano fc6ab3
         producing any compressed output. DICT is the Adler-32 checksum
kusano fc6ab3
         of this sequence of bytes (see the definition of ADLER32
kusano fc6ab3
         below).  The decompressor can use this identifier to determine
kusano fc6ab3
         which dictionary has been used by the compressor.
kusano fc6ab3
kusano fc6ab3
      FLEVEL (Compression level)
kusano fc6ab3
         These flags are available for use by specific compression
kusano fc6ab3
         methods.  The "deflate" method (CM = 8) sets these flags as
kusano fc6ab3
         follows:
kusano fc6ab3
kusano fc6ab3
            0 - compressor used fastest algorithm
kusano fc6ab3
            1 - compressor used fast algorithm
kusano fc6ab3
            2 - compressor used default algorithm
kusano fc6ab3
            3 - compressor used maximum compression, slowest algorithm
kusano fc6ab3
kusano fc6ab3
         The information in FLEVEL is not needed for decompression; it
kusano fc6ab3
         is there to indicate if recompression might be worthwhile.
kusano fc6ab3
kusano fc6ab3
      compressed data
kusano fc6ab3
         For compression method 8, the compressed data is stored in the
kusano fc6ab3
         deflate compressed data format as described in the document
kusano fc6ab3
         "DEFLATE Compressed Data Format Specification" by L. Peter
kusano fc6ab3
         Deutsch. (See reference [3] in Chapter 3, below)
kusano fc6ab3
kusano fc6ab3
         Other compressed data formats are not specified in this version
kusano fc6ab3
         of the zlib specification.
kusano fc6ab3
kusano fc6ab3
      ADLER32 (Adler-32 checksum)
kusano fc6ab3
         This contains a checksum value of the uncompressed data
kusano fc6ab3
         (excluding any dictionary data) computed according to Adler-32
kusano fc6ab3
         algorithm. This algorithm is a 32-bit extension and improvement
kusano fc6ab3
         of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
kusano fc6ab3
         standard. See references [4] and [5] in Chapter 3, below)
kusano fc6ab3
kusano fc6ab3
         Adler-32 is composed of two sums accumulated per byte: s1 is
kusano fc6ab3
         the sum of all bytes, s2 is the sum of all s1 values. Both sums
kusano fc6ab3
         are done modulo 65521. s1 is initialized to 1, s2 to zero.  The
kusano fc6ab3
         Adler-32 checksum is stored as s2*65536 + s1 in most-
kusano fc6ab3
         significant-byte first (network) order.
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Deutsch & Gailly             Informational                      [Page 6]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB Compressed Data Format Specification        May 1996
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
   2.3. Compliance
kusano fc6ab3
kusano fc6ab3
      A compliant compressor must produce streams with correct CMF, FLG
kusano fc6ab3
      and ADLER32, but need not support preset dictionaries.  When the
kusano fc6ab3
      zlib data format is used as part of another standard data format,
kusano fc6ab3
      the compressor may use only preset dictionaries that are specified
kusano fc6ab3
      by this other data format.  If this other format does not use the
kusano fc6ab3
      preset dictionary feature, the compressor must not set the FDICT
kusano fc6ab3
      flag.
kusano fc6ab3
kusano fc6ab3
      A compliant decompressor must check CMF, FLG, and ADLER32, and
kusano fc6ab3
      provide an error indication if any of these have incorrect values.
kusano fc6ab3
      A compliant decompressor must give an error indication if CM is
kusano fc6ab3
      not one of the values defined in this specification (only the
kusano fc6ab3
      value 8 is permitted in this version), since another value could
kusano fc6ab3
      indicate the presence of new features that would cause subsequent
kusano fc6ab3
      data to be interpreted incorrectly.  A compliant decompressor must
kusano fc6ab3
      give an error indication if FDICT is set and DICTID is not the
kusano fc6ab3
      identifier of a known preset dictionary.  A decompressor may
kusano fc6ab3
      ignore FLEVEL and still be compliant.  When the zlib data format
kusano fc6ab3
      is being used as a part of another standard format, a compliant
kusano fc6ab3
      decompressor must support all the preset dictionaries specified by
kusano fc6ab3
      the other format. When the other format does not use the preset
kusano fc6ab3
      dictionary feature, a compliant decompressor must reject any
kusano fc6ab3
      stream in which the FDICT flag is set.
kusano fc6ab3
kusano fc6ab3
3. References
kusano fc6ab3
kusano fc6ab3
   [1] Deutsch, L.P.,"GZIP Compressed Data Format Specification",
kusano fc6ab3
       available in ftp://ftp.uu.net/pub/archiving/zip/doc/
kusano fc6ab3
kusano fc6ab3
   [2] Thomas Boutell, "PNG (Portable Network Graphics) specification",
kusano fc6ab3
       available in ftp://ftp.uu.net/graphics/png/documents/
kusano fc6ab3
kusano fc6ab3
   [3] Deutsch, L.P.,"DEFLATE Compressed Data Format Specification",
kusano fc6ab3
       available in ftp://ftp.uu.net/pub/archiving/zip/doc/
kusano fc6ab3
kusano fc6ab3
   [4] Fletcher, J. G., "An Arithmetic Checksum for Serial
kusano fc6ab3
       Transmissions," IEEE Transactions on Communications, Vol. COM-30,
kusano fc6ab3
       No. 1, January 1982, pp. 247-252.
kusano fc6ab3
kusano fc6ab3
   [5] ITU-T Recommendation X.224, Annex D, "Checksum Algorithms,"
kusano fc6ab3
       November, 1993, pp. 144, 145. (Available from
kusano fc6ab3
       gopher://info.itu.ch). ITU-T X.244 is also the same as ISO 8073.
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Deutsch & Gailly             Informational                      [Page 7]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB Compressed Data Format Specification        May 1996
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
4. Source code
kusano fc6ab3
kusano fc6ab3
   Source code for a C language implementation of a "zlib" compliant
kusano fc6ab3
   library is available at ftp://ftp.uu.net/pub/archiving/zip/zlib/.
kusano fc6ab3
kusano fc6ab3
5. Security Considerations
kusano fc6ab3
kusano fc6ab3
   A decoder that fails to check the ADLER32 checksum value may be
kusano fc6ab3
   subject to undetected data corruption.
kusano fc6ab3
kusano fc6ab3
6. Acknowledgements
kusano fc6ab3
kusano fc6ab3
   Trademarks cited in this document are the property of their
kusano fc6ab3
   respective owners.
kusano fc6ab3
kusano fc6ab3
   Jean-Loup Gailly and Mark Adler designed the zlib format and wrote
kusano fc6ab3
   the related software described in this specification.  Glenn
kusano fc6ab3
   Randers-Pehrson converted this document to RFC and HTML format.
kusano fc6ab3
kusano fc6ab3
7. Authors' Addresses
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
kusano fc6ab3
   Jean-Loup Gailly
kusano fc6ab3
kusano fc6ab3
   EMail: <gzip@prep.ai.mit.edu></gzip@prep.ai.mit.edu>
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
Deutsch & Gailly             Informational                      [Page 8]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB Compressed Data Format Specification        May 1996
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
8. Appendix: Rationale
kusano fc6ab3
kusano fc6ab3
   8.1. Preset dictionaries
kusano fc6ab3
kusano fc6ab3
      A preset dictionary is specially useful to compress short input
kusano fc6ab3
      sequences. The compressor can take advantage of the dictionary
kusano fc6ab3
      context to encode the input in a more compact manner. The
kusano fc6ab3
      decompressor can be initialized with the appropriate context by
kusano fc6ab3
      virtually decompressing a compressed version of the dictionary
kusano fc6ab3
      without producing any output. However for certain compression
kusano fc6ab3
      algorithms such as the deflate algorithm this operation can be
kusano fc6ab3
      achieved without actually performing any decompression.
kusano fc6ab3
kusano fc6ab3
      The compressor and the decompressor must use exactly the same
kusano fc6ab3
      dictionary. The dictionary may be fixed or may be chosen among a
kusano fc6ab3
      certain number of predefined dictionaries, according to the kind
kusano fc6ab3
      of input data. The decompressor can determine which dictionary has
kusano fc6ab3
      been chosen by the compressor by checking the dictionary
kusano fc6ab3
      identifier. This document does not specify the contents of
kusano fc6ab3
      predefined dictionaries, since the optimal dictionaries are
kusano fc6ab3
      application specific. Standard data formats using this feature of
kusano fc6ab3
      the zlib specification must precisely define the allowed
kusano fc6ab3
      dictionaries.
kusano fc6ab3
kusano fc6ab3
   8.2. The Adler-32 algorithm
kusano fc6ab3
kusano fc6ab3
      The Adler-32 algorithm is much faster than the CRC32 algorithm yet
kusano fc6ab3
      still provides an extremely low probability of undetected errors.
kusano fc6ab3
kusano fc6ab3
      The modulo on unsigned long accumulators can be delayed for 5552
kusano fc6ab3
      bytes, so the modulo operation time is negligible.  If the bytes
kusano fc6ab3
      are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
kusano fc6ab3
      and order sensitive, unlike the first sum, which is just a
kusano fc6ab3
      checksum.  That 65521 is prime is important to avoid a possible
kusano fc6ab3
      large class of two-byte errors that leave the check unchanged.
kusano fc6ab3
      (The Fletcher checksum uses 255, which is not prime and which also
kusano fc6ab3
      makes the Fletcher check insensitive to single byte changes 0 <->
kusano fc6ab3
      255.)
kusano fc6ab3
kusano fc6ab3
      The sum s1 is initialized to 1 instead of zero to make the length
kusano fc6ab3
      of the sequence part of s2, so that the length does not have to be
kusano fc6ab3
      checked separately. (Any sequence of zeroes has a Fletcher
kusano fc6ab3
      checksum of zero.)
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Deutsch & Gailly             Informational                      [Page 9]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB Compressed Data Format Specification        May 1996
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
9. Appendix: Sample code
kusano fc6ab3
kusano fc6ab3
   The following C code computes the Adler-32 checksum of a data buffer.
kusano fc6ab3
   It is written for clarity, not for speed.  The sample code is in the
kusano fc6ab3
   ANSI C programming language. Non C users may find it easier to read
kusano fc6ab3
   with these hints:
kusano fc6ab3
kusano fc6ab3
      &      Bitwise AND operator.
kusano fc6ab3
      >>     Bitwise right shift operator. When applied to an
kusano fc6ab3
             unsigned quantity, as here, right shift inserts zero bit(s)
kusano fc6ab3
             at the left.
kusano fc6ab3
      <<     Bitwise left shift operator. Left shift inserts zero
kusano fc6ab3
             bit(s) at the right.
kusano fc6ab3
      ++     "n++" increments the variable n.
kusano fc6ab3
      %      modulo operator: a % b is the remainder of a divided by b.
kusano fc6ab3
kusano fc6ab3
      #define BASE 65521 /* largest prime smaller than 65536 */
kusano fc6ab3
kusano fc6ab3
      /*
kusano fc6ab3
         Update a running Adler-32 checksum with the bytes buf[0..len-1]
kusano fc6ab3
       and return the updated checksum. The Adler-32 checksum should be
kusano fc6ab3
       initialized to 1.
kusano fc6ab3
kusano fc6ab3
       Usage example:
kusano fc6ab3
kusano fc6ab3
         unsigned long adler = 1L;
kusano fc6ab3
kusano fc6ab3
         while (read_buffer(buffer, length) != EOF) {
kusano fc6ab3
           adler = update_adler32(adler, buffer, length);
kusano fc6ab3
         }
kusano fc6ab3
         if (adler != original_adler) error();
kusano fc6ab3
      */
kusano fc6ab3
      unsigned long update_adler32(unsigned long adler,
kusano fc6ab3
         unsigned char *buf, int len)
kusano fc6ab3
      {
kusano fc6ab3
        unsigned long s1 = adler & 0xffff;
kusano fc6ab3
        unsigned long s2 = (adler >> 16) & 0xffff;
kusano fc6ab3
        int n;
kusano fc6ab3
kusano fc6ab3
        for (n = 0; n < len; n++) {
kusano fc6ab3
          s1 = (s1 + buf[n]) % BASE;
kusano fc6ab3
          s2 = (s2 + s1)     % BASE;
kusano fc6ab3
        }
kusano fc6ab3
        return (s2 << 16) + s1;
kusano fc6ab3
      }
kusano fc6ab3
kusano fc6ab3
      /* Return the adler32 of the bytes buf[0..len-1] */
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Deutsch & Gailly             Informational                     [Page 10]
kusano fc6ab3

kusano fc6ab3
RFC 1950       ZLIB Compressed Data Format Specification        May 1996
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
      unsigned long adler32(unsigned char *buf, int len)
kusano fc6ab3
      {
kusano fc6ab3
        return update_adler32(1L, buf, len);
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
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 & Gailly             Informational                     [Page 11]
kusano fc6ab3