1 /* 2 * Copyright (C) 2021 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 STRING_UTIL_H 17 #define STRING_UTIL_H 18 19 #include <stdint.h> 20 #include "uint8buff_utils.h" 21 22 #define BYTE_TO_HEX_OPER_LENGTH 2 23 #define BYTE_TO_BASE64_DIVISOR 3 24 #define BYTE_TO_BASE64_MULTIPLIER 4 25 #define DEC 10 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 /* 32 * Convert hex string to byte. 33 * @param hexStr: hex string 34 * @param byte: the converted result, need malloc by caller 35 * @param byteLen: the length of byte, must be not shorter than strlen(hexStr) / 2 36 * @result success(0), otherwise, failure. 37 */ 38 int32_t HexStringToByte(const char *hexStr, uint8_t *byte, uint32_t byteLen); 39 40 /* 41 * Convert byte to hex string. 42 * @param byte: byte to be converted 43 * @param byteLen: the length of byte 44 * @param hexStr: the converted result, need malloc by caller, and need malloc for '\0' 45 * @param hexLen: length of hexStr, must be not shorter than byteLen * 2 + 1, for '\0' 46 * @result success(0), otherwise, failure. 47 */ 48 int32_t ByteToHexString(const uint8_t *byte, uint32_t byteLen, char *hexStr, uint32_t hexLen); 49 50 /* 51 * Convert string to int64_t. 52 * @param cp: string to be converted 53 * @return the converted result. 54 */ 55 int64_t StringToInt64(const char *cp); 56 57 /* 58 * Convert string to upper case. 59 * @param oriStr: original string. 60 * @param desStr: the converted result. Need free. 61 * @return success(0), otherwise, failure. 62 */ 63 int32_t ToUpperCase(const char *oriStr, char **desStr); 64 65 /* 66 * Deep copy string. 67 * @param str: original string. 68 * @param newStr: the new string. Need free. 69 * @return success(0), otherwise, failure. 70 */ 71 int32_t DeepCopyString(const char *str, char **newStr); 72 73 #ifdef __cplusplus 74 } 75 #endif 76 #endif