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