1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #include <stdlib.h>
6 #include <sys/time.h>
7 #include <time.h>
8 #include <signal.h>
9 #include <string.h>
10 #include "aos/kernel.h"
11 #include "ota_log.h"
12 #include "ota_hal_os.h"
13 
14 #ifdef OTA_LINUX
15 #include <unistd.h>
16 #include <pthread.h>
17 #include <sys/reboot.h>
18 #else
19 #include "aos/kernel.h"
20 #endif
21 
22 /*Memory realloc*/
ota_realloc(void * ptr,int size)23 void *ota_realloc(void *ptr, int size)
24 {
25 #if !defined OTA_LINUX
26     return aos_realloc(ptr, size);
27 #else
28     return realloc(ptr, size);
29 #endif
30 }
31 
32 /*Memory calloc*/
ota_calloc(int n,int size)33 void *ota_calloc(int n, int size)
34 {
35 #if !defined OTA_LINUX
36     return aos_calloc(n, size);
37 #else
38     return calloc(n, size);
39 #endif
40 }
41 
42 /*Reboot*/
ota_reboot(void)43 void ota_reboot(void)
44 {
45 #if !defined OTA_LINUX
46     aos_reboot();
47 #else
48     reboot(0x1234567);
49 #endif
50 }
51 
52 /*Memory malloc*/
ota_malloc(int size)53 void *ota_malloc(int size)
54 {
55 #if !defined OTA_LINUX
56     return aos_malloc(size);
57 #else
58     return malloc(size);
59 #endif
60 }
61 
62 /*Memory free*/
ota_free(void * ptr)63 void ota_free(void *ptr)
64 {
65 #if !defined OTA_LINUX
66     aos_free(ptr);
67 #else
68     free(ptr);
69 #endif
70 }
71 
72 /*Sleep ms*/
ota_msleep(int ms)73 void ota_msleep(int ms)
74 {
75 #if !defined OTA_LINUX
76     aos_msleep(ms);
77 #else
78     usleep(1000 * ms);
79 #endif
80 }
81 
ota_thread_create(void ** thread_handle,void * (* work_routine)(void *),void * arg,void * pm,int stack_size)82 int ota_thread_create(void **thread_handle, void *(*work_routine)(void *), void *arg, void *pm, int stack_size)
83 {
84     return 0;
85 }
86 
ota_thread_destroy(void * ptread)87 void ota_thread_destroy(void *ptread)
88 {
89     aos_task_exit(0);
90 }
91 
92 /*Strings*/
ota_to_capital(char * value,int len)93 int ota_to_capital(char *value, int len)
94 {
95     int i = 0;
96     int ret = -1;
97     if ((value != NULL) && (len > 0)) {
98         ret = 0;
99         for (; i < len; i++) {
100             if (*(value + i) >= 'a' && *(value + i) <= 'z') {
101                 *(value + i) -= 'a' - 'A';
102             }
103         }
104     }
105     return ret;
106 }
107 
ota_hex2str(char * dest_buf,const unsigned char * src_buf,unsigned int dest_len,unsigned int src_len)108 int ota_hex2str(char *dest_buf, const unsigned char *src_buf, unsigned int dest_len, unsigned int src_len)
109 {
110     int i = 0;
111     int ret = -1;
112     if ((dest_buf != NULL) && (src_buf != NULL) && (dest_len > 2 * src_len)) {
113         ret = 0;
114         memset(dest_buf, 0x00, dest_len);
115         for (i = 0; i < src_len; i++) {
116             ota_snprintf(dest_buf + i * 2, 2 + 1, "%02X", src_buf[i]);
117         }
118     }
119     return ret;
120 }
121 
ota_str2hex(const char * src,char * dest,unsigned int dest_len)122 int ota_str2hex(const char *src, char *dest, unsigned int dest_len)
123 {
124     int i, n = 0;
125     int ret = -1;
126     if ((src != NULL) && (dest != NULL) && (strlen(src) % 2 == 0) && (dest_len >= strlen(src) / 2)) {
127         ret = 0;
128         for (i = 0; src[i]; i += 2) {
129             if (src[i] >= 'A' && src[i] <= 'F') {
130                 dest[n] = src[i] - 'A' + 10;
131             } else {
132                 dest[n] = src[i] - '0';
133             }
134             if (src[i + 1] >= 'A' && src[i + 1] <= 'F') {
135                 dest[n] = (dest[n] << 4) | (src[i + 1] - 'A' + 10);
136             } else {
137                dest[n] = (dest[n] << 4) | (src[i + 1] - '0');
138             }
139             ++n;
140         }
141     }
142     return ret;
143 }
144 
145 /*CRC16*/
update_crc16(unsigned short crcIn,unsigned char byte)146 static unsigned short update_crc16(unsigned short crcIn, unsigned char byte)
147 {
148     unsigned int crc = crcIn;
149     unsigned int in = byte | 0x100;
150 
151     do {
152         crc <<= 1;
153         in <<= 1;
154         if (in & 0x100) {
155             ++crc;
156         }
157         if (crc & 0x10000) {
158             crc ^= 0x1021;
159         }
160     } while (!(in & 0x10000));
161     return crc & 0xffffu;
162 }
163 
ota_crc16_init(ota_crc16_ctx * inCtx)164 void ota_crc16_init(ota_crc16_ctx *inCtx)
165 {
166     inCtx->crc = 0;
167 }
168 
ota_crc16_update(ota_crc16_ctx * inCtx,const void * inSrc,unsigned int inLen)169 void ota_crc16_update(ota_crc16_ctx *inCtx, const void *inSrc, unsigned int inLen)
170 {
171     const unsigned char *src = (const unsigned char *) inSrc;
172     const unsigned char *srcEnd = src + inLen;
173     while (src < srcEnd) {
174         inCtx->crc = update_crc16(inCtx->crc, *src++);
175     }
176 }
177 
ota_crc16_final(ota_crc16_ctx * inCtx,unsigned short * outResult)178 void ota_crc16_final(ota_crc16_ctx *inCtx, unsigned short *outResult)
179 {
180     inCtx->crc = update_crc16(inCtx->crc, 0);
181     inCtx->crc = update_crc16(inCtx->crc, 0);
182     *outResult = inCtx->crc & 0xffffu;
183 }
184 
ota_get_data_crc16(const unsigned char * buf,unsigned int len)185 unsigned short ota_get_data_crc16(const unsigned char *buf, unsigned int len)
186 {
187     ota_crc16_ctx ctx;
188     unsigned short crc16 = 0xffff;
189     if ((buf != NULL) && (len > 0)) {
190         ota_crc16_init(&ctx);
191         ota_crc16_update(&ctx, buf, len);
192         ota_crc16_final(&ctx, &crc16);
193     }
194     return crc16;
195 }
196 
197 /*base64*/
198 static const unsigned char base64_dec_map[128] = {
199     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
200     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
201     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
202     127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
203     127, 127, 127,  62, 127, 127, 127,  63,  52,  53,
204      54,  55,  56,  57,  58,  59,  60,  61, 127, 127,
205     127,  64, 127, 127, 127,   0,   1,   2,   3,   4,
206       5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
207      15,  16,  17,  18,  19,  20,  21,  22,  23,  24,
208      25, 127, 127, 127, 127, 127, 127,  26,  27,  28,
209      29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
210      39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
211      49,  50,  51, 127, 127, 127, 127, 127
212 };
213 
ota_base64_decode(const unsigned char * src,unsigned int slen,unsigned char * dst,unsigned int * dlen)214 int ota_base64_decode(const unsigned char *src, unsigned int slen, unsigned char *dst, unsigned int *dlen)
215 {
216     unsigned int i, n;
217     unsigned int j, x;
218     unsigned char *p;
219 
220     for (i = n = j = 0; i < slen; i++) {
221         if ((slen - i) >= 2 &&
222             src[i] == '\r' && src[i + 1] == '\n')
223             continue;
224 
225         if (src[i] == '\n')
226             continue;
227 
228         if (src[i] == '=' && ++j > 2)
229             return -1;
230 
231         if (src[i] > 127 || base64_dec_map[src[i]] == 127)
232             return -1;
233 
234         if (base64_dec_map[src[i]] < 64 && j != 0)
235             return -1;
236 
237         n++;
238     }
239 
240     if (n == 0)
241         return 0;
242 
243     n = ((n * 6) + 7) >> 3;
244     n -= j;
245 
246     if (dst == 0 || *dlen < n) {
247         *dlen = n;
248         return -2;
249     }
250 
251     for (j = 3, n = x = 0, p = dst; i > 0; i--, src++) {
252         if (*src == '\r' || *src == '\n')
253             continue;
254 
255         j -= (base64_dec_map[*src] == 64);
256         x = (x << 6) | (base64_dec_map[*src] & 0x3F);
257 
258         if (++n == 4) {
259             n = 0;
260             if (j > 0)
261                 *p++ = (unsigned char)(x >> 16);
262             if (j > 1)
263                 *p++ = (unsigned char)(x >> 8);
264             if (j > 2)
265                 *p++ = (unsigned char)(x);
266         }
267     }
268 
269     *dlen = p - dst;
270     return 0;
271 }