| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /*
- ** Command & Conquer Generals(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** 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 3 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, see <http://www.gnu.org/licenses/>.
- */
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // (c) 2001-2003 Electronic Arts Inc. //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- // FILE: QuotedPrintable.cpp /////////////////////////////////////////////////////////
- // Author: Matt Campbell, February 2002
- // Description: Quoted-printable encode/decode
- ////////////////////////////////////////////////////////////////////////////
- #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
- #include "Common/QuotedPrintable.h"
- #define MAGIC_CHAR '_'
- // takes an integer and returns an ASCII representation
- static char intToHexDigit(int num)
- {
- if (num<0 || num >15) return '\0';
- if (num<10)
- {
- return '0' + num;
- }
- return 'A' + (num-10);
- }
- // convert an ASCII representation of a hex digit into the digit itself
- static int hexDigitToInt(char c)
- {
- if (c <= '9' && c >= '0') return (c - '0');
- if (c <= 'f' && c >= 'a') return (c - 'a' + 10);
- if (c <= 'F' && c >= 'A') return (c - 'A' + 10);
- return 0;
- }
- // Convert unicode strings into ascii quoted-printable strings
- AsciiString UnicodeStringToQuotedPrintable(UnicodeString original)
- {
- static char dest[1024];
- const char *src = (const char *)original.str();
- int i=0;
- while ( !(src[0]=='\0' && src[1]=='\0') && i<1021 )
- {
- if (!isalnum(*src))
- {
- dest[i++] = MAGIC_CHAR;
- dest[i++] = intToHexDigit((*src)>>4);
- dest[i++] = intToHexDigit((*src)&0xf);
- } else
- {
- dest[i++] = *src;
- }
- src ++;
- if (!isalnum(*src))
- {
- dest[i++] = MAGIC_CHAR;
- dest[i++] = intToHexDigit((*src)>>4);
- dest[i++] = intToHexDigit((*src)&0xf);
- }
- else
- {
- dest[i++] = *src;
- }
- src ++;
- }
- dest[i] = '\0';
- return dest;
- }
- // Convert ascii strings into ascii quoted-printable strings
- AsciiString AsciiStringToQuotedPrintable(AsciiString original)
- {
- static char dest[1024];
- const char *src = (const char *)original.str();
- int i=0;
- while ( src[0]!='\0' && i<1021 )
- {
- if (!isalnum(*src))
- {
- dest[i++] = MAGIC_CHAR;
- dest[i++] = intToHexDigit((*src)>>4);
- dest[i++] = intToHexDigit((*src)&0xf);
- } else
- {
- dest[i++] = *src;
- }
- src ++;
- }
- dest[i] = '\0';
- return dest;
- }
- // Convert ascii quoted-printable strings into unicode strings
- UnicodeString QuotedPrintableToUnicodeString(AsciiString original)
- {
- static unsigned short dest[1024];
- int i=0;
- unsigned char *c = (unsigned char *)dest;
- const unsigned char *src = (const unsigned char *)original.str();
- while (*src && i<1023)
- {
- if (*src == MAGIC_CHAR)
- {
- if (src[1] == '\0')
- {
- // string ends with MAGIC_CHAR
- break;
- }
- *c = hexDigitToInt(src[1]);
- src++;
- if (src[1] != '\0')
- {
- *c = *c<<4;
- *c = *c | hexDigitToInt(src[1]);
- src++;
- }
- }
- else
- {
- *c = *src;
- }
- src++;
- c++;
- }
- // Fixup odd-length strings
- if ((c-(unsigned char *)dest)%2)
- {
- // OK
- }
- else
- {
- *c = '\0';
- c++;
- }
- *c = 0;
- UnicodeString out(dest);
- return out;
- }
- // Convert ascii quoted-printable strings into ascii strings
- AsciiString QuotedPrintableToAsciiString(AsciiString original)
- {
- static unsigned char dest[1024];
- int i=0;
- unsigned char *c = (unsigned char *)dest;
- const unsigned char *src = (const unsigned char *)original.str();
- while (*src && i<1023)
- {
- if (*src == MAGIC_CHAR)
- {
- if (src[1] == '\0')
- {
- // string ends with MAGIC_CHAR
- break;
- }
- *c = hexDigitToInt(src[1]);
- src++;
- if (src[1] != '\0')
- {
- *c = *c<<4;
- *c = *c | hexDigitToInt(src[1]);
- src++;
- }
- }
- else
- {
- *c = *src;
- }
- src++;
- c++;
- }
- *c = 0;
- return AsciiString((const char *)dest);
- }
|