| @@ 15-53 (lines=39) @@ | ||
| 12 | ||
| 13 | var FloatArray = global.Float64Array || global.Float32Array; // make PhantomJS happy |
|
| 14 | ||
| 15 | function string_to_bytes ( str, utf8 ) { |
|
| 16 | utf8 = !!utf8; |
|
| 17 | ||
| 18 | var len = str.length, |
|
| 19 | bytes = new Uint8Array( utf8 ? 4*len : len ); |
|
| 20 | ||
| 21 | for ( var i = 0, j = 0; i < len; i++ ) { |
|
| 22 | var c = str.charCodeAt(i); |
|
| 23 | ||
| 24 | if ( utf8 && 0xd800 <= c && c <= 0xdbff ) { |
|
| 25 | if ( ++i >= len ) throw new Error( "Malformed string, low surrogate expected at position " + i ); |
|
| 26 | c = ( (c ^ 0xd800) << 10 ) | 0x10000 | ( str.charCodeAt(i) ^ 0xdc00 ); |
|
| 27 | } |
|
| 28 | else if ( !utf8 && c >>> 8 ) { |
|
| 29 | throw new Error("Wide characters are not allowed."); |
|
| 30 | } |
|
| 31 | ||
| 32 | if ( !utf8 || c <= 0x7f ) { |
|
| 33 | bytes[j++] = c; |
|
| 34 | } |
|
| 35 | else if ( c <= 0x7ff ) { |
|
| 36 | bytes[j++] = 0xc0 | (c >> 6); |
|
| 37 | bytes[j++] = 0x80 | (c & 0x3f); |
|
| 38 | } |
|
| 39 | else if ( c <= 0xffff ) { |
|
| 40 | bytes[j++] = 0xe0 | (c >> 12); |
|
| 41 | bytes[j++] = 0x80 | (c >> 6 & 0x3f); |
|
| 42 | bytes[j++] = 0x80 | (c & 0x3f); |
|
| 43 | } |
|
| 44 | else { |
|
| 45 | bytes[j++] = 0xf0 | (c >> 18); |
|
| 46 | bytes[j++] = 0x80 | (c >> 12 & 0x3f); |
|
| 47 | bytes[j++] = 0x80 | (c >> 6 & 0x3f); |
|
| 48 | bytes[j++] = 0x80 | (c & 0x3f); |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||
| 52 | return bytes.subarray(0, j); |
|
| 53 | } |
|
| 54 | ||
| 55 | function hex_to_bytes ( str ) { |
|
| 56 | var len = str.length; |
|
| @@ 10-23 (lines=14) @@ | ||
| 7 | var e = Error.apply(this, arguments); |
|
| 8 | this.message = e.message, this.stack = e.stack; |
|
| 9 | } |
|
| 10 | function c(e, t) { |
|
| 11 | t = !!t; |
|
| 12 | for (var r = e.length, s = new Uint8Array(t ? 4 * r : r), c = 0, i = 0; c < r; c++) { |
|
| 13 | var a = e.charCodeAt(c); |
|
| 14 | if (t && 55296 <= a && a <= 56319) { |
|
| 15 | if (++c >= r) throw new Error("Malformed string, low surrogate expected at position " + c); |
|
| 16 | a = (55296 ^ a) << 10 | 65536 | 56320 ^ e.charCodeAt(c); |
|
| 17 | } else if (!t && a >>> 8) throw new Error("Wide characters are not allowed."); |
|
| 18 | !t || a <= 127 ? s[i++] = a : a <= 2047 ? (s[i++] = 192 | a >> 6, s[i++] = 128 | 63 & a) : a <= 65535 ? (s[i++] = 224 | a >> 12, |
|
| 19 | s[i++] = 128 | a >> 6 & 63, s[i++] = 128 | 63 & a) : (s[i++] = 240 | a >> 18, s[i++] = 128 | a >> 12 & 63, |
|
| 20 | s[i++] = 128 | a >> 6 & 63, s[i++] = 128 | 63 & a); |
|
| 21 | } |
|
| 22 | return s.subarray(0, i); |
|
| 23 | } |
|
| 24 | function i(e) { |
|
| 25 | return btoa(function(e, t) { |
|
| 26 | t = !!t; |
|