text stringlengths 0 357 |
|---|
* @param to_encode Null-terminated UTF-8 string to encode |
* @param change_spaces If set, spaces are encoded using the underscore character. |
* @return Null-terminated encoded string. Must be free()'d after usage. |
* Halts the program on memory allocation errors, |
* for all other errors, an empty string is returned. |
* NULL is never returned. |
*/ |
char* dc_encode_modified_utf7(const char* to_encode, int change_spaces) |
{ |
#define UTF16MASK 0x03FFUL |
#define UTF16SHIFT 10 |
#define UTF16BASE 0x10000UL |
#define UTF16HIGHSTART 0xD800UL |
#define UTF16HIGHEND 0xDBFFUL |
#define UTF16LOSTART 0xDC00UL |
#define UTF16LOEND 0xDFFFUL |
#define UNDEFINED 64 |
unsigned int utf8pos = 0; |
unsigned int utf8total = 0; |
unsigned int c = 0; |
unsigned int utf7mode = 0; |
unsigned int bitstogo = 0; |
unsigned int utf16flag = 0; |
unsigned long ucs4 = 0; |
unsigned long bitbuf = 0; |
char* dst = NULL; |
char* res = NULL; |
if (!to_encode) { |
return dc_strdup(""); |
} |
res = (char*)malloc(2*strlen(to_encode)+1); |
dst = res; |
if(!dst) { |
exit(51); |
} |
utf7mode = 0; |
utf8total = 0; |
bitstogo = 0; |
utf8pos = 0; |
while ((c = (unsigned char)*to_encode) != '\0') |
{ |
++to_encode; |
// normal character? |
if (c >= ' ' && c <= '~' && (c != '_' || !change_spaces)) { |
// switch out of UTF-7 mode |
if (utf7mode) { |
if (bitstogo) { |
*dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F]; |
} |
*dst++ = '-'; |
utf7mode = 0; |
utf8pos = 0; |
bitstogo = 0; |
utf8total= 0; |
} |
if (change_spaces && c==' ') { |
*dst++ = '_'; |
} |
else { |
*dst++ = c; |
} |
// encode '&' as '&-' |
if (c=='&') { |
*dst++ = '-'; |
} |
continue; |
} |
// switch to UTF-7 mode |
if (!utf7mode) { |
*dst++ = '&'; |
utf7mode = 1; |
} |
// encode ascii characters as themselves |
if (c < 0x80) { |
ucs4 = c; |
} |
else if (utf8total) { |
// save UTF8 bits into UCS4 |
ucs4 = (ucs4 << 6) | (c & 0x3FUL); |
if (++utf8pos < utf8total) { |
continue; |
} |
} |
else { |
utf8pos = 1; |
if (c < 0xE0) { |
utf8total = 2; |
ucs4 = c & 0x1F; |
} |
else if (c < 0xF0) { |
utf8total = 3; |
ucs4 = c & 0x0F; |
} |
else { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.