gzipファイルフォーマット解析

13339 ワード

Windowsの下でUltraEditを使って接尾辞を開きます.gzのファイルに表示されるデータフォーマットは以下の通りです.
2 bytes GZIPフラグバイト:0 x 1 f,0 x 8 b(037213)
1 byte圧縮方法:(0..7reserved,8=deflate)
1 byteフラグビット:
bit 0 set:ファイルはASCIIテキストファイルかもしれません
bit 1 set:gzipファイル部分を複数添付
bit 2 set:オプションの追加コンテンツが存在する
bit 3 set:元のファイル名が指定されています
bit 4 set:O-エンドのファイル内容が提供されます
bit 5 set:ファイルが暗号化されている
bit 6,7:保持
4 bytesファイル変更時間(Unix時間)
1 byteの追加フラグにより、圧縮方法が決定されます.2:最大圧縮、最も遅いアルゴリズムを使う
4:最も速いアルゴリズムを採用
1 byteこのフラグは、圧縮を行うときのシステムのタイプを示します.
                 0 - FAT filesystem (MS-DOS, OS/2, NT/Win32)
                 1 - Amiga
                 2 - VMS (or OpenVMS)
                 3 - Unix
                 4 - VM/CMS
                 5 - Atari TOS
                 6 - HPFS filesystem (OS/2, NT)
                 7 - Macintosh
                 8 - Z-System
                 9 - CP/M
                10 - TOPS-20
                11 - NTFS filesystem (NT)
                12 - QDOS
                13 - Acorn RISCOS
               255 - unknown
