|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +// @lint-ignore-every LICENSELINT |
| 9 | +/************************************************************************** |
| 10 | + Copyright (c) 2023 sewenew |
| 11 | +
|
| 12 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | + you may not use this file except in compliance with the License. |
| 14 | + You may obtain a copy of the License at |
| 15 | +
|
| 16 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +
|
| 18 | + Unless required by applicable law or agreed to in writing, software |
| 19 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | + See the License for the specific language governing permissions and |
| 22 | + limitations under the License. |
| 23 | + *************************************************************************/ |
| 24 | + |
| 25 | +#pragma once |
| 26 | + |
| 27 | +#include <executorch/runtime/platform/assert.h> |
| 28 | +#include <cassert> |
| 29 | +#include <string> |
| 30 | +#include <string_view> |
| 31 | + |
| 32 | +namespace torch { |
| 33 | +namespace executor { |
| 34 | +namespace base64 { |
| 35 | + |
| 36 | +std::string decode(const std::string_view& input); |
| 37 | + |
| 38 | +namespace detail { |
| 39 | + |
| 40 | +constexpr uint32_t DECODE_TABLE[] = { |
| 41 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 42 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 43 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, |
| 44 | + 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, |
| 45 | + 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 46 | + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 47 | + 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, |
| 48 | + 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 49 | + 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 50 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 51 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 52 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 53 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 54 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 55 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 56 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 57 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 58 | + 255}; |
| 59 | + |
| 60 | +inline void validate(uint32_t v) { |
| 61 | + ET_CHECK_MSG(v != 255, "invalid char"); |
| 62 | +} |
| 63 | + |
| 64 | +inline void decode(const std::string_view& input, std::string& output) { |
| 65 | + ET_CHECK_MSG( |
| 66 | + input.size() == 4, "input length must be 4, got %zu", input.size()); |
| 67 | + |
| 68 | + uint32_t val = 0; |
| 69 | + |
| 70 | + uint8_t c = input[0]; |
| 71 | + auto v = DECODE_TABLE[c]; |
| 72 | + validate(v); |
| 73 | + val = v; |
| 74 | + |
| 75 | + c = input[1]; |
| 76 | + v = DECODE_TABLE[c]; |
| 77 | + validate(v); |
| 78 | + val = (val << 6) | v; |
| 79 | + |
| 80 | + c = input[2]; |
| 81 | + v = DECODE_TABLE[c]; |
| 82 | + validate(v); |
| 83 | + val = (val << 6) | v; |
| 84 | + |
| 85 | + c = input[3]; |
| 86 | + v = DECODE_TABLE[c]; |
| 87 | + validate(v); |
| 88 | + val = (val << 6) | v; |
| 89 | + |
| 90 | + output.push_back(static_cast<char>((val >> 16) & 0xFF)); |
| 91 | + output.push_back(static_cast<char>((val >> 8) & 0xFF)); |
| 92 | + output.push_back(static_cast<char>(val & 0xFF)); |
| 93 | +} |
| 94 | + |
| 95 | +inline void decode_1_padding( |
| 96 | + const std::string_view& input, |
| 97 | + std::string& output) { |
| 98 | + ET_CHECK_MSG( |
| 99 | + input.size() == 3, "input length must be 3, got %zu", input.size()); |
| 100 | + |
| 101 | + uint32_t val = 0; |
| 102 | + |
| 103 | + uint8_t c = input[0]; |
| 104 | + auto v = DECODE_TABLE[c]; |
| 105 | + validate(v); |
| 106 | + val = v; |
| 107 | + |
| 108 | + c = input[1]; |
| 109 | + v = DECODE_TABLE[c]; |
| 110 | + validate(v); |
| 111 | + val = (val << 6) | v; |
| 112 | + |
| 113 | + c = input[2]; |
| 114 | + v = DECODE_TABLE[c]; |
| 115 | + validate(v); |
| 116 | + val = (val << 6) | v; |
| 117 | + |
| 118 | + output.push_back(static_cast<char>((val >> 10) & 0xFF)); |
| 119 | + output.push_back(static_cast<char>((val >> 2) & 0xFF)); |
| 120 | +} |
| 121 | + |
| 122 | +inline void decode_2_padding( |
| 123 | + const std::string_view& input, |
| 124 | + std::string& output) { |
| 125 | + assert(input.size() == 2); |
| 126 | + |
| 127 | + uint32_t val = 0; |
| 128 | + |
| 129 | + uint8_t c = input[0]; |
| 130 | + auto v = DECODE_TABLE[c]; |
| 131 | + validate(v); |
| 132 | + val = v; |
| 133 | + |
| 134 | + c = input[1]; |
| 135 | + v = DECODE_TABLE[c]; |
| 136 | + validate(v); |
| 137 | + val = (val << 6) | v; |
| 138 | + |
| 139 | + output.push_back(static_cast<char>((val >> 4) & 0xFF)); |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace detail |
| 143 | + |
| 144 | +inline std::string decode(const std::string_view& input) { |
| 145 | + ET_CHECK_MSG(!input.empty(), "empty input"); |
| 146 | + |
| 147 | + // Faster than `input.size() % 4`. |
| 148 | + ET_CHECK_MSG( |
| 149 | + (input.size() & 3) == 0 && input.size() >= 4, |
| 150 | + "input length must be larger than 4 and is multiple of 4, got %zu", |
| 151 | + input.size()); |
| 152 | + |
| 153 | + std::string output; |
| 154 | + output.reserve(input.size() / 4 * 3); |
| 155 | + auto idx = 0U; |
| 156 | + for (; idx < input.size() - 4; idx += 4) { |
| 157 | + detail::decode(input.substr(idx, 4), output); |
| 158 | + } |
| 159 | + |
| 160 | + // Last 4 bytes. Might contain paddings. |
| 161 | + if (input[idx + 3] == '=') { |
| 162 | + if (input[idx + 2] == '=') { |
| 163 | + // Tow paddings. |
| 164 | + detail::decode_2_padding(input.substr(idx, 2), output); |
| 165 | + } else { |
| 166 | + // One padding. |
| 167 | + detail::decode_1_padding(input.substr(idx, 3), output); |
| 168 | + } |
| 169 | + } else { |
| 170 | + // No padding. |
| 171 | + detail::decode(input.substr(idx, 4), output); |
| 172 | + } |
| 173 | + |
| 174 | + return output; |
| 175 | +} |
| 176 | + |
| 177 | +} // namespace base64 |
| 178 | + |
| 179 | +} // namespace executor |
| 180 | +} // namespace torch |
0 commit comments