text
stringlengths
0
357
else {
// convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX
bitbuf = 0;
bitcount = 0;
ucs4 = 0;
while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
++src;
bitbuf = (bitbuf << 6) | c;
bitcount += 6;
// enough bits for a UTF-16 character?
if (bitcount >= 16)
{
bitcount -= 16;
utf16 = (bitcount ? bitbuf >> bitcount : bitbuf) & 0xffff;
// convert UTF16 to UCS4
if (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT;
continue;
}
else if (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
ucs4 += utf16 - UTF16LOSTART + UTF16BASE;
}
else {
ucs4 = utf16;
}
// convert UTF-16 range of UCS4 to UTF-8
if (ucs4 <= 0x7fUL) {
dst[0] = ucs4;
dst += 1;
}
else if (ucs4 <= 0x7ffUL) {
dst[0] = 0xc0 | (ucs4 >> 6);
dst[1] = 0x80 | (ucs4 & 0x3f);
dst += 2;
}
else if (ucs4 <= 0xffffUL) {
dst[0] = 0xe0 | (ucs4 >> 12);
dst[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
dst[2] = 0x80 | (ucs4 & 0x3f);
dst += 3;
}
else {
dst[0] = 0xf0 | (ucs4 >> 18);
dst[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
dst[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
dst[3] = 0x80 | (ucs4 & 0x3f);
dst += 4;
}
}
}
// skip over trailing '-' in modified UTF-7 encoding
if (*src=='-') {
++src;
}
}
}
*dst = '\0';
return res;
}
/*******************************************************************************
* Encode/decode extended header, RFC 2231, RFC 5987
******************************************************************************/
/**
* Check if extended header format is needed for a given string.
*
* @param to_check Null-terminated UTF-8 string to check.
* @return 0=extended header encoding is not needed,
* 1=extended header encoding is needed,
* use dc_encode_ext_header() for this purpose.
*/
int dc_needs_ext_header(const char* to_check)
{
if (to_check) {
while (*to_check)
{
if (!isalnum(*to_check) && *to_check!='-' && *to_check!='_' && *to_check!='.' && *to_check!='~') {
return 1;
}
to_check++;
}
}
return 0;
}
/**
* Encode an UTF-8 string to the extended header format.
*
* Example: `Björn Petersen` gets encoded to `utf-8''Bj%C3%B6rn%20Petersen`
*