1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ECMASCRIPT_TOOLING_BASE_PT_BASE64_H
17#define ECMASCRIPT_TOOLING_BASE_PT_BASE64_H
18
19#include <string>
20
21namespace panda::ecmascript::tooling {
22class PtBase64 {
23public:
24    PtBase64() = default;
25    ~PtBase64() = default;
26
27    static std::size_t constexpr DecodedSize(std::size_t n)
28    {
29        return n / ENCODED_GROUP_BYTES * UNENCODED_GROUP_BYTES;
30    }
31
32    static std::size_t constexpr EncodedSize(std::size_t n)
33    {
34        return ENCODED_GROUP_BYTES * ((n + 2) / UNENCODED_GROUP_BYTES); // 2: byte filling
35    }
36
37    static std::pair<std::size_t, bool> Decode(void *output, const char *input, std::size_t len);
38    static size_t Encode(char *output, const void *input, std::size_t len);
39
40private:
41   static constexpr uint8_t UNENCODED_GROUP_BYTES = 3;
42   static constexpr uint8_t ENCODED_GROUP_BYTES = 4;
43   static constexpr uint8_t INVAILD_VALUE = 255;
44};
45}  // namespace panda::ecmascript::tooling
46#endif
47