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 #include "net/filter/gzip_header.h" | 5 #include "net/filter/gzip_header.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "third_party/zlib/zlib.h" | 10 #include "third_party/zlib/zlib.h" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 const uint8 GZipHeader::magic[] = { 0x1f, 0x8b }; | 14 const uint8 GZipHeader::magic[] = {0x1f, 0x8b}; |
15 | 15 |
16 GZipHeader::GZipHeader() { | 16 GZipHeader::GZipHeader() { |
17 Reset(); | 17 Reset(); |
18 } | 18 } |
19 | 19 |
20 GZipHeader::~GZipHeader() { | 20 GZipHeader::~GZipHeader() { |
21 } | 21 } |
22 | 22 |
23 void GZipHeader::Reset() { | 23 void GZipHeader::Reset() { |
24 state_ = IN_HEADER_ID1; | 24 state_ = IN_HEADER_ID1; |
25 flags_ = 0; | 25 flags_ = 0; |
26 extra_length_ = 0; | 26 extra_length_ = 0; |
27 } | 27 } |
28 | 28 |
29 GZipHeader::Status GZipHeader::ReadMore(const char* inbuf, int inbuf_len, | 29 GZipHeader::Status GZipHeader::ReadMore(const char* inbuf, |
| 30 int inbuf_len, |
30 const char** header_end) { | 31 const char** header_end) { |
31 DCHECK_GE(inbuf_len, 0); | 32 DCHECK_GE(inbuf_len, 0); |
32 const uint8* pos = reinterpret_cast<const uint8*>(inbuf); | 33 const uint8* pos = reinterpret_cast<const uint8*>(inbuf); |
33 const uint8* const end = pos + inbuf_len; | 34 const uint8* const end = pos + inbuf_len; |
34 | 35 |
35 while ( pos < end ) { | 36 while (pos < end) { |
36 switch ( state_ ) { | 37 switch (state_) { |
37 case IN_HEADER_ID1: | 38 case IN_HEADER_ID1: |
38 if ( *pos != magic[0] ) return INVALID_HEADER; | 39 if (*pos != magic[0]) |
| 40 return INVALID_HEADER; |
39 pos++; | 41 pos++; |
40 state_++; | 42 state_++; |
41 break; | 43 break; |
42 case IN_HEADER_ID2: | 44 case IN_HEADER_ID2: |
43 if ( *pos != magic[1] ) return INVALID_HEADER; | 45 if (*pos != magic[1]) |
| 46 return INVALID_HEADER; |
44 pos++; | 47 pos++; |
45 state_++; | 48 state_++; |
46 break; | 49 break; |
47 case IN_HEADER_CM: | 50 case IN_HEADER_CM: |
48 if ( *pos != Z_DEFLATED ) return INVALID_HEADER; | 51 if (*pos != Z_DEFLATED) |
| 52 return INVALID_HEADER; |
49 pos++; | 53 pos++; |
50 state_++; | 54 state_++; |
51 break; | 55 break; |
52 case IN_HEADER_FLG: | 56 case IN_HEADER_FLG: |
53 flags_ = (*pos) & (FLAG_FHCRC | FLAG_FEXTRA | | 57 flags_ = |
54 FLAG_FNAME | FLAG_FCOMMENT); | 58 (*pos) & (FLAG_FHCRC | FLAG_FEXTRA | FLAG_FNAME | FLAG_FCOMMENT); |
55 pos++; | 59 pos++; |
56 state_++; | 60 state_++; |
57 break; | 61 break; |
58 | 62 |
59 case IN_HEADER_MTIME_BYTE_0: | 63 case IN_HEADER_MTIME_BYTE_0: |
60 pos++; | 64 pos++; |
61 state_++; | 65 state_++; |
62 break; | 66 break; |
63 case IN_HEADER_MTIME_BYTE_1: | 67 case IN_HEADER_MTIME_BYTE_1: |
64 pos++; | 68 pos++; |
(...skipping 12 matching lines...) Expand all Loading... |
77 pos++; | 81 pos++; |
78 state_++; | 82 state_++; |
79 break; | 83 break; |
80 | 84 |
81 case IN_HEADER_OS: | 85 case IN_HEADER_OS: |
82 pos++; | 86 pos++; |
83 state_++; | 87 state_++; |
84 break; | 88 break; |
85 | 89 |
86 case IN_XLEN_BYTE_0: | 90 case IN_XLEN_BYTE_0: |
87 if ( !(flags_ & FLAG_FEXTRA) ) { | 91 if (!(flags_ & FLAG_FEXTRA)) { |
88 state_ = IN_FNAME; | 92 state_ = IN_FNAME; |
89 break; | 93 break; |
90 } | 94 } |
91 // We have a two-byte little-endian length, followed by a | 95 // We have a two-byte little-endian length, followed by a |
92 // field of that length. | 96 // field of that length. |
93 extra_length_ = *pos; | 97 extra_length_ = *pos; |
94 pos++; | 98 pos++; |
95 state_++; | 99 state_++; |
96 break; | 100 break; |
97 case IN_XLEN_BYTE_1: | 101 case IN_XLEN_BYTE_1: |
98 extra_length_ += *pos << 8; | 102 extra_length_ += *pos << 8; |
99 pos++; | 103 pos++; |
100 state_++; | 104 state_++; |
101 // We intentionally fall through, because if we have a | 105 // We intentionally fall through, because if we have a |
102 // zero-length FEXTRA, we want to check to notice that we're | 106 // zero-length FEXTRA, we want to check to notice that we're |
103 // done reading the FEXTRA before we exit this loop... | 107 // done reading the FEXTRA before we exit this loop... |
104 | 108 |
105 case IN_FEXTRA: { | 109 case IN_FEXTRA: { |
106 // Grab the rest of the bytes in the extra field, or as many | 110 // Grab the rest of the bytes in the extra field, or as many |
107 // of them as are actually present so far. | 111 // of them as are actually present so far. |
108 const int num_extra_bytes = static_cast<const int>(std::min( | 112 const int num_extra_bytes = static_cast<const int>( |
109 static_cast<ptrdiff_t>(extra_length_), | 113 std::min(static_cast<ptrdiff_t>(extra_length_), (end - pos))); |
110 (end - pos))); | |
111 pos += num_extra_bytes; | 114 pos += num_extra_bytes; |
112 extra_length_ -= num_extra_bytes; | 115 extra_length_ -= num_extra_bytes; |
113 if ( extra_length_ == 0 ) { | 116 if (extra_length_ == 0) { |
114 state_ = IN_FNAME; // advance when we've seen extra_length_ bytes | 117 state_ = IN_FNAME; // advance when we've seen extra_length_ bytes |
115 flags_ &= ~FLAG_FEXTRA; // we're done with the FEXTRA stuff | 118 flags_ &= ~FLAG_FEXTRA; // we're done with the FEXTRA stuff |
116 } | 119 } |
117 break; | 120 break; |
118 } | 121 } |
119 | 122 |
120 case IN_FNAME: | 123 case IN_FNAME: |
121 if ( !(flags_ & FLAG_FNAME) ) { | 124 if (!(flags_ & FLAG_FNAME)) { |
122 state_ = IN_FCOMMENT; | 125 state_ = IN_FCOMMENT; |
123 break; | 126 break; |
124 } | 127 } |
125 // See if we can find the end of the \0-terminated FNAME field. | 128 // See if we can find the end of the \0-terminated FNAME field. |
126 pos = reinterpret_cast<const uint8*>(memchr(pos, '\0', (end - pos))); | 129 pos = reinterpret_cast<const uint8*>(memchr(pos, '\0', (end - pos))); |
127 if ( pos != NULL ) { | 130 if (pos != NULL) { |
128 pos++; // advance past the '\0' | 131 pos++; // advance past the '\0' |
129 flags_ &= ~FLAG_FNAME; // we're done with the FNAME stuff | 132 flags_ &= ~FLAG_FNAME; // we're done with the FNAME stuff |
130 state_ = IN_FCOMMENT; | 133 state_ = IN_FCOMMENT; |
131 } else { | 134 } else { |
132 pos = end; // everything we have so far is part of the FNAME | 135 pos = end; // everything we have so far is part of the FNAME |
133 } | 136 } |
134 break; | 137 break; |
135 | 138 |
136 case IN_FCOMMENT: | 139 case IN_FCOMMENT: |
137 if ( !(flags_ & FLAG_FCOMMENT) ) { | 140 if (!(flags_ & FLAG_FCOMMENT)) { |
138 state_ = IN_FHCRC_BYTE_0; | 141 state_ = IN_FHCRC_BYTE_0; |
139 break; | 142 break; |
140 } | 143 } |
141 // See if we can find the end of the \0-terminated FCOMMENT field. | 144 // See if we can find the end of the \0-terminated FCOMMENT field. |
142 pos = reinterpret_cast<const uint8*>(memchr(pos, '\0', (end - pos))); | 145 pos = reinterpret_cast<const uint8*>(memchr(pos, '\0', (end - pos))); |
143 if ( pos != NULL ) { | 146 if (pos != NULL) { |
144 pos++; // advance past the '\0' | 147 pos++; // advance past the '\0' |
145 flags_ &= ~FLAG_FCOMMENT; // we're done with the FCOMMENT stuff | 148 flags_ &= ~FLAG_FCOMMENT; // we're done with the FCOMMENT stuff |
146 state_ = IN_FHCRC_BYTE_0; | 149 state_ = IN_FHCRC_BYTE_0; |
147 } else { | 150 } else { |
148 pos = end; // everything we have so far is part of the FNAME | 151 pos = end; // everything we have so far is part of the FNAME |
149 } | 152 } |
150 break; | 153 break; |
151 | 154 |
152 case IN_FHCRC_BYTE_0: | 155 case IN_FHCRC_BYTE_0: |
153 if ( !(flags_ & FLAG_FHCRC) ) { | 156 if (!(flags_ & FLAG_FHCRC)) { |
154 state_ = IN_DONE; | 157 state_ = IN_DONE; |
155 break; | 158 break; |
156 } | 159 } |
157 pos++; | 160 pos++; |
158 state_++; | 161 state_++; |
159 break; | 162 break; |
160 | 163 |
161 case IN_FHCRC_BYTE_1: | 164 case IN_FHCRC_BYTE_1: |
162 pos++; | 165 pos++; |
163 flags_ &= ~FLAG_FHCRC; // we're done with the FHCRC stuff | 166 flags_ &= ~FLAG_FHCRC; // we're done with the FHCRC stuff |
164 state_++; | 167 state_++; |
165 break; | 168 break; |
166 | 169 |
167 case IN_DONE: | 170 case IN_DONE: |
168 *header_end = reinterpret_cast<const char*>(pos); | 171 *header_end = reinterpret_cast<const char*>(pos); |
169 return COMPLETE_HEADER; | 172 return COMPLETE_HEADER; |
170 } | 173 } |
171 } | 174 } |
172 | 175 |
173 if ( (state_ > IN_HEADER_OS) && (flags_ == 0) ) { | 176 if ((state_ > IN_HEADER_OS) && (flags_ == 0)) { |
174 *header_end = reinterpret_cast<const char*>(pos); | 177 *header_end = reinterpret_cast<const char*>(pos); |
175 return COMPLETE_HEADER; | 178 return COMPLETE_HEADER; |
176 } else { | 179 } else { |
177 return INCOMPLETE_HEADER; | 180 return INCOMPLETE_HEADER; |
178 } | 181 } |
179 } | 182 } |
180 | 183 |
181 } // namespace net | 184 } // namespace net |
OLD | NEW |