Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

955 lines
36 KiB

  1. Network Working Group P. Deutsch
  2. Request for Comments: 1951 Aladdin Enterprises
  3. Category: Informational May 1996
  4. DEFLATE Compressed Data Format Specification version 1.3
  5. Status of This Memo
  6. This memo provides information for the Internet community. This memo
  7. does not specify an Internet standard of any kind. Distribution of
  8. this memo is unlimited.
  9. IESG Note:
  10. The IESG takes no position on the validity of any Intellectual
  11. Property Rights statements contained in this document.
  12. Notices
  13. Copyright (c) 1996 L. Peter Deutsch
  14. Permission is granted to copy and distribute this document for any
  15. purpose and without charge, including translations into other
  16. languages and incorporation into compilations, provided that the
  17. copyright notice and this notice are preserved, and that any
  18. substantive changes or deletions from the original are clearly
  19. marked.
  20. A pointer to the latest version of this and related documentation in
  21. HTML format can be found at the URL
  22. <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>.
  23. Abstract
  24. This specification defines a lossless compressed data format that
  25. compresses data using a combination of the LZ77 algorithm and Huffman
  26. coding, with efficiency comparable to the best currently available
  27. general-purpose compression methods. The data can be produced or
  28. consumed, even for an arbitrarily long sequentially presented input
  29. data stream, using only an a priori bounded amount of intermediate
  30. storage. The format can be implemented readily in a manner not
  31. covered by patents.
  32. Deutsch Informational [Page 1]
  33. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  34. Table of Contents
  35. 1. Introduction ................................................... 2
  36. 1.1. Purpose ................................................... 2
  37. 1.2. Intended audience ......................................... 3
  38. 1.3. Scope ..................................................... 3
  39. 1.4. Compliance ................................................ 3
  40. 1.5. Definitions of terms and conventions used ................ 3
  41. 1.6. Changes from previous versions ............................ 4
  42. 2. Compressed representation overview ............................. 4
  43. 3. Detailed specification ......................................... 5
  44. 3.1. Overall conventions ....................................... 5
  45. 3.1.1. Packing into bytes .................................. 5
  46. 3.2. Compressed block format ................................... 6
  47. 3.2.1. Synopsis of prefix and Huffman coding ............... 6
  48. 3.2.2. Use of Huffman coding in the "deflate" format ....... 7
  49. 3.2.3. Details of block format ............................. 9
  50. 3.2.4. Non-compressed blocks (BTYPE=00) ................... 11
  51. 3.2.5. Compressed blocks (length and distance codes) ...... 11
  52. 3.2.6. Compression with fixed Huffman codes (BTYPE=01) .... 12
  53. 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) .. 13
  54. 3.3. Compliance ............................................... 14
  55. 4. Compression algorithm details ................................. 14
  56. 5. References .................................................... 16
  57. 6. Security Considerations ....................................... 16
  58. 7. Source code ................................................... 16
  59. 8. Acknowledgements .............................................. 16
  60. 9. Author's Address .............................................. 17
  61. 1. Introduction
  62. 1.1. Purpose
  63. The purpose of this specification is to define a lossless
  64. compressed data format that:
  65. * Is independent of CPU type, operating system, file system,
  66. and character set, and hence can be used for interchange;
  67. * Can be produced or consumed, even for an arbitrarily long
  68. sequentially presented input data stream, using only an a
  69. priori bounded amount of intermediate storage, and hence
  70. can be used in data communications or similar structures
  71. such as Unix filters;
  72. * Compresses data with efficiency comparable to the best
  73. currently available general-purpose compression methods,
  74. and in particular considerably better than the "compress"
  75. program;
  76. * Can be implemented readily in a manner not covered by
  77. patents, and hence can be practiced freely;
  78. Deutsch Informational [Page 2]
  79. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  80. * Is compatible with the file format produced by the current
  81. widely used gzip utility, in that conforming decompressors
  82. will be able to read data produced by the existing gzip
  83. compressor.
  84. The data format defined by this specification does not attempt to:
  85. * Allow random access to compressed data;
  86. * Compress specialized data (e.g., raster graphics) as well
  87. as the best currently available specialized algorithms.
  88. A simple counting argument shows that no lossless compression
  89. algorithm can compress every possible input data set. For the
  90. format defined here, the worst case expansion is 5 bytes per 32K-
  91. byte block, i.e., a size increase of 0.015% for large data sets.
  92. English text usually compresses by a factor of 2.5 to 3;
  93. executable files usually compress somewhat less; graphical data
  94. such as raster images may compress much more.
  95. 1.2. Intended audience
  96. This specification is intended for use by implementors of software
  97. to compress data into "deflate" format and/or decompress data from
  98. "deflate" format.
  99. The text of the specification assumes a basic background in
  100. programming at the level of bits and other primitive data
  101. representations. Familiarity with the technique of Huffman coding
  102. is helpful but not required.
  103. 1.3. Scope
  104. The specification specifies a method for representing a sequence
  105. of bytes as a (usually shorter) sequence of bits, and a method for
  106. packing the latter bit sequence into bytes.
  107. 1.4. Compliance
  108. Unless otherwise indicated below, a compliant decompressor must be
  109. able to accept and decompress any data set that conforms to all
  110. the specifications presented here; a compliant compressor must
  111. produce data sets that conform to all the specifications presented
  112. here.
  113. 1.5. Definitions of terms and conventions used
  114. Byte: 8 bits stored or transmitted as a unit (same as an octet).
  115. For this specification, a byte is exactly 8 bits, even on machines
  116. Deutsch Informational [Page 3]
  117. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  118. which store a character on a number of bits different from eight.
  119. See below, for the numbering of bits within a byte.
  120. String: a sequence of arbitrary bytes.
  121. 1.6. Changes from previous versions
  122. There have been no technical changes to the deflate format since
  123. version 1.1 of this specification. In version 1.2, some
  124. terminology was changed. Version 1.3 is a conversion of the
  125. specification to RFC style.
  126. 2. Compressed representation overview
  127. A compressed data set consists of a series of blocks, corresponding
  128. to successive blocks of input data. The block sizes are arbitrary,
  129. except that non-compressible blocks are limited to 65,535 bytes.
  130. Each block is compressed using a combination of the LZ77 algorithm
  131. and Huffman coding. The Huffman trees for each block are independent
  132. of those for previous or subsequent blocks; the LZ77 algorithm may
  133. use a reference to a duplicated string occurring in a previous block,
  134. up to 32K input bytes before.
  135. Each block consists of two parts: a pair of Huffman code trees that
  136. describe the representation of the compressed data part, and a
  137. compressed data part. (The Huffman trees themselves are compressed
  138. using Huffman encoding.) The compressed data consists of a series of
  139. elements of two types: literal bytes (of strings that have not been
  140. detected as duplicated within the previous 32K input bytes), and
  141. pointers to duplicated strings, where a pointer is represented as a
  142. pair <length, backward distance>. The representation used in the
  143. "deflate" format limits distances to 32K bytes and lengths to 258
  144. bytes, but does not limit the size of a block, except for
  145. uncompressible blocks, which are limited as noted above.
  146. Each type of value (literals, distances, and lengths) in the
  147. compressed data is represented using a Huffman code, using one code
  148. tree for literals and lengths and a separate code tree for distances.
  149. The code trees for each block appear in a compact form just before
  150. the compressed data for that block.
  151. Deutsch Informational [Page 4]
  152. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  153. 3. Detailed specification
  154. 3.1. Overall conventions In the diagrams below, a box like this:
  155. +---+
  156. | | <-- the vertical bars might be missing
  157. +---+
  158. represents one byte; a box like this:
  159. +==============+
  160. | |
  161. +==============+
  162. represents a variable number of bytes.
  163. Bytes stored within a computer do not have a "bit order", since
  164. they are always treated as a unit. However, a byte considered as
  165. an integer between 0 and 255 does have a most- and least-
  166. significant bit, and since we write numbers with the most-
  167. significant digit on the left, we also write bytes with the most-
  168. significant bit on the left. In the diagrams below, we number the
  169. bits of a byte so that bit 0 is the least-significant bit, i.e.,
  170. the bits are numbered:
  171. +--------+
  172. |76543210|
  173. +--------+
  174. Within a computer, a number may occupy multiple bytes. All
  175. multi-byte numbers in the format described here are stored with
  176. the least-significant byte first (at the lower memory address).
  177. For example, the decimal number 520 is stored as:
  178. 0 1
  179. +--------+--------+
  180. |00001000|00000010|
  181. +--------+--------+
  182. ^ ^
  183. | |
  184. | + more significant byte = 2 x 256
  185. + less significant byte = 8
  186. 3.1.1. Packing into bytes
  187. This document does not address the issue of the order in which
  188. bits of a byte are transmitted on a bit-sequential medium,
  189. since the final data format described here is byte- rather than
  190. Deutsch Informational [Page 5]
  191. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  192. bit-oriented. However, we describe the compressed block format
  193. in below, as a sequence of data elements of various bit
  194. lengths, not a sequence of bytes. We must therefore specify
  195. how to pack these data elements into bytes to form the final
  196. compressed byte sequence:
  197. * Data elements are packed into bytes in order of
  198. increasing bit number within the byte, i.e., starting
  199. with the least-significant bit of the byte.
  200. * Data elements other than Huffman codes are packed
  201. starting with the least-significant bit of the data
  202. element.
  203. * Huffman codes are packed starting with the most-
  204. significant bit of the code.
  205. In other words, if one were to print out the compressed data as
  206. a sequence of bytes, starting with the first byte at the
  207. *right* margin and proceeding to the *left*, with the most-
  208. significant bit of each byte on the left as usual, one would be
  209. able to parse the result from right to left, with fixed-width
  210. elements in the correct MSB-to-LSB order and Huffman codes in
  211. bit-reversed order (i.e., with the first bit of the code in the
  212. relative LSB position).
  213. 3.2. Compressed block format
  214. 3.2.1. Synopsis of prefix and Huffman coding
  215. Prefix coding represents symbols from an a priori known
  216. alphabet by bit sequences (codes), one code for each symbol, in
  217. a manner such that different symbols may be represented by bit
  218. sequences of different lengths, but a parser can always parse
  219. an encoded string unambiguously symbol-by-symbol.
  220. We define a prefix code in terms of a binary tree in which the
  221. two edges descending from each non-leaf node are labeled 0 and
  222. 1 and in which the leaf nodes correspond one-for-one with (are
  223. labeled with) the symbols of the alphabet; then the code for a
  224. symbol is the sequence of 0's and 1's on the edges leading from
  225. the root to the leaf labeled with that symbol. For example:
  226. Deutsch Informational [Page 6]
  227. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  228. /\ Symbol Code
  229. 0 1 ------ ----
  230. / \ A 00
  231. /\ B B 1
  232. 0 1 C 011
  233. / \ D 010
  234. A /\
  235. 0 1
  236. / \
  237. D C
  238. A parser can decode the next symbol from an encoded input
  239. stream by walking down the tree from the root, at each step
  240. choosing the edge corresponding to the next input bit.
  241. Given an alphabet with known symbol frequencies, the Huffman
  242. algorithm allows the construction of an optimal prefix code
  243. (one which represents strings with those symbol frequencies
  244. using the fewest bits of any possible prefix codes for that
  245. alphabet). Such a code is called a Huffman code. (See
  246. reference [1] in Chapter 5, references for additional
  247. information on Huffman codes.)
  248. Note that in the "deflate" format, the Huffman codes for the
  249. various alphabets must not exceed certain maximum code lengths.
  250. This constraint complicates the algorithm for computing code
  251. lengths from symbol frequencies. Again, see Chapter 5,
  252. references for details.
  253. 3.2.2. Use of Huffman coding in the "deflate" format
  254. The Huffman codes used for each alphabet in the "deflate"
  255. format have two additional rules:
  256. * All codes of a given bit length have lexicographically
  257. consecutive values, in the same order as the symbols
  258. they represent;
  259. * Shorter codes lexicographically precede longer codes.
  260. Deutsch Informational [Page 7]
  261. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  262. We could recode the example above to follow this rule as
  263. follows, assuming that the order of the alphabet is ABCD:
  264. Symbol Code
  265. ------ ----
  266. A 10
  267. B 0
  268. C 110
  269. D 111
  270. I.e., 0 precedes 10 which precedes 11x, and 110 and 111 are
  271. lexicographically consecutive.
  272. Given this rule, we can define the Huffman code for an alphabet
  273. just by giving the bit lengths of the codes for each symbol of
  274. the alphabet in order; this is sufficient to determine the
  275. actual codes. In our example, the code is completely defined
  276. by the sequence of bit lengths (2, 1, 3, 3). The following
  277. algorithm generates the codes as integers, intended to be read
  278. from most- to least-significant bit. The code lengths are
  279. initially in tree[I].Len; the codes are produced in
  280. tree[I].Code.
  281. 1) Count the number of codes for each code length. Let
  282. bl_count[N] be the number of codes of length N, N >= 1.
  283. 2) Find the numerical value of the smallest code for each
  284. code length:
  285. code = 0;
  286. bl_count[0] = 0;
  287. for (bits = 1; bits <= MAX_BITS; bits++) {
  288. code = (code + bl_count[bits-1]) << 1;
  289. next_code[bits] = code;
  290. }
  291. 3) Assign numerical values to all codes, using consecutive
  292. values for all codes of the same length with the base
  293. values determined at step 2. Codes that are never used
  294. (which have a bit length of zero) must not be assigned a
  295. value.
  296. for (n = 0; n <= max_code; n++) {
  297. len = tree[n].Len;
  298. if (len != 0) {
  299. tree[n].Code = next_code[len];
  300. next_code[len]++;
  301. }
  302. Deutsch Informational [Page 8]
  303. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  304. }
  305. Example:
  306. Consider the alphabet ABCDEFGH, with bit lengths (3, 3, 3, 3,
  307. 3, 2, 4, 4). After step 1, we have:
  308. N bl_count[N]
  309. - -----------
  310. 2 1
  311. 3 5
  312. 4 2
  313. Step 2 computes the following next_code values:
  314. N next_code[N]
  315. - ------------
  316. 1 0
  317. 2 0
  318. 3 2
  319. 4 14
  320. Step 3 produces the following code values:
  321. Symbol Length Code
  322. ------ ------ ----
  323. A 3 010
  324. B 3 011
  325. C 3 100
  326. D 3 101
  327. E 3 110
  328. F 2 00
  329. G 4 1110
  330. H 4 1111
  331. 3.2.3. Details of block format
  332. Each block of compressed data begins with 3 header bits
  333. containing the following data:
  334. first bit BFINAL
  335. next 2 bits BTYPE
  336. Note that the header bits do not necessarily begin on a byte
  337. boundary, since a block does not necessarily occupy an integral
  338. number of bytes.
  339. Deutsch Informational [Page 9]
  340. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  341. BFINAL is set if and only if this is the last block of the data
  342. set.
  343. BTYPE specifies how the data are compressed, as follows:
  344. 00 - no compression
  345. 01 - compressed with fixed Huffman codes
  346. 10 - compressed with dynamic Huffman codes
  347. 11 - reserved (error)
  348. The only difference between the two compressed cases is how the
  349. Huffman codes for the literal/length and distance alphabets are
  350. defined.
  351. In all cases, the decoding algorithm for the actual data is as
  352. follows:
  353. do
  354. read block header from input stream.
  355. if stored with no compression
  356. skip any remaining bits in current partially
  357. processed byte
  358. read LEN and NLEN (see next section)
  359. copy LEN bytes of data to output
  360. otherwise
  361. if compressed with dynamic Huffman codes
  362. read representation of code trees (see
  363. subsection below)
  364. loop (until end of block code recognized)
  365. decode literal/length value from input stream
  366. if value < 256
  367. copy value (literal byte) to output stream
  368. otherwise
  369. if value = end of block (256)
  370. break from loop
  371. otherwise (value = 257..285)
  372. decode distance from input stream
  373. move backwards distance bytes in the output
  374. stream, and copy length bytes from this
  375. position to the output stream.
  376. end loop
  377. while not last block
  378. Note that a duplicated string reference may refer to a string
  379. in a previous block; i.e., the backward distance may cross one
  380. or more block boundaries. However a distance cannot refer past
  381. the beginning of the output stream. (An application using a
  382. Deutsch Informational [Page 10]
  383. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  384. preset dictionary might discard part of the output stream; a
  385. distance can refer to that part of the output stream anyway)
  386. Note also that the referenced string may overlap the current
  387. position; for example, if the last 2 bytes decoded have values
  388. X and Y, a string reference with <length = 5, distance = 2>
  389. adds X,Y,X,Y,X to the output stream.
  390. We now specify each compression method in turn.
  391. 3.2.4. Non-compressed blocks (BTYPE=00)
  392. Any bits of input up to the next byte boundary are ignored.
  393. The rest of the block consists of the following information:
  394. 0 1 2 3 4...
  395. +---+---+---+---+================================+
  396. | LEN | NLEN |... LEN bytes of literal data...|
  397. +---+---+---+---+================================+
  398. LEN is the number of data bytes in the block. NLEN is the
  399. one's complement of LEN.
  400. 3.2.5. Compressed blocks (length and distance codes)
  401. As noted above, encoded data blocks in the "deflate" format
  402. consist of sequences of symbols drawn from three conceptually
  403. distinct alphabets: either literal bytes, from the alphabet of
  404. byte values (0..255), or <length, backward distance> pairs,
  405. where the length is drawn from (3..258) and the distance is
  406. drawn from (1..32,768). In fact, the literal and length
  407. alphabets are merged into a single alphabet (0..285), where
  408. values 0..255 represent literal bytes, the value 256 indicates
  409. end-of-block, and values 257..285 represent length codes
  410. (possibly in conjunction with extra bits following the symbol
  411. code) as follows:
  412. Deutsch Informational [Page 11]
  413. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  414. Extra Extra Extra
  415. Code Bits Length(s) Code Bits Lengths Code Bits Length(s)
  416. ---- ---- ------ ---- ---- ------- ---- ---- -------
  417. 257 0 3 267 1 15,16 277 4 67-82
  418. 258 0 4 268 1 17,18 278 4 83-98
  419. 259 0 5 269 2 19-22 279 4 99-114
  420. 260 0 6 270 2 23-26 280 4 115-130
  421. 261 0 7 271 2 27-30 281 5 131-162
  422. 262 0 8 272 2 31-34 282 5 163-194
  423. 263 0 9 273 3 35-42 283 5 195-226
  424. 264 0 10 274 3 43-50 284 5 227-257
  425. 265 1 11,12 275 3 51-58 285 0 258
  426. 266 1 13,14 276 3 59-66
  427. The extra bits should be interpreted as a machine integer
  428. stored with the most-significant bit first, e.g., bits 1110
  429. represent the value 14.
  430. Extra Extra Extra
  431. Code Bits Dist Code Bits Dist Code Bits Distance
  432. ---- ---- ---- ---- ---- ------ ---- ---- --------
  433. 0 0 1 10 4 33-48 20 9 1025-1536
  434. 1 0 2 11 4 49-64 21 9 1537-2048
  435. 2 0 3 12 5 65-96 22 10 2049-3072
  436. 3 0 4 13 5 97-128 23 10 3073-4096
  437. 4 1 5,6 14 6 129-192 24 11 4097-6144
  438. 5 1 7,8 15 6 193-256 25 11 6145-8192
  439. 6 2 9-12 16 7 257-384 26 12 8193-12288
  440. 7 2 13-16 17 7 385-512 27 12 12289-16384
  441. 8 3 17-24 18 8 513-768 28 13 16385-24576
  442. 9 3 25-32 19 8 769-1024 29 13 24577-32768
  443. 3.2.6. Compression with fixed Huffman codes (BTYPE=01)
  444. The Huffman codes for the two alphabets are fixed, and are not
  445. represented explicitly in the data. The Huffman code lengths
  446. for the literal/length alphabet are:
  447. Lit Value Bits Codes
  448. --------- ---- -----
  449. 0 - 143 8 00110000 through
  450. 10111111
  451. 144 - 255 9 110010000 through
  452. 111111111
  453. 256 - 279 7 0000000 through
  454. 0010111
  455. 280 - 287 8 11000000 through
  456. 11000111
  457. Deutsch Informational [Page 12]
  458. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  459. The code lengths are sufficient to generate the actual codes,
  460. as described above; we show the codes in the table for added
  461. clarity. Literal/length values 286-287 will never actually
  462. occur in the compressed data, but participate in the code
  463. construction.
  464. Distance codes 0-31 are represented by (fixed-length) 5-bit
  465. codes, with possible additional bits as shown in the table
  466. shown in Paragraph 3.2.5, above. Note that distance codes 30-
  467. 31 will never actually occur in the compressed data.
  468. 3.2.7. Compression with dynamic Huffman codes (BTYPE=10)
  469. The Huffman codes for the two alphabets appear in the block
  470. immediately after the header bits and before the actual
  471. compressed data, first the literal/length code and then the
  472. distance code. Each code is defined by a sequence of code
  473. lengths, as discussed in Paragraph 3.2.2, above. For even
  474. greater compactness, the code length sequences themselves are
  475. compressed using a Huffman code. The alphabet for code lengths
  476. is as follows:
  477. 0 - 15: Represent code lengths of 0 - 15
  478. 16: Copy the previous code length 3 - 6 times.
  479. The next 2 bits indicate repeat length
  480. (0 = 3, ... , 3 = 6)
  481. Example: Codes 8, 16 (+2 bits 11),
  482. 16 (+2 bits 10) will expand to
  483. 12 code lengths of 8 (1 + 6 + 5)
  484. 17: Repeat a code length of 0 for 3 - 10 times.
  485. (3 bits of length)
  486. 18: Repeat a code length of 0 for 11 - 138 times
  487. (7 bits of length)
  488. A code length of 0 indicates that the corresponding symbol in
  489. the literal/length or distance alphabet will not occur in the
  490. block, and should not participate in the Huffman code
  491. construction algorithm given earlier. If only one distance
  492. code is used, it is encoded using one bit, not zero bits; in
  493. this case there is a single code length of one, with one unused
  494. code. One distance code of zero bits means that there are no
  495. distance codes used at all (the data is all literals).
  496. We can now define the format of the block:
  497. 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286)
  498. 5 Bits: HDIST, # of Distance codes - 1 (1 - 32)
  499. 4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19)
  500. Deutsch Informational [Page 13]
  501. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  502. (HCLEN + 4) x 3 bits: code lengths for the code length
  503. alphabet given just above, in the order: 16, 17, 18,
  504. 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  505. These code lengths are interpreted as 3-bit integers
  506. (0-7); as above, a code length of 0 means the
  507. corresponding symbol (literal/length or distance code
  508. length) is not used.
  509. HLIT + 257 code lengths for the literal/length alphabet,
  510. encoded using the code length Huffman code
  511. HDIST + 1 code lengths for the distance alphabet,
  512. encoded using the code length Huffman code
  513. The actual compressed data of the block,
  514. encoded using the literal/length and distance Huffman
  515. codes
  516. The literal/length symbol 256 (end of data),
  517. encoded using the literal/length Huffman code
  518. The code length repeat codes can cross from HLIT + 257 to the
  519. HDIST + 1 code lengths. In other words, all code lengths form
  520. a single sequence of HLIT + HDIST + 258 values.
  521. 3.3. Compliance
  522. A compressor may limit further the ranges of values specified in
  523. the previous section and still be compliant; for example, it may
  524. limit the range of backward pointers to some value smaller than
  525. 32K. Similarly, a compressor may limit the size of blocks so that
  526. a compressible block fits in memory.
  527. A compliant decompressor must accept the full range of possible
  528. values defined in the previous section, and must accept blocks of
  529. arbitrary size.
  530. 4. Compression algorithm details
  531. While it is the intent of this document to define the "deflate"
  532. compressed data format without reference to any particular
  533. compression algorithm, the format is related to the compressed
  534. formats produced by LZ77 (Lempel-Ziv 1977, see reference [2] below);
  535. since many variations of LZ77 are patented, it is strongly
  536. recommended that the implementor of a compressor follow the general
  537. algorithm presented here, which is known not to be patented per se.
  538. The material in this section is not part of the definition of the
  539. Deutsch Informational [Page 14]
  540. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  541. specification per se, and a compressor need not follow it in order to
  542. be compliant.
  543. The compressor terminates a block when it determines that starting a
  544. new block with fresh trees would be useful, or when the block size
  545. fills up the compressor's block buffer.
  546. The compressor uses a chained hash table to find duplicated strings,
  547. using a hash function that operates on 3-byte sequences. At any
  548. given point during compression, let XYZ be the next 3 input bytes to
  549. be examined (not necessarily all different, of course). First, the
  550. compressor examines the hash chain for XYZ. If the chain is empty,
  551. the compressor simply writes out X as a literal byte and advances one
  552. byte in the input. If the hash chain is not empty, indicating that
  553. the sequence XYZ (or, if we are unlucky, some other 3 bytes with the
  554. same hash function value) has occurred recently, the compressor
  555. compares all strings on the XYZ hash chain with the actual input data
  556. sequence starting at the current point, and selects the longest
  557. match.
  558. The compressor searches the hash chains starting with the most recent
  559. strings, to favor small distances and thus take advantage of the
  560. Huffman encoding. The hash chains are singly linked. There are no
  561. deletions from the hash chains; the algorithm simply discards matches
  562. that are too old. To avoid a worst-case situation, very long hash
  563. chains are arbitrarily truncated at a certain length, determined by a
  564. run-time parameter.
  565. To improve overall compression, the compressor optionally defers the
  566. selection of matches ("lazy matching"): after a match of length N has
  567. been found, the compressor searches for a longer match starting at
  568. the next input byte. If it finds a longer match, it truncates the
  569. previous match to a length of one (thus producing a single literal
  570. byte) and then emits the longer match. Otherwise, it emits the
  571. original match, and, as described above, advances N bytes before
  572. continuing.
  573. Run-time parameters also control this "lazy match" procedure. If
  574. compression ratio is most important, the compressor attempts a
  575. complete second search regardless of the length of the first match.
  576. In the normal case, if the current match is "long enough", the
  577. compressor reduces the search for a longer match, thus speeding up
  578. the process. If speed is most important, the compressor inserts new
  579. strings in the hash table only when no match was found, or when the
  580. match is not "too long". This degrades the compression ratio but
  581. saves time since there are both fewer insertions and fewer searches.
  582. Deutsch Informational [Page 15]
  583. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  584. 5. References
  585. [1] Huffman, D. A., "A Method for the Construction of Minimum
  586. Redundancy Codes", Proceedings of the Institute of Radio
  587. Engineers, September 1952, Volume 40, Number 9, pp. 1098-1101.
  588. [2] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data
  589. Compression", IEEE Transactions on Information Theory, Vol. 23,
  590. No. 3, pp. 337-343.
  591. [3] Gailly, J.-L., and Adler, M., ZLIB documentation and sources,
  592. available in ftp://ftp.uu.net/pub/archiving/zip/doc/
  593. [4] Gailly, J.-L., and Adler, M., GZIP documentation and sources,
  594. available as gzip-*.tar in ftp://prep.ai.mit.edu/pub/gnu/
  595. [5] Schwartz, E. S., and Kallick, B. "Generating a canonical prefix
  596. encoding." Comm. ACM, 7,3 (Mar. 1964), pp. 166-169.
  597. [6] Hirschberg and Lelewer, "Efficient decoding of prefix codes,"
  598. Comm. ACM, 33,4, April 1990, pp. 449-459.
  599. 6. Security Considerations
  600. Any data compression method involves the reduction of redundancy in
  601. the data. Consequently, any corruption of the data is likely to have
  602. severe effects and be difficult to correct. Uncompressed text, on
  603. the other hand, will probably still be readable despite the presence
  604. of some corrupted bytes.
  605. It is recommended that systems using this data format provide some
  606. means of validating the integrity of the compressed data. See
  607. reference [3], for example.
  608. 7. Source code
  609. Source code for a C language implementation of a "deflate" compliant
  610. compressor and decompressor is available within the zlib package at
  611. ftp://ftp.uu.net/pub/archiving/zip/zlib/.
  612. 8. Acknowledgements
  613. Trademarks cited in this document are the property of their
  614. respective owners.
  615. Phil Katz designed the deflate format. Jean-Loup Gailly and Mark
  616. Adler wrote the related software described in this specification.
  617. Glenn Randers-Pehrson converted this document to RFC and HTML format.
  618. Deutsch Informational [Page 16]
  619. RFC 1951 DEFLATE Compressed Data Format Specification May 1996
  620. 9. Author's Address
  621. L. Peter Deutsch
  622. Aladdin Enterprises
  623. 203 Santa Margarita Ave.
  624. Menlo Park, CA 94025
  625. Phone: (415) 322-0103 (AM only)
  626. FAX: (415) 322-1734
  627. EMail: <[email protected]>
  628. Questions about the technical content of this specification can be
  629. sent by email to:
  630. Jean-Loup Gailly <[email protected]> and
  631. Mark Adler <[email protected]>
  632. Editorial comments on this specification can be sent by email to:
  633. L. Peter Deutsch <[email protected]> and
  634. Glenn Randers-Pehrson <[email protected]>
  635. Deutsch Informational [Page 17]