1 #include "core_string.h"
2
core_str2uint(char * input,uint8_t input_len,uint32_t * output)3 int32_t core_str2uint(char *input, uint8_t input_len, uint32_t *output)
4 {
5 uint8_t index = 0;
6 uint32_t temp = 0;
7
8 for (index = 0; index < input_len; index++) {
9 if (input[index] < '0' || input[index] > '9') {
10 return STATE_USER_INPUT_OUT_RANGE;
11 }
12 temp = temp * 10 + input[index] - '0';
13 }
14 *output = temp;
15
16 return STATE_SUCCESS;
17 }
18
core_str2uint64(char * input,uint8_t input_len,uint64_t * output)19 int32_t core_str2uint64(char *input, uint8_t input_len, uint64_t *output)
20 {
21 uint8_t index = 0;
22 uint64_t temp = 0;
23
24 for (index = 0; index < input_len; index++) {
25 if (input[index] < '0' || input[index] > '9') {
26 return STATE_USER_INPUT_OUT_RANGE;
27 }
28 temp = temp * 10 + input[index] - '0';
29 }
30 *output = temp;
31
32 return STATE_SUCCESS;
33 }
34
core_uint2str(uint32_t input,char * output,uint8_t * output_len)35 int32_t core_uint2str(uint32_t input, char *output, uint8_t *output_len)
36 {
37 uint8_t i = 0, j = 0;
38 char temp[10] = {0};
39
40 do {
41 temp[i++] = input % 10 + '0';
42 } while ((input /= 10) > 0);
43
44 do {
45 output[--i] = temp[j++];
46 } while (i > 0);
47
48 if (output_len) {
49 *output_len = j;
50 }
51
52 return STATE_SUCCESS;
53 }
54
core_uint642str(uint64_t input,char * output,uint8_t * output_len)55 int32_t core_uint642str(uint64_t input, char *output, uint8_t *output_len)
56 {
57 uint8_t i = 0, j = 0;
58 char temp[20] = {0};
59
60 do {
61 temp[i++] = input % 10 + '0';
62 } while ((input /= 10) > 0);
63
64 do {
65 output[--i] = temp[j++];
66 } while (i > 0);
67
68 if (output_len) {
69 *output_len = j;
70 }
71
72 return STATE_SUCCESS;
73 }
74
core_int2str(int32_t input,char * output,uint8_t * output_len)75 int32_t core_int2str(int32_t input, char *output, uint8_t *output_len)
76 {
77 uint8_t i = 0, j = 0, minus = 0;
78 char temp[11] = {0};
79 int64_t in = input;
80
81 if (in < 0) {
82 minus = 1;
83 in = -in;
84 }
85
86 do {
87 temp[minus + (i++)] = in % 10 + '0';
88 } while ((in /= 10) > 0);
89
90 do {
91 output[minus + (--i)] = temp[minus + (j++)];
92 } while (i > 0);
93
94 if (minus == 1) {
95 output[0] = '-';
96 }
97
98 if (output_len) {
99 *output_len = j + minus;
100 }
101
102 return STATE_SUCCESS;
103 }
104
core_hex2str(uint8_t * input,uint32_t input_len,char * output,uint8_t lowercase)105 int32_t core_hex2str(uint8_t *input, uint32_t input_len, char *output, uint8_t lowercase)
106 {
107 char *upper = "0123456789ABCDEF";
108 char *lower = "0123456789abcdef";
109 char *encode = upper;
110 int i = 0, j = 0;
111
112 if (lowercase) {
113 encode = lower;
114 }
115
116 for (i = 0; i < input_len; i++) {
117 output[j++] = encode[(input[i] >> 4) & 0xf];
118 output[j++] = encode[(input[i]) & 0xf];
119 }
120
121 return STATE_SUCCESS;
122 }
123
core_str2hex(char * input,uint32_t input_len,uint8_t * output)124 int32_t core_str2hex(char *input, uint32_t input_len, uint8_t *output)
125 {
126 uint32_t idx = 0;
127
128 if (input_len % 2 != 0) {
129 return STATE_USER_INPUT_OUT_RANGE;
130 }
131
132 for (idx = 0; idx < input_len; idx += 2) {
133 if (input[idx] >= '0' && input[idx] <= '9') {
134 output[idx / 2] = (input[idx] - '0') << 4;
135 } else if (input[idx] >= 'A' && input[idx] <= 'F') {
136 output[idx / 2] = (input[idx] - 'A' + 0x0A) << 4;
137 } else if (input[idx] >= 'a' && input[idx] <= 'f') {
138 output[idx / 2] = (input[idx] - 'a' + 0x0A) << 4;
139 }
140 if (input[idx + 1] >= '0' && input[idx + 1] <= '9') {
141 output[idx / 2] |= (input[idx + 1] - '0');
142 } else if (input[idx + 1] >= 'A' && input[idx + 1] <= 'F') {
143 output[idx / 2] |= (input[idx + 1] - 'A' + 0x0A);
144 } else if (input[idx + 1] >= 'a' && input[idx + 1] <= 'f') {
145 output[idx / 2] |= (input[idx + 1] - 'a' + 0x0A);
146 }
147 }
148
149 return STATE_SUCCESS;
150 }
151
core_strdup(aiot_sysdep_portfile_t * sysdep,char ** dest,char * src,char * module_name)152 int32_t core_strdup(aiot_sysdep_portfile_t *sysdep, char **dest, char *src, char *module_name)
153 {
154 if (*dest != NULL) {
155 sysdep->core_sysdep_free(*dest);
156 *dest = NULL;
157 }
158
159 *dest = sysdep->core_sysdep_malloc((uint32_t)(strlen(src) + 1), module_name);
160 if (*dest == NULL) {
161 return STATE_SYS_DEPEND_MALLOC_FAILED;
162 }
163 memset(*dest, 0, strlen(src) + 1);
164 memcpy(*dest, src, strlen(src));
165
166 return STATE_SUCCESS;
167 }
168
core_sprintf(aiot_sysdep_portfile_t * sysdep,char ** dest,char * fmt,char * src[],uint8_t count,char * module_name)169 int32_t core_sprintf(aiot_sysdep_portfile_t *sysdep, char **dest, char *fmt, char *src[], uint8_t count,
170 char *module_name)
171 {
172 char *buffer = NULL, *value = NULL;
173 uint8_t idx = 0, percent_idx = 0;
174 uint32_t buffer_len = 0;
175
176 buffer_len += strlen(fmt) - 2 * count;
177 for (percent_idx = 0; percent_idx < count; percent_idx++) {
178 value = (*(src + percent_idx) == NULL) ? ("") : (*(src + percent_idx));
179 buffer_len += strlen(value);
180 }
181
182 buffer = sysdep->core_sysdep_malloc(buffer_len + 1, module_name);
183 if (buffer == NULL) {
184 return STATE_SYS_DEPEND_MALLOC_FAILED;
185 }
186 memset(buffer, 0, buffer_len + 1);
187
188 for (idx = 0, percent_idx = 0; idx < strlen(fmt);) {
189 if (fmt[idx] == '%' && fmt[idx + 1] == 's') {
190 value = (*(src + percent_idx) == NULL) ? ("") : (*(src + percent_idx));
191 memcpy(buffer + strlen(buffer), value, strlen(value));
192 percent_idx++;
193 idx += 2;
194 } else {
195 buffer[strlen(buffer)] = fmt[idx++];
196 }
197 }
198 *dest = buffer;
199 return STATE_SUCCESS;
200 }
201
core_json_value(const char * input,uint32_t input_len,const char * key,uint32_t key_len,char ** value,uint32_t * value_len)202 int32_t core_json_value(const char *input, uint32_t input_len, const char *key, uint32_t key_len, char **value,
203 uint32_t *value_len)
204 {
205 uint32_t idx = 0;
206
207 for (idx = 0; idx < input_len; idx++) {
208 if (idx + key_len >= input_len) {
209 return STATE_USER_INPUT_JSON_PARSE_FAILED;
210 }
211 if ((memcmp(&input[idx], key, key_len) == 0) &&
212 ((idx > 0) && (input[idx - 1] == '"')) &&
213 ((idx + key_len < input_len) && (input[idx + key_len] == '"'))) {
214 idx += key_len;
215 /* shortest ":x, or ":x} or ":x] */
216 if ((idx + 2 >= input_len) ||
217 (input[idx + 1] != ':')) {
218 return STATE_USER_INPUT_JSON_PARSE_FAILED;
219 }
220 idx += 2;
221 if (input[idx] == '"') {
222 *value = (char *)&input[++idx];
223 for (; idx < input_len; idx++) {
224 if ((input[idx] == '"')) {
225 *value_len = (uint32_t)(idx - (*value - input));
226 return STATE_SUCCESS;
227 }
228 }
229 } else if (input[idx] == '{' || input[idx] == '[') {
230 char start = input[idx];
231 char end = (start == '{') ? ('}') : (']');
232 uint8_t count = 0;
233 *value = (char *)&input[idx];
234 for (; idx < input_len; idx++) {
235 if ((input[idx] == start)) {
236 count++;
237 } else if ((input[idx] == end)) {
238 if (--count == 0) {
239 *value_len = (uint32_t)(idx - (*value - input) + 1);
240 return STATE_SUCCESS;
241 }
242 }
243 }
244 } else {
245 *value = (char *)&input[idx];
246 for (; idx < input_len; idx++) {
247 if ((input[idx] == ',' || input[idx] == ']' || input[idx] == '}')) {
248 *value_len = (uint32_t)(idx - (*value - input));
249 return STATE_SUCCESS;
250 }
251 }
252 }
253 }
254 }
255
256 return STATE_USER_INPUT_JSON_PARSE_FAILED;
257 }
258
core_utc2date(uint64_t utc,int8_t zone,core_date_t * date)259 int32_t core_utc2date(uint64_t utc, int8_t zone, core_date_t *date)
260 {
261 uint32_t day_sec = 0, day_num = 0;
262 uint64_t utc_zone_s = 0;
263
264 #define DAYS_IN_YEAR(year) (IS_LEAP_YEAR(year) ? 366 : 365)
265 #define IS_LEAP_YEAR(year) (!((year) % 400) || (((year) % 100) && !((year) % 4)))
266 #define DAYS_IN_MONTH(lpyr, mon) ((mon == 1)?(28 + lpyr):((mon > 6)?(((mon - 1) & 1)?(30):(31)):((mon & 1)?(30):(31))))
267
268 date->msec = utc % 1000;
269 utc_zone_s = (utc / 1000) + (zone * 60 * 60);
270
271 day_sec = utc_zone_s % (24 * 60 * 60);
272 day_num = utc_zone_s / (24 * 60 * 60);
273
274 date->sec = day_sec % 60;
275 date->min = (day_sec % 3600) / 60;
276 date->hour = day_sec / 3600;
277
278 date->year = 1970;
279 while (day_num >= DAYS_IN_YEAR(date->year)) {
280 day_num -= DAYS_IN_YEAR(date->year);
281 date->year++;
282 }
283
284 date->mon = 0;
285 while (day_num >= DAYS_IN_MONTH(IS_LEAP_YEAR(date->year), date->mon)) {
286 day_num -= DAYS_IN_MONTH(IS_LEAP_YEAR(date->year), date->mon);
287 date->mon++;
288 }
289 date->mon++;
290 date->day = day_num + 1;
291
292 return 0;
293 }
294
295 #if 0
296 int32_t core_strcat(aiot_sysdep_portfile_t *sysdep, char **dest, char *src1, char *src2, char *module_name)
297 {
298 uint32_t dest_len = 0;
299 char *tmp_dest = NULL;
300
301 dest_len = strlen(src1) + strlen(src2) + 1;
302 tmp_dest = sysdep->core_sysdep_malloc(dest_len, module_name);
303 if (tmp_dest == NULL) {
304 return STATE_SYS_DEPEND_MALLOC_FAILED;
305 }
306 memset(tmp_dest, 0, dest_len);
307 memcpy(tmp_dest + strlen(tmp_dest), src1, strlen(src1));
308 memcpy(tmp_dest + strlen(tmp_dest), src2, strlen(src2));
309
310 if (*dest != NULL) {
311 sysdep->core_sysdep_free(*dest);
312 *dest = NULL;
313 }
314
315 *dest = tmp_dest;
316
317 return STATE_SUCCESS;
318 }
319 #endif
320
321