1/* eslint-disable @typescript-eslint/strict-boolean-expressions */ 2 3// --------------------------------------------------------------------------- 4// Specific encodings 5// --------------------------------------------------------------------------- 6var base16Encoding = { 7 chars: '0123456789ABCDEF', 8 bits: 4 9}; 10var base32Encoding = { 11 chars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', 12 bits: 5 13}; 14var base32HexEncoding = { 15 chars: '0123456789ABCDEFGHIJKLMNOPQRSTUV', 16 bits: 5 17}; 18var base64Encoding = { 19 chars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', 20 bits: 6 21}; 22var base64UrlEncoding = { 23 chars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', 24 bits: 6 25}; 26var base16 = { 27 parse: function parse(string, opts) { 28 return _parse(string.toUpperCase(), base16Encoding, opts); 29 }, 30 stringify: function stringify(data, opts) { 31 return _stringify(data, base16Encoding, opts); 32 } 33}; 34var base32 = { 35 parse: function parse(string, opts) { 36 if (opts === void 0) { 37 opts = {}; 38 } 39 return _parse(opts.loose ? string.toUpperCase().replace(/0/g, 'O').replace(/1/g, 'L').replace(/8/g, 'B') : string, base32Encoding, opts); 40 }, 41 stringify: function stringify(data, opts) { 42 return _stringify(data, base32Encoding, opts); 43 } 44}; 45var base32hex = { 46 parse: function parse(string, opts) { 47 return _parse(string, base32HexEncoding, opts); 48 }, 49 stringify: function stringify(data, opts) { 50 return _stringify(data, base32HexEncoding, opts); 51 } 52}; 53var base64 = { 54 parse: function parse(string, opts) { 55 return _parse(string, base64Encoding, opts); 56 }, 57 stringify: function stringify(data, opts) { 58 return _stringify(data, base64Encoding, opts); 59 } 60}; 61var base64url = { 62 parse: function parse(string, opts) { 63 return _parse(string, base64UrlEncoding, opts); 64 }, 65 stringify: function stringify(data, opts) { 66 return _stringify(data, base64UrlEncoding, opts); 67 } 68}; 69 70// --------------------------------------------------------------------------- 71// Codec implementation 72// --------------------------------------------------------------------------- 73 74var codec = { 75 parse: _parse, 76 stringify: _stringify 77}; 78function _parse(string, encoding, opts) { 79 var _opts$out; 80 if (opts === void 0) { 81 opts = {}; 82 } 83 // Build the character lookup table: 84 if (!encoding.codes) { 85 encoding.codes = {}; 86 for (var i = 0; i < encoding.chars.length; ++i) { 87 encoding.codes[encoding.chars[i]] = i; 88 } 89 } 90 91 // The string must have a whole number of bytes: 92 if (!opts.loose && string.length * encoding.bits & 7) { 93 throw new SyntaxError('Invalid padding'); 94 } 95 96 // Count the padding bytes: 97 var end = string.length; 98 while (string[end - 1] === '=') { 99 --end; 100 101 // If we get a whole number of bytes, there is too much padding: 102 if (!opts.loose && !((string.length - end) * encoding.bits & 7)) { 103 throw new SyntaxError('Invalid padding'); 104 } 105 } 106 107 // Allocate the output: 108 var out = new ((_opts$out = opts.out) != null ? _opts$out : Uint8Array)(end * encoding.bits / 8 | 0); 109 110 // Parse the data: 111 var bits = 0; // Number of bits currently in the buffer 112 var buffer = 0; // Bits waiting to be written out, MSB first 113 var written = 0; // Next byte to write 114 for (var _i = 0; _i < end; ++_i) { 115 // Read one character from the string: 116 var value = encoding.codes[string[_i]]; 117 if (value === undefined) { 118 throw new SyntaxError('Invalid character ' + string[_i]); 119 } 120 121 // Append the bits to the buffer: 122 buffer = buffer << encoding.bits | value; 123 bits += encoding.bits; 124 125 // Write out some bits if the buffer has a byte's worth: 126 if (bits >= 8) { 127 bits -= 8; 128 out[written++] = 0xff & buffer >> bits; 129 } 130 } 131 132 // Verify that we have received just enough bits: 133 if (bits >= encoding.bits || 0xff & buffer << 8 - bits) { 134 throw new SyntaxError('Unexpected end of data'); 135 } 136 return out; 137} 138function _stringify(data, encoding, opts) { 139 if (opts === void 0) { 140 opts = {}; 141 } 142 var _opts = opts, 143 _opts$pad = _opts.pad, 144 pad = _opts$pad === void 0 ? true : _opts$pad; 145 var mask = (1 << encoding.bits) - 1; 146 var out = ''; 147 var bits = 0; // Number of bits currently in the buffer 148 var buffer = 0;
148 // Bits waiting to be written out, MSB first 149 for (var i = 0; i < data.length; ++i) { 150 // Slurp data into the buffer: 151 buffer = buffer << 8 | 0xff & data[i]; 152 bits += 8; 153 154 // Write out as much as we can: 155 while (bits > encoding.bits) { 156 bits -= encoding.bits; 157 out += encoding.chars[mask & buffer >> bits]; 158 } 159 } 160 161 // Partial character: 162 if (bits) { 163 out += encoding.chars[mask & buffer << encoding.bits - bits]; 164 } 165 166 // Add padding characters until we hit a byte boundary: 167 if (pad) { 168 while (out.length * encoding.bits & 7) { 169 out += '='; 170 } 171 } 172 return out; 173} 174 175export { base16, base32, base32hex, base64, base64url, codec };
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.