Revision 5600f0bd
b/ui/static/snf/js/lib/jquery.base64.js | ||
---|---|---|
1 |
/*jslint adsafe: false, bitwise: true, browser: true, cap: false, css: false, |
|
2 |
debug: false, devel: true, eqeqeq: true, es5: false, evil: false, |
|
3 |
forin: false, fragment: false, immed: true, laxbreak: false, newcap: true, |
|
4 |
nomen: false, on: false, onevar: true, passfail: false, plusplus: true, |
|
5 |
regexp: false, rhino: true, safe: false, strict: false, sub: false, |
|
6 |
undef: true, white: false, widget: false, windows: false */ |
|
7 |
/*global jQuery: false, window: false */ |
|
8 |
"use strict"; |
|
9 |
|
|
10 |
/* |
|
11 |
* Original code (c) 2010 Nick Galbreath |
|
12 |
* http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript |
|
13 |
* |
|
14 |
* jQuery port (c) 2010 Carlo Zottmann |
|
15 |
* http://github.com/carlo/jquery-base64 |
|
16 |
* |
|
17 |
* Permission is hereby granted, free of charge, to any person |
|
18 |
* obtaining a copy of this software and associated documentation |
|
19 |
* files (the "Software"), to deal in the Software without |
|
20 |
* restriction, including without limitation the rights to use, |
|
21 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
22 |
* copies of the Software, and to permit persons to whom the |
|
23 |
* Software is furnished to do so, subject to the following |
|
24 |
* conditions: |
|
25 |
* |
|
26 |
* The above copyright notice and this permission notice shall be |
|
27 |
* included in all copies or substantial portions of the Software. |
|
28 |
* |
|
29 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
30 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
31 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
32 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
33 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
34 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
35 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
36 |
* OTHER DEALINGS IN THE SOFTWARE. |
|
37 |
*/ |
|
38 |
|
|
39 |
/* base64 encode/decode compatible with window.btoa/atob |
|
40 |
* |
|
41 |
* window.atob/btoa is a Firefox extension to convert binary data (the "b") |
|
42 |
* to base64 (ascii, the "a"). |
|
43 |
* |
|
44 |
* It is also found in Safari and Chrome. It is not available in IE. |
|
45 |
* |
|
46 |
* if (!window.btoa) window.btoa = $.base64.encode |
|
47 |
* if (!window.atob) window.atob = $.base64.decode |
|
48 |
* |
|
49 |
* The original spec's for atob/btoa are a bit lacking |
|
50 |
* https://developer.mozilla.org/en/DOM/window.atob |
|
51 |
* https://developer.mozilla.org/en/DOM/window.btoa |
|
52 |
* |
|
53 |
* window.btoa and $.base64.encode takes a string where charCodeAt is [0,255] |
|
54 |
* If any character is not [0,255], then an exception is thrown. |
|
55 |
* |
|
56 |
* window.atob and $.base64.decode take a base64-encoded string |
|
57 |
* If the input length is not a multiple of 4, or contains invalid characters |
|
58 |
* then an exception is thrown. |
|
59 |
*/ |
|
60 |
|
|
61 |
jQuery.base64 = ( function( $ ) { |
|
62 |
|
|
63 |
var _PADCHAR = "=", |
|
64 |
_ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", |
|
65 |
_VERSION = "1.0"; |
|
66 |
|
|
67 |
|
|
68 |
function _getbyte64( s, i ) { |
|
69 |
// This is oddly fast, except on Chrome/V8. |
|
70 |
// Minimal or no improvement in performance by using a |
|
71 |
// object with properties mapping chars to value (eg. 'A': 0) |
|
72 |
|
|
73 |
var idx = _ALPHA.indexOf( s.charAt( i ) ); |
|
74 |
|
|
75 |
if ( idx === -1 ) { |
|
76 |
throw "Cannot decode base64"; |
|
77 |
} |
|
78 |
|
|
79 |
return idx; |
|
80 |
} |
|
81 |
|
|
82 |
|
|
83 |
function _decode( s ) { |
|
84 |
var pads = 0, |
|
85 |
i, |
|
86 |
b10, |
|
87 |
imax = s.length, |
|
88 |
x = []; |
|
89 |
|
|
90 |
s = String( s ); |
|
91 |
|
|
92 |
if ( imax === 0 ) { |
|
93 |
return s; |
|
94 |
} |
|
95 |
|
|
96 |
if ( imax % 4 !== 0 ) { |
|
97 |
throw "Cannot decode base64"; |
|
98 |
} |
|
99 |
|
|
100 |
if ( s.charAt( imax - 1 ) === _PADCHAR ) { |
|
101 |
pads = 1; |
|
102 |
|
|
103 |
if ( s.charAt( imax - 2 ) === _PADCHAR ) { |
|
104 |
pads = 2; |
|
105 |
} |
|
106 |
|
|
107 |
// either way, we want to ignore this last block |
|
108 |
imax -= 4; |
|
109 |
} |
|
110 |
|
|
111 |
for ( i = 0; i < imax; i += 4 ) { |
|
112 |
b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 ) | _getbyte64( s, i + 3 ); |
|
113 |
x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff, b10 & 0xff ) ); |
|
114 |
} |
|
115 |
|
|
116 |
switch ( pads ) { |
|
117 |
case 1: |
|
118 |
b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 ); |
|
119 |
x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff ) ); |
|
120 |
break; |
|
121 |
|
|
122 |
case 2: |
|
123 |
b10 = ( _getbyte64( s, i ) << 18) | ( _getbyte64( s, i + 1 ) << 12 ); |
|
124 |
x.push( String.fromCharCode( b10 >> 16 ) ); |
|
125 |
break; |
|
126 |
} |
|
127 |
|
|
128 |
return x.join( "" ); |
|
129 |
} |
|
130 |
|
|
131 |
|
|
132 |
function _getbyte( s, i ) { |
|
133 |
var x = s.charCodeAt( i ); |
|
134 |
|
|
135 |
if ( x > 255 ) { |
|
136 |
throw "INVALID_CHARACTER_ERR: DOM Exception 5"; |
|
137 |
} |
|
138 |
|
|
139 |
return x; |
|
140 |
} |
|
141 |
|
|
142 |
|
|
143 |
function _encode( s ) { |
|
144 |
if ( arguments.length !== 1 ) { |
|
145 |
throw "SyntaxError: exactly one argument required"; |
|
146 |
} |
|
147 |
|
|
148 |
s = String( s ); |
|
149 |
|
|
150 |
var i, |
|
151 |
b10, |
|
152 |
x = [], |
|
153 |
imax = s.length - s.length % 3; |
|
154 |
|
|
155 |
if ( s.length === 0 ) { |
|
156 |
return s; |
|
157 |
} |
|
158 |
|
|
159 |
for ( i = 0; i < imax; i += 3 ) { |
|
160 |
b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 ) | _getbyte( s, i + 2 ); |
|
161 |
x.push( _ALPHA.charAt( b10 >> 18 ) ); |
|
162 |
x.push( _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) ); |
|
163 |
x.push( _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) ); |
|
164 |
x.push( _ALPHA.charAt( b10 & 0x3f ) ); |
|
165 |
} |
|
166 |
|
|
167 |
switch ( s.length - imax ) { |
|
168 |
case 1: |
|
169 |
b10 = _getbyte( s, i ) << 16; |
|
170 |
x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _PADCHAR + _PADCHAR ); |
|
171 |
break; |
|
172 |
|
|
173 |
case 2: |
|
174 |
b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 ); |
|
175 |
x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) + _PADCHAR ); |
|
176 |
break; |
|
177 |
} |
|
178 |
|
|
179 |
return x.join( "" ); |
|
180 |
} |
|
181 |
|
|
182 |
|
|
183 |
return { |
|
184 |
decode: _decode, |
|
185 |
encode: _encode, |
|
186 |
VERSION: _VERSION |
|
187 |
}; |
|
188 |
|
|
189 |
}( jQuery ) ); |
|
190 |
|
b/ui/static/snf/js/lib/jspack.js | ||
---|---|---|
1 |
/*! |
|
2 |
* Copyright ? 2008 Fair Oaks Labs, Inc. |
|
3 |
* All rights reserved. |
|
4 |
*/ |
|
5 |
|
|
6 |
// Utility object: Encode/Decode C-style binary primitives to/from octet arrays |
|
7 |
function JSPack() |
|
8 |
{ |
|
9 |
// Module-level (private) variables |
|
10 |
var el, bBE = false, m = this; |
|
11 |
|
|
12 |
|
|
13 |
// Raw byte arrays |
|
14 |
m._DeArray = function (a, p, l) |
|
15 |
{ |
|
16 |
return [a.slice(p,p+l)]; |
|
17 |
}; |
|
18 |
m._EnArray = function (a, p, l, v) |
|
19 |
{ |
|
20 |
for (var i = 0; i < l; a[p+i] = v[i]?v[i]:0, i++); |
|
21 |
}; |
|
22 |
|
|
23 |
// ASCII characters |
|
24 |
m._DeChar = function (a, p) |
|
25 |
{ |
|
26 |
return String.fromCharCode(a[p]); |
|
27 |
}; |
|
28 |
m._EnChar = function (a, p, v) |
|
29 |
{ |
|
30 |
a[p] = v.charCodeAt(0); |
|
31 |
}; |
|
32 |
|
|
33 |
// Little-endian (un)signed N-byte integers |
|
34 |
m._DeInt = function (a, p) |
|
35 |
{ |
|
36 |
var lsb = bBE?(el.len-1):0, nsb = bBE?-1:1, stop = lsb+nsb*el.len, rv, i, f; |
|
37 |
for (rv = 0, i = lsb, f = 1; i != stop; rv+=(a[p+i]*f), i+=nsb, f*=256); |
|
38 |
if (el.bSigned && (rv & Math.pow(2, el.len*8-1))) { rv -= Math.pow(2, el.len*8); } |
|
39 |
return rv; |
|
40 |
}; |
|
41 |
m._EnInt = function (a, p, v) |
|
42 |
{ |
|
43 |
var lsb = bBE?(el.len-1):0, nsb = bBE?-1:1, stop = lsb+nsb*el.len, i; |
|
44 |
v = (v<el.min)?el.min:(v>el.max)?el.max:v; |
|
45 |
for (i = lsb; i != stop; a[p+i]=v&0xff, i+=nsb, v>>=8); |
|
46 |
}; |
|
47 |
|
|
48 |
// ASCII character strings |
|
49 |
m._DeString = function (a, p, l) |
|
50 |
{ |
|
51 |
for (var rv = new Array(l), i = 0; i < l; rv[i] = String.fromCharCode(a[p+i]), i++); |
|
52 |
return rv.join(''); |
|
53 |
}; |
|
54 |
m._EnString = function (a, p, l, v) |
|
55 |
{ |
|
56 |
for (var t, i = 0; i < l; a[p+i] = (t=v.charCodeAt(i))?t:0, i++); |
|
57 |
}; |
|
58 |
|
|
59 |
// Little-endian N-bit IEEE 754 floating point |
|
60 |
m._De754 = function (a, p) |
|
61 |
{ |
|
62 |
var s, e, m, i, d, nBits, mLen, eLen, eBias, eMax; |
|
63 |
mLen = el.mLen, eLen = el.len*8-el.mLen-1, eMax = (1<<eLen)-1, eBias = eMax>>1; |
|
64 |
|
|
65 |
i = bBE?0:(el.len-1); d = bBE?1:-1; s = a[p+i]; i+=d; nBits = -7; |
|
66 |
for (e = s&((1<<(-nBits))-1), s>>=(-nBits), nBits += eLen; nBits > 0; e=e*256+a[p+i], i+=d, nBits-=8); |
|
67 |
for (m = e&((1<<(-nBits))-1), e>>=(-nBits), nBits += mLen; nBits > 0; m=m*256+a[p+i], i+=d, nBits-=8); |
|
68 |
|
|
69 |
switch (e) |
|
70 |
{ |
|
71 |
case 0: |
|
72 |
// Zero, or denormalized number |
|
73 |
e = 1-eBias; |
|
74 |
break; |
|
75 |
case eMax: |
|
76 |
// NaN, or +/-Infinity |
|
77 |
return m?NaN:((s?-1:1)*Infinity); |
|
78 |
default: |
|
79 |
// Normalized number |
|
80 |
m = m + Math.pow(2, mLen); |
|
81 |
e = e - eBias; |
|
82 |
break; |
|
83 |
} |
|
84 |
return (s?-1:1) * m * Math.pow(2, e-mLen); |
|
85 |
}; |
|
86 |
m._En754 = function (a, p, v) |
|
87 |
{ |
|
88 |
var s, e, m, i, d, c, mLen, eLen, eBias, eMax; |
|
89 |
mLen = el.mLen, eLen = el.len*8-el.mLen-1, eMax = (1<<eLen)-1, eBias = eMax>>1; |
|
90 |
|
|
91 |
s = v<0?1:0; |
|
92 |
v = Math.abs(v); |
|
93 |
if (isNaN(v) || (v == Infinity)) |
|
94 |
{ |
|
95 |
m = isNaN(v)?1:0; |
|
96 |
e = eMax; |
|
97 |
} |
|
98 |
else |
|
99 |
{ |
|
100 |
e = Math.floor(Math.log(v)/Math.LN2); // Calculate log2 of the value |
|
101 |
if (v*(c = Math.pow(2, -e)) < 1) { e--; c*=2; } // Math.log() isn't 100% reliable |
|
102 |
|
|
103 |
// Round by adding 1/2 the significand's LSD |
|
104 |
if (e+eBias >= 1) { v += el.rt/c; } // Normalized: mLen significand digits |
|
105 |
else { v += el.rt*Math.pow(2, 1-eBias); } // Denormalized: <= mLen significand digits |
|
106 |
if (v*c >= 2) { e++; c/=2; } // Rounding can increment the exponent |
|
107 |
|
|
108 |
if (e+eBias >= eMax) |
|
109 |
{ |
|
110 |
// Overflow |
|
111 |
m = 0; |
|
112 |
e = eMax; |
|
113 |
} |
|
114 |
else if (e+eBias >= 1) |
|
115 |
{ |
|
116 |
// Normalized - term order matters, as Math.pow(2, 52-e) and v*Math.pow(2, 52) can overflow |
|
117 |
m = (v*c-1)*Math.pow(2, mLen); |
|
118 |
e = e + eBias; |
|
119 |
} |
|
120 |
else |
|
121 |
{ |
|
122 |
// Denormalized - also catches the '0' case, somewhat by chance |
|
123 |
m = v*Math.pow(2, eBias-1)*Math.pow(2, mLen); |
|
124 |
e = 0; |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
for (i = bBE?(el.len-1):0, d=bBE?-1:1; mLen >= 8; a[p+i]=m&0xff, i+=d, m/=256, mLen-=8); |
|
129 |
for (e=(e<<mLen)|m, eLen+=mLen; eLen > 0; a[p+i]=e&0xff, i+=d, e/=256, eLen-=8); |
|
130 |
a[p+i-d] |= s*128; |
|
131 |
}; |
|
132 |
|
|
133 |
|
|
134 |
// Class data |
|
135 |
m._sPattern = '(\\d+)?([AxcbBhHsfdiIlL])'; |
|
136 |
m._lenLut = {'A':1, 'x':1, 'c':1, 'b':1, 'B':1, 'h':2, 'H':2, 's':1, 'f':4, 'd':8, 'i':4, 'I':4, 'l':4, 'L':4}; |
|
137 |
m._elLut = { 'A': {en:m._EnArray, de:m._DeArray}, |
|
138 |
's': {en:m._EnString, de:m._DeString}, |
|
139 |
'c': {en:m._EnChar, de:m._DeChar}, |
|
140 |
'b': {en:m._EnInt, de:m._DeInt, len:1, bSigned:true, min:-Math.pow(2, 7), max:Math.pow(2, 7)-1}, |
|
141 |
'B': {en:m._EnInt, de:m._DeInt, len:1, bSigned:false, min:0, max:Math.pow(2, 8)-1}, |
|
142 |
'h': {en:m._EnInt, de:m._DeInt, len:2, bSigned:true, min:-Math.pow(2, 15), max:Math.pow(2, 15)-1}, |
|
143 |
'H': {en:m._EnInt, de:m._DeInt, len:2, bSigned:false, min:0, max:Math.pow(2, 16)-1}, |
|
144 |
'i': {en:m._EnInt, de:m._DeInt, len:4, bSigned:true, min:-Math.pow(2, 31), max:Math.pow(2, 31)-1}, |
|
145 |
'I': {en:m._EnInt, de:m._DeInt, len:4, bSigned:false, min:0, max:Math.pow(2, 32)-1}, |
|
146 |
'l': {en:m._EnInt, de:m._DeInt, len:4, bSigned:true, min:-Math.pow(2, 31), max:Math.pow(2, 31)-1}, |
|
147 |
'L': {en:m._EnInt, de:m._DeInt, len:4, bSigned:false, min:0, max:Math.pow(2, 32)-1}, |
|
148 |
'f': {en:m._En754, de:m._De754, len:4, mLen:23, rt:Math.pow(2, -24)-Math.pow(2, -77)}, |
|
149 |
'd': {en:m._En754, de:m._De754, len:8, mLen:52, rt:0}}; |
|
150 |
|
|
151 |
// Unpack a series of n elements of size s from array a at offset p with fxn |
|
152 |
m._UnpackSeries = function (n, s, a, p) |
|
153 |
{ |
|
154 |
for (var fxn = el.de, rv = [], i = 0; i < n; rv.push(fxn(a, p+i*s)), i++); |
|
155 |
return rv; |
|
156 |
}; |
|
157 |
|
|
158 |
// Pack a series of n elements of size s from array v at offset i to array a at offset p with fxn |
|
159 |
m._PackSeries = function (n, s, a, p, v, i) |
|
160 |
{ |
|
161 |
for (var fxn = el.en, o = 0; o < n; fxn(a, p+o*s, v[i+o]), o++); |
|
162 |
}; |
|
163 |
|
|
164 |
// Unpack the octet array a, beginning at offset p, according to the fmt string |
|
165 |
m.Unpack = function (fmt, a, p) |
|
166 |
{ |
|
167 |
// Set the private bBE flag based on the format string - assume big-endianness |
|
168 |
bBE = (fmt.charAt(0) != '<'); |
|
169 |
|
|
170 |
p = p?p:0; |
|
171 |
var re = new RegExp(this._sPattern, 'g'), m, n, s, rv = []; |
|
172 |
while (m = re.exec(fmt)) |
|
173 |
{ |
|
174 |
n = ((m[1]==undefined)||(m[1]==''))?1:parseInt(m[1]); |
|
175 |
s = this._lenLut[m[2]]; |
|
176 |
if ((p + n*s) > a.length) |
|
177 |
{ |
|
178 |
return undefined; |
|
179 |
} |
|
180 |
switch (m[2]) |
|
181 |
{ |
|
182 |
case 'A': case 's': |
|
183 |
rv.push(this._elLut[m[2]].de(a, p, n)); |
|
184 |
break; |
|
185 |
case 'c': case 'b': case 'B': case 'h': case 'H': |
|
186 |
case 'i': case 'I': case 'l': case 'L': case 'f': case 'd': |
|
187 |
el = this._elLut[m[2]]; |
|
188 |
rv.push(this._UnpackSeries(n, s, a, p)); |
|
189 |
break; |
|
190 |
} |
|
191 |
p += n*s; |
|
192 |
} |
|
193 |
return Array.prototype.concat.apply([], rv); |
|
194 |
}; |
|
195 |
|
|
196 |
// Pack the supplied values into the octet array a, beginning at offset p, according to the fmt string |
|
197 |
m.PackTo = function (fmt, a, p, values) |
|
198 |
{ |
|
199 |
// Set the private bBE flag based on the format string - assume big-endianness |
|
200 |
bBE = (fmt.charAt(0) != '<'); |
|
201 |
|
|
202 |
var re = new RegExp(this._sPattern, 'g'), m, n, s, i = 0, j; |
|
203 |
while (m = re.exec(fmt)) |
|
204 |
{ |
|
205 |
n = ((m[1]==undefined)||(m[1]==''))?1:parseInt(m[1]); |
|
206 |
s = this._lenLut[m[2]]; |
|
207 |
if ((p + n*s) > a.length) |
|
208 |
{ |
|
209 |
return false; |
|
210 |
} |
|
211 |
switch (m[2]) |
|
212 |
{ |
|
213 |
case 'A': case 's': |
|
214 |
if ((i + 1) > values.length) { return false; } |
|
215 |
this._elLut[m[2]].en(a, p, n, values[i]); |
|
216 |
i += 1; |
|
217 |
break; |
|
218 |
case 'c': case 'b': case 'B': case 'h': case 'H': |
|
219 |
case 'i': case 'I': case 'l': case 'L': case 'f': case 'd': |
|
220 |
el = this._elLut[m[2]]; |
|
221 |
if ((i + n) > values.length) { return false; } |
|
222 |
this._PackSeries(n, s, a, p, values, i); |
|
223 |
i += n; |
|
224 |
break; |
|
225 |
case 'x': |
|
226 |
for (j = 0; j < n; j++) { a[p+j] = 0; } |
|
227 |
break; |
|
228 |
} |
|
229 |
p += n*s; |
|
230 |
} |
|
231 |
return a; |
|
232 |
}; |
|
233 |
|
|
234 |
// Pack the supplied values into a new octet array, according to the fmt string |
|
235 |
m.Pack = function (fmt, values) |
|
236 |
{ |
|
237 |
return this.PackTo(fmt, new Array(this.CalcLength(fmt)), 0, values); |
|
238 |
}; |
|
239 |
|
|
240 |
// Determine the number of bytes represented by the format string |
|
241 |
m.CalcLength = function (fmt) |
|
242 |
{ |
|
243 |
var re = new RegExp(this._sPattern, 'g'), m, sum = 0; |
|
244 |
while (m = re.exec(fmt)) |
|
245 |
{ |
|
246 |
sum += (((m[1]==undefined)||(m[1]==''))?1:parseInt(m[1])) * this._lenLut[m[2]]; |
|
247 |
} |
|
248 |
return sum; |
|
249 |
}; |
|
250 |
}; |
|
251 |
|
Also available in: Unified diff