File tree Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Original file line number Diff line number Diff line change 5
5
* Copyright (c) 2012 Niklas von Hertzen
6
6
* Licensed under the MIT license.
7
7
*/
8
- ( function ( chars ) {
8
+ ( function ( ) {
9
9
"use strict" ;
10
10
11
+ var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
12
+
13
+ // Use a lookup table to find the index.
14
+ var lookup = new Uint8Array ( 256 ) ;
15
+ for ( var i = 0 ; i < chars . length ; i ++ ) {
16
+ lookup [ chars . charCodeAt ( i ) ] = i ;
17
+ }
18
+
11
19
exports . encode = function ( arraybuffer ) {
12
20
var bytes = new Uint8Array ( arraybuffer ) ,
13
21
i , len = bytes . length , base64 = "" ;
44
52
bytes = new Uint8Array ( arraybuffer ) ;
45
53
46
54
for ( i = 0 ; i < len ; i += 4 ) {
47
- encoded1 = chars . indexOf ( base64 [ i ] ) ;
48
- encoded2 = chars . indexOf ( base64 [ i + 1 ] ) ;
49
- encoded3 = chars . indexOf ( base64 [ i + 2 ] ) ;
50
- encoded4 = chars . indexOf ( base64 [ i + 3 ] ) ;
55
+ encoded1 = lookup [ base64 . charCodeAt ( i ) ] ;
56
+ encoded2 = lookup [ base64 . charCodeAt ( i + 1 ) ] ;
57
+ encoded3 = lookup [ base64 . charCodeAt ( i + 2 ) ] ;
58
+ encoded4 = lookup [ base64 . charCodeAt ( i + 3 ) ] ;
51
59
52
60
bytes [ p ++ ] = ( encoded1 << 2 ) | ( encoded2 >> 4 ) ;
53
61
bytes [ p ++ ] = ( ( encoded2 & 15 ) << 4 ) | ( encoded3 >> 2 ) ;
56
64
57
65
return arraybuffer ;
58
66
} ;
59
- } ) ( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ) ;
67
+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments