|
| 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 <cassert> |
| 28 | +#include <cstdint> |
| 29 | +#include <string> |
| 30 | +#include <string_view> |
| 31 | + |
| 32 | +#include "result.h" |
| 33 | + |
| 34 | +namespace base64 { |
| 35 | + |
| 36 | +using tokenizers::Error; |
| 37 | +using tokenizers::Result; |
| 38 | + |
| 39 | +Result<std::string> decode(const std::string_view &input); |
| 40 | + |
| 41 | +namespace detail { |
| 42 | + |
| 43 | +constexpr uint32_t DECODE_TABLE[] = { |
| 44 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 45 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 46 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, |
| 47 | + 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, |
| 48 | + 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 49 | + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 50 | + 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, |
| 51 | + 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 52 | + 49, 50, 51, 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, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 59 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 60 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 61 | + 255}; |
| 62 | + |
| 63 | +inline Error validate(uint32_t v) { |
| 64 | + if (v == 255) { |
| 65 | + fprintf(stderr, "invalid char"); |
| 66 | + return Error::Base64DecodeFailure; |
| 67 | + } |
| 68 | + return Error::Ok; |
| 69 | +} |
| 70 | + |
| 71 | +inline Error decode(const std::string_view &input, std::string &output) { |
| 72 | + if (input.size() != 4) { |
| 73 | + fprintf(stderr, "input length must be 4, got %zu", input.size()); |
| 74 | + return Error::Base64DecodeFailure; |
| 75 | + } |
| 76 | + |
| 77 | + uint32_t val = 0; |
| 78 | + |
| 79 | + uint8_t c = input[0]; |
| 80 | + auto v = DECODE_TABLE[c]; |
| 81 | + TK_CHECK_OK_OR_RETURN_ERROR(validate(v)); |
| 82 | + val = v; |
| 83 | + |
| 84 | + c = input[1]; |
| 85 | + v = DECODE_TABLE[c]; |
| 86 | + TK_CHECK_OK_OR_RETURN_ERROR(validate(v)); |
| 87 | + val = (val << 6) | v; |
| 88 | + |
| 89 | + c = input[2]; |
| 90 | + v = DECODE_TABLE[c]; |
| 91 | + TK_CHECK_OK_OR_RETURN_ERROR(validate(v)); |
| 92 | + val = (val << 6) | v; |
| 93 | + |
| 94 | + c = input[3]; |
| 95 | + v = DECODE_TABLE[c]; |
| 96 | + TK_CHECK_OK_OR_RETURN_ERROR(validate(v)); |
| 97 | + val = (val << 6) | v; |
| 98 | + |
| 99 | + output.push_back(static_cast<char>((val >> 16) & 0xFF)); |
| 100 | + output.push_back(static_cast<char>((val >> 8) & 0xFF)); |
| 101 | + output.push_back(static_cast<char>(val & 0xFF)); |
| 102 | + return Error::Ok; |
| 103 | +} |
| 104 | + |
| 105 | +inline Error decode_1_padding(const std::string_view &input, |
| 106 | + std::string &output) { |
| 107 | + if (input.size() != 3) { |
| 108 | + fprintf(stderr, "input length must be 3, got %zu", input.size()); |
| 109 | + return Error::Base64DecodeFailure; |
| 110 | + } |
| 111 | + |
| 112 | + uint32_t val = 0; |
| 113 | + |
| 114 | + uint8_t c = input[0]; |
| 115 | + auto v = DECODE_TABLE[c]; |
| 116 | + TK_CHECK_OK_OR_RETURN_ERROR(validate(v)); |
| 117 | + val = v; |
| 118 | + |
| 119 | + c = input[1]; |
| 120 | + v = DECODE_TABLE[c]; |
| 121 | + TK_CHECK_OK_OR_RETURN_ERROR(validate(v)); |
| 122 | + val = (val << 6) | v; |
| 123 | + |
| 124 | + c = input[2]; |
| 125 | + v = DECODE_TABLE[c]; |
| 126 | + TK_CHECK_OK_OR_RETURN_ERROR(validate(v)); |
| 127 | + val = (val << 6) | v; |
| 128 | + |
| 129 | + output.push_back(static_cast<char>((val >> 10) & 0xFF)); |
| 130 | + output.push_back(static_cast<char>((val >> 2) & 0xFF)); |
| 131 | + return Error::Ok; |
| 132 | +} |
| 133 | + |
| 134 | +inline Error decode_2_padding(const std::string_view &input, |
| 135 | + std::string &output) { |
| 136 | + TK_CHECK_OR_RETURN_ERROR(input.size() == 2, Base64DecodeFailure); |
| 137 | + |
| 138 | + uint32_t val = 0; |
| 139 | + |
| 140 | + uint8_t c = input[0]; |
| 141 | + auto v = DECODE_TABLE[c]; |
| 142 | + TK_CHECK_OK_OR_RETURN_ERROR(validate(v)); |
| 143 | + val = v; |
| 144 | + |
| 145 | + c = input[1]; |
| 146 | + v = DECODE_TABLE[c]; |
| 147 | + TK_CHECK_OK_OR_RETURN_ERROR(validate(v)); |
| 148 | + val = (val << 6) | v; |
| 149 | + |
| 150 | + output.push_back(static_cast<char>((val >> 4) & 0xFF)); |
| 151 | + return Error::Ok; |
| 152 | +} |
| 153 | + |
| 154 | +} // namespace detail |
| 155 | + |
| 156 | +inline tokenizers::Result<std::string> decode(const std::string_view &input) { |
| 157 | + if (input.empty()) { |
| 158 | + fprintf(stderr, "empty input"); |
| 159 | + return Error::Base64DecodeFailure; |
| 160 | + } |
| 161 | + |
| 162 | + // Faster than `input.size() % 4`. |
| 163 | + if ((input.size() & 3) != 0 || input.size() < 4) { |
| 164 | + fprintf(stderr, |
| 165 | + "input length must be larger than 4 and is multiple of 4, got %zu", |
| 166 | + input.size()); |
| 167 | + return Error::Base64DecodeFailure; |
| 168 | + } |
| 169 | + |
| 170 | + std::string output; |
| 171 | + output.reserve(input.size() / 4 * 3); |
| 172 | + auto idx = 0U; |
| 173 | + for (; idx < input.size() - 4; idx += 4) { |
| 174 | + TK_CHECK_OK_OR_RETURN_ERROR(detail::decode(input.substr(idx, 4), output)); |
| 175 | + } |
| 176 | + |
| 177 | + // Last 4 bytes. Might contain paddings. |
| 178 | + if (input[idx + 3] == '=') { |
| 179 | + if (input[idx + 2] == '=') { |
| 180 | + // Tow paddings. |
| 181 | + TK_CHECK_OK_OR_RETURN_ERROR( |
| 182 | + detail::decode_2_padding(input.substr(idx, 2), output)); |
| 183 | + } else { |
| 184 | + // One padding. |
| 185 | + TK_CHECK_OK_OR_RETURN_ERROR( |
| 186 | + detail::decode_1_padding(input.substr(idx, 3), output)); |
| 187 | + } |
| 188 | + } else { |
| 189 | + // No padding. |
| 190 | + TK_CHECK_OK_OR_RETURN_ERROR(detail::decode(input.substr(idx, 4), output)); |
| 191 | + } |
| 192 | + |
| 193 | + return output; |
| 194 | +} |
| 195 | +} // namespace base64 |
0 commit comments