blob: fe659aa1abccda4341b1673fdfe8d84e7106fde2 [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
33function URIAddEncodedOctetToBuffer(octet, result, index) {
34 result[index++] = 37; // Char code of '%'.
35 result[index++] = hexCharCodeArray[octet >> 4];
36 result[index++] = hexCharCodeArray[octet & 0x0F];
37 return index;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000038}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
40
41function URIEncodeOctets(octets, result, index) {
42 index = URIAddEncodedOctetToBuffer(octets[0], result, index);
43 if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index);
44 if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index);
45 if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index);
46 return index;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000047}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048
49
50function URIEncodeSingle(cc, result, index) {
51 var x = (cc >> 12) & 0xF;
52 var y = (cc >> 6) & 63;
53 var z = cc & 63;
54 var octets = new $Array(3);
55 if (cc <= 0x007F) {
56 octets[0] = cc;
57 } else if (cc <= 0x07FF) {
58 octets[0] = y + 192;
59 octets[1] = z + 128;
60 } else {
61 octets[0] = x + 224;
62 octets[1] = y + 128;
63 octets[2] = z + 128;
64 }
65 return URIEncodeOctets(octets, result, index);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000066}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067
68
69function URIEncodePair(cc1 , cc2, result, index) {
70 var u = ((cc1 >> 6) & 0xF) + 1;
71 var w = (cc1 >> 2) & 0xF;
72 var x = cc1 & 3;
73 var y = (cc2 >> 6) & 0xF;
74 var z = cc2 & 63;
75 var octets = new $Array(4);
76 octets[0] = (u >> 2) + 240;
77 octets[1] = (((u & 3) << 4) | w) + 128;
78 octets[2] = ((x << 4) | y) + 128;
79 octets[3] = z + 128;
80 return URIEncodeOctets(octets, result, index);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000081}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082
83
84function URIHexCharsToCharCode(ch1, ch2) {
85 if (HexValueOf(ch1) == -1 || HexValueOf(ch2) == -1) {
86 throw new $URIError("URI malformed");
87 }
88 return HexStrToCharCode(ch1 + ch2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000089}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090
91
92function URIDecodeOctets(octets, result, index) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000093 var value;
94 var o0 = octets[0];
95 if (o0 < 0x80) {
96 value = o0;
97 } else if (o0 < 0xc2) {
98 throw new $URIError("URI malformed");
99 } else {
100 var o1 = octets[1];
101 if (o0 < 0xe0) {
102 var a = o0 & 0x1f;
103 if ((o1 < 0x80) || (o1 > 0xbf))
104 throw new $URIError("URI malformed");
105 var b = o1 & 0x3f;
106 value = (a << 6) + b;
107 if (value < 0x80 || value > 0x7ff)
108 throw new $URIError("URI malformed");
109 } else {
110 var o2 = octets[2];
111 if (o0 < 0xf0) {
112 var a = o0 & 0x0f;
113 if ((o1 < 0x80) || (o1 > 0xbf))
114 throw new $URIError("URI malformed");
115 var b = o1 & 0x3f;
116 if ((o2 < 0x80) || (o2 > 0xbf))
117 throw new $URIError("URI malformed");
118 var c = o2 & 0x3f;
119 value = (a << 12) + (b << 6) + c;
120 if ((value < 0x800) || (value > 0xffff))
121 throw new $URIError("URI malformed");
122 } else {
123 var o3 = octets[3];
124 if (o0 < 0xf8) {
125 var a = (o0 & 0x07);
126 if ((o1 < 0x80) || (o1 > 0xbf))
127 throw new $URIError("URI malformed");
128 var b = (o1 & 0x3f);
129 if ((o2 < 0x80) || (o2 > 0xbf))
130 throw new $URIError("URI malformed");
131 var c = (o2 & 0x3f);
132 if ((o3 < 0x80) || (o3 > 0xbf))
133 throw new $URIError("URI malformed");
134 var d = (o3 & 0x3f);
135 value = (a << 18) + (b << 12) + (c << 6) + d;
136 if ((value < 0x10000) || (value > 0x10ffff))
137 throw new $URIError("URI malformed");
138 } else {
139 throw new $URIError("URI malformed");
140 }
141 }
142 }
143 }
144 if (value < 0x10000) {
145 result[index++] = value;
146 return index;
147 } else {
148 result[index++] = (value >> 10) + 0xd7c0;
149 result[index++] = (value & 0x3ff) + 0xdc00;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 return index;
151 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000152}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153
154
155// ECMA-262, section 15.1.3
156function Encode(uri, unescape) {
157 var uriLength = uri.length;
158 var result = new $Array(uriLength);
159 var index = 0;
160 for (var k = 0; k < uriLength; k++) {
161 var cc1 = uri.charCodeAt(k);
162 if (unescape(cc1)) {
163 result[index++] = cc1;
164 } else {
165 if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed");
166 if (cc1 < 0xD800 || cc1 > 0xDBFF) {
167 index = URIEncodeSingle(cc1, result, index);
168 } else {
169 k++;
170 if (k == uriLength) throw new $URIError("URI malformed");
171 var cc2 = uri.charCodeAt(k);
172 if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed");
173 index = URIEncodePair(cc1, cc2, result, index);
174 }
175 }
176 }
177 return %StringFromCharCodeArray(result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000178}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179
180
181// ECMA-262, section 15.1.3
182function Decode(uri, reserved) {
183 var uriLength = uri.length;
184 var result = new $Array(uriLength);
185 var index = 0;
186 for (var k = 0; k < uriLength; k++) {
187 var ch = uri.charAt(k);
188 if (ch == '%') {
189 if (k + 2 >= uriLength) throw new $URIError("URI malformed");
190 var cc = URIHexCharsToCharCode(uri.charAt(++k), uri.charAt(++k));
191 if (cc >> 7) {
192 var n = 0;
193 while (((cc << ++n) & 0x80) != 0) ;
194 if (n == 1 || n > 4) throw new $URIError("URI malformed");
195 var octets = new $Array(n);
196 octets[0] = cc;
197 if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed");
198 for (var i = 1; i < n; i++) {
199 k++;
200 octets[i] = URIHexCharsToCharCode(uri.charAt(++k), uri.charAt(++k));
201 }
202 index = URIDecodeOctets(octets, result, index);
203 } else {
204 if (reserved(cc)) {
205 result[index++] = 37; // Char code of '%'.
206 result[index++] = uri.charCodeAt(k - 1);
207 result[index++] = uri.charCodeAt(k);
208 } else {
209 result[index++] = cc;
210 }
211 }
212 } else {
213 result[index++] = ch.charCodeAt(0);
214 }
215 }
216 result.length = index;
217 return %StringFromCharCodeArray(result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000218}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
220
221// ECMA-262 - 15.1.3.1.
222function URIDecode(uri) {
223 function reservedPredicate(cc) {
224 // #$
225 if (35 <= cc && cc <= 36) return true;
226 // &
227 if (cc == 38) return true;
228 // +,
229 if (43 <= cc && cc <= 44) return true;
230 // /
231 if (cc == 47) return true;
232 // :;
233 if (58 <= cc && cc <= 59) return true;
234 // =
235 if (cc == 61) return true;
236 // ?@
237 if (63 <= cc && cc <= 64) return true;
238
239 return false;
240 };
241 var string = ToString(uri);
242 return Decode(string, reservedPredicate);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000243}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244
245
246// ECMA-262 - 15.1.3.2.
247function URIDecodeComponent(component) {
248 function reservedPredicate(cc) { return false; };
249 var string = ToString(component);
250 return Decode(string, reservedPredicate);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000251}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252
253
254// Does the char code correspond to an alpha-numeric char.
255function isAlphaNumeric(cc) {
256 // a - z
257 if (97 <= cc && cc <= 122) return true;
258 // A - Z
259 if (65 <= cc && cc <= 90) return true;
260 // 0 - 9
261 if (48 <= cc && cc <= 57) return true;
262
263 return false;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000264}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265
266
267// ECMA-262 - 15.1.3.3.
268function URIEncode(uri) {
269 function unescapePredicate(cc) {
270 if (isAlphaNumeric(cc)) return true;
271 // !
272 if (cc == 33) return true;
273 // #$
274 if (35 <= cc && cc <= 36) return true;
275 // &'()*+,-./
276 if (38 <= cc && cc <= 47) return true;
277 // :;
278 if (58 <= cc && cc <= 59) return true;
279 // =
280 if (cc == 61) return true;
281 // ?@
282 if (63 <= cc && cc <= 64) return true;
283 // _
284 if (cc == 95) return true;
285 // ~
286 if (cc == 126) return true;
287
288 return false;
289 };
290
291 var string = ToString(uri);
292 return Encode(string, unescapePredicate);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000293}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294
295
296// ECMA-262 - 15.1.3.4
297function URIEncodeComponent(component) {
298 function unescapePredicate(cc) {
299 if (isAlphaNumeric(cc)) return true;
300 // !
301 if (cc == 33) return true;
302 // '()*
303 if (39 <= cc && cc <= 42) return true;
304 // -.
305 if (45 <= cc && cc <= 46) return true;
306 // _
307 if (cc == 95) return true;
308 // ~
309 if (cc == 126) return true;
310
311 return false;
312 };
313
314 var string = ToString(component);
315 return Encode(string, unescapePredicate);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000316}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317
318
319const hexCharArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
320 "A", "B", "C", "D", "E", "F"];
321
322const hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
323 65, 66, 67, 68, 69, 70];
324
325
326function HexValueOf(c) {
327 var code = c.charCodeAt(0);
328
329 // 0-9
330 if (code >= 48 && code <= 57) return code - 48;
331 // A-F
332 if (code >= 65 && code <= 70) return code - 55;
333 // a-f
334 if (code >= 97 && code <= 102) return code - 87;
335
336 return -1;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000337}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338
339
340// Convert a character code to 4-digit hex string representation
341// 64 -> 0040, 62234 -> F31A.
342function CharCodeToHex4Str(cc) {
343 var r = "";
344 for (var i = 0; i < 4; ++i) {
345 var c = hexCharArray[cc & 0x0F];
346 r = c + r;
347 cc = cc >>> 4;
348 }
349 return r;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000350}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351
352
353// Converts hex string to char code. Not efficient.
354function HexStrToCharCode(s) {
355 var m = 0;
356 var r = 0;
357 for (var i = s.length - 1; i >= 0; --i) {
358 r = r + (HexValueOf(s.charAt(i)) << m);
359 m = m + 4;
360 }
361 return r;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000362}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363
364
365// 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();
409