-
Notifications
You must be signed in to change notification settings - Fork 31
/
scx_util.c
1197 lines (1083 loc) · 28.4 KB
/
scx_util.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <memory.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <sys/resource.h>
#include <errno.h>
#include <sys/stat.h>
#include <zlib.h>
//#include <ncapi.h>
//#include <trace.h>
#include "common.h"
#include "reqpool.h"
#include "vstring.h"
#include "scx_util.h"
volatile long long int g__scx_dynamic_memory_sum = 0LL; //전체 할당량
volatile long long int g__scx_adynamic_memory_sum = 0LL; //g__scx_dynamic_memory_sum + __scx_meminfo_t 헤더 크기
ContainerAllocator iCCLMalloc = { ccl_malloc, ccl_free, ccl_realloc, ccl_calloc };
#pragma pack(0)
typedef struct {
uint64_t magic;
uint64_t length;
#if USE_MEM_LIST_CNT
int line;
#endif
uint64_t *barrier; /* 할당된 메모리가 깨졌는지 체크할 때 사용 */
} __scx_meminfo_t;
#pragma pack()
#if USE_MEM_LIST_CNT
typedef struct {
uint64_t count;
uint64_t size;
char file[64];
} __scx_memtrace_t;
volatile __scx_memtrace_t __scx_line_count[10000] = {0};
#endif
#define __SCX_MAGIC 0x5AA5B33B
#define __SCX_MAGIC_FREE 0xB33B5AA5
#define __SCX_MAGIC_SIZE (sizeof(__scx_meminfo_t) + sizeof(long long))
#define SCX_ALLOC_ALIGN_UNIT (sizeof(long long))
#define SCX_ALLOC_ALIGN(_n) (((_n)+(SCX_ALLOC_ALIGN_UNIT-1)) & (~(SCX_ALLOC_ALIGN_UNIT-1)))
extern vstring_t *gscx__core_file_path; //core dump file이 생성될 위치를 지정한다
static inline void
__scx_set_barrier(__scx_meminfo_t *mp)
{
mp->barrier = (long long *)((char *)mp + sizeof(__scx_meminfo_t) + SCX_ALLOC_ALIGN(mp->length));
(*mp->barrier) = ~__SCX_MAGIC;
}
static inline int
__scx_check_overflow(__scx_meminfo_t *mp)
{
return (*mp->barrier == ~__SCX_MAGIC);
}
static inline void *
__scx_set_info(void *p, unsigned int l, const char *sf, int sl)
{
__scx_meminfo_t *mp = (__scx_meminfo_t *)p;
mp->magic = __SCX_MAGIC;
mp->length = l;
#if USE_MEM_LIST_CNT
mp->line = sl;
#endif
__scx_set_barrier(mp);
return (void *)((char *)p + sizeof(__scx_meminfo_t));
}
static inline unsigned int
__scx_set_free(void *p)
{
__scx_meminfo_t *mp = (__scx_meminfo_t *)((char *)p - sizeof(__scx_meminfo_t));
mp->magic = __SCX_MAGIC_FREE;
return mp->length;
}
static inline int
__scx_check_magic(void *p)
{
__scx_meminfo_t *mp = (__scx_meminfo_t *)((char *)p - sizeof(__scx_meminfo_t));
if (mp->magic != __SCX_MAGIC) {
if (__SCX_MAGIC_FREE == mp->magic || 0 == mp->magic ) {
TRACE((T_ERROR, "address %p, seems double free\n", p));
}
else {
TRACE((T_ERROR, "address %p, corrupt at topmost area of the allocated memory block.\n", p));
}
ASSERT(0);
}
if (!__scx_check_overflow(mp)) {
TRACE((T_ERROR, "address %p, corrupt beyond the bottom area of the allocated memory block.\n", p));
ASSERT(0);
}
// return (mp->magic == __SCX_MAGIC);
return 1;
}
static unsigned int
scx_get_len(void *p)
{
__scx_meminfo_t *mp = (__scx_meminfo_t *)((char *)p - sizeof(__scx_meminfo_t));
if (mp->magic != __SCX_MAGIC) abort();
return mp->length;
}
#if USE_MEM_LIST_CNT
static unsigned int
scx_get_line(void *p)
{
__scx_meminfo_t *mp = (__scx_meminfo_t *)((char *)p - sizeof(__scx_meminfo_t));
if (mp->magic != __SCX_MAGIC) abort();
return mp->line;
}
#endif
void
scx_add_dm(int asz, int sz)
{
// ATOMIC_ADD(g__scx_dynamic_memory_sum, sz); // 현재 사용하고 있는 부분이 없이 부하만 발생시키고 있어서 이부분은 사용하지 않는다.
ATOMIC_ADD(g__scx_adynamic_memory_sum, asz);
}
void
scx_sub_dm(int asz, int sz)
{
// ATOMIC_SUB(g__scx_dynamic_memory_sum, sz); // 현재 사용하고 있는 부분이 없이 부하만 발생시키고 있어서 이부분은 사용하지 않는다.
ATOMIC_SUB(g__scx_adynamic_memory_sum, asz);
}
/*
* clear가 1로 설정되면 calloc(), 이외에는 malloc()로 메모리 할당
*/
void *
scx_malloc(size_t n, int clear, const char *sf, int sl)
{
#if USE_NC_ALLOCATOR
#if USE_MEM_TRACE
int nsz = SCX_ALLOC_ALIGN(n+__SCX_MAGIC_SIZE + 1);
__scx_meminfo_t *mp;
mp = (__scx_meminfo_t *)__nc_malloc(nsz, sf, sl);
scx_add_dm(nsz, n);
#if USE_MEM_LIST_CNT
if (sl < 10000) {
if (ATOMIC_ADD(__scx_line_count[sl].count, 1) == 1)
snprintf(__scx_line_count[sl].file, 64, "%s", sf);
ATOMIC_ADD(__scx_line_count[sl].size, n);
}
#endif /* end of USE_MEM_LIST_CNT */
return __scx_set_info((void *)mp, n, sf, sl);
#else
return __nc_malloc(n, sf, sl);
#endif /* end of USE_MEM_TRACE */
#else
int nsz = SCX_ALLOC_ALIGN(n+__SCX_MAGIC_SIZE + 1);
__scx_meminfo_t *mp;
if (1 == clear) {
mp = (__scx_meminfo_t *)calloc(1, nsz);
}
else {
mp = (__scx_meminfo_t *)malloc(nsz);
}
// TRACE((T_INFO, "****total %llu, %s , size = %d, alloced = %d\n", __sync_add_and_fetch(&g__scx_adynamic_memory_sum, 0), sf, n, nsz));
ASSERT(mp);
scx_add_dm(nsz, n);
#if USE_MEM_LIST_CNT
if (sl < 10000) {
if (ATOMIC_ADD(__scx_line_count[sl].count, 1) == 1)
snprintf(__scx_line_count[sl].file, 64, "%s", sf);
ATOMIC_ADD(__scx_line_count[sl].size, n);
}
#endif /* end of USE_MEM_LIST_CNT */
return __scx_set_info((void *)mp, n, sf, sl);
#endif /* end of USE_NC_ALLOCATOR */
}
void *
scx_calloc(size_t elcount, size_t elsize, const char *sf, int sl)
{
return scx_malloc(elcount*elsize, 1, sf, sl);
}
void *
scx_realloc(void * old_, size_t reqsiz, const char *sf, int sl)
{
#if USE_NC_ALLOCATOR
#if USE_MEM_TRACE
void *new_ = (void *)scx_malloc(reqsiz, 0, sf, sl);
unsigned int olds = 0;
if (old_) {
olds = scx_get_len(old_);
memcpy(new_, old_, min(olds, reqsiz));
scx_free(old_, sf, sl);
}
return new_;
#else
return __nc_realloc(old_, reqsiz, sf, sl);
#endif /* end of USE_MEM_TRACE */
#else
void *new_ = (void *)scx_malloc(reqsiz, 0, sf, sl);
unsigned int olds = 0;
if (old_) {
olds = scx_get_len(old_);
memcpy(new_, old_, min(olds, reqsiz));
scx_free(old_, sf, sl);
}
return new_;
#endif /* end of USE_NC_ALLOCATOR */
}
void
scx_free(void *p, const char *sf, int lno)
{
#if USE_NC_ALLOCATOR
#if USE_MEM_TRACE
if (p) {
if (__scx_check_magic(p)) {
size_t ol = 0;
ol = scx_get_len(p);
scx_sub_dm(SCX_ALLOC_ALIGN(ol+__SCX_MAGIC_SIZE + 1), ol);
#if USE_MEM_LIST_CNT
int sl = scx_get_line(p);
if (sl < 10000) {
ATOMIC_SUB(__scx_line_count[sl].count, 1);
ATOMIC_SUB(__scx_line_count[sl].size, ol);
}
#endif /* end of USE_MEM_LIST_CNT */
__scx_set_free(p);
// TRACE((T_INFO, "****total %llu, %s , size =%d, freed = %d\n", __sync_add_and_fetch(&g__scx_adynamic_memory_sum, 0), sf, ol, SCX_ALLOC_ALIGN(ol+__SCX_MAGIC_SIZE + 1)));
__nc_free((char *)p - sizeof(__scx_meminfo_t), sf, lno);
}
else {
__scx_meminfo_t *mp = (__scx_meminfo_t *)((char *)p - sizeof(__scx_meminfo_t));
TRACE((T_ERROR, "address %p, seems double free at %d@%s(freed_magic=%d)\n", p, lno, sf, (int)(mp->magic == __SCX_MAGIC_FREE)));
ASSERT(0);
//TRAP;
}
}
#else
__nc_free(p, sf, lno);
#endif /* end of USE_MEM_TRACE */
#else
if (p) {
if (__scx_check_magic(p)) {
size_t ol = 0;
ol = scx_get_len(p);
scx_sub_dm(SCX_ALLOC_ALIGN(ol+__SCX_MAGIC_SIZE + 1), ol);
#if USE_MEM_LIST_CNT
int sl = scx_get_line(p);
if (sl < 10000) {
ATOMIC_SUB(__scx_line_count[sl].count, 1);
ATOMIC_SUB(__scx_line_count[sl].size, ol);
}
#endif /* end of USE_MEM_LIST_CNT */
__scx_set_free(p);
// TRACE((T_INFO, "****total %llu, %s , size =%d, freed = %d\n", __sync_add_and_fetch(&g__scx_adynamic_memory_sum, 0), sf, ol, SCX_ALLOC_ALIGN(ol+__SCX_MAGIC_SIZE + 1)));
free((char *)p - sizeof(__scx_meminfo_t));
}
else {
__scx_meminfo_t *mp = (__scx_meminfo_t *)((char *)p - sizeof(__scx_meminfo_t));
TRACE((T_ERROR, "address %p, seems double free at %d@%s(freed_magic=%d)\n", p, lno, sf, (int)(mp->magic == __SCX_MAGIC_FREE)));
ASSERT(0);
}
}
#endif /* end of USE_NC_ALLOCATOR */
}
char *
scx_strdup(const char *s, const char *sf, int sl)
{
#if USE_NC_ALLOCATOR
#if USE_MEM_TRACE
char *n = (char *)scx_malloc(strlen(s)+1, 1, sf, sl);
strcpy(n, s);
return n;
#else
return __nc_strdup(s, sf, sl);
#endif /* end of USE_MEM_TRACE */
#else
char *n = (char *)scx_malloc(strlen(s)+1, 1, sf, sl);
strcpy(n, s);
return n;
#endif /* end of USE_NC_ALLOCATOR */
}
char *
scx_strndup(const char *s, size_t n, const char *sf, int sl)
{
#if USE_NC_ALLOCATOR
#if USE_MEM_TRACE
char *news = (char *)scx_malloc(n+1, 1, sf, sl);
strncpy(news, s, n);
return news;
#else
#endif /* end of USE_MEM_TRACE */
return __nc_strndup(s, n, sf, sl);
#else
char *news = (char *)scx_malloc(n+1, 1, sf, sl);
strncpy(news, s, n);
return news;
#endif /* end of USE_NC_ALLOCATOR */
}
void
scx_used_memory_list()
{
#if USE_MEM_LIST_CNT
int i = 0;
uint64_t count = 0;
uint64_t size = 0;
for(i = 0; i < 10000; i++) {
count = ATOMIC_VAL(__scx_line_count[i].count);
size = ATOMIC_VAL(__scx_line_count[i].size);
if(count > 0)
TRACE((T_INFO, "source line = %d, count = %lld, size = %lld, file = %s\n", i, count, size, __scx_line_count[i].file));
}
#endif
}
void *
scx_dict_malloc(size_t size)
{
return SCX_CALLOC(1, size);
}
void
scx_dict_free(void *buf)
{
SCX_FREE(buf);
}
//core 파일 on/off 설정 가능하게 변경
void
set_resource_limits()
{
struct rlimit rlim;
#if 0
//현재 netcache core의 nc_init()에서 core dump를 막고 있기 때문에 이부분이 동작하지 않는다.
getrlimit(RLIMIT_CORE, &rlim);
if(gscx__core_file_path)
{
rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
//rlim.rlim_cur = rlim.rlim_max = 1024*1024*100;
chdir(vs_data(gscx__core_file_path)); // core file이 생성될 위치를 변경한다.
}
else
{
rlim.rlim_cur = rlim.rlim_max = 0;
}
if (setrlimit(RLIMIT_CORE, (const struct rlimit *)&rlim) == -1)
{
TRACE((T_ERROR, "setrlimit(RLIMIT_CORE) failed(%s).\n", strerror(errno)));
exit(0);
}
#endif
getrlimit(RLIMIT_NOFILE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 262144;
if (setrlimit(RLIMIT_NOFILE, (const struct rlimit *)&rlim) == -1)
{
TRACE((T_ERROR, "setrlimit(RLIMIT_NOFILE) failed(%s).\n",strerror(errno)));
// exit(0);
}
#if 0
rlim.rlim_cur = rlim.rlim_max = 8388608 * 2;
if (setrlimit(RLIMIT_STACK, (const struct rlimit *)&rlim) == -1)
{
TRACE((T_ERROR, "setrlimit(RLIMIT_STACK) failed(%s).\n", strerror(errno)));
exit(0);
}
#endif
}
/*
* IP Address 인지를 판별
* IP Address인 경우 1을 리턴
*/
int
is_valid_ip_address(char *ip)
{
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ip, &(sa.sin_addr));
return result != 0;
}
static pthread_rwlock_t __scx_clock_lock = PTHREAD_RWLOCK_INITIALIZER;
static volatile double __scx_time_usec = 0;
static volatile time_t __scx_time_sec = 0;
double
scx_get_cached_time_usec()
{
double t;
// pthread_rwlock_rdlock(&__scx_clock_lock);
t = __scx_time_usec;
// pthread_rwlock_unlock(&__scx_clock_lock);
return t;
}
time_t
scx_get_cached_time_sec()
{
time_t t_sec;
// pthread_rwlock_rdlock(&__scx_clock_lock);
t_sec = __scx_time_sec;
// pthread_rwlock_unlock(&__scx_clock_lock);
return t_sec;
}
/* 성능 측정을 위한 시간이외에는 가능하면 cache time을 사용하도록 해야 한다. */
double
scx_update_cached_time_usec()
{
struct timeval tv;
double t;
gettimeofday(&tv, NULL);
pthread_rwlock_wrlock(&__scx_clock_lock);
__scx_time_usec = (tv.tv_sec) * 1000000.0 + (tv.tv_usec);
t = __scx_time_usec;
__scx_time_sec = tv.tv_sec;
pthread_rwlock_unlock(&__scx_clock_lock);
return t;
}
time_t
scx_update_cached_time_sec()
{
struct timeval tv;
time_t t_sec;
gettimeofday(&tv, NULL);
pthread_rwlock_wrlock(&__scx_clock_lock);
__scx_time_usec = (tv.tv_sec) * 1000000.0 + (tv.tv_usec);
__scx_time_sec = tv.tv_sec;
t_sec = __scx_time_sec;
pthread_rwlock_unlock(&__scx_clock_lock);
return t_sec;
}
double
sx_get_time()
{
struct timeval tv;
double t;
gettimeofday(&tv, NULL);
t = (tv.tv_sec) * 1000000.0 + (tv.tv_usec);
return t;
}
int
scx_mkdir(const char *dir)
{
char tmp[1024];
char *p = NULL;
size_t len;
int ret = 0;
struct stat dirstat;
snprintf(tmp, sizeof(tmp),"%s",dir);
len = strlen(tmp);
if (tmp[len - 1] == '/')
tmp[len - 1] = 0;
for (p = tmp + 1; *p; p++)
if(*p == '/') {
*p = 0;
ret = stat(tmp, &dirstat);
if (ret < 0) {
ret = mkdir(tmp, S_IRWXU| S_IRWXG | S_IROTH | S_IXOTH);
if (ret < 0) return ret;
}
*p = '/';
}
ret = mkdir(tmp, S_IRWXU| S_IRWXG| S_IROTH | S_IXOTH);
return ret;
}
/*
* 지정된 디렉토리를 확인해보고
* 디렉토리가 없는 경우 새로 생성한다.
*/
int
scx_check_dir(const char *dir)
{
int i, ret;
struct stat dirstat;
ret = stat(dir, &dirstat);
if (ret < 0) {
ret = scx_mkdir(dir);
if (ret < 0) {
TRACE((T_INFO, "Failed to create directory(%s), reason(%s)\n", dir, strerror(errno)));
return -1;
}
else {
TRACE((T_INFO, "Create directory(%s)\n", dir));
}
}
else if (!S_ISDIR(dirstat.st_mode)) {
/* 디렉토리가 아닌 경우 에러를 리턴함 */
TRACE((T_ERROR, "'%s' is not directory.\n", dir));
return -1;
}
return 0;
}
void
hex2ascii(char *dest, char *src, int max)
{
char tmp[4]={0x0};
char **ptr=NULL,
*buf=dest;
*buf=0x0;
if(*src){
for(int i=0;src[i] && i < max;i+=2){
strncpy(tmp,&src[i],2);
sprintf(tmp,"%c",strtol(tmp,ptr,16) );
*buf++=tmp[0];
}
}
*buf=0x0;
}
char *
ascii2hex(char *dest, char *src, int len)
{
static char hex[] = "0123456789abcdef";
while (len--) {
*dest++ = hex[(*src & 0xf0) >> 4];
*dest++ = hex[*src++ & 0xf];
}
return dest;
}
#if 0
#define IS_ALNUM(ch) \
( ch >= 'a' && ch <= 'z' ) || \
( ch >= 'A' && ch <= 'Z' ) || \
( ch >= '0' && ch <= '9' ) || \
( ch >= '-' && ch <= '.' )
static char _x2c(char hex_up, char hex_low)
{
char digit;
digit = 16 * (hex_up >= 'A'
? ((hex_up & 0xdf) - 'A') + 10 : (hex_up - '0'));
digit += (hex_low >= 'A'
? ((hex_low & 0xdf) - 'A') + 10 : (hex_low - '0'));
return (digit);
}
size_t
scx_url_decode(char *str)
{
int i = 0, j = 0;
int len;
if (!str) return NULL;
len = strlen( str );
for (i = j = 0; j < len; i++, j++)
{
switch(str[j])
{
case '+':
str[i] = ' ';
break;
case '%':
if (IS_ALNUM(str[i+1]) && IS_ALNUM(str[i+2]) && j < (len - 2)) {
str[i] = _x2c(str[j + 1], str[j + 2]);
j += 2;
}
else {
str[i] = str[j];
}
break;
default:
str[i] = str[j];
break;
}
}
str[i]= '\0';
return strlen(str);
}
size_t
scx_url_decode_e (char *str)
{
return scx_url_decode(char *str);
}
#else
size_t
scx_url_decode (char *str)
{
char *rpos = str;
char *wpos = str;
char *end;
unsigned int num;
char buf3[3];
while ('\0' != *rpos)
{
switch (*rpos)
{
case '%':
if ( ('\0' == rpos[1]) ||
('\0' == rpos[2]) )
{
*wpos = '\0';
return wpos - str;
}
buf3[0] = rpos[1];
buf3[1] = rpos[2];
buf3[2] = '\0';
num = strtoul (buf3, &end, 16);
if ('\0' == *end)
{
*wpos = (char)((unsigned char) num);
wpos++;
rpos += 3;
break;
}
/* intentional fall through! */
default:
*wpos = *rpos;
wpos++;
rpos++;
}
}
*wpos = '\0'; /* add 0-terminator */
return wpos - str; /* = strlen(val) */
}
/*
* url decoding시 일부 문자는 decoding에서 제외
* 현재는 ?(%3F), #(%23)만 decoding에서 제외
* 관련 일감 : https://jarvis.solbox.com/redmine/issues/33636?
* scx_check_url_encoding()와 pair로 동작해야 한다.
*/
size_t
scx_url_decode_e (char *str)
{
char *rpos = str;
char *wpos = str;
char *end;
unsigned int num;
char buf3[3];
while ('\0' != *rpos)
{
switch (*rpos)
{
case '%':
if ( ('\0' == rpos[1]) ||
('\0' == rpos[2]) )
{
*wpos = '\0';
return wpos - str;
}
else if ( ('3' == rpos[1]) &&
(('F' == rpos[2]) || ('f' == rpos[2])) )
{ //?(%3F)
*wpos = *rpos;
wpos++;
rpos++;
continue;
}
else if ( ('2' == rpos[1]) && ('3' == rpos[2]) )
{ //#(%23)
*wpos = *rpos;
wpos++;
rpos++;
continue;
}
buf3[0] = rpos[1];
buf3[1] = rpos[2];
buf3[2] = '\0';
num = strtoul (buf3, &end, 16);
if ('\0' == *end)
{
*wpos = (char)((unsigned char) num);
wpos++;
rpos += 3;
break;
}
/* intentional fall through! */
default:
*wpos = *rpos;
wpos++;
rpos++;
}
}
*wpos = '\0'; /* add 0-terminator */
return wpos - str; /* = strlen(val) */
}
#endif
static const char scx_base64_chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char scx_base64_url_chars[] = /* url safe base64 code */
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
static const char scx_base64_digits[] = /* 표준 base64와 url safe base64를 모두 지원하도록 테이블 생성 */
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 62, 0, 62, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
0, 0, 0, -1, 0, 0, 0, 0, 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, 0, 0, 0, 0, 63, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int
scx_base64_decode_internal(char * decoded, const char* src)
{
size_t in_len = strlen (src);
char* dest = decoded;
if (in_len % 4)
{
/* Wrong base64 string length */
return SCX_NO;
}
while (*src) {
char a = scx_base64_digits[(unsigned char)*(src++)];
char b = scx_base64_digits[(unsigned char)*(src++)];
char c = scx_base64_digits[(unsigned char)*(src++)];
char d = scx_base64_digits[(unsigned char)*(src++)];
*(dest++) = (a << 2) | ((b & 0x30) >> 4);
if (c == (char)-1)
break;
*(dest++) = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2);
if (d == (char)-1)
break;
*(dest++) = ((c & 0x03) << 6) | d;
}
*dest = 0;
return SCX_YES;
}
/*
* url safe 형식도 같이 지원하기 위해서 내부에서 변환 처리를 한다.
*/
int
scx_base64_decode(char * decoded, const char* src)
{
int z, len;
char *input;
int ret = 0;
len = strlen(src);
z = 4 - (len % 4);
if (z > 0) {
input = alloca(len+z+1);
sprintf(input, "%s",src);
if (z < 4) {
while (z--)
input[len++] = '=';
}
input[len] = '\0';
}
else {
input = src;
}
ret = scx_base64_decode_internal(decoded, input);
return ret;
}
int
scx_encode_base64_internal(char *dst, char *src, const char *basis)
{
u_char *d, *s;
size_t len;
len = strlen(src);
s = src;
d = dst;
while (len > 2) {
*d++ = basis[(s[0] >> 2) & 0x3f];
*d++ = basis[((s[0] & 3) << 4) | (s[1] >> 4)];
*d++ = basis[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
*d++ = basis[s[2] & 0x3f];
s += 3;
len -= 3;
}
if (len) {
*d++ = basis[(s[0] >> 2) & 0x3f];
if (len == 1) {
*d++ = basis[(s[0] & 3) << 4];
*d++ = '=';
} else {
*d++ = basis[((s[0] & 3) << 4) | (s[1] >> 4)];
*d++ = basis[(s[1] & 0x0f) << 2];
}
*d++ = '=';
}
*d = 0;
return 0;
}
int
scx_encode_base64(char *dst, char *src) /* original base64 endcoding */
{
int ret = 0;
ret = scx_encode_base64_internal(dst,src, scx_base64_chars);
return ret;
}
int
scx_encode_base64_url(char *dst, char *src) /* URL safe base64 encoding */
{
int ret = 0;
ret = scx_encode_base64_internal(dst,src, scx_base64_url_chars);
return ret;
}
/*
* url에 ?, #이 두번 encoding이 된게 있는지 확인해서
* 한번 decoding 해준다.
* ? : %253F인경우 %3F로 변환
* # : %2523인경우 %23으로 변환
* 관련 일감 : https://jarvis.solbox.com/redmine/issues/33636
* scx_url_decode_e()와 pair로 동작
*/
int
scx_check_url_encoding(char *url)
{
char *rpos = url;
char *wpos = url;
size_t len;
while ('\0' != *rpos)
{
if (*rpos == '%')
{
if ( ('\0' != rpos[1]) &&
('\0' != rpos[2]) &&
('\0' != rpos[3]) &&
('\0' != rpos[4]) )
{
if ( ('2' == rpos[1]) &&
('5' == rpos[2]) &&
( ('3' == rpos[3]) &&
(('F' == rpos[4]) || ('f' == rpos[4]))
) ||
( ('2' == rpos[3]) &&
('3' == rpos[4])
)
)
{
// %253F나 %2523 경우 25를 뺀다.
*wpos = *rpos;
rpos++;
rpos++;
rpos++;
wpos++;
continue;
}
}
} // end of if (*rpos == '%')
if (*rpos != *wpos) {
*wpos = *rpos;
}
rpos++;
wpos++;
}
if (*wpos != '\0') {
*wpos = '\0';
}
return 0;
}
/*
* url에 ?, #가 인코딩된 %3F와 %23가 있는지 확인해서 해당 문자만 디코딩 한다.
* strm_file_open()에서 로컬 파일을 사용하는 경우 호출된다.
* 관련 일감 : https://jarvis.solbox.com/redmine/issues/33636
* scx_url_decode_e()와 pair로 동작
*/
int
scx_check_local_url_encoding(char *url)
{
char *rpos = url;
char *wpos = url;
size_t len;
while ('\0' != *rpos)
{
if (*rpos == '%')
{
if ( ('\0' != rpos[1]) &&
('\0' != rpos[2]))
{
if ( ('3' == rpos[1]) &&
(('F' == rpos[2]) || ('f' == rpos[2]))
) {
*wpos = '?';
rpos++;
rpos++;
rpos++;
wpos++;
continue;
}
else if ( ('2' == rpos[3]) &&
('3' == rpos[4])
) {
*wpos = '#';
rpos++;
rpos++;
rpos++;
wpos++;
continue;
}
}
} // end of if (*rpos == '%')
if (*rpos != *wpos) {
*wpos = *rpos;
}
rpos++;
wpos++;
}
if (*wpos != '\0') {
*wpos = '\0';
}
return 0;
}
unsigned int
scx_mix(unsigned int a, unsigned int b, unsigned int c)
{
a=a-b; a=a-c; a=a^(c >> 13);
b=b-c; b=b-a; b=b^(a << 8);
c=c-a; c=c-b; c=c^(b >> 13);
a=a-b; a=a-c; a=a^(c >> 12);
b=b-c; b=b-a; b=b^(a << 16);
c=c-a; c=c-b; c=c^(b >> 5);
a=a-b; a=a-c; a=a^(c >> 3);
b=b-c; b=b-a; b=b^(a << 10);
c=c-a; c=c-b; c=c^(b >> 15);
return c;
}
unsigned int
scx_hash(const void *p)
{
unsigned int hash = 2166136261U;
for (const uint8_t *ptr = p; *ptr;) {
hash = (hash ^ *ptr++) * 16777619U;
}
return hash;
}
/*
* 메모리 cache의 크기를 전체 물리 메모리 기준으로 일정 비율로 계산한다.
*/
int
scx_get_cache_size()
{
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
// uint64_t cache_size = pages * page_size / (1024 * 1024) / 2;
/* 전체 메모리의 40%로 계산 */
uint64_t cache_size = pages * page_size / 10 * 4 / (1024 * 1024 ) ;
return (int)cache_size;
}
extern char **environ;
static char **argv0;
static int argv_lth;