These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | /* |
||
2 | * $Id: base64.js,v 1.7 2012/08/23 10:30:18 dankogai Exp dankogai $ |
||
3 | * |
||
4 | * Licensed under the MIT license. |
||
5 | * http://www.opensource.org/licenses/mit-license.php |
||
6 | * |
||
7 | * References: |
||
8 | * http://en.wikipedia.org/wiki/Base64 |
||
9 | */ |
||
10 | |||
11 | (function(global){ |
||
12 | |||
13 | var b64chars |
||
14 | = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
||
15 | |||
16 | var b64charcodes = function(){ |
||
17 | var a = []; |
||
18 | var codeA = 'A'.charCodeAt(0); |
||
19 | var codea = 'a'.charCodeAt(0); |
||
20 | var code0 = '0'.charCodeAt(0); |
||
21 | for (var i = 0; i < 26; i ++) a.push(codeA + i); |
||
0 ignored issues
–
show
|
|||
22 | for (var i = 0; i < 26; i ++) a.push(codea + i); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
Comprehensibility
Naming
Best Practice
introduced
by
The variable
i already seems to be declared on line 21 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.
Loading history...
|
|||
23 | for (var i = 0; i < 10; i ++) a.push(code0 + i); |
||
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
i already seems to be declared on line 21 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.
Loading history...
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
24 | a.push('+'.charCodeAt(0)); |
||
25 | a.push('/'.charCodeAt(0)); |
||
26 | return a; |
||
27 | }(); |
||
28 | |||
29 | var b64tab = function(bin){ |
||
30 | var t = {}; |
||
31 | for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
32 | return t; |
||
33 | }(b64chars); |
||
34 | |||
35 | var stringToArray = function(s){ |
||
36 | var a = []; |
||
37 | for (var i = 0, l = s.length; i < l; i ++) a[i] = s.charCodeAt(i); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
38 | return a; |
||
39 | }; |
||
40 | |||
41 | var convertUTF8ArrayToBase64 = function(bin){ |
||
42 | var padlen = 0; |
||
43 | while (bin.length % 3){ |
||
44 | bin.push(0); |
||
45 | padlen++; |
||
46 | }; |
||
47 | var b64 = []; |
||
48 | for (var i = 0, l = bin.length; i < l; i += 3){ |
||
49 | var c0 = bin[i], c1 = bin[i+1], c2 = bin[i+2]; |
||
50 | if (c0 >= 256 || c1 >= 256 || c2 >= 256) |
||
51 | throw 'unsupported character found'; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
52 | var n = (c0 << 16) | (c1 << 8) | c2; |
||
53 | b64.push( |
||
54 | b64charcodes[ n >>> 18], |
||
55 | b64charcodes[(n >>> 12) & 63], |
||
56 | b64charcodes[(n >>> 6) & 63], |
||
57 | b64charcodes[ n & 63] |
||
58 | ); |
||
59 | } |
||
60 | while (padlen--) b64[b64.length - padlen - 1] = '='.charCodeAt(0); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
61 | return chunkStringFromCharCodeApply(b64); |
||
62 | }; |
||
63 | |||
64 | var convertBase64ToUTF8Array = function(b64){ |
||
65 | b64 = b64.replace(/[^A-Za-z0-9+\/]+/g, ''); |
||
66 | var bin = []; |
||
67 | var padlen = b64.length % 4; |
||
68 | for (var i = 0, l = b64.length; i < l; i += 4){ |
||
69 | var n = ((b64tab[b64.charAt(i )] || 0) << 18) |
||
70 | | ((b64tab[b64.charAt(i+1)] || 0) << 12) |
||
71 | | ((b64tab[b64.charAt(i+2)] || 0) << 6) |
||
72 | | ((b64tab[b64.charAt(i+3)] || 0)); |
||
73 | bin.push( |
||
74 | ( n >> 16 ), |
||
75 | ( (n >> 8) & 0xff ), |
||
76 | ( n & 0xff ) |
||
77 | ); |
||
78 | } |
||
79 | bin.length -= [0,0,2,1][padlen]; |
||
80 | return bin; |
||
81 | }; |
||
82 | |||
83 | var convertUTF16ArrayToUTF8Array = function(uni){ |
||
0 ignored issues
–
show
|
|||
84 | var bin = []; |
||
85 | for (var i = 0, l = uni.length; i < l; i++){ |
||
86 | var n = uni[i]; |
||
87 | if (n < 0x80) |
||
88 | bin.push(n); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
89 | else if (n < 0x800) |
||
90 | bin.push( |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
91 | 0xc0 | (n >>> 6), |
||
92 | 0x80 | (n & 0x3f)); |
||
93 | else |
||
94 | bin.push( |
||
95 | 0xe0 | ((n >>> 12) & 0x0f), |
||
96 | 0x80 | ((n >>> 6) & 0x3f), |
||
97 | 0x80 | (n & 0x3f)); |
||
98 | } |
||
99 | return bin; |
||
100 | }; |
||
101 | |||
102 | var convertUTF8ArrayToUTF16Array = function(bin){ |
||
0 ignored issues
–
show
|
|||
103 | var uni = []; |
||
104 | for (var i = 0, l = bin.length; i < l; i++){ |
||
105 | var c0 = bin[i]; |
||
106 | if (c0 < 0x80){ |
||
107 | uni.push(c0); |
||
108 | }else{ |
||
109 | var c1 = bin[++i]; |
||
0 ignored issues
–
show
|
|||
110 | if (c0 < 0xe0){ |
||
111 | uni.push(((c0 & 0x1f) << 6) | (c1 & 0x3f)); |
||
112 | }else{ |
||
113 | var c2 = bin[++i]; |
||
114 | uni.push( |
||
115 | ((c0 & 0x0f) << 12) | ((c1 & 0x3f) << 6) | (c2 & 0x3f) |
||
116 | ); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | return uni; |
||
121 | }; |
||
122 | |||
123 | var convertUTF8StringToBase64 = function(bin){ |
||
124 | return convertUTF8ArrayToBase64(stringToArray(bin)); |
||
125 | }; |
||
126 | |||
127 | var convertBase64ToUTF8String = function(b64){ |
||
128 | return chunkStringFromCharCodeApply(convertBase64ToUTF8Array(b64)); |
||
129 | }; |
||
130 | |||
131 | var convertUTF8StringToUTF16Array = function(bin){ |
||
132 | return convertUTF8ArrayToUTF16Array(stringToArray(bin)); |
||
133 | }; |
||
134 | |||
135 | var convertUTF8ArrayToUTF16String = function(bin){ |
||
136 | return chunkStringFromCharCodeApply(convertUTF8ArrayToUTF16Array(bin)); |
||
137 | }; |
||
138 | |||
139 | var convertUTF8StringToUTF16String = function(bin){ |
||
140 | return chunkStringFromCharCodeApply( |
||
141 | convertUTF8ArrayToUTF16Array(stringToArray(bin)) |
||
142 | ); |
||
143 | }; |
||
144 | |||
145 | var convertUTF16StringToUTF8Array = function(uni){ |
||
146 | return convertUTF16ArrayToUTF8Array(stringToArray(uni)); |
||
147 | }; |
||
148 | |||
149 | var convertUTF16ArrayToUTF8String = function(uni){ |
||
150 | return chunkStringFromCharCodeApply(convertUTF16ArrayToUTF8Array(uni)); |
||
151 | }; |
||
152 | |||
153 | var convertUTF16StringToUTF8String = function(uni){ |
||
154 | return chunkStringFromCharCodeApply( |
||
155 | convertUTF16ArrayToUTF8Array(stringToArray(uni)) |
||
156 | ); |
||
157 | }; |
||
158 | |||
159 | /* |
||
160 | * String.fromCharCode.apply will only handle arrays as big as 65536, |
||
161 | * after that it'll return a truncated string with no warning. |
||
162 | */ |
||
163 | var chunkStringFromCharCodeApply = function(arr){ |
||
164 | var strs = [], i; |
||
165 | for (i = 0; i < arr.length; i += 65536){ |
||
166 | strs.push(String.fromCharCode.apply(String, arr.slice(i, i+65536))); |
||
167 | } |
||
168 | return strs.join(''); |
||
169 | }; |
||
170 | |||
171 | if (global.btoa){ |
||
172 | var btoa = global.btoa; |
||
173 | var convertUTF16StringToBase64 = function (uni){ |
||
174 | return btoa(convertUTF16StringToUTF8String(uni)); |
||
175 | }; |
||
176 | } |
||
177 | else { |
||
178 | var btoa = convertUTF8StringToBase64; |
||
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
btoa already seems to be declared on line 172 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.
Loading history...
|
|||
179 | var convertUTF16StringToBase64 = function (uni){ |
||
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
convertUTF16StringToBase64 already seems to be declared on line 173 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.
Loading history...
|
|||
180 | return convertUTF8ArrayToBase64(convertUTF16StringToUTF8Array(uni)); |
||
181 | }; |
||
182 | } |
||
183 | |||
184 | if (global.atob){ |
||
185 | var atob = global.atob; |
||
186 | var convertBase64ToUTF16String = function (b64){ |
||
187 | return convertUTF8StringToUTF16String(atob(b64)); |
||
188 | }; |
||
189 | } |
||
190 | else { |
||
191 | var atob = convertBase64ToUTF8String; |
||
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
atob already seems to be declared on line 185 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.
Loading history...
|
|||
192 | var convertBase64ToUTF16String = function (b64){ |
||
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
convertBase64ToUTF16String already seems to be declared on line 186 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.
Loading history...
|
|||
193 | return convertUTF8ArrayToUTF16String(convertBase64ToUTF8Array(b64)); |
||
194 | }; |
||
195 | } |
||
196 | |||
197 | global.Base64 = { |
||
198 | convertUTF8ArrayToBase64:convertUTF8ArrayToBase64, |
||
199 | convertByteArrayToBase64:convertUTF8ArrayToBase64, |
||
200 | convertBase64ToUTF8Array:convertBase64ToUTF8Array, |
||
201 | convertBase64ToByteArray:convertBase64ToUTF8Array, |
||
202 | convertUTF16ArrayToUTF8Array:convertUTF16ArrayToUTF8Array, |
||
203 | convertUTF16ArrayToByteArray:convertUTF16ArrayToUTF8Array, |
||
204 | convertUTF8ArrayToUTF16Array:convertUTF8ArrayToUTF16Array, |
||
205 | convertByteArrayToUTF16Array:convertUTF8ArrayToUTF16Array, |
||
206 | convertUTF8StringToBase64:convertUTF8StringToBase64, |
||
207 | convertBase64ToUTF8String:convertBase64ToUTF8String, |
||
208 | convertUTF8StringToUTF16Array:convertUTF8StringToUTF16Array, |
||
209 | convertUTF8ArrayToUTF16String:convertUTF8ArrayToUTF16String, |
||
210 | convertByteArrayToUTF16String:convertUTF8ArrayToUTF16String, |
||
211 | convertUTF8StringToUTF16String:convertUTF8StringToUTF16String, |
||
212 | convertUTF16StringToUTF8Array:convertUTF16StringToUTF8Array, |
||
213 | convertUTF16StringToByteArray:convertUTF16StringToUTF8Array, |
||
214 | convertUTF16ArrayToUTF8String:convertUTF16ArrayToUTF8String, |
||
215 | convertUTF16StringToUTF8String:convertUTF16StringToUTF8String, |
||
216 | convertUTF16StringToBase64:convertUTF16StringToBase64, |
||
217 | convertBase64ToUTF16String:convertBase64ToUTF16String, |
||
218 | fromBase64:convertBase64ToUTF8String, |
||
219 | toBase64:convertUTF8StringToBase64, |
||
220 | atob:atob, |
||
221 | btoa:btoa, |
||
222 | utob:convertUTF16StringToUTF8String, |
||
223 | btou:convertUTF8StringToUTF16String, |
||
224 | encode:convertUTF16StringToBase64, |
||
225 | encodeURI:function(u){ |
||
226 | return convertUTF16StringToBase64(u).replace(/[+\/]/g, function(m0){ |
||
227 | return m0 == '+' ? '-' : '_'; |
||
228 | }).replace(/=+$/, ''); |
||
229 | }, |
||
230 | decode:function(a){ |
||
231 | return convertBase64ToUTF16String(a.replace(/[-_]/g, function(m0){ |
||
232 | return m0 == '-' ? '+' : '/'; |
||
233 | })); |
||
234 | } |
||
235 | }; |
||
236 | |||
237 | })(this); |
||
238 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.