[SignalingServer] Optimize dependent modules
[platform/framework/web/wrtjs.git] / device_home / node_modules / base64-js / index.js
1 'use strict'
2
3 exports.byteLength = byteLength
4 exports.toByteArray = toByteArray
5 exports.fromByteArray = fromByteArray
6
7 var lookup = []
8 var revLookup = []
9 var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
10
11 var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
12 for (var i = 0, len = code.length; i < len; ++i) {
13   lookup[i] = code[i]
14   revLookup[code.charCodeAt(i)] = i
15 }
16
17 // Support decoding URL-safe base64 strings, as Node.js does.
18 // See: https://en.wikipedia.org/wiki/Base64#URL_applications
19 revLookup['-'.charCodeAt(0)] = 62
20 revLookup['_'.charCodeAt(0)] = 63
21
22 function getLens (b64) {
23   var len = b64.length
24
25   if (len % 4 > 0) {
26     throw new Error('Invalid string. Length must be a multiple of 4')
27   }
28
29   // Trim off extra bytes after placeholder bytes are found
30   // See: https://github.com/beatgammit/base64-js/issues/42
31   var validLen = b64.indexOf('=')
32   if (validLen === -1) validLen = len
33
34   var placeHoldersLen = validLen === len
35     ? 0
36     : 4 - (validLen % 4)
37
38   return [validLen, placeHoldersLen]
39 }
40
41 // base64 is 4/3 + up to two characters of the original data
42 function byteLength (b64) {
43   var lens = getLens(b64)
44   var validLen = lens[0]
45   var placeHoldersLen = lens[1]
46   return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
47 }
48
49 function _byteLength (b64, validLen, placeHoldersLen) {
50   return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
51 }
52
53 function toByteArray (b64) {
54   var tmp
55   var lens = getLens(b64)
56   var validLen = lens[0]
57   var placeHoldersLen = lens[1]
58
59   var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen))
60
61   var curByte = 0
62
63   // if there are placeholders, only get up to the last complete 4 chars
64   var len = placeHoldersLen > 0
65     ? validLen - 4
66     : validLen
67
68   var i
69   for (i = 0; i < len; i += 4) {
70     tmp =
71       (revLookup[b64.charCodeAt(i)] << 18) |
72       (revLookup[b64.charCodeAt(i + 1)] << 12) |
73       (revLookup[b64.charCodeAt(i + 2)] << 6) |
74       revLookup[b64.charCodeAt(i + 3)]
75     arr[curByte++] = (tmp >> 16) & 0xFF
76     arr[curByte++] = (tmp >> 8) & 0xFF
77     arr[curByte++] = tmp & 0xFF
78   }
79
80   if (placeHoldersLen === 2) {
81     tmp =
82       (revLookup[b64.charCodeAt(i)] << 2) |
83       (revLookup[b64.charCodeAt(i + 1)] >> 4)
84     arr[curByte++] = tmp & 0xFF
85   }
86
87   if (placeHoldersLen === 1) {
88     tmp =
89       (revLookup[b64.charCodeAt(i)] << 10) |
90       (revLookup[b64.charCodeAt(i + 1)] << 4) |
91       (revLookup[b64.charCodeAt(i + 2)] >> 2)
92     arr[curByte++] = (tmp >> 8) & 0xFF
93     arr[curByte++] = tmp & 0xFF
94   }
95
96   return arr
97 }
98
99 function tripletToBase64 (num) {
100   return lookup[num >> 18 & 0x3F] +
101     lookup[num >> 12 & 0x3F] +
102     lookup[num >> 6 & 0x3F] +
103     lookup[num & 0x3F]
104 }
105
106 function encodeChunk (uint8, start, end) {
107   var tmp
108   var output = []
109   for (var i = start; i < end; i += 3) {
110     tmp =
111       ((uint8[i] << 16) & 0xFF0000) +
112       ((uint8[i + 1] << 8) & 0xFF00) +
113       (uint8[i + 2] & 0xFF)
114     output.push(tripletToBase64(tmp))
115   }
116   return output.join('')
117 }
118
119 function fromByteArray (uint8) {
120   var tmp
121   var len = uint8.length
122   var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
123   var parts = []
124   var maxChunkLength = 16383 // must be multiple of 3
125
126   // go through the array every three bytes, we'll deal with trailing stuff later
127   for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
128     parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
129   }
130
131   // pad the end with zeros, but make sure to not forget the extra bytes
132   if (extraBytes === 1) {
133     tmp = uint8[len - 1]
134     parts.push(
135       lookup[tmp >> 2] +
136       lookup[(tmp << 4) & 0x3F] +
137       '=='
138     )
139   } else if (extraBytes === 2) {
140     tmp = (uint8[len - 2] << 8) + uint8[len - 1]
141     parts.push(
142       lookup[tmp >> 10] +
143       lookup[(tmp >> 4) & 0x3F] +
144       lookup[(tmp << 2) & 0x3F] +
145       '='
146     )
147   }
148
149   return parts.join('')
150 }