1 // Copyright 2014 The Chromium Authors 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_FILTER_GZIP_HEADER_H_ 16 #define NET_FILTER_GZIP_HEADER_H_ 17 18 #include <stddef.h> 19 #include <stdint.h> 20 21 #include "net/base/net_export.h" 22 23 namespace net { 24 25 class NET_EXPORT GZipHeader { 26 public: 27 enum Status { 28 INCOMPLETE_HEADER, // don't have all the bits yet... 29 COMPLETE_HEADER, // complete, valid header 30 INVALID_HEADER, // found something invalid in the header 31 }; 32 33 GZipHeader(); 34 35 GZipHeader(const GZipHeader&) = delete; 36 GZipHeader& operator=(const GZipHeader&) = delete; 37 38 ~GZipHeader(); 39 40 // Wipe the slate clean and start from scratch. 41 void Reset(); 42 43 // Attempt to parse the given buffer as the next installment of 44 // bytes from a gzip header. If the bytes we've seen so far do not 45 // yet constitute a complete gzip header, return 46 // INCOMPLETE_HEADER. If these bytes do not constitute a *valid* 47 // gzip header, return INVALID_HEADER. When we've seen a complete 48 // gzip header, return COMPLETE_HEADER and set the pointer pointed 49 // to by header_end to the first byte beyond the gzip header. 50 Status ReadMore(const char* inbuf, size_t inbuf_len, const char** header_end); 51 52 private: 53 enum { // flags (see RFC) 54 FLAG_FTEXT = 0x01, // bit 0 set: file probably ascii text 55 FLAG_FHCRC = 0x02, // bit 1 set: header CRC present 56 FLAG_FEXTRA = 0x04, // bit 2 set: extra field present 57 FLAG_FNAME = 0x08, // bit 3 set: original file name present 58 FLAG_FCOMMENT = 0x10, // bit 4 set: file comment present 59 FLAG_RESERVED = 0xE0, // bits 5..7: reserved 60 }; 61 62 enum State { 63 // The first 10 bytes are the fixed-size header: 64 IN_HEADER_ID1, 65 IN_HEADER_ID2, 66 IN_HEADER_CM, 67 IN_HEADER_FLG, 68 IN_HEADER_MTIME_BYTE_0, 69 IN_HEADER_MTIME_BYTE_1, 70 IN_HEADER_MTIME_BYTE_2, 71 IN_HEADER_MTIME_BYTE_3, 72 IN_HEADER_XFL, 73 IN_HEADER_OS, 74 75 IN_XLEN_BYTE_0, 76 IN_XLEN_BYTE_1, 77 IN_FEXTRA, 78 79 IN_FNAME, 80 81 IN_FCOMMENT, 82 83 IN_FHCRC_BYTE_0, 84 IN_FHCRC_BYTE_1, 85 86 IN_DONE, 87 }; 88 89 static const uint8_t magic[]; // gzip magic header 90 91 int state_; // our current State in the parsing FSM: an int so we can ++ 92 uint8_t flags_; // the flags byte of the header ("FLG" in the RFC) 93 uint16_t extra_length_; // how much of the "extra field" we have yet to read 94 }; 95 96 } // namespace net 97 98 #endif // NET_FILTER_GZIP_HEADER_H_ 99