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 BUFFER_CONVERTER_H
17#define BUFFER_CONVERTER_H
18
19#include <string>
20
21#include "tools/log.h"
22
23namespace OHOS::buffer {
24enum EncodingType {
25    ASCII = 1,
26    UTF8,
27    UTF16LE,
28    BASE64,
29    BASE64URL,
30    LATIN1,
31    BINARY,
32    HEX
33};
34
35constexpr uint32_t LOWER_EIGHT_BITS_MASK = 0x00FF;
36constexpr uint8_t HIGER_4_BITS_MASK = 0xF0;
37constexpr uint8_t FOUR_BYTES_STYLE = 0xF0;
38constexpr uint8_t THREE_BYTES_STYLE = 0xE0;
39constexpr uint8_t TWO_BYTES_STYLE1 = 0xD0;
40constexpr uint8_t TWO_BYTES_STYLE2 = 0xC0;
41constexpr uint32_t LOWER_10_BITS_MASK = 0x03FFU;
42constexpr uint32_t LOWER_8_BITS_MASK = 0x00FFU;
43constexpr uint8_t LOWER_6_BITS_MASK = 0x3FU;
44constexpr uint8_t LOWER_5_BITS_MASK = 0x1FU;
45constexpr uint8_t LOWER_4_BITS_MASK = 0x0FU;
46constexpr uint8_t LOWER_3_BITS_MASK = 0x07U;
47constexpr uint8_t LOWER_2_BITS_MASK = 0x03U;
48constexpr uint8_t MIDDLE_4_BITS_MASK = 0x3CU;
49constexpr uint32_t HIGH_AGENT_MASK = 0xD800U;
50constexpr uint32_t LOW_AGENT_MASK = 0xDC00U;
51constexpr uint32_t UTF8_VALID_BITS = 6;
52constexpr uint32_t UTF8_ONE_BYTE_MAX = 0x007F;
53constexpr uint32_t UTF8_ONE_BYTE_SCALE = UTF8_ONE_BYTE_MAX + 1;
54constexpr uint32_t UTF8_TWO_BYTES_MAX = 0x07FF;
55constexpr uint32_t HIGH_AGENT_RANGE_FROM = 0xD800;
56constexpr uint32_t HIGH_AGENT_RANGE_TO = 0xDBFF;
57constexpr uint32_t LOW_AGENT_RANGE_FROM = 0xDC00;
58constexpr uint8_t UTF8_TWO_BYTES_HEAD_BYTE_MASK = 0xC0;
59constexpr uint8_t UTF8_TAIL_BYTE_MASK = 0x80;
60constexpr uint8_t UTF8_THREE_BYTES_HEAD_BYTE_MASK = 0xE0;
61constexpr uint8_t UTF8_FOUR_BYTES_HEAD_BYTE_MASK = 0xF0;
62constexpr uint32_t UTF16_SPECIAL_VALUE = 0x10000;
63const std::string BASE64_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
64const std::string BASE64URL_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
65
66/**
67* IsOneByte - checks whether a charactor in a utf8 string is a one byte coding or not
68* @u8Char: a uint8_t char
69* Returns: if the highest bit of u8Char is 0, return true, else ,return false;
70*/
71bool IsOneByte(uint8_t u8Char);
72bool IsBase64Char(unsigned char c);
73
74std::u16string Utf8ToUtf16BE(const std::string &u8Str, bool *ok = nullptr);
75std::string Utf16BEToANSI(const std::wstring &wstr);
76std::u16string Utf16BEToLE(const std::u16string &wstr);
77std::string Utf8ToUtf16BEToANSI(const std::string &str);
78std::string Base64Encode(const unsigned char *src, size_t len, EncodingType type);
79std::string Base64Decode(std::string const& encodedStr, EncodingType type);
80std::string HexDecode(const std::string &hexStr);
81int FindLastIndex(uint8_t *source, uint8_t *target, int soulen, int tarlen);
82int FindIndex(uint8_t* source, uint8_t* target, int soulen, int tarlen);
83int GetGoodSuffixLengthByLastChar(uint8_t *pat, int patIndex, int patLen);
84int GetGoodSuffixLengthByFirstChar(uint8_t *pat, int patIndex, int tarlen);
85int GetBadCharLengthInReverseOrder(uint8_t *pat, char singleChar, int patIndex);
86int GetBadCharLengthInSequence(uint8_t *pat, char singleChar, int patIndex, int tarlen);
87} // namespace OHOS::Buffer
88#endif // BUFFER_CONVERTER_H
89