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

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 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
« no previous file with comments | « media/audio/win/audio_manager_win.cc ('k') | net/base/x509_certificate_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 155
156 // FTP overrides the following restricted ports. 156 // FTP overrides the following restricted ports.
157 static const int kAllowedFtpPorts[] = { 157 static const int kAllowedFtpPorts[] = {
158 21, // ftp data 158 21, // ftp data
159 22, // ssh 159 22, // ssh
160 }; 160 };
161 161
162 std::string::size_type CountTrailingChars( 162 std::string::size_type CountTrailingChars(
163 const std::string input, 163 const std::string input,
164 const std::string::value_type trailing_chars[]) { 164 const std::string::value_type trailing_chars[]) {
165 const std::string::size_type last_good_char = 165 const size_t last_good_char = input.find_last_not_of(trailing_chars);
166 input.find_last_not_of(trailing_chars); 166 return (last_good_char == std::string::npos) ?
167 167 input.length() : (input.length() - last_good_char - 1);
168 if (last_good_char == std::string::npos)
169 return input.length();
170 else
171 return input.length() - last_good_char - 1;
172 } 168 }
173 169
174 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence 170 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence
175 // of bytes. If input is invalid, return false. 171 // of bytes. If input is invalid, return false.
176 bool QPDecode(const std::string& input, std::string* output) { 172 bool QPDecode(const std::string& input, std::string* output) {
177 std::string temp; 173 std::string temp;
178 temp.reserve(input.size()); 174 temp.reserve(input.size());
179 std::string::const_iterator it = input.begin(); 175 for (std::string::const_iterator it = input.begin(); it != input.end();
180 while (it != input.end()) { 176 ++it) {
181 if (*it == '_') { 177 if (*it == '_') {
182 temp.push_back(' '); 178 temp.push_back(' ');
183 } else if (*it == '=') { 179 } else if (*it == '=') {
184 if (input.end() - it < 3) { 180 if ((input.end() - it < 3) ||
181 !IsHexDigit(static_cast<unsigned char>(*(it + 1))) ||
182 !IsHexDigit(static_cast<unsigned char>(*(it + 2))))
185 return false; 183 return false;
186 } 184 unsigned char ch = HexDigitToInt(*(it + 1)) * 16 +
187 if (IsHexDigit(static_cast<unsigned char>(*(it + 1))) && 185 HexDigitToInt(*(it + 2));
188 IsHexDigit(static_cast<unsigned char>(*(it + 2)))) { 186 temp.push_back(static_cast<char>(ch));
189 unsigned char ch = HexDigitToInt(*(it + 1)) * 16 + 187 ++it;
190 HexDigitToInt(*(it + 2)); 188 ++it;
191 temp.push_back(static_cast<char>(ch));
192 ++it;
193 ++it;
194 } else {
195 return false;
196 }
197 } else if (0x20 < *it && *it < 0x7F) { 189 } else if (0x20 < *it && *it < 0x7F) {
198 // In a Q-encoded word, only printable ASCII characters 190 // In a Q-encoded word, only printable ASCII characters
199 // represent themselves. Besides, space, '=', '_' and '?' are 191 // represent themselves. Besides, space, '=', '_' and '?' are
200 // not allowed, but they're already filtered out. 192 // not allowed, but they're already filtered out.
201 DCHECK(*it != 0x3D && *it != 0x5F && *it != 0x3F); 193 DCHECK_NE('=', *it);
194 DCHECK_NE('?', *it);
195 DCHECK_NE('_', *it);
202 temp.push_back(*it); 196 temp.push_back(*it);
203 } else { 197 } else {
204 return false; 198 return false;
205 } 199 }
206 ++it;
207 } 200 }
208 output->swap(temp); 201 output->swap(temp);
209 return true; 202 return true;
210 } 203 }
211 204
212 enum RFC2047EncodingType {Q_ENCODING, B_ENCODING}; 205 enum RFC2047EncodingType {Q_ENCODING, B_ENCODING};
213 bool DecodeBQEncoding(const std::string& part, RFC2047EncodingType enc_type, 206 bool DecodeBQEncoding(const std::string& part,
214 const std::string& charset, std::string* output) { 207 RFC2047EncodingType enc_type,
208 const std::string& charset,
209 std::string* output) {
215 std::string decoded; 210 std::string decoded;
216 if (enc_type == B_ENCODING) { 211 if (!((enc_type == B_ENCODING) ?
217 if (!base::Base64Decode(part, &decoded)) { 212 base::Base64Decode(part, &decoded) : QPDecode(part, &decoded)))
218 return false; 213 return false;
219 } 214
220 } else { 215 if (decoded.empty()) {
221 if (!QPDecode(part, &decoded)) { 216 output->clear();
222 return false; 217 return true;
223 }
224 } 218 }
225 219
226 UErrorCode err = U_ZERO_ERROR; 220 UErrorCode err = U_ZERO_ERROR;
227 UConverter* converter(ucnv_open(charset.c_str(), &err)); 221 UConverter* converter(ucnv_open(charset.c_str(), &err));
228 if (U_FAILURE(err)) { 222 if (U_FAILURE(err))
229 return false; 223 return false;
230 }
231 224
232 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8. 225 // A single byte in a legacy encoding can be expanded to 3 bytes in UTF-8.
233 // A 'two-byte character' in a legacy encoding can be expanded to 4 bytes 226 // A 'two-byte character' in a legacy encoding can be expanded to 4 bytes
234 // in UTF-8. Therefore, the expansion ratio is 3 at most. 227 // in UTF-8. Therefore, the expansion ratio is 3 at most. Add one for a
235 int length = static_cast<int>(decoded.length()); 228 // trailing '\0'.
236 char* buf = WriteInto(output, length * 3); 229 size_t output_length = decoded.length() * 3 + 1;
237 length = ucnv_toAlgorithmic(UCNV_UTF8, converter, buf, length * 3, 230 char* buf = WriteInto(output, output_length);
238 decoded.data(), length, &err); 231 output_length = ucnv_toAlgorithmic(UCNV_UTF8, converter, buf, output_length,
232 decoded.data(), decoded.length(), &err);
239 ucnv_close(converter); 233 ucnv_close(converter);
240 if (U_FAILURE(err)) { 234 if (U_FAILURE(err))
241 return false; 235 return false;
242 } 236 output->resize(output_length);
243 output->resize(length);
244 return true; 237 return true;
245 } 238 }
246 239
247 bool DecodeWord(const std::string& encoded_word, 240 bool DecodeWord(const std::string& encoded_word,
248 const std::string& referrer_charset, 241 const std::string& referrer_charset,
249 bool* is_rfc2047, 242 bool* is_rfc2047,
250 std::string* output) { 243 std::string* output) {
251 *is_rfc2047 = false; 244 *is_rfc2047 = false;
252 output->clear(); 245 output->clear();
253 if (encoded_word.empty()) 246 if (encoded_word.empty())
(...skipping 2207 matching lines...) Expand 10 before | Expand all | Expand 10 after
2461 2454
2462 NetworkInterface::NetworkInterface(const std::string& name, 2455 NetworkInterface::NetworkInterface(const std::string& name,
2463 const IPAddressNumber& address) 2456 const IPAddressNumber& address)
2464 : name(name), address(address) { 2457 : name(name), address(address) {
2465 } 2458 }
2466 2459
2467 NetworkInterface::~NetworkInterface() { 2460 NetworkInterface::~NetworkInterface() {
2468 } 2461 }
2469 2462
2470 } // namespace net 2463 } // namespace net
OLDNEW
« no previous file with comments | « media/audio/win/audio_manager_win.cc ('k') | net/base/x509_certificate_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698