14d6c458bSopenharmony_ci/* 24d6c458bSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 34d6c458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44d6c458bSopenharmony_ci * you may not use this file except in compliance with the License. 54d6c458bSopenharmony_ci * You may obtain a copy of the License at 64d6c458bSopenharmony_ci * 74d6c458bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84d6c458bSopenharmony_ci * 94d6c458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104d6c458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114d6c458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124d6c458bSopenharmony_ci * See the License for the specific language governing permissions and 134d6c458bSopenharmony_ci * limitations under the License. 144d6c458bSopenharmony_ci */ 154d6c458bSopenharmony_ci 164d6c458bSopenharmony_ci#ifndef BUFFER_CONVERTER_H 174d6c458bSopenharmony_ci#define BUFFER_CONVERTER_H 184d6c458bSopenharmony_ci 194d6c458bSopenharmony_ci#include <string> 204d6c458bSopenharmony_ci 214d6c458bSopenharmony_ci#include "tools/log.h" 224d6c458bSopenharmony_ci 234d6c458bSopenharmony_cinamespace OHOS::buffer { 244d6c458bSopenharmony_cienum EncodingType { 254d6c458bSopenharmony_ci ASCII = 1, 264d6c458bSopenharmony_ci UTF8, 274d6c458bSopenharmony_ci UTF16LE, 284d6c458bSopenharmony_ci BASE64, 294d6c458bSopenharmony_ci BASE64URL, 304d6c458bSopenharmony_ci LATIN1, 314d6c458bSopenharmony_ci BINARY, 324d6c458bSopenharmony_ci HEX 334d6c458bSopenharmony_ci}; 344d6c458bSopenharmony_ci 354d6c458bSopenharmony_ciconstexpr uint32_t LOWER_EIGHT_BITS_MASK = 0x00FF; 364d6c458bSopenharmony_ciconstexpr uint8_t HIGER_4_BITS_MASK = 0xF0; 374d6c458bSopenharmony_ciconstexpr uint8_t FOUR_BYTES_STYLE = 0xF0; 384d6c458bSopenharmony_ciconstexpr uint8_t THREE_BYTES_STYLE = 0xE0; 394d6c458bSopenharmony_ciconstexpr uint8_t TWO_BYTES_STYLE1 = 0xD0; 404d6c458bSopenharmony_ciconstexpr uint8_t TWO_BYTES_STYLE2 = 0xC0; 414d6c458bSopenharmony_ciconstexpr uint32_t LOWER_10_BITS_MASK = 0x03FFU; 424d6c458bSopenharmony_ciconstexpr uint32_t LOWER_8_BITS_MASK = 0x00FFU; 434d6c458bSopenharmony_ciconstexpr uint8_t LOWER_6_BITS_MASK = 0x3FU; 444d6c458bSopenharmony_ciconstexpr uint8_t LOWER_5_BITS_MASK = 0x1FU; 454d6c458bSopenharmony_ciconstexpr uint8_t LOWER_4_BITS_MASK = 0x0FU; 464d6c458bSopenharmony_ciconstexpr uint8_t LOWER_3_BITS_MASK = 0x07U; 474d6c458bSopenharmony_ciconstexpr uint8_t LOWER_2_BITS_MASK = 0x03U; 484d6c458bSopenharmony_ciconstexpr uint8_t MIDDLE_4_BITS_MASK = 0x3CU; 494d6c458bSopenharmony_ciconstexpr uint32_t HIGH_AGENT_MASK = 0xD800U; 504d6c458bSopenharmony_ciconstexpr uint32_t LOW_AGENT_MASK = 0xDC00U; 514d6c458bSopenharmony_ciconstexpr uint32_t UTF8_VALID_BITS = 6; 524d6c458bSopenharmony_ciconstexpr uint32_t UTF8_ONE_BYTE_MAX = 0x007F; 534d6c458bSopenharmony_ciconstexpr uint32_t UTF8_ONE_BYTE_SCALE = UTF8_ONE_BYTE_MAX + 1; 544d6c458bSopenharmony_ciconstexpr uint32_t UTF8_TWO_BYTES_MAX = 0x07FF; 554d6c458bSopenharmony_ciconstexpr uint32_t HIGH_AGENT_RANGE_FROM = 0xD800; 564d6c458bSopenharmony_ciconstexpr uint32_t HIGH_AGENT_RANGE_TO = 0xDBFF; 574d6c458bSopenharmony_ciconstexpr uint32_t LOW_AGENT_RANGE_FROM = 0xDC00; 584d6c458bSopenharmony_ciconstexpr uint8_t UTF8_TWO_BYTES_HEAD_BYTE_MASK = 0xC0; 594d6c458bSopenharmony_ciconstexpr uint8_t UTF8_TAIL_BYTE_MASK = 0x80; 604d6c458bSopenharmony_ciconstexpr uint8_t UTF8_THREE_BYTES_HEAD_BYTE_MASK = 0xE0; 614d6c458bSopenharmony_ciconstexpr uint8_t UTF8_FOUR_BYTES_HEAD_BYTE_MASK = 0xF0; 624d6c458bSopenharmony_ciconstexpr uint32_t UTF16_SPECIAL_VALUE = 0x10000; 634d6c458bSopenharmony_ciconst std::string BASE64_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 644d6c458bSopenharmony_ciconst std::string BASE64URL_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; 654d6c458bSopenharmony_ci 664d6c458bSopenharmony_ci/** 674d6c458bSopenharmony_ci* IsOneByte - checks whether a charactor in a utf8 string is a one byte coding or not 684d6c458bSopenharmony_ci* @u8Char: a uint8_t char 694d6c458bSopenharmony_ci* Returns: if the highest bit of u8Char is 0, return true, else ,return false; 704d6c458bSopenharmony_ci*/ 714d6c458bSopenharmony_cibool IsOneByte(uint8_t u8Char); 724d6c458bSopenharmony_cibool IsBase64Char(unsigned char c); 734d6c458bSopenharmony_ci 744d6c458bSopenharmony_cistd::u16string Utf8ToUtf16BE(const std::string &u8Str, bool *ok = nullptr); 754d6c458bSopenharmony_cistd::string Utf16BEToANSI(const std::wstring &wstr); 764d6c458bSopenharmony_cistd::u16string Utf16BEToLE(const std::u16string &wstr); 774d6c458bSopenharmony_cistd::string Utf8ToUtf16BEToANSI(const std::string &str); 784d6c458bSopenharmony_cistd::string Base64Encode(const unsigned char *src, size_t len, EncodingType type); 794d6c458bSopenharmony_cistd::string Base64Decode(std::string const& encodedStr, EncodingType type); 804d6c458bSopenharmony_cistd::string HexDecode(const std::string &hexStr); 814d6c458bSopenharmony_ciint FindLastIndex(uint8_t *source, uint8_t *target, int soulen, int tarlen); 824d6c458bSopenharmony_ciint FindIndex(uint8_t* source, uint8_t* target, int soulen, int tarlen); 834d6c458bSopenharmony_ciint GetGoodSuffixLengthByLastChar(uint8_t *pat, int patIndex, int patLen); 844d6c458bSopenharmony_ciint GetGoodSuffixLengthByFirstChar(uint8_t *pat, int patIndex, int tarlen); 854d6c458bSopenharmony_ciint GetBadCharLengthInReverseOrder(uint8_t *pat, char singleChar, int patIndex); 864d6c458bSopenharmony_ciint GetBadCharLengthInSequence(uint8_t *pat, char singleChar, int patIndex, int tarlen); 874d6c458bSopenharmony_ci} // namespace OHOS::Buffer 884d6c458bSopenharmony_ci#endif // BUFFER_CONVERTER_H 89