annotate src/zlib-1.2.8/doc/rfc1950.txt @ 83:ae30d91d2ffe

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