c@117
|
1 'use strict'
|
c@117
|
2
|
c@117
|
3 if (typeof document === "undefined") {
|
c@117
|
4 exports.byteLength = byteLength
|
c@117
|
5 exports.toByteArray = toByteArray
|
c@117
|
6 exports.fromByteArray = fromByteArray
|
c@117
|
7 }
|
c@117
|
8
|
c@117
|
9 var lookup = []
|
c@117
|
10 var revLookup = []
|
c@117
|
11 var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
|
c@117
|
12
|
c@117
|
13 var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
c@117
|
14 for (var i = 0, len = code.length; i < len; ++i) {
|
c@117
|
15 lookup[i] = code[i]
|
c@117
|
16 revLookup[code.charCodeAt(i)] = i
|
c@117
|
17 }
|
c@117
|
18
|
c@117
|
19 revLookup['-'.charCodeAt(0)] = 62
|
c@117
|
20 revLookup['_'.charCodeAt(0)] = 63
|
c@117
|
21
|
c@117
|
22 function placeHoldersCount (b64) {
|
c@117
|
23 var len = b64.length
|
c@117
|
24 if (len % 4 > 0) {
|
c@117
|
25 throw new Error('Invalid string. Length must be a multiple of 4')
|
c@117
|
26 }
|
c@117
|
27
|
c@117
|
28 // the number of equal signs (place holders)
|
c@117
|
29 // if there are two placeholders, than the two characters before it
|
c@117
|
30 // represent one byte
|
c@117
|
31 // if there is only one, then the three characters before it represent 2 bytes
|
c@117
|
32 // this is just a cheap hack to not do indexOf twice
|
c@117
|
33 return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0
|
c@117
|
34 }
|
c@117
|
35
|
c@117
|
36 function byteLength (b64) {
|
c@117
|
37 // base64 is 4/3 + up to two characters of the original data
|
c@117
|
38 return b64.length * 3 / 4 - placeHoldersCount(b64)
|
c@117
|
39 }
|
c@117
|
40
|
c@117
|
41 function toByteArray (b64) {
|
c@117
|
42 var i, j, l, tmp, placeHolders, arr
|
c@117
|
43 var len = b64.length
|
c@117
|
44 placeHolders = placeHoldersCount(b64)
|
c@117
|
45
|
c@117
|
46 arr = new Arr(len * 3 / 4 - placeHolders)
|
c@117
|
47
|
c@117
|
48 // if there are placeholders, only get up to the last complete 4 chars
|
c@117
|
49 l = placeHolders > 0 ? len - 4 : len
|
c@117
|
50
|
c@117
|
51 var L = 0
|
c@117
|
52
|
c@117
|
53 for (i = 0, j = 0; i < l; i += 4, j += 3) {
|
c@117
|
54 tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]
|
c@117
|
55 arr[L++] = (tmp >> 16) & 0xFF
|
c@117
|
56 arr[L++] = (tmp >> 8) & 0xFF
|
c@117
|
57 arr[L++] = tmp & 0xFF
|
c@117
|
58 }
|
c@117
|
59
|
c@117
|
60 if (placeHolders === 2) {
|
c@117
|
61 tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)
|
c@117
|
62 arr[L++] = tmp & 0xFF
|
c@117
|
63 } else if (placeHolders === 1) {
|
c@117
|
64 tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2)
|
c@117
|
65 arr[L++] = (tmp >> 8) & 0xFF
|
c@117
|
66 arr[L++] = tmp & 0xFF
|
c@117
|
67 }
|
c@117
|
68
|
c@117
|
69 return arr
|
c@117
|
70 }
|
c@117
|
71
|
c@117
|
72 function tripletToBase64 (num) {
|
c@117
|
73 return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
|
c@117
|
74 }
|
c@117
|
75
|
c@117
|
76 function encodeChunk (uint8, start, end) {
|
c@117
|
77 var tmp
|
c@117
|
78 var output = []
|
c@117
|
79 for (var i = start; i < end; i += 3) {
|
c@117
|
80 tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
|
c@117
|
81 output.push(tripletToBase64(tmp))
|
c@117
|
82 }
|
c@117
|
83 return output.join('')
|
c@117
|
84 }
|
c@117
|
85
|
c@117
|
86 function fromByteArray (uint8) {
|
c@117
|
87 var tmp
|
c@117
|
88 var len = uint8.length
|
c@117
|
89 var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
|
c@117
|
90 var output = ''
|
c@117
|
91 var parts = []
|
c@117
|
92 var maxChunkLength = 16383 // must be multiple of 3
|
c@117
|
93
|
c@117
|
94 // go through the array every three bytes, we'll deal with trailing stuff later
|
c@117
|
95 for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
|
c@117
|
96 parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
|
c@117
|
97 }
|
c@117
|
98
|
c@117
|
99 // pad the end with zeros, but make sure to not forget the extra bytes
|
c@117
|
100 if (extraBytes === 1) {
|
c@117
|
101 tmp = uint8[len - 1]
|
c@117
|
102 output += lookup[tmp >> 2]
|
c@117
|
103 output += lookup[(tmp << 4) & 0x3F]
|
c@117
|
104 output += '=='
|
c@117
|
105 } else if (extraBytes === 2) {
|
c@117
|
106 tmp = (uint8[len - 2] << 8) + (uint8[len - 1])
|
c@117
|
107 output += lookup[tmp >> 10]
|
c@117
|
108 output += lookup[(tmp >> 4) & 0x3F]
|
c@117
|
109 output += lookup[(tmp << 2) & 0x3F]
|
c@117
|
110 output += '='
|
c@117
|
111 }
|
c@117
|
112
|
c@117
|
113 parts.push(output)
|
c@117
|
114
|
c@117
|
115 return parts.join('')
|
c@117
|
116 }
|