| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project 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 "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
| 8 #include "src/conversions.h" | 8 #include "src/conversions.h" |
| 9 #include "src/runtime/runtime-utils.h" | 9 #include "src/runtime/runtime-utils.h" |
| 10 #include "src/string-search.h" | 10 #include "src/string-search.h" |
| 11 #include "src/utils.h" | 11 #include "src/utils.h" |
| 12 | 12 |
| 13 | 13 |
| 14 namespace v8 { | 14 namespace v8 { |
| 15 namespace internal { | 15 namespace internal { |
| 16 | 16 |
| 17 template <typename Char> | |
| 18 static INLINE(Vector<const Char> GetCharVector(Handle<String> string)); | |
| 19 | |
| 20 | |
| 21 template <> | |
| 22 Vector<const uint8_t> GetCharVector(Handle<String> string) { | |
| 23 String::FlatContent flat = string->GetFlatContent(); | |
| 24 DCHECK(flat.IsOneByte()); | |
| 25 return flat.ToOneByteVector(); | |
| 26 } | |
| 27 | |
| 28 | |
| 29 template <> | |
| 30 Vector<const uc16> GetCharVector(Handle<String> string) { | |
| 31 String::FlatContent flat = string->GetFlatContent(); | |
| 32 DCHECK(flat.IsTwoByte()); | |
| 33 return flat.ToUC16Vector(); | |
| 34 } | |
| 35 | |
| 36 | |
| 37 class URIUnescape : public AllStatic { | 17 class URIUnescape : public AllStatic { |
| 38 public: | 18 public: |
| 39 template <typename Char> | 19 template <typename Char> |
| 40 MUST_USE_RESULT static MaybeHandle<String> Unescape(Isolate* isolate, | 20 MUST_USE_RESULT static MaybeHandle<String> Unescape(Isolate* isolate, |
| 41 Handle<String> source); | 21 Handle<String> source); |
| 42 | 22 |
| 43 private: | 23 private: |
| 44 static const signed char kHexValue['g']; | 24 static const signed char kHexValue['g']; |
| 45 | 25 |
| 46 template <typename Char> | 26 template <typename Char> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 65 -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15}; | 45 -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15}; |
| 66 | 46 |
| 67 | 47 |
| 68 template <typename Char> | 48 template <typename Char> |
| 69 MaybeHandle<String> URIUnescape::Unescape(Isolate* isolate, | 49 MaybeHandle<String> URIUnescape::Unescape(Isolate* isolate, |
| 70 Handle<String> source) { | 50 Handle<String> source) { |
| 71 int index; | 51 int index; |
| 72 { | 52 { |
| 73 DisallowHeapAllocation no_allocation; | 53 DisallowHeapAllocation no_allocation; |
| 74 StringSearch<uint8_t, Char> search(isolate, STATIC_CHAR_VECTOR("%")); | 54 StringSearch<uint8_t, Char> search(isolate, STATIC_CHAR_VECTOR("%")); |
| 75 index = search.Search(GetCharVector<Char>(source), 0); | 55 index = search.Search(source->GetCharVector<Char>(), 0); |
| 76 if (index < 0) return source; | 56 if (index < 0) return source; |
| 77 } | 57 } |
| 78 return UnescapeSlow<Char>(isolate, source, index); | 58 return UnescapeSlow<Char>(isolate, source, index); |
| 79 } | 59 } |
| 80 | 60 |
| 81 | 61 |
| 82 template <typename Char> | 62 template <typename Char> |
| 83 MaybeHandle<String> URIUnescape::UnescapeSlow(Isolate* isolate, | 63 MaybeHandle<String> URIUnescape::UnescapeSlow(Isolate* isolate, |
| 84 Handle<String> string, | 64 Handle<String> string, |
| 85 int start_index) { | 65 int start_index) { |
| 86 bool one_byte = true; | 66 bool one_byte = true; |
| 87 int length = string->length(); | 67 int length = string->length(); |
| 88 | 68 |
| 89 int unescaped_length = 0; | 69 int unescaped_length = 0; |
| 90 { | 70 { |
| 91 DisallowHeapAllocation no_allocation; | 71 DisallowHeapAllocation no_allocation; |
| 92 Vector<const Char> vector = GetCharVector<Char>(string); | 72 Vector<const Char> vector = string->GetCharVector<Char>(); |
| 93 for (int i = start_index; i < length; unescaped_length++) { | 73 for (int i = start_index; i < length; unescaped_length++) { |
| 94 int step; | 74 int step; |
| 95 if (UnescapeChar(vector, i, length, &step) > | 75 if (UnescapeChar(vector, i, length, &step) > |
| 96 String::kMaxOneByteCharCode) { | 76 String::kMaxOneByteCharCode) { |
| 97 one_byte = false; | 77 one_byte = false; |
| 98 } | 78 } |
| 99 i += step; | 79 i += step; |
| 100 } | 80 } |
| 101 } | 81 } |
| 102 | 82 |
| 103 DCHECK(start_index < length); | 83 DCHECK(start_index < length); |
| 104 Handle<String> first_part = | 84 Handle<String> first_part = |
| 105 isolate->factory()->NewProperSubString(string, 0, start_index); | 85 isolate->factory()->NewProperSubString(string, 0, start_index); |
| 106 | 86 |
| 107 int dest_position = 0; | 87 int dest_position = 0; |
| 108 Handle<String> second_part; | 88 Handle<String> second_part; |
| 109 DCHECK(unescaped_length <= String::kMaxLength); | 89 DCHECK(unescaped_length <= String::kMaxLength); |
| 110 if (one_byte) { | 90 if (one_byte) { |
| 111 Handle<SeqOneByteString> dest = isolate->factory() | 91 Handle<SeqOneByteString> dest = isolate->factory() |
| 112 ->NewRawOneByteString(unescaped_length) | 92 ->NewRawOneByteString(unescaped_length) |
| 113 .ToHandleChecked(); | 93 .ToHandleChecked(); |
| 114 DisallowHeapAllocation no_allocation; | 94 DisallowHeapAllocation no_allocation; |
| 115 Vector<const Char> vector = GetCharVector<Char>(string); | 95 Vector<const Char> vector = string->GetCharVector<Char>(); |
| 116 for (int i = start_index; i < length; dest_position++) { | 96 for (int i = start_index; i < length; dest_position++) { |
| 117 int step; | 97 int step; |
| 118 dest->SeqOneByteStringSet(dest_position, | 98 dest->SeqOneByteStringSet(dest_position, |
| 119 UnescapeChar(vector, i, length, &step)); | 99 UnescapeChar(vector, i, length, &step)); |
| 120 i += step; | 100 i += step; |
| 121 } | 101 } |
| 122 second_part = dest; | 102 second_part = dest; |
| 123 } else { | 103 } else { |
| 124 Handle<SeqTwoByteString> dest = isolate->factory() | 104 Handle<SeqTwoByteString> dest = isolate->factory() |
| 125 ->NewRawTwoByteString(unescaped_length) | 105 ->NewRawTwoByteString(unescaped_length) |
| 126 .ToHandleChecked(); | 106 .ToHandleChecked(); |
| 127 DisallowHeapAllocation no_allocation; | 107 DisallowHeapAllocation no_allocation; |
| 128 Vector<const Char> vector = GetCharVector<Char>(string); | 108 Vector<const Char> vector = string->GetCharVector<Char>(); |
| 129 for (int i = start_index; i < length; dest_position++) { | 109 for (int i = start_index; i < length; dest_position++) { |
| 130 int step; | 110 int step; |
| 131 dest->SeqTwoByteStringSet(dest_position, | 111 dest->SeqTwoByteStringSet(dest_position, |
| 132 UnescapeChar(vector, i, length, &step)); | 112 UnescapeChar(vector, i, length, &step)); |
| 133 i += step; | 113 i += step; |
| 134 } | 114 } |
| 135 second_part = dest; | 115 second_part = dest; |
| 136 } | 116 } |
| 137 return isolate->factory()->NewConsString(first_part, second_part); | 117 return isolate->factory()->NewConsString(first_part, second_part); |
| 138 } | 118 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 194 |
| 215 | 195 |
| 216 template <typename Char> | 196 template <typename Char> |
| 217 MaybeHandle<String> URIEscape::Escape(Isolate* isolate, Handle<String> string) { | 197 MaybeHandle<String> URIEscape::Escape(Isolate* isolate, Handle<String> string) { |
| 218 DCHECK(string->IsFlat()); | 198 DCHECK(string->IsFlat()); |
| 219 int escaped_length = 0; | 199 int escaped_length = 0; |
| 220 int length = string->length(); | 200 int length = string->length(); |
| 221 | 201 |
| 222 { | 202 { |
| 223 DisallowHeapAllocation no_allocation; | 203 DisallowHeapAllocation no_allocation; |
| 224 Vector<const Char> vector = GetCharVector<Char>(string); | 204 Vector<const Char> vector = string->GetCharVector<Char>(); |
| 225 for (int i = 0; i < length; i++) { | 205 for (int i = 0; i < length; i++) { |
| 226 uint16_t c = vector[i]; | 206 uint16_t c = vector[i]; |
| 227 if (c >= 256) { | 207 if (c >= 256) { |
| 228 escaped_length += 6; | 208 escaped_length += 6; |
| 229 } else if (IsNotEscaped(c)) { | 209 } else if (IsNotEscaped(c)) { |
| 230 escaped_length++; | 210 escaped_length++; |
| 231 } else { | 211 } else { |
| 232 escaped_length += 3; | 212 escaped_length += 3; |
| 233 } | 213 } |
| 234 | 214 |
| 235 // We don't allow strings that are longer than a maximal length. | 215 // We don't allow strings that are longer than a maximal length. |
| 236 DCHECK(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow. | 216 DCHECK(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow. |
| 237 if (escaped_length > String::kMaxLength) break; // Provoke exception. | 217 if (escaped_length > String::kMaxLength) break; // Provoke exception. |
| 238 } | 218 } |
| 239 } | 219 } |
| 240 | 220 |
| 241 // No length change implies no change. Return original string if no change. | 221 // No length change implies no change. Return original string if no change. |
| 242 if (escaped_length == length) return string; | 222 if (escaped_length == length) return string; |
| 243 | 223 |
| 244 Handle<SeqOneByteString> dest; | 224 Handle<SeqOneByteString> dest; |
| 245 ASSIGN_RETURN_ON_EXCEPTION( | 225 ASSIGN_RETURN_ON_EXCEPTION( |
| 246 isolate, dest, isolate->factory()->NewRawOneByteString(escaped_length), | 226 isolate, dest, isolate->factory()->NewRawOneByteString(escaped_length), |
| 247 String); | 227 String); |
| 248 int dest_position = 0; | 228 int dest_position = 0; |
| 249 | 229 |
| 250 { | 230 { |
| 251 DisallowHeapAllocation no_allocation; | 231 DisallowHeapAllocation no_allocation; |
| 252 Vector<const Char> vector = GetCharVector<Char>(string); | 232 Vector<const Char> vector = string->GetCharVector<Char>(); |
| 253 for (int i = 0; i < length; i++) { | 233 for (int i = 0; i < length; i++) { |
| 254 uint16_t c = vector[i]; | 234 uint16_t c = vector[i]; |
| 255 if (c >= 256) { | 235 if (c >= 256) { |
| 256 dest->SeqOneByteStringSet(dest_position, '%'); | 236 dest->SeqOneByteStringSet(dest_position, '%'); |
| 257 dest->SeqOneByteStringSet(dest_position + 1, 'u'); | 237 dest->SeqOneByteStringSet(dest_position + 1, 'u'); |
| 258 dest->SeqOneByteStringSet(dest_position + 2, kHexChars[c >> 12]); | 238 dest->SeqOneByteStringSet(dest_position + 2, kHexChars[c >> 12]); |
| 259 dest->SeqOneByteStringSet(dest_position + 3, kHexChars[(c >> 8) & 0xf]); | 239 dest->SeqOneByteStringSet(dest_position + 3, kHexChars[(c >> 8) & 0xf]); |
| 260 dest->SeqOneByteStringSet(dest_position + 4, kHexChars[(c >> 4) & 0xf]); | 240 dest->SeqOneByteStringSet(dest_position + 4, kHexChars[(c >> 4) & 0xf]); |
| 261 dest->SeqOneByteStringSet(dest_position + 5, kHexChars[c & 0xf]); | 241 dest->SeqOneByteStringSet(dest_position + 5, kHexChars[c & 0xf]); |
| 262 dest_position += 6; | 242 dest_position += 6; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 DCHECK(string->IsFlat()); | 279 DCHECK(string->IsFlat()); |
| 300 Handle<String> result; | 280 Handle<String> result; |
| 301 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 281 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 302 isolate, result, string->IsOneByteRepresentationUnderneath() | 282 isolate, result, string->IsOneByteRepresentationUnderneath() |
| 303 ? URIUnescape::Unescape<uint8_t>(isolate, source) | 283 ? URIUnescape::Unescape<uint8_t>(isolate, source) |
| 304 : URIUnescape::Unescape<uc16>(isolate, source)); | 284 : URIUnescape::Unescape<uc16>(isolate, source)); |
| 305 return *result; | 285 return *result; |
| 306 } | 286 } |
| 307 } | 287 } |
| 308 } // namespace v8::internal | 288 } // namespace v8::internal |
| OLD | NEW |