|
| 1 | +// source: https://github.com/Holmojan/base94 |
| 2 | +#pragma once |
| 3 | + |
| 4 | +#ifndef BASE94_HPP |
| 5 | +#define BASE94_HPP |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | +#include <vector> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +class Base94 { |
| 12 | + /* 96 printable characters(include tab) */ |
| 13 | + /* remove \ for compatibility */ |
| 14 | + /* remove tab for uniformity */ |
| 15 | + /* luckly, 11/9 > log(256)/log(94) */ |
| 16 | +protected: |
| 17 | + enum { |
| 18 | + BASE94_SYMBOL_COUNT = 94, |
| 19 | + BASE94_INPUT_BLOCK_SIZE = 9, |
| 20 | + BASE94_OUTPUT_BLOCK_SIZE = 11, |
| 21 | + }; |
| 22 | + static constexpr char encode_table[BASE94_SYMBOL_COUNT + 1] = { |
| 23 | + " !\"#$%&'()*+,-./" |
| 24 | + "0123456789" |
| 25 | + ":;<=>?@" |
| 26 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 27 | + "[]^_`" |
| 28 | + "abcdefghijklmnopqrstuvwxyz" |
| 29 | + "{|}~" |
| 30 | + }; |
| 31 | + static constexpr char first_symbol = encode_table[0]; |
| 32 | + static constexpr char last_symbol = encode_table[BASE94_SYMBOL_COUNT - 1]; |
| 33 | + |
| 34 | + static constexpr uint32_t encode_tail_cut[BASE94_INPUT_BLOCK_SIZE] = { |
| 35 | + 0, 9, 8, 7, 6, 4, 3, 2, 1 //11-ceil(input_tail*8/log2(94)) |
| 36 | + }; |
| 37 | + static constexpr uint32_t decode_tail_cut[BASE94_OUTPUT_BLOCK_SIZE] = { |
| 38 | + 0, 0, 8, 7, 6, 5, 0, 4, 3, 2, 1 //9-input_tail |
| 39 | + }; |
| 40 | + |
| 41 | + typedef uint8_t base94_input_block[BASE94_INPUT_BLOCK_SIZE]; |
| 42 | + typedef char base94_output_block[BASE94_OUTPUT_BLOCK_SIZE]; |
| 43 | + |
| 44 | + inline static bool encode_symbol(uint32_t x, char& y) { |
| 45 | + y = encode_table[x]; |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + inline static bool decode_symbol(char x, uint32_t& y) { |
| 50 | + if (x < first_symbol || x > last_symbol || x == '\\') |
| 51 | + return false; |
| 52 | + if (x > '\\') x--; |
| 53 | + y = x - first_symbol; |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + static bool encode_block(const base94_input_block& x, base94_output_block& y) { |
| 58 | + enum { |
| 59 | + BASE94_ENCODE_MOD = (1 << 24) % BASE94_SYMBOL_COUNT, |
| 60 | + BASE94_ENCODE_MOD2 = (BASE94_ENCODE_MOD * BASE94_ENCODE_MOD) % BASE94_SYMBOL_COUNT, |
| 61 | + }; |
| 62 | + uint32_t a = x[0] | (x[1] << 8) | (x[2] << 16); |
| 63 | + uint32_t b = x[3] | (x[4] << 8) | (x[5] << 16); |
| 64 | + uint32_t c = x[6] | (x[7] << 8) | (x[8] << 16); |
| 65 | + uint32_t d = 0; |
| 66 | + for (uint32_t i = 0; i < BASE94_OUTPUT_BLOCK_SIZE; i++) { |
| 67 | + d = (a + b * BASE94_ENCODE_MOD + c * BASE94_ENCODE_MOD2) % BASE94_SYMBOL_COUNT; |
| 68 | + if (!encode_symbol(d, y[i])) |
| 69 | + return false; |
| 70 | + b += c % BASE94_SYMBOL_COUNT << 24; |
| 71 | + a += b % BASE94_SYMBOL_COUNT << 24; |
| 72 | + c /= BASE94_SYMBOL_COUNT; |
| 73 | + b /= BASE94_SYMBOL_COUNT; |
| 74 | + a /= BASE94_SYMBOL_COUNT; |
| 75 | + } |
| 76 | + return true; |
| 77 | + } |
| 78 | + static bool decode_block(const base94_output_block& x, base94_input_block& y) { |
| 79 | + enum { |
| 80 | + BASE94_DECODE_MASK = (1 << 24) - 1, |
| 81 | + }; |
| 82 | + uint32_t a = 0; |
| 83 | + uint32_t b = 0; |
| 84 | + uint32_t c = 0; |
| 85 | + uint32_t d = 0; |
| 86 | + for (uint32_t i = BASE94_OUTPUT_BLOCK_SIZE - 1; i != -1; i--) { |
| 87 | + if (!decode_symbol(x[i], d)) |
| 88 | + return false; |
| 89 | + a *= BASE94_SYMBOL_COUNT; |
| 90 | + b *= BASE94_SYMBOL_COUNT; |
| 91 | + c *= BASE94_SYMBOL_COUNT; |
| 92 | + a += d; |
| 93 | + b += a >> 24; |
| 94 | + c += b >> 24; |
| 95 | + a &= BASE94_DECODE_MASK; |
| 96 | + b &= BASE94_DECODE_MASK; |
| 97 | + } |
| 98 | + y[0] = a & 0xFF; |
| 99 | + y[1] = (a >> 8) & 0xFF; |
| 100 | + y[2] = a >> 16; |
| 101 | + y[3] = b & 0xFF; |
| 102 | + y[4] = (b >> 8) & 0xFF; |
| 103 | + y[5] = b >> 16; |
| 104 | + y[6] = c & 0xFF; |
| 105 | + y[7] = (c >> 8) & 0xFF; |
| 106 | + y[8] = c >> 16; |
| 107 | + return true; |
| 108 | + } |
| 109 | +public: |
| 110 | + bool encode(const std::vector<uint8_t>& /*int*/ v, std::string& /*out*/ s) { |
| 111 | + uint32_t block_count = (v.size() + BASE94_INPUT_BLOCK_SIZE - 1) / BASE94_INPUT_BLOCK_SIZE; |
| 112 | + uint32_t buffer_size = block_count * BASE94_OUTPUT_BLOCK_SIZE; |
| 113 | + uint32_t tail = v.size() % BASE94_INPUT_BLOCK_SIZE; |
| 114 | + s.resize(buffer_size); |
| 115 | + uint32_t off_v = 0, off_s = 0; |
| 116 | + for (; off_v + BASE94_INPUT_BLOCK_SIZE <= v.size(); off_v += BASE94_INPUT_BLOCK_SIZE, off_s += BASE94_OUTPUT_BLOCK_SIZE) { |
| 117 | + if (!encode_block(*(base94_input_block*)(v.data() + off_v), *(base94_output_block*)(s.data() + off_s))) |
| 118 | + return false; |
| 119 | + } |
| 120 | + if (tail > 0) { |
| 121 | + base94_input_block buff = { 0 }; |
| 122 | + for (uint32_t i = 0; i < tail; i++) |
| 123 | + buff[i] = v[off_v + i]; |
| 124 | + if (!encode_block(buff, *(base94_output_block*)(s.data() + off_s))) |
| 125 | + return false; |
| 126 | + s.resize(buffer_size - encode_tail_cut[tail]); |
| 127 | + } |
| 128 | + return true; |
| 129 | + } |
| 130 | + bool decode(const std::string& /*in*/ s, std::vector<uint8_t>& /*out*/ v) { |
| 131 | + uint32_t block_count = (s.size() + BASE94_OUTPUT_BLOCK_SIZE - 1) / BASE94_OUTPUT_BLOCK_SIZE; |
| 132 | + uint32_t buffer_size = block_count * BASE94_INPUT_BLOCK_SIZE; |
| 133 | + uint32_t tail = s.size() % BASE94_OUTPUT_BLOCK_SIZE; |
| 134 | + |
| 135 | + if (tail > 0 && decode_tail_cut[tail] == 0) |
| 136 | + return false; |
| 137 | + |
| 138 | + v.resize(buffer_size); |
| 139 | + uint32_t off_s = 0, off_v = 0; |
| 140 | + for (; off_s + BASE94_OUTPUT_BLOCK_SIZE <= s.size(); off_s += BASE94_OUTPUT_BLOCK_SIZE, off_v += BASE94_INPUT_BLOCK_SIZE) { |
| 141 | + if (!decode_block(*(base94_output_block*)(s.data() + off_s), *(base94_input_block*)(v.data() + off_v))) |
| 142 | + return false; |
| 143 | + } |
| 144 | + if (tail > 0) { |
| 145 | + base94_output_block buff = { first_symbol, first_symbol, first_symbol, first_symbol, first_symbol, |
| 146 | + first_symbol, first_symbol, first_symbol, first_symbol, first_symbol, first_symbol }; |
| 147 | + for (uint32_t i = 0; i < tail; i++) |
| 148 | + buff[i] = s[off_v + i]; |
| 149 | + if (!decode_block(buff, *(base94_input_block*)(v.data() + off_v))) |
| 150 | + return false; |
| 151 | + v.resize(buffer_size - decode_tail_cut[tail]); |
| 152 | + } |
| 153 | + return true; |
| 154 | + } |
| 155 | +}; |
| 156 | + |
| 157 | +#endif // BASE94_HPP |
0 commit comments