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