• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #define BYTE_TO_HEX_OPER_LENGTH 2
22 #define BYTE_TO_BASE64_DIVISOR 3
23 #define BYTE_TO_BASE64_MULTIPLIER 4
24 #define DEC 10
25 
26 typedef struct {
27     uint8_t *val;
28     uint32_t length;
29 } Uint8Buff;
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /*
36  * Convert hex string to byte.
37  * @param hexStr: hex string
38  * @param byte: the converted result, need malloc by caller
39  * @param byteLen: the length of byte, must be not shorter than strlen(hexStr) / 2
40  * @result success(0), otherwise, failure.
41  */
42 int32_t HexStringToByte(const char *hexStr, uint8_t *byte, uint32_t byteLen);
43 
44 /*
45  * Convert byte to hex string.
46  * @param byte: byte to be converted
47  * @param byteLen: the length of byte
48  * @param hexStr: the converted result, need malloc by caller, and need malloc for '\0'
49  * @param hexLen: length of hexStr, must be not shorter than byteLen * 2 + 1, for '\0'
50  * @result success(0), otherwise, failure.
51  */
52 int32_t ByteToHexString(const uint8_t *byte, uint32_t byteLen, char *hexStr, uint32_t hexLen);
53 
54 /*
55  * Convert string to int64_t.
56  * @param cp: string to be converted
57  * @return the converted result.
58  */
59 int64_t StringToInt64(const char *cp);
60 
61 /*
62  * Convert string to anonymous string.
63  * @param originalStr: string to be converted
64  * @param anonymousStr: the converted result
65  */
66 void ConvertToAnonymousStr(const char *originalStr, char **anonymousStr);
67 
68 /*
69  * Convert base64 string to byte.
70  * @param base64Str: base64 string
71  * @param byte: the converted result, need malloc by caller
72  * @param byteLen: the length of byte, must be not shorter than strlen(base64Str) / 4 * 3,
73  *                 and update it to the real length of the result written
74  * @result success(0), otherwise, failure.
75  */
76 int32_t Base64StringToByte(const char *base64Str, uint8_t *byte, uint32_t *byteLen);
77 
78 /*
79  * Convert byte to base64 string.
80  * @param byte: byte to be converted
81  * @param byteLen: the length of byte
82  * @param base64Str: the converted result, need malloc by caller, and need malloc for '\0'
83  * @param strLen: length of base64Str, must be not shorter than (byteLen / 3 + (byteLen % 3 != 0)) * 4 + 1, with '\0'
84  * @result success(0), otherwise, failure.
85  */
86 int32_t ByteToBase64String(const uint8_t *byte, uint32_t byteLen, char *base64Str, uint32_t strLen);
87 
88 /*
89  * Convert string to upper case.
90  * @param oriStr: original string.
91  * @param desStr: the converted result. Need free.
92  * @return success(0), otherwise, failure.
93  */
94 int32_t ToUpperCase(const char *oriStr, char **desStr);
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 #endif