OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 // This file contains support for URI manipulations written in | 5 // This file contains support for URI manipulations written in |
6 // JavaScript. | 6 // JavaScript. |
7 | 7 |
8 (function() { | 8 (function() { |
9 | 9 |
10 "use strict"; | 10 "use strict"; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 octets[0] = (u >> 2) + 240; | 90 octets[0] = (u >> 2) + 240; |
91 octets[1] = (((u & 3) << 4) | w) + 128; | 91 octets[1] = (((u & 3) << 4) | w) + 128; |
92 octets[2] = ((x << 4) | y) + 128; | 92 octets[2] = ((x << 4) | y) + 128; |
93 octets[3] = z + 128; | 93 octets[3] = z + 128; |
94 return URIEncodeOctets(octets, result, index); | 94 return URIEncodeOctets(octets, result, index); |
95 } | 95 } |
96 | 96 |
97 function URIHexCharsToCharCode(highChar, lowChar) { | 97 function URIHexCharsToCharCode(highChar, lowChar) { |
98 var highCode = HexValueOf(highChar); | 98 var highCode = HexValueOf(highChar); |
99 var lowCode = HexValueOf(lowChar); | 99 var lowCode = HexValueOf(lowChar); |
100 if (highCode == -1 || lowCode == -1) { | 100 if (highCode == -1 || lowCode == -1) throw MakeURIError(); |
101 throw new $URIError("URI malformed"); | |
102 } | |
103 return (highCode << 4) | lowCode; | 101 return (highCode << 4) | lowCode; |
104 } | 102 } |
105 | 103 |
106 // Callers must ensure that |result| is a sufficiently long sequential | 104 // Callers must ensure that |result| is a sufficiently long sequential |
107 // two-byte string! | 105 // two-byte string! |
108 function URIDecodeOctets(octets, result, index) { | 106 function URIDecodeOctets(octets, result, index) { |
109 var value; | 107 var value; |
110 var o0 = octets[0]; | 108 var o0 = octets[0]; |
111 if (o0 < 0x80) { | 109 if (o0 < 0x80) { |
112 value = o0; | 110 value = o0; |
113 } else if (o0 < 0xc2) { | 111 } else if (o0 < 0xc2) { |
114 throw new $URIError("URI malformed"); | 112 throw MakeURIError(); |
115 } else { | 113 } else { |
116 var o1 = octets[1]; | 114 var o1 = octets[1]; |
117 if (o0 < 0xe0) { | 115 if (o0 < 0xe0) { |
118 var a = o0 & 0x1f; | 116 var a = o0 & 0x1f; |
119 if ((o1 < 0x80) || (o1 > 0xbf)) { | 117 if ((o1 < 0x80) || (o1 > 0xbf)) throw MakeURIError(); |
120 throw new $URIError("URI malformed"); | |
121 } | |
122 var b = o1 & 0x3f; | 118 var b = o1 & 0x3f; |
123 value = (a << 6) + b; | 119 value = (a << 6) + b; |
124 if (value < 0x80 || value > 0x7ff) { | 120 if (value < 0x80 || value > 0x7ff) throw MakeURIError(); |
125 throw new $URIError("URI malformed"); | |
126 } | |
127 } else { | 121 } else { |
128 var o2 = octets[2]; | 122 var o2 = octets[2]; |
129 if (o0 < 0xf0) { | 123 if (o0 < 0xf0) { |
130 var a = o0 & 0x0f; | 124 var a = o0 & 0x0f; |
131 if ((o1 < 0x80) || (o1 > 0xbf)) { | 125 if ((o1 < 0x80) || (o1 > 0xbf)) throw MakeURIError(); |
132 throw new $URIError("URI malformed"); | |
133 } | |
134 var b = o1 & 0x3f; | 126 var b = o1 & 0x3f; |
135 if ((o2 < 0x80) || (o2 > 0xbf)) { | 127 if ((o2 < 0x80) || (o2 > 0xbf)) throw MakeURIError(); |
136 throw new $URIError("URI malformed"); | |
137 } | |
138 var c = o2 & 0x3f; | 128 var c = o2 & 0x3f; |
139 value = (a << 12) + (b << 6) + c; | 129 value = (a << 12) + (b << 6) + c; |
140 if ((value < 0x800) || (value > 0xffff)) { | 130 if ((value < 0x800) || (value > 0xffff)) throw MakeURIError(); |
141 throw new $URIError("URI malformed"); | |
142 } | |
143 } else { | 131 } else { |
144 var o3 = octets[3]; | 132 var o3 = octets[3]; |
145 if (o0 < 0xf8) { | 133 if (o0 < 0xf8) { |
146 var a = (o0 & 0x07); | 134 var a = (o0 & 0x07); |
147 if ((o1 < 0x80) || (o1 > 0xbf)) { | 135 if ((o1 < 0x80) || (o1 > 0xbf)) throw MakeURIError(); |
148 throw new $URIError("URI malformed"); | |
149 } | |
150 var b = (o1 & 0x3f); | 136 var b = (o1 & 0x3f); |
151 if ((o2 < 0x80) || (o2 > 0xbf)) { | 137 if ((o2 < 0x80) || (o2 > 0xbf)) { |
152 throw new $URIError("URI malformed"); | 138 throw MakeURIError(); |
153 } | 139 } |
154 var c = (o2 & 0x3f); | 140 var c = (o2 & 0x3f); |
155 if ((o3 < 0x80) || (o3 > 0xbf)) { | 141 if ((o3 < 0x80) || (o3 > 0xbf)) throw MakeURIError(); |
156 throw new $URIError("URI malformed"); | |
157 } | |
158 var d = (o3 & 0x3f); | 142 var d = (o3 & 0x3f); |
159 value = (a << 18) + (b << 12) + (c << 6) + d; | 143 value = (a << 18) + (b << 12) + (c << 6) + d; |
160 if ((value < 0x10000) || (value > 0x10ffff)) { | 144 if ((value < 0x10000) || (value > 0x10ffff)) throw MakeURIError(); |
161 throw new $URIError("URI malformed"); | |
162 } | |
163 } else { | 145 } else { |
164 throw new $URIError("URI malformed"); | 146 throw MakeURIError(); |
165 } | 147 } |
166 } | 148 } |
167 } | 149 } |
168 } | 150 } |
169 if (0xD800 <= value && value <= 0xDFFF) { | 151 if (0xD800 <= value && value <= 0xDFFF) throw MakeURIError(); |
170 throw new $URIError("URI malformed"); | |
171 } | |
172 if (value < 0x10000) { | 152 if (value < 0x10000) { |
173 %_TwoByteSeqStringSetChar(index++, value, result); | 153 %_TwoByteSeqStringSetChar(index++, value, result); |
174 } else { | 154 } else { |
175 %_TwoByteSeqStringSetChar(index++, (value >> 10) + 0xd7c0, result); | 155 %_TwoByteSeqStringSetChar(index++, (value >> 10) + 0xd7c0, result); |
176 %_TwoByteSeqStringSetChar(index++, (value & 0x3ff) + 0xdc00, result); | 156 %_TwoByteSeqStringSetChar(index++, (value & 0x3ff) + 0xdc00, result); |
177 } | 157 } |
178 return index; | 158 return index; |
179 } | 159 } |
180 | 160 |
181 // ECMA-262, section 15.1.3 | 161 // ECMA-262, section 15.1.3 |
182 function Encode(uri, unescape) { | 162 function Encode(uri, unescape) { |
183 var uriLength = uri.length; | 163 var uriLength = uri.length; |
184 var array = new InternalArray(uriLength); | 164 var array = new InternalArray(uriLength); |
185 var index = 0; | 165 var index = 0; |
186 for (var k = 0; k < uriLength; k++) { | 166 for (var k = 0; k < uriLength; k++) { |
187 var cc1 = uri.charCodeAt(k); | 167 var cc1 = uri.charCodeAt(k); |
188 if (unescape(cc1)) { | 168 if (unescape(cc1)) { |
189 array[index++] = cc1; | 169 array[index++] = cc1; |
190 } else { | 170 } else { |
191 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed"); | 171 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw MakeURIError(); |
192 if (cc1 < 0xD800 || cc1 > 0xDBFF) { | 172 if (cc1 < 0xD800 || cc1 > 0xDBFF) { |
193 index = URIEncodeSingle(cc1, array, index); | 173 index = URIEncodeSingle(cc1, array, index); |
194 } else { | 174 } else { |
195 k++; | 175 k++; |
196 if (k == uriLength) throw new $URIError("URI malformed"); | 176 if (k == uriLength) throw MakeURIError(); |
197 var cc2 = uri.charCodeAt(k); | 177 var cc2 = uri.charCodeAt(k); |
198 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed"); | 178 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw MakeURIError(); |
199 index = URIEncodePair(cc1, cc2, array, index); | 179 index = URIEncodePair(cc1, cc2, array, index); |
200 } | 180 } |
201 } | 181 } |
202 } | 182 } |
203 | 183 |
204 var result = %NewString(array.length, NEW_ONE_BYTE_STRING); | 184 var result = %NewString(array.length, NEW_ONE_BYTE_STRING); |
205 for (var i = 0; i < array.length; i++) { | 185 for (var i = 0; i < array.length; i++) { |
206 %_OneByteSeqStringSetChar(i, array[i], result); | 186 %_OneByteSeqStringSetChar(i, array[i], result); |
207 } | 187 } |
208 return result; | 188 return result; |
209 } | 189 } |
210 | 190 |
211 // ECMA-262, section 15.1.3 | 191 // ECMA-262, section 15.1.3 |
212 function Decode(uri, reserved) { | 192 function Decode(uri, reserved) { |
213 var uriLength = uri.length; | 193 var uriLength = uri.length; |
214 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); | 194 var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); |
215 var index = 0; | 195 var index = 0; |
216 var k = 0; | 196 var k = 0; |
217 | 197 |
218 // Optimistically assume one-byte string. | 198 // Optimistically assume one-byte string. |
219 for ( ; k < uriLength; k++) { | 199 for ( ; k < uriLength; k++) { |
220 var code = uri.charCodeAt(k); | 200 var code = uri.charCodeAt(k); |
221 if (code == 37) { // '%' | 201 if (code == 37) { // '%' |
222 if (k + 2 >= uriLength) throw new $URIError("URI malformed"); | 202 if (k + 2 >= uriLength) throw MakeURIError(); |
223 var cc = URIHexCharsToCharCode(uri.charCodeAt(k+1), uri.charCodeAt(k+2)); | 203 var cc = URIHexCharsToCharCode(uri.charCodeAt(k+1), uri.charCodeAt(k+2)); |
224 if (cc >> 7) break; // Assumption wrong, two-byte string. | 204 if (cc >> 7) break; // Assumption wrong, two-byte string. |
225 if (reserved(cc)) { | 205 if (reserved(cc)) { |
226 %_OneByteSeqStringSetChar(index++, 37, one_byte); // '%'. | 206 %_OneByteSeqStringSetChar(index++, 37, one_byte); // '%'. |
227 %_OneByteSeqStringSetChar(index++, uri.charCodeAt(k+1), one_byte); | 207 %_OneByteSeqStringSetChar(index++, uri.charCodeAt(k+1), one_byte); |
228 %_OneByteSeqStringSetChar(index++, uri.charCodeAt(k+2), one_byte); | 208 %_OneByteSeqStringSetChar(index++, uri.charCodeAt(k+2), one_byte); |
229 } else { | 209 } else { |
230 %_OneByteSeqStringSetChar(index++, cc, one_byte); | 210 %_OneByteSeqStringSetChar(index++, cc, one_byte); |
231 } | 211 } |
232 k += 2; | 212 k += 2; |
233 } else { | 213 } else { |
234 if (code > 0x7f) break; // Assumption wrong, two-byte string. | 214 if (code > 0x7f) break; // Assumption wrong, two-byte string. |
235 %_OneByteSeqStringSetChar(index++, code, one_byte); | 215 %_OneByteSeqStringSetChar(index++, code, one_byte); |
236 } | 216 } |
237 } | 217 } |
238 | 218 |
239 one_byte = %TruncateString(one_byte, index); | 219 one_byte = %TruncateString(one_byte, index); |
240 if (k == uriLength) return one_byte; | 220 if (k == uriLength) return one_byte; |
241 | 221 |
242 // Write into two byte string. | 222 // Write into two byte string. |
243 var two_byte = %NewString(uriLength - k, NEW_TWO_BYTE_STRING); | 223 var two_byte = %NewString(uriLength - k, NEW_TWO_BYTE_STRING); |
244 index = 0; | 224 index = 0; |
245 | 225 |
246 for ( ; k < uriLength; k++) { | 226 for ( ; k < uriLength; k++) { |
247 var code = uri.charCodeAt(k); | 227 var code = uri.charCodeAt(k); |
248 if (code == 37) { // '%' | 228 if (code == 37) { // '%' |
249 if (k + 2 >= uriLength) throw new $URIError("URI malformed"); | 229 if (k + 2 >= uriLength) throw MakeURIError(); |
250 var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k)); | 230 var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k)); |
251 if (cc >> 7) { | 231 if (cc >> 7) { |
252 var n = 0; | 232 var n = 0; |
253 while (((cc << ++n) & 0x80) != 0) { } | 233 while (((cc << ++n) & 0x80) != 0) { } |
254 if (n == 1 || n > 4) throw new $URIError("URI malformed"); | 234 if (n == 1 || n > 4) throw MakeURIError(); |
255 var octets = new GlobalArray(n); | 235 var octets = new GlobalArray(n); |
256 octets[0] = cc; | 236 octets[0] = cc; |
257 if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed"); | 237 if (k + 3 * (n - 1) >= uriLength) throw MakeURIError(); |
258 for (var i = 1; i < n; i++) { | 238 for (var i = 1; i < n; i++) { |
259 if (uri.charAt(++k) != '%') throw new $URIError("URI malformed"); | 239 if (uri.charAt(++k) != '%') throw MakeURIError(); |
260 octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k), | 240 octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k), |
261 uri.charCodeAt(++k)); | 241 uri.charCodeAt(++k)); |
262 } | 242 } |
263 index = URIDecodeOctets(octets, two_byte, index); | 243 index = URIDecodeOctets(octets, two_byte, index); |
264 } else if (reserved(cc)) { | 244 } else if (reserved(cc)) { |
265 %_TwoByteSeqStringSetChar(index++, 37, two_byte); // '%'. | 245 %_TwoByteSeqStringSetChar(index++, 37, two_byte); // '%'. |
266 %_TwoByteSeqStringSetChar(index++, uri.charCodeAt(k - 1), two_byte); | 246 %_TwoByteSeqStringSetChar(index++, uri.charCodeAt(k - 1), two_byte); |
267 %_TwoByteSeqStringSetChar(index++, uri.charCodeAt(k), two_byte); | 247 %_TwoByteSeqStringSetChar(index++, uri.charCodeAt(k), two_byte); |
268 } else { | 248 } else { |
269 %_TwoByteSeqStringSetChar(index++, cc, two_byte); | 249 %_TwoByteSeqStringSetChar(index++, cc, two_byte); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 InstallFunctions(global, DONT_ENUM, [ | 359 InstallFunctions(global, DONT_ENUM, [ |
380 "escape", URIEscapeJS, | 360 "escape", URIEscapeJS, |
381 "unescape", URIUnescapeJS, | 361 "unescape", URIUnescapeJS, |
382 "decodeURI", URIDecode, | 362 "decodeURI", URIDecode, |
383 "decodeURIComponent", URIDecodeComponent, | 363 "decodeURIComponent", URIDecodeComponent, |
384 "encodeURI", URIEncode, | 364 "encodeURI", URIEncode, |
385 "encodeURIComponent", URIEncodeComponent | 365 "encodeURIComponent", URIEncodeComponent |
386 ]); | 366 ]); |
387 | 367 |
388 })(); | 368 })(); |
OLD | NEW |