117fd14ceSopenharmony_ci/* 217fd14ceSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 317fd14ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 417fd14ceSopenharmony_ci * you may not use this file except in compliance with the License. 517fd14ceSopenharmony_ci * You may obtain a copy of the License at 617fd14ceSopenharmony_ci * 717fd14ceSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 817fd14ceSopenharmony_ci * 917fd14ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1017fd14ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1117fd14ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1217fd14ceSopenharmony_ci * See the License for the specific language governing permissions and 1317fd14ceSopenharmony_ci * limitations under the License. 1417fd14ceSopenharmony_ci */ 1517fd14ceSopenharmony_ci 1617fd14ceSopenharmony_ci#ifndef STRING_UTIL_H 1717fd14ceSopenharmony_ci#define STRING_UTIL_H 1817fd14ceSopenharmony_ci 1917fd14ceSopenharmony_ci#include <stdint.h> 2017fd14ceSopenharmony_ci#include "uint8buff_utils.h" 2117fd14ceSopenharmony_ci 2217fd14ceSopenharmony_ci#define BYTE_TO_HEX_OPER_LENGTH 2 2317fd14ceSopenharmony_ci#define BYTE_TO_BASE64_DIVISOR 3 2417fd14ceSopenharmony_ci#define BYTE_TO_BASE64_MULTIPLIER 4 2517fd14ceSopenharmony_ci#define DEC 10 2617fd14ceSopenharmony_ci 2717fd14ceSopenharmony_ci#ifndef DEV_AUTH_PRINT_DEBUG_MSG 2817fd14ceSopenharmony_ci#define PRINT_DEBUG_MSG(msgBuff, msgLen, msgTag) 2917fd14ceSopenharmony_ci#else 3017fd14ceSopenharmony_ci#define PRINT_DEBUG_MSG(msgBuff, msgLen, msgTag) PrintBuffer(msgBuff, msgLen, msgTag) 3117fd14ceSopenharmony_ci#endif 3217fd14ceSopenharmony_ci 3317fd14ceSopenharmony_ci#ifdef __cplusplus 3417fd14ceSopenharmony_ciextern "C" { 3517fd14ceSopenharmony_ci#endif 3617fd14ceSopenharmony_ci 3717fd14ceSopenharmony_ci/* 3817fd14ceSopenharmony_ci * Convert hex string to byte. 3917fd14ceSopenharmony_ci * @param hexStr: hex string 4017fd14ceSopenharmony_ci * @param byte: the converted result, need malloc by caller 4117fd14ceSopenharmony_ci * @param byteLen: the length of byte, must be not shorter than strlen(hexStr) / 2 4217fd14ceSopenharmony_ci * @result success(0), otherwise, failure. 4317fd14ceSopenharmony_ci */ 4417fd14ceSopenharmony_ciint32_t HexStringToByte(const char *hexStr, uint8_t *byte, uint32_t byteLen); 4517fd14ceSopenharmony_ci 4617fd14ceSopenharmony_ci/* 4717fd14ceSopenharmony_ci * Convert byte to hex string. 4817fd14ceSopenharmony_ci * @param byte: byte to be converted 4917fd14ceSopenharmony_ci * @param byteLen: the length of byte 5017fd14ceSopenharmony_ci * @param hexStr: the converted result, need malloc by caller, and need malloc for '\0' 5117fd14ceSopenharmony_ci * @param hexLen: length of hexStr, must be not shorter than byteLen * 2 + 1, for '\0' 5217fd14ceSopenharmony_ci * @result success(0), otherwise, failure. 5317fd14ceSopenharmony_ci */ 5417fd14ceSopenharmony_ciint32_t ByteToHexString(const uint8_t *byte, uint32_t byteLen, char *hexStr, uint32_t hexLen); 5517fd14ceSopenharmony_ci 5617fd14ceSopenharmony_ci/* 5717fd14ceSopenharmony_ci * Convert string to int64_t. 5817fd14ceSopenharmony_ci * @param cp: string to be converted 5917fd14ceSopenharmony_ci * @return the converted result. 6017fd14ceSopenharmony_ci */ 6117fd14ceSopenharmony_ciint64_t StringToInt64(const char *cp); 6217fd14ceSopenharmony_ci 6317fd14ceSopenharmony_ci/* 6417fd14ceSopenharmony_ci * Convert string to upper case. 6517fd14ceSopenharmony_ci * @param oriStr: original string. 6617fd14ceSopenharmony_ci * @param desStr: the converted result. Need free. 6717fd14ceSopenharmony_ci * @return success(0), otherwise, failure. 6817fd14ceSopenharmony_ci */ 6917fd14ceSopenharmony_ciint32_t ToUpperCase(const char *oriStr, char **desStr); 7017fd14ceSopenharmony_ci 7117fd14ceSopenharmony_ci/* 7217fd14ceSopenharmony_ci * Deep copy string. 7317fd14ceSopenharmony_ci * @param str: original string. 7417fd14ceSopenharmony_ci * @param newStr: the new string. Need free. 7517fd14ceSopenharmony_ci * @return success(0), otherwise, failure. 7617fd14ceSopenharmony_ci */ 7717fd14ceSopenharmony_ciint32_t DeepCopyString(const char *str, char **newStr); 7817fd14ceSopenharmony_ci 7917fd14ceSopenharmony_ci/* 8017fd14ceSopenharmony_ci * Print the msg buffer in hex string. 8117fd14ceSopenharmony_ci * @param msgBuff: the msg buffer to be print. 8217fd14ceSopenharmony_ci * @param msgLen: the msg len. 8317fd14ceSopenharmony_ci * @param msgTag: the msg tag. 8417fd14ceSopenharmony_ci */ 8517fd14ceSopenharmony_civoid PrintBuffer(const uint8_t *msgBuff, uint32_t msgLen, const char *msgTag); 8617fd14ceSopenharmony_ci 8717fd14ceSopenharmony_ci#ifdef __cplusplus 8817fd14ceSopenharmony_ci} 8917fd14ceSopenharmony_ci#endif 9017fd14ceSopenharmony_ci#endif