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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
|
#include <string.h>
#include "String.h"
#include "FileUtils.h"
#include "MD5.h"
#include "main.h"
const char* g_szHTMLescapes[256] = {
"�", 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, "&", "'", // 30-39
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40-49
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50-59
"<", 0, ">", 0, 0, 0, 0, 0, 0, 0, // 60-69
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70-79
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-89
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 90-99
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 100-109
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 110-119
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 120-129
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 130-139
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 140-149
0, 0, 0, "™", 0, 0, 0, 0, 0, 0, // 150-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
};
inline unsigned char* CString::strnchr(const unsigned char* src, unsigned char c, unsigned int iMaxBytes, unsigned char* pFill, unsigned int* piCount) const {
for (unsigned int a = 0; a < iMaxBytes && *src; a++, src++) {
if (pFill) {
pFill[a] = *src;
}
if (*src == c) {
if (pFill) {
pFill[a +1] = 0;
}
if (piCount) {
*piCount = a;
}
return (unsigned char*) src;
}
}
if (pFill) {
*pFill = 0;
}
if (piCount) {
*piCount = 0;
}
return NULL;
}
int CString::CaseCmp(const CString& s) const {
return strcasecmp(c_str(), s.c_str());
}
int CString::StrCmp(const CString& s) const {
return strcmp(c_str(), s.c_str());
}
bool CString::WildCmp(const CString& sWild, const CString& sString) {
// Written by Jack Handy - jakkhandy@hotmail.com
const char *wild = sWild.c_str(), *CString = sString.c_str();
const char *cp = NULL, *mp = NULL;
while ((*CString) && (*wild != '*')) {
if ((*wild != *CString) && (*wild != '?')) {
return false;
}
wild++;
CString++;
}
while (*CString) {
if (*wild == '*') {
if (!*++wild) {
return true;
}
mp = wild;
cp = CString+1;
} else if ((*wild == *CString) || (*wild == '?')) {
wild++;
CString++;
} else {
wild = mp;
CString = cp++;
}
}
while (*wild == '*') {
wild++;
}
return (*wild == 0);
}
bool CString::WildCmp(const CString& sWild) const {
return CString::WildCmp(sWild, *this);
}
CString& CString::MakeUpper() {
for (unsigned int a = 0; a < length(); a++) {
char& c = (*this)[a];
c = toupper(c);
}
return *this;
}
CString& CString::MakeLower() {
for (unsigned int a = 0; a < length(); a++) {
char& c = (*this)[a];
c = tolower(c);
}
return *this;
}
CString CString::AsUpper() const {
CString sRet = *this;
for (unsigned int a = 0; a < length(); a++) {
char& c = sRet[a];
c = toupper(c);
}
return sRet;
}
CString CString::AsLower() const {
CString sRet = *this;
for (unsigned int a = 0; a < length(); a++) {
char& c = sRet[a];
c = tolower(c);
}
return sRet;
}
CString::EEscape CString::ToEscape(const CString& sEsc) {
if (sEsc.CaseCmp("ASCII") == 0) {
return EASCII;
} else if (sEsc.CaseCmp("HTML") == 0) {
return EHTML;
} else if (sEsc.CaseCmp("URL") == 0) {
return EURL;
} else if (sEsc.CaseCmp("SQL") == 0) {
return ESQL;
}
return EASCII;
}
CString CString::Escape_n(EEscape eFrom, EEscape eTo) const {
CString sRet;
const char szHex[] = "0123456789ABCDEF";
const unsigned char *pStart = (const unsigned char*) data();
const unsigned char *p = (const unsigned char*) data();
unsigned int iLength = length();
sRet.reserve(iLength *3);
unsigned char ch = 0;
unsigned int iMaxLen = (eFrom == EHTML) ? 20 : 0;
unsigned char pTmp[iMaxLen +1];
unsigned int iCounted = 0;
for (unsigned int a = 0; a < iLength; a++, p = pStart + a) {
switch (eFrom) {
case EHTML:
if ((*p == '&') && (strnchr((unsigned char*) p, ';', iMaxLen, pTmp, &iCounted))) {
if ((iCounted >= 3) && (pTmp[1] == '#')) {
// do XML and HTML a <
int base = 10;
if ((pTmp[2] & 0xDF) == 'X') {
base = 16;
}
char* endptr = NULL;
unsigned int b = strtol((const char*) (pTmp +2 + (base == 16)), &endptr, base);
if ( ( *endptr == ';' ) && ( b <= 255 ) )
{ // incase they do something like �
ch = b;
a += iCounted;
break;
}
}
for (unsigned int c = 0; c < 256; c++) {
if (strcmp(g_szHTMLescapes[c], (const char*) &pTmp) == 0) {
ch = c;
break;
}
}
if (ch > 0) {
a += iCounted;
} else {
ch = *p; // Not a valid escape, just record the &
}
} else {
ch = *p;
}
break;
case EASCII:
ch = *p;
break;
case EURL:
if (*p == '%' && (a +2) < iLength && isxdigit(*(p +1)) && isxdigit(*(p +2))) {
p++;
if (isdigit(*p)) {
ch = (*p - '0') << 4;
} else {
ch = (tolower(*p) - 'a' +10) << 4;
}
p++;
if (isdigit(*p)) {
ch |= (*p - '0');
} else {
ch |= (tolower(*p) - 'a' +10);
}
a += 2;
} else if (pStart[a] == '+') {
ch = ' ';
} else {
ch = *p;
}
break;
case ESQL:
if (*p != '\\' || iLength < (a +1)) {
ch = *p;
} else {
a++;
p++;
if (*p == 'n') {
ch = '\n';
} else if (*p == 'r') {
ch = '\r';
} else if (*p == '0') {
ch = '\0';
} else if (*p == 't') {
ch = '\t';
} else if (*p == 'b') {
ch = '\b';
} else {
ch = *p;
}
}
break;
}
switch (eTo) {
case EHTML:
if (g_szHTMLescapes[ch]) {
sRet += g_szHTMLescapes[ch];
} else {
sRet += ch;
}
break;
case EASCII:
sRet += ch;
break;
case EURL:
if (isalnum(ch) || ch == '_' || ch == '.' || ch == '-') {
sRet += ch;
} else if (ch == ' ') {
sRet += '+';
} else {
sRet += '%';
sRet += szHex[ch >> 4];
sRet += szHex[ch & 0xf];
}
break;
case ESQL:
if (ch == '\0') { sRet += '\\'; sRet += '0';
} else if (ch == '\n') { sRet += '\\'; sRet += 'n';
} else if (ch == '\t') { sRet += '\\'; sRet += 't';
} else if (ch == '\r') { sRet += '\\'; sRet += 'r';
} else if (ch == '\b') { sRet += '\\'; sRet += 'b';
} else if (ch == '\"') { sRet += '\\'; sRet += '\"';
} else if (ch == '\'') { sRet += '\\'; sRet += '\'';
} else if (ch == '\\') { sRet += '\\'; sRet += '\\';
} else { sRet += ch; }
break;
}
}
sRet.reserve(0);
return sRet;
}
CString CString::Escape_n(EEscape eTo) const {
return Escape_n(EASCII, eTo);
}
CString& CString::Escape(EEscape eFrom, EEscape eTo) {
return (*this = Escape_n(eFrom, eTo));
}
CString& CString::Escape(EEscape eTo) {
return (*this = Escape_n(eTo));
}
CString CString::Replace_n(const CString& sReplace, const CString& sWith, const CString& sLeft, const CString& sRight, bool bRemoveDelims) const {
CString sRet = *this;
CString::Replace(sRet, sReplace, sWith, sLeft, sRight, bRemoveDelims);
return sRet;
}
unsigned int CString::Replace(const CString& sReplace, const CString& sWith, const CString& sLeft, const CString& sRight, bool bRemoveDelims) {
return CString::Replace(*this, sReplace, sWith, sLeft, sRight, bRemoveDelims);
}
unsigned int CString::Replace(CString& sStr, const CString& sReplace, const CString& sWith, const CString& sLeft, const CString& sRight, bool bRemoveDelims) {
unsigned int uRet = 0;
CString sCopy = sStr;
sStr.clear();
unsigned int uReplaceWidth = sReplace.length();
unsigned int uLeftWidth = sLeft.length();
unsigned int uRightWidth = sRight.length();
const char* p = sCopy.c_str();
bool bInside = false;
while (*p) {
if (!bInside && uLeftWidth && strncmp(p, sLeft.c_str(), uLeftWidth) == 0) {
if (!bRemoveDelims) {
sStr += sLeft;
}
p += uLeftWidth -1;
bInside = true;
} else if (bInside && uRightWidth && strncmp(p, sRight.c_str(), uRightWidth) == 0) {
if (!bRemoveDelims) {
sStr += sRight;
}
p += uRightWidth -1;
bInside = false;
} else if (!bInside && strncmp(p, sReplace.c_str(), uReplaceWidth) == 0) {
sStr += sWith;
p += uReplaceWidth -1;
uRet++;
} else {
sStr.append(p, 1);
}
p++;
}
return uRet;
}
CString CString::Token(unsigned int uPos, bool bRest, const CString& sSep) const {
string sRet;
const char* p = c_str();
unsigned int uSepLen = sSep.length();
if (uSepLen) {
uSepLen--;
}
while (*p) {
if (uPos) {
if (strncmp(p, sSep.c_str(), sSep.length()) == 0) {
uPos--;
p += uSepLen;
}
} else {
if (strncmp(p, sSep.c_str(), sSep.length()) == 0) {
if (!bRest) {
return sRet;
}
}
sRet += *p;
}
p++;
}
return sRet;
}
CString CString::Ellipsize(unsigned int uLen) const {
if (uLen >= size()) {
return *this;
}
string sRet;
// @todo this looks suspect
if (uLen < 4) {
for (unsigned int a = 0; a < uLen; a++) {
sRet += ".";
}
return sRet;
}
sRet = substr(0, uLen -3) + "...";
return sRet;
}
CString CString::Left(unsigned int uCount) const {
uCount = (uCount > length()) ? length() : uCount;
return substr(0, uCount);
}
CString CString::Right(unsigned int uCount) const {
uCount = (uCount > length()) ? length() : uCount;
return substr(length() - uCount, uCount);
}
unsigned int CString::URLSplit(MCString& msRet) const {
msRet.clear();
VCString vsPairs;
Split("&", vsPairs);
for (size_t a = 0; a < vsPairs.size(); a++) {
const CString& sPair = vsPairs[a];
msRet[sPair.Token(0, false, "=").Escape(CString::EURL, CString::EASCII)] = sPair.Token(1, true, "=").Escape(CString::EURL, CString::EASCII);
}
return msRet.size();
}
unsigned int CString::Split(const CString& sDelim, VCString& vsRet, bool bAllowEmpty, const CString& sLeft, const CString& sRight) const {
vsRet.clear();
if (empty()) {
return 0;
}
CString sTmp;
bool bInside = false;
unsigned int uDelimLen = sDelim.length();
unsigned int uLeftLen = sLeft.length();
unsigned int uRightLen = sRight.length();
const char* p = c_str();
while (*p) {
if (uLeftLen && uRightLen && !bInside && strncasecmp(p, sLeft.c_str(), uLeftLen) == 0) {
p += uLeftLen;
bInside = true;
continue;
}
if (uLeftLen && uRightLen && bInside && strncasecmp(p, sRight.c_str(), uRightLen) == 0) {
p += uRightLen;
bInside = false;
continue;
}
if (uDelimLen && !bInside && strncasecmp(p, sDelim.c_str(), uDelimLen) == 0) {
vsRet.push_back(sTmp);
sTmp.clear();
p += uDelimLen;
if (!bAllowEmpty) {
while (strncasecmp(p, sDelim.c_str(), uDelimLen) == 0) {
p += uDelimLen;
}
}
bInside = false;
continue;
} else {
sTmp += *p;
}
p++;
}
if (!sTmp.empty()) {
vsRet.push_back(sTmp);
}
return vsRet.size();
/*vsRet.clear();
CString sTmp = *this;
while (sTmp.size()) {
CString sTok = sTmp.Token(0, false, sDelim);
CString sRest = sTmp.Token(1, true, sDelim);
if (bAllowEmpty || !sTok.empty()) {
vsRet.push_back(sTok);
}
if (bAllowEmpty && sRest.empty() && sTok.size() < sTmp.size()) {
vsRet.push_back("");
}
sTmp = sRest;
}
return vsRet.size();*/
}
unsigned int CString::Split(const CString& sDelim, SCString& ssRet, bool bAllowEmpty, const CString& sLeft, const CString& sRight) const {
VCString vsTokens;
Split(sDelim, vsTokens, bAllowEmpty, sLeft, sRight);
ssRet.clear();
for (size_t a = 0; a < vsTokens.size(); a++) {
ssRet.insert(vsTokens[a]);
}
return ssRet.size();
}
CString CString::Format(const CString& sFormatStr, ...) {
return "";
}
CString CString::RandomString(unsigned int uLength) {
CString sRet;
for (unsigned int a = 0; a < uLength; a++) {
sRet += (char) (rand() % 26) + 65;
}
return sRet;
}
bool CString::Base64Encode(unsigned int uWrap) {
CString sCopy(*this);
return sCopy.Base64Encode(*this, uWrap);
}
unsigned long CString::Base64Decode() {
CString sCopy(*this);
return sCopy.Base64Decode(*this);
}
CString CString::Base64Encode_n(unsigned int uWrap) const {
CString sRet;
Base64Encode(sRet, uWrap);
return sRet;
}
CString CString::Base64Decode_n() const {
CString sRet;
Base64Decode(sRet);
return sRet;
}
bool CString::Base64Encode(CString& sRet, unsigned int uWrap) const {
static char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
sRet.clear();
size_t len = size();
const unsigned char* input = (const unsigned char*) c_str();
unsigned char *output, *p;
size_t i = 0, mod = len % 3, toalloc;
toalloc = (len / 3) * 4 + (3 - mod) % 3 + 1 + 8;
if (uWrap) {
toalloc += len / 57;
if (len % 57) {
toalloc++;
}
}
if (toalloc < len) {
return 0;
}
//p = output = (unsigned char *)malloc(toalloc);
p = output = new unsigned char [toalloc];
if (!p) {
return false;
}
while (i < len - mod) {
*p++ = b64table[input[i++] >> 2];
*p++ = b64table[((input[i - 1] << 4) | (input[i] >> 4)) & 0x3f];
*p++ = b64table[((input[i] << 2) | (input[i + 1] >> 6)) & 0x3f];
*p++ = b64table[input[i + 1] & 0x3f];
i += 2;
if (uWrap && !(i % 57)) {
*p++ = '\n';
}
}
if (!mod) {
if (uWrap && i % 57) {
*p++ = '\n';
}
*p = 0;
sRet = (char*) output;
delete[] output;
return true;
} else {
*p++ = b64table[input[i++] >> 2];
*p++ = b64table[((input[i - 1] << 4) | (input[i] >> 4)) & 0x3f];
if (mod == 1) {
*p++ = '=';
*p++ = '=';
if (uWrap) {
*p++ = '\n';
}
*p = 0;
sRet = (char*) output;
delete[] output;
return true;
} else {
*p++ = b64table[(input[i] << 2) & 0x3f];
*p++ = '=';
if (uWrap) {
*p++ = '\n';
}
*p = 0;
sRet = (char*) output;
delete[] output;
return true;
}
}
sRet = (char*) output;
delete[] output;
return true;
}
unsigned long CString::Base64Decode(CString& sRet) const {
const char* in = c_str();
char c, c1, *p;
unsigned long i;
unsigned long uLen = size();
char* out = (char*) malloc(size() +1);
for (i = 0, p = out; i < uLen; i++) {
c = (char)base64_table[(unsigned char)in[i++]];
c1 = (char)base64_table[(unsigned char)in[i++]];
*p++ = (c << 2) | ((c1 >> 4) & 0x3);
if (i < uLen) {
if (in[i] == '=') {
break;
}
c = (char)base64_table[(unsigned char)in[i]];
*p++ = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
}
if (++i < uLen) {
if (in[i] == '=') {
break;
}
*p++ = ((c << 6) & 0xc0) | (char)base64_table[(unsigned char)in[i]];
}
}
*p = '\0';
unsigned long uRet = p - out;
sRet.clear();
sRet.append(out, uRet);
free(out);
return uRet;
}
CString CString::MD5() const {
return (const char*) CMD5(*this);
}
#ifdef HAVE_LIBSSL
CString CString::Encrypt_n(const CString& sPass, const CString& sIvec) {
CString sRet;
sRet.Encrypt(sPass, sIvec);
return sRet;
}
CString CString::Decrypt_n(const CString& sPass, const CString& sIvec) {
CString sRet;
sRet.Decrypt(sPass, sIvec);
return sRet;
}
void CString::Encrypt(const CString& sPass, const CString& sIvec) {
Crypt(sPass, true, sIvec);
}
void CString::Decrypt(const CString& sPass, const CString& sIvec) {
Crypt(sPass, false, sIvec);
}
void CString::Crypt(const CString& sPass, bool bEncrypt, const CString& sIvec) {
unsigned char szIvec[8] = {0,0,0,0,0,0,0,0};
BF_KEY bKey;
if (sIvec.length() >= 8) {
memcpy(szIvec, sIvec.data(), 8);
}
BF_set_key(&bKey, sPass.length(), (unsigned char*) sPass.data());
unsigned int uPad = (length() % 8);
if (uPad) {
uPad = 8 - uPad;
append(uPad, '\0');
}
size_t uLen = length();
unsigned char* szBuff = (unsigned char*) malloc(uLen);
BF_cbc_encrypt((const unsigned char*) data(), szBuff, uLen, &bKey, szIvec, ((bEncrypt) ? BF_ENCRYPT : BF_DECRYPT));
clear();
append((const char*) szBuff, uLen);
free(szBuff);
}
#endif // HAVE_LIBSSL
CString CString::ToString(char c) { stringstream s; s << c; return s.str(); }
CString CString::ToString(unsigned char c) { stringstream s; s << c; return s.str(); }
CString CString::ToString(short i) { stringstream s; s << i; return s.str(); }
CString CString::ToString(unsigned short i) { stringstream s; s << i; return s.str(); }
CString CString::ToString(int i) { stringstream s; s << i; return s.str(); }
CString CString::ToString(unsigned int i) { stringstream s; s << i; return s.str(); }
CString CString::ToString(long i) { stringstream s; s << i; return s.str(); }
CString CString::ToString(unsigned long i) { stringstream s; s << i; return s.str(); }
CString CString::ToString(long long i) { stringstream s; s << i; return s.str(); }
CString CString::ToString(unsigned long long i) { stringstream s; s << i; return s.str(); }
CString CString::ToString(double i) { stringstream s; s << i; return s.str(); }
CString CString::ToString(float i) { stringstream s; s << i; return s.str(); }
CString CString::ToPercent(double d) {
char szRet[32];
snprintf(szRet, 32, "%.02f%%", d);
return szRet;
}
CString CString::ToKBytes(double d) {
char szRet[32];
snprintf(szRet, 32, "%.0f K/s", d);
return szRet;
}
bool CString::ToBool() const { return (!Trim_n().Trim_n("0").empty() && Trim_n().CaseCmp("false") != 0); }
short CString::ToShort() const { return strtoul(this->c_str(), (char**) NULL, 10); }
unsigned short CString::ToUShort() const { return strtoul(this->c_str(), (char**) NULL, 10); }
unsigned int CString::ToUInt() const { return strtoul(this->c_str(), (char**) NULL, 10); }
int CString::ToInt() const { return strtoul(this->c_str(), (char**) NULL, 10); }
unsigned long CString::ToULong() const { return strtoul(c_str(), NULL, 10); }
unsigned long long CString::ToULongLong() const { return strtoull(c_str(), NULL, 10); }
long long CString::ToLongLong() const { return strtoll(c_str(), NULL, 10); }
double CString::ToDouble() const { return strtod(c_str(), NULL); }
bool CString::Trim(const CString& s) {
bool bLeft = TrimLeft(s);
return (TrimRight(s) || bLeft);
}
bool CString::TrimLeft(const CString& s) {
bool bRet = false;
while (length() && s.find(Left(1)) != CString::npos) {
LeftChomp();
bRet = true;
}
return bRet;
}
bool CString::TrimRight(const CString& s) {
bool bRet = false;
while (length() && s.find(Right(1)) != CString::npos) {
RightChomp();
bRet = true;
}
return bRet;
}
CString CString::Trim_n(const CString& s) const {
CString sRet = *this;
sRet.Trim(s);
return sRet;
}
CString CString::TrimLeft_n(const CString& s) const {
CString sRet = *this;
sRet.TrimLeft(s);
return sRet;
}
CString CString::TrimRight_n(const CString& s) const {
CString sRet = *this;
sRet.TrimRight(s);
return sRet;
}
CString CString::LeftChomp_n(unsigned int uLen) const {
CString sRet = *this;
sRet.LeftChomp(uLen);
return sRet;
}
CString CString::RightChomp_n(unsigned int uLen) const {
CString sRet = *this;
sRet.RightChomp(uLen);
return sRet;
}
bool CString::LeftChomp(unsigned int uLen) {
bool bRet = false;
while ((uLen--) && (length())) {
erase(0, 1);
bRet = true;
}
return bRet;
}
bool CString::RightChomp(unsigned int uLen) {
bool bRet = false;
while ((uLen--) && (length())) {
erase(length() -1);
bRet = true;
}
return bRet;
}
//////////////// MCString ////////////////
int MCString::WriteToDisk(const CString& sPath, mode_t iMode) {
CFile cFile(sPath);
if (!cFile.Open(O_WRONLY|O_CREAT|O_TRUNC, iMode)) {
return MCS_EOPEN;
}
for (MCString::iterator it = this->begin(); it != this->end(); it++) {
CString sKey = it->first;
CString sValue = it->second;
if (!WriteFilter(sKey, sValue)) {
return MCS_EWRITEFIL;
}
if (sKey.empty()) {
continue;
}
if (cFile.Write(Encode(sKey) + " " + Encode(sValue) + "\n") <= 0) {
return MCS_EWRITE;
}
}
cFile.Close();
return MCS_SUCCESS;
}
int MCString::ReadFromDisk(const CString& sPath, mode_t iMode) {
clear();
CFile cFile(sPath);
if (!cFile.Open(O_RDONLY|O_CREAT, iMode)) {
return MCS_EOPEN;
}
CString sBuffer;
while (cFile.ReadLine(sBuffer)) {
sBuffer.Trim();
CString sKey = sBuffer.Token(0);
CString sValue = sBuffer.Token(1);
Decode(sKey);
Decode(sValue);
if (!ReadFilter(sKey, sValue))
return MCS_EREADFIL;
(*this)[sKey] = sValue;
}
cFile.Close();
return MCS_SUCCESS;
}
static const char hexdigits[] = "0123456789abcdef";
CString& MCString::Encode(CString& sValue) {
CString sTmp;
for (CString::iterator it = sValue.begin(); it != sValue.end(); it++) {
if (isalnum(*it)) {
sTmp += *it;
} else {
sTmp += "%";
sTmp += hexdigits[*it >> 4];
sTmp += hexdigits[*it & 0xf];
sTmp += ";";
}
}
sValue = sTmp;
return sValue;
}
CString& MCString::Decode(CString& sValue) {
const char *pTmp = sValue.c_str();
char *endptr;
CString sTmp;
while(*pTmp) {
if (*pTmp != '%') {
sTmp += *pTmp++;
} else {
char ch = (char )strtol(((const char *)pTmp + 1), &endptr, 16);
if (*endptr == ';') {
sTmp += ch;
pTmp = ++endptr;
} else {
sTmp += *pTmp++;
}
}
}
sValue = sTmp;
return sValue;
}
|