Skip to content
Permalink
Newer
Older
100644 94 lines (78 sloc) 3.2 KB
October 24, 2011 11:33
1
/*
2
Copyright (c) 2011, Daniel Guerrero
3
All rights reserved.
4
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright
8
notice, this list of conditions and the following disclaimer.
9
* Redistributions in binary form must reproduce the above copyright
10
notice, this list of conditions and the following disclaimer in the
11
documentation and/or other materials provided with the distribution.
12
13
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
DISCLAIMED. IN NO EVENT SHALL DANIEL GUERRERO BE LIABLE FOR ANY
17
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
*/
24
25
/**
26
* Uses the new array typed in javascript to binary base64 encode/decode
27
* at the moment just decodes a binary base64 encoded
28
* into either an ArrayBuffer (decodeArrayBuffer)
29
* or into an Uint8Array (decode)
30
*
31
* References:
32
* https://developer.mozilla.org/en/JavaScript_typed_arrays/ArrayBuffer
33
* https://developer.mozilla.org/en/JavaScript_typed_arrays/Uint8Array
34
*/
35
36
var Base64Binary = {
37
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
38
39
/* will return a Uint8Array type */
40
decodeArrayBuffer: function(input) {
June 5, 2015 16:20
41
var bytes = (input.length/4) * 3;
October 24, 2011 11:33
42
var ab = new ArrayBuffer(bytes);
43
this.decode(input, ab);
44
45
return ab;
46
},
June 5, 2015 16:20
47
48
removePaddingChars: function(input){
49
var lkey = this._keyStr.indexOf(input.charAt(input.length - 1));
50
if(lkey == 64){
51
return input.substring(0,input.length - 1);
52
}
53
return input;
54
},
55
56
decode: function (input, arrayBuffer) {
October 24, 2011 11:33
57
//get last chars to see if are valid
June 5, 2015 16:20
58
input = this.removePaddingChars(input);
59
input = this.removePaddingChars(input);
60
61
var bytes = parseInt((input.length / 4) * 3, 10);
October 24, 2011 11:33
62
63
var uarray;
64
var chr1, chr2, chr3;
65
var enc1, enc2, enc3, enc4;
66
var i = 0;
67
var j = 0;
68
69
if (arrayBuffer)
70
uarray = new Uint8Array(arrayBuffer);
71
else
72
uarray = new Uint8Array(bytes);
73
74
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
75
76
for (i=0; i<bytes; i+=3) {
77
//get the 3 octects in 4 ascii chars
78
enc1 = this._keyStr.indexOf(input.charAt(j++));
79
enc2 = this._keyStr.indexOf(input.charAt(j++));
80
enc3 = this._keyStr.indexOf(input.charAt(j++));
81
enc4 = this._keyStr.indexOf(input.charAt(j++));
82
83
chr1 = (enc1 << 2) | (enc2 >> 4);
84
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
85
chr3 = ((enc3 & 3) << 6) | enc4;
86
87
uarray[i] = chr1;
88
if (enc3 != 64) uarray[i+1] = chr2;
89
if (enc4 != 64) uarray[i+2] = chr3;
90
}
91
92
return uarray;
93
}
June 5, 2015 16:20
94
}