OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_BASE_GZIP_HEADER_H_ | 15 #ifndef NET_BASE_GZIP_HEADER_H_ |
16 #define NET_BASE_GZIP_HEADER_H_ | 16 #define NET_BASE_GZIP_HEADER_H_ |
17 #pragma once | 17 #pragma once |
18 | 18 |
19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
20 | 20 |
21 class GZipHeader { | 21 class GZipHeader { |
22 public: | 22 public: |
| 23 enum Status { |
| 24 INCOMPLETE_HEADER, // don't have all the bits yet... |
| 25 COMPLETE_HEADER, // complete, valid header |
| 26 INVALID_HEADER, // found something invalid in the header |
| 27 }; |
| 28 |
23 GZipHeader() { | 29 GZipHeader() { |
24 Reset(); | 30 Reset(); |
25 } | 31 } |
| 32 |
26 ~GZipHeader() { | 33 ~GZipHeader() { |
27 } | 34 } |
28 | 35 |
29 // Wipe the slate clean and start from scratch. | 36 // Wipe the slate clean and start from scratch. |
30 void Reset() { | 37 void Reset() { |
31 state_ = IN_HEADER_ID1; | 38 state_ = IN_HEADER_ID1; |
32 flags_ = 0; | 39 flags_ = 0; |
33 extra_length_ = 0; | 40 extra_length_ = 0; |
34 } | 41 } |
35 | 42 |
36 enum Status { | |
37 INCOMPLETE_HEADER, // don't have all the bits yet... | |
38 COMPLETE_HEADER, // complete, valid header | |
39 INVALID_HEADER, // found something invalid in the header | |
40 }; | |
41 | |
42 // Attempt to parse the given buffer as the next installment of | 43 // Attempt to parse the given buffer as the next installment of |
43 // bytes from a gzip header. If the bytes we've seen so far do not | 44 // bytes from a gzip header. If the bytes we've seen so far do not |
44 // yet constitute a complete gzip header, return | 45 // yet constitute a complete gzip header, return |
45 // INCOMPLETE_HEADER. If these bytes do not constitute a *valid* | 46 // INCOMPLETE_HEADER. If these bytes do not constitute a *valid* |
46 // gzip header, return INVALID_HEADER. When we've seen a complete | 47 // gzip header, return INVALID_HEADER. When we've seen a complete |
47 // gzip header, return COMPLETE_HEADER and set the pointer pointed | 48 // gzip header, return COMPLETE_HEADER and set the pointer pointed |
48 // to by header_end to the first byte beyond the gzip header. | 49 // to by header_end to the first byte beyond the gzip header. |
49 Status ReadMore(const char* inbuf, int inbuf_len, | 50 Status ReadMore(const char* inbuf, int inbuf_len, |
50 const char** header_end); | 51 const char** header_end); |
51 private: | 52 private: |
52 | |
53 static const uint8 magic[]; // gzip magic header | |
54 | |
55 enum { // flags (see RFC) | 53 enum { // flags (see RFC) |
56 FLAG_FTEXT = 0x01, // bit 0 set: file probably ascii text | 54 FLAG_FTEXT = 0x01, // bit 0 set: file probably ascii text |
57 FLAG_FHCRC = 0x02, // bit 1 set: header CRC present | 55 FLAG_FHCRC = 0x02, // bit 1 set: header CRC present |
58 FLAG_FEXTRA = 0x04, // bit 2 set: extra field present | 56 FLAG_FEXTRA = 0x04, // bit 2 set: extra field present |
59 FLAG_FNAME = 0x08, // bit 3 set: original file name present | 57 FLAG_FNAME = 0x08, // bit 3 set: original file name present |
60 FLAG_FCOMMENT = 0x10, // bit 4 set: file comment present | 58 FLAG_FCOMMENT = 0x10, // bit 4 set: file comment present |
61 FLAG_RESERVED = 0xE0, // bits 5..7: reserved | 59 FLAG_RESERVED = 0xE0, // bits 5..7: reserved |
62 }; | 60 }; |
63 | 61 |
64 enum State { | 62 enum State { |
(...skipping 16 matching lines...) Expand all Loading... |
81 IN_FNAME, | 79 IN_FNAME, |
82 | 80 |
83 IN_FCOMMENT, | 81 IN_FCOMMENT, |
84 | 82 |
85 IN_FHCRC_BYTE_0, | 83 IN_FHCRC_BYTE_0, |
86 IN_FHCRC_BYTE_1, | 84 IN_FHCRC_BYTE_1, |
87 | 85 |
88 IN_DONE, | 86 IN_DONE, |
89 }; | 87 }; |
90 | 88 |
| 89 static const uint8 magic[]; // gzip magic header |
| 90 |
91 int state_; // our current State in the parsing FSM: an int so we can ++ | 91 int state_; // our current State in the parsing FSM: an int so we can ++ |
92 uint8 flags_; // the flags byte of the header ("FLG" in the RFC) | 92 uint8 flags_; // the flags byte of the header ("FLG" in the RFC) |
93 uint16 extra_length_; // how much of the "extra field" we have yet to read | 93 uint16 extra_length_; // how much of the "extra field" we have yet to read |
94 }; | 94 }; |
95 | 95 |
96 #endif // NET_BASE_GZIP_HEADER_H_ | 96 #endif // NET_BASE_GZIP_HEADER_H_ |
OLD | NEW |