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_t 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, int inbuf_len, |
30 const char** header_end) { | 30 const char** header_end) { |
31 DCHECK_GE(inbuf_len, 0); | 31 DCHECK_GE(inbuf_len, 0); |
32 const uint8* pos = reinterpret_cast<const uint8*>(inbuf); | 32 const uint8_t* pos = reinterpret_cast<const uint8_t*>(inbuf); |
33 const uint8* const end = pos + inbuf_len; | 33 const uint8_t* const end = pos + inbuf_len; |
34 | 34 |
35 while ( pos < end ) { | 35 while ( pos < end ) { |
36 switch ( state_ ) { | 36 switch ( state_ ) { |
37 case IN_HEADER_ID1: | 37 case IN_HEADER_ID1: |
38 if ( *pos != magic[0] ) return INVALID_HEADER; | 38 if ( *pos != magic[0] ) return INVALID_HEADER; |
39 pos++; | 39 pos++; |
40 state_++; | 40 state_++; |
41 break; | 41 break; |
42 case IN_HEADER_ID2: | 42 case IN_HEADER_ID2: |
43 if ( *pos != magic[1] ) return INVALID_HEADER; | 43 if ( *pos != magic[1] ) return INVALID_HEADER; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 extra_length_ += *pos << 8; | 98 extra_length_ += *pos << 8; |
99 pos++; | 99 pos++; |
100 state_++; | 100 state_++; |
101 // We intentionally fall through, because if we have a | 101 // We intentionally fall through, because if we have a |
102 // zero-length FEXTRA, we want to check to notice that we're | 102 // zero-length FEXTRA, we want to check to notice that we're |
103 // done reading the FEXTRA before we exit this loop... | 103 // done reading the FEXTRA before we exit this loop... |
104 | 104 |
105 case IN_FEXTRA: { | 105 case IN_FEXTRA: { |
106 // Grab the rest of the bytes in the extra field, or as many | 106 // Grab the rest of the bytes in the extra field, or as many |
107 // of them as are actually present so far. | 107 // of them as are actually present so far. |
108 const uint16 num_extra_bytes = static_cast<uint16>(std::min( | 108 const uint16_t num_extra_bytes = static_cast<uint16_t>( |
109 static_cast<ptrdiff_t>(extra_length_), | 109 std::min(static_cast<ptrdiff_t>(extra_length_), (end - pos))); |
110 (end - pos))); | |
111 pos += num_extra_bytes; | 110 pos += num_extra_bytes; |
112 extra_length_ -= num_extra_bytes; | 111 extra_length_ -= num_extra_bytes; |
113 if ( extra_length_ == 0 ) { | 112 if ( extra_length_ == 0 ) { |
114 state_ = IN_FNAME; // advance when we've seen extra_length_ bytes | 113 state_ = IN_FNAME; // advance when we've seen extra_length_ bytes |
115 flags_ &= ~FLAG_FEXTRA; // we're done with the FEXTRA stuff | 114 flags_ &= ~FLAG_FEXTRA; // we're done with the FEXTRA stuff |
116 } | 115 } |
117 break; | 116 break; |
118 } | 117 } |
119 | 118 |
120 case IN_FNAME: | 119 case IN_FNAME: |
121 if ( !(flags_ & FLAG_FNAME) ) { | 120 if ( !(flags_ & FLAG_FNAME) ) { |
122 state_ = IN_FCOMMENT; | 121 state_ = IN_FCOMMENT; |
123 break; | 122 break; |
124 } | 123 } |
125 // See if we can find the end of the \0-terminated FNAME field. | 124 // See if we can find the end of the \0-terminated FNAME field. |
126 pos = reinterpret_cast<const uint8*>(memchr(pos, '\0', (end - pos))); | 125 pos = reinterpret_cast<const uint8_t*>(memchr(pos, '\0', (end - pos))); |
127 if ( pos != NULL ) { | 126 if ( pos != NULL ) { |
128 pos++; // advance past the '\0' | 127 pos++; // advance past the '\0' |
129 flags_ &= ~FLAG_FNAME; // we're done with the FNAME stuff | 128 flags_ &= ~FLAG_FNAME; // we're done with the FNAME stuff |
130 state_ = IN_FCOMMENT; | 129 state_ = IN_FCOMMENT; |
131 } else { | 130 } else { |
132 pos = end; // everything we have so far is part of the FNAME | 131 pos = end; // everything we have so far is part of the FNAME |
133 } | 132 } |
134 break; | 133 break; |
135 | 134 |
136 case IN_FCOMMENT: | 135 case IN_FCOMMENT: |
137 if ( !(flags_ & FLAG_FCOMMENT) ) { | 136 if ( !(flags_ & FLAG_FCOMMENT) ) { |
138 state_ = IN_FHCRC_BYTE_0; | 137 state_ = IN_FHCRC_BYTE_0; |
139 break; | 138 break; |
140 } | 139 } |
141 // See if we can find the end of the \0-terminated FCOMMENT field. | 140 // See if we can find the end of the \0-terminated FCOMMENT field. |
142 pos = reinterpret_cast<const uint8*>(memchr(pos, '\0', (end - pos))); | 141 pos = reinterpret_cast<const uint8_t*>(memchr(pos, '\0', (end - pos))); |
143 if ( pos != NULL ) { | 142 if ( pos != NULL ) { |
144 pos++; // advance past the '\0' | 143 pos++; // advance past the '\0' |
145 flags_ &= ~FLAG_FCOMMENT; // we're done with the FCOMMENT stuff | 144 flags_ &= ~FLAG_FCOMMENT; // we're done with the FCOMMENT stuff |
146 state_ = IN_FHCRC_BYTE_0; | 145 state_ = IN_FHCRC_BYTE_0; |
147 } else { | 146 } else { |
148 pos = end; // everything we have so far is part of the FNAME | 147 pos = end; // everything we have so far is part of the FNAME |
149 } | 148 } |
150 break; | 149 break; |
151 | 150 |
152 case IN_FHCRC_BYTE_0: | 151 case IN_FHCRC_BYTE_0: |
(...skipping 19 matching lines...) Expand all Loading... |
172 | 171 |
173 if ( (state_ > IN_HEADER_OS) && (flags_ == 0) ) { | 172 if ( (state_ > IN_HEADER_OS) && (flags_ == 0) ) { |
174 *header_end = reinterpret_cast<const char*>(pos); | 173 *header_end = reinterpret_cast<const char*>(pos); |
175 return COMPLETE_HEADER; | 174 return COMPLETE_HEADER; |
176 } else { | 175 } else { |
177 return INCOMPLETE_HEADER; | 176 return INCOMPLETE_HEADER; |
178 } | 177 } |
179 } | 178 } |
180 | 179 |
181 } // namespace net | 180 } // namespace net |
OLD | NEW |