ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 28 | // This file relies on the fact that the following declaration has been made |
| 29 | // in runtime.js: |
| 30 | // var $Array = global.Array; |
| 31 | |
| 32 | // ------------------------------------------------------------------- |
| 33 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | // This file contains support for URI manipulations written in |
| 35 | // JavaScript. |
| 36 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 37 | // Lazily initialized. |
| 38 | var hexCharArray = 0; |
| 39 | var hexCharCodeArray = 0; |
| 40 | |
| 41 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | function URIAddEncodedOctetToBuffer(octet, result, index) { |
| 43 | result[index++] = 37; // Char code of '%'. |
| 44 | result[index++] = hexCharCodeArray[octet >> 4]; |
| 45 | result[index++] = hexCharCodeArray[octet & 0x0F]; |
| 46 | return index; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 47 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | function URIEncodeOctets(octets, result, index) { |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 51 | if (hexCharCodeArray === 0) { |
| 52 | hexCharCodeArray = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, |
| 53 | 65, 66, 67, 68, 69, 70]; |
| 54 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | index = URIAddEncodedOctetToBuffer(octets[0], result, index); |
| 56 | if (octets[1]) index = URIAddEncodedOctetToBuffer(octets[1], result, index); |
| 57 | if (octets[2]) index = URIAddEncodedOctetToBuffer(octets[2], result, index); |
| 58 | if (octets[3]) index = URIAddEncodedOctetToBuffer(octets[3], result, index); |
| 59 | return index; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 60 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | function URIEncodeSingle(cc, result, index) { |
| 64 | var x = (cc >> 12) & 0xF; |
| 65 | var y = (cc >> 6) & 63; |
| 66 | var z = cc & 63; |
| 67 | var octets = new $Array(3); |
| 68 | if (cc <= 0x007F) { |
| 69 | octets[0] = cc; |
| 70 | } else if (cc <= 0x07FF) { |
| 71 | octets[0] = y + 192; |
| 72 | octets[1] = z + 128; |
| 73 | } else { |
| 74 | octets[0] = x + 224; |
| 75 | octets[1] = y + 128; |
| 76 | octets[2] = z + 128; |
| 77 | } |
| 78 | return URIEncodeOctets(octets, result, index); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 79 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | function URIEncodePair(cc1 , cc2, result, index) { |
| 83 | var u = ((cc1 >> 6) & 0xF) + 1; |
| 84 | var w = (cc1 >> 2) & 0xF; |
| 85 | var x = cc1 & 3; |
| 86 | var y = (cc2 >> 6) & 0xF; |
| 87 | var z = cc2 & 63; |
| 88 | var octets = new $Array(4); |
| 89 | octets[0] = (u >> 2) + 240; |
| 90 | octets[1] = (((u & 3) << 4) | w) + 128; |
| 91 | octets[2] = ((x << 4) | y) + 128; |
| 92 | octets[3] = z + 128; |
| 93 | return URIEncodeOctets(octets, result, index); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 94 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | |
| 96 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 97 | function URIHexCharsToCharCode(highChar, lowChar) { |
| 98 | var highCode = HexValueOf(highChar); |
| 99 | var lowCode = HexValueOf(lowChar); |
| 100 | if (highCode == -1 || lowCode == -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 101 | throw new $URIError("URI malformed"); |
| 102 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 103 | return (highCode << 4) | lowCode; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 104 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | |
| 106 | |
| 107 | function URIDecodeOctets(octets, result, index) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 108 | var value; |
| 109 | var o0 = octets[0]; |
| 110 | if (o0 < 0x80) { |
| 111 | value = o0; |
| 112 | } else if (o0 < 0xc2) { |
| 113 | throw new $URIError("URI malformed"); |
| 114 | } else { |
| 115 | var o1 = octets[1]; |
| 116 | if (o0 < 0xe0) { |
| 117 | var a = o0 & 0x1f; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 118 | if ((o1 < 0x80) || (o1 > 0xbf)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 119 | throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 120 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 121 | var b = o1 & 0x3f; |
| 122 | value = (a << 6) + b; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 123 | if (value < 0x80 || value > 0x7ff) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 124 | throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 125 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 126 | } else { |
| 127 | var o2 = octets[2]; |
| 128 | if (o0 < 0xf0) { |
| 129 | var a = o0 & 0x0f; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 130 | if ((o1 < 0x80) || (o1 > 0xbf)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 131 | throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 132 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 133 | var b = o1 & 0x3f; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 134 | if ((o2 < 0x80) || (o2 > 0xbf)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 135 | throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 136 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 137 | var c = o2 & 0x3f; |
| 138 | value = (a << 12) + (b << 6) + c; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 139 | if ((value < 0x800) || (value > 0xffff)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 140 | throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 141 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 142 | } else { |
| 143 | var o3 = octets[3]; |
| 144 | if (o0 < 0xf8) { |
| 145 | var a = (o0 & 0x07); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 146 | if ((o1 < 0x80) || (o1 > 0xbf)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 147 | throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 148 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 149 | var b = (o1 & 0x3f); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 150 | if ((o2 < 0x80) || (o2 > 0xbf)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 151 | throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 152 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 153 | var c = (o2 & 0x3f); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 154 | if ((o3 < 0x80) || (o3 > 0xbf)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 155 | throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 156 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 157 | var d = (o3 & 0x3f); |
| 158 | value = (a << 18) + (b << 12) + (c << 6) + d; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 159 | if ((value < 0x10000) || (value > 0x10ffff)) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 160 | throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 161 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 162 | } else { |
| 163 | throw new $URIError("URI malformed"); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 168 | if (0xD800 <= value && value <= 0xDFFF) { |
| 169 | throw new $URIError("URI malformed"); |
| 170 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 171 | if (value < 0x10000) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 172 | %_TwoByteSeqStringSetChar(result, index++, value); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 173 | return index; |
| 174 | } else { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 175 | %_TwoByteSeqStringSetChar(result, index++, (value >> 10) + 0xd7c0); |
| 176 | %_TwoByteSeqStringSetChar(result, index++, (value & 0x3ff) + 0xdc00); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 177 | return index; |
| 178 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 179 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | // ECMA-262, section 15.1.3 |
| 183 | function Encode(uri, unescape) { |
| 184 | var uriLength = uri.length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 185 | var array = new InternalArray(uriLength); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | var index = 0; |
| 187 | for (var k = 0; k < uriLength; k++) { |
| 188 | var cc1 = uri.charCodeAt(k); |
| 189 | if (unescape(cc1)) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 190 | array[index++] = cc1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 191 | } else { |
| 192 | if (cc1 >= 0xDC00 && cc1 <= 0xDFFF) throw new $URIError("URI malformed"); |
| 193 | if (cc1 < 0xD800 || cc1 > 0xDBFF) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 194 | index = URIEncodeSingle(cc1, array, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 195 | } else { |
| 196 | k++; |
| 197 | if (k == uriLength) throw new $URIError("URI malformed"); |
| 198 | var cc2 = uri.charCodeAt(k); |
| 199 | if (cc2 < 0xDC00 || cc2 > 0xDFFF) throw new $URIError("URI malformed"); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 200 | index = URIEncodePair(cc1, cc2, array, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 204 | |
| 205 | var result = %NewString(array.length, NEW_ONE_BYTE_STRING); |
| 206 | for (var i = 0; i < array.length; i++) { |
| 207 | %_OneByteSeqStringSetChar(result, i, array[i]); |
| 208 | } |
| 209 | return result; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 210 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | // ECMA-262, section 15.1.3 |
| 214 | function Decode(uri, reserved) { |
| 215 | var uriLength = uri.length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 216 | var one_byte = %NewString(uriLength, NEW_ONE_BYTE_STRING); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | var index = 0; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 218 | var k = 0; |
| 219 | |
| 220 | // Optimistically assume ascii string. |
| 221 | for ( ; k < uriLength; k++) { |
| 222 | var code = uri.charCodeAt(k); |
| 223 | if (code == 37) { // '%' |
| 224 | if (k + 2 >= uriLength) throw new $URIError("URI malformed"); |
| 225 | var cc = URIHexCharsToCharCode(uri.charCodeAt(k+1), uri.charCodeAt(k+2)); |
| 226 | if (cc >> 7) break; // Assumption wrong, two byte string. |
| 227 | if (reserved(cc)) { |
| 228 | %_OneByteSeqStringSetChar(one_byte, index++, 37); // '%'. |
| 229 | %_OneByteSeqStringSetChar(one_byte, index++, uri.charCodeAt(k+1)); |
| 230 | %_OneByteSeqStringSetChar(one_byte, index++, uri.charCodeAt(k+2)); |
| 231 | } else { |
| 232 | %_OneByteSeqStringSetChar(one_byte, index++, cc); |
| 233 | } |
| 234 | k += 2; |
| 235 | } else { |
| 236 | if (code > 0x7f) break; // Assumption wrong, two byte string. |
| 237 | %_OneByteSeqStringSetChar(one_byte, index++, code); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | one_byte = %TruncateString(one_byte, index); |
| 242 | if (k == uriLength) return one_byte; |
| 243 | |
| 244 | // Write into two byte string. |
| 245 | var two_byte = %NewString(uriLength - k, NEW_TWO_BYTE_STRING); |
| 246 | index = 0; |
| 247 | |
| 248 | for ( ; k < uriLength; k++) { |
| 249 | var code = uri.charCodeAt(k); |
| 250 | if (code == 37) { // '%' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | if (k + 2 >= uriLength) throw new $URIError("URI malformed"); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 252 | var cc = URIHexCharsToCharCode(uri.charCodeAt(++k), uri.charCodeAt(++k)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | if (cc >> 7) { |
| 254 | var n = 0; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 255 | while (((cc << ++n) & 0x80) != 0) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 256 | if (n == 1 || n > 4) throw new $URIError("URI malformed"); |
| 257 | var octets = new $Array(n); |
| 258 | octets[0] = cc; |
| 259 | if (k + 3 * (n - 1) >= uriLength) throw new $URIError("URI malformed"); |
| 260 | for (var i = 1; i < n; i++) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 261 | if (uri.charAt(++k) != '%') throw new $URIError("URI malformed"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 262 | octets[i] = URIHexCharsToCharCode(uri.charCodeAt(++k), |
| 263 | uri.charCodeAt(++k)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 264 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 265 | index = URIDecodeOctets(octets, two_byte, index); |
| 266 | } else if (reserved(cc)) { |
| 267 | %_TwoByteSeqStringSetChar(two_byte, index++, 37); // '%'. |
| 268 | %_TwoByteSeqStringSetChar(two_byte, index++, uri.charCodeAt(k - 1)); |
| 269 | %_TwoByteSeqStringSetChar(two_byte, index++, uri.charCodeAt(k)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 270 | } else { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 271 | %_TwoByteSeqStringSetChar(two_byte, index++, cc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | } |
| 273 | } else { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 274 | %_TwoByteSeqStringSetChar(two_byte, index++, code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 277 | |
| 278 | two_byte = %TruncateString(two_byte, index); |
| 279 | return one_byte + two_byte; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 280 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | |
| 282 | |
| 283 | // ECMA-262 - 15.1.3.1. |
| 284 | function URIDecode(uri) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 285 | var reservedPredicate = function(cc) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 286 | // #$ |
| 287 | if (35 <= cc && cc <= 36) return true; |
| 288 | // & |
| 289 | if (cc == 38) return true; |
| 290 | // +, |
| 291 | if (43 <= cc && cc <= 44) return true; |
| 292 | // / |
| 293 | if (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; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 300 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | return false; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 302 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 303 | var string = ToString(uri); |
| 304 | return Decode(string, reservedPredicate); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 305 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 306 | |
| 307 | |
| 308 | // ECMA-262 - 15.1.3.2. |
| 309 | function URIDecodeComponent(component) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 310 | var reservedPredicate = function(cc) { return false; }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | var string = ToString(component); |
| 312 | return Decode(string, reservedPredicate); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 313 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 314 | |
| 315 | |
| 316 | // Does the char code correspond to an alpha-numeric char. |
| 317 | function isAlphaNumeric(cc) { |
| 318 | // a - z |
| 319 | if (97 <= cc && cc <= 122) return true; |
| 320 | // A - Z |
| 321 | if (65 <= cc && cc <= 90) return true; |
| 322 | // 0 - 9 |
| 323 | if (48 <= cc && cc <= 57) return true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 324 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 325 | return false; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 326 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | |
| 328 | |
| 329 | // ECMA-262 - 15.1.3.3. |
| 330 | function URIEncode(uri) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 331 | var unescapePredicate = function(cc) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | if (isAlphaNumeric(cc)) return true; |
| 333 | // ! |
| 334 | if (cc == 33) return true; |
| 335 | // #$ |
| 336 | if (35 <= cc && cc <= 36) return true; |
| 337 | // &'()*+,-./ |
| 338 | if (38 <= cc && cc <= 47) return true; |
| 339 | // :; |
| 340 | if (58 <= cc && cc <= 59) return true; |
| 341 | // = |
| 342 | if (cc == 61) return true; |
| 343 | // ?@ |
| 344 | if (63 <= cc && cc <= 64) return true; |
| 345 | // _ |
| 346 | if (cc == 95) return true; |
| 347 | // ~ |
| 348 | if (cc == 126) return true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 349 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 350 | return false; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 351 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 352 | |
| 353 | var string = ToString(uri); |
| 354 | return Encode(string, unescapePredicate); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 355 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 356 | |
| 357 | |
| 358 | // ECMA-262 - 15.1.3.4 |
| 359 | function URIEncodeComponent(component) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 360 | var unescapePredicate = function(cc) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | if (isAlphaNumeric(cc)) return true; |
| 362 | // ! |
| 363 | if (cc == 33) return true; |
| 364 | // '()* |
| 365 | if (39 <= cc && cc <= 42) return true; |
| 366 | // -. |
| 367 | if (45 <= cc && cc <= 46) return true; |
| 368 | // _ |
| 369 | if (cc == 95) return true; |
| 370 | // ~ |
| 371 | if (cc == 126) return true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 372 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 373 | return false; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 374 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 375 | |
| 376 | var string = ToString(component); |
| 377 | return Encode(string, unescapePredicate); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 378 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 379 | |
| 380 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 381 | function HexValueOf(code) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 382 | // 0-9 |
| 383 | if (code >= 48 && code <= 57) return code - 48; |
| 384 | // A-F |
| 385 | if (code >= 65 && code <= 70) return code - 55; |
| 386 | // a-f |
| 387 | if (code >= 97 && code <= 102) return code - 87; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 388 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 389 | return -1; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 390 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 391 | |
| 392 | |
| 393 | // Convert a character code to 4-digit hex string representation |
| 394 | // 64 -> 0040, 62234 -> F31A. |
| 395 | function CharCodeToHex4Str(cc) { |
| 396 | var r = ""; |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 397 | if (hexCharArray === 0) { |
| 398 | hexCharArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", |
| 399 | "A", "B", "C", "D", "E", "F"]; |
| 400 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 401 | for (var i = 0; i < 4; ++i) { |
| 402 | var c = hexCharArray[cc & 0x0F]; |
| 403 | r = c + r; |
| 404 | cc = cc >>> 4; |
| 405 | } |
| 406 | return r; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 407 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 408 | |
| 409 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 410 | // Returns true if all digits in string s are valid hex numbers |
| 411 | function IsValidHex(s) { |
| 412 | for (var i = 0; i < s.length; ++i) { |
| 413 | var cc = s.charCodeAt(i); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 414 | if ((48 <= cc && cc <= 57) || |
| 415 | (65 <= cc && cc <= 70) || |
| 416 | (97 <= cc && cc <= 102)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 417 | // '0'..'9', 'A'..'F' and 'a' .. 'f'. |
| 418 | } else { |
| 419 | return false; |
| 420 | } |
| 421 | } |
| 422 | return true; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 423 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 424 | |
| 425 | |
| 426 | // ECMA-262 - B.2.1. |
| 427 | function URIEscape(str) { |
| 428 | var s = ToString(str); |
| 429 | return %URIEscape(s); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 430 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 431 | |
| 432 | |
| 433 | // ECMA-262 - B.2.2. |
| 434 | function URIUnescape(str) { |
| 435 | var s = ToString(str); |
| 436 | return %URIUnescape(s); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | // ------------------------------------------------------------------- |
| 441 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 442 | function SetUpUri() { |
| 443 | %CheckIsBootstrapping(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 444 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 445 | // Set up non-enumerable URI functions on the global object and set |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 446 | // their names. |
| 447 | InstallFunctions(global, DONT_ENUM, $Array( |
| 448 | "escape", URIEscape, |
| 449 | "unescape", URIUnescape, |
| 450 | "decodeURI", URIDecode, |
| 451 | "decodeURIComponent", URIDecodeComponent, |
| 452 | "encodeURI", URIEncode, |
| 453 | "encodeURIComponent", URIEncodeComponent |
| 454 | )); |
| 455 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 456 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 457 | SetUpUri(); |