Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: net/base/net_util.cc

Issue 8418034: Make string_util::WriteInto() DCHECK() that the supplied |length_with_null| > 1, meaning that the... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <map> 9 #include <map>
10 10
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 154
155 // FTP overrides the following restricted ports. 155 // FTP overrides the following restricted ports.
156 static const int kAllowedFtpPorts[] = { 156 static const int kAllowedFtpPorts[] = {
157 21, // ftp data 157 21, // ftp data
158 22, // ssh 158 22, // ssh
159 }; 159 };
160 160
161 std::string::size_type CountTrailingChars( 161 std::string::size_type CountTrailingChars(
162 const std::string input, 162 const std::string input,
163 const std::string::value_type trailing_chars[]) { 163 const std::string::value_type trailing_chars[]) {
164 const std::string::size_type last_good_char = 164 const size_t last_good_char = input.find_last_not_of(trailing_chars);
cbentzel 2011/11/15 11:37:10 Should this stay size_type? find_last_not_of retur
Peter Kasting 2011/11/15 18:50:32 size_type is banned in favor of size_t per the Chr
165 input.find_last_not_of(trailing_chars); 165 return (last_good_char == std::string::npos) ?
166 166 input.length() : (input.length() - last_good_char - 1);
167 if (last_good_char == std::string::npos)
168 return input.length();
169 else
170 return input.length() - last_good_char - 1;
171 } 167 }
172 168
173 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence 169 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence
174 // of bytes. If input is invalid, return false. 170 // of bytes. If input is invalid, return false.
175 bool QPDecode(const std::string& input, std::string* output) { 171 bool QPDecode(const std::string& input, std::string* output) {
176 std::string temp; 172 std::string temp;
177 temp.reserve(input.size()); 173 temp.reserve(input.size());
178 std::string::const_iterator it = input.begin(); 174 for (std::string::const_iterator it = input.begin(); it != input.end();
179 while (it != input.end()) { 175 ++it) {
180 if (*it == '_') { 176 if (*it == '_') {
181 temp.push_back(' '); 177 temp.push_back(' ');
182 } else if (*it == '=') { 178 } else if (*it == '=') {
183 if (input.end() - it < 3) { 179 if ((input.end() - it < 3) ||
180 !IsHexDigit(static_cast<unsigned char>(*(it + 1))) ||
181 !IsHexDigit(static_cast<unsigned char>(*(it + 2))))
184 return false; 182 return false;
185 } 183 unsigned char ch = HexDigitToInt(*(it + 1)) * 16 +
186 if (IsHexDigit(static_cast<unsigned char>(*(it + 1))) && 184 HexDigitToInt(*(it + 2));
187 IsHexDigit(static_cast<unsigned char>(*(it + 2)))) { 185 temp.push_back(static_cast<char>(ch));
188 unsigned char ch = HexDigitToInt(*(it + 1)) * 16 + 186 ++it;
189 HexDigitToInt(*(it + 2)); 187 ++it;
190 temp.push_back(static_cast<char>(ch));
191 ++it;
192 ++it;
193 } else {
194 return false;
195 }
196 } else if (0x20 < *it && *it < 0x7F) { 188 } else if (0x20 < *it && *it < 0x7F) {
197 // In a Q-encoded word, only printable ASCII characters 189 // In a Q-encoded word, only printable ASCII characters
198 // represent themselves. Besides, space, '=', '_' and '?' are 190 // represent themselves. Besides, space, '=', '_' and '?' are
199 // not allowed, but they're already filtered out. 191 // not allowed, but they're already filtered out.
200 DCHECK(*it != 0x3D && *it != 0x5F && *it != 0x3F); 192 DCHECK_NE('=', *it);
193 DCHECK_NE('?', *it);
194 DCHECK_NE('_', *it);
201 temp.push_back(*it); 195 temp.push_back(*it);
202 } else { 196 } else {
203 return false; 197 return false;
204 } 198 }
205 ++it;
206 } 199 }
207 output->swap(temp); 200 output->swap(temp);
208 return true; 201 return true;
209 } 202 }
210 203
211 enum RFC2047EncodingType {Q_ENCODING, B_ENCODING}; 204 enum RFC2047EncodingType {Q_ENCODING, B_ENCODING};
212 bool DecodeBQEncoding(const std::string& part, RFC2047EncodingType enc_type, 205 bool DecodeBQEncoding(const std::string& part,
213 const std::string& charset, std::string* output) { 206 RFC2047EncodingType enc_type,
207 const std::string& charset,
208 std::string* output) {
214 std::string decoded; 209 std::string decoded;
215 if (enc_type == B_ENCODING) { 210 if (!((enc_type == B_ENCODING) ?
cbentzel 2011/11/15 11:37:10 I find the old logic easier to parse than this ter
216 if (!base::Base64Decode(part, &decoded)) { 211 base::Base64Decode(part, &decoded) : QPDecode(part, &decoded)))
217 return false; 212 return false;
218 } 213
219 } else { 214 if (decoded.empty()) {
220 if (!QPDecode(part, &decoded)) { 215 output->clear();
221 return false; 216 return true;
222 }
223 } 217 }
224 218
225 UErrorCode err = U_ZERO_ERROR; 219 UErrorCode err = U_ZERO_ERROR;
226 UConverter* converter(ucnv_open(charset.c_str(), &err)); 220 UConverter* converter(ucnv_open(charset.c_str(), &err));
227 if (U_FAILURE(err)) { 221 if (U_FAILURE(err))
228 return false; 222 return false;
229 }
230 223
231 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8. 224 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8.
232 // A 'two-byte character' in a legacy encoding can be expanded to 4 bytes 225 // A 'two-byte character' in a legacy encoding can be expanded to 4 bytes
233 // in UTF-8. Therefore, the expansion ratio is 3 at most. 226 // in UTF-8. Therefore, the expansion ratio is 3 at most. Add one for a
234 int length = static_cast<int>(decoded.length()); 227 // trailing '\0'.
235 char* buf = WriteInto(output, length * 3); 228 size_t output_length = decoded.length() * 3 + 1;
236 length = ucnv_toAlgorithmic(UCNV_UTF8, converter, buf, length * 3, 229 char* buf = WriteInto(output, output_length);
237 decoded.data(), length, &err); 230 output_length = ucnv_toAlgorithmic(UCNV_UTF8, converter, buf, output_length,
231 decoded.data(), decoded.length(), &err);
238 ucnv_close(converter); 232 ucnv_close(converter);
239 if (U_FAILURE(err)) { 233 if (U_FAILURE(err))
240 return false; 234 return false;
241 } 235 output->resize(output_length);
242 output->resize(length);
243 return true; 236 return true;
244 } 237 }
245 238
246 bool DecodeWord(const std::string& encoded_word, 239 bool DecodeWord(const std::string& encoded_word,
247 const std::string& referrer_charset, 240 const std::string& referrer_charset,
248 bool* is_rfc2047, 241 bool* is_rfc2047,
249 std::string* output) { 242 std::string* output) {
250 *is_rfc2047 = false; 243 *is_rfc2047 = false;
251 output->clear(); 244 output->clear();
252 if (encoded_word.empty()) 245 if (encoded_word.empty())
(...skipping 2198 matching lines...) Expand 10 before | Expand all | Expand 10 after
2451 2444
2452 NetworkInterface::NetworkInterface(const std::string& name, 2445 NetworkInterface::NetworkInterface(const std::string& name,
2453 const IPAddressNumber& address) 2446 const IPAddressNumber& address)
2454 : name(name), address(address) { 2447 : name(name), address(address) {
2455 } 2448 }
2456 2449
2457 NetworkInterface::~NetworkInterface() { 2450 NetworkInterface::~NetworkInterface() {
2458 } 2451 }
2459 2452
2460 } // namespace net 2453 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698