Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // The GZipHeader class allows you to parse a gzip header, such as you
      6 // might find at the beginning of a file compressed by gzip (ie, a .gz
      7 // file), or at the beginning of an HTTP response that uses a gzip
      8 // Content-Encoding. See RFC 1952 for the specification for the gzip
      9 // header.
     10 //
     11 // The model is that you call ReadMore() for each chunk of bytes
     12 // you've read from a file or socket.
     13 //
     14 
     15 #ifndef NET_BASE_GZIP_HEADER_H_
     16 #define NET_BASE_GZIP_HEADER_H_
     17 #pragma once
     18 
     19 #include "base/basictypes.h"
     20 
     21 namespace net {
     22 
     23 class GZipHeader {
     24  public:
     25   enum Status {
     26     INCOMPLETE_HEADER,    // don't have all the bits yet...
     27     COMPLETE_HEADER,      // complete, valid header
     28     INVALID_HEADER,       // found something invalid in the header
     29   };
     30 
     31   GZipHeader();
     32   ~GZipHeader();
     33 
     34   // Wipe the slate clean and start from scratch.
     35   void Reset();
     36 
     37   // Attempt to parse the given buffer as the next installment of
     38   // bytes from a gzip header. If the bytes we've seen so far do not
     39   // yet constitute a complete gzip header, return
     40   // INCOMPLETE_HEADER. If these bytes do not constitute a *valid*
     41   // gzip header, return INVALID_HEADER. When we've seen a complete
     42   // gzip header, return COMPLETE_HEADER and set the pointer pointed
     43   // to by header_end to the first byte beyond the gzip header.
     44   Status ReadMore(const char* inbuf,
     45                   int inbuf_len,
     46                   const char** header_end);
     47  private:
     48   enum {                       // flags (see RFC)
     49     FLAG_FTEXT        = 0x01,  // bit 0 set: file probably ascii text
     50     FLAG_FHCRC        = 0x02,  // bit 1 set: header CRC present
     51     FLAG_FEXTRA       = 0x04,  // bit 2 set: extra field present
     52     FLAG_FNAME        = 0x08,  // bit 3 set: original file name present
     53     FLAG_FCOMMENT     = 0x10,  // bit 4 set: file comment present
     54     FLAG_RESERVED     = 0xE0,  // bits 5..7: reserved
     55   };
     56 
     57   enum State {
     58     // The first 10 bytes are the fixed-size header:
     59     IN_HEADER_ID1,
     60     IN_HEADER_ID2,
     61     IN_HEADER_CM,
     62     IN_HEADER_FLG,
     63     IN_HEADER_MTIME_BYTE_0,
     64     IN_HEADER_MTIME_BYTE_1,
     65     IN_HEADER_MTIME_BYTE_2,
     66     IN_HEADER_MTIME_BYTE_3,
     67     IN_HEADER_XFL,
     68     IN_HEADER_OS,
     69 
     70     IN_XLEN_BYTE_0,
     71     IN_XLEN_BYTE_1,
     72     IN_FEXTRA,
     73 
     74     IN_FNAME,
     75 
     76     IN_FCOMMENT,
     77 
     78     IN_FHCRC_BYTE_0,
     79     IN_FHCRC_BYTE_1,
     80 
     81     IN_DONE,
     82   };
     83 
     84   static const uint8 magic[];  // gzip magic header
     85 
     86   int    state_;  // our current State in the parsing FSM: an int so we can ++
     87   uint8  flags_;         // the flags byte of the header ("FLG" in the RFC)
     88   uint16 extra_length_;  // how much of the "extra field" we have yet to read
     89 
     90   DISALLOW_COPY_AND_ASSIGN(GZipHeader);
     91 };
     92 
     93 }  // namespace net
     94 
     95 #endif  // NET_BASE_GZIP_HEADER_H_
     96