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