| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | import * as Log from './util/logging.js'; |
| 8 | |
| 9 | export default { |
| 10 | |
| 11 | toBase64Table: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split(''), |
| 12 | base64Pad: '=', |
| 13 | |
| 14 | encode(data) { |
| 15 | "use strict"; |
| 16 | if (typeof data.toBase64 === 'function') { |
| 17 | return data.toBase64(); |
| 18 | } |
| 19 | let result = ''; |
| 20 | const length = data.length; |
| 21 | const lengthpad = (length % 3); |
| 22 | |
| 23 | |
| 24 | for (let i = 0; i < (length - 2); i += 3) { |
| 25 | result += this.toBase64Table[data[i] >> 2]; |
| 26 | result += this.toBase64Table[((data[i] & 0x03) << 4) + (data[i + 1] >> 4)]; |
| 27 | result += this.toBase64Table[((data[i + 1] & 0x0f) << 2) + (data[i + 2] >> 6)]; |
| 28 | result += this.toBase64Table[data[i + 2] & 0x3f]; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | const j = length - lengthpad; |
| 33 | if (lengthpad === 2) { |
| 34 | result += this.toBase64Table[data[j] >> 2]; |
| 35 | result += this.toBase64Table[((data[j] & 0x03) << 4) + (data[j + 1] >> 4)]; |
| 36 | result += this.toBase64Table[(data[j + 1] & 0x0f) << 2]; |
| 37 | result += this.toBase64Table[64]; |
| 38 | } else if (lengthpad === 1) { |
| 39 | result += this.toBase64Table[data[j] >> 2]; |
| 40 | result += this.toBase64Table[(data[j] & 0x03) << 4]; |
| 41 | result += this.toBase64Table[64]; |
| 42 | result += this.toBase64Table[64]; |
| 43 | } |
| 44 | |
| 45 | return result; |
| 46 | }, |
| 47 | |
| 48 | |
| 49 | |
| 50 | toBinaryTable: [ |
| 51 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 52 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 53 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, |
| 54 | 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, |
| 55 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, |
| 56 | 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, |
| 57 | -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, |
| 58 | 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 |
| 59 | ], |
| 60 | |
| 61 | |
| 62 | decode(data, offset = 0) { |
| 63 | let dataLength = data.indexOf('=') - offset; |
| 64 | if (dataLength < 0) { dataLength = data.length - offset; } |
| 65 | |
| 66 | |
| 67 | const resultLength = (dataLength >> 2) * 3 + Math.floor((dataLength % 4) / 1.5); |
| 68 | const result = new Array(resultLength); |
| 69 | |
| 70 | |
| 71 | |
| 72 | let leftbits = 0; |
| 73 | let leftdata = 0; |
| 74 | for (let idx = 0, i = offset; i < data.length; i++) { |
| 75 | const c = this.toBinaryTable[data.charCodeAt(i) & 0x7f]; |
| 76 | const padding = (data.charAt(i) === this.base64Pad); |
| 77 | |
| 78 | if (c === -1) { |
| 79 | Log.Error("Illegal character code " + data.charCodeAt(i) + " at position " + i); |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | leftdata = (leftdata << 6) | c; |
| 85 | leftbits += 6; |
| 86 | |
| 87 | |
| 88 | if (leftbits >= 8) { |
| 89 | leftbits -= 8; |
| 90 | |
| 91 | if (!padding) { |
| 92 | result[idx++] = (leftdata >> leftbits) & 0xff; |
| 93 | } |
| 94 | leftdata &= (1 << leftbits) - 1; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | if (leftbits) { |
| 100 | const err = new Error('Corrupted base64 string'); |
| 101 | err.name = 'Base64-Error'; |
| 102 | throw err; |
| 103 | } |
| 104 | |
| 105 | return result; |
| 106 | } |
| 107 | }; |