-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilx9.c
1918 lines (1647 loc) · 37.4 KB
/
utilx9.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (C) 2017 - 2020, Lanka Hsu, <lankahsu@gmail.com>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "utilx9.h"
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define DBG_TMP_Y(format,args...) //DBG_LN_Y(format, ## args)
#define DBG_TMP_DUMP(ibuf,len,delim,format,args...) //DBG_TR_DUMP(ibuf,len,delim,format,## args)
#ifdef UTIL_EX_DBG
int dbg_more = DBG_LVL_INFO;//DBG_LVL_INFO;
int dbg_lvl_round(void)
{
dbg_more++;
dbg_more %= DBG_LVL_MAX;
return dbg_more;
}
int dbg_lvl_set(int lvl)
{
dbg_more = lvl;
dbg_more %= DBG_LVL_MAX;
return dbg_more;
}
int dbg_lvl_get(void)
{
return dbg_more;
}
#else
int dbg_lvl_get(void)
{
return 0;
}
#endif
#ifdef UTIL_EX_SAFE
void* pcheck(void* a)
{
return a ;
}
int select_ex(int fd, fd_set *fdrset_ptr, fd_set *fdwset_ptr, fd_set *fdeset_ptr, int msec, int usec)
{
int maxfd = fd+1;
struct timeval *tv_ptr = NULL;
struct timeval timeout = {0,0};
SAFE_FD_SET_EX(fd, fdrset_ptr);
SAFE_FD_SET_EX(fd, fdwset_ptr);
SAFE_FD_SET_EX(fd, fdeset_ptr);
if ((msec>0) || (usec>0))
{
timeout.tv_sec = msec /1000;
timeout.tv_usec = (msec % 1000) * 1000 + usec;
//DBG_ER_LN("(tv_sec: %ld, tv_usec: %ld)", timeout.tv_sec , timeout.tv_usec);
tv_ptr = &timeout;
}
else
{
// block
}
return SAFE_SELECT(maxfd, fdrset_ptr, fdwset_ptr, fdeset_ptr, tv_ptr);
}
#endif
#ifdef UTIL_EX_BASIC
#include <fcntl.h>
#if (0)
int str_isspace(char *str)
{
char *s=" \n\t\r";
if (SAFE_STRCHR(s,str))
{
return 1;
}
else
{
return 0;
}
return 0;
}
#endif
char *version_show(void)
{
static char buff[LEN_OF_VAL64] = "";
if (SAFE_STRLEN(buff) == 0)
{
time_t bootup_t = time(NULL);
SAFE_SPRINTF_EX(buff, "0x%08X, %s, %s, %s, %ld", LIBUTILX9_VERSION, PJ_REVISION, PJ_BUILDNO, PJ_BUILDER, bootup_t);
}
return buff;
}
int system_ex(char *fmt, ...)
{
va_list vargs;
char *cmd = NULL;
int rc = 0;
int ret = -1;
va_start(vargs, fmt);
ret = vasprintf(&cmd, fmt, vargs);
va_end(vargs);
if (ret<0)
{
DBG_ER_LN("vasprintf error !!!");
}
if (cmd)
{
DBG_DB_LN("(cmd: %s)", cmd);
rc = system(cmd);
SAFE_FREE(cmd);
}
return rc;
}
char *str_cat_ex(char *str, ...)
{
if (str)
{
va_list vargs;
va_start(vargs, str);
char *s = va_arg(vargs, char*);
while (s)
{
strcat(str, s);
s = va_arg(vargs, char*);
}
va_end(vargs);
}
return str;
}
// 0: is number. -1: fail
int str_isnum(const char *str)
{
int ret = -1;
while (*str != '\0')
{
if ((*str < '0') || (*str > '9'))
{
return ret;
}
str++;
}
return 0;
}
char *str_rtrim(char *str)
{
if ((str == NULL) || (*str == '\0'))
{
return str;
}
int len = SAFE_STRLEN(str);
char *p = str + len - 1;
while ((p >= str) && (isspace(*p)))
{
*p = '\0';
--p;
}
return str;
}
char *str_ltrim(char *str)
{
if ((str == NULL) || (*str == '\0'))
{
return str;
}
int len = 0;
char *p = str;
while ((*p != '\0') && (isspace(*p)))
{
++p;
++len;
}
if (len>0)
{
SAFE_MEMMOVE(str, p, SAFE_STRLEN(str) - len + 1);
}
return str;
}
char *str_trim(char *str)
{
str = str_rtrim(str);
str = str_ltrim(str);
return str;
}
char *str_trim_char(char *str, const char *delim, int delim_len)
{
if ((str == NULL) || (*str == '\0'))
{
return str;
}
char *p = str;
while (*p != '\0')
{
//if (*p == trim)
if (SAFE_MEMCHR((char*)delim, *p, delim_len))
{
char *endp = NULL;
endp = p+SAFE_STRLEN(p+1);
SAFE_MEMMOVE(p, p+1, SAFE_STRLEN(p+1));
*endp = '\0';
}
else
{
p++;
}
}
return str;
}
void str_toupper(char *str)
{
if (str)
{
int c = 0;
while (str[c] != '\0')
{
if (str[c] >= 'a' && str[c] <= 'z')
{
str[c] = str[c] - 32;
}
c++;
}
}
}
void str_tolower(char *str)
{
if (str)
{
int c = 0;
while (str[c] != '\0')
{
if (str[c] >= 'A' && str[c] <= 'Z')
{
str[c] = str[c] + 32;
}
c++;
}
}
}
uint32_t byte2big_endian(uint8_t size, uint8_t *data)
{
uint32_t val =0;
switch (size)
{
case 1:
//cmd[4] = cfg_param_rec->param_val & 0xFF;
val = data[0];
break;
case 2:
//cmd[4] = (cfg_param_rec->param_val >> 8) & 0xFF;
//cmd[5] = cfg_param_rec->param_val & 0xFF;
val = ((unsigned int)data[0] << 8) | data[1];
break;
case 4:
//cmd[4] = (cfg_param_rec->param_val >> 24) & 0xFF;
//cmd[5] = (cfg_param_rec->param_val >> 16) & 0xFF;
//cmd[6] = (cfg_param_rec->param_val >> 8) & 0xFF;
//cmd[7] = cfg_param_rec->param_val & 0xFF;
val = ((unsigned int)data[0] << 24) | ((unsigned int)data[1] << 16) | ((unsigned int)data[2] << 8) | data[3];
break;
default:
break;
}
return val;
}
void big_endian2byte(uint8_t size, uint32_t val, uint8_t *data)
{
switch (size)
{
case 1:
data[0] = val & 0xFF;
break;
case 2:
data[0] = (val >> 8) & 0xFF;
data[1] = val & 0xFF;
break;
case 4:
data[0] = (val >> 24) & 0xFF;
data[1] = (val >> 16) & 0xFF;
data[2] = (val >> 8) & 0xFF;
data[3] = val & 0xFF;
break;
default:
break;
}
}
uint32_t byte2little_endian(uint8_t size, uint8_t *data)
{
uint32_t val =0;
switch (size)
{
case 1:
//cmd[4] = cfg_param_rec->param_val & 0xFF;
val = data[0];
break;
case 2:
//cmd[4] = (cfg_param_rec->param_val >> 8) & 0xFF;
//cmd[5] = cfg_param_rec->param_val & 0xFF;
val = ((unsigned int)data[1] << 8) | data[0];
break;
case 4:
//cmd[4] = (cfg_param_rec->param_val >> 24) & 0xFF;
//cmd[5] = (cfg_param_rec->param_val >> 16) & 0xFF;
//cmd[6] = (cfg_param_rec->param_val >> 8) & 0xFF;
//cmd[7] = cfg_param_rec->param_val & 0xFF;
val = ((unsigned int)data[3] << 24) | ((unsigned int)data[2] << 16) | ((unsigned int)data[1] << 8) | data[0];
break;
default:
break;
}
return val;
}
void little_endian2byte(uint8_t size, uint32_t val, uint8_t *data)
{
switch (size)
{
case 1:
data[0] = val & 0xFF;
break;
case 2:
data[1] = (val >> 8) & 0xFF;
data[0] = val & 0xFF;
break;
case 4:
data[3] = (val >> 24) & 0xFF;
data[2] = (val >> 16) & 0xFF;
data[1] = (val >> 8) & 0xFF;
data[0] = val & 0xFF;
break;
default:
break;
}
}
uint8_t buff_crc8_xor(uint8_t *start, uint8_t *buf, int len)
{
int i = 0;
int idx = 0;
uint8_t crc = 0;
if (start)
{
idx = 0;
crc = start[0];
}
else
{
idx = 1;
crc = buf[0];
}
for (i = idx; i < len; i++)
{
crc ^= buf[i];
}
return crc;
}
#define BB_LITTLE_ENDIAN 1
unsigned short buf_cksum(unsigned short *addr, int nleft)
{
/*
* Our algorithm is simple, using a 32 bit accumulator,
* we add sequential 16 bit words to it, and at the end, fold
* back all the carry bits from the top 16 bits into the lower
* 16 bits.
*/
unsigned sum = 0;
while (nleft > 1)
{
sum += *addr++;
nleft -= 2;
}
/* Mop up an odd byte, if necessary */
if (nleft == 1)
{
if (BB_LITTLE_ENDIAN)
{
sum += *(unsigned char*)addr;
}
else
{
sum += *(unsigned char*)addr << 8;
}
}
/* Add back carry outs from top 16 bits to low 16 bits */
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
return (unsigned short)~sum;
}
static void time_sec2day(int duration, int *day, int *hrs, int *min, int *sec)
{
*day = (duration / 86400);
duration = (duration % 86400);
*hrs = (duration / 3600);
duration = (duration % 3600);
*min = (duration / 60);
*sec = (duration % 60);
}
char *time_now_full(time_t now_t) // output: 2020-03-06 15:01:47
{
static char buff[LEN_OF_VAL128] = "";
//time_t now_t = time(NULL);
struct tm *timeinfo = localtime(&now_t);
SAFE_SPRINTF_EX(buff, "%04d-%02d-%02d %02d:%02d:%02d",
timeinfo->tm_year+1900, timeinfo->tm_mon+1, timeinfo->tm_mday,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
return buff;
}
char *time_now_short(time_t now_t) // output: 20200306 150147
{
static char buff[LEN_OF_VAL128] = "";
//time_t now_t = time(NULL);
struct tm *timeinfo = localtime(&now_t);
SAFE_SPRINTF_EX(buff, "%04d%02d%02d %02d%02d%02d",
timeinfo->tm_year+1900, timeinfo->tm_mon+1, timeinfo->tm_mday,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
return buff;
}
double time_diff_secs(time_t end_t, time_t start_t)
{
double diff_t = difftime(end_t, start_t);
return diff_t;
}
double time_diff_mins(time_t end_t, time_t start_t)
{
double diff_t = difftime(end_t, start_t);
diff_t = diff_t/60; //60
return diff_t;
}
double time_diff_hours(time_t end_t, time_t start_t)
{
double diff_t = difftime(end_t, start_t);
diff_t = diff_t/3600; //60*60
return diff_t;
}
double time_diff_days(time_t end_t, time_t start_t)
{
double diff_t = difftime(end_t, start_t);
diff_t = diff_t/86400; //60*60*24
return diff_t;
}
double time_diff_weeks(time_t end_t, time_t start_t)
{
double diff_t = difftime(end_t, start_t);
diff_t = diff_t/604800; //60*60*24*7
return diff_t;
}
#include <libgen.h>
int file_exe_chk(char *filename)
{
int ret = -1;
struct stat sb;
if ((stat(filename, &sb) == 0) && (sb.st_mode & S_IXUSR))
{
ret = 0;
}
return ret;
}
// 1: soft link
int file_slink(char *filename)
{
struct stat file_stat= {0};
if (lstat(filename, &file_stat) == -1)
{
return 0;
}
return S_ISLNK(file_stat.st_mode) ? 1 : 0;
}
int file_spath(char *filename, char *spath, int length)
{
int ret = -1;
if (file_slink(filename))
{
DBG_DB_LN("S_ISLNK !!!(filename: %s)", filename);
ret = readlink(filename, spath, length);
}
else
{
DBG_DB_LN("S_ISLNK not !!! (filename: %s)", filename);
}
return ret;
}
char *file_path(char *filename, char *actualpath)
{
#if (0)
char *dirc, *basec, *bname, *dname;
//char path[128] = "";//"/etc/passwd";
//SAFE_SPRINTF_EX(path, "/etc/%s", "../etc/passwd");
dirc = strdup(filename);
basec = strdup(filename);
dname = dirname(dirc);
bname = basename(basec);
DBG_IF_LN("(filename: %s, dirname: %s, basename: %s)", filename, dname, bname);
SAFE_FREE(dirc);
SAFE_FREE(basec);
#endif
char *ptr = realpath(filename, actualpath);
if (ptr)
{
DBG_IF_LN("(actualpath: %s)", actualpath);
}
else
{
DBG_IF_LN("%s not exist !!!", filename);
}
return ptr;
}
size_t file_append(char *filename, char *buf, int wantsize)
{
FILE *fp;
size_t writesize = 0;
//time_t t;
fp = SAFE_FOPEN(filename, "a+");
if (fp == NULL)
{
DBG_ER_LN("return, SAFE_FOPEN error !!! (%s, errno: %d %s)", filename, errno, strerror(errno));
return 0;
}
SAFE_FSEEK_END(fp, 0);
writesize = SAFE_FWRITE(buf, 1, wantsize, fp);
if (writesize <= 0)
{
DBG_ER_LN("SAFE_FWRITE error !!! (writesize: %zd, wantsize: %d)", writesize, wantsize);
}
SAFE_FCLOSE(fp);
return writesize;
}
size_t file_writer(char *filename, char *buf, int wantsize)
{
FILE *fp;
size_t writesize = 0;
//time_t t;
fp = SAFE_FOPEN(filename, "w");
if (fp == NULL)
{
DBG_ER_LN("return, SAFE_FOPEN error !!! (%s, errno: %d %s)", filename, errno, strerror(errno));
return 0;
}
writesize = SAFE_FWRITE(buf, 1, wantsize, fp);
if (writesize <= 0)
{
DBG_ER_LN("SAFE_FWRITE error !!! (writesize: %zd, wantsize: %d)", writesize, wantsize);
}
SAFE_FCLOSE(fp);
return writesize;
}
char *file_reader(char *filename, int *filesize)
{
FILE *fp;
int size = 0;
char *buf = NULL;
if (access(filename, F_OK) == -1)
{
DBG_ER_LN("return, %s not exist !!!", filename);
return NULL;
}
fp = SAFE_FOPEN(filename, "r");
if (fp == NULL)
{
DBG_ER_LN("return, SAFE_FOPEN error !!! (%s, errno: %d %s)", filename, errno, strerror(errno));
return NULL;
}
SAFE_FSEEK_END(fp, 0);
size = SAFE_FTELL(fp);
SAFE_FSEEK_SET(fp, 0);
if (size > 0)
{
buf = SAFE_CALLOC(1, size);
*filesize = SAFE_FREAD(buf, 1, size, fp);
}
SAFE_FCLOSE(fp);
return buf;
}
int file_copy(const char *file_from, const char *file_to)
{
int ret = 0;
int fd_to = -1;
int fd_from = -1;
char buf[4096];
ssize_t nread;
//int saved_errno;
if ((file_to == NULL) || (file_from == NULL))
{
DBG_ER_LN("file_to or file_from is NULL !!!");
ret = -1;
goto cp_exit;
}
DBG_IF_LN("(%s -> %s)", file_from, file_to);
SAFE_MEMSET(buf, 0, sizeof(buf));
fd_from = SAFE_OPEN((char *)file_from, O_RDONLY);
if (fd_from < 0)
{
DBG_ER_LN("SAFE_OPEN error !!! (%s)", file_from);
ret = -1;
goto cp_exit;
}
fd_to = SAFE_OPEN((char *)file_to, O_WRONLY | O_CREAT, 0666);
if (fd_to < 0)
{
DBG_ER_LN("SAFE_OPEN error !!! (%s)", file_to);
ret = -1;
goto cp_exit;
}
while ((nread = read(fd_from, buf, sizeof(buf))) > 0)
{
char *out_ptr = buf;
ssize_t nwritten;
do
{
nwritten = write(fd_to, out_ptr, nread);
if (nwritten >= 0)
{
nread -= nwritten;
out_ptr += nwritten;
}
else if (errno != EINTR)
{
ret = -1;
goto cp_exit;
}
} while (nread > 0);
}
cp_exit:
SAFE_FSYNC(fd_to);
SAFE_FSYNC(fd_from);
SAFE_CLOSE(fd_from);
SAFE_CLOSE(fd_to);
return ret;
}
void file_lookup(char *filename, newline_lookup_fn lookup_cb, void *arg)
{
if ((lookup_cb) && (filename) && (access(filename, F_OK) != -1))
{
char newline[LEN_OF_NEWLINE];
DBG_TR_LN("enter (%s)", filename);
FILE *fp = SAFE_FOPEN(filename, "r");
if (fp)
{
while (SAFE_FGETS(newline, sizeof(newline), fp)!= NULL)
{
if (lookup_cb(newline, arg) != 0)
{
break;
}
}
SAFE_FCLOSE(fp);
}
else
{
DBG_ER_LN("SAFE_FOPEN error !!! (%s, errno: %d %s)", filename, errno, strerror(errno));
}
}
else
{
DBG_ER_LN("filename or lookup_cb is NULL !!!");
}
}
void pfile_lookup(char *cmdline, newline_lookup_fn lookup_cb, void *arg)
{
if ((lookup_cb) && (cmdline) && (SAFE_STRLEN(cmdline) > 0))
{
char newline[LEN_OF_NEWLINE];
DBG_TR_LN("enter (%s)", cmdline);
FILE *fp = SAFE_POPEN(cmdline, "r");
if (fp)
{
while (SAFE_FGETS(newline, sizeof(newline), fp)!= NULL)
{
if (lookup_cb(newline, arg) != 0)
{
break;
}
}
SAFE_PCLOSE(arg);
}
else
{
DBG_ER_LN("SAFE_POPEN error !!! (%s)", cmdline);
}
}
else
{
DBG_ER_LN("cmdline or lookup_cb is NULL !!!");
}
}
char *os_random_uuid(char *buf, int buf_len)
{
static int srand_1st = 0;
if ((buf) && (buf_len>=LEN_OF_UUID))
{
const char *c = "89ab";
char *p = buf;
int n;
if (srand_1st == 0)
{
srand_1st ++;
srand(time(NULL) + getpid());
}
for (n = 0; n < 16; ++n)
{
int b = rand()%255;
switch (n)
{
case 6:
SAFE_SPRINTF(p, "4%X", b%15);
break;
case 8:
SAFE_SPRINTF(p, "%c%X", c[rand()%SAFE_STRLEN((char*)c)], b%15);
break;
default:
SAFE_SPRINTF(p, "%02X", b);
break;
}
p += 2;
switch (n)
{
case 3:
case 5:
case 7:
case 9:
*p++ = '-';
break;
}
}
*p = 0;
}
else
{
DBG_ER_LN("%s (buf: %p, buf_len: %d < %d)", DBG_TXT_WRONG, buf, buf_len, LEN_OF_UUID);
}
return buf;
}
char *os_urandom(int byte_count)
{
char *data = SAFE_CALLOC(1, byte_count);
FILE *fp = SAFE_FOPEN("/dev/urandom", "r");
if ((data) && (fp))
{
if (SAFE_FREAD(data, 1, byte_count, fp) > 0)
{
// got !!!
}
}
SAFE_FCLOSE(fp);
return data;
}
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/aes.h>
//#include <openssl/cmac.h>
//#include <openssl/err.h>
//#include <openssl/ssl.h>
#ifdef UTIL_EX_SSL
//#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#if (1)
// https://www.openssl.org/docs/manmaster/man7/migration_guide.html#Deprecated-low-level-encryption-functions
int sec_aes_cbc_encX(char *in, int in_len, char *out, unsigned char *evp_key, unsigned char*iv_key, int pad)
{
int ret = -1;
EVP_CIPHER_CTX *evp_ctx = NULL;
if (!in || !out)
{
goto exit_enc;
}
DBG_DB_LN("(evp_key: %s, iv_key: %s, pad: %d)", evp_key, iv_key, pad);
evp_ctx = EVP_CIPHER_CTX_new();
//EVP_CIPHER_CTX_set_key_length(evp_ctx, 16);
EVP_CIPHER_CTX_set_padding(evp_ctx, pad);
if ( EVP_EncryptInit_ex(evp_ctx, EVP_aes_128_cbc(), NULL, evp_key, iv_key) <= 0 )
{
DBG_ER_LN("EVP_EncryptInit_ex error !!!");
goto exit_enc;
}
int out_idx = 0;
if ( EVP_EncryptUpdate(evp_ctx, (unsigned char *)out, &out_idx, (unsigned char *)in, in_len) <= 0)
{
DBG_ER_LN("EVP_EncryptUpdate error !!!");
goto exit_enc;
}
int fin_len = 0;
if ( EVP_EncryptFinal_ex(evp_ctx, (unsigned char *)out + out_idx, &fin_len) <= 0)
{
DBG_ER_LN("EVP_EncryptFinal_ex error !!!");
goto exit_enc;
}
ret = out_idx+fin_len;
exit_enc:
if (evp_ctx)
{
EVP_CIPHER_CTX_free(evp_ctx);
}
return ret;
}
int sec_aes_cbc_decX(char *in, int in_len, char *out, unsigned char *evp_key, unsigned char*iv_key, int pad)
{
int ret = -1;
EVP_CIPHER_CTX *evp_ctx = NULL;
if (!in || !out)
{
goto exit_enc;
}
DBG_DB_LN("(evp_key: %s, iv_key: %s, pad: %d)", evp_key, iv_key, pad);
evp_ctx = EVP_CIPHER_CTX_new();
//EVP_CIPHER_CTX_set_key_length(evp_ctx, 16);
EVP_CIPHER_CTX_set_padding(evp_ctx, pad);
if ( EVP_DecryptInit_ex(evp_ctx, EVP_aes_128_cbc(), NULL, evp_key, iv_key) <= 0 )
{
DBG_ER_LN("EVP_DecryptInit_ex error !!!");
goto exit_enc;
}
int out_idx = 0;
if ( EVP_DecryptUpdate(evp_ctx, (unsigned char *)out, &out_idx, (unsigned char *)in, in_len) <= 0)
{
DBG_ER_LN("EVP_DecryptUpdate error !!!");
goto exit_enc;
}
int fin_len = 0;
if ( EVP_DecryptFinal_ex(evp_ctx, (unsigned char *)out + out_idx, &fin_len) <= 0)
{
DBG_ER_LN("EVP_DecryptFinal_ex error !!!");
goto exit_enc;
}
ret = out_idx+fin_len;
exit_enc:
if (evp_ctx)
{
EVP_CIPHER_CTX_free(evp_ctx);
}
return ret;
}
#endif
int sec_aes_cbc_enc(char *in, int in_len, char *out, unsigned char *evp_key, unsigned char*iv_key)
{
int ret = 0;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
int pad = 1;
ret = sec_aes_cbc_encX(in, in_len, out, evp_key, iv_key, pad);
#else
char *in_pad = NULL;
AES_KEY aes;
int out_len = ((SAFE_STRLEN(in)/AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
int pad_num = out_len-in_len;
unsigned char evp_save[AES_BLOCK_SIZE];
unsigned char iv_save[AES_BLOCK_SIZE];
if (!in || !out)
{
ret = -1;
goto exit_enc;
}