| author | |
| committer | |
| log | d1f61f2d6801240c593bcf5a3219020ecae0e736 |
| tree | 15875ab5fae54f0be59401f30897b1228bcfe93e |
| parent | aca9c74e80e106309b9783ff251ab0cdd3fb9626 |
| parent | 8f98a2b90fe0834af34a943886c67855ba6c4f7b |
| signature | Signed by PGP key 4AEE18F83AFDEB23 |
Update gitattributes and re-enable std lib compression test on Windows18 files changed, 1644 insertions(+), 1644 deletions(-)
.gitattributes+2-2| ... | @@ -1,9 +1,9 @@ | ... | @@ -1,9 +1,9 @@ |
| 1 | *.zig text eol=lf | 1 | *.zig text eol=lf |
| 2 | *.txt text eol=lf | 2 | *.txt text eol=lf |
| 3 | langref.html.in text eol=lf | 3 | langref.html.in text eol=lf |
| 4 | deps/SoftFloat-3e/*.txt text eol=crlf | 4 | lib/std/compress/testdata/** binary |
| 5 | lib/std/compress/deflate/testdata/** binary | ||
| 5 | 6 | ||
| 6 | deps/** linguist-vendored | ||
| 7 | lib/include/** linguist-vendored | 7 | lib/include/** linguist-vendored |
| 8 | lib/libc/** linguist-vendored | 8 | lib/libc/** linguist-vendored |
| 9 | lib/libcxx/** linguist-vendored | 9 | lib/libcxx/** linguist-vendored |
build.zig+1-1| ... | @@ -106,7 +106,7 @@ pub fn build(b: *Builder) !void { | ... | @@ -106,7 +106,7 @@ pub fn build(b: *Builder) !void { |
| 106 | .install_dir = .lib, | 106 | .install_dir = .lib, |
| 107 | .install_subdir = "zig", | 107 | .install_subdir = "zig", |
| 108 | .exclude_extensions = &[_][]const u8{ | 108 | .exclude_extensions = &[_][]const u8{ |
| 109 | // exclude files from lib/std/compress/ | 109 | // exclude files from lib/std/compress/testdata |
| 110 | ".gz", | 110 | ".gz", |
| 111 | ".z.0", | 111 | ".z.0", |
| 112 | ".z.9", | 112 | ".z.9", |
ci/aarch64-windows.ps1+5| ... | @@ -31,6 +31,11 @@ if ((git rev-parse --is-shallow-repository) -eq "true") { | ... | @@ -31,6 +31,11 @@ if ((git rev-parse --is-shallow-repository) -eq "true") { |
| 31 | git fetch --unshallow # `git describe` won't work on a shallow repo | 31 | git fetch --unshallow # `git describe` won't work on a shallow repo |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | # Fix files that have their gitattributes changed | ||
| 35 | git rm -rf lib | ||
| 36 | git restore --staged lib | ||
| 37 | git checkout lib | ||
| 38 | |||
| 34 | Write-Output "Building from source..." | 39 | Write-Output "Building from source..." |
| 35 | Remove-Item -Path 'build-release' -Recurse -Force -ErrorAction Ignore | 40 | Remove-Item -Path 'build-release' -Recurse -Force -ErrorAction Ignore |
| 36 | New-Item -Path 'build-release' -ItemType Directory | 41 | New-Item -Path 'build-release' -ItemType Directory |
lib/std/compress/deflate/huffman_bit_writer.zig-5| ... | @@ -848,11 +848,6 @@ test "writeBlockHuff" { | ... | @@ -848,11 +848,6 @@ test "writeBlockHuff" { |
| 848 | // Tests huffman encoding against reference files to detect possible regressions. | 848 | // Tests huffman encoding against reference files to detect possible regressions. |
| 849 | // If encoding/bit allocation changes you can regenerate these files | 849 | // If encoding/bit allocation changes you can regenerate these files |
| 850 | 850 | ||
| 851 | if (builtin.os.tag == .windows) { | ||
| 852 | // https://github.com/ziglang/zig/issues/13892 | ||
| 853 | return error.SkipZigTest; | ||
| 854 | } | ||
| 855 | |||
| 856 | try testBlockHuff( | 851 | try testBlockHuff( |
| 857 | "huffman-null-max.input", | 852 | "huffman-null-max.input", |
| 858 | "huffman-null-max.golden", | 853 | "huffman-null-max.golden", |
lib/std/compress/gzip.zig+2-2| ... | @@ -172,8 +172,8 @@ fn testReader(data: []const u8, comptime expected: []const u8) !void { | ... | @@ -172,8 +172,8 @@ fn testReader(data: []const u8, comptime expected: []const u8) !void { |
| 172 | // SHA256=164ef0897b4cbec63abf1b57f069f3599bd0fb7c72c2a4dee21bd7e03ec9af67 | 172 | // SHA256=164ef0897b4cbec63abf1b57f069f3599bd0fb7c72c2a4dee21bd7e03ec9af67 |
| 173 | test "compressed data" { | 173 | test "compressed data" { |
| 174 | try testReader( | 174 | try testReader( |
| 175 | @embedFile("rfc1952.txt.gz"), | 175 | @embedFile("testdata/rfc1952.txt.gz"), |
| 176 | @embedFile("rfc1952.txt"), | 176 | @embedFile("testdata/rfc1952.txt"), |
| 177 | ); | 177 | ); |
| 178 | } | 178 | } |
| 179 | 179 |
lib/std/compress/rfc1951.txt deleted-955| ... | @@ -1,955 +0,0 @@ | ||
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | Network Working Group P. Deutsch | ||
| 8 | Request for Comments: 1951 Aladdin Enterprises | ||
| 9 | Category: Informational May 1996 | ||
| 10 | |||
| 11 | |||
| 12 | DEFLATE Compressed Data Format Specification version 1.3 | ||
| 13 | |||
| 14 | Status of This Memo | ||
| 15 | |||
| 16 | This memo provides information for the Internet community. This memo | ||
| 17 | does not specify an Internet standard of any kind. Distribution of | ||
| 18 | this memo is unlimited. | ||
| 19 | |||
| 20 | IESG Note: | ||
| 21 | |||
| 22 | The IESG takes no position on the validity of any Intellectual | ||
| 23 | Property Rights statements contained in this document. | ||
| 24 | |||
| 25 | Notices | ||
| 26 | |||
| 27 | Copyright (c) 1996 L. Peter Deutsch | ||
| 28 | |||
| 29 | Permission is granted to copy and distribute this document for any | ||
| 30 | purpose and without charge, including translations into other | ||
| 31 | languages and incorporation into compilations, provided that the | ||
| 32 | copyright notice and this notice are preserved, and that any | ||
| 33 | substantive changes or deletions from the original are clearly | ||
| 34 | marked. | ||
| 35 | |||
| 36 | A pointer to the latest version of this and related documentation in | ||
| 37 | HTML format can be found at the URL | ||
| 38 | <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>. | ||
| 39 | |||
| 40 | Abstract | ||
| 41 | |||
| 42 | This specification defines a lossless compressed data format that | ||
| 43 | compresses data using a combination of the LZ77 algorithm and Huffman | ||
| 44 | coding, with efficiency comparable to the best currently available | ||
| 45 | general-purpose compression methods. The data can be produced or | ||
| 46 | consumed, even for an arbitrarily long sequentially presented input | ||
| 47 | data stream, using only an a priori bounded amount of intermediate | ||
| 48 | storage. The format can be implemented readily in a manner not | ||
| 49 | covered by patents. | ||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | Deutsch Informational [Page 1] | ||
| 59 | |||
| 60 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 61 | |||
| 62 | |||
| 63 | Table of Contents | ||
| 64 | |||
| 65 | 1. Introduction ................................................... 2 | ||
| 66 | 1.1. Purpose ................................................... 2 | ||
| 67 | 1.2. Intended audience ......................................... 3 | ||
| 68 | 1.3. Scope ..................................................... 3 | ||
| 69 | 1.4. Compliance ................................................ 3 | ||
| 70 | 1.5. Definitions of terms and conventions used ................ 3 | ||
| 71 | 1.6. Changes from previous versions ............................ 4 | ||
| 72 | 2. Compressed representation overview ............................. 4 | ||
| 73 | 3. Detailed specification ......................................... 5 | ||
| 74 | 3.1. Overall conventions ....................................... 5 | ||
| 75 | 3.1.1. Packing into bytes .................................. 5 | ||
| 76 | 3.2. Compressed block format ................................... 6 | ||
| 77 | 3.2.1. Synopsis of prefix and Huffman coding ............... 6 | ||
| 78 | 3.2.2. Use of Huffman coding in the "deflate" format ....... 7 | ||
| 79 | 3.2.3. Details of block format ............................. 9 | ||
| 80 | 3.2.4. Non-compressed blocks (BTYPE=00) ................... 11 | ||
| 81 | 3.2.5. Compressed blocks (length and distance codes) ...... 11 | ||
| 82 | 3.2.6. Compression with fixed Huffman codes (BTYPE=01) .... 12 | ||
| 83 | 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) .. 13 | ||
| 84 | 3.3. Compliance ............................................... 14 | ||
| 85 | 4. Compression algorithm details ................................. 14 | ||
| 86 | 5. References .................................................... 16 | ||
| 87 | 6. Security Considerations ....................................... 16 | ||
| 88 | 7. Source code ................................................... 16 | ||
| 89 | 8. Acknowledgements .............................................. 16 | ||
| 90 | 9. Author's Address .............................................. 17 | ||
| 91 | |||
| 92 | 1. Introduction | ||
| 93 | |||
| 94 | 1.1. Purpose | ||
| 95 | |||
| 96 | The purpose of this specification is to define a lossless | ||
| 97 | compressed data format that: | ||
| 98 | * Is independent of CPU type, operating system, file system, | ||
| 99 | and character set, and hence can be used for interchange; | ||
| 100 | * Can be produced or consumed, even for an arbitrarily long | ||
| 101 | sequentially presented input data stream, using only an a | ||
| 102 | priori bounded amount of intermediate storage, and hence | ||
| 103 | can be used in data communications or similar structures | ||
| 104 | such as Unix filters; | ||
| 105 | * Compresses data with efficiency comparable to the best | ||
| 106 | currently available general-purpose compression methods, | ||
| 107 | and in particular considerably better than the "compress" | ||
| 108 | program; | ||
| 109 | * Can be implemented readily in a manner not covered by | ||
| 110 | patents, and hence can be practiced freely; | ||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | Deutsch Informational [Page 2] | ||
| 115 | |||
| 116 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 117 | |||
| 118 | |||
| 119 | * Is compatible with the file format produced by the current | ||
| 120 | widely used gzip utility, in that conforming decompressors | ||
| 121 | will be able to read data produced by the existing gzip | ||
| 122 | compressor. | ||
| 123 | |||
| 124 | The data format defined by this specification does not attempt to: | ||
| 125 | |||
| 126 | * Allow random access to compressed data; | ||
| 127 | * Compress specialized data (e.g., raster graphics) as well | ||
| 128 | as the best currently available specialized algorithms. | ||
| 129 | |||
| 130 | A simple counting argument shows that no lossless compression | ||
| 131 | algorithm can compress every possible input data set. For the | ||
| 132 | format defined here, the worst case expansion is 5 bytes per 32K- | ||
| 133 | byte block, i.e., a size increase of 0.015% for large data sets. | ||
| 134 | English text usually compresses by a factor of 2.5 to 3; | ||
| 135 | executable files usually compress somewhat less; graphical data | ||
| 136 | such as raster images may compress much more. | ||
| 137 | |||
| 138 | 1.2. Intended audience | ||
| 139 | |||
| 140 | This specification is intended for use by implementors of software | ||
| 141 | to compress data into "deflate" format and/or decompress data from | ||
| 142 | "deflate" format. | ||
| 143 | |||
| 144 | The text of the specification assumes a basic background in | ||
| 145 | programming at the level of bits and other primitive data | ||
| 146 | representations. Familiarity with the technique of Huffman coding | ||
| 147 | is helpful but not required. | ||
| 148 | |||
| 149 | 1.3. Scope | ||
| 150 | |||
| 151 | The specification specifies a method for representing a sequence | ||
| 152 | of bytes as a (usually shorter) sequence of bits, and a method for | ||
| 153 | packing the latter bit sequence into bytes. | ||
| 154 | |||
| 155 | 1.4. Compliance | ||
| 156 | |||
| 157 | Unless otherwise indicated below, a compliant decompressor must be | ||
| 158 | able to accept and decompress any data set that conforms to all | ||
| 159 | the specifications presented here; a compliant compressor must | ||
| 160 | produce data sets that conform to all the specifications presented | ||
| 161 | here. | ||
| 162 | |||
| 163 | 1.5. Definitions of terms and conventions used | ||
| 164 | |||
| 165 | Byte: 8 bits stored or transmitted as a unit (same as an octet). | ||
| 166 | For this specification, a byte is exactly 8 bits, even on machines | ||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | Deutsch Informational [Page 3] | ||
| 171 | |||
| 172 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 173 | |||
| 174 | |||
| 175 | which store a character on a number of bits different from eight. | ||
| 176 | See below, for the numbering of bits within a byte. | ||
| 177 | |||
| 178 | String: a sequence of arbitrary bytes. | ||
| 179 | |||
| 180 | 1.6. Changes from previous versions | ||
| 181 | |||
| 182 | There have been no technical changes to the deflate format since | ||
| 183 | version 1.1 of this specification. In version 1.2, some | ||
| 184 | terminology was changed. Version 1.3 is a conversion of the | ||
| 185 | specification to RFC style. | ||
| 186 | |||
| 187 | 2. Compressed representation overview | ||
| 188 | |||
| 189 | A compressed data set consists of a series of blocks, corresponding | ||
| 190 | to successive blocks of input data. The block sizes are arbitrary, | ||
| 191 | except that non-compressible blocks are limited to 65,535 bytes. | ||
| 192 | |||
| 193 | Each block is compressed using a combination of the LZ77 algorithm | ||
| 194 | and Huffman coding. The Huffman trees for each block are independent | ||
| 195 | of those for previous or subsequent blocks; the LZ77 algorithm may | ||
| 196 | use a reference to a duplicated string occurring in a previous block, | ||
| 197 | up to 32K input bytes before. | ||
| 198 | |||
| 199 | Each block consists of two parts: a pair of Huffman code trees that | ||
| 200 | describe the representation of the compressed data part, and a | ||
| 201 | compressed data part. (The Huffman trees themselves are compressed | ||
| 202 | using Huffman encoding.) The compressed data consists of a series of | ||
| 203 | elements of two types: literal bytes (of strings that have not been | ||
| 204 | detected as duplicated within the previous 32K input bytes), and | ||
| 205 | pointers to duplicated strings, where a pointer is represented as a | ||
| 206 | pair <length, backward distance>. The representation used in the | ||
| 207 | "deflate" format limits distances to 32K bytes and lengths to 258 | ||
| 208 | bytes, but does not limit the size of a block, except for | ||
| 209 | uncompressible blocks, which are limited as noted above. | ||
| 210 | |||
| 211 | Each type of value (literals, distances, and lengths) in the | ||
| 212 | compressed data is represented using a Huffman code, using one code | ||
| 213 | tree for literals and lengths and a separate code tree for distances. | ||
| 214 | The code trees for each block appear in a compact form just before | ||
| 215 | the compressed data for that block. | ||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | Deutsch Informational [Page 4] | ||
| 227 | |||
| 228 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 229 | |||
| 230 | |||
| 231 | 3. Detailed specification | ||
| 232 | |||
| 233 | 3.1. Overall conventions In the diagrams below, a box like this: | ||
| 234 | |||
| 235 | +---+ | ||
| 236 | | | <-- the vertical bars might be missing | ||
| 237 | +---+ | ||
| 238 | |||
| 239 | represents one byte; a box like this: | ||
| 240 | |||
| 241 | +==============+ | ||
| 242 | | | | ||
| 243 | +==============+ | ||
| 244 | |||
| 245 | represents a variable number of bytes. | ||
| 246 | |||
| 247 | Bytes stored within a computer do not have a "bit order", since | ||
| 248 | they are always treated as a unit. However, a byte considered as | ||
| 249 | an integer between 0 and 255 does have a most- and least- | ||
| 250 | significant bit, and since we write numbers with the most- | ||
| 251 | significant digit on the left, we also write bytes with the most- | ||
| 252 | significant bit on the left. In the diagrams below, we number the | ||
| 253 | bits of a byte so that bit 0 is the least-significant bit, i.e., | ||
| 254 | the bits are numbered: | ||
| 255 | |||
| 256 | +--------+ | ||
| 257 | |76543210| | ||
| 258 | +--------+ | ||
| 259 | |||
| 260 | Within a computer, a number may occupy multiple bytes. All | ||
| 261 | multi-byte numbers in the format described here are stored with | ||
| 262 | the least-significant byte first (at the lower memory address). | ||
| 263 | For example, the decimal number 520 is stored as: | ||
| 264 | |||
| 265 | 0 1 | ||
| 266 | +--------+--------+ | ||
| 267 | |00001000|00000010| | ||
| 268 | +--------+--------+ | ||
| 269 | ^ ^ | ||
| 270 | | | | ||
| 271 | | + more significant byte = 2 x 256 | ||
| 272 | + less significant byte = 8 | ||
| 273 | |||
| 274 | 3.1.1. Packing into bytes | ||
| 275 | |||
| 276 | This document does not address the issue of the order in which | ||
| 277 | bits of a byte are transmitted on a bit-sequential medium, | ||
| 278 | since the final data format described here is byte- rather than | ||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | Deutsch Informational [Page 5] | ||
| 283 | |||
| 284 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 285 | |||
| 286 | |||
| 287 | bit-oriented. However, we describe the compressed block format | ||
| 288 | in below, as a sequence of data elements of various bit | ||
| 289 | lengths, not a sequence of bytes. We must therefore specify | ||
| 290 | how to pack these data elements into bytes to form the final | ||
| 291 | compressed byte sequence: | ||
| 292 | |||
| 293 | * Data elements are packed into bytes in order of | ||
| 294 | increasing bit number within the byte, i.e., starting | ||
| 295 | with the least-significant bit of the byte. | ||
| 296 | * Data elements other than Huffman codes are packed | ||
| 297 | starting with the least-significant bit of the data | ||
| 298 | element. | ||
| 299 | * Huffman codes are packed starting with the most- | ||
| 300 | significant bit of the code. | ||
| 301 | |||
| 302 | In other words, if one were to print out the compressed data as | ||
| 303 | a sequence of bytes, starting with the first byte at the | ||
| 304 | *right* margin and proceeding to the *left*, with the most- | ||
| 305 | significant bit of each byte on the left as usual, one would be | ||
| 306 | able to parse the result from right to left, with fixed-width | ||
| 307 | elements in the correct MSB-to-LSB order and Huffman codes in | ||
| 308 | bit-reversed order (i.e., with the first bit of the code in the | ||
| 309 | relative LSB position). | ||
| 310 | |||
| 311 | 3.2. Compressed block format | ||
| 312 | |||
| 313 | 3.2.1. Synopsis of prefix and Huffman coding | ||
| 314 | |||
| 315 | Prefix coding represents symbols from an a priori known | ||
| 316 | alphabet by bit sequences (codes), one code for each symbol, in | ||
| 317 | a manner such that different symbols may be represented by bit | ||
| 318 | sequences of different lengths, but a parser can always parse | ||
| 319 | an encoded string unambiguously symbol-by-symbol. | ||
| 320 | |||
| 321 | We define a prefix code in terms of a binary tree in which the | ||
| 322 | two edges descending from each non-leaf node are labeled 0 and | ||
| 323 | 1 and in which the leaf nodes correspond one-for-one with (are | ||
| 324 | labeled with) the symbols of the alphabet; then the code for a | ||
| 325 | symbol is the sequence of 0's and 1's on the edges leading from | ||
| 326 | the root to the leaf labeled with that symbol. For example: | ||
| 327 | |||
| 328 | |||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | Deutsch Informational [Page 6] | ||
| 339 | |||
| 340 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 341 | |||
| 342 | |||
| 343 | /\ Symbol Code | ||
| 344 | 0 1 ------ ---- | ||
| 345 | / \ A 00 | ||
| 346 | /\ B B 1 | ||
| 347 | 0 1 C 011 | ||
| 348 | / \ D 010 | ||
| 349 | A /\ | ||
| 350 | 0 1 | ||
| 351 | / \ | ||
| 352 | D C | ||
| 353 | |||
| 354 | A parser can decode the next symbol from an encoded input | ||
| 355 | stream by walking down the tree from the root, at each step | ||
| 356 | choosing the edge corresponding to the next input bit. | ||
| 357 | |||
| 358 | Given an alphabet with known symbol frequencies, the Huffman | ||
| 359 | algorithm allows the construction of an optimal prefix code | ||
| 360 | (one which represents strings with those symbol frequencies | ||
| 361 | using the fewest bits of any possible prefix codes for that | ||
| 362 | alphabet). Such a code is called a Huffman code. (See | ||
| 363 | reference [1] in Chapter 5, references for additional | ||
| 364 | information on Huffman codes.) | ||
| 365 | |||
| 366 | Note that in the "deflate" format, the Huffman codes for the | ||
| 367 | various alphabets must not exceed certain maximum code lengths. | ||
| 368 | This constraint complicates the algorithm for computing code | ||
| 369 | lengths from symbol frequencies. Again, see Chapter 5, | ||
| 370 | references for details. | ||
| 371 | |||
| 372 | 3.2.2. Use of Huffman coding in the "deflate" format | ||
| 373 | |||
| 374 | The Huffman codes used for each alphabet in the "deflate" | ||
| 375 | format have two additional rules: | ||
| 376 | |||
| 377 | * All codes of a given bit length have lexicographically | ||
| 378 | consecutive values, in the same order as the symbols | ||
| 379 | they represent; | ||
| 380 | |||
| 381 | * Shorter codes lexicographically precede longer codes. | ||
| 382 | |||
| 383 | |||
| 384 | |||
| 385 | |||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | |||
| 394 | Deutsch Informational [Page 7] | ||
| 395 | |||
| 396 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 397 | |||
| 398 | |||
| 399 | We could recode the example above to follow this rule as | ||
| 400 | follows, assuming that the order of the alphabet is ABCD: | ||
| 401 | |||
| 402 | Symbol Code | ||
| 403 | ------ ---- | ||
| 404 | A 10 | ||
| 405 | B 0 | ||
| 406 | C 110 | ||
| 407 | D 111 | ||
| 408 | |||
| 409 | I.e., 0 precedes 10 which precedes 11x, and 110 and 111 are | ||
| 410 | lexicographically consecutive. | ||
| 411 | |||
| 412 | Given this rule, we can define the Huffman code for an alphabet | ||
| 413 | just by giving the bit lengths of the codes for each symbol of | ||
| 414 | the alphabet in order; this is sufficient to determine the | ||
| 415 | actual codes. In our example, the code is completely defined | ||
| 416 | by the sequence of bit lengths (2, 1, 3, 3). The following | ||
| 417 | algorithm generates the codes as integers, intended to be read | ||
| 418 | from most- to least-significant bit. The code lengths are | ||
| 419 | initially in tree[I].Len; the codes are produced in | ||
| 420 | tree[I].Code. | ||
| 421 | |||
| 422 | 1) Count the number of codes for each code length. Let | ||
| 423 | bl_count[N] be the number of codes of length N, N >= 1. | ||
| 424 | |||
| 425 | 2) Find the numerical value of the smallest code for each | ||
| 426 | code length: | ||
| 427 | |||
| 428 | code = 0; | ||
| 429 | bl_count[0] = 0; | ||
| 430 | for (bits = 1; bits <= MAX_BITS; bits++) { | ||
| 431 | code = (code + bl_count[bits-1]) << 1; | ||
| 432 | next_code[bits] = code; | ||
| 433 | } | ||
| 434 | |||
| 435 | 3) Assign numerical values to all codes, using consecutive | ||
| 436 | values for all codes of the same length with the base | ||
| 437 | values determined at step 2. Codes that are never used | ||
| 438 | (which have a bit length of zero) must not be assigned a | ||
| 439 | value. | ||
| 440 | |||
| 441 | for (n = 0; n <= max_code; n++) { | ||
| 442 | len = tree[n].Len; | ||
| 443 | if (len != 0) { | ||
| 444 | tree[n].Code = next_code[len]; | ||
| 445 | next_code[len]++; | ||
| 446 | } | ||
| 447 | |||
| 448 | |||
| 449 | |||
| 450 | Deutsch Informational [Page 8] | ||
| 451 | |||
| 452 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 453 | |||
| 454 | |||
| 455 | } | ||
| 456 | |||
| 457 | Example: | ||
| 458 | |||
| 459 | Consider the alphabet ABCDEFGH, with bit lengths (3, 3, 3, 3, | ||
| 460 | 3, 2, 4, 4). After step 1, we have: | ||
| 461 | |||
| 462 | N bl_count[N] | ||
| 463 | - ----------- | ||
| 464 | 2 1 | ||
| 465 | 3 5 | ||
| 466 | 4 2 | ||
| 467 | |||
| 468 | Step 2 computes the following next_code values: | ||
| 469 | |||
| 470 | N next_code[N] | ||
| 471 | - ------------ | ||
| 472 | 1 0 | ||
| 473 | 2 0 | ||
| 474 | 3 2 | ||
| 475 | 4 14 | ||
| 476 | |||
| 477 | Step 3 produces the following code values: | ||
| 478 | |||
| 479 | Symbol Length Code | ||
| 480 | ------ ------ ---- | ||
| 481 | A 3 010 | ||
| 482 | B 3 011 | ||
| 483 | C 3 100 | ||
| 484 | D 3 101 | ||
| 485 | E 3 110 | ||
| 486 | F 2 00 | ||
| 487 | G 4 1110 | ||
| 488 | H 4 1111 | ||
| 489 | |||
| 490 | 3.2.3. Details of block format | ||
| 491 | |||
| 492 | Each block of compressed data begins with 3 header bits | ||
| 493 | containing the following data: | ||
| 494 | |||
| 495 | first bit BFINAL | ||
| 496 | next 2 bits BTYPE | ||
| 497 | |||
| 498 | Note that the header bits do not necessarily begin on a byte | ||
| 499 | boundary, since a block does not necessarily occupy an integral | ||
| 500 | number of bytes. | ||
| 501 | |||
| 502 | |||
| 503 | |||
| 504 | |||
| 505 | |||
| 506 | Deutsch Informational [Page 9] | ||
| 507 | |||
| 508 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 509 | |||
| 510 | |||
| 511 | BFINAL is set if and only if this is the last block of the data | ||
| 512 | set. | ||
| 513 | |||
| 514 | BTYPE specifies how the data are compressed, as follows: | ||
| 515 | |||
| 516 | 00 - no compression | ||
| 517 | 01 - compressed with fixed Huffman codes | ||
| 518 | 10 - compressed with dynamic Huffman codes | ||
| 519 | 11 - reserved (error) | ||
| 520 | |||
| 521 | The only difference between the two compressed cases is how the | ||
| 522 | Huffman codes for the literal/length and distance alphabets are | ||
| 523 | defined. | ||
| 524 | |||
| 525 | In all cases, the decoding algorithm for the actual data is as | ||
| 526 | follows: | ||
| 527 | |||
| 528 | do | ||
| 529 | read block header from input stream. | ||
| 530 | if stored with no compression | ||
| 531 | skip any remaining bits in current partially | ||
| 532 | processed byte | ||
| 533 | read LEN and NLEN (see next section) | ||
| 534 | copy LEN bytes of data to output | ||
| 535 | otherwise | ||
| 536 | if compressed with dynamic Huffman codes | ||
| 537 | read representation of code trees (see | ||
| 538 | subsection below) | ||
| 539 | loop (until end of block code recognized) | ||
| 540 | decode literal/length value from input stream | ||
| 541 | if value < 256 | ||
| 542 | copy value (literal byte) to output stream | ||
| 543 | otherwise | ||
| 544 | if value = end of block (256) | ||
| 545 | break from loop | ||
| 546 | otherwise (value = 257..285) | ||
| 547 | decode distance from input stream | ||
| 548 | |||
| 549 | move backwards distance bytes in the output | ||
| 550 | stream, and copy length bytes from this | ||
| 551 | position to the output stream. | ||
| 552 | end loop | ||
| 553 | while not last block | ||
| 554 | |||
| 555 | Note that a duplicated string reference may refer to a string | ||
| 556 | in a previous block; i.e., the backward distance may cross one | ||
| 557 | or more block boundaries. However a distance cannot refer past | ||
| 558 | the beginning of the output stream. (An application using a | ||
| 559 | |||
| 560 | |||
| 561 | |||
| 562 | Deutsch Informational [Page 10] | ||
| 563 | |||
| 564 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 565 | |||
| 566 | |||
| 567 | preset dictionary might discard part of the output stream; a | ||
| 568 | distance can refer to that part of the output stream anyway) | ||
| 569 | Note also that the referenced string may overlap the current | ||
| 570 | position; for example, if the last 2 bytes decoded have values | ||
| 571 | X and Y, a string reference with <length = 5, distance = 2> | ||
| 572 | adds X,Y,X,Y,X to the output stream. | ||
| 573 | |||
| 574 | We now specify each compression method in turn. | ||
| 575 | |||
| 576 | 3.2.4. Non-compressed blocks (BTYPE=00) | ||
| 577 | |||
| 578 | Any bits of input up to the next byte boundary are ignored. | ||
| 579 | The rest of the block consists of the following information: | ||
| 580 | |||
| 581 | 0 1 2 3 4... | ||
| 582 | +---+---+---+---+================================+ | ||
| 583 | | LEN | NLEN |... LEN bytes of literal data...| | ||
| 584 | +---+---+---+---+================================+ | ||
| 585 | |||
| 586 | LEN is the number of data bytes in the block. NLEN is the | ||
| 587 | one's complement of LEN. | ||
| 588 | |||
| 589 | 3.2.5. Compressed blocks (length and distance codes) | ||
| 590 | |||
| 591 | As noted above, encoded data blocks in the "deflate" format | ||
| 592 | consist of sequences of symbols drawn from three conceptually | ||
| 593 | distinct alphabets: either literal bytes, from the alphabet of | ||
| 594 | byte values (0..255), or <length, backward distance> pairs, | ||
| 595 | where the length is drawn from (3..258) and the distance is | ||
| 596 | drawn from (1..32,768). In fact, the literal and length | ||
| 597 | alphabets are merged into a single alphabet (0..285), where | ||
| 598 | values 0..255 represent literal bytes, the value 256 indicates | ||
| 599 | end-of-block, and values 257..285 represent length codes | ||
| 600 | (possibly in conjunction with extra bits following the symbol | ||
| 601 | code) as follows: | ||
| 602 | |||
| 603 | |||
| 604 | |||
| 605 | |||
| 606 | |||
| 607 | |||
| 608 | |||
| 609 | |||
| 610 | |||
| 611 | |||
| 612 | |||
| 613 | |||
| 614 | |||
| 615 | |||
| 616 | |||
| 617 | |||
| 618 | Deutsch Informational [Page 11] | ||
| 619 | |||
| 620 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 621 | |||
| 622 | |||
| 623 | Extra Extra Extra | ||
| 624 | Code Bits Length(s) Code Bits Lengths Code Bits Length(s) | ||
| 625 | ---- ---- ------ ---- ---- ------- ---- ---- ------- | ||
| 626 | 257 0 3 267 1 15,16 277 4 67-82 | ||
| 627 | 258 0 4 268 1 17,18 278 4 83-98 | ||
| 628 | 259 0 5 269 2 19-22 279 4 99-114 | ||
| 629 | 260 0 6 270 2 23-26 280 4 115-130 | ||
| 630 | 261 0 7 271 2 27-30 281 5 131-162 | ||
| 631 | 262 0 8 272 2 31-34 282 5 163-194 | ||
| 632 | 263 0 9 273 3 35-42 283 5 195-226 | ||
| 633 | 264 0 10 274 3 43-50 284 5 227-257 | ||
| 634 | 265 1 11,12 275 3 51-58 285 0 258 | ||
| 635 | 266 1 13,14 276 3 59-66 | ||
| 636 | |||
| 637 | The extra bits should be interpreted as a machine integer | ||
| 638 | stored with the most-significant bit first, e.g., bits 1110 | ||
| 639 | represent the value 14. | ||
| 640 | |||
| 641 | Extra Extra Extra | ||
| 642 | Code Bits Dist Code Bits Dist Code Bits Distance | ||
| 643 | ---- ---- ---- ---- ---- ------ ---- ---- -------- | ||
| 644 | 0 0 1 10 4 33-48 20 9 1025-1536 | ||
| 645 | 1 0 2 11 4 49-64 21 9 1537-2048 | ||
| 646 | 2 0 3 12 5 65-96 22 10 2049-3072 | ||
| 647 | 3 0 4 13 5 97-128 23 10 3073-4096 | ||
| 648 | 4 1 5,6 14 6 129-192 24 11 4097-6144 | ||
| 649 | 5 1 7,8 15 6 193-256 25 11 6145-8192 | ||
| 650 | 6 2 9-12 16 7 257-384 26 12 8193-12288 | ||
| 651 | 7 2 13-16 17 7 385-512 27 12 12289-16384 | ||
| 652 | 8 3 17-24 18 8 513-768 28 13 16385-24576 | ||
| 653 | 9 3 25-32 19 8 769-1024 29 13 24577-32768 | ||
| 654 | |||
| 655 | 3.2.6. Compression with fixed Huffman codes (BTYPE=01) | ||
| 656 | |||
| 657 | The Huffman codes for the two alphabets are fixed, and are not | ||
| 658 | represented explicitly in the data. The Huffman code lengths | ||
| 659 | for the literal/length alphabet are: | ||
| 660 | |||
| 661 | Lit Value Bits Codes | ||
| 662 | --------- ---- ----- | ||
| 663 | 0 - 143 8 00110000 through | ||
| 664 | 10111111 | ||
| 665 | 144 - 255 9 110010000 through | ||
| 666 | 111111111 | ||
| 667 | 256 - 279 7 0000000 through | ||
| 668 | 0010111 | ||
| 669 | 280 - 287 8 11000000 through | ||
| 670 | 11000111 | ||
| 671 | |||
| 672 | |||
| 673 | |||
| 674 | Deutsch Informational [Page 12] | ||
| 675 | |||
| 676 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 677 | |||
| 678 | |||
| 679 | The code lengths are sufficient to generate the actual codes, | ||
| 680 | as described above; we show the codes in the table for added | ||
| 681 | clarity. Literal/length values 286-287 will never actually | ||
| 682 | occur in the compressed data, but participate in the code | ||
| 683 | construction. | ||
| 684 | |||
| 685 | Distance codes 0-31 are represented by (fixed-length) 5-bit | ||
| 686 | codes, with possible additional bits as shown in the table | ||
| 687 | shown in Paragraph 3.2.5, above. Note that distance codes 30- | ||
| 688 | 31 will never actually occur in the compressed data. | ||
| 689 | |||
| 690 | 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) | ||
| 691 | |||
| 692 | The Huffman codes for the two alphabets appear in the block | ||
| 693 | immediately after the header bits and before the actual | ||
| 694 | compressed data, first the literal/length code and then the | ||
| 695 | distance code. Each code is defined by a sequence of code | ||
| 696 | lengths, as discussed in Paragraph 3.2.2, above. For even | ||
| 697 | greater compactness, the code length sequences themselves are | ||
| 698 | compressed using a Huffman code. The alphabet for code lengths | ||
| 699 | is as follows: | ||
| 700 | |||
| 701 | 0 - 15: Represent code lengths of 0 - 15 | ||
| 702 | 16: Copy the previous code length 3 - 6 times. | ||
| 703 | The next 2 bits indicate repeat length | ||
| 704 | (0 = 3, ... , 3 = 6) | ||
| 705 | Example: Codes 8, 16 (+2 bits 11), | ||
| 706 | 16 (+2 bits 10) will expand to | ||
| 707 | 12 code lengths of 8 (1 + 6 + 5) | ||
| 708 | 17: Repeat a code length of 0 for 3 - 10 times. | ||
| 709 | (3 bits of length) | ||
| 710 | 18: Repeat a code length of 0 for 11 - 138 times | ||
| 711 | (7 bits of length) | ||
| 712 | |||
| 713 | A code length of 0 indicates that the corresponding symbol in | ||
| 714 | the literal/length or distance alphabet will not occur in the | ||
| 715 | block, and should not participate in the Huffman code | ||
| 716 | construction algorithm given earlier. If only one distance | ||
| 717 | code is used, it is encoded using one bit, not zero bits; in | ||
| 718 | this case there is a single code length of one, with one unused | ||
| 719 | code. One distance code of zero bits means that there are no | ||
| 720 | distance codes used at all (the data is all literals). | ||
| 721 | |||
| 722 | We can now define the format of the block: | ||
| 723 | |||
| 724 | 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286) | ||
| 725 | 5 Bits: HDIST, # of Distance codes - 1 (1 - 32) | ||
| 726 | 4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19) | ||
| 727 | |||
| 728 | |||
| 729 | |||
| 730 | Deutsch Informational [Page 13] | ||
| 731 | |||
| 732 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 733 | |||
| 734 | |||
| 735 | (HCLEN + 4) x 3 bits: code lengths for the code length | ||
| 736 | alphabet given just above, in the order: 16, 17, 18, | ||
| 737 | 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 | ||
| 738 | |||
| 739 | These code lengths are interpreted as 3-bit integers | ||
| 740 | (0-7); as above, a code length of 0 means the | ||
| 741 | corresponding symbol (literal/length or distance code | ||
| 742 | length) is not used. | ||
| 743 | |||
| 744 | HLIT + 257 code lengths for the literal/length alphabet, | ||
| 745 | encoded using the code length Huffman code | ||
| 746 | |||
| 747 | HDIST + 1 code lengths for the distance alphabet, | ||
| 748 | encoded using the code length Huffman code | ||
| 749 | |||
| 750 | The actual compressed data of the block, | ||
| 751 | encoded using the literal/length and distance Huffman | ||
| 752 | codes | ||
| 753 | |||
| 754 | The literal/length symbol 256 (end of data), | ||
| 755 | encoded using the literal/length Huffman code | ||
| 756 | |||
| 757 | The code length repeat codes can cross from HLIT + 257 to the | ||
| 758 | HDIST + 1 code lengths. In other words, all code lengths form | ||
| 759 | a single sequence of HLIT + HDIST + 258 values. | ||
| 760 | |||
| 761 | 3.3. Compliance | ||
| 762 | |||
| 763 | A compressor may limit further the ranges of values specified in | ||
| 764 | the previous section and still be compliant; for example, it may | ||
| 765 | limit the range of backward pointers to some value smaller than | ||
| 766 | 32K. Similarly, a compressor may limit the size of blocks so that | ||
| 767 | a compressible block fits in memory. | ||
| 768 | |||
| 769 | A compliant decompressor must accept the full range of possible | ||
| 770 | values defined in the previous section, and must accept blocks of | ||
| 771 | arbitrary size. | ||
| 772 | |||
| 773 | 4. Compression algorithm details | ||
| 774 | |||
| 775 | While it is the intent of this document to define the "deflate" | ||
| 776 | compressed data format without reference to any particular | ||
| 777 | compression algorithm, the format is related to the compressed | ||
| 778 | formats produced by LZ77 (Lempel-Ziv 1977, see reference [2] below); | ||
| 779 | since many variations of LZ77 are patented, it is strongly | ||
| 780 | recommended that the implementor of a compressor follow the general | ||
| 781 | algorithm presented here, which is known not to be patented per se. | ||
| 782 | The material in this section is not part of the definition of the | ||
| 783 | |||
| 784 | |||
| 785 | |||
| 786 | Deutsch Informational [Page 14] | ||
| 787 | |||
| 788 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 789 | |||
| 790 | |||
| 791 | specification per se, and a compressor need not follow it in order to | ||
| 792 | be compliant. | ||
| 793 | |||
| 794 | The compressor terminates a block when it determines that starting a | ||
| 795 | new block with fresh trees would be useful, or when the block size | ||
| 796 | fills up the compressor's block buffer. | ||
| 797 | |||
| 798 | The compressor uses a chained hash table to find duplicated strings, | ||
| 799 | using a hash function that operates on 3-byte sequences. At any | ||
| 800 | given point during compression, let XYZ be the next 3 input bytes to | ||
| 801 | be examined (not necessarily all different, of course). First, the | ||
| 802 | compressor examines the hash chain for XYZ. If the chain is empty, | ||
| 803 | the compressor simply writes out X as a literal byte and advances one | ||
| 804 | byte in the input. If the hash chain is not empty, indicating that | ||
| 805 | the sequence XYZ (or, if we are unlucky, some other 3 bytes with the | ||
| 806 | same hash function value) has occurred recently, the compressor | ||
| 807 | compares all strings on the XYZ hash chain with the actual input data | ||
| 808 | sequence starting at the current point, and selects the longest | ||
| 809 | match. | ||
| 810 | |||
| 811 | The compressor searches the hash chains starting with the most recent | ||
| 812 | strings, to favor small distances and thus take advantage of the | ||
| 813 | Huffman encoding. The hash chains are singly linked. There are no | ||
| 814 | deletions from the hash chains; the algorithm simply discards matches | ||
| 815 | that are too old. To avoid a worst-case situation, very long hash | ||
| 816 | chains are arbitrarily truncated at a certain length, determined by a | ||
| 817 | run-time parameter. | ||
| 818 | |||
| 819 | To improve overall compression, the compressor optionally defers the | ||
| 820 | selection of matches ("lazy matching"): after a match of length N has | ||
| 821 | been found, the compressor searches for a longer match starting at | ||
| 822 | the next input byte. If it finds a longer match, it truncates the | ||
| 823 | previous match to a length of one (thus producing a single literal | ||
| 824 | byte) and then emits the longer match. Otherwise, it emits the | ||
| 825 | original match, and, as described above, advances N bytes before | ||
| 826 | continuing. | ||
| 827 | |||
| 828 | Run-time parameters also control this "lazy match" procedure. If | ||
| 829 | compression ratio is most important, the compressor attempts a | ||
| 830 | complete second search regardless of the length of the first match. | ||
| 831 | In the normal case, if the current match is "long enough", the | ||
| 832 | compressor reduces the search for a longer match, thus speeding up | ||
| 833 | the process. If speed is most important, the compressor inserts new | ||
| 834 | strings in the hash table only when no match was found, or when the | ||
| 835 | match is not "too long". This degrades the compression ratio but | ||
| 836 | saves time since there are both fewer insertions and fewer searches. | ||
| 837 | |||
| 838 | |||
| 839 | |||
| 840 | |||
| 841 | |||
| 842 | Deutsch Informational [Page 15] | ||
| 843 | |||
| 844 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 845 | |||
| 846 | |||
| 847 | 5. References | ||
| 848 | |||
| 849 | [1] Huffman, D. A., "A Method for the Construction of Minimum | ||
| 850 | Redundancy Codes", Proceedings of the Institute of Radio | ||
| 851 | Engineers, September 1952, Volume 40, Number 9, pp. 1098-1101. | ||
| 852 | |||
| 853 | [2] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data | ||
| 854 | Compression", IEEE Transactions on Information Theory, Vol. 23, | ||
| 855 | No. 3, pp. 337-343. | ||
| 856 | |||
| 857 | [3] Gailly, J.-L., and Adler, M., ZLIB documentation and sources, | ||
| 858 | available in ftp://ftp.uu.net/pub/archiving/zip/doc/ | ||
| 859 | |||
| 860 | [4] Gailly, J.-L., and Adler, M., GZIP documentation and sources, | ||
| 861 | available as gzip-*.tar in ftp://prep.ai.mit.edu/pub/gnu/ | ||
| 862 | |||
| 863 | [5] Schwartz, E. S., and Kallick, B. "Generating a canonical prefix | ||
| 864 | encoding." Comm. ACM, 7,3 (Mar. 1964), pp. 166-169. | ||
| 865 | |||
| 866 | [6] Hirschberg and Lelewer, "Efficient decoding of prefix codes," | ||
| 867 | Comm. ACM, 33,4, April 1990, pp. 449-459. | ||
| 868 | |||
| 869 | 6. Security Considerations | ||
| 870 | |||
| 871 | Any data compression method involves the reduction of redundancy in | ||
| 872 | the data. Consequently, any corruption of the data is likely to have | ||
| 873 | severe effects and be difficult to correct. Uncompressed text, on | ||
| 874 | the other hand, will probably still be readable despite the presence | ||
| 875 | of some corrupted bytes. | ||
| 876 | |||
| 877 | It is recommended that systems using this data format provide some | ||
| 878 | means of validating the integrity of the compressed data. See | ||
| 879 | reference [3], for example. | ||
| 880 | |||
| 881 | 7. Source code | ||
| 882 | |||
| 883 | Source code for a C language implementation of a "deflate" compliant | ||
| 884 | compressor and decompressor is available within the zlib package at | ||
| 885 | ftp://ftp.uu.net/pub/archiving/zip/zlib/. | ||
| 886 | |||
| 887 | 8. Acknowledgements | ||
| 888 | |||
| 889 | Trademarks cited in this document are the property of their | ||
| 890 | respective owners. | ||
| 891 | |||
| 892 | Phil Katz designed the deflate format. Jean-Loup Gailly and Mark | ||
| 893 | Adler wrote the related software described in this specification. | ||
| 894 | Glenn Randers-Pehrson converted this document to RFC and HTML format. | ||
| 895 | |||
| 896 | |||
| 897 | |||
| 898 | Deutsch Informational [Page 16] | ||
| 899 | |||
| 900 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 901 | |||
| 902 | |||
| 903 | 9. Author's Address | ||
| 904 | |||
| 905 | L. Peter Deutsch | ||
| 906 | Aladdin Enterprises | ||
| 907 | 203 Santa Margarita Ave. | ||
| 908 | Menlo Park, CA 94025 | ||
| 909 | |||
| 910 | Phone: (415) 322-0103 (AM only) | ||
| 911 | FAX: (415) 322-1734 | ||
| 912 | EMail: <ghost@aladdin.com> | ||
| 913 | |||
| 914 | Questions about the technical content of this specification can be | ||
| 915 | sent by email to: | ||
| 916 | |||
| 917 | Jean-Loup Gailly <gzip@prep.ai.mit.edu> and | ||
| 918 | Mark Adler <madler@alumni.caltech.edu> | ||
| 919 | |||
| 920 | Editorial comments on this specification can be sent by email to: | ||
| 921 | |||
| 922 | L. Peter Deutsch <ghost@aladdin.com> and | ||
| 923 | Glenn Randers-Pehrson <randeg@alumni.rpi.edu> | ||
| 924 | |||
| 925 | |||
| 926 | |||
| 927 | |||
| 928 | |||
| 929 | |||
| 930 | |||
| 931 | |||
| 932 | |||
| 933 | |||
| 934 | |||
| 935 | |||
| 936 | |||
| 937 | |||
| 938 | |||
| 939 | |||
| 940 | |||
| 941 | |||
| 942 | |||
| 943 | |||
| 944 | |||
| 945 | |||
| 946 | |||
| 947 | |||
| 948 | |||
| 949 | |||
| 950 | |||
| 951 | |||
| 952 | |||
| 953 | |||
| 954 | Deutsch Informational [Page 17] | ||
| 955 | |||
lib/std/compress/rfc1951.txt.fixed.z.9 deleted| Binary files a/lib/std/compress/rfc1951.txt.fixed.z.9 and /dev/null differ | |||
lib/std/compress/rfc1951.txt.z.0 deleted| Binary files a/lib/std/compress/rfc1951.txt.z.0 and /dev/null differ | |||
lib/std/compress/rfc1951.txt.z.9 deleted| Binary files a/lib/std/compress/rfc1951.txt.z.9 and /dev/null differ | |||
lib/std/compress/rfc1952.txt deleted-675| ... | @@ -1,675 +0,0 @@ | ||
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | Network Working Group P. Deutsch | ||
| 8 | Request for Comments: 1952 Aladdin Enterprises | ||
| 9 | Category: Informational May 1996 | ||
| 10 | |||
| 11 | |||
| 12 | GZIP file format specification version 4.3 | ||
| 13 | |||
| 14 | Status of This Memo | ||
| 15 | |||
| 16 | This memo provides information for the Internet community. This memo | ||
| 17 | does not specify an Internet standard of any kind. Distribution of | ||
| 18 | this memo is unlimited. | ||
| 19 | |||
| 20 | IESG Note: | ||
| 21 | |||
| 22 | The IESG takes no position on the validity of any Intellectual | ||
| 23 | Property Rights statements contained in this document. | ||
| 24 | |||
| 25 | Notices | ||
| 26 | |||
| 27 | Copyright (c) 1996 L. Peter Deutsch | ||
| 28 | |||
| 29 | Permission is granted to copy and distribute this document for any | ||
| 30 | purpose and without charge, including translations into other | ||
| 31 | languages and incorporation into compilations, provided that the | ||
| 32 | copyright notice and this notice are preserved, and that any | ||
| 33 | substantive changes or deletions from the original are clearly | ||
| 34 | marked. | ||
| 35 | |||
| 36 | A pointer to the latest version of this and related documentation in | ||
| 37 | HTML format can be found at the URL | ||
| 38 | <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>. | ||
| 39 | |||
| 40 | Abstract | ||
| 41 | |||
| 42 | This specification defines a lossless compressed data format that is | ||
| 43 | compatible with the widely used GZIP utility. The format includes a | ||
| 44 | cyclic redundancy check value for detecting data corruption. The | ||
| 45 | format presently uses the DEFLATE method of compression but can be | ||
| 46 | easily extended to use other compression methods. The format can be | ||
| 47 | implemented readily in a manner not covered by patents. | ||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | Deutsch Informational [Page 1] | ||
| 59 | |||
| 60 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 61 | |||
| 62 | |||
| 63 | Table of Contents | ||
| 64 | |||
| 65 | 1. Introduction ................................................... 2 | ||
| 66 | 1.1. Purpose ................................................... 2 | ||
| 67 | 1.2. Intended audience ......................................... 3 | ||
| 68 | 1.3. Scope ..................................................... 3 | ||
| 69 | 1.4. Compliance ................................................ 3 | ||
| 70 | 1.5. Definitions of terms and conventions used ................. 3 | ||
| 71 | 1.6. Changes from previous versions ............................ 3 | ||
| 72 | 2. Detailed specification ......................................... 4 | ||
| 73 | 2.1. Overall conventions ....................................... 4 | ||
| 74 | 2.2. File format ............................................... 5 | ||
| 75 | 2.3. Member format ............................................. 5 | ||
| 76 | 2.3.1. Member header and trailer ........................... 6 | ||
| 77 | 2.3.1.1. Extra field ................................... 8 | ||
| 78 | 2.3.1.2. Compliance .................................... 9 | ||
| 79 | 3. References .................................................. 9 | ||
| 80 | 4. Security Considerations .................................... 10 | ||
| 81 | 5. Acknowledgements ........................................... 10 | ||
| 82 | 6. Author's Address ........................................... 10 | ||
| 83 | 7. Appendix: Jean-Loup Gailly's gzip utility .................. 11 | ||
| 84 | 8. Appendix: Sample CRC Code .................................. 11 | ||
| 85 | |||
| 86 | 1. Introduction | ||
| 87 | |||
| 88 | 1.1. Purpose | ||
| 89 | |||
| 90 | The purpose of this specification is to define a lossless | ||
| 91 | compressed data format that: | ||
| 92 | |||
| 93 | * Is independent of CPU type, operating system, file system, | ||
| 94 | and character set, and hence can be used for interchange; | ||
| 95 | * Can compress or decompress a data stream (as opposed to a | ||
| 96 | randomly accessible file) to produce another data stream, | ||
| 97 | using only an a priori bounded amount of intermediate | ||
| 98 | storage, and hence can be used in data communications or | ||
| 99 | similar structures such as Unix filters; | ||
| 100 | * Compresses data with efficiency comparable to the best | ||
| 101 | currently available general-purpose compression methods, | ||
| 102 | and in particular considerably better than the "compress" | ||
| 103 | program; | ||
| 104 | * Can be implemented readily in a manner not covered by | ||
| 105 | patents, and hence can be practiced freely; | ||
| 106 | * Is compatible with the file format produced by the current | ||
| 107 | widely used gzip utility, in that conforming decompressors | ||
| 108 | will be able to read data produced by the existing gzip | ||
| 109 | compressor. | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | Deutsch Informational [Page 2] | ||
| 115 | |||
| 116 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 117 | |||
| 118 | |||
| 119 | The data format defined by this specification does not attempt to: | ||
| 120 | |||
| 121 | * Provide random access to compressed data; | ||
| 122 | * Compress specialized data (e.g., raster graphics) as well as | ||
| 123 | the best currently available specialized algorithms. | ||
| 124 | |||
| 125 | 1.2. Intended audience | ||
| 126 | |||
| 127 | This specification is intended for use by implementors of software | ||
| 128 | to compress data into gzip format and/or decompress data from gzip | ||
| 129 | format. | ||
| 130 | |||
| 131 | The text of the specification assumes a basic background in | ||
| 132 | programming at the level of bits and other primitive data | ||
| 133 | representations. | ||
| 134 | |||
| 135 | 1.3. Scope | ||
| 136 | |||
| 137 | The specification specifies a compression method and a file format | ||
| 138 | (the latter assuming only that a file can store a sequence of | ||
| 139 | arbitrary bytes). It does not specify any particular interface to | ||
| 140 | a file system or anything about character sets or encodings | ||
| 141 | (except for file names and comments, which are optional). | ||
| 142 | |||
| 143 | 1.4. Compliance | ||
| 144 | |||
| 145 | Unless otherwise indicated below, a compliant decompressor must be | ||
| 146 | able to accept and decompress any file that conforms to all the | ||
| 147 | specifications presented here; a compliant compressor must produce | ||
| 148 | files that conform to all the specifications presented here. The | ||
| 149 | material in the appendices is not part of the specification per se | ||
| 150 | and is not relevant to compliance. | ||
| 151 | |||
| 152 | 1.5. Definitions of terms and conventions used | ||
| 153 | |||
| 154 | byte: 8 bits stored or transmitted as a unit (same as an octet). | ||
| 155 | (For this specification, a byte is exactly 8 bits, even on | ||
| 156 | machines which store a character on a number of bits different | ||
| 157 | from 8.) See below for the numbering of bits within a byte. | ||
| 158 | |||
| 159 | 1.6. Changes from previous versions | ||
| 160 | |||
| 161 | There have been no technical changes to the gzip format since | ||
| 162 | version 4.1 of this specification. In version 4.2, some | ||
| 163 | terminology was changed, and the sample CRC code was rewritten for | ||
| 164 | clarity and to eliminate the requirement for the caller to do pre- | ||
| 165 | and post-conditioning. Version 4.3 is a conversion of the | ||
| 166 | specification to RFC style. | ||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | Deutsch Informational [Page 3] | ||
| 171 | |||
| 172 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 173 | |||
| 174 | |||
| 175 | 2. Detailed specification | ||
| 176 | |||
| 177 | 2.1. Overall conventions | ||
| 178 | |||
| 179 | In the diagrams below, a box like this: | ||
| 180 | |||
| 181 | +---+ | ||
| 182 | | | <-- the vertical bars might be missing | ||
| 183 | +---+ | ||
| 184 | |||
| 185 | represents one byte; a box like this: | ||
| 186 | |||
| 187 | +==============+ | ||
| 188 | | | | ||
| 189 | +==============+ | ||
| 190 | |||
| 191 | represents a variable number of bytes. | ||
| 192 | |||
| 193 | Bytes stored within a computer do not have a "bit order", since | ||
| 194 | they are always treated as a unit. However, a byte considered as | ||
| 195 | an integer between 0 and 255 does have a most- and least- | ||
| 196 | significant bit, and since we write numbers with the most- | ||
| 197 | significant digit on the left, we also write bytes with the most- | ||
| 198 | significant bit on the left. In the diagrams below, we number the | ||
| 199 | bits of a byte so that bit 0 is the least-significant bit, i.e., | ||
| 200 | the bits are numbered: | ||
| 201 | |||
| 202 | +--------+ | ||
| 203 | |76543210| | ||
| 204 | +--------+ | ||
| 205 | |||
| 206 | This document does not address the issue of the order in which | ||
| 207 | bits of a byte are transmitted on a bit-sequential medium, since | ||
| 208 | the data format described here is byte- rather than bit-oriented. | ||
| 209 | |||
| 210 | Within a computer, a number may occupy multiple bytes. All | ||
| 211 | multi-byte numbers in the format described here are stored with | ||
| 212 | the least-significant byte first (at the lower memory address). | ||
| 213 | For example, the decimal number 520 is stored as: | ||
| 214 | |||
| 215 | 0 1 | ||
| 216 | +--------+--------+ | ||
| 217 | |00001000|00000010| | ||
| 218 | +--------+--------+ | ||
| 219 | ^ ^ | ||
| 220 | | | | ||
| 221 | | + more significant byte = 2 x 256 | ||
| 222 | + less significant byte = 8 | ||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | Deutsch Informational [Page 4] | ||
| 227 | |||
| 228 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 229 | |||
| 230 | |||
| 231 | 2.2. File format | ||
| 232 | |||
| 233 | A gzip file consists of a series of "members" (compressed data | ||
| 234 | sets). The format of each member is specified in the following | ||
| 235 | section. The members simply appear one after another in the file, | ||
| 236 | with no additional information before, between, or after them. | ||
| 237 | |||
| 238 | 2.3. Member format | ||
| 239 | |||
| 240 | Each member has the following structure: | ||
| 241 | |||
| 242 | +---+---+---+---+---+---+---+---+---+---+ | ||
| 243 | |ID1|ID2|CM |FLG| MTIME |XFL|OS | (more-->) | ||
| 244 | +---+---+---+---+---+---+---+---+---+---+ | ||
| 245 | |||
| 246 | (if FLG.FEXTRA set) | ||
| 247 | |||
| 248 | +---+---+=================================+ | ||
| 249 | | XLEN |...XLEN bytes of "extra field"...| (more-->) | ||
| 250 | +---+---+=================================+ | ||
| 251 | |||
| 252 | (if FLG.FNAME set) | ||
| 253 | |||
| 254 | +=========================================+ | ||
| 255 | |...original file name, zero-terminated...| (more-->) | ||
| 256 | +=========================================+ | ||
| 257 | |||
| 258 | (if FLG.FCOMMENT set) | ||
| 259 | |||
| 260 | +===================================+ | ||
| 261 | |...file comment, zero-terminated...| (more-->) | ||
| 262 | +===================================+ | ||
| 263 | |||
| 264 | (if FLG.FHCRC set) | ||
| 265 | |||
| 266 | +---+---+ | ||
| 267 | | CRC16 | | ||
| 268 | +---+---+ | ||
| 269 | |||
| 270 | +=======================+ | ||
| 271 | |...compressed blocks...| (more-->) | ||
| 272 | +=======================+ | ||
| 273 | |||
| 274 | 0 1 2 3 4 5 6 7 | ||
| 275 | +---+---+---+---+---+---+---+---+ | ||
| 276 | | CRC32 | ISIZE | | ||
| 277 | +---+---+---+---+---+---+---+---+ | ||
| 278 | |||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | Deutsch Informational [Page 5] | ||
| 283 | |||
| 284 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 285 | |||
| 286 | |||
| 287 | 2.3.1. Member header and trailer | ||
| 288 | |||
| 289 | ID1 (IDentification 1) | ||
| 290 | ID2 (IDentification 2) | ||
| 291 | These have the fixed values ID1 = 31 (0x1f, \037), ID2 = 139 | ||
| 292 | (0x8b, \213), to identify the file as being in gzip format. | ||
| 293 | |||
| 294 | CM (Compression Method) | ||
| 295 | This identifies the compression method used in the file. CM | ||
| 296 | = 0-7 are reserved. CM = 8 denotes the "deflate" | ||
| 297 | compression method, which is the one customarily used by | ||
| 298 | gzip and which is documented elsewhere. | ||
| 299 | |||
| 300 | FLG (FLaGs) | ||
| 301 | This flag byte is divided into individual bits as follows: | ||
| 302 | |||
| 303 | bit 0 FTEXT | ||
| 304 | bit 1 FHCRC | ||
| 305 | bit 2 FEXTRA | ||
| 306 | bit 3 FNAME | ||
| 307 | bit 4 FCOMMENT | ||
| 308 | bit 5 reserved | ||
| 309 | bit 6 reserved | ||
| 310 | bit 7 reserved | ||
| 311 | |||
| 312 | If FTEXT is set, the file is probably ASCII text. This is | ||
| 313 | an optional indication, which the compressor may set by | ||
| 314 | checking a small amount of the input data to see whether any | ||
| 315 | non-ASCII characters are present. In case of doubt, FTEXT | ||
| 316 | is cleared, indicating binary data. For systems which have | ||
| 317 | different file formats for ascii text and binary data, the | ||
| 318 | decompressor can use FTEXT to choose the appropriate format. | ||
| 319 | We deliberately do not specify the algorithm used to set | ||
| 320 | this bit, since a compressor always has the option of | ||
| 321 | leaving it cleared and a decompressor always has the option | ||
| 322 | of ignoring it and letting some other program handle issues | ||
| 323 | of data conversion. | ||
| 324 | |||
| 325 | If FHCRC is set, a CRC16 for the gzip header is present, | ||
| 326 | immediately before the compressed data. The CRC16 consists | ||
| 327 | of the two least significant bytes of the CRC32 for all | ||
| 328 | bytes of the gzip header up to and not including the CRC16. | ||
| 329 | [The FHCRC bit was never set by versions of gzip up to | ||
| 330 | 1.2.4, even though it was documented with a different | ||
| 331 | meaning in gzip 1.2.4.] | ||
| 332 | |||
| 333 | If FEXTRA is set, optional extra fields are present, as | ||
| 334 | described in a following section. | ||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | Deutsch Informational [Page 6] | ||
| 339 | |||
| 340 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 341 | |||
| 342 | |||
| 343 | If FNAME is set, an original file name is present, | ||
| 344 | terminated by a zero byte. The name must consist of ISO | ||
| 345 | 8859-1 (LATIN-1) characters; on operating systems using | ||
| 346 | EBCDIC or any other character set for file names, the name | ||
| 347 | must be translated to the ISO LATIN-1 character set. This | ||
| 348 | is the original name of the file being compressed, with any | ||
| 349 | directory components removed, and, if the file being | ||
| 350 | compressed is on a file system with case insensitive names, | ||
| 351 | forced to lower case. There is no original file name if the | ||
| 352 | data was compressed from a source other than a named file; | ||
| 353 | for example, if the source was stdin on a Unix system, there | ||
| 354 | is no file name. | ||
| 355 | |||
| 356 | If FCOMMENT is set, a zero-terminated file comment is | ||
| 357 | present. This comment is not interpreted; it is only | ||
| 358 | intended for human consumption. The comment must consist of | ||
| 359 | ISO 8859-1 (LATIN-1) characters. Line breaks should be | ||
| 360 | denoted by a single line feed character (10 decimal). | ||
| 361 | |||
| 362 | Reserved FLG bits must be zero. | ||
| 363 | |||
| 364 | MTIME (Modification TIME) | ||
| 365 | This gives the most recent modification time of the original | ||
| 366 | file being compressed. The time is in Unix format, i.e., | ||
| 367 | seconds since 00:00:00 GMT, Jan. 1, 1970. (Note that this | ||
| 368 | may cause problems for MS-DOS and other systems that use | ||
| 369 | local rather than Universal time.) If the compressed data | ||
| 370 | did not come from a file, MTIME is set to the time at which | ||
| 371 | compression started. MTIME = 0 means no time stamp is | ||
| 372 | available. | ||
| 373 | |||
| 374 | XFL (eXtra FLags) | ||
| 375 | These flags are available for use by specific compression | ||
| 376 | methods. The "deflate" method (CM = 8) sets these flags as | ||
| 377 | follows: | ||
| 378 | |||
| 379 | XFL = 2 - compressor used maximum compression, | ||
| 380 | slowest algorithm | ||
| 381 | XFL = 4 - compressor used fastest algorithm | ||
| 382 | |||
| 383 | OS (Operating System) | ||
| 384 | This identifies the type of file system on which compression | ||
| 385 | took place. This may be useful in determining end-of-line | ||
| 386 | convention for text files. The currently defined values are | ||
| 387 | as follows: | ||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | |||
| 394 | Deutsch Informational [Page 7] | ||
| 395 | |||
| 396 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 397 | |||
| 398 | |||
| 399 | 0 - FAT filesystem (MS-DOS, OS/2, NT/Win32) | ||
| 400 | 1 - Amiga | ||
| 401 | 2 - VMS (or OpenVMS) | ||
| 402 | 3 - Unix | ||
| 403 | 4 - VM/CMS | ||
| 404 | 5 - Atari TOS | ||
| 405 | 6 - HPFS filesystem (OS/2, NT) | ||
| 406 | 7 - Macintosh | ||
| 407 | 8 - Z-System | ||
| 408 | 9 - CP/M | ||
| 409 | 10 - TOPS-20 | ||
| 410 | 11 - NTFS filesystem (NT) | ||
| 411 | 12 - QDOS | ||
| 412 | 13 - Acorn RISCOS | ||
| 413 | 255 - unknown | ||
| 414 | |||
| 415 | XLEN (eXtra LENgth) | ||
| 416 | If FLG.FEXTRA is set, this gives the length of the optional | ||
| 417 | extra field. See below for details. | ||
| 418 | |||
| 419 | CRC32 (CRC-32) | ||
| 420 | This contains a Cyclic Redundancy Check value of the | ||
| 421 | uncompressed data computed according to CRC-32 algorithm | ||
| 422 | used in the ISO 3309 standard and in section 8.1.1.6.2 of | ||
| 423 | ITU-T recommendation V.42. (See http://www.iso.ch for | ||
| 424 | ordering ISO documents. See gopher://info.itu.ch for an | ||
| 425 | online version of ITU-T V.42.) | ||
| 426 | |||
| 427 | ISIZE (Input SIZE) | ||
| 428 | This contains the size of the original (uncompressed) input | ||
| 429 | data modulo 2^32. | ||
| 430 | |||
| 431 | 2.3.1.1. Extra field | ||
| 432 | |||
| 433 | If the FLG.FEXTRA bit is set, an "extra field" is present in | ||
| 434 | the header, with total length XLEN bytes. It consists of a | ||
| 435 | series of subfields, each of the form: | ||
| 436 | |||
| 437 | +---+---+---+---+==================================+ | ||
| 438 | |SI1|SI2| LEN |... LEN bytes of subfield data ...| | ||
| 439 | +---+---+---+---+==================================+ | ||
| 440 | |||
| 441 | SI1 and SI2 provide a subfield ID, typically two ASCII letters | ||
| 442 | with some mnemonic value. Jean-Loup Gailly | ||
| 443 | <gzip@prep.ai.mit.edu> is maintaining a registry of subfield | ||
| 444 | IDs; please send him any subfield ID you wish to use. Subfield | ||
| 445 | IDs with SI2 = 0 are reserved for future use. The following | ||
| 446 | IDs are currently defined: | ||
| 447 | |||
| 448 | |||
| 449 | |||
| 450 | Deutsch Informational [Page 8] | ||
| 451 | |||
| 452 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 453 | |||
| 454 | |||
| 455 | SI1 SI2 Data | ||
| 456 | ---------- ---------- ---- | ||
| 457 | 0x41 ('A') 0x70 ('P') Apollo file type information | ||
| 458 | |||
| 459 | LEN gives the length of the subfield data, excluding the 4 | ||
| 460 | initial bytes. | ||
| 461 | |||
| 462 | 2.3.1.2. Compliance | ||
| 463 | |||
| 464 | A compliant compressor must produce files with correct ID1, | ||
| 465 | ID2, CM, CRC32, and ISIZE, but may set all the other fields in | ||
| 466 | the fixed-length part of the header to default values (255 for | ||
| 467 | OS, 0 for all others). The compressor must set all reserved | ||
| 468 | bits to zero. | ||
| 469 | |||
| 470 | A compliant decompressor must check ID1, ID2, and CM, and | ||
| 471 | provide an error indication if any of these have incorrect | ||
| 472 | values. It must examine FEXTRA/XLEN, FNAME, FCOMMENT and FHCRC | ||
| 473 | at least so it can skip over the optional fields if they are | ||
| 474 | present. It need not examine any other part of the header or | ||
| 475 | trailer; in particular, a decompressor may ignore FTEXT and OS | ||
| 476 | and always produce binary output, and still be compliant. A | ||
| 477 | compliant decompressor must give an error indication if any | ||
| 478 | reserved bit is non-zero, since such a bit could indicate the | ||
| 479 | presence of a new field that would cause subsequent data to be | ||
| 480 | interpreted incorrectly. | ||
| 481 | |||
| 482 | 3. References | ||
| 483 | |||
| 484 | [1] "Information Processing - 8-bit single-byte coded graphic | ||
| 485 | character sets - Part 1: Latin alphabet No.1" (ISO 8859-1:1987). | ||
| 486 | The ISO 8859-1 (Latin-1) character set is a superset of 7-bit | ||
| 487 | ASCII. Files defining this character set are available as | ||
| 488 | iso_8859-1.* in ftp://ftp.uu.net/graphics/png/documents/ | ||
| 489 | |||
| 490 | [2] ISO 3309 | ||
| 491 | |||
| 492 | [3] ITU-T recommendation V.42 | ||
| 493 | |||
| 494 | [4] Deutsch, L.P.,"DEFLATE Compressed Data Format Specification", | ||
| 495 | available in ftp://ftp.uu.net/pub/archiving/zip/doc/ | ||
| 496 | |||
| 497 | [5] Gailly, J.-L., GZIP documentation, available as gzip-*.tar in | ||
| 498 | ftp://prep.ai.mit.edu/pub/gnu/ | ||
| 499 | |||
| 500 | [6] Sarwate, D.V., "Computation of Cyclic Redundancy Checks via Table | ||
| 501 | Look-Up", Communications of the ACM, 31(8), pp.1008-1013. | ||
| 502 | |||
| 503 | |||
| 504 | |||
| 505 | |||
| 506 | Deutsch Informational [Page 9] | ||
| 507 | |||
| 508 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 509 | |||
| 510 | |||
| 511 | [7] Schwaderer, W.D., "CRC Calculation", April 85 PC Tech Journal, | ||
| 512 | pp.118-133. | ||
| 513 | |||
| 514 | [8] ftp://ftp.adelaide.edu.au/pub/rocksoft/papers/crc_v3.txt, | ||
| 515 | describing the CRC concept. | ||
| 516 | |||
| 517 | 4. Security Considerations | ||
| 518 | |||
| 519 | Any data compression method involves the reduction of redundancy in | ||
| 520 | the data. Consequently, any corruption of the data is likely to have | ||
| 521 | severe effects and be difficult to correct. Uncompressed text, on | ||
| 522 | the other hand, will probably still be readable despite the presence | ||
| 523 | of some corrupted bytes. | ||
| 524 | |||
| 525 | It is recommended that systems using this data format provide some | ||
| 526 | means of validating the integrity of the compressed data, such as by | ||
| 527 | setting and checking the CRC-32 check value. | ||
| 528 | |||
| 529 | 5. Acknowledgements | ||
| 530 | |||
| 531 | Trademarks cited in this document are the property of their | ||
| 532 | respective owners. | ||
| 533 | |||
| 534 | Jean-Loup Gailly designed the gzip format and wrote, with Mark Adler, | ||
| 535 | the related software described in this specification. Glenn | ||
| 536 | Randers-Pehrson converted this document to RFC and HTML format. | ||
| 537 | |||
| 538 | 6. Author's Address | ||
| 539 | |||
| 540 | L. Peter Deutsch | ||
| 541 | Aladdin Enterprises | ||
| 542 | 203 Santa Margarita Ave. | ||
| 543 | Menlo Park, CA 94025 | ||
| 544 | |||
| 545 | Phone: (415) 322-0103 (AM only) | ||
| 546 | FAX: (415) 322-1734 | ||
| 547 | EMail: <ghost@aladdin.com> | ||
| 548 | |||
| 549 | Questions about the technical content of this specification can be | ||
| 550 | sent by email to: | ||
| 551 | |||
| 552 | Jean-Loup Gailly <gzip@prep.ai.mit.edu> and | ||
| 553 | Mark Adler <madler@alumni.caltech.edu> | ||
| 554 | |||
| 555 | Editorial comments on this specification can be sent by email to: | ||
| 556 | |||
| 557 | L. Peter Deutsch <ghost@aladdin.com> and | ||
| 558 | Glenn Randers-Pehrson <randeg@alumni.rpi.edu> | ||
| 559 | |||
| 560 | |||
| 561 | |||
| 562 | Deutsch Informational [Page 10] | ||
| 563 | |||
| 564 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 565 | |||
| 566 | |||
| 567 | 7. Appendix: Jean-Loup Gailly's gzip utility | ||
| 568 | |||
| 569 | The most widely used implementation of gzip compression, and the | ||
| 570 | original documentation on which this specification is based, were | ||
| 571 | created by Jean-Loup Gailly <gzip@prep.ai.mit.edu>. Since this | ||
| 572 | implementation is a de facto standard, we mention some more of its | ||
| 573 | features here. Again, the material in this section is not part of | ||
| 574 | the specification per se, and implementations need not follow it to | ||
| 575 | be compliant. | ||
| 576 | |||
| 577 | When compressing or decompressing a file, gzip preserves the | ||
| 578 | protection, ownership, and modification time attributes on the local | ||
| 579 | file system, since there is no provision for representing protection | ||
| 580 | attributes in the gzip file format itself. Since the file format | ||
| 581 | includes a modification time, the gzip decompressor provides a | ||
| 582 | command line switch that assigns the modification time from the file, | ||
| 583 | rather than the local modification time of the compressed input, to | ||
| 584 | the decompressed output. | ||
| 585 | |||
| 586 | 8. Appendix: Sample CRC Code | ||
| 587 | |||
| 588 | The following sample code represents a practical implementation of | ||
| 589 | the CRC (Cyclic Redundancy Check). (See also ISO 3309 and ITU-T V.42 | ||
| 590 | for a formal specification.) | ||
| 591 | |||
| 592 | The sample code is in the ANSI C programming language. Non C users | ||
| 593 | may find it easier to read with these hints: | ||
| 594 | |||
| 595 | & Bitwise AND operator. | ||
| 596 | ^ Bitwise exclusive-OR operator. | ||
| 597 | >> Bitwise right shift operator. When applied to an | ||
| 598 | unsigned quantity, as here, right shift inserts zero | ||
| 599 | bit(s) at the left. | ||
| 600 | ! Logical NOT operator. | ||
| 601 | ++ "n++" increments the variable n. | ||
| 602 | 0xNNN 0x introduces a hexadecimal (base 16) constant. | ||
| 603 | Suffix L indicates a long value (at least 32 bits). | ||
| 604 | |||
| 605 | /* Table of CRCs of all 8-bit messages. */ | ||
| 606 | unsigned long crc_table[256]; | ||
| 607 | |||
| 608 | /* Flag: has the table been computed? Initially false. */ | ||
| 609 | int crc_table_computed = 0; | ||
| 610 | |||
| 611 | /* Make the table for a fast CRC. */ | ||
| 612 | void make_crc_table(void) | ||
| 613 | { | ||
| 614 | unsigned long c; | ||
| 615 | |||
| 616 | |||
| 617 | |||
| 618 | Deutsch Informational [Page 11] | ||
| 619 | |||
| 620 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 621 | |||
| 622 | |||
| 623 | int n, k; | ||
| 624 | for (n = 0; n < 256; n++) { | ||
| 625 | c = (unsigned long) n; | ||
| 626 | for (k = 0; k < 8; k++) { | ||
| 627 | if (c & 1) { | ||
| 628 | c = 0xedb88320L ^ (c >> 1); | ||
| 629 | } else { | ||
| 630 | c = c >> 1; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | crc_table[n] = c; | ||
| 634 | } | ||
| 635 | crc_table_computed = 1; | ||
| 636 | } | ||
| 637 | |||
| 638 | /* | ||
| 639 | Update a running crc with the bytes buf[0..len-1] and return | ||
| 640 | the updated crc. The crc should be initialized to zero. Pre- and | ||
| 641 | post-conditioning (one's complement) is performed within this | ||
| 642 | function so it shouldn't be done by the caller. Usage example: | ||
| 643 | |||
| 644 | unsigned long crc = 0L; | ||
| 645 | |||
| 646 | while (read_buffer(buffer, length) != EOF) { | ||
| 647 | crc = update_crc(crc, buffer, length); | ||
| 648 | } | ||
| 649 | if (crc != original_crc) error(); | ||
| 650 | */ | ||
| 651 | unsigned long update_crc(unsigned long crc, | ||
| 652 | unsigned char *buf, int len) | ||
| 653 | { | ||
| 654 | unsigned long c = crc ^ 0xffffffffL; | ||
| 655 | int n; | ||
| 656 | |||
| 657 | if (!crc_table_computed) | ||
| 658 | make_crc_table(); | ||
| 659 | for (n = 0; n < len; n++) { | ||
| 660 | c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8); | ||
| 661 | } | ||
| 662 | return c ^ 0xffffffffL; | ||
| 663 | } | ||
| 664 | |||
| 665 | /* Return the CRC of the bytes buf[0..len-1]. */ | ||
| 666 | unsigned long crc(unsigned char *buf, int len) | ||
| 667 | { | ||
| 668 | return update_crc(0L, buf, len); | ||
| 669 | } | ||
| 670 | |||
| 671 | |||
| 672 | |||
| 673 | |||
| 674 | Deutsch Informational [Page 12] | ||
| 675 | |||
lib/std/compress/rfc1952.txt.gz deleted| Binary files a/lib/std/compress/rfc1952.txt.gz and /dev/null differ | |||
lib/std/compress/testdata/rfc1951.txt created+955| ... | @@ -0,0 +1,955 @@ | ||
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | Network Working Group P. Deutsch | ||
| 8 | Request for Comments: 1951 Aladdin Enterprises | ||
| 9 | Category: Informational May 1996 | ||
| 10 | |||
| 11 | |||
| 12 | DEFLATE Compressed Data Format Specification version 1.3 | ||
| 13 | |||
| 14 | Status of This Memo | ||
| 15 | |||
| 16 | This memo provides information for the Internet community. This memo | ||
| 17 | does not specify an Internet standard of any kind. Distribution of | ||
| 18 | this memo is unlimited. | ||
| 19 | |||
| 20 | IESG Note: | ||
| 21 | |||
| 22 | The IESG takes no position on the validity of any Intellectual | ||
| 23 | Property Rights statements contained in this document. | ||
| 24 | |||
| 25 | Notices | ||
| 26 | |||
| 27 | Copyright (c) 1996 L. Peter Deutsch | ||
| 28 | |||
| 29 | Permission is granted to copy and distribute this document for any | ||
| 30 | purpose and without charge, including translations into other | ||
| 31 | languages and incorporation into compilations, provided that the | ||
| 32 | copyright notice and this notice are preserved, and that any | ||
| 33 | substantive changes or deletions from the original are clearly | ||
| 34 | marked. | ||
| 35 | |||
| 36 | A pointer to the latest version of this and related documentation in | ||
| 37 | HTML format can be found at the URL | ||
| 38 | <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>. | ||
| 39 | |||
| 40 | Abstract | ||
| 41 | |||
| 42 | This specification defines a lossless compressed data format that | ||
| 43 | compresses data using a combination of the LZ77 algorithm and Huffman | ||
| 44 | coding, with efficiency comparable to the best currently available | ||
| 45 | general-purpose compression methods. The data can be produced or | ||
| 46 | consumed, even for an arbitrarily long sequentially presented input | ||
| 47 | data stream, using only an a priori bounded amount of intermediate | ||
| 48 | storage. The format can be implemented readily in a manner not | ||
| 49 | covered by patents. | ||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | Deutsch Informational [Page 1] | ||
| 59 | |||
| 60 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 61 | |||
| 62 | |||
| 63 | Table of Contents | ||
| 64 | |||
| 65 | 1. Introduction ................................................... 2 | ||
| 66 | 1.1. Purpose ................................................... 2 | ||
| 67 | 1.2. Intended audience ......................................... 3 | ||
| 68 | 1.3. Scope ..................................................... 3 | ||
| 69 | 1.4. Compliance ................................................ 3 | ||
| 70 | 1.5. Definitions of terms and conventions used ................ 3 | ||
| 71 | 1.6. Changes from previous versions ............................ 4 | ||
| 72 | 2. Compressed representation overview ............................. 4 | ||
| 73 | 3. Detailed specification ......................................... 5 | ||
| 74 | 3.1. Overall conventions ....................................... 5 | ||
| 75 | 3.1.1. Packing into bytes .................................. 5 | ||
| 76 | 3.2. Compressed block format ................................... 6 | ||
| 77 | 3.2.1. Synopsis of prefix and Huffman coding ............... 6 | ||
| 78 | 3.2.2. Use of Huffman coding in the "deflate" format ....... 7 | ||
| 79 | 3.2.3. Details of block format ............................. 9 | ||
| 80 | 3.2.4. Non-compressed blocks (BTYPE=00) ................... 11 | ||
| 81 | 3.2.5. Compressed blocks (length and distance codes) ...... 11 | ||
| 82 | 3.2.6. Compression with fixed Huffman codes (BTYPE=01) .... 12 | ||
| 83 | 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) .. 13 | ||
| 84 | 3.3. Compliance ............................................... 14 | ||
| 85 | 4. Compression algorithm details ................................. 14 | ||
| 86 | 5. References .................................................... 16 | ||
| 87 | 6. Security Considerations ....................................... 16 | ||
| 88 | 7. Source code ................................................... 16 | ||
| 89 | 8. Acknowledgements .............................................. 16 | ||
| 90 | 9. Author's Address .............................................. 17 | ||
| 91 | |||
| 92 | 1. Introduction | ||
| 93 | |||
| 94 | 1.1. Purpose | ||
| 95 | |||
| 96 | The purpose of this specification is to define a lossless | ||
| 97 | compressed data format that: | ||
| 98 | * Is independent of CPU type, operating system, file system, | ||
| 99 | and character set, and hence can be used for interchange; | ||
| 100 | * Can be produced or consumed, even for an arbitrarily long | ||
| 101 | sequentially presented input data stream, using only an a | ||
| 102 | priori bounded amount of intermediate storage, and hence | ||
| 103 | can be used in data communications or similar structures | ||
| 104 | such as Unix filters; | ||
| 105 | * Compresses data with efficiency comparable to the best | ||
| 106 | currently available general-purpose compression methods, | ||
| 107 | and in particular considerably better than the "compress" | ||
| 108 | program; | ||
| 109 | * Can be implemented readily in a manner not covered by | ||
| 110 | patents, and hence can be practiced freely; | ||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | Deutsch Informational [Page 2] | ||
| 115 | |||
| 116 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 117 | |||
| 118 | |||
| 119 | * Is compatible with the file format produced by the current | ||
| 120 | widely used gzip utility, in that conforming decompressors | ||
| 121 | will be able to read data produced by the existing gzip | ||
| 122 | compressor. | ||
| 123 | |||
| 124 | The data format defined by this specification does not attempt to: | ||
| 125 | |||
| 126 | * Allow random access to compressed data; | ||
| 127 | * Compress specialized data (e.g., raster graphics) as well | ||
| 128 | as the best currently available specialized algorithms. | ||
| 129 | |||
| 130 | A simple counting argument shows that no lossless compression | ||
| 131 | algorithm can compress every possible input data set. For the | ||
| 132 | format defined here, the worst case expansion is 5 bytes per 32K- | ||
| 133 | byte block, i.e., a size increase of 0.015% for large data sets. | ||
| 134 | English text usually compresses by a factor of 2.5 to 3; | ||
| 135 | executable files usually compress somewhat less; graphical data | ||
| 136 | such as raster images may compress much more. | ||
| 137 | |||
| 138 | 1.2. Intended audience | ||
| 139 | |||
| 140 | This specification is intended for use by implementors of software | ||
| 141 | to compress data into "deflate" format and/or decompress data from | ||
| 142 | "deflate" format. | ||
| 143 | |||
| 144 | The text of the specification assumes a basic background in | ||
| 145 | programming at the level of bits and other primitive data | ||
| 146 | representations. Familiarity with the technique of Huffman coding | ||
| 147 | is helpful but not required. | ||
| 148 | |||
| 149 | 1.3. Scope | ||
| 150 | |||
| 151 | The specification specifies a method for representing a sequence | ||
| 152 | of bytes as a (usually shorter) sequence of bits, and a method for | ||
| 153 | packing the latter bit sequence into bytes. | ||
| 154 | |||
| 155 | 1.4. Compliance | ||
| 156 | |||
| 157 | Unless otherwise indicated below, a compliant decompressor must be | ||
| 158 | able to accept and decompress any data set that conforms to all | ||
| 159 | the specifications presented here; a compliant compressor must | ||
| 160 | produce data sets that conform to all the specifications presented | ||
| 161 | here. | ||
| 162 | |||
| 163 | 1.5. Definitions of terms and conventions used | ||
| 164 | |||
| 165 | Byte: 8 bits stored or transmitted as a unit (same as an octet). | ||
| 166 | For this specification, a byte is exactly 8 bits, even on machines | ||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | Deutsch Informational [Page 3] | ||
| 171 | |||
| 172 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 173 | |||
| 174 | |||
| 175 | which store a character on a number of bits different from eight. | ||
| 176 | See below, for the numbering of bits within a byte. | ||
| 177 | |||
| 178 | String: a sequence of arbitrary bytes. | ||
| 179 | |||
| 180 | 1.6. Changes from previous versions | ||
| 181 | |||
| 182 | There have been no technical changes to the deflate format since | ||
| 183 | version 1.1 of this specification. In version 1.2, some | ||
| 184 | terminology was changed. Version 1.3 is a conversion of the | ||
| 185 | specification to RFC style. | ||
| 186 | |||
| 187 | 2. Compressed representation overview | ||
| 188 | |||
| 189 | A compressed data set consists of a series of blocks, corresponding | ||
| 190 | to successive blocks of input data. The block sizes are arbitrary, | ||
| 191 | except that non-compressible blocks are limited to 65,535 bytes. | ||
| 192 | |||
| 193 | Each block is compressed using a combination of the LZ77 algorithm | ||
| 194 | and Huffman coding. The Huffman trees for each block are independent | ||
| 195 | of those for previous or subsequent blocks; the LZ77 algorithm may | ||
| 196 | use a reference to a duplicated string occurring in a previous block, | ||
| 197 | up to 32K input bytes before. | ||
| 198 | |||
| 199 | Each block consists of two parts: a pair of Huffman code trees that | ||
| 200 | describe the representation of the compressed data part, and a | ||
| 201 | compressed data part. (The Huffman trees themselves are compressed | ||
| 202 | using Huffman encoding.) The compressed data consists of a series of | ||
| 203 | elements of two types: literal bytes (of strings that have not been | ||
| 204 | detected as duplicated within the previous 32K input bytes), and | ||
| 205 | pointers to duplicated strings, where a pointer is represented as a | ||
| 206 | pair <length, backward distance>. The representation used in the | ||
| 207 | "deflate" format limits distances to 32K bytes and lengths to 258 | ||
| 208 | bytes, but does not limit the size of a block, except for | ||
| 209 | uncompressible blocks, which are limited as noted above. | ||
| 210 | |||
| 211 | Each type of value (literals, distances, and lengths) in the | ||
| 212 | compressed data is represented using a Huffman code, using one code | ||
| 213 | tree for literals and lengths and a separate code tree for distances. | ||
| 214 | The code trees for each block appear in a compact form just before | ||
| 215 | the compressed data for that block. | ||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | Deutsch Informational [Page 4] | ||
| 227 | |||
| 228 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 229 | |||
| 230 | |||
| 231 | 3. Detailed specification | ||
| 232 | |||
| 233 | 3.1. Overall conventions In the diagrams below, a box like this: | ||
| 234 | |||
| 235 | +---+ | ||
| 236 | | | <-- the vertical bars might be missing | ||
| 237 | +---+ | ||
| 238 | |||
| 239 | represents one byte; a box like this: | ||
| 240 | |||
| 241 | +==============+ | ||
| 242 | | | | ||
| 243 | +==============+ | ||
| 244 | |||
| 245 | represents a variable number of bytes. | ||
| 246 | |||
| 247 | Bytes stored within a computer do not have a "bit order", since | ||
| 248 | they are always treated as a unit. However, a byte considered as | ||
| 249 | an integer between 0 and 255 does have a most- and least- | ||
| 250 | significant bit, and since we write numbers with the most- | ||
| 251 | significant digit on the left, we also write bytes with the most- | ||
| 252 | significant bit on the left. In the diagrams below, we number the | ||
| 253 | bits of a byte so that bit 0 is the least-significant bit, i.e., | ||
| 254 | the bits are numbered: | ||
| 255 | |||
| 256 | +--------+ | ||
| 257 | |76543210| | ||
| 258 | +--------+ | ||
| 259 | |||
| 260 | Within a computer, a number may occupy multiple bytes. All | ||
| 261 | multi-byte numbers in the format described here are stored with | ||
| 262 | the least-significant byte first (at the lower memory address). | ||
| 263 | For example, the decimal number 520 is stored as: | ||
| 264 | |||
| 265 | 0 1 | ||
| 266 | +--------+--------+ | ||
| 267 | |00001000|00000010| | ||
| 268 | +--------+--------+ | ||
| 269 | ^ ^ | ||
| 270 | | | | ||
| 271 | | + more significant byte = 2 x 256 | ||
| 272 | + less significant byte = 8 | ||
| 273 | |||
| 274 | 3.1.1. Packing into bytes | ||
| 275 | |||
| 276 | This document does not address the issue of the order in which | ||
| 277 | bits of a byte are transmitted on a bit-sequential medium, | ||
| 278 | since the final data format described here is byte- rather than | ||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | Deutsch Informational [Page 5] | ||
| 283 | |||
| 284 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 285 | |||
| 286 | |||
| 287 | bit-oriented. However, we describe the compressed block format | ||
| 288 | in below, as a sequence of data elements of various bit | ||
| 289 | lengths, not a sequence of bytes. We must therefore specify | ||
| 290 | how to pack these data elements into bytes to form the final | ||
| 291 | compressed byte sequence: | ||
| 292 | |||
| 293 | * Data elements are packed into bytes in order of | ||
| 294 | increasing bit number within the byte, i.e., starting | ||
| 295 | with the least-significant bit of the byte. | ||
| 296 | * Data elements other than Huffman codes are packed | ||
| 297 | starting with the least-significant bit of the data | ||
| 298 | element. | ||
| 299 | * Huffman codes are packed starting with the most- | ||
| 300 | significant bit of the code. | ||
| 301 | |||
| 302 | In other words, if one were to print out the compressed data as | ||
| 303 | a sequence of bytes, starting with the first byte at the | ||
| 304 | *right* margin and proceeding to the *left*, with the most- | ||
| 305 | significant bit of each byte on the left as usual, one would be | ||
| 306 | able to parse the result from right to left, with fixed-width | ||
| 307 | elements in the correct MSB-to-LSB order and Huffman codes in | ||
| 308 | bit-reversed order (i.e., with the first bit of the code in the | ||
| 309 | relative LSB position). | ||
| 310 | |||
| 311 | 3.2. Compressed block format | ||
| 312 | |||
| 313 | 3.2.1. Synopsis of prefix and Huffman coding | ||
| 314 | |||
| 315 | Prefix coding represents symbols from an a priori known | ||
| 316 | alphabet by bit sequences (codes), one code for each symbol, in | ||
| 317 | a manner such that different symbols may be represented by bit | ||
| 318 | sequences of different lengths, but a parser can always parse | ||
| 319 | an encoded string unambiguously symbol-by-symbol. | ||
| 320 | |||
| 321 | We define a prefix code in terms of a binary tree in which the | ||
| 322 | two edges descending from each non-leaf node are labeled 0 and | ||
| 323 | 1 and in which the leaf nodes correspond one-for-one with (are | ||
| 324 | labeled with) the symbols of the alphabet; then the code for a | ||
| 325 | symbol is the sequence of 0's and 1's on the edges leading from | ||
| 326 | the root to the leaf labeled with that symbol. For example: | ||
| 327 | |||
| 328 | |||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | Deutsch Informational [Page 6] | ||
| 339 | |||
| 340 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 341 | |||
| 342 | |||
| 343 | /\ Symbol Code | ||
| 344 | 0 1 ------ ---- | ||
| 345 | / \ A 00 | ||
| 346 | /\ B B 1 | ||
| 347 | 0 1 C 011 | ||
| 348 | / \ D 010 | ||
| 349 | A /\ | ||
| 350 | 0 1 | ||
| 351 | / \ | ||
| 352 | D C | ||
| 353 | |||
| 354 | A parser can decode the next symbol from an encoded input | ||
| 355 | stream by walking down the tree from the root, at each step | ||
| 356 | choosing the edge corresponding to the next input bit. | ||
| 357 | |||
| 358 | Given an alphabet with known symbol frequencies, the Huffman | ||
| 359 | algorithm allows the construction of an optimal prefix code | ||
| 360 | (one which represents strings with those symbol frequencies | ||
| 361 | using the fewest bits of any possible prefix codes for that | ||
| 362 | alphabet). Such a code is called a Huffman code. (See | ||
| 363 | reference [1] in Chapter 5, references for additional | ||
| 364 | information on Huffman codes.) | ||
| 365 | |||
| 366 | Note that in the "deflate" format, the Huffman codes for the | ||
| 367 | various alphabets must not exceed certain maximum code lengths. | ||
| 368 | This constraint complicates the algorithm for computing code | ||
| 369 | lengths from symbol frequencies. Again, see Chapter 5, | ||
| 370 | references for details. | ||
| 371 | |||
| 372 | 3.2.2. Use of Huffman coding in the "deflate" format | ||
| 373 | |||
| 374 | The Huffman codes used for each alphabet in the "deflate" | ||
| 375 | format have two additional rules: | ||
| 376 | |||
| 377 | * All codes of a given bit length have lexicographically | ||
| 378 | consecutive values, in the same order as the symbols | ||
| 379 | they represent; | ||
| 380 | |||
| 381 | * Shorter codes lexicographically precede longer codes. | ||
| 382 | |||
| 383 | |||
| 384 | |||
| 385 | |||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | |||
| 394 | Deutsch Informational [Page 7] | ||
| 395 | |||
| 396 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 397 | |||
| 398 | |||
| 399 | We could recode the example above to follow this rule as | ||
| 400 | follows, assuming that the order of the alphabet is ABCD: | ||
| 401 | |||
| 402 | Symbol Code | ||
| 403 | ------ ---- | ||
| 404 | A 10 | ||
| 405 | B 0 | ||
| 406 | C 110 | ||
| 407 | D 111 | ||
| 408 | |||
| 409 | I.e., 0 precedes 10 which precedes 11x, and 110 and 111 are | ||
| 410 | lexicographically consecutive. | ||
| 411 | |||
| 412 | Given this rule, we can define the Huffman code for an alphabet | ||
| 413 | just by giving the bit lengths of the codes for each symbol of | ||
| 414 | the alphabet in order; this is sufficient to determine the | ||
| 415 | actual codes. In our example, the code is completely defined | ||
| 416 | by the sequence of bit lengths (2, 1, 3, 3). The following | ||
| 417 | algorithm generates the codes as integers, intended to be read | ||
| 418 | from most- to least-significant bit. The code lengths are | ||
| 419 | initially in tree[I].Len; the codes are produced in | ||
| 420 | tree[I].Code. | ||
| 421 | |||
| 422 | 1) Count the number of codes for each code length. Let | ||
| 423 | bl_count[N] be the number of codes of length N, N >= 1. | ||
| 424 | |||
| 425 | 2) Find the numerical value of the smallest code for each | ||
| 426 | code length: | ||
| 427 | |||
| 428 | code = 0; | ||
| 429 | bl_count[0] = 0; | ||
| 430 | for (bits = 1; bits <= MAX_BITS; bits++) { | ||
| 431 | code = (code + bl_count[bits-1]) << 1; | ||
| 432 | next_code[bits] = code; | ||
| 433 | } | ||
| 434 | |||
| 435 | 3) Assign numerical values to all codes, using consecutive | ||
| 436 | values for all codes of the same length with the base | ||
| 437 | values determined at step 2. Codes that are never used | ||
| 438 | (which have a bit length of zero) must not be assigned a | ||
| 439 | value. | ||
| 440 | |||
| 441 | for (n = 0; n <= max_code; n++) { | ||
| 442 | len = tree[n].Len; | ||
| 443 | if (len != 0) { | ||
| 444 | tree[n].Code = next_code[len]; | ||
| 445 | next_code[len]++; | ||
| 446 | } | ||
| 447 | |||
| 448 | |||
| 449 | |||
| 450 | Deutsch Informational [Page 8] | ||
| 451 | |||
| 452 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 453 | |||
| 454 | |||
| 455 | } | ||
| 456 | |||
| 457 | Example: | ||
| 458 | |||
| 459 | Consider the alphabet ABCDEFGH, with bit lengths (3, 3, 3, 3, | ||
| 460 | 3, 2, 4, 4). After step 1, we have: | ||
| 461 | |||
| 462 | N bl_count[N] | ||
| 463 | - ----------- | ||
| 464 | 2 1 | ||
| 465 | 3 5 | ||
| 466 | 4 2 | ||
| 467 | |||
| 468 | Step 2 computes the following next_code values: | ||
| 469 | |||
| 470 | N next_code[N] | ||
| 471 | - ------------ | ||
| 472 | 1 0 | ||
| 473 | 2 0 | ||
| 474 | 3 2 | ||
| 475 | 4 14 | ||
| 476 | |||
| 477 | Step 3 produces the following code values: | ||
| 478 | |||
| 479 | Symbol Length Code | ||
| 480 | ------ ------ ---- | ||
| 481 | A 3 010 | ||
| 482 | B 3 011 | ||
| 483 | C 3 100 | ||
| 484 | D 3 101 | ||
| 485 | E 3 110 | ||
| 486 | F 2 00 | ||
| 487 | G 4 1110 | ||
| 488 | H 4 1111 | ||
| 489 | |||
| 490 | 3.2.3. Details of block format | ||
| 491 | |||
| 492 | Each block of compressed data begins with 3 header bits | ||
| 493 | containing the following data: | ||
| 494 | |||
| 495 | first bit BFINAL | ||
| 496 | next 2 bits BTYPE | ||
| 497 | |||
| 498 | Note that the header bits do not necessarily begin on a byte | ||
| 499 | boundary, since a block does not necessarily occupy an integral | ||
| 500 | number of bytes. | ||
| 501 | |||
| 502 | |||
| 503 | |||
| 504 | |||
| 505 | |||
| 506 | Deutsch Informational [Page 9] | ||
| 507 | |||
| 508 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 509 | |||
| 510 | |||
| 511 | BFINAL is set if and only if this is the last block of the data | ||
| 512 | set. | ||
| 513 | |||
| 514 | BTYPE specifies how the data are compressed, as follows: | ||
| 515 | |||
| 516 | 00 - no compression | ||
| 517 | 01 - compressed with fixed Huffman codes | ||
| 518 | 10 - compressed with dynamic Huffman codes | ||
| 519 | 11 - reserved (error) | ||
| 520 | |||
| 521 | The only difference between the two compressed cases is how the | ||
| 522 | Huffman codes for the literal/length and distance alphabets are | ||
| 523 | defined. | ||
| 524 | |||
| 525 | In all cases, the decoding algorithm for the actual data is as | ||
| 526 | follows: | ||
| 527 | |||
| 528 | do | ||
| 529 | read block header from input stream. | ||
| 530 | if stored with no compression | ||
| 531 | skip any remaining bits in current partially | ||
| 532 | processed byte | ||
| 533 | read LEN and NLEN (see next section) | ||
| 534 | copy LEN bytes of data to output | ||
| 535 | otherwise | ||
| 536 | if compressed with dynamic Huffman codes | ||
| 537 | read representation of code trees (see | ||
| 538 | subsection below) | ||
| 539 | loop (until end of block code recognized) | ||
| 540 | decode literal/length value from input stream | ||
| 541 | if value < 256 | ||
| 542 | copy value (literal byte) to output stream | ||
| 543 | otherwise | ||
| 544 | if value = end of block (256) | ||
| 545 | break from loop | ||
| 546 | otherwise (value = 257..285) | ||
| 547 | decode distance from input stream | ||
| 548 | |||
| 549 | move backwards distance bytes in the output | ||
| 550 | stream, and copy length bytes from this | ||
| 551 | position to the output stream. | ||
| 552 | end loop | ||
| 553 | while not last block | ||
| 554 | |||
| 555 | Note that a duplicated string reference may refer to a string | ||
| 556 | in a previous block; i.e., the backward distance may cross one | ||
| 557 | or more block boundaries. However a distance cannot refer past | ||
| 558 | the beginning of the output stream. (An application using a | ||
| 559 | |||
| 560 | |||
| 561 | |||
| 562 | Deutsch Informational [Page 10] | ||
| 563 | |||
| 564 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 565 | |||
| 566 | |||
| 567 | preset dictionary might discard part of the output stream; a | ||
| 568 | distance can refer to that part of the output stream anyway) | ||
| 569 | Note also that the referenced string may overlap the current | ||
| 570 | position; for example, if the last 2 bytes decoded have values | ||
| 571 | X and Y, a string reference with <length = 5, distance = 2> | ||
| 572 | adds X,Y,X,Y,X to the output stream. | ||
| 573 | |||
| 574 | We now specify each compression method in turn. | ||
| 575 | |||
| 576 | 3.2.4. Non-compressed blocks (BTYPE=00) | ||
| 577 | |||
| 578 | Any bits of input up to the next byte boundary are ignored. | ||
| 579 | The rest of the block consists of the following information: | ||
| 580 | |||
| 581 | 0 1 2 3 4... | ||
| 582 | +---+---+---+---+================================+ | ||
| 583 | | LEN | NLEN |... LEN bytes of literal data...| | ||
| 584 | +---+---+---+---+================================+ | ||
| 585 | |||
| 586 | LEN is the number of data bytes in the block. NLEN is the | ||
| 587 | one's complement of LEN. | ||
| 588 | |||
| 589 | 3.2.5. Compressed blocks (length and distance codes) | ||
| 590 | |||
| 591 | As noted above, encoded data blocks in the "deflate" format | ||
| 592 | consist of sequences of symbols drawn from three conceptually | ||
| 593 | distinct alphabets: either literal bytes, from the alphabet of | ||
| 594 | byte values (0..255), or <length, backward distance> pairs, | ||
| 595 | where the length is drawn from (3..258) and the distance is | ||
| 596 | drawn from (1..32,768). In fact, the literal and length | ||
| 597 | alphabets are merged into a single alphabet (0..285), where | ||
| 598 | values 0..255 represent literal bytes, the value 256 indicates | ||
| 599 | end-of-block, and values 257..285 represent length codes | ||
| 600 | (possibly in conjunction with extra bits following the symbol | ||
| 601 | code) as follows: | ||
| 602 | |||
| 603 | |||
| 604 | |||
| 605 | |||
| 606 | |||
| 607 | |||
| 608 | |||
| 609 | |||
| 610 | |||
| 611 | |||
| 612 | |||
| 613 | |||
| 614 | |||
| 615 | |||
| 616 | |||
| 617 | |||
| 618 | Deutsch Informational [Page 11] | ||
| 619 | |||
| 620 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 621 | |||
| 622 | |||
| 623 | Extra Extra Extra | ||
| 624 | Code Bits Length(s) Code Bits Lengths Code Bits Length(s) | ||
| 625 | ---- ---- ------ ---- ---- ------- ---- ---- ------- | ||
| 626 | 257 0 3 267 1 15,16 277 4 67-82 | ||
| 627 | 258 0 4 268 1 17,18 278 4 83-98 | ||
| 628 | 259 0 5 269 2 19-22 279 4 99-114 | ||
| 629 | 260 0 6 270 2 23-26 280 4 115-130 | ||
| 630 | 261 0 7 271 2 27-30 281 5 131-162 | ||
| 631 | 262 0 8 272 2 31-34 282 5 163-194 | ||
| 632 | 263 0 9 273 3 35-42 283 5 195-226 | ||
| 633 | 264 0 10 274 3 43-50 284 5 227-257 | ||
| 634 | 265 1 11,12 275 3 51-58 285 0 258 | ||
| 635 | 266 1 13,14 276 3 59-66 | ||
| 636 | |||
| 637 | The extra bits should be interpreted as a machine integer | ||
| 638 | stored with the most-significant bit first, e.g., bits 1110 | ||
| 639 | represent the value 14. | ||
| 640 | |||
| 641 | Extra Extra Extra | ||
| 642 | Code Bits Dist Code Bits Dist Code Bits Distance | ||
| 643 | ---- ---- ---- ---- ---- ------ ---- ---- -------- | ||
| 644 | 0 0 1 10 4 33-48 20 9 1025-1536 | ||
| 645 | 1 0 2 11 4 49-64 21 9 1537-2048 | ||
| 646 | 2 0 3 12 5 65-96 22 10 2049-3072 | ||
| 647 | 3 0 4 13 5 97-128 23 10 3073-4096 | ||
| 648 | 4 1 5,6 14 6 129-192 24 11 4097-6144 | ||
| 649 | 5 1 7,8 15 6 193-256 25 11 6145-8192 | ||
| 650 | 6 2 9-12 16 7 257-384 26 12 8193-12288 | ||
| 651 | 7 2 13-16 17 7 385-512 27 12 12289-16384 | ||
| 652 | 8 3 17-24 18 8 513-768 28 13 16385-24576 | ||
| 653 | 9 3 25-32 19 8 769-1024 29 13 24577-32768 | ||
| 654 | |||
| 655 | 3.2.6. Compression with fixed Huffman codes (BTYPE=01) | ||
| 656 | |||
| 657 | The Huffman codes for the two alphabets are fixed, and are not | ||
| 658 | represented explicitly in the data. The Huffman code lengths | ||
| 659 | for the literal/length alphabet are: | ||
| 660 | |||
| 661 | Lit Value Bits Codes | ||
| 662 | --------- ---- ----- | ||
| 663 | 0 - 143 8 00110000 through | ||
| 664 | 10111111 | ||
| 665 | 144 - 255 9 110010000 through | ||
| 666 | 111111111 | ||
| 667 | 256 - 279 7 0000000 through | ||
| 668 | 0010111 | ||
| 669 | 280 - 287 8 11000000 through | ||
| 670 | 11000111 | ||
| 671 | |||
| 672 | |||
| 673 | |||
| 674 | Deutsch Informational [Page 12] | ||
| 675 | |||
| 676 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 677 | |||
| 678 | |||
| 679 | The code lengths are sufficient to generate the actual codes, | ||
| 680 | as described above; we show the codes in the table for added | ||
| 681 | clarity. Literal/length values 286-287 will never actually | ||
| 682 | occur in the compressed data, but participate in the code | ||
| 683 | construction. | ||
| 684 | |||
| 685 | Distance codes 0-31 are represented by (fixed-length) 5-bit | ||
| 686 | codes, with possible additional bits as shown in the table | ||
| 687 | shown in Paragraph 3.2.5, above. Note that distance codes 30- | ||
| 688 | 31 will never actually occur in the compressed data. | ||
| 689 | |||
| 690 | 3.2.7. Compression with dynamic Huffman codes (BTYPE=10) | ||
| 691 | |||
| 692 | The Huffman codes for the two alphabets appear in the block | ||
| 693 | immediately after the header bits and before the actual | ||
| 694 | compressed data, first the literal/length code and then the | ||
| 695 | distance code. Each code is defined by a sequence of code | ||
| 696 | lengths, as discussed in Paragraph 3.2.2, above. For even | ||
| 697 | greater compactness, the code length sequences themselves are | ||
| 698 | compressed using a Huffman code. The alphabet for code lengths | ||
| 699 | is as follows: | ||
| 700 | |||
| 701 | 0 - 15: Represent code lengths of 0 - 15 | ||
| 702 | 16: Copy the previous code length 3 - 6 times. | ||
| 703 | The next 2 bits indicate repeat length | ||
| 704 | (0 = 3, ... , 3 = 6) | ||
| 705 | Example: Codes 8, 16 (+2 bits 11), | ||
| 706 | 16 (+2 bits 10) will expand to | ||
| 707 | 12 code lengths of 8 (1 + 6 + 5) | ||
| 708 | 17: Repeat a code length of 0 for 3 - 10 times. | ||
| 709 | (3 bits of length) | ||
| 710 | 18: Repeat a code length of 0 for 11 - 138 times | ||
| 711 | (7 bits of length) | ||
| 712 | |||
| 713 | A code length of 0 indicates that the corresponding symbol in | ||
| 714 | the literal/length or distance alphabet will not occur in the | ||
| 715 | block, and should not participate in the Huffman code | ||
| 716 | construction algorithm given earlier. If only one distance | ||
| 717 | code is used, it is encoded using one bit, not zero bits; in | ||
| 718 | this case there is a single code length of one, with one unused | ||
| 719 | code. One distance code of zero bits means that there are no | ||
| 720 | distance codes used at all (the data is all literals). | ||
| 721 | |||
| 722 | We can now define the format of the block: | ||
| 723 | |||
| 724 | 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286) | ||
| 725 | 5 Bits: HDIST, # of Distance codes - 1 (1 - 32) | ||
| 726 | 4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19) | ||
| 727 | |||
| 728 | |||
| 729 | |||
| 730 | Deutsch Informational [Page 13] | ||
| 731 | |||
| 732 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 733 | |||
| 734 | |||
| 735 | (HCLEN + 4) x 3 bits: code lengths for the code length | ||
| 736 | alphabet given just above, in the order: 16, 17, 18, | ||
| 737 | 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 | ||
| 738 | |||
| 739 | These code lengths are interpreted as 3-bit integers | ||
| 740 | (0-7); as above, a code length of 0 means the | ||
| 741 | corresponding symbol (literal/length or distance code | ||
| 742 | length) is not used. | ||
| 743 | |||
| 744 | HLIT + 257 code lengths for the literal/length alphabet, | ||
| 745 | encoded using the code length Huffman code | ||
| 746 | |||
| 747 | HDIST + 1 code lengths for the distance alphabet, | ||
| 748 | encoded using the code length Huffman code | ||
| 749 | |||
| 750 | The actual compressed data of the block, | ||
| 751 | encoded using the literal/length and distance Huffman | ||
| 752 | codes | ||
| 753 | |||
| 754 | The literal/length symbol 256 (end of data), | ||
| 755 | encoded using the literal/length Huffman code | ||
| 756 | |||
| 757 | The code length repeat codes can cross from HLIT + 257 to the | ||
| 758 | HDIST + 1 code lengths. In other words, all code lengths form | ||
| 759 | a single sequence of HLIT + HDIST + 258 values. | ||
| 760 | |||
| 761 | 3.3. Compliance | ||
| 762 | |||
| 763 | A compressor may limit further the ranges of values specified in | ||
| 764 | the previous section and still be compliant; for example, it may | ||
| 765 | limit the range of backward pointers to some value smaller than | ||
| 766 | 32K. Similarly, a compressor may limit the size of blocks so that | ||
| 767 | a compressible block fits in memory. | ||
| 768 | |||
| 769 | A compliant decompressor must accept the full range of possible | ||
| 770 | values defined in the previous section, and must accept blocks of | ||
| 771 | arbitrary size. | ||
| 772 | |||
| 773 | 4. Compression algorithm details | ||
| 774 | |||
| 775 | While it is the intent of this document to define the "deflate" | ||
| 776 | compressed data format without reference to any particular | ||
| 777 | compression algorithm, the format is related to the compressed | ||
| 778 | formats produced by LZ77 (Lempel-Ziv 1977, see reference [2] below); | ||
| 779 | since many variations of LZ77 are patented, it is strongly | ||
| 780 | recommended that the implementor of a compressor follow the general | ||
| 781 | algorithm presented here, which is known not to be patented per se. | ||
| 782 | The material in this section is not part of the definition of the | ||
| 783 | |||
| 784 | |||
| 785 | |||
| 786 | Deutsch Informational [Page 14] | ||
| 787 | |||
| 788 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 789 | |||
| 790 | |||
| 791 | specification per se, and a compressor need not follow it in order to | ||
| 792 | be compliant. | ||
| 793 | |||
| 794 | The compressor terminates a block when it determines that starting a | ||
| 795 | new block with fresh trees would be useful, or when the block size | ||
| 796 | fills up the compressor's block buffer. | ||
| 797 | |||
| 798 | The compressor uses a chained hash table to find duplicated strings, | ||
| 799 | using a hash function that operates on 3-byte sequences. At any | ||
| 800 | given point during compression, let XYZ be the next 3 input bytes to | ||
| 801 | be examined (not necessarily all different, of course). First, the | ||
| 802 | compressor examines the hash chain for XYZ. If the chain is empty, | ||
| 803 | the compressor simply writes out X as a literal byte and advances one | ||
| 804 | byte in the input. If the hash chain is not empty, indicating that | ||
| 805 | the sequence XYZ (or, if we are unlucky, some other 3 bytes with the | ||
| 806 | same hash function value) has occurred recently, the compressor | ||
| 807 | compares all strings on the XYZ hash chain with the actual input data | ||
| 808 | sequence starting at the current point, and selects the longest | ||
| 809 | match. | ||
| 810 | |||
| 811 | The compressor searches the hash chains starting with the most recent | ||
| 812 | strings, to favor small distances and thus take advantage of the | ||
| 813 | Huffman encoding. The hash chains are singly linked. There are no | ||
| 814 | deletions from the hash chains; the algorithm simply discards matches | ||
| 815 | that are too old. To avoid a worst-case situation, very long hash | ||
| 816 | chains are arbitrarily truncated at a certain length, determined by a | ||
| 817 | run-time parameter. | ||
| 818 | |||
| 819 | To improve overall compression, the compressor optionally defers the | ||
| 820 | selection of matches ("lazy matching"): after a match of length N has | ||
| 821 | been found, the compressor searches for a longer match starting at | ||
| 822 | the next input byte. If it finds a longer match, it truncates the | ||
| 823 | previous match to a length of one (thus producing a single literal | ||
| 824 | byte) and then emits the longer match. Otherwise, it emits the | ||
| 825 | original match, and, as described above, advances N bytes before | ||
| 826 | continuing. | ||
| 827 | |||
| 828 | Run-time parameters also control this "lazy match" procedure. If | ||
| 829 | compression ratio is most important, the compressor attempts a | ||
| 830 | complete second search regardless of the length of the first match. | ||
| 831 | In the normal case, if the current match is "long enough", the | ||
| 832 | compressor reduces the search for a longer match, thus speeding up | ||
| 833 | the process. If speed is most important, the compressor inserts new | ||
| 834 | strings in the hash table only when no match was found, or when the | ||
| 835 | match is not "too long". This degrades the compression ratio but | ||
| 836 | saves time since there are both fewer insertions and fewer searches. | ||
| 837 | |||
| 838 | |||
| 839 | |||
| 840 | |||
| 841 | |||
| 842 | Deutsch Informational [Page 15] | ||
| 843 | |||
| 844 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 845 | |||
| 846 | |||
| 847 | 5. References | ||
| 848 | |||
| 849 | [1] Huffman, D. A., "A Method for the Construction of Minimum | ||
| 850 | Redundancy Codes", Proceedings of the Institute of Radio | ||
| 851 | Engineers, September 1952, Volume 40, Number 9, pp. 1098-1101. | ||
| 852 | |||
| 853 | [2] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data | ||
| 854 | Compression", IEEE Transactions on Information Theory, Vol. 23, | ||
| 855 | No. 3, pp. 337-343. | ||
| 856 | |||
| 857 | [3] Gailly, J.-L., and Adler, M., ZLIB documentation and sources, | ||
| 858 | available in ftp://ftp.uu.net/pub/archiving/zip/doc/ | ||
| 859 | |||
| 860 | [4] Gailly, J.-L., and Adler, M., GZIP documentation and sources, | ||
| 861 | available as gzip-*.tar in ftp://prep.ai.mit.edu/pub/gnu/ | ||
| 862 | |||
| 863 | [5] Schwartz, E. S., and Kallick, B. "Generating a canonical prefix | ||
| 864 | encoding." Comm. ACM, 7,3 (Mar. 1964), pp. 166-169. | ||
| 865 | |||
| 866 | [6] Hirschberg and Lelewer, "Efficient decoding of prefix codes," | ||
| 867 | Comm. ACM, 33,4, April 1990, pp. 449-459. | ||
| 868 | |||
| 869 | 6. Security Considerations | ||
| 870 | |||
| 871 | Any data compression method involves the reduction of redundancy in | ||
| 872 | the data. Consequently, any corruption of the data is likely to have | ||
| 873 | severe effects and be difficult to correct. Uncompressed text, on | ||
| 874 | the other hand, will probably still be readable despite the presence | ||
| 875 | of some corrupted bytes. | ||
| 876 | |||
| 877 | It is recommended that systems using this data format provide some | ||
| 878 | means of validating the integrity of the compressed data. See | ||
| 879 | reference [3], for example. | ||
| 880 | |||
| 881 | 7. Source code | ||
| 882 | |||
| 883 | Source code for a C language implementation of a "deflate" compliant | ||
| 884 | compressor and decompressor is available within the zlib package at | ||
| 885 | ftp://ftp.uu.net/pub/archiving/zip/zlib/. | ||
| 886 | |||
| 887 | 8. Acknowledgements | ||
| 888 | |||
| 889 | Trademarks cited in this document are the property of their | ||
| 890 | respective owners. | ||
| 891 | |||
| 892 | Phil Katz designed the deflate format. Jean-Loup Gailly and Mark | ||
| 893 | Adler wrote the related software described in this specification. | ||
| 894 | Glenn Randers-Pehrson converted this document to RFC and HTML format. | ||
| 895 | |||
| 896 | |||
| 897 | |||
| 898 | Deutsch Informational [Page 16] | ||
| 899 | |||
| 900 | RFC 1951 DEFLATE Compressed Data Format Specification May 1996 | ||
| 901 | |||
| 902 | |||
| 903 | 9. Author's Address | ||
| 904 | |||
| 905 | L. Peter Deutsch | ||
| 906 | Aladdin Enterprises | ||
| 907 | 203 Santa Margarita Ave. | ||
| 908 | Menlo Park, CA 94025 | ||
| 909 | |||
| 910 | Phone: (415) 322-0103 (AM only) | ||
| 911 | FAX: (415) 322-1734 | ||
| 912 | EMail: <ghost@aladdin.com> | ||
| 913 | |||
| 914 | Questions about the technical content of this specification can be | ||
| 915 | sent by email to: | ||
| 916 | |||
| 917 | Jean-Loup Gailly <gzip@prep.ai.mit.edu> and | ||
| 918 | Mark Adler <madler@alumni.caltech.edu> | ||
| 919 | |||
| 920 | Editorial comments on this specification can be sent by email to: | ||
| 921 | |||
| 922 | L. Peter Deutsch <ghost@aladdin.com> and | ||
| 923 | Glenn Randers-Pehrson <randeg@alumni.rpi.edu> | ||
| 924 | |||
| 925 | |||
| 926 | |||
| 927 | |||
| 928 | |||
| 929 | |||
| 930 | |||
| 931 | |||
| 932 | |||
| 933 | |||
| 934 | |||
| 935 | |||
| 936 | |||
| 937 | |||
| 938 | |||
| 939 | |||
| 940 | |||
| 941 | |||
| 942 | |||
| 943 | |||
| 944 | |||
| 945 | |||
| 946 | |||
| 947 | |||
| 948 | |||
| 949 | |||
| 950 | |||
| 951 | |||
| 952 | |||
| 953 | |||
| 954 | Deutsch Informational [Page 17] | ||
| 955 | |||
lib/std/compress/testdata/rfc1951.txt.fixed.z.9 created| Binary files /dev/null and b/lib/std/compress/testdata/rfc1951.txt.fixed.z.9 differ | |||
lib/std/compress/testdata/rfc1951.txt.z.0 created| Binary files /dev/null and b/lib/std/compress/testdata/rfc1951.txt.z.0 differ | |||
lib/std/compress/testdata/rfc1951.txt.z.9 created| Binary files /dev/null and b/lib/std/compress/testdata/rfc1951.txt.z.9 differ | |||
lib/std/compress/testdata/rfc1952.txt created+675| ... | @@ -0,0 +1,675 @@ | ||
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | Network Working Group P. Deutsch | ||
| 8 | Request for Comments: 1952 Aladdin Enterprises | ||
| 9 | Category: Informational May 1996 | ||
| 10 | |||
| 11 | |||
| 12 | GZIP file format specification version 4.3 | ||
| 13 | |||
| 14 | Status of This Memo | ||
| 15 | |||
| 16 | This memo provides information for the Internet community. This memo | ||
| 17 | does not specify an Internet standard of any kind. Distribution of | ||
| 18 | this memo is unlimited. | ||
| 19 | |||
| 20 | IESG Note: | ||
| 21 | |||
| 22 | The IESG takes no position on the validity of any Intellectual | ||
| 23 | Property Rights statements contained in this document. | ||
| 24 | |||
| 25 | Notices | ||
| 26 | |||
| 27 | Copyright (c) 1996 L. Peter Deutsch | ||
| 28 | |||
| 29 | Permission is granted to copy and distribute this document for any | ||
| 30 | purpose and without charge, including translations into other | ||
| 31 | languages and incorporation into compilations, provided that the | ||
| 32 | copyright notice and this notice are preserved, and that any | ||
| 33 | substantive changes or deletions from the original are clearly | ||
| 34 | marked. | ||
| 35 | |||
| 36 | A pointer to the latest version of this and related documentation in | ||
| 37 | HTML format can be found at the URL | ||
| 38 | <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>. | ||
| 39 | |||
| 40 | Abstract | ||
| 41 | |||
| 42 | This specification defines a lossless compressed data format that is | ||
| 43 | compatible with the widely used GZIP utility. The format includes a | ||
| 44 | cyclic redundancy check value for detecting data corruption. The | ||
| 45 | format presently uses the DEFLATE method of compression but can be | ||
| 46 | easily extended to use other compression methods. The format can be | ||
| 47 | implemented readily in a manner not covered by patents. | ||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | Deutsch Informational [Page 1] | ||
| 59 | |||
| 60 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 61 | |||
| 62 | |||
| 63 | Table of Contents | ||
| 64 | |||
| 65 | 1. Introduction ................................................... 2 | ||
| 66 | 1.1. Purpose ................................................... 2 | ||
| 67 | 1.2. Intended audience ......................................... 3 | ||
| 68 | 1.3. Scope ..................................................... 3 | ||
| 69 | 1.4. Compliance ................................................ 3 | ||
| 70 | 1.5. Definitions of terms and conventions used ................. 3 | ||
| 71 | 1.6. Changes from previous versions ............................ 3 | ||
| 72 | 2. Detailed specification ......................................... 4 | ||
| 73 | 2.1. Overall conventions ....................................... 4 | ||
| 74 | 2.2. File format ............................................... 5 | ||
| 75 | 2.3. Member format ............................................. 5 | ||
| 76 | 2.3.1. Member header and trailer ........................... 6 | ||
| 77 | 2.3.1.1. Extra field ................................... 8 | ||
| 78 | 2.3.1.2. Compliance .................................... 9 | ||
| 79 | 3. References .................................................. 9 | ||
| 80 | 4. Security Considerations .................................... 10 | ||
| 81 | 5. Acknowledgements ........................................... 10 | ||
| 82 | 6. Author's Address ........................................... 10 | ||
| 83 | 7. Appendix: Jean-Loup Gailly's gzip utility .................. 11 | ||
| 84 | 8. Appendix: Sample CRC Code .................................. 11 | ||
| 85 | |||
| 86 | 1. Introduction | ||
| 87 | |||
| 88 | 1.1. Purpose | ||
| 89 | |||
| 90 | The purpose of this specification is to define a lossless | ||
| 91 | compressed data format that: | ||
| 92 | |||
| 93 | * Is independent of CPU type, operating system, file system, | ||
| 94 | and character set, and hence can be used for interchange; | ||
| 95 | * Can compress or decompress a data stream (as opposed to a | ||
| 96 | randomly accessible file) to produce another data stream, | ||
| 97 | using only an a priori bounded amount of intermediate | ||
| 98 | storage, and hence can be used in data communications or | ||
| 99 | similar structures such as Unix filters; | ||
| 100 | * Compresses data with efficiency comparable to the best | ||
| 101 | currently available general-purpose compression methods, | ||
| 102 | and in particular considerably better than the "compress" | ||
| 103 | program; | ||
| 104 | * Can be implemented readily in a manner not covered by | ||
| 105 | patents, and hence can be practiced freely; | ||
| 106 | * Is compatible with the file format produced by the current | ||
| 107 | widely used gzip utility, in that conforming decompressors | ||
| 108 | will be able to read data produced by the existing gzip | ||
| 109 | compressor. | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | Deutsch Informational [Page 2] | ||
| 115 | |||
| 116 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 117 | |||
| 118 | |||
| 119 | The data format defined by this specification does not attempt to: | ||
| 120 | |||
| 121 | * Provide random access to compressed data; | ||
| 122 | * Compress specialized data (e.g., raster graphics) as well as | ||
| 123 | the best currently available specialized algorithms. | ||
| 124 | |||
| 125 | 1.2. Intended audience | ||
| 126 | |||
| 127 | This specification is intended for use by implementors of software | ||
| 128 | to compress data into gzip format and/or decompress data from gzip | ||
| 129 | format. | ||
| 130 | |||
| 131 | The text of the specification assumes a basic background in | ||
| 132 | programming at the level of bits and other primitive data | ||
| 133 | representations. | ||
| 134 | |||
| 135 | 1.3. Scope | ||
| 136 | |||
| 137 | The specification specifies a compression method and a file format | ||
| 138 | (the latter assuming only that a file can store a sequence of | ||
| 139 | arbitrary bytes). It does not specify any particular interface to | ||
| 140 | a file system or anything about character sets or encodings | ||
| 141 | (except for file names and comments, which are optional). | ||
| 142 | |||
| 143 | 1.4. Compliance | ||
| 144 | |||
| 145 | Unless otherwise indicated below, a compliant decompressor must be | ||
| 146 | able to accept and decompress any file that conforms to all the | ||
| 147 | specifications presented here; a compliant compressor must produce | ||
| 148 | files that conform to all the specifications presented here. The | ||
| 149 | material in the appendices is not part of the specification per se | ||
| 150 | and is not relevant to compliance. | ||
| 151 | |||
| 152 | 1.5. Definitions of terms and conventions used | ||
| 153 | |||
| 154 | byte: 8 bits stored or transmitted as a unit (same as an octet). | ||
| 155 | (For this specification, a byte is exactly 8 bits, even on | ||
| 156 | machines which store a character on a number of bits different | ||
| 157 | from 8.) See below for the numbering of bits within a byte. | ||
| 158 | |||
| 159 | 1.6. Changes from previous versions | ||
| 160 | |||
| 161 | There have been no technical changes to the gzip format since | ||
| 162 | version 4.1 of this specification. In version 4.2, some | ||
| 163 | terminology was changed, and the sample CRC code was rewritten for | ||
| 164 | clarity and to eliminate the requirement for the caller to do pre- | ||
| 165 | and post-conditioning. Version 4.3 is a conversion of the | ||
| 166 | specification to RFC style. | ||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | Deutsch Informational [Page 3] | ||
| 171 | |||
| 172 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 173 | |||
| 174 | |||
| 175 | 2. Detailed specification | ||
| 176 | |||
| 177 | 2.1. Overall conventions | ||
| 178 | |||
| 179 | In the diagrams below, a box like this: | ||
| 180 | |||
| 181 | +---+ | ||
| 182 | | | <-- the vertical bars might be missing | ||
| 183 | +---+ | ||
| 184 | |||
| 185 | represents one byte; a box like this: | ||
| 186 | |||
| 187 | +==============+ | ||
| 188 | | | | ||
| 189 | +==============+ | ||
| 190 | |||
| 191 | represents a variable number of bytes. | ||
| 192 | |||
| 193 | Bytes stored within a computer do not have a "bit order", since | ||
| 194 | they are always treated as a unit. However, a byte considered as | ||
| 195 | an integer between 0 and 255 does have a most- and least- | ||
| 196 | significant bit, and since we write numbers with the most- | ||
| 197 | significant digit on the left, we also write bytes with the most- | ||
| 198 | significant bit on the left. In the diagrams below, we number the | ||
| 199 | bits of a byte so that bit 0 is the least-significant bit, i.e., | ||
| 200 | the bits are numbered: | ||
| 201 | |||
| 202 | +--------+ | ||
| 203 | |76543210| | ||
| 204 | +--------+ | ||
| 205 | |||
| 206 | This document does not address the issue of the order in which | ||
| 207 | bits of a byte are transmitted on a bit-sequential medium, since | ||
| 208 | the data format described here is byte- rather than bit-oriented. | ||
| 209 | |||
| 210 | Within a computer, a number may occupy multiple bytes. All | ||
| 211 | multi-byte numbers in the format described here are stored with | ||
| 212 | the least-significant byte first (at the lower memory address). | ||
| 213 | For example, the decimal number 520 is stored as: | ||
| 214 | |||
| 215 | 0 1 | ||
| 216 | +--------+--------+ | ||
| 217 | |00001000|00000010| | ||
| 218 | +--------+--------+ | ||
| 219 | ^ ^ | ||
| 220 | | | | ||
| 221 | | + more significant byte = 2 x 256 | ||
| 222 | + less significant byte = 8 | ||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | Deutsch Informational [Page 4] | ||
| 227 | |||
| 228 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 229 | |||
| 230 | |||
| 231 | 2.2. File format | ||
| 232 | |||
| 233 | A gzip file consists of a series of "members" (compressed data | ||
| 234 | sets). The format of each member is specified in the following | ||
| 235 | section. The members simply appear one after another in the file, | ||
| 236 | with no additional information before, between, or after them. | ||
| 237 | |||
| 238 | 2.3. Member format | ||
| 239 | |||
| 240 | Each member has the following structure: | ||
| 241 | |||
| 242 | +---+---+---+---+---+---+---+---+---+---+ | ||
| 243 | |ID1|ID2|CM |FLG| MTIME |XFL|OS | (more-->) | ||
| 244 | +---+---+---+---+---+---+---+---+---+---+ | ||
| 245 | |||
| 246 | (if FLG.FEXTRA set) | ||
| 247 | |||
| 248 | +---+---+=================================+ | ||
| 249 | | XLEN |...XLEN bytes of "extra field"...| (more-->) | ||
| 250 | +---+---+=================================+ | ||
| 251 | |||
| 252 | (if FLG.FNAME set) | ||
| 253 | |||
| 254 | +=========================================+ | ||
| 255 | |...original file name, zero-terminated...| (more-->) | ||
| 256 | +=========================================+ | ||
| 257 | |||
| 258 | (if FLG.FCOMMENT set) | ||
| 259 | |||
| 260 | +===================================+ | ||
| 261 | |...file comment, zero-terminated...| (more-->) | ||
| 262 | +===================================+ | ||
| 263 | |||
| 264 | (if FLG.FHCRC set) | ||
| 265 | |||
| 266 | +---+---+ | ||
| 267 | | CRC16 | | ||
| 268 | +---+---+ | ||
| 269 | |||
| 270 | +=======================+ | ||
| 271 | |...compressed blocks...| (more-->) | ||
| 272 | +=======================+ | ||
| 273 | |||
| 274 | 0 1 2 3 4 5 6 7 | ||
| 275 | +---+---+---+---+---+---+---+---+ | ||
| 276 | | CRC32 | ISIZE | | ||
| 277 | +---+---+---+---+---+---+---+---+ | ||
| 278 | |||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | Deutsch Informational [Page 5] | ||
| 283 | |||
| 284 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 285 | |||
| 286 | |||
| 287 | 2.3.1. Member header and trailer | ||
| 288 | |||
| 289 | ID1 (IDentification 1) | ||
| 290 | ID2 (IDentification 2) | ||
| 291 | These have the fixed values ID1 = 31 (0x1f, \037), ID2 = 139 | ||
| 292 | (0x8b, \213), to identify the file as being in gzip format. | ||
| 293 | |||
| 294 | CM (Compression Method) | ||
| 295 | This identifies the compression method used in the file. CM | ||
| 296 | = 0-7 are reserved. CM = 8 denotes the "deflate" | ||
| 297 | compression method, which is the one customarily used by | ||
| 298 | gzip and which is documented elsewhere. | ||
| 299 | |||
| 300 | FLG (FLaGs) | ||
| 301 | This flag byte is divided into individual bits as follows: | ||
| 302 | |||
| 303 | bit 0 FTEXT | ||
| 304 | bit 1 FHCRC | ||
| 305 | bit 2 FEXTRA | ||
| 306 | bit 3 FNAME | ||
| 307 | bit 4 FCOMMENT | ||
| 308 | bit 5 reserved | ||
| 309 | bit 6 reserved | ||
| 310 | bit 7 reserved | ||
| 311 | |||
| 312 | If FTEXT is set, the file is probably ASCII text. This is | ||
| 313 | an optional indication, which the compressor may set by | ||
| 314 | checking a small amount of the input data to see whether any | ||
| 315 | non-ASCII characters are present. In case of doubt, FTEXT | ||
| 316 | is cleared, indicating binary data. For systems which have | ||
| 317 | different file formats for ascii text and binary data, the | ||
| 318 | decompressor can use FTEXT to choose the appropriate format. | ||
| 319 | We deliberately do not specify the algorithm used to set | ||
| 320 | this bit, since a compressor always has the option of | ||
| 321 | leaving it cleared and a decompressor always has the option | ||
| 322 | of ignoring it and letting some other program handle issues | ||
| 323 | of data conversion. | ||
| 324 | |||
| 325 | If FHCRC is set, a CRC16 for the gzip header is present, | ||
| 326 | immediately before the compressed data. The CRC16 consists | ||
| 327 | of the two least significant bytes of the CRC32 for all | ||
| 328 | bytes of the gzip header up to and not including the CRC16. | ||
| 329 | [The FHCRC bit was never set by versions of gzip up to | ||
| 330 | 1.2.4, even though it was documented with a different | ||
| 331 | meaning in gzip 1.2.4.] | ||
| 332 | |||
| 333 | If FEXTRA is set, optional extra fields are present, as | ||
| 334 | described in a following section. | ||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 | Deutsch Informational [Page 6] | ||
| 339 | |||
| 340 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 341 | |||
| 342 | |||
| 343 | If FNAME is set, an original file name is present, | ||
| 344 | terminated by a zero byte. The name must consist of ISO | ||
| 345 | 8859-1 (LATIN-1) characters; on operating systems using | ||
| 346 | EBCDIC or any other character set for file names, the name | ||
| 347 | must be translated to the ISO LATIN-1 character set. This | ||
| 348 | is the original name of the file being compressed, with any | ||
| 349 | directory components removed, and, if the file being | ||
| 350 | compressed is on a file system with case insensitive names, | ||
| 351 | forced to lower case. There is no original file name if the | ||
| 352 | data was compressed from a source other than a named file; | ||
| 353 | for example, if the source was stdin on a Unix system, there | ||
| 354 | is no file name. | ||
| 355 | |||
| 356 | If FCOMMENT is set, a zero-terminated file comment is | ||
| 357 | present. This comment is not interpreted; it is only | ||
| 358 | intended for human consumption. The comment must consist of | ||
| 359 | ISO 8859-1 (LATIN-1) characters. Line breaks should be | ||
| 360 | denoted by a single line feed character (10 decimal). | ||
| 361 | |||
| 362 | Reserved FLG bits must be zero. | ||
| 363 | |||
| 364 | MTIME (Modification TIME) | ||
| 365 | This gives the most recent modification time of the original | ||
| 366 | file being compressed. The time is in Unix format, i.e., | ||
| 367 | seconds since 00:00:00 GMT, Jan. 1, 1970. (Note that this | ||
| 368 | may cause problems for MS-DOS and other systems that use | ||
| 369 | local rather than Universal time.) If the compressed data | ||
| 370 | did not come from a file, MTIME is set to the time at which | ||
| 371 | compression started. MTIME = 0 means no time stamp is | ||
| 372 | available. | ||
| 373 | |||
| 374 | XFL (eXtra FLags) | ||
| 375 | These flags are available for use by specific compression | ||
| 376 | methods. The "deflate" method (CM = 8) sets these flags as | ||
| 377 | follows: | ||
| 378 | |||
| 379 | XFL = 2 - compressor used maximum compression, | ||
| 380 | slowest algorithm | ||
| 381 | XFL = 4 - compressor used fastest algorithm | ||
| 382 | |||
| 383 | OS (Operating System) | ||
| 384 | This identifies the type of file system on which compression | ||
| 385 | took place. This may be useful in determining end-of-line | ||
| 386 | convention for text files. The currently defined values are | ||
| 387 | as follows: | ||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | |||
| 394 | Deutsch Informational [Page 7] | ||
| 395 | |||
| 396 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 397 | |||
| 398 | |||
| 399 | 0 - FAT filesystem (MS-DOS, OS/2, NT/Win32) | ||
| 400 | 1 - Amiga | ||
| 401 | 2 - VMS (or OpenVMS) | ||
| 402 | 3 - Unix | ||
| 403 | 4 - VM/CMS | ||
| 404 | 5 - Atari TOS | ||
| 405 | 6 - HPFS filesystem (OS/2, NT) | ||
| 406 | 7 - Macintosh | ||
| 407 | 8 - Z-System | ||
| 408 | 9 - CP/M | ||
| 409 | 10 - TOPS-20 | ||
| 410 | 11 - NTFS filesystem (NT) | ||
| 411 | 12 - QDOS | ||
| 412 | 13 - Acorn RISCOS | ||
| 413 | 255 - unknown | ||
| 414 | |||
| 415 | XLEN (eXtra LENgth) | ||
| 416 | If FLG.FEXTRA is set, this gives the length of the optional | ||
| 417 | extra field. See below for details. | ||
| 418 | |||
| 419 | CRC32 (CRC-32) | ||
| 420 | This contains a Cyclic Redundancy Check value of the | ||
| 421 | uncompressed data computed according to CRC-32 algorithm | ||
| 422 | used in the ISO 3309 standard and in section 8.1.1.6.2 of | ||
| 423 | ITU-T recommendation V.42. (See http://www.iso.ch for | ||
| 424 | ordering ISO documents. See gopher://info.itu.ch for an | ||
| 425 | online version of ITU-T V.42.) | ||
| 426 | |||
| 427 | ISIZE (Input SIZE) | ||
| 428 | This contains the size of the original (uncompressed) input | ||
| 429 | data modulo 2^32. | ||
| 430 | |||
| 431 | 2.3.1.1. Extra field | ||
| 432 | |||
| 433 | If the FLG.FEXTRA bit is set, an "extra field" is present in | ||
| 434 | the header, with total length XLEN bytes. It consists of a | ||
| 435 | series of subfields, each of the form: | ||
| 436 | |||
| 437 | +---+---+---+---+==================================+ | ||
| 438 | |SI1|SI2| LEN |... LEN bytes of subfield data ...| | ||
| 439 | +---+---+---+---+==================================+ | ||
| 440 | |||
| 441 | SI1 and SI2 provide a subfield ID, typically two ASCII letters | ||
| 442 | with some mnemonic value. Jean-Loup Gailly | ||
| 443 | <gzip@prep.ai.mit.edu> is maintaining a registry of subfield | ||
| 444 | IDs; please send him any subfield ID you wish to use. Subfield | ||
| 445 | IDs with SI2 = 0 are reserved for future use. The following | ||
| 446 | IDs are currently defined: | ||
| 447 | |||
| 448 | |||
| 449 | |||
| 450 | Deutsch Informational [Page 8] | ||
| 451 | |||
| 452 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 453 | |||
| 454 | |||
| 455 | SI1 SI2 Data | ||
| 456 | ---------- ---------- ---- | ||
| 457 | 0x41 ('A') 0x70 ('P') Apollo file type information | ||
| 458 | |||
| 459 | LEN gives the length of the subfield data, excluding the 4 | ||
| 460 | initial bytes. | ||
| 461 | |||
| 462 | 2.3.1.2. Compliance | ||
| 463 | |||
| 464 | A compliant compressor must produce files with correct ID1, | ||
| 465 | ID2, CM, CRC32, and ISIZE, but may set all the other fields in | ||
| 466 | the fixed-length part of the header to default values (255 for | ||
| 467 | OS, 0 for all others). The compressor must set all reserved | ||
| 468 | bits to zero. | ||
| 469 | |||
| 470 | A compliant decompressor must check ID1, ID2, and CM, and | ||
| 471 | provide an error indication if any of these have incorrect | ||
| 472 | values. It must examine FEXTRA/XLEN, FNAME, FCOMMENT and FHCRC | ||
| 473 | at least so it can skip over the optional fields if they are | ||
| 474 | present. It need not examine any other part of the header or | ||
| 475 | trailer; in particular, a decompressor may ignore FTEXT and OS | ||
| 476 | and always produce binary output, and still be compliant. A | ||
| 477 | compliant decompressor must give an error indication if any | ||
| 478 | reserved bit is non-zero, since such a bit could indicate the | ||
| 479 | presence of a new field that would cause subsequent data to be | ||
| 480 | interpreted incorrectly. | ||
| 481 | |||
| 482 | 3. References | ||
| 483 | |||
| 484 | [1] "Information Processing - 8-bit single-byte coded graphic | ||
| 485 | character sets - Part 1: Latin alphabet No.1" (ISO 8859-1:1987). | ||
| 486 | The ISO 8859-1 (Latin-1) character set is a superset of 7-bit | ||
| 487 | ASCII. Files defining this character set are available as | ||
| 488 | iso_8859-1.* in ftp://ftp.uu.net/graphics/png/documents/ | ||
| 489 | |||
| 490 | [2] ISO 3309 | ||
| 491 | |||
| 492 | [3] ITU-T recommendation V.42 | ||
| 493 | |||
| 494 | [4] Deutsch, L.P.,"DEFLATE Compressed Data Format Specification", | ||
| 495 | available in ftp://ftp.uu.net/pub/archiving/zip/doc/ | ||
| 496 | |||
| 497 | [5] Gailly, J.-L., GZIP documentation, available as gzip-*.tar in | ||
| 498 | ftp://prep.ai.mit.edu/pub/gnu/ | ||
| 499 | |||
| 500 | [6] Sarwate, D.V., "Computation of Cyclic Redundancy Checks via Table | ||
| 501 | Look-Up", Communications of the ACM, 31(8), pp.1008-1013. | ||
| 502 | |||
| 503 | |||
| 504 | |||
| 505 | |||
| 506 | Deutsch Informational [Page 9] | ||
| 507 | |||
| 508 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 509 | |||
| 510 | |||
| 511 | [7] Schwaderer, W.D., "CRC Calculation", April 85 PC Tech Journal, | ||
| 512 | pp.118-133. | ||
| 513 | |||
| 514 | [8] ftp://ftp.adelaide.edu.au/pub/rocksoft/papers/crc_v3.txt, | ||
| 515 | describing the CRC concept. | ||
| 516 | |||
| 517 | 4. Security Considerations | ||
| 518 | |||
| 519 | Any data compression method involves the reduction of redundancy in | ||
| 520 | the data. Consequently, any corruption of the data is likely to have | ||
| 521 | severe effects and be difficult to correct. Uncompressed text, on | ||
| 522 | the other hand, will probably still be readable despite the presence | ||
| 523 | of some corrupted bytes. | ||
| 524 | |||
| 525 | It is recommended that systems using this data format provide some | ||
| 526 | means of validating the integrity of the compressed data, such as by | ||
| 527 | setting and checking the CRC-32 check value. | ||
| 528 | |||
| 529 | 5. Acknowledgements | ||
| 530 | |||
| 531 | Trademarks cited in this document are the property of their | ||
| 532 | respective owners. | ||
| 533 | |||
| 534 | Jean-Loup Gailly designed the gzip format and wrote, with Mark Adler, | ||
| 535 | the related software described in this specification. Glenn | ||
| 536 | Randers-Pehrson converted this document to RFC and HTML format. | ||
| 537 | |||
| 538 | 6. Author's Address | ||
| 539 | |||
| 540 | L. Peter Deutsch | ||
| 541 | Aladdin Enterprises | ||
| 542 | 203 Santa Margarita Ave. | ||
| 543 | Menlo Park, CA 94025 | ||
| 544 | |||
| 545 | Phone: (415) 322-0103 (AM only) | ||
| 546 | FAX: (415) 322-1734 | ||
| 547 | EMail: <ghost@aladdin.com> | ||
| 548 | |||
| 549 | Questions about the technical content of this specification can be | ||
| 550 | sent by email to: | ||
| 551 | |||
| 552 | Jean-Loup Gailly <gzip@prep.ai.mit.edu> and | ||
| 553 | Mark Adler <madler@alumni.caltech.edu> | ||
| 554 | |||
| 555 | Editorial comments on this specification can be sent by email to: | ||
| 556 | |||
| 557 | L. Peter Deutsch <ghost@aladdin.com> and | ||
| 558 | Glenn Randers-Pehrson <randeg@alumni.rpi.edu> | ||
| 559 | |||
| 560 | |||
| 561 | |||
| 562 | Deutsch Informational [Page 10] | ||
| 563 | |||
| 564 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 565 | |||
| 566 | |||
| 567 | 7. Appendix: Jean-Loup Gailly's gzip utility | ||
| 568 | |||
| 569 | The most widely used implementation of gzip compression, and the | ||
| 570 | original documentation on which this specification is based, were | ||
| 571 | created by Jean-Loup Gailly <gzip@prep.ai.mit.edu>. Since this | ||
| 572 | implementation is a de facto standard, we mention some more of its | ||
| 573 | features here. Again, the material in this section is not part of | ||
| 574 | the specification per se, and implementations need not follow it to | ||
| 575 | be compliant. | ||
| 576 | |||
| 577 | When compressing or decompressing a file, gzip preserves the | ||
| 578 | protection, ownership, and modification time attributes on the local | ||
| 579 | file system, since there is no provision for representing protection | ||
| 580 | attributes in the gzip file format itself. Since the file format | ||
| 581 | includes a modification time, the gzip decompressor provides a | ||
| 582 | command line switch that assigns the modification time from the file, | ||
| 583 | rather than the local modification time of the compressed input, to | ||
| 584 | the decompressed output. | ||
| 585 | |||
| 586 | 8. Appendix: Sample CRC Code | ||
| 587 | |||
| 588 | The following sample code represents a practical implementation of | ||
| 589 | the CRC (Cyclic Redundancy Check). (See also ISO 3309 and ITU-T V.42 | ||
| 590 | for a formal specification.) | ||
| 591 | |||
| 592 | The sample code is in the ANSI C programming language. Non C users | ||
| 593 | may find it easier to read with these hints: | ||
| 594 | |||
| 595 | & Bitwise AND operator. | ||
| 596 | ^ Bitwise exclusive-OR operator. | ||
| 597 | >> Bitwise right shift operator. When applied to an | ||
| 598 | unsigned quantity, as here, right shift inserts zero | ||
| 599 | bit(s) at the left. | ||
| 600 | ! Logical NOT operator. | ||
| 601 | ++ "n++" increments the variable n. | ||
| 602 | 0xNNN 0x introduces a hexadecimal (base 16) constant. | ||
| 603 | Suffix L indicates a long value (at least 32 bits). | ||
| 604 | |||
| 605 | /* Table of CRCs of all 8-bit messages. */ | ||
| 606 | unsigned long crc_table[256]; | ||
| 607 | |||
| 608 | /* Flag: has the table been computed? Initially false. */ | ||
| 609 | int crc_table_computed = 0; | ||
| 610 | |||
| 611 | /* Make the table for a fast CRC. */ | ||
| 612 | void make_crc_table(void) | ||
| 613 | { | ||
| 614 | unsigned long c; | ||
| 615 | |||
| 616 | |||
| 617 | |||
| 618 | Deutsch Informational [Page 11] | ||
| 619 | |||
| 620 | RFC 1952 GZIP File Format Specification May 1996 | ||
| 621 | |||
| 622 | |||
| 623 | int n, k; | ||
| 624 | for (n = 0; n < 256; n++) { | ||
| 625 | c = (unsigned long) n; | ||
| 626 | for (k = 0; k < 8; k++) { | ||
| 627 | if (c & 1) { | ||
| 628 | c = 0xedb88320L ^ (c >> 1); | ||
| 629 | } else { | ||
| 630 | c = c >> 1; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | crc_table[n] = c; | ||
| 634 | } | ||
| 635 | crc_table_computed = 1; | ||
| 636 | } | ||
| 637 | |||
| 638 | /* | ||
| 639 | Update a running crc with the bytes buf[0..len-1] and return | ||
| 640 | the updated crc. The crc should be initialized to zero. Pre- and | ||
| 641 | post-conditioning (one's complement) is performed within this | ||
| 642 | function so it shouldn't be done by the caller. Usage example: | ||
| 643 | |||
| 644 | unsigned long crc = 0L; | ||
| 645 | |||
| 646 | while (read_buffer(buffer, length) != EOF) { | ||
| 647 | crc = update_crc(crc, buffer, length); | ||
| 648 | } | ||
| 649 | if (crc != original_crc) error(); | ||
| 650 | */ | ||
| 651 | unsigned long update_crc(unsigned long crc, | ||
| 652 | unsigned char *buf, int len) | ||
| 653 | { | ||
| 654 | unsigned long c = crc ^ 0xffffffffL; | ||
| 655 | int n; | ||
| 656 | |||
| 657 | if (!crc_table_computed) | ||
| 658 | make_crc_table(); | ||
| 659 | for (n = 0; n < len; n++) { | ||
| 660 | c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8); | ||
| 661 | } | ||
| 662 | return c ^ 0xffffffffL; | ||
| 663 | } | ||
| 664 | |||
| 665 | /* Return the CRC of the bytes buf[0..len-1]. */ | ||
| 666 | unsigned long crc(unsigned char *buf, int len) | ||
| 667 | { | ||
| 668 | return update_crc(0L, buf, len); | ||
| 669 | } | ||
| 670 | |||
| 671 | |||
| 672 | |||
| 673 | |||
| 674 | Deutsch Informational [Page 12] | ||
| 675 | |||
lib/std/compress/testdata/rfc1952.txt.gz created| Binary files /dev/null and b/lib/std/compress/testdata/rfc1952.txt.gz differ | |||
lib/std/compress/zlib.zig+4-4| ... | @@ -107,21 +107,21 @@ fn testReader(data: []const u8, expected: []const u8) !void { | ... | @@ -107,21 +107,21 @@ fn testReader(data: []const u8, expected: []const u8) !void { |
| 107 | // https://tools.ietf.org/rfc/rfc1951.txt length=36944 bytes | 107 | // https://tools.ietf.org/rfc/rfc1951.txt length=36944 bytes |
| 108 | // SHA256=5ebf4b5b7fe1c3a0c0ab9aa3ac8c0f3853a7dc484905e76e03b0b0f301350009 | 108 | // SHA256=5ebf4b5b7fe1c3a0c0ab9aa3ac8c0f3853a7dc484905e76e03b0b0f301350009 |
| 109 | test "compressed data" { | 109 | test "compressed data" { |
| 110 | const rfc1951_txt = @embedFile("rfc1951.txt"); | 110 | const rfc1951_txt = @embedFile("testdata/rfc1951.txt"); |
| 111 | 111 | ||
| 112 | // Compressed with compression level = 0 | 112 | // Compressed with compression level = 0 |
| 113 | try testReader( | 113 | try testReader( |
| 114 | @embedFile("rfc1951.txt.z.0"), | 114 | @embedFile("testdata/rfc1951.txt.z.0"), |
| 115 | rfc1951_txt, | 115 | rfc1951_txt, |
| 116 | ); | 116 | ); |
| 117 | // Compressed with compression level = 9 | 117 | // Compressed with compression level = 9 |
| 118 | try testReader( | 118 | try testReader( |
| 119 | @embedFile("rfc1951.txt.z.9"), | 119 | @embedFile("testdata/rfc1951.txt.z.9"), |
| 120 | rfc1951_txt, | 120 | rfc1951_txt, |
| 121 | ); | 121 | ); |
| 122 | // Compressed with compression level = 9 and fixed Huffman codes | 122 | // Compressed with compression level = 9 and fixed Huffman codes |
| 123 | try testReader( | 123 | try testReader( |
| 124 | @embedFile("rfc1951.txt.fixed.z.9"), | 124 | @embedFile("testdata/rfc1951.txt.fixed.z.9"), |
| 125 | rfc1951_txt, | 125 | rfc1951_txt, |
| 126 | ); | 126 | ); |
| 127 | } | 127 | } |