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