2 bytes optional part number(second part=1)オプションのシーケンス番号
2 bytes optional extra field lengthオプションの追加コンテンツの長さ
? bytes optional extra fieldオプションの追加コンテンツ
? bytes  optional original file name, zero terminated  
オプションの元のファイル名を'0'で終了
? bytes  optional file comment, zero terminated  
オプションのファイルの内容(この部分は説明されず、読み取り可能な人が使用するもので、'0'で終了します.
12 bytes optional encryption header      
? bytes  compressed data
4 bytes crc 32非圧縮データのサイクル冗長検査値です.
4 bytes uncompressed input size modulo 2^32元のデータの長さを2の32乗で模した値です.
 
逆検索ではなく、非圧縮データや出力を予知することなく、一方向符号化可能なフォーマットを設計した.
圧縮されたデータのサイズ.入力されたデータがファイルでない場合、変更時間は圧縮されたオープンに設定されます.
開始時間.
The format was designed to allow single pass compression without any
backwards seek, and without a priori knowledge of the uncompressed
input size or the available size on the output media. If input does
not come from a regular disk file, the file modification time is set
to the time at which compression started.
 
タイムスタンプは、主にgzipファイルをネットワーク上で転送する場合に使用されます.この場合、すべてを保存する必要はありません.
のプロパティです.ローカル転送の場合、所有者のプロパティは圧縮/解凍時にgzipによって保存されます.無視値:
0のタイムスタンプ. 
The time stamp is useful mainly when one gzip file is transferred over
a network. In this case it would not help to keep ownership
attributes. In the local case, the ownership attributes are preserved
by gzip when compressing/decompressing the file. A time stamp of zero
is ignored.
 
フラグビットでは、値が0のビットはオプションで、入力したデータを事前に理解することができます.いいえ
確定した場合は、フラグビットをクリアします.異なるファイルフォーマット(テキストファイルとバイナリファイル)を持つシステムでは、
復号化では、フラグビットを使用して異なるフォーマットを選択できます.
Bit 0 in the flags is only an optional indication, which can be set by
a small lookahead in the input data. In case of doubt, the flag is
cleared indicating binary data. For systems which have different
file formats for ascii text and binary data, the decompressor can
use the flag to choose the appropriate format.
 
追加のコンテンツがある場合は、1つ以上のサブフィールドを含む必要があります.各サブフィールドには次のフォーマットがあります.
The extra field, if present, must consist of one or more subfields,
each with the following format:
 
subfield id:2 bytesサブフィールドID
subfield size:2 bytes(little-endian format)サブフィールド長(小端バイト順)
subfield dataサブフィールドの内容
 
サブフィールドIDは、2つの記憶可能なアルファベットを含むことができる.このようなIDをいくつか送ってください[email protected]
2バイト目が0のIDは保持される.以下のIDが定義されています
The subfield id can consist of two letters with some mnemonic value.
Please send any such id to [email protected]. Ids with a zero second
byte are reserved for future use. The following ids are defined:
 
  Ap (0x41, 0x70) : Apollo file type information
 
サブフィールド長は、サブフィールドの内容の長さであり、IDおよびサブフィールド長の4バイトは含まれません.でも
前述した「オプションの付加コンテンツの長さ」には、IDおよびサブフィールドの長さの4バイトが含まれている.
The subfield size is the size of the subfield data and does not
include the id and the size itself. The field 'extra field length' is
the total size of the extra field, including subfield ids and sizes.
 
データの実際の長さにかかわらず、圧縮データの中でデータの終了位置を見つけることができる必要があります.圧縮する場合
ディスクの場合など、1つのファイルにデータを置くことはできません.各セクションはヘッダフィールドで開始されますが、
最後の部分にCRC 32と元のデータの長さがあります.解凍プログラムは別の入力を促すことができるはずです.
の各見出しページがあります.これは必要ですが、絶対的ではありません.一部のデータが破壊されると、
他の部分の内容を取得します.
It must be possible to detect the end of the compressed data with any
compression format, regardless of the actual size of the compressed
data. If the compressed data cannot fit in one file (in particular for
diskettes), each part starts with a header as described above, but
only the last part has the crc32 and uncompressed size. A decompressor
may prompt for additional data for multipart compressed files. It is
desirable but not mandatory that multiple parts be extractable
independently so that partial data can be recovered if one of the
parts is damaged. This is possible only if no compression state is
kept from one part to the other. The compression-type dependent flags
can indicate this.
 
圧縮ファイルのシステムがファイル名の大文字と小文字に敏感でない場合、元のファイル名は強制的に小文字に変換されます.
標準入力から読み込まれたデータの場合、元のファイル名はありません.
If the file being compressed is on a file system with case insensitive
names, the original name field must be forced to lower case. There is
no original file name if the data was compressed from standard input.
 
圧縮されたファイルが元のファイルより大きくなっても、圧縮は完了します.
Compression is always performed, even if the compressed file is
slightly larger than the original. The worst case expansion is
a few bytes for the gzip file header, plus 5 bytes every 32K block,
or an expansion ratio of 0.015% for large files. Note that the actual
number of used disk blocks almost never increases.
 
The encryption is that of zip 1.9. For the encryption check, the
last byte of the decoded encryption header must be zero. The time
stamp of an encrypted file might be set to zero to avoid giving a clue
about the construction of the random header.
gzip-1.2.4プログラム分析
    :
     gzip.c :
DECLARE(uch, inbuf,  INBUFSIZ +INBUF_EXTRA);
DECLARE(uch, outbuf, OUTBUFSIZ+OUTBUF_EXTRA);
DECLARE(ush, d_buf,  DIST_BUFSIZE);
DECLARE(uch, window, 2L*WSIZE);
#ifndef MAXSEG_64K
       DECLARE(ush, tab_prefix, 1L<<BITS);
#else
    DECLARE(ush, tab_prefix0, 1L<<(BITS-1));
    DECLARE(ush, tab_prefix1, 1L<<(BITS-1));
#endif
              :inbuf,outbuf,d_buf,window,tab_prefix,tab_prefix0,tabfix1.
1/
==================================================================================
    :gzip-1.2.4/gzip.c
  :  int main (argc, argv)
         int argc;
         char **argv;
  :  1)      (gzip,gunzip,unzip ),      (       )。
    2)    ,          ,     ,    :ascii(  
     ,                        )、decompress(        ) level(       —    
               ,  ,       )。
    3)   、            。
    4)  treat_file(argv[optind++]);       。
 
 
2/
==================================================================================
  :  local void treat_file(iname)
         char *iname;
  :       ;
  :  1)          :name,size,time,mode 。
    2)         。
    3)        ,      local int get_method(in)    gz       。
    4)         -l,   do_list()      。
    5)  local int create_outfile()      。
    6)   (*work)(ifd, ofd)    、      。   work   get_method()
    unzip()  (   ),      zip()  。     ,
          ,           。
 
 
3/
==================================================================================
  :  local int get_method(in)
        int in;        /* input file descriptor */
  :      
  :  1)           0x1F,0x8B。
    2)         0x08(deflate)。
    3)      work = unzip。(work     zip)
    4)    flags     。
    5)      1、5、6、7 ,       。(  0 7       )
    6)  5 8              time_stamp 。
    7)   9  (        —        ) 
 10  (        )。
    8)     flags  1 ,          
    9)     flags  2 (        ),          ,
        。
    10)     flags  3 (          ),          。
    11)     flags  4 (           ,          ),
         。
    12)          :header_bytes,      CRC       。
  :        (   "deflate",        8)
 
 
