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