1205194Sdelphij
2205194Sdelphij
3205194Sdelphij
4205194Sdelphij
5205194Sdelphij
6205194Sdelphij
7205194SdelphijNetwork Working Group                                         P. Deutsch
8205194SdelphijRequest for Comments: 1950                           Aladdin Enterprises
9205194SdelphijCategory: Informational                                      J-L. Gailly
10205194Sdelphij                                                                Info-ZIP
11205194Sdelphij                                                                May 1996
12205194Sdelphij
13205194Sdelphij
14205194Sdelphij         ZLIB Compressed Data Format Specification version 3.3
15205194Sdelphij
16205194SdelphijStatus of This Memo
17205194Sdelphij
18205194Sdelphij   This memo provides information for the Internet community.  This memo
19205194Sdelphij   does not specify an Internet standard of any kind.  Distribution of
20205194Sdelphij   this memo is unlimited.
21205194Sdelphij
22205194SdelphijIESG Note:
23205194Sdelphij
24205194Sdelphij   The IESG takes no position on the validity of any Intellectual
25205194Sdelphij   Property Rights statements contained in this document.
26205194Sdelphij
27205194SdelphijNotices
28205194Sdelphij
29205194Sdelphij   Copyright (c) 1996 L. Peter Deutsch and Jean-Loup Gailly
30205194Sdelphij
31205194Sdelphij   Permission is granted to copy and distribute this document for any
32205194Sdelphij   purpose and without charge, including translations into other
33205194Sdelphij   languages and incorporation into compilations, provided that the
34205194Sdelphij   copyright notice and this notice are preserved, and that any
35205194Sdelphij   substantive changes or deletions from the original are clearly
36205194Sdelphij   marked.
37205194Sdelphij
38205194Sdelphij   A pointer to the latest version of this and related documentation in
39205194Sdelphij   HTML format can be found at the URL
40205194Sdelphij   <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>.
41205194Sdelphij
42205194SdelphijAbstract
43205194Sdelphij
44205194Sdelphij   This specification defines a lossless compressed data format.  The
45205194Sdelphij   data can be produced or consumed, even for an arbitrarily long
46205194Sdelphij   sequentially presented input data stream, using only an a priori
47205194Sdelphij   bounded amount of intermediate storage.  The format presently uses
48205194Sdelphij   the DEFLATE compression method but can be easily extended to use
49205194Sdelphij   other compression methods.  It can be implemented readily in a manner
50205194Sdelphij   not covered by patents.  This specification also defines the ADLER-32
51205194Sdelphij   checksum (an extension and improvement of the Fletcher checksum),
52205194Sdelphij   used for detection of data corruption, and provides an algorithm for
53205194Sdelphij   computing it.
54205194Sdelphij
55205194Sdelphij
56205194Sdelphij
57205194Sdelphij
58205194SdelphijDeutsch & Gailly             Informational                      [Page 1]
59205194Sdelphij
60205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
61205194Sdelphij
62205194Sdelphij
63205194SdelphijTable of Contents
64205194Sdelphij
65205194Sdelphij   1. Introduction ................................................... 2
66205194Sdelphij      1.1. Purpose ................................................... 2
67205194Sdelphij      1.2. Intended audience ......................................... 3
68205194Sdelphij      1.3. Scope ..................................................... 3
69205194Sdelphij      1.4. Compliance ................................................ 3
70205194Sdelphij      1.5.  Definitions of terms and conventions used ................ 3
71205194Sdelphij      1.6. Changes from previous versions ............................ 3
72205194Sdelphij   2. Detailed specification ......................................... 3
73205194Sdelphij      2.1. Overall conventions ....................................... 3
74205194Sdelphij      2.2. Data format ............................................... 4
75205194Sdelphij      2.3. Compliance ................................................ 7
76205194Sdelphij   3. References ..................................................... 7
77205194Sdelphij   4. Source code .................................................... 8
78205194Sdelphij   5. Security Considerations ........................................ 8
79205194Sdelphij   6. Acknowledgements ............................................... 8
80205194Sdelphij   7. Authors' Addresses ............................................. 8
81205194Sdelphij   8. Appendix: Rationale ............................................ 9
82205194Sdelphij   9. Appendix: Sample code ..........................................10
83205194Sdelphij
84205194Sdelphij1. Introduction
85205194Sdelphij
86205194Sdelphij   1.1. Purpose
87205194Sdelphij
88205194Sdelphij      The purpose of this specification is to define a lossless
89205194Sdelphij      compressed data format that:
90205194Sdelphij
91205194Sdelphij          * Is independent of CPU type, operating system, file system,
92205194Sdelphij            and character set, and hence can be used for interchange;
93205194Sdelphij
94205194Sdelphij          * Can be produced or consumed, even for an arbitrarily long
95205194Sdelphij            sequentially presented input data stream, using only an a
96205194Sdelphij            priori bounded amount of intermediate storage, and hence can
97205194Sdelphij            be used in data communications or similar structures such as
98205194Sdelphij            Unix filters;
99205194Sdelphij
100205194Sdelphij          * Can use a number of different compression methods;
101205194Sdelphij
102205194Sdelphij          * Can be implemented readily in a manner not covered by
103205194Sdelphij            patents, and hence can be practiced freely.
104205194Sdelphij
105205194Sdelphij      The data format defined by this specification does not attempt to
106205194Sdelphij      allow random access to compressed data.
107205194Sdelphij
108205194Sdelphij
109205194Sdelphij
110205194Sdelphij
111205194Sdelphij
112205194Sdelphij
113205194Sdelphij
114205194SdelphijDeutsch & Gailly             Informational                      [Page 2]
115205194Sdelphij
116205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
117205194Sdelphij
118205194Sdelphij
119205194Sdelphij   1.2. Intended audience
120205194Sdelphij
121205194Sdelphij      This specification is intended for use by implementors of software
122205194Sdelphij      to compress data into zlib format and/or decompress data from zlib
123205194Sdelphij      format.
124205194Sdelphij
125205194Sdelphij      The text of the specification assumes a basic background in
126205194Sdelphij      programming at the level of bits and other primitive data
127205194Sdelphij      representations.
128205194Sdelphij
129205194Sdelphij   1.3. Scope
130205194Sdelphij
131205194Sdelphij      The specification specifies a compressed data format that can be
132205194Sdelphij      used for in-memory compression of a sequence of arbitrary bytes.
133205194Sdelphij
134205194Sdelphij   1.4. Compliance
135205194Sdelphij
136205194Sdelphij      Unless otherwise indicated below, a compliant decompressor must be
137205194Sdelphij      able to accept and decompress any data set that conforms to all
138205194Sdelphij      the specifications presented here; a compliant compressor must
139205194Sdelphij      produce data sets that conform to all the specifications presented
140205194Sdelphij      here.
141205194Sdelphij
142205194Sdelphij   1.5.  Definitions of terms and conventions used
143205194Sdelphij
144205194Sdelphij      byte: 8 bits stored or transmitted as a unit (same as an octet).
145205194Sdelphij      (For this specification, a byte is exactly 8 bits, even on
146205194Sdelphij      machines which store a character on a number of bits different
147205194Sdelphij      from 8.) See below, for the numbering of bits within a byte.
148205194Sdelphij
149205194Sdelphij   1.6. Changes from previous versions
150205194Sdelphij
151205194Sdelphij      Version 3.1 was the first public release of this specification.
152205194Sdelphij      In version 3.2, some terminology was changed and the Adler-32
153205194Sdelphij      sample code was rewritten for clarity.  In version 3.3, the
154205194Sdelphij      support for a preset dictionary was introduced, and the
155205194Sdelphij      specification was converted to RFC style.
156205194Sdelphij
157205194Sdelphij2. Detailed specification
158205194Sdelphij
159205194Sdelphij   2.1. Overall conventions
160205194Sdelphij
161205194Sdelphij      In the diagrams below, a box like this:
162205194Sdelphij
163205194Sdelphij         +---+
164205194Sdelphij         |   | <-- the vertical bars might be missing
165205194Sdelphij         +---+
166205194Sdelphij
167205194Sdelphij
168205194Sdelphij
169205194Sdelphij
170205194SdelphijDeutsch & Gailly             Informational                      [Page 3]
171205194Sdelphij
172205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
173205194Sdelphij
174205194Sdelphij
175205194Sdelphij      represents one byte; a box like this:
176205194Sdelphij
177205194Sdelphij         +==============+
178205194Sdelphij         |              |
179205194Sdelphij         +==============+
180205194Sdelphij
181205194Sdelphij      represents a variable number of bytes.
182205194Sdelphij
183205194Sdelphij      Bytes stored within a computer do not have a "bit order", since
184205194Sdelphij      they are always treated as a unit.  However, a byte considered as
185205194Sdelphij      an integer between 0 and 255 does have a most- and least-
186205194Sdelphij      significant bit, and since we write numbers with the most-
187205194Sdelphij      significant digit on the left, we also write bytes with the most-
188205194Sdelphij      significant bit on the left.  In the diagrams below, we number the
189205194Sdelphij      bits of a byte so that bit 0 is the least-significant bit, i.e.,
190205194Sdelphij      the bits are numbered:
191205194Sdelphij
192205194Sdelphij         +--------+
193205194Sdelphij         |76543210|
194205194Sdelphij         +--------+
195205194Sdelphij
196205194Sdelphij      Within a computer, a number may occupy multiple bytes.  All
197205194Sdelphij      multi-byte numbers in the format described here are stored with
198205194Sdelphij      the MOST-significant byte first (at the lower memory address).
199205194Sdelphij      For example, the decimal number 520 is stored as:
200205194Sdelphij
201205194Sdelphij             0     1
202205194Sdelphij         +--------+--------+
203205194Sdelphij         |00000010|00001000|
204205194Sdelphij         +--------+--------+
205205194Sdelphij          ^        ^
206205194Sdelphij          |        |
207205194Sdelphij          |        + less significant byte = 8
208205194Sdelphij          + more significant byte = 2 x 256
209205194Sdelphij
210205194Sdelphij   2.2. Data format
211205194Sdelphij
212205194Sdelphij      A zlib stream has the following structure:
213205194Sdelphij
214205194Sdelphij           0   1
215205194Sdelphij         +---+---+
216205194Sdelphij         |CMF|FLG|   (more-->)
217205194Sdelphij         +---+---+
218205194Sdelphij
219205194Sdelphij
220205194Sdelphij
221205194Sdelphij
222205194Sdelphij
223205194Sdelphij
224205194Sdelphij
225205194Sdelphij
226205194SdelphijDeutsch & Gailly             Informational                      [Page 4]
227205194Sdelphij
228205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
229205194Sdelphij
230205194Sdelphij
231205194Sdelphij      (if FLG.FDICT set)
232205194Sdelphij
233205194Sdelphij           0   1   2   3
234205194Sdelphij         +---+---+---+---+
235205194Sdelphij         |     DICTID    |   (more-->)
236205194Sdelphij         +---+---+---+---+
237205194Sdelphij
238205194Sdelphij         +=====================+---+---+---+---+
239205194Sdelphij         |...compressed data...|    ADLER32    |
240205194Sdelphij         +=====================+---+---+---+---+
241205194Sdelphij
242205194Sdelphij      Any data which may appear after ADLER32 are not part of the zlib
243205194Sdelphij      stream.
244205194Sdelphij
245205194Sdelphij      CMF (Compression Method and flags)
246205194Sdelphij         This byte is divided into a 4-bit compression method and a 4-
247205194Sdelphij         bit information field depending on the compression method.
248205194Sdelphij
249205194Sdelphij            bits 0 to 3  CM     Compression method
250205194Sdelphij            bits 4 to 7  CINFO  Compression info
251205194Sdelphij
252205194Sdelphij      CM (Compression method)
253205194Sdelphij         This identifies the compression method used in the file. CM = 8
254205194Sdelphij         denotes the "deflate" compression method with a window size up
255205194Sdelphij         to 32K.  This is the method used by gzip and PNG (see
256205194Sdelphij         references [1] and [2] in Chapter 3, below, for the reference
257205194Sdelphij         documents).  CM = 15 is reserved.  It might be used in a future
258205194Sdelphij         version of this specification to indicate the presence of an
259205194Sdelphij         extra field before the compressed data.
260205194Sdelphij
261205194Sdelphij      CINFO (Compression info)
262205194Sdelphij         For CM = 8, CINFO is the base-2 logarithm of the LZ77 window
263205194Sdelphij         size, minus eight (CINFO=7 indicates a 32K window size). Values
264205194Sdelphij         of CINFO above 7 are not allowed in this version of the
265205194Sdelphij         specification.  CINFO is not defined in this specification for
266205194Sdelphij         CM not equal to 8.
267205194Sdelphij
268205194Sdelphij      FLG (FLaGs)
269205194Sdelphij         This flag byte is divided as follows:
270205194Sdelphij
271205194Sdelphij            bits 0 to 4  FCHECK  (check bits for CMF and FLG)
272205194Sdelphij            bit  5       FDICT   (preset dictionary)
273205194Sdelphij            bits 6 to 7  FLEVEL  (compression level)
274205194Sdelphij
275205194Sdelphij         The FCHECK value must be such that CMF and FLG, when viewed as
276205194Sdelphij         a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG),
277205194Sdelphij         is a multiple of 31.
278205194Sdelphij
279205194Sdelphij
280205194Sdelphij
281205194Sdelphij
282205194SdelphijDeutsch & Gailly             Informational                      [Page 5]
283205194Sdelphij
284205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
285205194Sdelphij
286205194Sdelphij
287205194Sdelphij      FDICT (Preset dictionary)
288205194Sdelphij         If FDICT is set, a DICT dictionary identifier is present
289205194Sdelphij         immediately after the FLG byte. The dictionary is a sequence of
290205194Sdelphij         bytes which are initially fed to the compressor without
291205194Sdelphij         producing any compressed output. DICT is the Adler-32 checksum
292205194Sdelphij         of this sequence of bytes (see the definition of ADLER32
293205194Sdelphij         below).  The decompressor can use this identifier to determine
294205194Sdelphij         which dictionary has been used by the compressor.
295205194Sdelphij
296205194Sdelphij      FLEVEL (Compression level)
297205194Sdelphij         These flags are available for use by specific compression
298205194Sdelphij         methods.  The "deflate" method (CM = 8) sets these flags as
299205194Sdelphij         follows:
300205194Sdelphij
301205194Sdelphij            0 - compressor used fastest algorithm
302205194Sdelphij            1 - compressor used fast algorithm
303205194Sdelphij            2 - compressor used default algorithm
304205194Sdelphij            3 - compressor used maximum compression, slowest algorithm
305205194Sdelphij
306205194Sdelphij         The information in FLEVEL is not needed for decompression; it
307205194Sdelphij         is there to indicate if recompression might be worthwhile.
308205194Sdelphij
309205194Sdelphij      compressed data
310205194Sdelphij         For compression method 8, the compressed data is stored in the
311205194Sdelphij         deflate compressed data format as described in the document
312205194Sdelphij         "DEFLATE Compressed Data Format Specification" by L. Peter
313205194Sdelphij         Deutsch. (See reference [3] in Chapter 3, below)
314205194Sdelphij
315205194Sdelphij         Other compressed data formats are not specified in this version
316205194Sdelphij         of the zlib specification.
317205194Sdelphij
318205194Sdelphij      ADLER32 (Adler-32 checksum)
319205194Sdelphij         This contains a checksum value of the uncompressed data
320205194Sdelphij         (excluding any dictionary data) computed according to Adler-32
321205194Sdelphij         algorithm. This algorithm is a 32-bit extension and improvement
322205194Sdelphij         of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
323205194Sdelphij         standard. See references [4] and [5] in Chapter 3, below)
324205194Sdelphij
325205194Sdelphij         Adler-32 is composed of two sums accumulated per byte: s1 is
326205194Sdelphij         the sum of all bytes, s2 is the sum of all s1 values. Both sums
327205194Sdelphij         are done modulo 65521. s1 is initialized to 1, s2 to zero.  The
328205194Sdelphij         Adler-32 checksum is stored as s2*65536 + s1 in most-
329205194Sdelphij         significant-byte first (network) order.
330205194Sdelphij
331205194Sdelphij
332205194Sdelphij
333205194Sdelphij
334205194Sdelphij
335205194Sdelphij
336205194Sdelphij
337205194Sdelphij
338205194SdelphijDeutsch & Gailly             Informational                      [Page 6]
339205194Sdelphij
340205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
341205194Sdelphij
342205194Sdelphij
343205194Sdelphij   2.3. Compliance
344205194Sdelphij
345205194Sdelphij      A compliant compressor must produce streams with correct CMF, FLG
346205194Sdelphij      and ADLER32, but need not support preset dictionaries.  When the
347205194Sdelphij      zlib data format is used as part of another standard data format,
348205194Sdelphij      the compressor may use only preset dictionaries that are specified
349205194Sdelphij      by this other data format.  If this other format does not use the
350205194Sdelphij      preset dictionary feature, the compressor must not set the FDICT
351205194Sdelphij      flag.
352205194Sdelphij
353205194Sdelphij      A compliant decompressor must check CMF, FLG, and ADLER32, and
354205194Sdelphij      provide an error indication if any of these have incorrect values.
355205194Sdelphij      A compliant decompressor must give an error indication if CM is
356205194Sdelphij      not one of the values defined in this specification (only the
357205194Sdelphij      value 8 is permitted in this version), since another value could
358205194Sdelphij      indicate the presence of new features that would cause subsequent
359205194Sdelphij      data to be interpreted incorrectly.  A compliant decompressor must
360205194Sdelphij      give an error indication if FDICT is set and DICTID is not the
361205194Sdelphij      identifier of a known preset dictionary.  A decompressor may
362205194Sdelphij      ignore FLEVEL and still be compliant.  When the zlib data format
363205194Sdelphij      is being used as a part of another standard format, a compliant
364205194Sdelphij      decompressor must support all the preset dictionaries specified by
365205194Sdelphij      the other format. When the other format does not use the preset
366205194Sdelphij      dictionary feature, a compliant decompressor must reject any
367205194Sdelphij      stream in which the FDICT flag is set.
368205194Sdelphij
369205194Sdelphij3. References
370205194Sdelphij
371205194Sdelphij   [1] Deutsch, L.P.,"GZIP Compressed Data Format Specification",
372205194Sdelphij       available in ftp://ftp.uu.net/pub/archiving/zip/doc/
373205194Sdelphij
374205194Sdelphij   [2] Thomas Boutell, "PNG (Portable Network Graphics) specification",
375205194Sdelphij       available in ftp://ftp.uu.net/graphics/png/documents/
376205194Sdelphij
377205194Sdelphij   [3] Deutsch, L.P.,"DEFLATE Compressed Data Format Specification",
378205194Sdelphij       available in ftp://ftp.uu.net/pub/archiving/zip/doc/
379205194Sdelphij
380205194Sdelphij   [4] Fletcher, J. G., "An Arithmetic Checksum for Serial
381205194Sdelphij       Transmissions," IEEE Transactions on Communications, Vol. COM-30,
382205194Sdelphij       No. 1, January 1982, pp. 247-252.
383205194Sdelphij
384205194Sdelphij   [5] ITU-T Recommendation X.224, Annex D, "Checksum Algorithms,"
385205194Sdelphij       November, 1993, pp. 144, 145. (Available from
386205194Sdelphij       gopher://info.itu.ch). ITU-T X.244 is also the same as ISO 8073.
387205194Sdelphij
388205194Sdelphij
389205194Sdelphij
390205194Sdelphij
391205194Sdelphij
392205194Sdelphij
393205194Sdelphij
394205194SdelphijDeutsch & Gailly             Informational                      [Page 7]
395205194Sdelphij
396205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
397205194Sdelphij
398205194Sdelphij
399205194Sdelphij4. Source code
400205194Sdelphij
401205194Sdelphij   Source code for a C language implementation of a "zlib" compliant
402205194Sdelphij   library is available at ftp://ftp.uu.net/pub/archiving/zip/zlib/.
403205194Sdelphij
404205194Sdelphij5. Security Considerations
405205194Sdelphij
406205194Sdelphij   A decoder that fails to check the ADLER32 checksum value may be
407205194Sdelphij   subject to undetected data corruption.
408205194Sdelphij
409205194Sdelphij6. Acknowledgements
410205194Sdelphij
411205194Sdelphij   Trademarks cited in this document are the property of their
412205194Sdelphij   respective owners.
413205194Sdelphij
414205194Sdelphij   Jean-Loup Gailly and Mark Adler designed the zlib format and wrote
415205194Sdelphij   the related software described in this specification.  Glenn
416205194Sdelphij   Randers-Pehrson converted this document to RFC and HTML format.
417205194Sdelphij
418205194Sdelphij7. Authors' Addresses
419205194Sdelphij
420205194Sdelphij   L. Peter Deutsch
421205194Sdelphij   Aladdin Enterprises
422205194Sdelphij   203 Santa Margarita Ave.
423205194Sdelphij   Menlo Park, CA 94025
424205194Sdelphij
425205194Sdelphij   Phone: (415) 322-0103 (AM only)
426205194Sdelphij   FAX:   (415) 322-1734
427205194Sdelphij   EMail: <ghost@aladdin.com>
428205194Sdelphij
429205194Sdelphij
430205194Sdelphij   Jean-Loup Gailly
431205194Sdelphij
432205194Sdelphij   EMail: <gzip@prep.ai.mit.edu>
433205194Sdelphij
434205194Sdelphij   Questions about the technical content of this specification can be
435205194Sdelphij   sent by email to
436205194Sdelphij
437205194Sdelphij   Jean-Loup Gailly <gzip@prep.ai.mit.edu> and
438205194Sdelphij   Mark Adler <madler@alumni.caltech.edu>
439205194Sdelphij
440205194Sdelphij   Editorial comments on this specification can be sent by email to
441205194Sdelphij
442205194Sdelphij   L. Peter Deutsch <ghost@aladdin.com> and
443205194Sdelphij   Glenn Randers-Pehrson <randeg@alumni.rpi.edu>
444205194Sdelphij
445205194Sdelphij
446205194Sdelphij
447205194Sdelphij
448205194Sdelphij
449205194Sdelphij
450205194SdelphijDeutsch & Gailly             Informational                      [Page 8]
451205194Sdelphij
452205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
453205194Sdelphij
454205194Sdelphij
455205194Sdelphij8. Appendix: Rationale
456205194Sdelphij
457205194Sdelphij   8.1. Preset dictionaries
458205194Sdelphij
459205194Sdelphij      A preset dictionary is specially useful to compress short input
460205194Sdelphij      sequences. The compressor can take advantage of the dictionary
461205194Sdelphij      context to encode the input in a more compact manner. The
462205194Sdelphij      decompressor can be initialized with the appropriate context by
463205194Sdelphij      virtually decompressing a compressed version of the dictionary
464205194Sdelphij      without producing any output. However for certain compression
465205194Sdelphij      algorithms such as the deflate algorithm this operation can be
466205194Sdelphij      achieved without actually performing any decompression.
467205194Sdelphij
468205194Sdelphij      The compressor and the decompressor must use exactly the same
469205194Sdelphij      dictionary. The dictionary may be fixed or may be chosen among a
470205194Sdelphij      certain number of predefined dictionaries, according to the kind
471205194Sdelphij      of input data. The decompressor can determine which dictionary has
472205194Sdelphij      been chosen by the compressor by checking the dictionary
473205194Sdelphij      identifier. This document does not specify the contents of
474205194Sdelphij      predefined dictionaries, since the optimal dictionaries are
475205194Sdelphij      application specific. Standard data formats using this feature of
476205194Sdelphij      the zlib specification must precisely define the allowed
477205194Sdelphij      dictionaries.
478205194Sdelphij
479205194Sdelphij   8.2. The Adler-32 algorithm
480205194Sdelphij
481205194Sdelphij      The Adler-32 algorithm is much faster than the CRC32 algorithm yet
482205194Sdelphij      still provides an extremely low probability of undetected errors.
483205194Sdelphij
484205194Sdelphij      The modulo on unsigned long accumulators can be delayed for 5552
485205194Sdelphij      bytes, so the modulo operation time is negligible.  If the bytes
486205194Sdelphij      are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
487205194Sdelphij      and order sensitive, unlike the first sum, which is just a
488205194Sdelphij      checksum.  That 65521 is prime is important to avoid a possible
489205194Sdelphij      large class of two-byte errors that leave the check unchanged.
490205194Sdelphij      (The Fletcher checksum uses 255, which is not prime and which also
491205194Sdelphij      makes the Fletcher check insensitive to single byte changes 0 <->
492205194Sdelphij      255.)
493205194Sdelphij
494205194Sdelphij      The sum s1 is initialized to 1 instead of zero to make the length
495205194Sdelphij      of the sequence part of s2, so that the length does not have to be
496205194Sdelphij      checked separately. (Any sequence of zeroes has a Fletcher
497205194Sdelphij      checksum of zero.)
498205194Sdelphij
499205194Sdelphij
500205194Sdelphij
501205194Sdelphij
502205194Sdelphij
503205194Sdelphij
504205194Sdelphij
505205194Sdelphij
506205194SdelphijDeutsch & Gailly             Informational                      [Page 9]
507205194Sdelphij
508205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
509205194Sdelphij
510205194Sdelphij
511205194Sdelphij9. Appendix: Sample code
512205194Sdelphij
513205194Sdelphij   The following C code computes the Adler-32 checksum of a data buffer.
514205194Sdelphij   It is written for clarity, not for speed.  The sample code is in the
515205194Sdelphij   ANSI C programming language. Non C users may find it easier to read
516205194Sdelphij   with these hints:
517205194Sdelphij
518205194Sdelphij      &      Bitwise AND operator.
519205194Sdelphij      >>     Bitwise right shift operator. When applied to an
520205194Sdelphij             unsigned quantity, as here, right shift inserts zero bit(s)
521205194Sdelphij             at the left.
522205194Sdelphij      <<     Bitwise left shift operator. Left shift inserts zero
523205194Sdelphij             bit(s) at the right.
524205194Sdelphij      ++     "n++" increments the variable n.
525205194Sdelphij      %      modulo operator: a % b is the remainder of a divided by b.
526205194Sdelphij
527205194Sdelphij      #define BASE 65521 /* largest prime smaller than 65536 */
528205194Sdelphij
529205194Sdelphij      /*
530205194Sdelphij         Update a running Adler-32 checksum with the bytes buf[0..len-1]
531205194Sdelphij       and return the updated checksum. The Adler-32 checksum should be
532205194Sdelphij       initialized to 1.
533205194Sdelphij
534205194Sdelphij       Usage example:
535205194Sdelphij
536205194Sdelphij         unsigned long adler = 1L;
537205194Sdelphij
538205194Sdelphij         while (read_buffer(buffer, length) != EOF) {
539205194Sdelphij           adler = update_adler32(adler, buffer, length);
540205194Sdelphij         }
541205194Sdelphij         if (adler != original_adler) error();
542205194Sdelphij      */
543205194Sdelphij      unsigned long update_adler32(unsigned long adler,
544205194Sdelphij         unsigned char *buf, int len)
545205194Sdelphij      {
546205194Sdelphij        unsigned long s1 = adler & 0xffff;
547205194Sdelphij        unsigned long s2 = (adler >> 16) & 0xffff;
548205194Sdelphij        int n;
549205194Sdelphij
550205194Sdelphij        for (n = 0; n < len; n++) {
551205194Sdelphij          s1 = (s1 + buf[n]) % BASE;
552205194Sdelphij          s2 = (s2 + s1)     % BASE;
553205194Sdelphij        }
554205194Sdelphij        return (s2 << 16) + s1;
555205194Sdelphij      }
556205194Sdelphij
557205194Sdelphij      /* Return the adler32 of the bytes buf[0..len-1] */
558205194Sdelphij
559205194Sdelphij
560205194Sdelphij
561205194Sdelphij
562205194SdelphijDeutsch & Gailly             Informational                     [Page 10]
563205194Sdelphij
564205194SdelphijRFC 1950       ZLIB Compressed Data Format Specification        May 1996
565205194Sdelphij
566205194Sdelphij
567205194Sdelphij      unsigned long adler32(unsigned char *buf, int len)
568205194Sdelphij      {
569205194Sdelphij        return update_adler32(1L, buf, len);
570205194Sdelphij      }
571205194Sdelphij
572205194Sdelphij
573205194Sdelphij
574205194Sdelphij
575205194Sdelphij
576205194Sdelphij
577205194Sdelphij
578205194Sdelphij
579205194Sdelphij
580205194Sdelphij
581205194Sdelphij
582205194Sdelphij
583205194Sdelphij
584205194Sdelphij
585205194Sdelphij
586205194Sdelphij
587205194Sdelphij
588205194Sdelphij
589205194Sdelphij
590205194Sdelphij
591205194Sdelphij
592205194Sdelphij
593205194Sdelphij
594205194Sdelphij
595205194Sdelphij
596205194Sdelphij
597205194Sdelphij
598205194Sdelphij
599205194Sdelphij
600205194Sdelphij
601205194Sdelphij
602205194Sdelphij
603205194Sdelphij
604205194Sdelphij
605205194Sdelphij
606205194Sdelphij
607205194Sdelphij
608205194Sdelphij
609205194Sdelphij
610205194Sdelphij
611205194Sdelphij
612205194Sdelphij
613205194Sdelphij
614205194Sdelphij
615205194Sdelphij
616205194Sdelphij
617205194Sdelphij
618205194SdelphijDeutsch & Gailly             Informational                     [Page 11]
619205194Sdelphij
620