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

Unified Diff: src/uri.cc

Issue 1994733003: Rewrite decodeURL as builtin function, remove now unused runtime functions. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Move new test 612109.js to regress/ Created 4 years, 7 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/uri.h ('k') | test/cctest/compiler/test-run-intrinsics.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/uri.cc
diff --git a/src/uri.cc b/src/uri.cc
index c459be5e53f4ad915d669a0122cec77b1d1889e2..3924bcc3f0520cbca6b32e2d26c5c835fbe8d8c7 100644
--- a/src/uri.cc
+++ b/src/uri.cc
@@ -60,36 +60,29 @@ void AddHexEncodedToBuffer(uint8_t octet, List<uint8_t>* buffer) {
}
void EncodeSingle(uc16 c, List<uint8_t>* buffer) {
- uint8_t x = (c >> 12) & 0xF;
- uint8_t y = (c >> 6) & 63;
- uint8_t z = c & 63;
- if (c <= 0x007F) {
- AddHexEncodedToBuffer(c, buffer);
- } else if (c <= 0x07FF) {
- AddHexEncodedToBuffer(y + 192, buffer);
- AddHexEncodedToBuffer(z + 128, buffer);
- } else {
- AddHexEncodedToBuffer(x + 224, buffer);
- AddHexEncodedToBuffer(y + 128, buffer);
- AddHexEncodedToBuffer(z + 128, buffer);
+ char s[4];
+ int number_of_bytes;
+ number_of_bytes =
+ unibrow::Utf8::Encode(s, c, unibrow::Utf16::kNoPreviousCharacter, false);
+ for (int k = 0; k < number_of_bytes; k++) {
+ AddHexEncodedToBuffer(s[k], buffer);
}
}
void EncodePair(uc16 cc1, uc16 cc2, List<uint8_t>* buffer) {
- uint8_t u = ((cc1 >> 6) & 0xF) + 1;
- uint8_t w = (cc1 >> 2) & 0xF;
- uint8_t x = cc1 & 3;
- uint8_t y = (cc2 >> 6) & 0xF;
- uint8_t z = cc2 & 63;
- AddHexEncodedToBuffer((u >> 2) + 240, buffer);
- AddHexEncodedToBuffer((((u & 3) << 4) | w) + 128, buffer);
- AddHexEncodedToBuffer(((x << 4) | y) + 128, buffer);
- AddHexEncodedToBuffer(z + 128, buffer);
+ char s[4];
+ int number_of_bytes =
+ unibrow::Utf8::Encode(s, unibrow::Utf16::CombineSurrogatePair(cc1, cc2),
+ unibrow::Utf16::kNoPreviousCharacter, false);
+ for (int k = 0; k < number_of_bytes; k++) {
+ AddHexEncodedToBuffer(s[k], buffer);
+ }
}
} // anonymous namespace
-Object* Uri::Encode(Isolate* isolate, Handle<String> uri, bool is_uri) {
+MaybeHandle<Object> Uri::Encode(Isolate* isolate, Handle<String> uri,
+ bool is_uri) {
uri = String::Flatten(uri);
int uri_length = uri->length();
List<uint8_t> buffer(uri_length);
@@ -120,15 +113,216 @@ Object* Uri::Encode(Isolate* isolate, Handle<String> uri, bool is_uri) {
}
AllowHeapAllocation allocate_error_and_return;
- THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewURIError());
+ THROW_NEW_ERROR(isolate, NewURIError(), Object);
}
}
Handle<String> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ ASSIGN_RETURN_ON_EXCEPTION(
isolate, result,
- isolate->factory()->NewStringFromOneByte(buffer.ToConstVector()));
- return *result;
+ isolate->factory()->NewStringFromOneByte(buffer.ToConstVector()), Object);
+ return result;
+}
+
+namespace { // anonymous namespace for DecodeURI helper functions
+
+bool IsReservedPredicate(uc16 c) {
+ switch (c) {
+ case '#':
+ case '$':
+ case '&':
+ case '+':
+ case ',':
+ case '/':
+ case ':':
+ case ';':
+ case '=':
+ case '?':
+ case '@':
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool IsReplacementCharacter(const uint8_t* octets, int length) {
+ // The replacement character is at codepoint U+FFFD in the Unicode Specials
+ // table. Its UTF-8 encoding is 0xEF 0xBF 0xBD.
+ if (length != 3 || octets[0] != 0xef || octets[1] != 0xbf ||
+ octets[2] != 0xbd) {
+ return false;
+ }
+ return true;
+}
+
+bool DecodeOctets(const uint8_t* octets, int length,
+ List<uc16>* two_byte_buffer) {
+ size_t cursor = 0;
+ uc32 value = unibrow::Utf8::ValueOf(octets, length, &cursor);
+ if (value == unibrow::Utf8::kBadChar &&
+ !IsReplacementCharacter(octets, length)) {
+ return false;
+ }
+
+ if (value <= unibrow::Utf16::kMaxNonSurrogateCharCode) {
+ two_byte_buffer->Add(value);
+ } else {
+ two_byte_buffer->Add(unibrow::Utf16::LeadSurrogate(value));
+ two_byte_buffer->Add(unibrow::Utf16::TrailSurrogate(value));
+ }
+ return true;
+}
+
+bool TwoDigitHex(uc16* decoded, int index, String::FlatContent* uri_content) {
+ char high = HexValue(uri_content->Get(index + 1));
+ char low = HexValue(uri_content->Get(index + 2));
+ if (high < 0 || low < 0) {
+ return false;
+ }
+ *decoded = (high << 4) | low;
+ return true;
+}
+
+template <typename T>
+bool AddToBuffer(uc16 decoded, String::FlatContent* uri_content, int index,
+ bool is_uri, List<T>* buffer) {
+ if (is_uri && IsReservedPredicate(decoded)) {
+ buffer->Add('%');
+ uc16 first = uri_content->Get(index + 1);
+ uc16 second = uri_content->Get(index + 2);
+ if (first > std::numeric_limits<T>::max() ||
Yang 2016/05/23 11:24:59 Do you expect this to happen in some cases? Imo th
Franzi 2016/05/24 15:07:38 You're right. Can never happen. Done.
+ second > std::numeric_limits<T>::max()) {
+ return false;
+ }
+ buffer->Add(first);
+ buffer->Add(second);
+ } else {
+ buffer->Add(decoded);
+ }
+ return true;
+}
+
+bool IntoTwoByte(int index, bool is_uri, int uri_length,
+ String::FlatContent* uri_content,
+ List<uc16>* two_byte_buffer) {
+ for (int k = index; k < uri_length; k++) {
+ uc16 code = uri_content->Get(k);
+ if (code == '%') {
+ uc16 decoded;
+ if (k + 2 >= uri_length || !TwoDigitHex(&decoded, k, uri_content)) {
+ return false;
+ }
+ k += 2;
+ if (decoded > unibrow::Utf8::kMaxOneByteChar) {
+ int n = 1;
+ while ((decoded << n) & 0x80) {
Yang 2016/05/23 11:24:59 Actually, you could merge this while loop with the
Franzi 2016/05/24 15:07:38 Merged. Also renamed n to number_of_continuation_b
+ n++;
+ }
+ if (n == 1 || n > 4 || k + 3 * (n - 1) >= uri_length) {
+ return false;
+ }
+ uint8_t octets[4];
+ int octet_length = 0;
+ octets[octet_length++] = decoded;
+
+ for (int i = 1; i < n; i++) {
+ uc16 decodedTrail;
+
+ if (uri_content->Get(++k) != '%' || k + 2 >= uri_length ||
+ !TwoDigitHex(&decodedTrail, k, uri_content)) {
+ return false;
+ }
+ k += 2;
+ octets[octet_length++] = decodedTrail;
+ }
+
+ if (!DecodeOctets(octets, octet_length, two_byte_buffer)) {
+ return false;
+ }
+ } else {
+ if (!AddToBuffer(decoded, uri_content, k - 2, is_uri,
+ two_byte_buffer)) {
+ return false;
+ }
+ }
+ } else {
+ two_byte_buffer->Add(code);
+ }
+ }
+ return true;
+}
+
+bool IntoOneAndTwoByte(Handle<String> uri, bool is_uri,
+ List<uint8_t>* one_byte_buffer,
+ List<uc16>* two_byte_buffer) {
+ DisallowHeapAllocation no_gc;
+ String::FlatContent uri_content = uri->GetFlatContent();
+
+ int uri_length = uri->length();
+ for (int k = 0; k < uri_length; k++) {
+ uc16 code = uri_content.Get(k);
+ if (code == '%') {
+ uc16 decoded;
+ if (k + 2 >= uri_length || !TwoDigitHex(&decoded, k, &uri_content)) {
+ return false;
+ }
+
+ if (decoded > unibrow::Utf8::kMaxOneByteChar) {
+ return IntoTwoByte(k, is_uri, uri_length, &uri_content,
+ two_byte_buffer);
+ }
+
+ if (!AddToBuffer(decoded, &uri_content, k, is_uri, one_byte_buffer)) {
+ return false;
+ }
+ k += 2;
+ } else {
+ if (code > unibrow::Utf8::kMaxOneByteChar) {
+ return IntoTwoByte(k, is_uri, uri_length, &uri_content,
+ two_byte_buffer);
+ }
+ one_byte_buffer->Add(code);
+ }
+ }
+ return true;
+}
+
+} // anonymous namespace
+
+MaybeHandle<Object> Uri::Decode(Isolate* isolate, Handle<String> uri,
+ bool is_uri) {
+ uri = String::Flatten(uri);
+ List<uint8_t> one_byte_buffer;
+ List<uc16> two_byte_buffer;
+
+ if (!IntoOneAndTwoByte(uri, is_uri, &one_byte_buffer, &two_byte_buffer)) {
+ THROW_NEW_ERROR(isolate, NewURIError(), Object);
+ }
+
+ if (two_byte_buffer.is_empty()) {
+ Handle<SeqOneByteString> result;
+
+ ASSIGN_RETURN_ON_EXCEPTION(
+ isolate, result,
+ isolate->factory()->NewRawOneByteString(one_byte_buffer.length()),
Yang 2016/05/23 11:24:59 You can use NewStringFromOneByte here.
Franzi 2016/05/24 15:07:38 Done.
+ Object);
+ CopyChars(result->GetChars(), one_byte_buffer.ToConstVector().start(),
+ one_byte_buffer.length());
+ return result;
+ }
+
+ Handle<SeqTwoByteString> result;
+ ASSIGN_RETURN_ON_EXCEPTION(
+ isolate, result, isolate->factory()->NewRawTwoByteString(
+ one_byte_buffer.length() + two_byte_buffer.length()),
+ Object);
+
+ CopyChars(result->GetChars(), one_byte_buffer.ToConstVector().start(),
+ one_byte_buffer.length());
+ CopyChars(result->GetChars() + one_byte_buffer.length(),
+ two_byte_buffer.ToConstVector().start(), two_byte_buffer.length());
+
+ return result;
}
} // namespace internal
« no previous file with comments | « src/uri.h ('k') | test/cctest/compiler/test-run-intrinsics.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698