Commit 27b95a92 authored by sammc's avatar sammc Committed by Commit bot

Change net/base/escape.h to use StringPiece.

BUG=678837

Review-Url: https://codereview.chromium.org/2615633007
Cr-Commit-Position: refs/heads/master@{#441791}
parent 2f1d5f56
...@@ -4,13 +4,8 @@ ...@@ -4,13 +4,8 @@
#include "net/base/escape.h" #include "net/base/escape.h"
#include <algorithm>
#include <memory>
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_offset_string_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
namespace net { namespace net {
...@@ -41,7 +36,7 @@ struct Charmap { ...@@ -41,7 +36,7 @@ struct Charmap {
// to +, otherwise, if spaces are in the charmap, they are converted to // to +, otherwise, if spaces are in the charmap, they are converted to
// %20. And if keep_escaped is true, %XX will be kept as it is, otherwise, if // %20. And if keep_escaped is true, %XX will be kept as it is, otherwise, if
// '%' is in the charmap, it is converted to %25. // '%' is in the charmap, it is converted to %25.
std::string Escape(const std::string& text, std::string Escape(base::StringPiece text,
const Charmap& charmap, const Charmap& charmap,
bool use_plus, bool use_plus,
bool keep_escaped = false) { bool keep_escaped = false) {
...@@ -106,8 +101,8 @@ const char kUrlUnescape[128] = { ...@@ -106,8 +101,8 @@ const char kUrlUnescape[128] = {
// Attempts to unescape the sequence at |index| within |escaped_text|. If // Attempts to unescape the sequence at |index| within |escaped_text|. If
// successful, sets |value| to the unescaped value. Returns whether // successful, sets |value| to the unescaped value. Returns whether
// unescaping succeeded. // unescaping succeeded.
template<typename STR> template <typename STR>
bool UnescapeUnsignedCharAtIndex(const STR& escaped_text, bool UnescapeUnsignedCharAtIndex(STR escaped_text,
size_t index, size_t index,
unsigned char* value) { unsigned char* value) {
if ((index + 2) >= escaped_text.size()) if ((index + 2) >= escaped_text.size())
...@@ -128,8 +123,8 @@ bool UnescapeUnsignedCharAtIndex(const STR& escaped_text, ...@@ -128,8 +123,8 @@ bool UnescapeUnsignedCharAtIndex(const STR& escaped_text,
// Returns true if there is an Arabic Language Mark at |index|. |first_byte| // Returns true if there is an Arabic Language Mark at |index|. |first_byte|
// is the byte at |index|. // is the byte at |index|.
template<typename STR> template <typename STR>
bool HasArabicLanguageMarkAtIndex(const STR& escaped_text, bool HasArabicLanguageMarkAtIndex(STR escaped_text,
unsigned char first_byte, unsigned char first_byte,
size_t index) { size_t index) {
if (first_byte != 0xD8) if (first_byte != 0xD8)
...@@ -142,8 +137,8 @@ bool HasArabicLanguageMarkAtIndex(const STR& escaped_text, ...@@ -142,8 +137,8 @@ bool HasArabicLanguageMarkAtIndex(const STR& escaped_text,
// Returns true if there is a BiDi control char at |index|. |first_byte| is the // Returns true if there is a BiDi control char at |index|. |first_byte| is the
// byte at |index|. // byte at |index|.
template<typename STR> template <typename STR>
bool HasThreeByteBidiControlCharAtIndex(const STR& escaped_text, bool HasThreeByteBidiControlCharAtIndex(STR escaped_text,
unsigned char first_byte, unsigned char first_byte,
size_t index) { size_t index) {
if (first_byte != 0xE2) if (first_byte != 0xE2)
...@@ -167,7 +162,7 @@ bool HasThreeByteBidiControlCharAtIndex(const STR& escaped_text, ...@@ -167,7 +162,7 @@ bool HasThreeByteBidiControlCharAtIndex(const STR& escaped_text,
// Returns true if there is a four-byte banned char at |index|. |first_byte| is // Returns true if there is a four-byte banned char at |index|. |first_byte| is
// the byte at |index|. // the byte at |index|.
template <typename STR> template <typename STR>
bool HasFourByteBannedCharAtIndex(const STR& escaped_text, bool HasFourByteBannedCharAtIndex(STR escaped_text,
unsigned char first_byte, unsigned char first_byte,
size_t index) { size_t index) {
// The following characters are blacklisted for spoofability concerns. // The following characters are blacklisted for spoofability concerns.
...@@ -201,16 +196,16 @@ bool HasFourByteBannedCharAtIndex(const STR& escaped_text, ...@@ -201,16 +196,16 @@ bool HasFourByteBannedCharAtIndex(const STR& escaped_text,
// the alterations done to the string that are not one-character-to-one- // the alterations done to the string that are not one-character-to-one-
// character. The resulting |adjustments| will always be sorted by increasing // character. The resulting |adjustments| will always be sorted by increasing
// offset. // offset.
template<typename STR> template <typename STR>
STR UnescapeURLWithAdjustmentsImpl( STR UnescapeURLWithAdjustmentsImpl(
const STR& escaped_text, base::BasicStringPiece<STR> escaped_text,
UnescapeRule::Type rules, UnescapeRule::Type rules,
base::OffsetAdjuster::Adjustments* adjustments) { base::OffsetAdjuster::Adjustments* adjustments) {
if (adjustments) if (adjustments)
adjustments->clear(); adjustments->clear();
// Do not unescape anything, return the |escaped_text| text. // Do not unescape anything, return the |escaped_text| text.
if (rules == UnescapeRule::NONE) if (rules == UnescapeRule::NONE)
return escaped_text; return escaped_text.as_string();
// The output of the unescaping is always smaller than the input, so we can // The output of the unescaping is always smaller than the input, so we can
// reserve the input size to make sure we have enough buffer and don't have // reserve the input size to make sure we have enough buffer and don't have
...@@ -265,19 +260,19 @@ STR UnescapeURLWithAdjustmentsImpl( ...@@ -265,19 +260,19 @@ STR UnescapeURLWithAdjustmentsImpl(
if (!(rules & UnescapeRule::SPOOFING_AND_CONTROL_CHARS)) { if (!(rules & UnescapeRule::SPOOFING_AND_CONTROL_CHARS)) {
if (HasArabicLanguageMarkAtIndex(escaped_text, first_byte, i)) { if (HasArabicLanguageMarkAtIndex(escaped_text, first_byte, i)) {
// Keep Arabic Language Mark escaped. // Keep Arabic Language Mark escaped.
result.append(escaped_text, i, 6); escaped_text.substr(i, 6).AppendToString(&result);
i += 5; i += 5;
continue; continue;
} }
if (HasThreeByteBidiControlCharAtIndex(escaped_text, first_byte, i)) { if (HasThreeByteBidiControlCharAtIndex(escaped_text, first_byte, i)) {
// Keep BiDi control char escaped. // Keep BiDi control char escaped.
result.append(escaped_text, i, 9); escaped_text.substr(i, 9).AppendToString(&result);
i += 8; i += 8;
continue; continue;
} }
if (HasFourByteBannedCharAtIndex(escaped_text, first_byte, i)) { if (HasFourByteBannedCharAtIndex(escaped_text, first_byte, i)) {
// Keep banned char escaped. // Keep banned char escaped.
result.append(escaped_text, i, 12); escaped_text.substr(i, 12).AppendToString(&result);
i += 11; i += 11;
continue; continue;
} }
...@@ -345,12 +340,13 @@ void AppendEscapedCharForHTMLImpl(typename str::value_type c, str* output) { ...@@ -345,12 +340,13 @@ void AppendEscapedCharForHTMLImpl(typename str::value_type c, str* output) {
} }
template <class str> template <class str>
str EscapeForHTMLImpl(const str& input) { str EscapeForHTMLImpl(base::BasicStringPiece<str> input) {
str result; str result;
result.reserve(input.size()); // Optimize for no escaping. result.reserve(input.size()); // Optimize for no escaping.
for (typename str::const_iterator i = input.begin(); i != input.end(); ++i) for (auto c : input) {
AppendEscapedCharForHTMLImpl(*i, &result); AppendEscapedCharForHTMLImpl(c, &result);
}
return result; return result;
} }
...@@ -397,29 +393,29 @@ static const Charmap kExternalHandlerCharmap = {{ ...@@ -397,29 +393,29 @@ static const Charmap kExternalHandlerCharmap = {{
} // namespace } // namespace
std::string EscapeQueryParamValue(const std::string& text, bool use_plus) { std::string EscapeQueryParamValue(base::StringPiece text, bool use_plus) {
return Escape(text, kQueryCharmap, use_plus); return Escape(text, kQueryCharmap, use_plus);
} }
std::string EscapePath(const std::string& path) { std::string EscapePath(base::StringPiece path) {
return Escape(path, kPathCharmap, false); return Escape(path, kPathCharmap, false);
} }
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
std::string EscapeNSURLPrecursor(const std::string& precursor) { std::string EscapeNSURLPrecursor(base::StringPiece precursor) {
return Escape(precursor, kNSURLCharmap, false, true); return Escape(precursor, kNSURLCharmap, false, true);
} }
#endif // defined(OS_MACOSX) #endif // defined(OS_MACOSX)
std::string EscapeUrlEncodedData(const std::string& path, bool use_plus) { std::string EscapeUrlEncodedData(base::StringPiece path, bool use_plus) {
return Escape(path, kUrlEscape, use_plus); return Escape(path, kUrlEscape, use_plus);
} }
std::string EscapeNonASCII(const std::string& input) { std::string EscapeNonASCII(base::StringPiece input) {
return Escape(input, kNonASCIICharmap, false); return Escape(input, kNonASCIICharmap, false);
} }
std::string EscapeExternalHandlerValue(const std::string& text) { std::string EscapeExternalHandlerValue(base::StringPiece text) {
return Escape(text, kExternalHandlerCharmap, false, true); return Escape(text, kExternalHandlerCharmap, false, true);
} }
...@@ -427,31 +423,31 @@ void AppendEscapedCharForHTML(char c, std::string* output) { ...@@ -427,31 +423,31 @@ void AppendEscapedCharForHTML(char c, std::string* output) {
AppendEscapedCharForHTMLImpl(c, output); AppendEscapedCharForHTMLImpl(c, output);
} }
std::string EscapeForHTML(const std::string& input) { std::string EscapeForHTML(base::StringPiece input) {
return EscapeForHTMLImpl(input); return EscapeForHTMLImpl(input);
} }
base::string16 EscapeForHTML(const base::string16& input) { base::string16 EscapeForHTML(base::StringPiece16 input) {
return EscapeForHTMLImpl(input); return EscapeForHTMLImpl(input);
} }
std::string UnescapeURLComponent(const std::string& escaped_text, std::string UnescapeURLComponent(base::StringPiece escaped_text,
UnescapeRule::Type rules) { UnescapeRule::Type rules) {
return UnescapeURLWithAdjustmentsImpl(escaped_text, rules, NULL); return UnescapeURLWithAdjustmentsImpl(escaped_text, rules, NULL);
} }
base::string16 UnescapeURLComponent(const base::string16& escaped_text, base::string16 UnescapeURLComponent(base::StringPiece16 escaped_text,
UnescapeRule::Type rules) { UnescapeRule::Type rules) {
return UnescapeURLWithAdjustmentsImpl(escaped_text, rules, NULL); return UnescapeURLWithAdjustmentsImpl(escaped_text, rules, NULL);
} }
base::string16 UnescapeAndDecodeUTF8URLComponent(const std::string& text, base::string16 UnescapeAndDecodeUTF8URLComponent(base::StringPiece text,
UnescapeRule::Type rules) { UnescapeRule::Type rules) {
return UnescapeAndDecodeUTF8URLComponentWithAdjustments(text, rules, NULL); return UnescapeAndDecodeUTF8URLComponentWithAdjustments(text, rules, NULL);
} }
base::string16 UnescapeAndDecodeUTF8URLComponentWithAdjustments( base::string16 UnescapeAndDecodeUTF8URLComponentWithAdjustments(
const std::string& text, base::StringPiece text,
UnescapeRule::Type rules, UnescapeRule::Type rules,
base::OffsetAdjuster::Adjustments* adjustments) { base::OffsetAdjuster::Adjustments* adjustments) {
base::string16 result; base::string16 result;
...@@ -472,7 +468,7 @@ base::string16 UnescapeAndDecodeUTF8URLComponentWithAdjustments( ...@@ -472,7 +468,7 @@ base::string16 UnescapeAndDecodeUTF8URLComponentWithAdjustments(
return base::UTF8ToUTF16WithAdjustments(text, adjustments); return base::UTF8ToUTF16WithAdjustments(text, adjustments);
} }
base::string16 UnescapeForHTML(const base::string16& input) { base::string16 UnescapeForHTML(base::StringPiece16 input) {
static const struct { static const struct {
const char* ampersand_code; const char* ampersand_code;
const char replacement; const char replacement;
...@@ -485,10 +481,10 @@ base::string16 UnescapeForHTML(const base::string16& input) { ...@@ -485,10 +481,10 @@ base::string16 UnescapeForHTML(const base::string16& input) {
}; };
if (input.find(base::ASCIIToUTF16("&")) == std::string::npos) if (input.find(base::ASCIIToUTF16("&")) == std::string::npos)
return input; return input.as_string();
base::string16 ampersand_chars[arraysize(kEscapeToChars)]; base::string16 ampersand_chars[arraysize(kEscapeToChars)];
base::string16 text(input); base::string16 text = input.as_string();
for (base::string16::iterator iter = text.begin(); for (base::string16::iterator iter = text.begin();
iter != text.end(); ++iter) { iter != text.end(); ++iter) {
if (*iter == '&') { if (*iter == '&') {
......
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <vector>
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/string_piece.h"
#include "base/strings/utf_offset_string_conversions.h" #include "base/strings/utf_offset_string_conversions.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
...@@ -22,44 +22,44 @@ namespace net { ...@@ -22,44 +22,44 @@ namespace net {
// We %XX everything except alphanumerics and -_.!~*'() // We %XX everything except alphanumerics and -_.!~*'()
// Spaces change to "+" unless you pass usePlus=false. // Spaces change to "+" unless you pass usePlus=false.
// This is basically the same as encodeURIComponent in javascript. // This is basically the same as encodeURIComponent in javascript.
NET_EXPORT std::string EscapeQueryParamValue(const std::string& text, NET_EXPORT std::string EscapeQueryParamValue(base::StringPiece text,
bool use_plus); bool use_plus);
// Escapes a partial or complete file/pathname. This includes: // Escapes a partial or complete file/pathname. This includes:
// non-printable, non-7bit, and (including space) "#%:<>?[\]^`{|} // non-printable, non-7bit, and (including space) "#%:<>?[\]^`{|}
// For the base::string16 version, we attempt a conversion to |codepage| before // For the base::string16 version, we attempt a conversion to |codepage| before
// encoding the string. If this conversion fails, we return false. // encoding the string. If this conversion fails, we return false.
NET_EXPORT std::string EscapePath(const std::string& path); NET_EXPORT std::string EscapePath(base::StringPiece path);
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// Escapes characters as per expectations of NSURL. This includes: // Escapes characters as per expectations of NSURL. This includes:
// non-printable, non-7bit, and (including space) "#%<>[\]^`{|} // non-printable, non-7bit, and (including space) "#%<>[\]^`{|}
NET_EXPORT std::string EscapeNSURLPrecursor(const std::string& precursor); NET_EXPORT std::string EscapeNSURLPrecursor(base::StringPiece precursor);
#endif // defined(OS_MACOSX) #endif // defined(OS_MACOSX)
// Escapes application/x-www-form-urlencoded content. This includes: // Escapes application/x-www-form-urlencoded content. This includes:
// non-printable, non-7bit, and (including space) ?>=<;+'&%$#"![\]^`{|} // non-printable, non-7bit, and (including space) ?>=<;+'&%$#"![\]^`{|}
// Space is escaped as + (if use_plus is true) and other special characters // Space is escaped as + (if use_plus is true) and other special characters
// as %XX (hex). // as %XX (hex).
NET_EXPORT std::string EscapeUrlEncodedData(const std::string& path, NET_EXPORT std::string EscapeUrlEncodedData(base::StringPiece path,
bool use_plus); bool use_plus);
// Escapes all non-ASCII input. // Escapes all non-ASCII input.
NET_EXPORT std::string EscapeNonASCII(const std::string& input); NET_EXPORT std::string EscapeNonASCII(base::StringPiece input);
// Escapes characters in text suitable for use as an external protocol handler // Escapes characters in text suitable for use as an external protocol handler
// command. // command.
// We %XX everything except alphanumerics and -_.!~*'() and the restricted // We %XX everything except alphanumerics and -_.!~*'() and the restricted
// chracters (;/?:@&=+$,#[]) and a valid percent escape sequence (%XX). // chracters (;/?:@&=+$,#[]) and a valid percent escape sequence (%XX).
NET_EXPORT std::string EscapeExternalHandlerValue(const std::string& text); NET_EXPORT std::string EscapeExternalHandlerValue(base::StringPiece text);
// Appends the given character to the output string, escaping the character if // Appends the given character to the output string, escaping the character if
// the character would be interpretted as an HTML delimiter. // the character would be interpretted as an HTML delimiter.
NET_EXPORT void AppendEscapedCharForHTML(char c, std::string* output); NET_EXPORT void AppendEscapedCharForHTML(char c, std::string* output);
// Escapes chars that might cause this text to be interpretted as HTML tags. // Escapes chars that might cause this text to be interpretted as HTML tags.
NET_EXPORT std::string EscapeForHTML(const std::string& text); NET_EXPORT std::string EscapeForHTML(base::StringPiece text);
NET_EXPORT base::string16 EscapeForHTML(const base::string16& text); NET_EXPORT base::string16 EscapeForHTML(base::StringPiece16 text);
// Unescaping ------------------------------------------------------------------ // Unescaping ------------------------------------------------------------------
...@@ -125,11 +125,10 @@ class UnescapeRule { ...@@ -125,11 +125,10 @@ class UnescapeRule {
// which, after unescaping, is supposed to be interpreted as UTF-8, and then // which, after unescaping, is supposed to be interpreted as UTF-8, and then
// converted into full UTF-16 chars. This function won't tell you if any // converted into full UTF-16 chars. This function won't tell you if any
// conversions need to take place, it only unescapes. // conversions need to take place, it only unescapes.
NET_EXPORT std::string UnescapeURLComponent(const std::string& escaped_text, NET_EXPORT std::string UnescapeURLComponent(base::StringPiece escaped_text,
UnescapeRule::Type rules); UnescapeRule::Type rules);
NET_EXPORT base::string16 UnescapeURLComponent( NET_EXPORT base::string16 UnescapeURLComponent(base::StringPiece16 escaped_text,
const base::string16& escaped_text, UnescapeRule::Type rules);
UnescapeRule::Type rules);
// Unescapes the given substring as a URL, and then tries to interpret the // Unescapes the given substring as a URL, and then tries to interpret the
// result as being encoded as UTF-8. If the result is convertable into UTF-8, it // result as being encoded as UTF-8. If the result is convertable into UTF-8, it
...@@ -138,16 +137,16 @@ NET_EXPORT base::string16 UnescapeURLComponent( ...@@ -138,16 +137,16 @@ NET_EXPORT base::string16 UnescapeURLComponent(
// information on how the original string was adjusted to get the string // information on how the original string was adjusted to get the string
// returned. // returned.
NET_EXPORT base::string16 UnescapeAndDecodeUTF8URLComponent( NET_EXPORT base::string16 UnescapeAndDecodeUTF8URLComponent(
const std::string& text, base::StringPiece text,
UnescapeRule::Type rules); UnescapeRule::Type rules);
NET_EXPORT base::string16 UnescapeAndDecodeUTF8URLComponentWithAdjustments( NET_EXPORT base::string16 UnescapeAndDecodeUTF8URLComponentWithAdjustments(
const std::string& text, base::StringPiece text,
UnescapeRule::Type rules, UnescapeRule::Type rules,
base::OffsetAdjuster::Adjustments* adjustments); base::OffsetAdjuster::Adjustments* adjustments);
// Unescapes the following ampersand character codes from |text|: // Unescapes the following ampersand character codes from |text|:
// &lt; &gt; &amp; &quot; &#39; // &lt; &gt; &amp; &quot; &#39;
NET_EXPORT base::string16 UnescapeForHTML(const base::string16& text); NET_EXPORT base::string16 UnescapeForHTML(base::StringPiece16 text);
} // namespace net } // namespace net
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment