| OLD | NEW |
| 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/escape.h" | 5 #include "net/base/escape.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 inline char IntToHex(int i) { | 21 inline char IntToHex(int i) { |
| 22 DCHECK_GE(i, 0) << i << " not a hex value"; | 22 DCHECK_GE(i, 0) << i << " not a hex value"; |
| 23 DCHECK_LE(i, 15) << i << " not a hex value"; | 23 DCHECK_LE(i, 15) << i << " not a hex value"; |
| 24 return kHexString[i]; | 24 return kHexString[i]; |
| 25 } | 25 } |
| 26 | 26 |
| 27 // A fast bit-vector map for ascii characters. | 27 // A fast bit-vector map for ascii characters. |
| 28 // | 28 // |
| 29 // Internally stores 256 bits in an array of 8 ints. | 29 // Internally stores 256 bits in an array of 8 ints. |
| 30 // Does quick bit-flicking to lookup needed characters. | 30 // Does quick bit-flicking to lookup needed characters. |
| 31 class Charmap { | 31 struct Charmap { |
| 32 public: | 32 bool Contains(unsigned char c) const { |
| 33 Charmap(uint32 b0, uint32 b1, uint32 b2, uint32 b3, | 33 return (map[c >> 5] & (1 << (c & 31))) ? true : false; |
| 34 uint32 b4, uint32 b5, uint32 b6, uint32 b7) { | |
| 35 map_[0] = b0; map_[1] = b1; map_[2] = b2; map_[3] = b3; | |
| 36 map_[4] = b4; map_[5] = b5; map_[6] = b6; map_[7] = b7; | |
| 37 } | 34 } |
| 38 | 35 |
| 39 bool Contains(unsigned char c) const { | 36 uint32 map[8]; |
| 40 return (map_[c >> 5] & (1 << (c & 31))) ? true : false; | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 uint32 map_[8]; | |
| 45 }; | 37 }; |
| 46 | 38 |
| 47 // Given text to escape and a Charmap defining which values to escape, | 39 // Given text to escape and a Charmap defining which values to escape, |
| 48 // return an escaped string. If use_plus is true, spaces are converted | 40 // return an escaped string. If use_plus is true, spaces are converted |
| 49 // to +, otherwise, if spaces are in the charmap, they are converted to | 41 // to +, otherwise, if spaces are in the charmap, they are converted to |
| 50 // %20. | 42 // %20. |
| 51 std::string Escape(const std::string& text, const Charmap& charmap, | 43 std::string Escape(const std::string& text, const Charmap& charmap, |
| 52 bool use_plus) { | 44 bool use_plus) { |
| 53 std::string escaped; | 45 std::string escaped; |
| 54 escaped.reserve(text.length() * 3); | 46 escaped.reserve(text.length() * 3); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 result.reserve(input.size()); // Optimize for no escaping. | 208 result.reserve(input.size()); // Optimize for no escaping. |
| 217 | 209 |
| 218 for (typename str::const_iterator i = input.begin(); i != input.end(); ++i) | 210 for (typename str::const_iterator i = input.begin(); i != input.end(); ++i) |
| 219 AppendEscapedCharForHTMLImpl(*i, &result); | 211 AppendEscapedCharForHTMLImpl(*i, &result); |
| 220 | 212 |
| 221 return result; | 213 return result; |
| 222 } | 214 } |
| 223 | 215 |
| 224 // Everything except alphanumerics and !'()*-._~ | 216 // Everything except alphanumerics and !'()*-._~ |
| 225 // See RFC 2396 for the list of reserved characters. | 217 // See RFC 2396 for the list of reserved characters. |
| 226 static const Charmap kQueryCharmap( | 218 static const Charmap kQueryCharmap = {{ |
| 227 0xffffffffL, 0xfc00987dL, 0x78000001L, 0xb8000001L, | 219 0xffffffffL, 0xfc00987dL, 0x78000001L, 0xb8000001L, |
| 228 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL); | 220 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 221 }}; |
| 229 | 222 |
| 230 // non-printable, non-7bit, and (including space) "#%:<>?[\]^`{|} | 223 // non-printable, non-7bit, and (including space) "#%:<>?[\]^`{|} |
| 231 static const Charmap kPathCharmap( | 224 static const Charmap kPathCharmap = {{ |
| 232 0xffffffffL, 0xd400002dL, 0x78000000L, 0xb8000001L, | 225 0xffffffffL, 0xd400002dL, 0x78000000L, 0xb8000001L, |
| 233 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL); | 226 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 227 }}; |
| 234 | 228 |
| 235 // non-printable, non-7bit, and (including space) ?>=<;+'&%$#"![\]^`{|} | 229 // non-printable, non-7bit, and (including space) ?>=<;+'&%$#"![\]^`{|} |
| 236 static const Charmap kUrlEscape( | 230 static const Charmap kUrlEscape = {{ |
| 237 0xffffffffL, 0xf80008fdL, 0x78000001L, 0xb8000001L, | 231 0xffffffffL, 0xf80008fdL, 0x78000001L, 0xb8000001L, |
| 238 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL | 232 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 239 ); | 233 }}; |
| 240 | 234 |
| 241 // non-7bit | 235 // non-7bit |
| 242 static const Charmap kNonASCIICharmap( | 236 static const Charmap kNonASCIICharmap = {{ |
| 243 0x00000000L, 0x00000000L, 0x00000000L, 0x00000000L, | 237 0x00000000L, 0x00000000L, 0x00000000L, 0x00000000L, |
| 244 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL); | 238 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 239 }}; |
| 245 | 240 |
| 246 // Everything except alphanumerics, the reserved characters(;/?:@&=+$,) and | 241 // Everything except alphanumerics, the reserved characters(;/?:@&=+$,) and |
| 247 // !'()*-._~% | 242 // !'()*-._~% |
| 248 static const Charmap kExternalHandlerCharmap( | 243 static const Charmap kExternalHandlerCharmap = {{ |
| 249 0xffffffffL, 0x5000080dL, 0x68000000L, 0xb8000001L, | 244 0xffffffffL, 0x5000080dL, 0x68000000L, 0xb8000001L, |
| 250 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL); | 245 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 246 }}; |
| 251 | 247 |
| 252 } // namespace | 248 } // namespace |
| 253 | 249 |
| 254 std::string EscapePath(const std::string& path) { | 250 std::string EscapePath(const std::string& path) { |
| 255 return Escape(path, kPathCharmap, false); | 251 return Escape(path, kPathCharmap, false); |
| 256 } | 252 } |
| 257 | 253 |
| 258 std::string EscapeUrlEncodedData(const std::string& path, bool use_plus) { | 254 std::string EscapeUrlEncodedData(const std::string& path, bool use_plus) { |
| 259 return Escape(path, kUrlEscape, use_plus); | 255 return Escape(path, kUrlEscape, use_plus); |
| 260 } | 256 } |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 return; | 386 return; |
| 391 } | 387 } |
| 392 adjusted_offset -= 2; | 388 adjusted_offset -= 2; |
| 393 } | 389 } |
| 394 offset = adjusted_offset; | 390 offset = adjusted_offset; |
| 395 } | 391 } |
| 396 | 392 |
| 397 } // namespace internal | 393 } // namespace internal |
| 398 | 394 |
| 399 } // namespace net | 395 } // namespace net |
| OLD | NEW |