1 #include "linkkit/infra/infra_config.h"
2
3 #ifdef INFRA_STRING
4
5 #include <stdio.h>
6 #include <string.h>
7 #include "linkkit/infra/infra_types.h"
8 #include "linkkit/infra/infra_string.h"
9
infra_hex2char(uint8_t hex)10 int8_t infra_hex2char(uint8_t hex)
11 {
12 hex = hex & 0xF;
13 return (int8_t)(hex < 10 ? '0' + hex : hex - 10 + 'a');
14 }
15
infra_hex2str(uint8_t * input,uint16_t input_len,char * output)16 void infra_hex2str(uint8_t *input, uint16_t input_len, char *output)
17 {
18 char *zEncode = "0123456789ABCDEF";
19 int i = 0, j = 0;
20
21 for (i = 0; i < input_len; i++) {
22 output[j++] = zEncode[(input[i] >> 4) & 0xf];
23 output[j++] = zEncode[(input[i]) & 0xf];
24 }
25 }
26
infra_int2str(uint32_t input,char output[10])27 void infra_int2str(uint32_t input, char output[10])
28 {
29 uint8_t i = 0, j = 0;
30 char tmp[10] = { 0 };
31
32 do {
33 tmp[i++] = input % 10 + '0';
34 } while ((input /= 10) > 0);
35
36 do {
37 output[--i] = tmp[j++];
38 } while (i > 0);
39 }
40
infra_strtok(char * str,const char * delim)41 char *infra_strtok(char *str, const char *delim)
42 {
43 int only_delim = 1;
44 static char *pos = NULL;
45 static char *target = NULL;
46
47 pos = (str == NULL) ? (pos) : (str);
48
49 if (pos == NULL || delim == NULL || strlen(pos) <= strlen(delim)) {
50 return NULL;
51 }
52
53 target = pos;
54 while (strlen(pos) >= strlen(delim)) {
55 if (memcmp(pos, delim, strlen(delim)) != 0) {
56 only_delim = 0;
57 pos++;
58 continue;
59 }
60
61 if (strlen(pos) == strlen(delim)) {
62 memset(pos, 0, strlen(delim));
63 if (only_delim) {
64 return NULL;
65 }
66 return target;
67 }
68
69 if (target == pos) {
70 pos += strlen(delim);
71 target = pos;
72 } else {
73 memset(pos, 0, strlen(delim));
74 pos += strlen(delim);
75 break;
76 }
77 }
78
79 return target;
80 }
81
82 #define LITE_isdigit(c) (((c) <= '9' && (c) >= '0') ? (1) : (0))
83
_hexval_of_char(char hex)84 static uint8_t _hexval_of_char(char hex)
85 {
86 if (LITE_isdigit(hex)) {
87 return hex - '0';
88 }
89 if (hex >= 'a' && hex <= 'f') {
90 return hex - 'a' + 10;
91 }
92 if (hex >= 'A' && hex <= 'F') {
93 return hex - 'A' + 10;
94 }
95
96 return 0;
97 }
98
LITE_hexstr_convert(char * input,int input_len,unsigned char * output,int output_len)99 void LITE_hexstr_convert(char *input, int input_len, unsigned char *output,
100 int output_len)
101 {
102 int i = 0;
103 uint8_t ch0, ch1;
104
105 if (input_len % 2 != 0) {
106 return;
107 }
108
109 while (i < input_len / 2 && i < output_len) {
110 ch0 = _hexval_of_char((char)input[2 * i]);
111 ch1 = _hexval_of_char((char)input[2 * i + 1]);
112 output[i] = (ch0 << 4 | ch1);
113 i++;
114 }
115 }
116
LITE_hexbuf_convert(unsigned char * digest,char * out,int in_len,int uppercase)117 void LITE_hexbuf_convert(unsigned char *digest, char *out, int in_len,
118 int uppercase)
119 {
120 static char *zEncode[] = { "0123456789abcdef", "0123456789ABCDEF" };
121 int j = 0;
122 int i = 0;
123 int idx = uppercase ? 1 : 0;
124
125 for (i = 0; i < in_len; i++) {
126 int a = digest[i];
127
128 out[j++] = zEncode[idx][(a >> 4) & 0xf];
129 out[j++] = zEncode[idx][a & 0xf];
130 }
131 }
132
infra_str2int(const char * input,int * val)133 int infra_str2int(const char *input, int *val)
134 {
135 int sign = 0;
136 int temp = 0;
137
138 if (input == NULL || val == NULL) {
139 return -1;
140 }
141
142 while (*input == ' ') { /* only support skipping space */
143 input++;
144 }
145
146 if (*input == '+') {
147 input++;
148 } else if (*input == '-') {
149 input++;
150 sign = -1;
151 }
152
153 while (*input != 0) {
154 if (*input < '0' || *input > '9') {
155 break;
156 }
157
158 temp = temp * 10 + (*input - '0');
159 input++;
160 }
161
162 if (sign == -1) {
163 temp = -temp;
164 }
165
166 *val = temp;
167 return 0;
168 }
169
170 #endif
171
infra_json_value(const char * input,uint32_t input_len,const char * key,uint32_t key_len,char ** value,uint32_t * value_len)172 int32_t infra_json_value(const char *input, uint32_t input_len, const char *key,
173 uint32_t key_len, char **value, uint32_t *value_len)
174 {
175 uint32_t idx = 0;
176
177 for (idx = 0; idx < input_len; idx++) {
178 if (idx + key_len >= input_len) {
179 return -1;
180 }
181 if (memcmp(&input[idx], key, key_len) == 0) {
182 idx += key_len;
183 /* shortest ":x, or ":x} or ":x] */
184 if ((idx + 4 >= input_len) || (input[idx + 1] != ':')) {
185 return -1;
186 }
187 idx += 2;
188 *value = (char *)&input[idx];
189 for (; idx < input_len; idx++) {
190 if ((input[idx] == ',') || (input[idx] == '}') ||
191 (input[idx] == ']')) {
192 *value_len = idx - (*value - input);
193 return 0;
194 }
195 }
196 }
197 }
198
199 return -1;
200 }
201