rfc1950.txt 20 KB

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