• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// 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
33// Lazily initialized.
34var hexCharArray = 0;
35var hexCharCodeArray = 0;
36
37
38function 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;
43}
44
45
46function URIEncodeOctets(octets, result, index) {
47  if (hexCharCodeArray === 0) {
48    hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
49                        65, 66, 67, 68, 69, 70];
50  }
51  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;
56}
57
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);
75}
76
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);
90}
91
92
93function URIHexCharsToCharCode(highChar, lowChar) {
94  var highCode = HexValueOf(highChar);
95  var lowCode = HexValueOf(lowChar);
96  if (highCode == -1 || lowCode == -1) {
97    throw new $URIError("URI malformed");
98  }
99  return (highCode << 4) | lowCode;
100}
101
102
103function URIDecodeOctets(octets, result, index) {
104  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;
161    return index;
162  }
163}
164
165
166// ECMA-262, section 15.1.3
167function Encode(uri, unescape) {
168  var uriLength = uri.length;
169  var result = new $Array(uriLength);
170  var index = 0;
171  for (var k = 0; k < uriLength; k++) {
172    var cc1 = uri.charCodeAt(k);
173    if (unescape(cc1)) {
174      result[index++] = cc1;
175    } else {
176      if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed");
177      if (cc1 < 0xD800 || cc1 > 0xDBFF) {
178        index = URIEncodeSingle(cc1, result, index);
179      } else {
180        k++;
181        if (k == uriLength) throw new $URIError("URI malformed");
182        var cc2 = uri.charCodeAt(k);
183        if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed");
184        index = URIEncodePair(cc1, cc2, result, index);
185      }
186    }
187  }
188  return %StringFromCharCodeArray(result);
189}
190
191
192// ECMA-262, section 15.1.3
193function Decode(uri, reserved) {
194  var uriLength = uri.length;
195  var result = new $Array(uriLength);
196  var index = 0;
197  for (var k = 0; k < uriLength; k++) {
198    var ch = uri.charAt(k);
199    if (ch == '%') {
200      if (k + 2 >= uriLength) throw new $URIError("URI malformed");
201      var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k));
202      if (cc >> 7) {
203        var n = 0;
204        while (((cc << ++n) & 0x80) != 0) ;
205        if (n == 1 || n > 4) throw new $URIError("URI malformed");
206        var octets = new $Array(n);
207        octets[0] = cc;
208        if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed");
209        for (var i = 1; i < n; i++) {
210          if (uri.charAt(++k) != '%') throw new $URIError("URI malformed");
211          octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k));
212        }
213        index = URIDecodeOctets(octets, result, index);
214      } else {
215        if (reserved(cc)) {
216          result[index++] = 37; // Char code of '%'.
217          result[index++] = uri.charCodeAt(k - 1);
218          result[index++] = uri.charCodeAt(k);
219        } else {
220          result[index++] = cc;
221        }
222      }
223    } else {
224      result[index++] = ch.charCodeAt(0);
225    }
226  }
227  result.length = index;
228  return %StringFromCharCodeArray(result);
229}
230
231
232// ECMA-262 - 15.1.3.1.
233function URIDecode(uri) {
234  function reservedPredicate(cc) {
235    // #$
236    if (35 <= cc && cc <= 36) return true;
237    // &
238    if (cc == 38) return true;
239    // +,
240    if (43 <= cc && cc <= 44) return true;
241    // /
242    if (cc == 47) return true;
243    // :;
244    if (58 <= cc && cc <= 59) return true;
245    // =
246    if (cc == 61) return true;
247    // ?@
248    if (63 <= cc && cc <= 64) return true;
249
250    return false;
251  };
252  var string = ToString(uri);
253  return Decode(string, reservedPredicate);
254}
255
256
257// ECMA-262 - 15.1.3.2.
258function URIDecodeComponent(component) {
259  function reservedPredicate(cc) { return false; };
260  var string = ToString(component);
261  return Decode(string, reservedPredicate);
262}
263
264
265// Does the char code correspond to an alpha-numeric char.
266function isAlphaNumeric(cc) {
267  // a - z
268  if (97 <= cc && cc <= 122) return true;
269  // A - Z
270  if (65 <= cc && cc <= 90) return true;
271  // 0 - 9
272  if (48 <= cc && cc <= 57) return true;
273
274  return false;
275}
276
277
278// ECMA-262 - 15.1.3.3.
279function URIEncode(uri) {
280  function unescapePredicate(cc) {
281    if (isAlphaNumeric(cc)) return true;
282    // !
283    if (cc == 33) return true;
284    // #$
285    if (35 <= cc && cc <= 36) return true;
286    // &'()*+,-./
287    if (38 <= cc && cc <= 47) return true;
288    // :;
289    if (58 <= cc && cc <= 59) return true;
290    // =
291    if (cc == 61) return true;
292    // ?@
293    if (63 <= cc && cc <= 64) return true;
294    // _
295    if (cc == 95) return true;
296    // ~
297    if (cc == 126) return true;
298
299    return false;
300  };
301
302  var string = ToString(uri);
303  return Encode(string, unescapePredicate);
304}
305
306
307// ECMA-262 - 15.1.3.4
308function URIEncodeComponent(component) {
309  function unescapePredicate(cc) {
310    if (isAlphaNumeric(cc)) return true;
311    // !
312    if (cc == 33) return true;
313    // '()*
314    if (39 <= cc && cc <= 42) return true;
315    // -.
316    if (45 <= cc && cc <= 46) return true;
317    // _
318    if (cc == 95) return true;
319    // ~
320    if (cc == 126) return true;
321
322    return false;
323  };
324
325  var string = ToString(component);
326  return Encode(string, unescapePredicate);
327}
328
329
330function HexValueOf(code) {
331  // 0-9
332  if (code >= 48 && code <= 57) return code - 48;
333  // A-F
334  if (code >= 65 && code <= 70) return code - 55;
335  // a-f
336  if (code >= 97 && code <= 102) return code - 87;
337
338  return -1;
339}
340
341
342// Convert a character code to 4-digit hex string representation
343// 64 -> 0040, 62234 -> F31A.
344function CharCodeToHex4Str(cc) {
345  var r = "";
346  if (hexCharArray === 0) {
347    hexCharArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
348                    "A", "B", "C", "D", "E", "F"];
349  }
350  for (var i = 0; i < 4; ++i) {
351    var c = hexCharArray[cc & 0x0F];
352    r = c + r;
353    cc = cc >>> 4;
354  }
355  return r;
356}
357
358
359// Returns true if all digits in string s are valid hex numbers
360function IsValidHex(s) {
361  for (var i = 0; i < s.length; ++i) {
362    var cc = s.charCodeAt(i);
363    if ((48 <= cc && cc <= 57) || (65 <= cc && cc <= 70) || (97 <= cc && cc <= 102)) {
364      // '0'..'9', 'A'..'F' and 'a' .. 'f'.
365    } else {
366      return false;
367    }
368  }
369  return true;
370}
371
372
373// ECMA-262 - B.2.1.
374function URIEscape(str) {
375  var s = ToString(str);
376  return %URIEscape(s);
377}
378
379
380// ECMA-262 - B.2.2.
381function URIUnescape(str) {
382  var s = ToString(str);
383  return %URIUnescape(s);
384}
385
386
387// -------------------------------------------------------------------
388
389function SetupURI() {
390  // Setup non-enumerable URI functions on the global object and set
391  // their names.
392  InstallFunctions(global, DONT_ENUM, $Array(
393    "escape", URIEscape,
394    "unescape", URIUnescape,
395    "decodeURI", URIDecode,
396    "decodeURIComponent", URIDecodeComponent,
397    "encodeURI", URIEncode,
398    "encodeURIComponent", URIEncodeComponent
399  ));
400}
401
402SetupURI();
403