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 #ifndef DEV_AUTH_PRINT_DEBUG_MSG 28 #define PRINT_DEBUG_MSG(msgBuff, msgLen, msgTag) 29 #else 30 #define PRINT_DEBUG_MSG(msgBuff, msgLen, msgTag) PrintBuffer(msgBuff, msgLen, msgTag) 31 #endif 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 /* 38 * Convert hex string to byte. 39 * @param hexStr: hex string 40 * @param byte: the converted result, need malloc by caller 41 * @param byteLen: the length of byte, must be not shorter than strlen(hexStr) / 2 42 * @result success(0), otherwise, failure. 43 */ 44 int32_t HexStringToByte(const char *hexStr, uint8_t *byte, uint32_t byteLen); 45 46 /* 47 * Convert byte to hex string. 48 * @param byte: byte to be converted 49 * @param byteLen: the length of byte 50 * @param hexStr: the converted result, need malloc by caller, and need malloc for '\0' 51 * @param hexLen: length of hexStr, must be not shorter than byteLen * 2 + 1, for '\0' 52 * @result success(0), otherwise, failure. 53 */ 54 int32_t ByteToHexString(const uint8_t *byte, uint32_t byteLen, char *hexStr, uint32_t hexLen); 55 56 /* 57 * Convert string to int64_t. 58 * @param cp: string to be converted 59 * @return the converted result. 60 */ 61 int64_t StringToInt64(const char *cp); 62 63 /* 64 * Convert string to upper case. 65 * @param oriStr: original string. 66 * @param desStr: the converted result. Need free. 67 * @return success(0), otherwise, failure. 68 */ 69 int32_t ToUpperCase(const char *oriStr, char **desStr); 70 71 /* 72 * Deep copy string. 73 * @param str: original string. 74 * @param newStr: the new string. Need free. 75 * @return success(0), otherwise, failure. 76 */ 77 int32_t DeepCopyString(const char *str, char **newStr); 78 79 /* 80 * Print the msg buffer in hex string. 81 * @param msgBuff: the msg buffer to be print. 82 * @param msgLen: the msg len. 83 * @param msgTag: the msg tag. 84 */ 85 void PrintBuffer(const uint8_t *msgBuff, uint32_t msgLen, const char *msgTag); 86 87 #ifdef __cplusplus 88 } 89 #endif 90 #endif