annotate zlib/doc/rfc1952.txt @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 Network Working Group P. Deutsch
kono
parents:
diff changeset
8 Request for Comments: 1952 Aladdin Enterprises
kono
parents:
diff changeset
9 Category: Informational May 1996
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GZIP file format specification version 4.3
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 Status of This Memo
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 This memo provides information for the Internet community. This memo
kono
parents:
diff changeset
17 does not specify an Internet standard of any kind. Distribution of
kono
parents:
diff changeset
18 this memo is unlimited.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 IESG Note:
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 The IESG takes no position on the validity of any Intellectual
kono
parents:
diff changeset
23 Property Rights statements contained in this document.
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 Notices
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 Copyright (c) 1996 L. Peter Deutsch
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 Permission is granted to copy and distribute this document for any
kono
parents:
diff changeset
30 purpose and without charge, including translations into other
kono
parents:
diff changeset
31 languages and incorporation into compilations, provided that the
kono
parents:
diff changeset
32 copyright notice and this notice are preserved, and that any
kono
parents:
diff changeset
33 substantive changes or deletions from the original are clearly
kono
parents:
diff changeset
34 marked.
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 A pointer to the latest version of this and related documentation in
kono
parents:
diff changeset
37 HTML format can be found at the URL
kono
parents:
diff changeset
38 <ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>.
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 Abstract
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 This specification defines a lossless compressed data format that is
kono
parents:
diff changeset
43 compatible with the widely used GZIP utility. The format includes a
kono
parents:
diff changeset
44 cyclic redundancy check value for detecting data corruption. The
kono
parents:
diff changeset
45 format presently uses the DEFLATE method of compression but can be
kono
parents:
diff changeset
46 easily extended to use other compression methods. The format can be
kono
parents:
diff changeset
47 implemented readily in a manner not covered by patents.
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 Deutsch Informational [Page 1]
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 Table of Contents
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 1. Introduction ................................................... 2
kono
parents:
diff changeset
66 1.1. Purpose ................................................... 2
kono
parents:
diff changeset
67 1.2. Intended audience ......................................... 3
kono
parents:
diff changeset
68 1.3. Scope ..................................................... 3
kono
parents:
diff changeset
69 1.4. Compliance ................................................ 3
kono
parents:
diff changeset
70 1.5. Definitions of terms and conventions used ................. 3
kono
parents:
diff changeset
71 1.6. Changes from previous versions ............................ 3
kono
parents:
diff changeset
72 2. Detailed specification ......................................... 4
kono
parents:
diff changeset
73 2.1. Overall conventions ....................................... 4
kono
parents:
diff changeset
74 2.2. File format ............................................... 5
kono
parents:
diff changeset
75 2.3. Member format ............................................. 5
kono
parents:
diff changeset
76 2.3.1. Member header and trailer ........................... 6
kono
parents:
diff changeset
77 2.3.1.1. Extra field ................................... 8
kono
parents:
diff changeset
78 2.3.1.2. Compliance .................................... 9
kono
parents:
diff changeset
79 3. References .................................................. 9
kono
parents:
diff changeset
80 4. Security Considerations .................................... 10
kono
parents:
diff changeset
81 5. Acknowledgements ........................................... 10
kono
parents:
diff changeset
82 6. Author's Address ........................................... 10
kono
parents:
diff changeset
83 7. Appendix: Jean-Loup Gailly's gzip utility .................. 11
kono
parents:
diff changeset
84 8. Appendix: Sample CRC Code .................................. 11
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 1. Introduction
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 1.1. Purpose
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 The purpose of this specification is to define a lossless
kono
parents:
diff changeset
91 compressed data format that:
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 * Is independent of CPU type, operating system, file system,
kono
parents:
diff changeset
94 and character set, and hence can be used for interchange;
kono
parents:
diff changeset
95 * Can compress or decompress a data stream (as opposed to a
kono
parents:
diff changeset
96 randomly accessible file) to produce another data stream,
kono
parents:
diff changeset
97 using only an a priori bounded amount of intermediate
kono
parents:
diff changeset
98 storage, and hence can be used in data communications or
kono
parents:
diff changeset
99 similar structures such as Unix filters;
kono
parents:
diff changeset
100 * Compresses data with efficiency comparable to the best
kono
parents:
diff changeset
101 currently available general-purpose compression methods,
kono
parents:
diff changeset
102 and in particular considerably better than the "compress"
kono
parents:
diff changeset
103 program;
kono
parents:
diff changeset
104 * Can be implemented readily in a manner not covered by
kono
parents:
diff changeset
105 patents, and hence can be practiced freely;
kono
parents:
diff changeset
106 * Is compatible with the file format produced by the current
kono
parents:
diff changeset
107 widely used gzip utility, in that conforming decompressors
kono
parents:
diff changeset
108 will be able to read data produced by the existing gzip
kono
parents:
diff changeset
109 compressor.
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 Deutsch Informational [Page 2]
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 The data format defined by this specification does not attempt to:
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 * Provide random access to compressed data;
kono
parents:
diff changeset
122 * Compress specialized data (e.g., raster graphics) as well as
kono
parents:
diff changeset
123 the best currently available specialized algorithms.
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 1.2. Intended audience
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 This specification is intended for use by implementors of software
kono
parents:
diff changeset
128 to compress data into gzip format and/or decompress data from gzip
kono
parents:
diff changeset
129 format.
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 The text of the specification assumes a basic background in
kono
parents:
diff changeset
132 programming at the level of bits and other primitive data
kono
parents:
diff changeset
133 representations.
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 1.3. Scope
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 The specification specifies a compression method and a file format
kono
parents:
diff changeset
138 (the latter assuming only that a file can store a sequence of
kono
parents:
diff changeset
139 arbitrary bytes). It does not specify any particular interface to
kono
parents:
diff changeset
140 a file system or anything about character sets or encodings
kono
parents:
diff changeset
141 (except for file names and comments, which are optional).
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 1.4. Compliance
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 Unless otherwise indicated below, a compliant decompressor must be
kono
parents:
diff changeset
146 able to accept and decompress any file that conforms to all the
kono
parents:
diff changeset
147 specifications presented here; a compliant compressor must produce
kono
parents:
diff changeset
148 files that conform to all the specifications presented here. The
kono
parents:
diff changeset
149 material in the appendices is not part of the specification per se
kono
parents:
diff changeset
150 and is not relevant to compliance.
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 1.5. Definitions of terms and conventions used
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 byte: 8 bits stored or transmitted as a unit (same as an octet).
kono
parents:
diff changeset
155 (For this specification, a byte is exactly 8 bits, even on
kono
parents:
diff changeset
156 machines which store a character on a number of bits different
kono
parents:
diff changeset
157 from 8.) See below for the numbering of bits within a byte.
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 1.6. Changes from previous versions
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 There have been no technical changes to the gzip format since
kono
parents:
diff changeset
162 version 4.1 of this specification. In version 4.2, some
kono
parents:
diff changeset
163 terminology was changed, and the sample CRC code was rewritten for
kono
parents:
diff changeset
164 clarity and to eliminate the requirement for the caller to do pre-
kono
parents:
diff changeset
165 and post-conditioning. Version 4.3 is a conversion of the
kono
parents:
diff changeset
166 specification to RFC style.
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 Deutsch Informational [Page 3]
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 2. Detailed specification
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 2.1. Overall conventions
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 In the diagrams below, a box like this:
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 +---+
kono
parents:
diff changeset
182 | | <-- the vertical bars might be missing
kono
parents:
diff changeset
183 +---+
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 represents one byte; a box like this:
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 +==============+
kono
parents:
diff changeset
188 | |
kono
parents:
diff changeset
189 +==============+
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 represents a variable number of bytes.
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 Bytes stored within a computer do not have a "bit order", since
kono
parents:
diff changeset
194 they are always treated as a unit. However, a byte considered as
kono
parents:
diff changeset
195 an integer between 0 and 255 does have a most- and least-
kono
parents:
diff changeset
196 significant bit, and since we write numbers with the most-
kono
parents:
diff changeset
197 significant digit on the left, we also write bytes with the most-
kono
parents:
diff changeset
198 significant bit on the left. In the diagrams below, we number the
kono
parents:
diff changeset
199 bits of a byte so that bit 0 is the least-significant bit, i.e.,
kono
parents:
diff changeset
200 the bits are numbered:
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 +--------+
kono
parents:
diff changeset
203 |76543210|
kono
parents:
diff changeset
204 +--------+
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 This document does not address the issue of the order in which
kono
parents:
diff changeset
207 bits of a byte are transmitted on a bit-sequential medium, since
kono
parents:
diff changeset
208 the data format described here is byte- rather than bit-oriented.
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 Within a computer, a number may occupy multiple bytes. All
kono
parents:
diff changeset
211 multi-byte numbers in the format described here are stored with
kono
parents:
diff changeset
212 the least-significant byte first (at the lower memory address).
kono
parents:
diff changeset
213 For example, the decimal number 520 is stored as:
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 0 1
kono
parents:
diff changeset
216 +--------+--------+
kono
parents:
diff changeset
217 |00001000|00000010|
kono
parents:
diff changeset
218 +--------+--------+
kono
parents:
diff changeset
219 ^ ^
kono
parents:
diff changeset
220 | |
kono
parents:
diff changeset
221 | + more significant byte = 2 x 256
kono
parents:
diff changeset
222 + less significant byte = 8
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225
kono
parents:
diff changeset
226 Deutsch Informational [Page 4]
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
229
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 2.2. File format
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 A gzip file consists of a series of "members" (compressed data
kono
parents:
diff changeset
234 sets). The format of each member is specified in the following
kono
parents:
diff changeset
235 section. The members simply appear one after another in the file,
kono
parents:
diff changeset
236 with no additional information before, between, or after them.
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 2.3. Member format
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 Each member has the following structure:
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 +---+---+---+---+---+---+---+---+---+---+
kono
parents:
diff changeset
243 |ID1|ID2|CM |FLG| MTIME |XFL|OS | (more-->)
kono
parents:
diff changeset
244 +---+---+---+---+---+---+---+---+---+---+
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 (if FLG.FEXTRA set)
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 +---+---+=================================+
kono
parents:
diff changeset
249 | XLEN |...XLEN bytes of "extra field"...| (more-->)
kono
parents:
diff changeset
250 +---+---+=================================+
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 (if FLG.FNAME set)
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 +=========================================+
kono
parents:
diff changeset
255 |...original file name, zero-terminated...| (more-->)
kono
parents:
diff changeset
256 +=========================================+
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 (if FLG.FCOMMENT set)
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 +===================================+
kono
parents:
diff changeset
261 |...file comment, zero-terminated...| (more-->)
kono
parents:
diff changeset
262 +===================================+
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 (if FLG.FHCRC set)
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 +---+---+
kono
parents:
diff changeset
267 | CRC16 |
kono
parents:
diff changeset
268 +---+---+
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 +=======================+
kono
parents:
diff changeset
271 |...compressed blocks...| (more-->)
kono
parents:
diff changeset
272 +=======================+
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 0 1 2 3 4 5 6 7
kono
parents:
diff changeset
275 +---+---+---+---+---+---+---+---+
kono
parents:
diff changeset
276 | CRC32 | ISIZE |
kono
parents:
diff changeset
277 +---+---+---+---+---+---+---+---+
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 Deutsch Informational [Page 5]
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 2.3.1. Member header and trailer
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 ID1 (IDentification 1)
kono
parents:
diff changeset
290 ID2 (IDentification 2)
kono
parents:
diff changeset
291 These have the fixed values ID1 = 31 (0x1f, \037), ID2 = 139
kono
parents:
diff changeset
292 (0x8b, \213), to identify the file as being in gzip format.
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 CM (Compression Method)
kono
parents:
diff changeset
295 This identifies the compression method used in the file. CM
kono
parents:
diff changeset
296 = 0-7 are reserved. CM = 8 denotes the "deflate"
kono
parents:
diff changeset
297 compression method, which is the one customarily used by
kono
parents:
diff changeset
298 gzip and which is documented elsewhere.
kono
parents:
diff changeset
299
kono
parents:
diff changeset
300 FLG (FLaGs)
kono
parents:
diff changeset
301 This flag byte is divided into individual bits as follows:
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 bit 0 FTEXT
kono
parents:
diff changeset
304 bit 1 FHCRC
kono
parents:
diff changeset
305 bit 2 FEXTRA
kono
parents:
diff changeset
306 bit 3 FNAME
kono
parents:
diff changeset
307 bit 4 FCOMMENT
kono
parents:
diff changeset
308 bit 5 reserved
kono
parents:
diff changeset
309 bit 6 reserved
kono
parents:
diff changeset
310 bit 7 reserved
kono
parents:
diff changeset
311
kono
parents:
diff changeset
312 If FTEXT is set, the file is probably ASCII text. This is
kono
parents:
diff changeset
313 an optional indication, which the compressor may set by
kono
parents:
diff changeset
314 checking a small amount of the input data to see whether any
kono
parents:
diff changeset
315 non-ASCII characters are present. In case of doubt, FTEXT
kono
parents:
diff changeset
316 is cleared, indicating binary data. For systems which have
kono
parents:
diff changeset
317 different file formats for ascii text and binary data, the
kono
parents:
diff changeset
318 decompressor can use FTEXT to choose the appropriate format.
kono
parents:
diff changeset
319 We deliberately do not specify the algorithm used to set
kono
parents:
diff changeset
320 this bit, since a compressor always has the option of
kono
parents:
diff changeset
321 leaving it cleared and a decompressor always has the option
kono
parents:
diff changeset
322 of ignoring it and letting some other program handle issues
kono
parents:
diff changeset
323 of data conversion.
kono
parents:
diff changeset
324
kono
parents:
diff changeset
325 If FHCRC is set, a CRC16 for the gzip header is present,
kono
parents:
diff changeset
326 immediately before the compressed data. The CRC16 consists
kono
parents:
diff changeset
327 of the two least significant bytes of the CRC32 for all
kono
parents:
diff changeset
328 bytes of the gzip header up to and not including the CRC16.
kono
parents:
diff changeset
329 [The FHCRC bit was never set by versions of gzip up to
kono
parents:
diff changeset
330 1.2.4, even though it was documented with a different
kono
parents:
diff changeset
331 meaning in gzip 1.2.4.]
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 If FEXTRA is set, optional extra fields are present, as
kono
parents:
diff changeset
334 described in a following section.
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337
kono
parents:
diff changeset
338 Deutsch Informational [Page 6]
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
341
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 If FNAME is set, an original file name is present,
kono
parents:
diff changeset
344 terminated by a zero byte. The name must consist of ISO
kono
parents:
diff changeset
345 8859-1 (LATIN-1) characters; on operating systems using
kono
parents:
diff changeset
346 EBCDIC or any other character set for file names, the name
kono
parents:
diff changeset
347 must be translated to the ISO LATIN-1 character set. This
kono
parents:
diff changeset
348 is the original name of the file being compressed, with any
kono
parents:
diff changeset
349 directory components removed, and, if the file being
kono
parents:
diff changeset
350 compressed is on a file system with case insensitive names,
kono
parents:
diff changeset
351 forced to lower case. There is no original file name if the
kono
parents:
diff changeset
352 data was compressed from a source other than a named file;
kono
parents:
diff changeset
353 for example, if the source was stdin on a Unix system, there
kono
parents:
diff changeset
354 is no file name.
kono
parents:
diff changeset
355
kono
parents:
diff changeset
356 If FCOMMENT is set, a zero-terminated file comment is
kono
parents:
diff changeset
357 present. This comment is not interpreted; it is only
kono
parents:
diff changeset
358 intended for human consumption. The comment must consist of
kono
parents:
diff changeset
359 ISO 8859-1 (LATIN-1) characters. Line breaks should be
kono
parents:
diff changeset
360 denoted by a single line feed character (10 decimal).
kono
parents:
diff changeset
361
kono
parents:
diff changeset
362 Reserved FLG bits must be zero.
kono
parents:
diff changeset
363
kono
parents:
diff changeset
364 MTIME (Modification TIME)
kono
parents:
diff changeset
365 This gives the most recent modification time of the original
kono
parents:
diff changeset
366 file being compressed. The time is in Unix format, i.e.,
kono
parents:
diff changeset
367 seconds since 00:00:00 GMT, Jan. 1, 1970. (Note that this
kono
parents:
diff changeset
368 may cause problems for MS-DOS and other systems that use
kono
parents:
diff changeset
369 local rather than Universal time.) If the compressed data
kono
parents:
diff changeset
370 did not come from a file, MTIME is set to the time at which
kono
parents:
diff changeset
371 compression started. MTIME = 0 means no time stamp is
kono
parents:
diff changeset
372 available.
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 XFL (eXtra FLags)
kono
parents:
diff changeset
375 These flags are available for use by specific compression
kono
parents:
diff changeset
376 methods. The "deflate" method (CM = 8) sets these flags as
kono
parents:
diff changeset
377 follows:
kono
parents:
diff changeset
378
kono
parents:
diff changeset
379 XFL = 2 - compressor used maximum compression,
kono
parents:
diff changeset
380 slowest algorithm
kono
parents:
diff changeset
381 XFL = 4 - compressor used fastest algorithm
kono
parents:
diff changeset
382
kono
parents:
diff changeset
383 OS (Operating System)
kono
parents:
diff changeset
384 This identifies the type of file system on which compression
kono
parents:
diff changeset
385 took place. This may be useful in determining end-of-line
kono
parents:
diff changeset
386 convention for text files. The currently defined values are
kono
parents:
diff changeset
387 as follows:
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390
kono
parents:
diff changeset
391
kono
parents:
diff changeset
392
kono
parents:
diff changeset
393
kono
parents:
diff changeset
394 Deutsch Informational [Page 7]
kono
parents:
diff changeset
395
kono
parents:
diff changeset
396 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 0 - FAT filesystem (MS-DOS, OS/2, NT/Win32)
kono
parents:
diff changeset
400 1 - Amiga
kono
parents:
diff changeset
401 2 - VMS (or OpenVMS)
kono
parents:
diff changeset
402 3 - Unix
kono
parents:
diff changeset
403 4 - VM/CMS
kono
parents:
diff changeset
404 5 - Atari TOS
kono
parents:
diff changeset
405 6 - HPFS filesystem (OS/2, NT)
kono
parents:
diff changeset
406 7 - Macintosh
kono
parents:
diff changeset
407 8 - Z-System
kono
parents:
diff changeset
408 9 - CP/M
kono
parents:
diff changeset
409 10 - TOPS-20
kono
parents:
diff changeset
410 11 - NTFS filesystem (NT)
kono
parents:
diff changeset
411 12 - QDOS
kono
parents:
diff changeset
412 13 - Acorn RISCOS
kono
parents:
diff changeset
413 255 - unknown
kono
parents:
diff changeset
414
kono
parents:
diff changeset
415 XLEN (eXtra LENgth)
kono
parents:
diff changeset
416 If FLG.FEXTRA is set, this gives the length of the optional
kono
parents:
diff changeset
417 extra field. See below for details.
kono
parents:
diff changeset
418
kono
parents:
diff changeset
419 CRC32 (CRC-32)
kono
parents:
diff changeset
420 This contains a Cyclic Redundancy Check value of the
kono
parents:
diff changeset
421 uncompressed data computed according to CRC-32 algorithm
kono
parents:
diff changeset
422 used in the ISO 3309 standard and in section 8.1.1.6.2 of
kono
parents:
diff changeset
423 ITU-T recommendation V.42. (See http://www.iso.ch for
kono
parents:
diff changeset
424 ordering ISO documents. See gopher://info.itu.ch for an
kono
parents:
diff changeset
425 online version of ITU-T V.42.)
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 ISIZE (Input SIZE)
kono
parents:
diff changeset
428 This contains the size of the original (uncompressed) input
kono
parents:
diff changeset
429 data modulo 2^32.
kono
parents:
diff changeset
430
kono
parents:
diff changeset
431 2.3.1.1. Extra field
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 If the FLG.FEXTRA bit is set, an "extra field" is present in
kono
parents:
diff changeset
434 the header, with total length XLEN bytes. It consists of a
kono
parents:
diff changeset
435 series of subfields, each of the form:
kono
parents:
diff changeset
436
kono
parents:
diff changeset
437 +---+---+---+---+==================================+
kono
parents:
diff changeset
438 |SI1|SI2| LEN |... LEN bytes of subfield data ...|
kono
parents:
diff changeset
439 +---+---+---+---+==================================+
kono
parents:
diff changeset
440
kono
parents:
diff changeset
441 SI1 and SI2 provide a subfield ID, typically two ASCII letters
kono
parents:
diff changeset
442 with some mnemonic value. Jean-Loup Gailly
kono
parents:
diff changeset
443 <gzip@prep.ai.mit.edu> is maintaining a registry of subfield
kono
parents:
diff changeset
444 IDs; please send him any subfield ID you wish to use. Subfield
kono
parents:
diff changeset
445 IDs with SI2 = 0 are reserved for future use. The following
kono
parents:
diff changeset
446 IDs are currently defined:
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 Deutsch Informational [Page 8]
kono
parents:
diff changeset
451
kono
parents:
diff changeset
452 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
453
kono
parents:
diff changeset
454
kono
parents:
diff changeset
455 SI1 SI2 Data
kono
parents:
diff changeset
456 ---------- ---------- ----
kono
parents:
diff changeset
457 0x41 ('A') 0x70 ('P') Apollo file type information
kono
parents:
diff changeset
458
kono
parents:
diff changeset
459 LEN gives the length of the subfield data, excluding the 4
kono
parents:
diff changeset
460 initial bytes.
kono
parents:
diff changeset
461
kono
parents:
diff changeset
462 2.3.1.2. Compliance
kono
parents:
diff changeset
463
kono
parents:
diff changeset
464 A compliant compressor must produce files with correct ID1,
kono
parents:
diff changeset
465 ID2, CM, CRC32, and ISIZE, but may set all the other fields in
kono
parents:
diff changeset
466 the fixed-length part of the header to default values (255 for
kono
parents:
diff changeset
467 OS, 0 for all others). The compressor must set all reserved
kono
parents:
diff changeset
468 bits to zero.
kono
parents:
diff changeset
469
kono
parents:
diff changeset
470 A compliant decompressor must check ID1, ID2, and CM, and
kono
parents:
diff changeset
471 provide an error indication if any of these have incorrect
kono
parents:
diff changeset
472 values. It must examine FEXTRA/XLEN, FNAME, FCOMMENT and FHCRC
kono
parents:
diff changeset
473 at least so it can skip over the optional fields if they are
kono
parents:
diff changeset
474 present. It need not examine any other part of the header or
kono
parents:
diff changeset
475 trailer; in particular, a decompressor may ignore FTEXT and OS
kono
parents:
diff changeset
476 and always produce binary output, and still be compliant. A
kono
parents:
diff changeset
477 compliant decompressor must give an error indication if any
kono
parents:
diff changeset
478 reserved bit is non-zero, since such a bit could indicate the
kono
parents:
diff changeset
479 presence of a new field that would cause subsequent data to be
kono
parents:
diff changeset
480 interpreted incorrectly.
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 3. References
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 [1] "Information Processing - 8-bit single-byte coded graphic
kono
parents:
diff changeset
485 character sets - Part 1: Latin alphabet No.1" (ISO 8859-1:1987).
kono
parents:
diff changeset
486 The ISO 8859-1 (Latin-1) character set is a superset of 7-bit
kono
parents:
diff changeset
487 ASCII. Files defining this character set are available as
kono
parents:
diff changeset
488 iso_8859-1.* in ftp://ftp.uu.net/graphics/png/documents/
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490 [2] ISO 3309
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 [3] ITU-T recommendation V.42
kono
parents:
diff changeset
493
kono
parents:
diff changeset
494 [4] Deutsch, L.P.,"DEFLATE Compressed Data Format Specification",
kono
parents:
diff changeset
495 available in ftp://ftp.uu.net/pub/archiving/zip/doc/
kono
parents:
diff changeset
496
kono
parents:
diff changeset
497 [5] Gailly, J.-L., GZIP documentation, available as gzip-*.tar in
kono
parents:
diff changeset
498 ftp://prep.ai.mit.edu/pub/gnu/
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 [6] Sarwate, D.V., "Computation of Cyclic Redundancy Checks via Table
kono
parents:
diff changeset
501 Look-Up", Communications of the ACM, 31(8), pp.1008-1013.
kono
parents:
diff changeset
502
kono
parents:
diff changeset
503
kono
parents:
diff changeset
504
kono
parents:
diff changeset
505
kono
parents:
diff changeset
506 Deutsch Informational [Page 9]
kono
parents:
diff changeset
507
kono
parents:
diff changeset
508 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510
kono
parents:
diff changeset
511 [7] Schwaderer, W.D., "CRC Calculation", April 85 PC Tech Journal,
kono
parents:
diff changeset
512 pp.118-133.
kono
parents:
diff changeset
513
kono
parents:
diff changeset
514 [8] ftp://ftp.adelaide.edu.au/pub/rocksoft/papers/crc_v3.txt,
kono
parents:
diff changeset
515 describing the CRC concept.
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 4. Security Considerations
kono
parents:
diff changeset
518
kono
parents:
diff changeset
519 Any data compression method involves the reduction of redundancy in
kono
parents:
diff changeset
520 the data. Consequently, any corruption of the data is likely to have
kono
parents:
diff changeset
521 severe effects and be difficult to correct. Uncompressed text, on
kono
parents:
diff changeset
522 the other hand, will probably still be readable despite the presence
kono
parents:
diff changeset
523 of some corrupted bytes.
kono
parents:
diff changeset
524
kono
parents:
diff changeset
525 It is recommended that systems using this data format provide some
kono
parents:
diff changeset
526 means of validating the integrity of the compressed data, such as by
kono
parents:
diff changeset
527 setting and checking the CRC-32 check value.
kono
parents:
diff changeset
528
kono
parents:
diff changeset
529 5. Acknowledgements
kono
parents:
diff changeset
530
kono
parents:
diff changeset
531 Trademarks cited in this document are the property of their
kono
parents:
diff changeset
532 respective owners.
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 Jean-Loup Gailly designed the gzip format and wrote, with Mark Adler,
kono
parents:
diff changeset
535 the related software described in this specification. Glenn
kono
parents:
diff changeset
536 Randers-Pehrson converted this document to RFC and HTML format.
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 6. Author's Address
kono
parents:
diff changeset
539
kono
parents:
diff changeset
540 L. Peter Deutsch
kono
parents:
diff changeset
541 Aladdin Enterprises
kono
parents:
diff changeset
542 203 Santa Margarita Ave.
kono
parents:
diff changeset
543 Menlo Park, CA 94025
kono
parents:
diff changeset
544
kono
parents:
diff changeset
545 Phone: (415) 322-0103 (AM only)
kono
parents:
diff changeset
546 FAX: (415) 322-1734
kono
parents:
diff changeset
547 EMail: <ghost@aladdin.com>
kono
parents:
diff changeset
548
kono
parents:
diff changeset
549 Questions about the technical content of this specification can be
kono
parents:
diff changeset
550 sent by email to:
kono
parents:
diff changeset
551
kono
parents:
diff changeset
552 Jean-Loup Gailly <gzip@prep.ai.mit.edu> and
kono
parents:
diff changeset
553 Mark Adler <madler@alumni.caltech.edu>
kono
parents:
diff changeset
554
kono
parents:
diff changeset
555 Editorial comments on this specification can be sent by email to:
kono
parents:
diff changeset
556
kono
parents:
diff changeset
557 L. Peter Deutsch <ghost@aladdin.com> and
kono
parents:
diff changeset
558 Glenn Randers-Pehrson <randeg@alumni.rpi.edu>
kono
parents:
diff changeset
559
kono
parents:
diff changeset
560
kono
parents:
diff changeset
561
kono
parents:
diff changeset
562 Deutsch Informational [Page 10]
kono
parents:
diff changeset
563
kono
parents:
diff changeset
564 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
565
kono
parents:
diff changeset
566
kono
parents:
diff changeset
567 7. Appendix: Jean-Loup Gailly's gzip utility
kono
parents:
diff changeset
568
kono
parents:
diff changeset
569 The most widely used implementation of gzip compression, and the
kono
parents:
diff changeset
570 original documentation on which this specification is based, were
kono
parents:
diff changeset
571 created by Jean-Loup Gailly <gzip@prep.ai.mit.edu>. Since this
kono
parents:
diff changeset
572 implementation is a de facto standard, we mention some more of its
kono
parents:
diff changeset
573 features here. Again, the material in this section is not part of
kono
parents:
diff changeset
574 the specification per se, and implementations need not follow it to
kono
parents:
diff changeset
575 be compliant.
kono
parents:
diff changeset
576
kono
parents:
diff changeset
577 When compressing or decompressing a file, gzip preserves the
kono
parents:
diff changeset
578 protection, ownership, and modification time attributes on the local
kono
parents:
diff changeset
579 file system, since there is no provision for representing protection
kono
parents:
diff changeset
580 attributes in the gzip file format itself. Since the file format
kono
parents:
diff changeset
581 includes a modification time, the gzip decompressor provides a
kono
parents:
diff changeset
582 command line switch that assigns the modification time from the file,
kono
parents:
diff changeset
583 rather than the local modification time of the compressed input, to
kono
parents:
diff changeset
584 the decompressed output.
kono
parents:
diff changeset
585
kono
parents:
diff changeset
586 8. Appendix: Sample CRC Code
kono
parents:
diff changeset
587
kono
parents:
diff changeset
588 The following sample code represents a practical implementation of
kono
parents:
diff changeset
589 the CRC (Cyclic Redundancy Check). (See also ISO 3309 and ITU-T V.42
kono
parents:
diff changeset
590 for a formal specification.)
kono
parents:
diff changeset
591
kono
parents:
diff changeset
592 The sample code is in the ANSI C programming language. Non C users
kono
parents:
diff changeset
593 may find it easier to read with these hints:
kono
parents:
diff changeset
594
kono
parents:
diff changeset
595 & Bitwise AND operator.
kono
parents:
diff changeset
596 ^ Bitwise exclusive-OR operator.
kono
parents:
diff changeset
597 >> Bitwise right shift operator. When applied to an
kono
parents:
diff changeset
598 unsigned quantity, as here, right shift inserts zero
kono
parents:
diff changeset
599 bit(s) at the left.
kono
parents:
diff changeset
600 ! Logical NOT operator.
kono
parents:
diff changeset
601 ++ "n++" increments the variable n.
kono
parents:
diff changeset
602 0xNNN 0x introduces a hexadecimal (base 16) constant.
kono
parents:
diff changeset
603 Suffix L indicates a long value (at least 32 bits).
kono
parents:
diff changeset
604
kono
parents:
diff changeset
605 /* Table of CRCs of all 8-bit messages. */
kono
parents:
diff changeset
606 unsigned long crc_table[256];
kono
parents:
diff changeset
607
kono
parents:
diff changeset
608 /* Flag: has the table been computed? Initially false. */
kono
parents:
diff changeset
609 int crc_table_computed = 0;
kono
parents:
diff changeset
610
kono
parents:
diff changeset
611 /* Make the table for a fast CRC. */
kono
parents:
diff changeset
612 void make_crc_table(void)
kono
parents:
diff changeset
613 {
kono
parents:
diff changeset
614 unsigned long c;
kono
parents:
diff changeset
615
kono
parents:
diff changeset
616
kono
parents:
diff changeset
617
kono
parents:
diff changeset
618 Deutsch Informational [Page 11]
kono
parents:
diff changeset
619
kono
parents:
diff changeset
620 RFC 1952 GZIP File Format Specification May 1996
kono
parents:
diff changeset
621
kono
parents:
diff changeset
622
kono
parents:
diff changeset
623 int n, k;
kono
parents:
diff changeset
624 for (n = 0; n < 256; n++) {
kono
parents:
diff changeset
625 c = (unsigned long) n;
kono
parents:
diff changeset
626 for (k = 0; k < 8; k++) {
kono
parents:
diff changeset
627 if (c & 1) {
kono
parents:
diff changeset
628 c = 0xedb88320L ^ (c >> 1);
kono
parents:
diff changeset
629 } else {
kono
parents:
diff changeset
630 c = c >> 1;
kono
parents:
diff changeset
631 }
kono
parents:
diff changeset
632 }
kono
parents:
diff changeset
633 crc_table[n] = c;
kono
parents:
diff changeset
634 }
kono
parents:
diff changeset
635 crc_table_computed = 1;
kono
parents:
diff changeset
636 }
kono
parents:
diff changeset
637
kono
parents:
diff changeset
638 /*
kono
parents:
diff changeset
639 Update a running crc with the bytes buf[0..len-1] and return
kono
parents:
diff changeset
640 the updated crc. The crc should be initialized to zero. Pre- and
kono
parents:
diff changeset
641 post-conditioning (one's complement) is performed within this
kono
parents:
diff changeset
642 function so it shouldn't be done by the caller. Usage example:
kono
parents:
diff changeset
643
kono
parents:
diff changeset
644 unsigned long crc = 0L;
kono
parents:
diff changeset
645
kono
parents:
diff changeset
646 while (read_buffer(buffer, length) != EOF) {
kono
parents:
diff changeset
647 crc = update_crc(crc, buffer, length);
kono
parents:
diff changeset
648 }
kono
parents:
diff changeset
649 if (crc != original_crc) error();
kono
parents:
diff changeset
650 */
kono
parents:
diff changeset
651 unsigned long update_crc(unsigned long crc,
kono
parents:
diff changeset
652 unsigned char *buf, int len)
kono
parents:
diff changeset
653 {
kono
parents:
diff changeset
654 unsigned long c = crc ^ 0xffffffffL;
kono
parents:
diff changeset
655 int n;
kono
parents:
diff changeset
656
kono
parents:
diff changeset
657 if (!crc_table_computed)
kono
parents:
diff changeset
658 make_crc_table();
kono
parents:
diff changeset
659 for (n = 0; n < len; n++) {
kono
parents:
diff changeset
660 c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
kono
parents:
diff changeset
661 }
kono
parents:
diff changeset
662 return c ^ 0xffffffffL;
kono
parents:
diff changeset
663 }
kono
parents:
diff changeset
664
kono
parents:
diff changeset
665 /* Return the CRC of the bytes buf[0..len-1]. */
kono
parents:
diff changeset
666 unsigned long crc(unsigned char *buf, int len)
kono
parents:
diff changeset
667 {
kono
parents:
diff changeset
668 return update_crc(0L, buf, len);
kono
parents:
diff changeset
669 }
kono
parents:
diff changeset
670
kono
parents:
diff changeset
671
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673
kono
parents:
diff changeset
674 Deutsch Informational [Page 12]
kono
parents:
diff changeset
675