4/
==================================================================================
   gzip-1.2.4/unzip.c :
  :  int unzip(in, out)
       int in, out;   /* input and output file descriptors */
  :   、    。
  :  1)       crc。
    2)    inflate()      。
    3)          CRC    。            ,     。
 
 
5/
==================================================================================
   gzip-1.2.4/inflate.c :
  :  int inflate()
  : ulg bb;                         /*   bit buffer */
unsigned bk;                    /*  bit buffer      ,       */
  :  1)     inflate_block(&e),         。
   2) bk>-8, bb       ,          。
    3)         。
 
 
6/
==================================================================================
   gzip-1.2.4/inflate.c :
  :  int inflate_block(e)
int *e;                 /* last block flag */
  :   1,           。
  :  1)     ,               (0,  ;1, )        。
    2)       : 
0,      ,
1,    Huffman    , RFC1951 3.2.6 。
2,    Huffman    , RFC1951 3.2.7 。
    3)        ,         :
        inflate_stored();            ,      。
        inflate_fixed();       Huffman       ,      。
        inflate_dynamic();          Huffman       ,      。
 
 
7/  
==================================================================================
   gzip-1.2.4/inflate.c :
  :  int inflate_stored()
  :            
1)         。         ,           ,     
              ,           ,        。
    2)        ,           。         ,      
                    ,   ,   。
    3)        ,         。
 
 
8/
==================================================================================
   gzip-1.2.4/inflate.c :
  :  int inflate_fixed()
  :      Huffman       
1)      0 287   /length       :
                   Lit Value    Bits        Codes
                   ---------    ----        -----
                     0 - 143     8          00110000 through
                                            10111111
                   144 - 255     9          110010000 through
                                            111111111
                   256 - 279     7          0000000 through
                                            0010111
                   280 - 287     8          11000000 through
                                            11000111
2)       huft_build()    /length  Huffman 
3)         distance ( 0 29)      5。
4)       huft_build()  distance  Huffman 
5)         inflate_codes()    。
 
 
9/
==================================================================================
   gzip-1.2.4/inflate.c :
  :  int inflate_dynamic()
  :      Huffman       
1)  5   HLIT,  nl = 257+HLIT。          。
2)  5   HDIST,  nd = 1+HDIST。  distance    。
3)  4   HCLEN,  nb = 4+HCLEN。          。
4)   3*nb ,                      。
5)  huft_build()       Huffman 。
6)    Huffman ,           nl+nd  ,      0~nl-1
     (    /length  ), 0~nd-1     (  distance  )。
7)             ,    huft_build()  ,    Huffman 
(      /length,     distance)。
8)    inflate_codes()    。
 
 
10/
==================================================================================
   gzip-1.2.4/inflate.c :
  :  int inflate_codes(tl, td, bl, bd)
struct huft *tl, *td; /* literal/length and distance decoder tables */
int bl, bd;            /* number of bits decoded by tl[] and td[] */
  :  tl,td   Huffman           ,  length distance      
  ,            。
          , struct huft     ,   bl,bd     。
  :                 。
1)       X,  0<=X<=255, X     ,  ,  1)。
2)    X==255,      ,    。
3)  X>255,         length ,     ,       ,     
length 。
4)         ,    distance    ,                
 distance。
5)          ,    distance   ,  length          。
6)    1)
 
 
11/
==================================================================================
   gzip-1.2.4/inflate.c :
  :  int huft_build()    int huft_free()    ,      ,    。
  :  int huft_build()    :  Huffman    。
int huft_free() :    。
12/
==================================================================================
   gzip-1.2.4/zip.c :
  :  int zip(in, out)
       int in, out;   /* input and output file descriptors */
  :   、    。
  :  
1)          :0x1F 0x8B 0x08。
2)           8       。
3)       4       。
4)     CRC  。
5)    bi_init(out)          。
6)    ct_init()      ,      ,         
  。
7)    lm_init()       "    "   。
8)        2  ,        ,       。
9)      ,         。
10)          。
11)     deflate()  。
12)   4   CRC 。
13)     4          。
14)               。
 
 
13/
==================================================================================
   gzip-1.2.4/deflate.c :
  :  ulg deflate()
  :      。                   ,      。
1)             ,     deflate_fast(),    。
2)                 ,       。
3)            ,   <length,distence>    ,        。
 
 
14/
==================================================================================
   gzip-1.2.4/deflate.c :
  :  ulg deflate()
  :      。                      ,      。
1)            ,       。
2)       ,   <length,distence>    ,        。