spice-html5-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +674 -0
- data/COPYING.LESSER +165 -0
- data/LICENSE.txt +15 -0
- data/README.md +35 -0
- data/lib/spice-html5-rails/version.rb +7 -0
- data/lib/spice-html5-rails.rb +10 -0
- data/vendor/assets/javascripts/spice-html5.js +25 -0
- data/vendor/assets/javascripts/spiceHTML5/atKeynames.js +183 -0
- data/vendor/assets/javascripts/spiceHTML5/bitmap.js +51 -0
- data/vendor/assets/javascripts/spiceHTML5/cursor.js +92 -0
- data/vendor/assets/javascripts/spiceHTML5/display.js +806 -0
- data/vendor/assets/javascripts/spiceHTML5/enums.js +282 -0
- data/vendor/assets/javascripts/spiceHTML5/inputs.js +271 -0
- data/vendor/assets/javascripts/spiceHTML5/lz.js +166 -0
- data/vendor/assets/javascripts/spiceHTML5/main.js +177 -0
- data/vendor/assets/javascripts/spiceHTML5/png.js +256 -0
- data/vendor/assets/javascripts/spiceHTML5/quic.js +1335 -0
- data/vendor/assets/javascripts/spiceHTML5/spiceconn.js +455 -0
- data/vendor/assets/javascripts/spiceHTML5/spicedataview.js +96 -0
- data/vendor/assets/javascripts/spiceHTML5/spicemsg.js +883 -0
- data/vendor/assets/javascripts/spiceHTML5/spicetype.js +480 -0
- data/vendor/assets/javascripts/spiceHTML5/thirdparty/jsbn.js +589 -0
- data/vendor/assets/javascripts/spiceHTML5/thirdparty/prng4.js +79 -0
- data/vendor/assets/javascripts/spiceHTML5/thirdparty/rng.js +102 -0
- data/vendor/assets/javascripts/spiceHTML5/thirdparty/rsa.js +146 -0
- data/vendor/assets/javascripts/spiceHTML5/thirdparty/sha1.js +346 -0
- data/vendor/assets/javascripts/spiceHTML5/ticket.js +250 -0
- data/vendor/assets/javascripts/spiceHTML5/utils.js +261 -0
- data/vendor/assets/javascripts/spiceHTML5/wire.js +123 -0
- metadata +108 -0
@@ -0,0 +1,102 @@
|
|
1
|
+
// Downloaded from http://www-cs-students.stanford.edu/~tjw/jsbn/ by Jeremy White on 6/1/2012
|
2
|
+
|
3
|
+
/*
|
4
|
+
* Copyright (c) 2003-2005 Tom Wu
|
5
|
+
* All Rights Reserved.
|
6
|
+
*
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
8
|
+
* a copy of this software and associated documentation files (the
|
9
|
+
* "Software"), to deal in the Software without restriction, including
|
10
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
11
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
12
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
13
|
+
* the following conditions:
|
14
|
+
*
|
15
|
+
* The above copyright notice and this permission notice shall be
|
16
|
+
* included in all copies or substantial portions of the Software.
|
17
|
+
*
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
19
|
+
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
20
|
+
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
21
|
+
*
|
22
|
+
* IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
|
23
|
+
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
|
24
|
+
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
|
25
|
+
* THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
|
26
|
+
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
27
|
+
*
|
28
|
+
* In addition, the following condition applies:
|
29
|
+
*
|
30
|
+
* All redistributions must retain an intact copy of this copyright notice
|
31
|
+
* and disclaimer.
|
32
|
+
*/
|
33
|
+
|
34
|
+
|
35
|
+
// Random number generator - requires a PRNG backend, e.g. prng4.js
|
36
|
+
|
37
|
+
// For best results, put code like
|
38
|
+
// <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
|
39
|
+
// in your main HTML document.
|
40
|
+
|
41
|
+
var rng_state;
|
42
|
+
var rng_pool;
|
43
|
+
var rng_pptr;
|
44
|
+
|
45
|
+
// Mix in a 32-bit integer into the pool
|
46
|
+
function rng_seed_int(x) {
|
47
|
+
rng_pool[rng_pptr++] ^= x & 255;
|
48
|
+
rng_pool[rng_pptr++] ^= (x >> 8) & 255;
|
49
|
+
rng_pool[rng_pptr++] ^= (x >> 16) & 255;
|
50
|
+
rng_pool[rng_pptr++] ^= (x >> 24) & 255;
|
51
|
+
if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
|
52
|
+
}
|
53
|
+
|
54
|
+
// Mix in the current time (w/milliseconds) into the pool
|
55
|
+
function rng_seed_time() {
|
56
|
+
rng_seed_int(new Date().getTime());
|
57
|
+
}
|
58
|
+
|
59
|
+
// Initialize the pool with junk if needed.
|
60
|
+
if(rng_pool == null) {
|
61
|
+
rng_pool = new Array();
|
62
|
+
rng_pptr = 0;
|
63
|
+
var t;
|
64
|
+
if(navigator.appName == "Netscape" && navigator.appVersion < "5" && window.crypto) {
|
65
|
+
// Extract entropy (256 bits) from NS4 RNG if available
|
66
|
+
var z = window.crypto.random(32);
|
67
|
+
for(t = 0; t < z.length; ++t)
|
68
|
+
rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;
|
69
|
+
}
|
70
|
+
while(rng_pptr < rng_psize) { // extract some randomness from Math.random()
|
71
|
+
t = Math.floor(65536 * Math.random());
|
72
|
+
rng_pool[rng_pptr++] = t >>> 8;
|
73
|
+
rng_pool[rng_pptr++] = t & 255;
|
74
|
+
}
|
75
|
+
rng_pptr = 0;
|
76
|
+
rng_seed_time();
|
77
|
+
//rng_seed_int(window.screenX);
|
78
|
+
//rng_seed_int(window.screenY);
|
79
|
+
}
|
80
|
+
|
81
|
+
function rng_get_byte() {
|
82
|
+
if(rng_state == null) {
|
83
|
+
rng_seed_time();
|
84
|
+
rng_state = prng_newstate();
|
85
|
+
rng_state.init(rng_pool);
|
86
|
+
for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
|
87
|
+
rng_pool[rng_pptr] = 0;
|
88
|
+
rng_pptr = 0;
|
89
|
+
//rng_pool = null;
|
90
|
+
}
|
91
|
+
// TODO: allow reseeding after first request
|
92
|
+
return rng_state.next();
|
93
|
+
}
|
94
|
+
|
95
|
+
function rng_get_bytes(ba) {
|
96
|
+
var i;
|
97
|
+
for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
|
98
|
+
}
|
99
|
+
|
100
|
+
function SecureRandom() {}
|
101
|
+
|
102
|
+
SecureRandom.prototype.nextBytes = rng_get_bytes;
|
@@ -0,0 +1,146 @@
|
|
1
|
+
// Downloaded from http://www-cs-students.stanford.edu/~tjw/jsbn/ by Jeremy White on 6/1/2012
|
2
|
+
|
3
|
+
/*
|
4
|
+
* Copyright (c) 2003-2005 Tom Wu
|
5
|
+
* All Rights Reserved.
|
6
|
+
*
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
8
|
+
* a copy of this software and associated documentation files (the
|
9
|
+
* "Software"), to deal in the Software without restriction, including
|
10
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
11
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
12
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
13
|
+
* the following conditions:
|
14
|
+
*
|
15
|
+
* The above copyright notice and this permission notice shall be
|
16
|
+
* included in all copies or substantial portions of the Software.
|
17
|
+
*
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
19
|
+
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
20
|
+
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
21
|
+
*
|
22
|
+
* IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
|
23
|
+
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
|
24
|
+
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
|
25
|
+
* THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
|
26
|
+
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
27
|
+
*
|
28
|
+
* In addition, the following condition applies:
|
29
|
+
*
|
30
|
+
* All redistributions must retain an intact copy of this copyright notice
|
31
|
+
* and disclaimer.
|
32
|
+
*/
|
33
|
+
|
34
|
+
|
35
|
+
// Depends on jsbn.js and rng.js
|
36
|
+
|
37
|
+
// Version 1.1: support utf-8 encoding in pkcs1pad2
|
38
|
+
|
39
|
+
// convert a (hex) string to a bignum object
|
40
|
+
function parseBigInt(str,r) {
|
41
|
+
return new BigInteger(str,r);
|
42
|
+
}
|
43
|
+
|
44
|
+
function linebrk(s,n) {
|
45
|
+
var ret = "";
|
46
|
+
var i = 0;
|
47
|
+
while(i + n < s.length) {
|
48
|
+
ret += s.substring(i,i+n) + "\n";
|
49
|
+
i += n;
|
50
|
+
}
|
51
|
+
return ret + s.substring(i,s.length);
|
52
|
+
}
|
53
|
+
|
54
|
+
function byte2Hex(b) {
|
55
|
+
if(b < 0x10)
|
56
|
+
return "0" + b.toString(16);
|
57
|
+
else
|
58
|
+
return b.toString(16);
|
59
|
+
}
|
60
|
+
|
61
|
+
// PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
|
62
|
+
function pkcs1pad2(s,n) {
|
63
|
+
if(n < s.length + 11) { // TODO: fix for utf-8
|
64
|
+
alert("Message too long for RSA");
|
65
|
+
return null;
|
66
|
+
}
|
67
|
+
var ba = new Array();
|
68
|
+
var i = s.length - 1;
|
69
|
+
while(i >= 0 && n > 0) {
|
70
|
+
var c = s.charCodeAt(i--);
|
71
|
+
if(c < 128) { // encode using utf-8
|
72
|
+
ba[--n] = c;
|
73
|
+
}
|
74
|
+
else if((c > 127) && (c < 2048)) {
|
75
|
+
ba[--n] = (c & 63) | 128;
|
76
|
+
ba[--n] = (c >> 6) | 192;
|
77
|
+
}
|
78
|
+
else {
|
79
|
+
ba[--n] = (c & 63) | 128;
|
80
|
+
ba[--n] = ((c >> 6) & 63) | 128;
|
81
|
+
ba[--n] = (c >> 12) | 224;
|
82
|
+
}
|
83
|
+
}
|
84
|
+
ba[--n] = 0;
|
85
|
+
var rng = new SecureRandom();
|
86
|
+
var x = new Array();
|
87
|
+
while(n > 2) { // random non-zero pad
|
88
|
+
x[0] = 0;
|
89
|
+
while(x[0] == 0) rng.nextBytes(x);
|
90
|
+
ba[--n] = x[0];
|
91
|
+
}
|
92
|
+
ba[--n] = 2;
|
93
|
+
ba[--n] = 0;
|
94
|
+
return new BigInteger(ba);
|
95
|
+
}
|
96
|
+
|
97
|
+
// "empty" RSA key constructor
|
98
|
+
function RSAKey() {
|
99
|
+
this.n = null;
|
100
|
+
this.e = 0;
|
101
|
+
this.d = null;
|
102
|
+
this.p = null;
|
103
|
+
this.q = null;
|
104
|
+
this.dmp1 = null;
|
105
|
+
this.dmq1 = null;
|
106
|
+
this.coeff = null;
|
107
|
+
}
|
108
|
+
|
109
|
+
// Set the public key fields N and e from hex strings
|
110
|
+
function RSASetPublic(N,E) {
|
111
|
+
if(N != null && E != null && N.length > 0 && E.length > 0) {
|
112
|
+
this.n = parseBigInt(N,16);
|
113
|
+
this.e = parseInt(E,16);
|
114
|
+
}
|
115
|
+
else
|
116
|
+
alert("Invalid RSA public key");
|
117
|
+
}
|
118
|
+
|
119
|
+
// Perform raw public operation on "x": return x^e (mod n)
|
120
|
+
function RSADoPublic(x) {
|
121
|
+
return x.modPowInt(this.e, this.n);
|
122
|
+
}
|
123
|
+
|
124
|
+
// Return the PKCS#1 RSA encryption of "text" as an even-length hex string
|
125
|
+
function RSAEncrypt(text) {
|
126
|
+
var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
|
127
|
+
if(m == null) return null;
|
128
|
+
var c = this.doPublic(m);
|
129
|
+
if(c == null) return null;
|
130
|
+
var h = c.toString(16);
|
131
|
+
if((h.length & 1) == 0) return h; else return "0" + h;
|
132
|
+
}
|
133
|
+
|
134
|
+
// Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
|
135
|
+
//function RSAEncryptB64(text) {
|
136
|
+
// var h = this.encrypt(text);
|
137
|
+
// if(h) return hex2b64(h); else return null;
|
138
|
+
//}
|
139
|
+
|
140
|
+
// protected
|
141
|
+
RSAKey.prototype.doPublic = RSADoPublic;
|
142
|
+
|
143
|
+
// public
|
144
|
+
RSAKey.prototype.setPublic = RSASetPublic;
|
145
|
+
RSAKey.prototype.encrypt = RSAEncrypt;
|
146
|
+
//RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
|
@@ -0,0 +1,346 @@
|
|
1
|
+
/*
|
2
|
+
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
|
3
|
+
* in FIPS 180-1
|
4
|
+
* Version 2.2 Copyright Paul Johnston 2000 - 2009.
|
5
|
+
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
6
|
+
* Distributed under the BSD License
|
7
|
+
* See http://pajhome.org.uk/crypt/md5 for details.
|
8
|
+
*/
|
9
|
+
|
10
|
+
/* Downloaded 6/1/2012 from the above address by Jeremy White.
|
11
|
+
License reproduce here for completeness:
|
12
|
+
|
13
|
+
Copyright (c) 1998 - 2009, Paul Johnston & Contributors
|
14
|
+
All rights reserved.
|
15
|
+
|
16
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
17
|
+
|
18
|
+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
19
|
+
|
20
|
+
Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
21
|
+
|
22
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
23
|
+
|
24
|
+
*/
|
25
|
+
|
26
|
+
/*
|
27
|
+
* Configurable variables. You may need to tweak these to be compatible with
|
28
|
+
* the server-side, but the defaults work in most cases.
|
29
|
+
*/
|
30
|
+
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
|
31
|
+
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
|
32
|
+
|
33
|
+
/*
|
34
|
+
* These are the functions you'll usually want to call
|
35
|
+
* They take string arguments and return either hex or base-64 encoded strings
|
36
|
+
*/
|
37
|
+
function hex_sha1(s) { return rstr2hex(rstr_sha1(str2rstr_utf8(s))); }
|
38
|
+
function b64_sha1(s) { return rstr2b64(rstr_sha1(str2rstr_utf8(s))); }
|
39
|
+
function any_sha1(s, e) { return rstr2any(rstr_sha1(str2rstr_utf8(s)), e); }
|
40
|
+
function hex_hmac_sha1(k, d)
|
41
|
+
{ return rstr2hex(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); }
|
42
|
+
function b64_hmac_sha1(k, d)
|
43
|
+
{ return rstr2b64(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d))); }
|
44
|
+
function any_hmac_sha1(k, d, e)
|
45
|
+
{ return rstr2any(rstr_hmac_sha1(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
|
46
|
+
|
47
|
+
/*
|
48
|
+
* Perform a simple self-test to see if the VM is working
|
49
|
+
*/
|
50
|
+
function sha1_vm_test()
|
51
|
+
{
|
52
|
+
return hex_sha1("abc").toLowerCase() == "a9993e364706816aba3e25717850c26c9cd0d89d";
|
53
|
+
}
|
54
|
+
|
55
|
+
/*
|
56
|
+
* Calculate the SHA1 of a raw string
|
57
|
+
*/
|
58
|
+
function rstr_sha1(s)
|
59
|
+
{
|
60
|
+
return binb2rstr(binb_sha1(rstr2binb(s), s.length * 8));
|
61
|
+
}
|
62
|
+
|
63
|
+
/*
|
64
|
+
* Calculate the HMAC-SHA1 of a key and some data (raw strings)
|
65
|
+
*/
|
66
|
+
function rstr_hmac_sha1(key, data)
|
67
|
+
{
|
68
|
+
var bkey = rstr2binb(key);
|
69
|
+
if(bkey.length > 16) bkey = binb_sha1(bkey, key.length * 8);
|
70
|
+
|
71
|
+
var ipad = Array(16), opad = Array(16);
|
72
|
+
for(var i = 0; i < 16; i++)
|
73
|
+
{
|
74
|
+
ipad[i] = bkey[i] ^ 0x36363636;
|
75
|
+
opad[i] = bkey[i] ^ 0x5C5C5C5C;
|
76
|
+
}
|
77
|
+
|
78
|
+
var hash = binb_sha1(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
|
79
|
+
return binb2rstr(binb_sha1(opad.concat(hash), 512 + 160));
|
80
|
+
}
|
81
|
+
|
82
|
+
/*
|
83
|
+
* Convert a raw string to a hex string
|
84
|
+
*/
|
85
|
+
function rstr2hex(input)
|
86
|
+
{
|
87
|
+
try { hexcase } catch(e) { hexcase=0; }
|
88
|
+
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
|
89
|
+
var output = "";
|
90
|
+
var x;
|
91
|
+
for(var i = 0; i < input.length; i++)
|
92
|
+
{
|
93
|
+
x = input.charCodeAt(i);
|
94
|
+
output += hex_tab.charAt((x >>> 4) & 0x0F)
|
95
|
+
+ hex_tab.charAt( x & 0x0F);
|
96
|
+
}
|
97
|
+
return output;
|
98
|
+
}
|
99
|
+
|
100
|
+
/*
|
101
|
+
* Convert a raw string to a base-64 string
|
102
|
+
*/
|
103
|
+
function rstr2b64(input)
|
104
|
+
{
|
105
|
+
try { b64pad } catch(e) { b64pad=''; }
|
106
|
+
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
107
|
+
var output = "";
|
108
|
+
var len = input.length;
|
109
|
+
for(var i = 0; i < len; i += 3)
|
110
|
+
{
|
111
|
+
var triplet = (input.charCodeAt(i) << 16)
|
112
|
+
| (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
|
113
|
+
| (i + 2 < len ? input.charCodeAt(i+2) : 0);
|
114
|
+
for(var j = 0; j < 4; j++)
|
115
|
+
{
|
116
|
+
if(i * 8 + j * 6 > input.length * 8) output += b64pad;
|
117
|
+
else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
return output;
|
121
|
+
}
|
122
|
+
|
123
|
+
/*
|
124
|
+
* Convert a raw string to an arbitrary string encoding
|
125
|
+
*/
|
126
|
+
function rstr2any(input, encoding)
|
127
|
+
{
|
128
|
+
var divisor = encoding.length;
|
129
|
+
var remainders = Array();
|
130
|
+
var i, q, x, quotient;
|
131
|
+
|
132
|
+
/* Convert to an array of 16-bit big-endian values, forming the dividend */
|
133
|
+
var dividend = Array(Math.ceil(input.length / 2));
|
134
|
+
for(i = 0; i < dividend.length; i++)
|
135
|
+
{
|
136
|
+
dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
|
137
|
+
}
|
138
|
+
|
139
|
+
/*
|
140
|
+
* Repeatedly perform a long division. The binary array forms the dividend,
|
141
|
+
* the length of the encoding is the divisor. Once computed, the quotient
|
142
|
+
* forms the dividend for the next step. We stop when the dividend is zero.
|
143
|
+
* All remainders are stored for later use.
|
144
|
+
*/
|
145
|
+
while(dividend.length > 0)
|
146
|
+
{
|
147
|
+
quotient = Array();
|
148
|
+
x = 0;
|
149
|
+
for(i = 0; i < dividend.length; i++)
|
150
|
+
{
|
151
|
+
x = (x << 16) + dividend[i];
|
152
|
+
q = Math.floor(x / divisor);
|
153
|
+
x -= q * divisor;
|
154
|
+
if(quotient.length > 0 || q > 0)
|
155
|
+
quotient[quotient.length] = q;
|
156
|
+
}
|
157
|
+
remainders[remainders.length] = x;
|
158
|
+
dividend = quotient;
|
159
|
+
}
|
160
|
+
|
161
|
+
/* Convert the remainders to the output string */
|
162
|
+
var output = "";
|
163
|
+
for(i = remainders.length - 1; i >= 0; i--)
|
164
|
+
output += encoding.charAt(remainders[i]);
|
165
|
+
|
166
|
+
/* Append leading zero equivalents */
|
167
|
+
var full_length = Math.ceil(input.length * 8 /
|
168
|
+
(Math.log(encoding.length) / Math.log(2)))
|
169
|
+
for(i = output.length; i < full_length; i++)
|
170
|
+
output = encoding[0] + output;
|
171
|
+
|
172
|
+
return output;
|
173
|
+
}
|
174
|
+
|
175
|
+
/*
|
176
|
+
* Encode a string as utf-8.
|
177
|
+
* For efficiency, this assumes the input is valid utf-16.
|
178
|
+
*/
|
179
|
+
function str2rstr_utf8(input)
|
180
|
+
{
|
181
|
+
var output = "";
|
182
|
+
var i = -1;
|
183
|
+
var x, y;
|
184
|
+
|
185
|
+
while(++i < input.length)
|
186
|
+
{
|
187
|
+
/* Decode utf-16 surrogate pairs */
|
188
|
+
x = input.charCodeAt(i);
|
189
|
+
y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
|
190
|
+
if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
|
191
|
+
{
|
192
|
+
x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
|
193
|
+
i++;
|
194
|
+
}
|
195
|
+
|
196
|
+
/* Encode output as utf-8 */
|
197
|
+
if(x <= 0x7F)
|
198
|
+
output += String.fromCharCode(x);
|
199
|
+
else if(x <= 0x7FF)
|
200
|
+
output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
|
201
|
+
0x80 | ( x & 0x3F));
|
202
|
+
else if(x <= 0xFFFF)
|
203
|
+
output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
|
204
|
+
0x80 | ((x >>> 6 ) & 0x3F),
|
205
|
+
0x80 | ( x & 0x3F));
|
206
|
+
else if(x <= 0x1FFFFF)
|
207
|
+
output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
|
208
|
+
0x80 | ((x >>> 12) & 0x3F),
|
209
|
+
0x80 | ((x >>> 6 ) & 0x3F),
|
210
|
+
0x80 | ( x & 0x3F));
|
211
|
+
}
|
212
|
+
return output;
|
213
|
+
}
|
214
|
+
|
215
|
+
/*
|
216
|
+
* Encode a string as utf-16
|
217
|
+
*/
|
218
|
+
function str2rstr_utf16le(input)
|
219
|
+
{
|
220
|
+
var output = "";
|
221
|
+
for(var i = 0; i < input.length; i++)
|
222
|
+
output += String.fromCharCode( input.charCodeAt(i) & 0xFF,
|
223
|
+
(input.charCodeAt(i) >>> 8) & 0xFF);
|
224
|
+
return output;
|
225
|
+
}
|
226
|
+
|
227
|
+
function str2rstr_utf16be(input)
|
228
|
+
{
|
229
|
+
var output = "";
|
230
|
+
for(var i = 0; i < input.length; i++)
|
231
|
+
output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
|
232
|
+
input.charCodeAt(i) & 0xFF);
|
233
|
+
return output;
|
234
|
+
}
|
235
|
+
|
236
|
+
/*
|
237
|
+
* Convert a raw string to an array of big-endian words
|
238
|
+
* Characters >255 have their high-byte silently ignored.
|
239
|
+
*/
|
240
|
+
function rstr2binb(input)
|
241
|
+
{
|
242
|
+
var output = Array(input.length >> 2);
|
243
|
+
for(var i = 0; i < output.length; i++)
|
244
|
+
output[i] = 0;
|
245
|
+
for(var i = 0; i < input.length * 8; i += 8)
|
246
|
+
output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
|
247
|
+
return output;
|
248
|
+
}
|
249
|
+
|
250
|
+
/*
|
251
|
+
* Convert an array of big-endian words to a string
|
252
|
+
*/
|
253
|
+
function binb2rstr(input)
|
254
|
+
{
|
255
|
+
var output = "";
|
256
|
+
for(var i = 0; i < input.length * 32; i += 8)
|
257
|
+
output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF);
|
258
|
+
return output;
|
259
|
+
}
|
260
|
+
|
261
|
+
/*
|
262
|
+
* Calculate the SHA-1 of an array of big-endian words, and a bit length
|
263
|
+
*/
|
264
|
+
function binb_sha1(x, len)
|
265
|
+
{
|
266
|
+
/* append padding */
|
267
|
+
x[len >> 5] |= 0x80 << (24 - len % 32);
|
268
|
+
x[((len + 64 >> 9) << 4) + 15] = len;
|
269
|
+
|
270
|
+
var w = Array(80);
|
271
|
+
var a = 1732584193;
|
272
|
+
var b = -271733879;
|
273
|
+
var c = -1732584194;
|
274
|
+
var d = 271733878;
|
275
|
+
var e = -1009589776;
|
276
|
+
|
277
|
+
for(var i = 0; i < x.length; i += 16)
|
278
|
+
{
|
279
|
+
var olda = a;
|
280
|
+
var oldb = b;
|
281
|
+
var oldc = c;
|
282
|
+
var oldd = d;
|
283
|
+
var olde = e;
|
284
|
+
|
285
|
+
for(var j = 0; j < 80; j++)
|
286
|
+
{
|
287
|
+
if(j < 16) w[j] = x[i + j];
|
288
|
+
else w[j] = bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
|
289
|
+
var t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)),
|
290
|
+
safe_add(safe_add(e, w[j]), sha1_kt(j)));
|
291
|
+
e = d;
|
292
|
+
d = c;
|
293
|
+
c = bit_rol(b, 30);
|
294
|
+
b = a;
|
295
|
+
a = t;
|
296
|
+
}
|
297
|
+
|
298
|
+
a = safe_add(a, olda);
|
299
|
+
b = safe_add(b, oldb);
|
300
|
+
c = safe_add(c, oldc);
|
301
|
+
d = safe_add(d, oldd);
|
302
|
+
e = safe_add(e, olde);
|
303
|
+
}
|
304
|
+
return Array(a, b, c, d, e);
|
305
|
+
|
306
|
+
}
|
307
|
+
|
308
|
+
/*
|
309
|
+
* Perform the appropriate triplet combination function for the current
|
310
|
+
* iteration
|
311
|
+
*/
|
312
|
+
function sha1_ft(t, b, c, d)
|
313
|
+
{
|
314
|
+
if(t < 20) return (b & c) | ((~b) & d);
|
315
|
+
if(t < 40) return b ^ c ^ d;
|
316
|
+
if(t < 60) return (b & c) | (b & d) | (c & d);
|
317
|
+
return b ^ c ^ d;
|
318
|
+
}
|
319
|
+
|
320
|
+
/*
|
321
|
+
* Determine the appropriate additive constant for the current iteration
|
322
|
+
*/
|
323
|
+
function sha1_kt(t)
|
324
|
+
{
|
325
|
+
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
|
326
|
+
(t < 60) ? -1894007588 : -899497514;
|
327
|
+
}
|
328
|
+
|
329
|
+
/*
|
330
|
+
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
|
331
|
+
* to work around bugs in some JS interpreters.
|
332
|
+
*/
|
333
|
+
function safe_add(x, y)
|
334
|
+
{
|
335
|
+
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
|
336
|
+
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
|
337
|
+
return (msw << 16) | (lsw & 0xFFFF);
|
338
|
+
}
|
339
|
+
|
340
|
+
/*
|
341
|
+
* Bitwise rotate a 32-bit number to the left.
|
342
|
+
*/
|
343
|
+
function bit_rol(num, cnt)
|
344
|
+
{
|
345
|
+
return (num << cnt) | (num >>> (32 - cnt));
|
346
|
+
}
|