blob: 72ca6f1565daa2d1b755e73622486afa4e1ddfc5 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// This file contains support for URI manipulations written in
29// JavaScript.
30
31// Expect $String = global.String;
32
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000033// Lazily initialized.
34var hexCharArray = 0;
35var hexCharCodeArray = 0;
36
37
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038function URIAddEncodedOctetToBuffer(octet, result, index) {
39 result[index++] = 37; // Char code of '%'.
40 result[index++] = hexCharCodeArray[octet >> 4];
41 result[index++] = hexCharCodeArray[octet & 0x0F];
42 return index;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000043}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
45
46function URIEncodeOctets(octets, result, index) {
ager@chromium.orgadd848f2009-08-13 12:44:13 +000047 if (hexCharCodeArray === 0) {
48 hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
49 65, 66, 67, 68, 69, 70];
50 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051 index = URIAddEncodedOctetToBuffer(octets[0], result, index);
52 if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index);
53 if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index);
54 if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index);
55 return index;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000056}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057
58
59function URIEncodeSingle(cc, result, index) {
60 var x = (cc >> 12) & 0xF;
61 var y = (cc >> 6) & 63;
62 var z = cc & 63;
63 var octets = new $Array(3);
64 if (cc <= 0x007F) {
65 octets[0] = cc;
66 } else if (cc <= 0x07FF) {
67 octets[0] = y + 192;
68 octets[1] = z + 128;
69 } else {
70 octets[0] = x + 224;
71 octets[1] = y + 128;
72 octets[2] = z + 128;
73 }
74 return URIEncodeOctets(octets, result, index);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000075}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076
77
78function URIEncodePair(cc1 , cc2, result, index) {
79 var u = ((cc1 >> 6) & 0xF) + 1;
80 var w = (cc1 >> 2) & 0xF;
81 var x = cc1 & 3;
82 var y = (cc2 >> 6) & 0xF;
83 var z = cc2 & 63;
84 var octets = new $Array(4);
85 octets[0] = (u >> 2) + 240;
86 octets[1] = (((u & 3) << 4) | w) + 128;
87 octets[2] = ((x << 4) | y) + 128;
88 octets[3] = z + 128;
89 return URIEncodeOctets(octets, result, index);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000090}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091
92
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +000093function URIHexCharsToCharCode(highChar, lowChar) {
94 var highCode = HexValueOf(highChar);
95 var lowCode = HexValueOf(lowChar);
96 if (highCode == -1 || lowCode == -1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 throw new $URIError("URI malformed");
98 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +000099 return (highCode << 4) | lowCode;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000100}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101
102
103function URIDecodeOctets(octets, result, index) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000104 var value;
105 var o0 = octets[0];
106 if (o0 < 0x80) {
107 value = o0;
108 } else if (o0 < 0xc2) {
109 throw new $URIError("URI malformed");
110 } else {
111 var o1 = octets[1];
112 if (o0 < 0xe0) {
113 var a = o0 & 0x1f;
114 if ((o1 < 0x80) || (o1 > 0xbf))
115 throw new $URIError("URI malformed");
116 var b = o1 & 0x3f;
117 value = (a << 6) + b;
118 if (value < 0x80 || value > 0x7ff)
119 throw new $URIError("URI malformed");
120 } else {
121 var o2 = octets[2];
122 if (o0 < 0xf0) {
123 var a = o0 & 0x0f;
124 if ((o1 < 0x80) || (o1 > 0xbf))
125 throw new $URIError("URI malformed");
126 var b = o1 & 0x3f;
127 if ((o2 < 0x80) || (o2 > 0xbf))
128 throw new $URIError("URI malformed");
129 var c = o2 & 0x3f;
130 value = (a << 12) + (b << 6) + c;
131 if ((value < 0x800) || (value > 0xffff))
132 throw new $URIError("URI malformed");
133 } else {
134 var o3 = octets[3];
135 if (o0 < 0xf8) {
136 var a = (o0 & 0x07);
137 if ((o1 < 0x80) || (o1 > 0xbf))
138 throw new $URIError("URI malformed");
139 var b = (o1 & 0x3f);
140 if ((o2 < 0x80) || (o2 > 0xbf))
141 throw new $URIError("URI malformed");
142 var c = (o2 & 0x3f);
143 if ((o3 < 0x80) || (o3 > 0xbf))
144 throw new $URIError("URI malformed");
145 var d = (o3 & 0x3f);
146 value = (a << 18) + (b << 12) + (c << 6) + d;
147 if ((value < 0x10000) || (value > 0x10ffff))
148 throw new $URIError("URI malformed");
149 } else {
150 throw new $URIError("URI malformed");
151 }
152 }
153 }
154 }
155 if (value < 0x10000) {
156 result[index++] = value;
157 return index;
158 } else {
159 result[index++] = (value >> 10) + 0xd7c0;
160 result[index++] = (value & 0x3ff) + 0xdc00;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 return index;
162 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000163}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164
165
166// ECMA-262, section 15.1.3
167function Encode(uri, unescape) {
168 var uriLength = uri.length;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000169 // We are going to pass result to %StringFromCharCodeArray
170 // which does not expect any getters/setters installed
171 // on the incoming array.
172 var result = new InternalArray(uriLength);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 var index = 0;
174 for (var k = 0; k < uriLength; k++) {
175 var cc1 = uri.charCodeAt(k);
176 if (unescape(cc1)) {
177 result[index++] = cc1;
178 } else {
179 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed");
180 if (cc1 < 0xD800 || cc1 > 0xDBFF) {
181 index = URIEncodeSingle(cc1, result, index);
182 } else {
183 k++;
184 if (k == uriLength) throw new $URIError("URI malformed");
185 var cc2 = uri.charCodeAt(k);
186 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed");
187 index = URIEncodePair(cc1, cc2, result, index);
188 }
189 }
190 }
191 return %StringFromCharCodeArray(result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000192}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193
194
195// ECMA-262, section 15.1.3
196function Decode(uri, reserved) {
197 var uriLength = uri.length;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000198 // We are going to pass result to %StringFromCharCodeArray
199 // which does not expect any getters/setters installed
200 // on the incoming array.
201 var result = new InternalArray(uriLength);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202 var index = 0;
203 for (var k = 0; k < uriLength; k++) {
204 var ch = uri.charAt(k);
205 if (ch == '%') {
206 if (k + 2 >= uriLength) throw new $URIError("URI malformed");
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000207 var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 if (cc >> 7) {
209 var n = 0;
210 while (((cc << ++n) & 0x80) != 0) ;
211 if (n == 1 || n > 4) throw new $URIError("URI malformed");
212 var octets = new $Array(n);
213 octets[0] = cc;
214 if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed");
215 for (var i = 1; i < n; i++) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000216 if (uri.charAt(++k) != '%') throw new $URIError("URI malformed");
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000217 octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218 }
219 index = URIDecodeOctets(octets, result, index);
220 } else {
221 if (reserved(cc)) {
222 result[index++] = 37; // Char code of '%'.
223 result[index++] = uri.charCodeAt(k - 1);
224 result[index++] = uri.charCodeAt(k);
225 } else {
226 result[index++] = cc;
227 }
228 }
229 } else {
230 result[index++] = ch.charCodeAt(0);
231 }
232 }
233 result.length = index;
234 return %StringFromCharCodeArray(result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000235}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236
237
238// ECMA-262 - 15.1.3.1.
239function URIDecode(uri) {
240 function reservedPredicate(cc) {
241 // #$
242 if (35 <= cc && cc <= 36) return true;
243 // &
244 if (cc == 38) return true;
245 // +,
246 if (43 <= cc && cc <= 44) return true;
247 // /
248 if (cc == 47) return true;
249 // :;
250 if (58 <= cc && cc <= 59) return true;
251 // =
252 if (cc == 61) return true;
253 // ?@
254 if (63 <= cc && cc <= 64) return true;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 return false;
257 };
258 var string = ToString(uri);
259 return Decode(string, reservedPredicate);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000260}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261
262
263// ECMA-262 - 15.1.3.2.
264function URIDecodeComponent(component) {
265 function reservedPredicate(cc) { return false; };
266 var string = ToString(component);
267 return Decode(string, reservedPredicate);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000268}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269
270
271// Does the char code correspond to an alpha-numeric char.
272function isAlphaNumeric(cc) {
273 // a - z
274 if (97 <= cc && cc <= 122) return true;
275 // A - Z
276 if (65 <= cc && cc <= 90) return true;
277 // 0 - 9
278 if (48 <= cc && cc <= 57) return true;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000279
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 return false;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000281}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282
283
284// ECMA-262 - 15.1.3.3.
285function URIEncode(uri) {
286 function unescapePredicate(cc) {
287 if (isAlphaNumeric(cc)) return true;
288 // !
289 if (cc == 33) return true;
290 // #$
291 if (35 <= cc && cc <= 36) return true;
292 // &'()*+,-./
293 if (38 <= cc && cc <= 47) return true;
294 // :;
295 if (58 <= cc && cc <= 59) return true;
296 // =
297 if (cc == 61) return true;
298 // ?@
299 if (63 <= cc && cc <= 64) return true;
300 // _
301 if (cc == 95) return true;
302 // ~
303 if (cc == 126) return true;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000304
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 return false;
306 };
307
308 var string = ToString(uri);
309 return Encode(string, unescapePredicate);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000310}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311
312
313// ECMA-262 - 15.1.3.4
314function URIEncodeComponent(component) {
315 function unescapePredicate(cc) {
316 if (isAlphaNumeric(cc)) return true;
317 // !
318 if (cc == 33) return true;
319 // '()*
320 if (39 <= cc && cc <= 42) return true;
321 // -.
322 if (45 <= cc && cc <= 46) return true;
323 // _
324 if (cc == 95) return true;
325 // ~
326 if (cc == 126) return true;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 return false;
329 };
330
331 var string = ToString(component);
332 return Encode(string, unescapePredicate);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000333}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334
335
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000336function HexValueOf(code) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 // 0-9
338 if (code >= 48 && code <= 57) return code - 48;
339 // A-F
340 if (code >= 65 && code <= 70) return code - 55;
341 // a-f
342 if (code >= 97 && code <= 102) return code - 87;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000343
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 return -1;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000345}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346
347
348// Convert a character code to 4-digit hex string representation
349// 64 -> 0040, 62234 -> F31A.
350function CharCodeToHex4Str(cc) {
351 var r = "";
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000352 if (hexCharArray === 0) {
353 hexCharArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
354 "A", "B", "C", "D", "E", "F"];
355 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356 for (var i = 0; i < 4; ++i) {
357 var c = hexCharArray[cc & 0x0F];
358 r = c + r;
359 cc = cc >>> 4;
360 }
361 return r;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000362}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363
364
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365// Returns true if all digits in string s are valid hex numbers
366function IsValidHex(s) {
367 for (var i = 0; i < s.length; ++i) {
368 var cc = s.charCodeAt(i);
369 if ((48 <= cc && cc <= 57) || (65 <= cc && cc <= 70) || (97 <= cc && cc <= 102)) {
370 // '0'..'9', 'A'..'F' and 'a' .. 'f'.
371 } else {
372 return false;
373 }
374 }
375 return true;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000376}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377
378
379// ECMA-262 - B.2.1.
380function URIEscape(str) {
381 var s = ToString(str);
382 return %URIEscape(s);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000383}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385
386// ECMA-262 - B.2.2.
387function URIUnescape(str) {
388 var s = ToString(str);
389 return %URIUnescape(s);
390}
391
392
393// -------------------------------------------------------------------
394
395function SetupURI() {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000396 // Setup non-enumerable URI functions on the global object and set
397 // their names.
398 InstallFunctions(global, DONT_ENUM, $Array(
399 "escape", URIEscape,
400 "unescape", URIUnescape,
401 "decodeURI", URIDecode,
402 "decodeURIComponent", URIDecodeComponent,
403 "encodeURI", URIEncode,
404 "encodeURIComponent", URIEncodeComponent
405 ));
406}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407
408SetupURI();