* Performs encode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
(content)
| 61 | * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content |
| 62 | */ |
| 63 | performEncode (content) { |
| 64 | const bytes = content.getBytes() |
| 65 | const variant = variantSpecs.find(variant => |
| 66 | variant.name === this.getSettingValue('variant')) |
| 67 | const n = bytes.length |
| 68 | |
| 69 | // Encode each tuple of 4 bytes |
| 70 | let string = '' |
| 71 | let digits, j, tuple |
| 72 | for (let i = 0; i < n; i += 4) { |
| 73 | // Read 32-bit unsigned integer from bytes following the |
| 74 | // big-endian convention (most significant byte first) |
| 75 | tuple = ( |
| 76 | ((bytes[i]) << 24) + |
| 77 | ((bytes[i + 1] || 0) << 16) + |
| 78 | ((bytes[i + 2] || 0) << 8) + |
| 79 | ((bytes[i + 3] || 0)) |
| 80 | ) >>> 0 |
| 81 | |
| 82 | if (variant.zeroTupleChar === null || tuple > 0) { |
| 83 | // Calculate 5 digits by repeatedly dividing |
| 84 | // by 85 and taking the remainder |
| 85 | digits = [] |
| 86 | for (j = 0; j < 5; j++) { |
| 87 | digits.push(tuple % 85) |
| 88 | tuple = Math.floor(tuple / 85) |
| 89 | } |
| 90 | |
| 91 | // Take most significant digit first |
| 92 | digits = digits.reverse() |
| 93 | |
| 94 | if (n < i + 4) { |
| 95 | // Omit final characters added due to bytes of padding |
| 96 | digits.splice(n - (i + 4), 4) |
| 97 | } |
| 98 | |
| 99 | // Convert digits to characters and glue them together |
| 100 | string += digits.map(digit => |
| 101 | variant.alphabet === null |
| 102 | ? String.fromCharCode(digit + 33) |
| 103 | : variant.alphabet[digit] |
| 104 | ).join('') |
| 105 | } else { |
| 106 | // An all-zero tuple is encoded as a single character |
| 107 | string += variant.zeroTupleChar |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return string |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Triggered before performing decode on given content. |
nothing calls this directly
no test coverage detected