This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
/
Util.cpp
1977 lines (1758 loc) · 41.7 KB
/
Util.cpp
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
/*
* This file is part of nzbget
*
* Copyright (C) 2007-2015 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $Revision$
* $Date$
*
*/
#include "nzbget.h"
#include "Util.h"
#ifndef WIN32
// function "code_revision" is automatically generated in file "code_revision.cpp" on each build
const char* code_revision(void);
#endif
#ifdef WIN32
// getopt for WIN32:
// from http://www.codeproject.com/cpp/xgetopt.asp
// Original Author: Hans Dietrich (hdietrich2@hotmail.com)
// Released to public domain from author (thanks)
// Slightly modified by Andrey Prygunkov
char *optarg; // global argument pointer
int optind = 0; // global argv index
int getopt(int argc, char *argv[], char *optstring)
{
static char *next = nullptr;
if (optind == 0)
next = nullptr;
optarg = nullptr;
if (next == nullptr || *next == '\0')
{
if (optind == 0)
optind++;
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
{
optarg = nullptr;
if (optind < argc)
optarg = argv[optind];
return -1;
}
if (strcmp(argv[optind], "--") == 0)
{
optind++;
optarg = nullptr;
if (optind < argc)
optarg = argv[optind];
return -1;
}
next = argv[optind];
next++; // skip past -
optind++;
}
char c = *next++;
char *cp = strchr(optstring, c);
if (cp == nullptr || c == ':')
{
fprintf(stderr, "Invalid option %c", c);
return '?';
}
cp++;
if (*cp == ':')
{
if (*next != '\0')
{
optarg = next;
next = nullptr;
}
else if (optind < argc)
{
optarg = argv[optind];
optind++;
}
else
{
fprintf(stderr, "Option %c needs an argument", c);
return '?';
}
}
return c;
}
#endif
char Util::VersionRevisionBuf[100];
void Util::Init()
{
#ifndef WIN32
if ((strlen(code_revision()) > 0) && strstr(VERSION, "testing"))
{
snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s-r%s", VERSION, code_revision());
}
else
#endif
{
snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s", VERSION);
}
// init static vars there
GetCurrentTicks();
}
int64 Util::JoinInt64(uint32 Hi, uint32 Lo)
{
return (((int64)Hi) << 32) + Lo;
}
void Util::SplitInt64(int64 Int64, uint32* Hi, uint32* Lo)
{
*Hi = (uint32)(Int64 >> 32);
*Lo = (uint32)(Int64 & 0xFFFFFFFF);
}
/* Base64 decryption is taken from
* Article "BASE 64 Decoding and Encoding Class 2003" by Jan Raddatz
* http://www.codeguru.com/cpp/cpp/algorithms/article.php/c5099/
*/
const static char BASE64_DEALPHABET [128] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 9
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10 - 19
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 - 29
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30 - 39
0, 0, 0, 62, 0, 0, 0, 63, 52, 53, // 40 - 49
54, 55, 56, 57, 58, 59, 60, 61, 0, 0, // 50 - 59
0, 61, 0, 0, 0, 0, 1, 2, 3, 4, // 60 - 69
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70 - 79
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 80 - 89
25, 0, 0, 0, 0, 0, 0, 26, 27, 28, // 90 - 99
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100 - 109
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110 - 119
49, 50, 51, 0, 0, 0, 0, 0 // 120 - 127
};
uint32 DecodeByteQuartet(char* inputBuffer, char* outputBuffer)
{
uint32 buffer = 0;
if (inputBuffer[3] == '=')
{
if (inputBuffer[2] == '=')
{
buffer = (buffer | BASE64_DEALPHABET [(int)inputBuffer[0]]) << 6;
buffer = (buffer | BASE64_DEALPHABET [(int)inputBuffer[1]]) << 6;
buffer = buffer << 14;
outputBuffer [0] = (char)(buffer >> 24);
return 1;
}
else
{
buffer = (buffer | BASE64_DEALPHABET [(int)inputBuffer[0]]) << 6;
buffer = (buffer | BASE64_DEALPHABET [(int)inputBuffer[1]]) << 6;
buffer = (buffer | BASE64_DEALPHABET [(int)inputBuffer[2]]) << 6;
buffer = buffer << 8;
outputBuffer [0] = (char)(buffer >> 24);
outputBuffer [1] = (char)(buffer >> 16);
return 2;
}
}
else
{
buffer = (buffer | BASE64_DEALPHABET [(int)inputBuffer[0]]) << 6;
buffer = (buffer | BASE64_DEALPHABET [(int)inputBuffer[1]]) << 6;
buffer = (buffer | BASE64_DEALPHABET [(int)inputBuffer[2]]) << 6;
buffer = (buffer | BASE64_DEALPHABET [(int)inputBuffer[3]]) << 6;
buffer = buffer << 2;
outputBuffer [0] = (char)(buffer >> 24);
outputBuffer [1] = (char)(buffer >> 16);
outputBuffer [2] = (char)(buffer >> 8);
return 3;
}
return 0;
}
CString Util::FormatSize(int64 fileSize)
{
CString result;
if (fileSize > 1024 * 1024 * 1000)
{
result.Format("%.2f GB", (float)((float)fileSize / 1024 / 1024 / 1024));
}
else if (fileSize > 1024 * 1000)
{
result.Format("%.2f MB", (float)((float)fileSize / 1024 / 1024));
}
else if (fileSize > 1000)
{
result.Format("%.2f KB", (float)((float)fileSize / 1024));
}
else if (fileSize == 0)
{
result = "0 MB";
}
else
{
result.Format("%i B", (int)fileSize);
}
return result;
}
CString Util::FormatSpeed(int bytesPerSecond)
{
CString result;
if (bytesPerSecond >= 100 * 1024 * 1024)
{
result.Format("%i MB/s", bytesPerSecond / 1024 / 1024);
}
else if (bytesPerSecond >= 10 * 1024 * 1024)
{
result.Format("%0.1f MB/s", (float)bytesPerSecond / 1024.0 / 1024.0);
}
else if (bytesPerSecond >= 1024 * 1000)
{
result.Format("%0.2f MB/s", (float)bytesPerSecond / 1024.0 / 1024.0);
}
else
{
result.Format("%i KB/s", bytesPerSecond / 1024);
}
return result;
}
void Util::FormatTime(time_t timeSec, char* buffer, int bufsize)
{
#ifdef HAVE_CTIME_R_3
ctime_r(&timeSec, buffer, bufsize);
#else
ctime_r(&timeSec, buffer);
#endif
buffer[bufsize-1] = '\0';
// trim LF
buffer[strlen(buffer) - 1] = '\0';
}
CString Util::FormatTime(time_t timeSec)
{
CString result;
result.Reserve(50);
FormatTime(timeSec, result, 50);
return result;
}
bool Util::MatchFileExt(const char* filename, const char* extensionList, const char* listSeparator)
{
int filenameLen = strlen(filename);
Tokenizer tok(extensionList, listSeparator);
while (const char* ext = tok.Next())
{
int extLen = strlen(ext);
if (filenameLen >= extLen && !strcasecmp(ext, filename + filenameLen - extLen))
{
return true;
}
if (strchr(ext, '*') || strchr(ext, '?'))
{
WildMask mask(ext);
if (mask.Match(filename))
{
return true;
}
}
}
return false;
}
bool Util::SplitCommandLine(const char* commandLine, char*** argv)
{
int argCount = 0;
char buf[1024];
char* pszArgList[100];
uint32 len = 0;
bool escaping = false;
bool space = true;
for (const char* p = commandLine; ; p++)
{
if (*p)
{
const char c = *p;
if (escaping)
{
if (c == '\'')
{
if (p[1] == '\'' && len < sizeof(buf) - 1)
{
buf[len++] = c;
p++;
}
else
{
escaping = false;
space = true;
}
}
else if (len < sizeof(buf) - 1)
{
buf[len++] = c;
}
}
else
{
if (c == ' ')
{
space = true;
}
else if (c == '\'' && space)
{
escaping = true;
space = false;
}
else if (len < sizeof(buf) - 1)
{
buf[len++] = c;
space = false;
}
}
}
if ((space || !*p) && len > 0 && argCount < 100)
{
//add token
buf[len] = '\0';
if (argv)
{
pszArgList[argCount] = strdup(buf);
}
(argCount)++;
len = 0;
}
if (!*p)
{
break;
}
}
if (argv)
{
pszArgList[argCount] = nullptr;
*argv = (char**)malloc((argCount + 1) * sizeof(char*));
memcpy(*argv, pszArgList, sizeof(char*) * (argCount + 1));
}
return argCount > 0;
}
void Util::TrimRight(char* str)
{
char* end = str + strlen(str) - 1;
while (end >= str && (*end == '\n' || *end == '\r' || *end == ' ' || *end == '\t'))
{
*end = '\0';
end--;
}
}
char* Util::Trim(char* str)
{
TrimRight(str);
while (*str == '\n' || *str == '\r' || *str == ' ' || *str == '\t')
{
str++;
}
return str;
}
char* Util::ReduceStr(char* str, const char* from, const char* to)
{
int lenFrom = strlen(from);
int lenTo = strlen(to);
// assert(iLenTo < iLenFrom);
while (char* p = strstr(str, from))
{
const char* src = to;
while ((*p++ = *src++)) ;
src = --p - lenTo + lenFrom;
while ((*p++ = *src++)) ;
}
return str;
}
std::vector<CString> Util::SplitStr(const char* str, const char* separators)
{
std::vector<CString> result;
Tokenizer tok(str, separators);
while (const char* substr = tok.Next())
{
result.emplace_back(substr);
}
return result;
}
/* Calculate Hash using Bob Jenkins (1996) algorithm
* http://burtleburtle.net/bob/c/lookup2.c
*/
#define hashsize(n) ((uint32)1<<(n))
#define hashmask(n) (hashsize(n)-1)
#define mix(a,b,c) \
{ \
a -= b; a -= c; a ^= (c>>13); \
b -= c; b -= a; b ^= (a<<8); \
c -= a; c -= b; c ^= (b>>13); \
a -= b; a -= c; a ^= (c>>12); \
b -= c; b -= a; b ^= (a<<16); \
c -= a; c -= b; c ^= (b>>5); \
a -= b; a -= c; a ^= (c>>3); \
b -= c; b -= a; b ^= (a<<10); \
c -= a; c -= b; c ^= (b>>15); \
}
uint32 hash(register uint8 *k, register uint32 length, register uint32 initval)
// register uint8 *k; /* the key */
// register uint32 length; /* the length of the key */
// register uint32 initval; /* the previous hash, or an arbitrary value */
{
uint32 a,b,c,len;
/* Set up the internal state */
len = length;
a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
c = initval; /* the previous hash value */
/*---------------------------------------- handle most of the key */
while (len >= 12)
{
a += (k[0] +((uint32)k[1]<<8) +((uint32)k[2]<<16) +((uint32)k[3]<<24));
b += (k[4] +((uint32)k[5]<<8) +((uint32)k[6]<<16) +((uint32)k[7]<<24));
c += (k[8] +((uint32)k[9]<<8) +((uint32)k[10]<<16)+((uint32)k[11]<<24));
mix(a,b,c);
k += 12; len -= 12;
}
/*------------------------------------- handle the last 11 bytes */
c += length;
switch(len) /* all the case statements fall through */
{
case 11: c+=((uint32)k[10]<<24);
case 10: c+=((uint32)k[9]<<16);
case 9 : c+=((uint32)k[8]<<8);
/* the first byte of c is reserved for the length */
case 8 : b+=((uint32)k[7]<<24);
case 7 : b+=((uint32)k[6]<<16);
case 6 : b+=((uint32)k[5]<<8);
case 5 : b+=k[4];
case 4 : a+=((uint32)k[3]<<24);
case 3 : a+=((uint32)k[2]<<16);
case 2 : a+=((uint32)k[1]<<8);
case 1 : a+=k[0];
/* case 0: nothing left to add */
}
mix(a,b,c);
/*-------------------------------------------- report the result */
return c;
}
uint32 Util::HashBJ96(const char* buffer, int bufSize, uint32 initValue)
{
return (uint32)hash((uint8*)buffer, (uint32)bufSize, (uint32)initValue);
}
#ifdef WIN32
bool Util::RegReadStr(HKEY keyRoot, const char* keyName, const char* valueName, char* buffer, int* bufLen)
{
HKEY subKey;
if (!RegOpenKeyEx(keyRoot, keyName, 0, KEY_READ, &subKey))
{
DWORD retBytes = *bufLen;
LONG ret = RegQueryValueEx(subKey, valueName, nullptr, nullptr, (LPBYTE)buffer, &retBytes);
*bufLen = retBytes;
RegCloseKey(subKey);
return ret == 0;
}
return false;
}
#endif
time_t Util::CurrentTime()
{
return ::time(nullptr);
}
/* From boost */
inline int is_leap(int year)
{
if(year % 400 == 0)
return 1;
if(year % 100 == 0)
return 0;
if(year % 4 == 0)
return 1;
return 0;
}
inline int days_from_0(int year)
{
year--;
return 365 * year + (year / 400) - (year/100) + (year / 4);
}
inline int days_from_1970(int year)
{
static const int days_from_0_to_1970 = 719162; // days_from_0(1970);
return days_from_0(year) - days_from_0_to_1970;
}
inline int days_from_1jan(int year,int month,int day)
{
static const int days[2][12] =
{
{ 0,31,59,90,120,151,181,212,243,273,304,334},
{ 0,31,60,91,121,152,182,213,244,274,305,335}
};
return days[is_leap(year)][month-1] + day - 1;
}
inline time_t internal_timegm(tm const *t)
{
int year = t->tm_year + 1900;
int month = t->tm_mon;
if(month > 11)
{
year += month/12;
month %= 12;
}
else if(month < 0)
{
int years_diff = (-month + 11)/12;
year -= years_diff;
month+=12 * years_diff;
}
month++;
int day = t->tm_mday;
int day_of_year = days_from_1jan(year,month,day);
int days_since_epoch = days_from_1970(year) + day_of_year;
time_t seconds_in_day = 3600 * 24;
time_t result = seconds_in_day * days_since_epoch + 3600 * t->tm_hour + 60 * t->tm_min + t->tm_sec;
return result;
}
time_t Util::Timegm(tm const *t)
{
return internal_timegm(t);
}
// prevent PC from going to sleep
void Util::SetStandByMode(bool standBy)
{
#ifdef WIN32
SetThreadExecutionState((standBy ? 0 : ES_SYSTEM_REQUIRED) | ES_CONTINUOUS);
#endif
}
static uint32 crc32_tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
/* This is a modified version of chksum_crc() from
* crc32.c (http://www.koders.com/c/fid699AFE0A656F0022C9D6B9D1743E697B69CE5815.aspx)
* (c) 1999,2000 Krzysztof Dabrowski
* (c) 1999,2000 ElysiuM deeZine
*
* chksum_crc() -- to a given block, this one calculates the
* crc32-checksum until the length is
* reached. the crc32-checksum will be
* the result.
*/
uint32 Util::Crc32m(uint32 startCrc, uchar *block, uint32 length)
{
uint32 crc = startCrc;
for (uint32 i = 0; i < length; i++)
{
crc = ((crc >> 8) & 0x00FFFFFF) ^ crc32_tab[(crc ^ *block++) & 0xFF];
}
return crc;
}
uint32 Util::Crc32(uchar *block, uint32 length)
{
return Util::Crc32m(0xFFFFFFFF, block, length) ^ 0xFFFFFFFF;
}
/* From zlib/crc32.c (http://www.zlib.net/)
* Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
*/
#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
uint32 gf2_matrix_times(uint32 *mat, uint32 vec)
{
uint32 sum;
sum = 0;
while (vec) {
if (vec & 1)
sum ^= *mat;
vec >>= 1;
mat++;
}
return sum;
}
void gf2_matrix_square(uint32 *square, uint32 *mat)
{
int n;
for (n = 0; n < GF2_DIM; n++)
square[n] = gf2_matrix_times(mat, mat[n]);
}
uint32 Util::Crc32Combine(uint32 crc1, uint32 crc2, uint32 len2)
{
int n;
uint32 row;
uint32 even[GF2_DIM]; /* even-power-of-two zeros operator */
uint32 odd[GF2_DIM]; /* odd-power-of-two zeros operator */
/* degenerate case (also disallow negative lengths) */
if (len2 <= 0)
return crc1;
/* put operator for one zero bit in odd */
odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
row = 1;
for (n = 1; n < GF2_DIM; n++) {
odd[n] = row;
row <<= 1;
}
/* put operator for two zero bits in even */
gf2_matrix_square(even, odd);
/* put operator for four zero bits in odd */
gf2_matrix_square(odd, even);
/* apply len2 zeros to crc1 (first square will put the operator for one
zero byte, eight zero bits, in even) */
do {
/* apply zeros operator for this bit of len2 */
gf2_matrix_square(even, odd);
if (len2 & 1)
crc1 = gf2_matrix_times(even, crc1);
len2 >>= 1;
/* if no more bits set, then done */
if (len2 == 0)
break;
/* another iteration of the loop with odd and even swapped */
gf2_matrix_square(odd, even);
if (len2 & 1)
crc1 = gf2_matrix_times(odd, crc1);
len2 >>= 1;
/* if no more bits set, then done */
} while (len2 != 0);
/* return combined crc */
crc1 ^= crc2;
return crc1;
}
int Util::NumberOfCpuCores()
{
#ifdef WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#elif HAVE_SC_NPROCESSORS_ONLN
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
return -1;
}
int64 Util::GetCurrentTicks()
{
#ifdef WIN32
static int64 hz=0, hzo=0;
if (!hz)
{
QueryPerformanceFrequency((LARGE_INTEGER*)&hz);
QueryPerformanceCounter((LARGE_INTEGER*)&hzo);
}
int64 t;
QueryPerformanceCounter((LARGE_INTEGER*)&t);
return ((t-hzo)*1000000)/hz;
#else
timeval t;
gettimeofday(&t, nullptr);
return (int64)(t.tv_sec) * 1000000ll + (int64)(t.tv_usec);
#endif
}
uint32 WebUtil::DecodeBase64(char* inputBuffer, int inputBufferLength, char* outputBuffer)
{
uint32 InputBufferIndex = 0;
uint32 OutputBufferIndex = 0;
uint32 InputBufferLength = inputBufferLength > 0 ? inputBufferLength : strlen(inputBuffer);
char ByteQuartet [4];
int i = 0;
while (InputBufferIndex < InputBufferLength)
{
// Ignore all characters except the ones in BASE64_ALPHABET
if ((inputBuffer [InputBufferIndex] >= 48 && inputBuffer [InputBufferIndex] <= 57) ||
(inputBuffer [InputBufferIndex] >= 65 && inputBuffer [InputBufferIndex] <= 90) ||
(inputBuffer [InputBufferIndex] >= 97 && inputBuffer [InputBufferIndex] <= 122) ||
inputBuffer [InputBufferIndex] == '+' ||
inputBuffer [InputBufferIndex] == '/' ||
inputBuffer [InputBufferIndex] == '=')
{
ByteQuartet [i] = inputBuffer [InputBufferIndex];
i++;
}
InputBufferIndex++;
if (i == 4) {
OutputBufferIndex += DecodeByteQuartet(ByteQuartet, outputBuffer + OutputBufferIndex);
i = 0;
}
}
// OutputBufferIndex gives us the next position of the next decoded character
// inside our output buffer and thus represents the number of decoded characters
// in our buffer.
return OutputBufferIndex;
}
/* END - Base64
*/
CString WebUtil::XmlEncode(const char* raw)
{
// calculate the required outputstring-size based on number of xml-entities and their sizes
int reqSize = strlen(raw);
for (const char* p = raw; *p; p++)
{
uchar ch = *p;
switch (ch)
{
case '>':
case '<':
reqSize += 4;
break;
case '&':
reqSize += 5;
break;
case '\'':
case '\"':
reqSize += 6;
break;
default:
if (ch < 0x20 || ch >= 0x80)
{
reqSize += 10;
break;
}
}
}
CString result;
result.Reserve(reqSize);
// copy string
char* output = result;
for (const char* p = raw; ; p++)
{
uchar ch = *p;
switch (ch)
{
case '\0':
goto BreakLoop;
case '<':
strcpy(output, "<");
output += 4;
break;
case '>':
strcpy(output, ">");
output += 4;
break;
case '&':
strcpy(output, "&");
output += 5;
break;
case '\'':
strcpy(output, "'");
output += 6;
break;
case '\"':
strcpy(output, """);
output += 6;
break;
default:
if (ch < 0x20 || ch > 0x80)
{
uint32 cp = ch;
// decode utf8
if ((cp >> 5) == 0x6 && (p[1] & 0xc0) == 0x80)
{
// 2 bytes
if (!(ch = *++p)) goto BreakLoop; // read next char
cp = ((cp << 6) & 0x7ff) + (ch & 0x3f);
}
else if ((cp >> 4) == 0xe && (p[1] & 0xc0) == 0x80)
{
// 3 bytes
if (!(ch = *++p)) goto BreakLoop; // read next char
cp = ((cp << 12) & 0xffff) + ((ch << 6) & 0xfff);
if (!(ch = *++p)) goto BreakLoop; // read next char
cp += ch & 0x3f;
}
else if ((cp >> 3) == 0x1e && (p[1] & 0xc0) == 0x80)
{
// 4 bytes
if (!(ch = *++p)) goto BreakLoop; // read next char
cp = ((cp << 18) & 0x1fffff) + ((ch << 12) & 0x3ffff);
if (!(ch = *++p)) goto BreakLoop; // read next char
cp += (ch << 6) & 0xfff;
if (!(ch = *++p)) goto BreakLoop; // read next char
cp += ch & 0x3f;
}
// accept only valid XML 1.0 characters
if (cp == 0x9 || cp == 0xA || cp == 0xD ||
(0x20 <= cp && cp <= 0xD7FF) ||
(0xE000 <= cp && cp <= 0xFFFD) ||
(0x10000 <= cp && cp <= 0x10FFFF))
{
sprintf(output, "&#x%06x;", cp);
output += 10;
}
else
{
// replace invalid characters with dots
*output++ = '.';
}
}
else
{
*output++ = ch;
}
break;
}
}
BreakLoop:
*output = '\0';
return result;
}
void WebUtil::XmlDecode(char* raw)
{
char* output = raw;
for (char* p = raw;;)
{
switch (*p)
{
case '\0':
goto BreakLoop;
case '&':
{
p++;
if (!strncmp(p, "lt;", 3))
{
*output++ = '<';
p += 3;
}
else if (!strncmp(p, "gt;", 3))
{
*output++ = '>';
p += 3;
}
else if (!strncmp(p, "amp;", 4))
{
*output++ = '&';
p += 4;
}
else if (!strncmp(p, "apos;", 5))
{
*output++ = '\'';
p += 5;
}
else if (!strncmp(p, "quot;", 5))
{
*output++ = '\"';
p += 5;
}
else if (*p == '#')
{
int code = atoi((p++)+1);
while (strchr("0123456789;", *p)) p++;
*output++ = (char)code;
}
else
{
// unknown entity, keep as is
*output++ = *(p-1);
*output++ = *p++;
}
break;
}
default:
*output++ = *p++;
break;
}
}
BreakLoop: