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