rfc1950.txt revision 1.1 1 1.1 christos
2 1.1 christos
3 1.1 christos
4 1.1 christos
5 1.1 christos
6 1.1 christos
7 1.1 christos Network Working Group P. Deutsch
8 1.1 christos Request for Comments: 1950 Aladdin Enterprises
9 1.1 christos Category: Informational J-L. Gailly
10 1.1 christos Info-ZIP
11 1.1 christos May 1996
12 1.1 christos
13 1.1 christos
14 1.1 christos ZLIB Compressed Data Format Specification version 3.3
15 1.1 christos
16 1.1 christos Status of This Memo
17 1.1 christos
18 1.1 christos This memo provides information for the Internet community. This memo
19 1.1 christos does not specify an Internet standard of any kind. Distribution of
20 1.1 christos this memo is unlimited.
21 1.1 christos
22 1.1 christos IESG Note:
23 1.1 christos
24 1.1 christos The IESG takes no position on the validity of any Intellectual
25 1.1 christos Property Rights statements contained in this document.
26 1.1 christos
27 1.1 christos Notices
28 1.1 christos
29 1.1 christos Copyright (c) 1996 L. Peter Deutsch and Jean-Loup Gailly
30 1.1 christos
31 1.1 christos Permission is granted to copy and distribute this document for any
32 1.1 christos purpose and without charge, including translations into other
33 1.1 christos languages and incorporation into compilations, provided that the
34 1.1 christos copyright notice and this notice are preserved, and that any
35 1.1 christos substantive changes or deletions from the original are clearly
36 1.1 christos marked.
37 1.1 christos
38 1.1 christos A pointer to the latest version of this and related documentation in
39 1.1 christos HTML format can be found at the URL
40 1.1 christos <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>.
41 1.1 christos
42 1.1 christos Abstract
43 1.1 christos
44 1.1 christos This specification defines a lossless compressed data format. The
45 1.1 christos data can be produced or consumed, even for an arbitrarily long
46 1.1 christos sequentially presented input data stream, using only an a priori
47 1.1 christos bounded amount of intermediate storage. The format presently uses
48 1.1 christos the DEFLATE compression method but can be easily extended to use
49 1.1 christos other compression methods. It can be implemented readily in a manner
50 1.1 christos not covered by patents. This specification also defines the ADLER-32
51 1.1 christos checksum (an extension and improvement of the Fletcher checksum),
52 1.1 christos used for detection of data corruption, and provides an algorithm for
53 1.1 christos computing it.
54 1.1 christos
55 1.1 christos
56 1.1 christos
57 1.1 christos
58 1.1 christos Deutsch & Gailly Informational [Page 1]
59 1.1 christos
61 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
62 1.1 christos
63 1.1 christos
64 1.1 christos Table of Contents
65 1.1 christos
66 1.1 christos 1. Introduction ................................................... 2
67 1.1 christos 1.1. Purpose ................................................... 2
68 1.1 christos 1.2. Intended audience ......................................... 3
69 1.1 christos 1.3. Scope ..................................................... 3
70 1.1 christos 1.4. Compliance ................................................ 3
71 1.1 christos 1.5. Definitions of terms and conventions used ................ 3
72 1.1 christos 1.6. Changes from previous versions ............................ 3
73 1.1 christos 2. Detailed specification ......................................... 3
74 1.1 christos 2.1. Overall conventions ....................................... 3
75 1.1 christos 2.2. Data format ............................................... 4
76 1.1 christos 2.3. Compliance ................................................ 7
77 1.1 christos 3. References ..................................................... 7
78 1.1 christos 4. Source code .................................................... 8
79 1.1 christos 5. Security Considerations ........................................ 8
80 1.1 christos 6. Acknowledgements ............................................... 8
81 1.1 christos 7. Authors' Addresses ............................................. 8
82 1.1 christos 8. Appendix: Rationale ............................................ 9
83 1.1 christos 9. Appendix: Sample code ..........................................10
84 1.1 christos
85 1.1 christos 1. Introduction
86 1.1 christos
87 1.1 christos 1.1. Purpose
88 1.1 christos
89 1.1 christos The purpose of this specification is to define a lossless
90 1.1 christos compressed data format that:
91 1.1 christos
92 1.1 christos * Is independent of CPU type, operating system, file system,
93 1.1 christos and character set, and hence can be used for interchange;
94 1.1 christos
95 1.1 christos * Can be produced or consumed, even for an arbitrarily long
96 1.1 christos sequentially presented input data stream, using only an a
97 1.1 christos priori bounded amount of intermediate storage, and hence can
98 1.1 christos be used in data communications or similar structures such as
99 1.1 christos Unix filters;
100 1.1 christos
101 1.1 christos * Can use a number of different compression methods;
102 1.1 christos
103 1.1 christos * Can be implemented readily in a manner not covered by
104 1.1 christos patents, and hence can be practiced freely.
105 1.1 christos
106 1.1 christos The data format defined by this specification does not attempt to
107 1.1 christos allow random access to compressed data.
108 1.1 christos
109 1.1 christos
110 1.1 christos
111 1.1 christos
112 1.1 christos
113 1.1 christos
114 1.1 christos
115 1.1 christos Deutsch & Gailly Informational [Page 2]
116 1.1 christos
118 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
119 1.1 christos
120 1.1 christos
121 1.1 christos 1.2. Intended audience
122 1.1 christos
123 1.1 christos This specification is intended for use by implementors of software
124 1.1 christos to compress data into zlib format and/or decompress data from zlib
125 1.1 christos format.
126 1.1 christos
127 1.1 christos The text of the specification assumes a basic background in
128 1.1 christos programming at the level of bits and other primitive data
129 1.1 christos representations.
130 1.1 christos
131 1.1 christos 1.3. Scope
132 1.1 christos
133 1.1 christos The specification specifies a compressed data format that can be
134 1.1 christos used for in-memory compression of a sequence of arbitrary bytes.
135 1.1 christos
136 1.1 christos 1.4. Compliance
137 1.1 christos
138 1.1 christos Unless otherwise indicated below, a compliant decompressor must be
139 1.1 christos able to accept and decompress any data set that conforms to all
140 1.1 christos the specifications presented here; a compliant compressor must
141 1.1 christos produce data sets that conform to all the specifications presented
142 1.1 christos here.
143 1.1 christos
144 1.1 christos 1.5. Definitions of terms and conventions used
145 1.1 christos
146 1.1 christos byte: 8 bits stored or transmitted as a unit (same as an octet).
147 1.1 christos (For this specification, a byte is exactly 8 bits, even on
148 1.1 christos machines which store a character on a number of bits different
149 1.1 christos from 8.) See below, for the numbering of bits within a byte.
150 1.1 christos
151 1.1 christos 1.6. Changes from previous versions
152 1.1 christos
153 1.1 christos Version 3.1 was the first public release of this specification.
154 1.1 christos In version 3.2, some terminology was changed and the Adler-32
155 1.1 christos sample code was rewritten for clarity. In version 3.3, the
156 1.1 christos support for a preset dictionary was introduced, and the
157 1.1 christos specification was converted to RFC style.
158 1.1 christos
159 1.1 christos 2. Detailed specification
160 1.1 christos
161 1.1 christos 2.1. Overall conventions
162 1.1 christos
163 1.1 christos In the diagrams below, a box like this:
164 1.1 christos
165 1.1 christos +---+
166 1.1 christos | | <-- the vertical bars might be missing
167 1.1 christos +---+
168 1.1 christos
169 1.1 christos
170 1.1 christos
171 1.1 christos
172 1.1 christos Deutsch & Gailly Informational [Page 3]
173 1.1 christos
175 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
176 1.1 christos
177 1.1 christos
178 1.1 christos represents one byte; a box like this:
179 1.1 christos
180 1.1 christos +==============+
181 1.1 christos | |
182 1.1 christos +==============+
183 1.1 christos
184 1.1 christos represents a variable number of bytes.
185 1.1 christos
186 1.1 christos Bytes stored within a computer do not have a "bit order", since
187 1.1 christos they are always treated as a unit. However, a byte considered as
188 1.1 christos an integer between 0 and 255 does have a most- and least-
189 1.1 christos significant bit, and since we write numbers with the most-
190 1.1 christos significant digit on the left, we also write bytes with the most-
191 1.1 christos significant bit on the left. In the diagrams below, we number the
192 1.1 christos bits of a byte so that bit 0 is the least-significant bit, i.e.,
193 1.1 christos the bits are numbered:
194 1.1 christos
195 1.1 christos +--------+
196 1.1 christos |76543210|
197 1.1 christos +--------+
198 1.1 christos
199 1.1 christos Within a computer, a number may occupy multiple bytes. All
200 1.1 christos multi-byte numbers in the format described here are stored with
201 1.1 christos the MOST-significant byte first (at the lower memory address).
202 1.1 christos For example, the decimal number 520 is stored as:
203 1.1 christos
204 1.1 christos 0 1
205 1.1 christos +--------+--------+
206 1.1 christos |00000010|00001000|
207 1.1 christos +--------+--------+
208 1.1 christos ^ ^
209 1.1 christos | |
210 1.1 christos | + less significant byte = 8
211 1.1 christos + more significant byte = 2 x 256
212 1.1 christos
213 1.1 christos 2.2. Data format
214 1.1 christos
215 1.1 christos A zlib stream has the following structure:
216 1.1 christos
217 1.1 christos 0 1
218 1.1 christos +---+---+
219 1.1 christos |CMF|FLG| (more-->)
220 1.1 christos +---+---+
221 1.1 christos
222 1.1 christos
223 1.1 christos
224 1.1 christos
225 1.1 christos
226 1.1 christos
227 1.1 christos
228 1.1 christos
229 1.1 christos Deutsch & Gailly Informational [Page 4]
230 1.1 christos
232 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
233 1.1 christos
234 1.1 christos
235 1.1 christos (if FLG.FDICT set)
236 1.1 christos
237 1.1 christos 0 1 2 3
238 1.1 christos +---+---+---+---+
239 1.1 christos | DICTID | (more-->)
240 1.1 christos +---+---+---+---+
241 1.1 christos
242 1.1 christos +=====================+---+---+---+---+
243 1.1 christos |...compressed data...| ADLER32 |
244 1.1 christos +=====================+---+---+---+---+
245 1.1 christos
246 1.1 christos Any data which may appear after ADLER32 are not part of the zlib
247 1.1 christos stream.
248 1.1 christos
249 1.1 christos CMF (Compression Method and flags)
250 1.1 christos This byte is divided into a 4-bit compression method and a 4-
251 1.1 christos bit information field depending on the compression method.
252 1.1 christos
253 1.1 christos bits 0 to 3 CM Compression method
254 1.1 christos bits 4 to 7 CINFO Compression info
255 1.1 christos
256 1.1 christos CM (Compression method)
257 1.1 christos This identifies the compression method used in the file. CM = 8
258 1.1 christos denotes the "deflate" compression method with a window size up
259 1.1 christos to 32K. This is the method used by gzip and PNG (see
260 1.1 christos references [1] and [2] in Chapter 3, below, for the reference
261 1.1 christos documents). CM = 15 is reserved. It might be used in a future
262 1.1 christos version of this specification to indicate the presence of an
263 1.1 christos extra field before the compressed data.
264 1.1 christos
265 1.1 christos CINFO (Compression info)
266 1.1 christos For CM = 8, CINFO is the base-2 logarithm of the LZ77 window
267 1.1 christos size, minus eight (CINFO=7 indicates a 32K window size). Values
268 1.1 christos of CINFO above 7 are not allowed in this version of the
269 1.1 christos specification. CINFO is not defined in this specification for
270 1.1 christos CM not equal to 8.
271 1.1 christos
272 1.1 christos FLG (FLaGs)
273 1.1 christos This flag byte is divided as follows:
274 1.1 christos
275 1.1 christos bits 0 to 4 FCHECK (check bits for CMF and FLG)
276 1.1 christos bit 5 FDICT (preset dictionary)
277 1.1 christos bits 6 to 7 FLEVEL (compression level)
278 1.1 christos
279 1.1 christos The FCHECK value must be such that CMF and FLG, when viewed as
280 1.1 christos a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG),
281 1.1 christos is a multiple of 31.
282 1.1 christos
283 1.1 christos
284 1.1 christos
285 1.1 christos
286 1.1 christos Deutsch & Gailly Informational [Page 5]
287 1.1 christos
289 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
290 1.1 christos
291 1.1 christos
292 1.1 christos FDICT (Preset dictionary)
293 1.1 christos If FDICT is set, a DICT dictionary identifier is present
294 1.1 christos immediately after the FLG byte. The dictionary is a sequence of
295 1.1 christos bytes which are initially fed to the compressor without
296 1.1 christos producing any compressed output. DICT is the Adler-32 checksum
297 1.1 christos of this sequence of bytes (see the definition of ADLER32
298 1.1 christos below). The decompressor can use this identifier to determine
299 1.1 christos which dictionary has been used by the compressor.
300 1.1 christos
301 1.1 christos FLEVEL (Compression level)
302 1.1 christos These flags are available for use by specific compression
303 1.1 christos methods. The "deflate" method (CM = 8) sets these flags as
304 1.1 christos follows:
305 1.1 christos
306 1.1 christos 0 - compressor used fastest algorithm
307 1.1 christos 1 - compressor used fast algorithm
308 1.1 christos 2 - compressor used default algorithm
309 1.1 christos 3 - compressor used maximum compression, slowest algorithm
310 1.1 christos
311 1.1 christos The information in FLEVEL is not needed for decompression; it
312 1.1 christos is there to indicate if recompression might be worthwhile.
313 1.1 christos
314 1.1 christos compressed data
315 1.1 christos For compression method 8, the compressed data is stored in the
316 1.1 christos deflate compressed data format as described in the document
317 1.1 christos "DEFLATE Compressed Data Format Specification" by L. Peter
318 1.1 christos Deutsch. (See reference [3] in Chapter 3, below)
319 1.1 christos
320 1.1 christos Other compressed data formats are not specified in this version
321 1.1 christos of the zlib specification.
322 1.1 christos
323 1.1 christos ADLER32 (Adler-32 checksum)
324 1.1 christos This contains a checksum value of the uncompressed data
325 1.1 christos (excluding any dictionary data) computed according to Adler-32
326 1.1 christos algorithm. This algorithm is a 32-bit extension and improvement
327 1.1 christos of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
328 1.1 christos standard. See references [4] and [5] in Chapter 3, below)
329 1.1 christos
330 1.1 christos Adler-32 is composed of two sums accumulated per byte: s1 is
331 1.1 christos the sum of all bytes, s2 is the sum of all s1 values. Both sums
332 1.1 christos are done modulo 65521. s1 is initialized to 1, s2 to zero. The
333 1.1 christos Adler-32 checksum is stored as s2*65536 + s1 in most-
334 1.1 christos significant-byte first (network) order.
335 1.1 christos
336 1.1 christos
337 1.1 christos
338 1.1 christos
339 1.1 christos
340 1.1 christos
341 1.1 christos
342 1.1 christos
343 1.1 christos Deutsch & Gailly Informational [Page 6]
344 1.1 christos
346 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
347 1.1 christos
348 1.1 christos
349 1.1 christos 2.3. Compliance
350 1.1 christos
351 1.1 christos A compliant compressor must produce streams with correct CMF, FLG
352 1.1 christos and ADLER32, but need not support preset dictionaries. When the
353 1.1 christos zlib data format is used as part of another standard data format,
354 1.1 christos the compressor may use only preset dictionaries that are specified
355 1.1 christos by this other data format. If this other format does not use the
356 1.1 christos preset dictionary feature, the compressor must not set the FDICT
357 1.1 christos flag.
358 1.1 christos
359 1.1 christos A compliant decompressor must check CMF, FLG, and ADLER32, and
360 1.1 christos provide an error indication if any of these have incorrect values.
361 1.1 christos A compliant decompressor must give an error indication if CM is
362 1.1 christos not one of the values defined in this specification (only the
363 1.1 christos value 8 is permitted in this version), since another value could
364 1.1 christos indicate the presence of new features that would cause subsequent
365 1.1 christos data to be interpreted incorrectly. A compliant decompressor must
366 1.1 christos give an error indication if FDICT is set and DICTID is not the
367 1.1 christos identifier of a known preset dictionary. A decompressor may
368 1.1 christos ignore FLEVEL and still be compliant. When the zlib data format
369 1.1 christos is being used as a part of another standard format, a compliant
370 1.1 christos decompressor must support all the preset dictionaries specified by
371 1.1 christos the other format. When the other format does not use the preset
372 1.1 christos dictionary feature, a compliant decompressor must reject any
373 1.1 christos stream in which the FDICT flag is set.
374 1.1 christos
375 1.1 christos 3. References
376 1.1 christos
377 1.1 christos [1] Deutsch, L.P.,"GZIP Compressed Data Format Specification",
378 1.1 christos available in ftp://ftp.uu.net/pub/archiving/zip/doc/
379 1.1 christos
380 1.1 christos [2] Thomas Boutell, "PNG (Portable Network Graphics) specification",
381 1.1 christos available in ftp://ftp.uu.net/graphics/png/documents/
382 1.1 christos
383 1.1 christos [3] Deutsch, L.P.,"DEFLATE Compressed Data Format Specification",
384 1.1 christos available in ftp://ftp.uu.net/pub/archiving/zip/doc/
385 1.1 christos
386 1.1 christos [4] Fletcher, J. G., "An Arithmetic Checksum for Serial
387 1.1 christos Transmissions," IEEE Transactions on Communications, Vol. COM-30,
388 1.1 christos No. 1, January 1982, pp. 247-252.
389 1.1 christos
390 1.1 christos [5] ITU-T Recommendation X.224, Annex D, "Checksum Algorithms,"
391 1.1 christos November, 1993, pp. 144, 145. (Available from
392 1.1 christos gopher://info.itu.ch). ITU-T X.244 is also the same as ISO 8073.
393 1.1 christos
394 1.1 christos
395 1.1 christos
396 1.1 christos
397 1.1 christos
398 1.1 christos
399 1.1 christos
400 1.1 christos Deutsch & Gailly Informational [Page 7]
401 1.1 christos
403 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
404 1.1 christos
405 1.1 christos
406 1.1 christos 4. Source code
407 1.1 christos
408 1.1 christos Source code for a C language implementation of a "zlib" compliant
409 1.1 christos library is available at ftp://ftp.uu.net/pub/archiving/zip/zlib/.
410 1.1 christos
411 1.1 christos 5. Security Considerations
412 1.1 christos
413 1.1 christos A decoder that fails to check the ADLER32 checksum value may be
414 1.1 christos subject to undetected data corruption.
415 1.1 christos
416 1.1 christos 6. Acknowledgements
417 1.1 christos
418 1.1 christos Trademarks cited in this document are the property of their
419 1.1 christos respective owners.
420 1.1 christos
421 1.1 christos Jean-Loup Gailly and Mark Adler designed the zlib format and wrote
422 1.1 christos the related software described in this specification. Glenn
423 1.1 christos Randers-Pehrson converted this document to RFC and HTML format.
424 1.1 christos
425 1.1 christos 7. Authors' Addresses
426 1.1 christos
427 1.1 christos L. Peter Deutsch
428 1.1 christos Aladdin Enterprises
429 1.1 christos 203 Santa Margarita Ave.
430 1.1 christos Menlo Park, CA 94025
431 1.1 christos
432 1.1 christos Phone: (415) 322-0103 (AM only)
433 1.1 christos FAX: (415) 322-1734
434 1.1 christos EMail: <ghost (a] aladdin.com>
435 1.1 christos
436 1.1 christos
437 1.1 christos Jean-Loup Gailly
438 1.1 christos
439 1.1 christos EMail: <gzip (a] prep.ai.mit.edu>
440 1.1 christos
441 1.1 christos Questions about the technical content of this specification can be
442 1.1 christos sent by email to
443 1.1 christos
444 1.1 christos Jean-Loup Gailly <gzip (a] prep.ai.mit.edu> and
445 1.1 christos Mark Adler <madler (a] alumni.caltech.edu>
446 1.1 christos
447 1.1 christos Editorial comments on this specification can be sent by email to
448 1.1 christos
449 1.1 christos L. Peter Deutsch <ghost (a] aladdin.com> and
450 1.1 christos Glenn Randers-Pehrson <randeg (a] alumni.rpi.edu>
451 1.1 christos
452 1.1 christos
453 1.1 christos
454 1.1 christos
455 1.1 christos
456 1.1 christos
457 1.1 christos Deutsch & Gailly Informational [Page 8]
458 1.1 christos
460 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
461 1.1 christos
462 1.1 christos
463 1.1 christos 8. Appendix: Rationale
464 1.1 christos
465 1.1 christos 8.1. Preset dictionaries
466 1.1 christos
467 1.1 christos A preset dictionary is specially useful to compress short input
468 1.1 christos sequences. The compressor can take advantage of the dictionary
469 1.1 christos context to encode the input in a more compact manner. The
470 1.1 christos decompressor can be initialized with the appropriate context by
471 1.1 christos virtually decompressing a compressed version of the dictionary
472 1.1 christos without producing any output. However for certain compression
473 1.1 christos algorithms such as the deflate algorithm this operation can be
474 1.1 christos achieved without actually performing any decompression.
475 1.1 christos
476 1.1 christos The compressor and the decompressor must use exactly the same
477 1.1 christos dictionary. The dictionary may be fixed or may be chosen among a
478 1.1 christos certain number of predefined dictionaries, according to the kind
479 1.1 christos of input data. The decompressor can determine which dictionary has
480 1.1 christos been chosen by the compressor by checking the dictionary
481 1.1 christos identifier. This document does not specify the contents of
482 1.1 christos predefined dictionaries, since the optimal dictionaries are
483 1.1 christos application specific. Standard data formats using this feature of
484 1.1 christos the zlib specification must precisely define the allowed
485 1.1 christos dictionaries.
486 1.1 christos
487 1.1 christos 8.2. The Adler-32 algorithm
488 1.1 christos
489 1.1 christos The Adler-32 algorithm is much faster than the CRC32 algorithm yet
490 1.1 christos still provides an extremely low probability of undetected errors.
491 1.1 christos
492 1.1 christos The modulo on unsigned long accumulators can be delayed for 5552
493 1.1 christos bytes, so the modulo operation time is negligible. If the bytes
494 1.1 christos are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
495 1.1 christos and order sensitive, unlike the first sum, which is just a
496 1.1 christos checksum. That 65521 is prime is important to avoid a possible
497 1.1 christos large class of two-byte errors that leave the check unchanged.
498 1.1 christos (The Fletcher checksum uses 255, which is not prime and which also
499 1.1 christos makes the Fletcher check insensitive to single byte changes 0 <->
500 1.1 christos 255.)
501 1.1 christos
502 1.1 christos The sum s1 is initialized to 1 instead of zero to make the length
503 1.1 christos of the sequence part of s2, so that the length does not have to be
504 1.1 christos checked separately. (Any sequence of zeroes has a Fletcher
505 1.1 christos checksum of zero.)
506 1.1 christos
507 1.1 christos
508 1.1 christos
509 1.1 christos
510 1.1 christos
511 1.1 christos
512 1.1 christos
513 1.1 christos
514 1.1 christos Deutsch & Gailly Informational [Page 9]
515 1.1 christos
517 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
518 1.1 christos
519 1.1 christos
520 1.1 christos 9. Appendix: Sample code
521 1.1 christos
522 1.1 christos The following C code computes the Adler-32 checksum of a data buffer.
523 1.1 christos It is written for clarity, not for speed. The sample code is in the
524 1.1 christos ANSI C programming language. Non C users may find it easier to read
525 1.1 christos with these hints:
526 1.1 christos
527 1.1 christos & Bitwise AND operator.
528 1.1 christos >> Bitwise right shift operator. When applied to an
529 1.1 christos unsigned quantity, as here, right shift inserts zero bit(s)
530 1.1 christos at the left.
531 1.1 christos << Bitwise left shift operator. Left shift inserts zero
532 1.1 christos bit(s) at the right.
533 1.1 christos ++ "n++" increments the variable n.
534 1.1 christos % modulo operator: a % b is the remainder of a divided by b.
535 1.1 christos
536 1.1 christos #define BASE 65521 /* largest prime smaller than 65536 */
537 1.1 christos
538 1.1 christos /*
539 1.1 christos Update a running Adler-32 checksum with the bytes buf[0..len-1]
540 1.1 christos and return the updated checksum. The Adler-32 checksum should be
541 1.1 christos initialized to 1.
542 1.1 christos
543 1.1 christos Usage example:
544 1.1 christos
545 1.1 christos unsigned long adler = 1L;
546 1.1 christos
547 1.1 christos while (read_buffer(buffer, length) != EOF) {
548 1.1 christos adler = update_adler32(adler, buffer, length);
549 1.1 christos }
550 1.1 christos if (adler != original_adler) error();
551 1.1 christos */
552 1.1 christos unsigned long update_adler32(unsigned long adler,
553 1.1 christos unsigned char *buf, int len)
554 1.1 christos {
555 1.1 christos unsigned long s1 = adler & 0xffff;
556 1.1 christos unsigned long s2 = (adler >> 16) & 0xffff;
557 1.1 christos int n;
558 1.1 christos
559 1.1 christos for (n = 0; n < len; n++) {
560 1.1 christos s1 = (s1 + buf[n]) % BASE;
561 1.1 christos s2 = (s2 + s1) % BASE;
562 1.1 christos }
563 1.1 christos return (s2 << 16) + s1;
564 1.1 christos }
565 1.1 christos
566 1.1 christos /* Return the adler32 of the bytes buf[0..len-1] */
567 1.1 christos
568 1.1 christos
569 1.1 christos
570 1.1 christos
571 1.1 christos Deutsch & Gailly Informational [Page 10]
572 1.1 christos
574 1.1 christos RFC 1950 ZLIB Compressed Data Format Specification May 1996
575 1.1 christos
576 1.1 christos
577 1.1 christos unsigned long adler32(unsigned char *buf, int len)
578 1.1 christos {
579 1.1 christos return update_adler32(1L, buf, len);
580 1.1 christos }
581 1.1 christos
582 1.1 christos
583 1.1 christos
584 1.1 christos
585 1.1 christos
586 1.1 christos
587 1.1 christos
588 1.1 christos
589 1.1 christos
590 1.1 christos
591 1.1 christos
592 1.1 christos
593 1.1 christos
594 1.1 christos
595 1.1 christos
596 1.1 christos
597 1.1 christos
598 1.1 christos
599 1.1 christos
600 1.1 christos
601 1.1 christos
602 1.1 christos
603 1.1 christos
604 1.1 christos
605 1.1 christos
606 1.1 christos
607 1.1 christos
608 1.1 christos
609 1.1 christos
610 1.1 christos
611 1.1 christos
612 1.1 christos
613 1.1 christos
614 1.1 christos
615 1.1 christos
616 1.1 christos
617 1.1 christos
618 1.1 christos
619 1.1 christos
620
621
622
623
624
625
626
627
628 Deutsch & Gailly Informational [Page 11]
629
631