text
stringlengths
0
357
pstr++;
}
*pbuf = '\0';
return buf;
}
/**
* Returns a url-decoded version of the given string.
* The string may be encoded eg. by dc_urlencode().
* Belongs to RFC 3986: https://tools.ietf.org/html/rfc3986#section-2
*
* @param to_decode Null-terminated string to decode.
* @return The function returns a null-terminated UTF-8 string.
* The return value must be free() when no longer used.
* On memory allocation errors the program halts.
* On other errors, an empty string is returned.
*/
char* dc_urldecode(const char* to_decode)
{
const char *pstr = to_decode;
if (to_decode==NULL) {
return dc_strdup("");
}
char *buf = malloc(strlen(to_decode) + 1), *pbuf = buf;
if (buf==NULL) {
exit(50);
}
while (*pstr)
{
if (*pstr=='%') {
if (pstr[1] && pstr[2]) {
*pbuf++ = hex_2_int(pstr[1]) << 4 | hex_2_int(pstr[2]);
pstr += 2;
}
}
else if (*pstr=='+') {
*pbuf++ = ' ';
}
else {
*pbuf++ = *pstr;
}
pstr++;
}
*pbuf = '\0';
return buf;
}
/*******************************************************************************
* Encode/decode header words, RFC 2047
******************************************************************************/
#define DEF_INCOMING_CHARSET "iso-8859-1"
#define DEF_DISPLAY_CHARSET "utf-8"
#define MAX_IMF_LINE 666 /* see comment below */
static int to_be_quoted(const char * word, size_t size)
{
const char* cur = word;
size_t i = 0;
for (i = 0; i < size; i++)
{
switch (*cur)
{
case ',':
case ':':
case '!':
case '"':
case '#':
case '$':
case '@':
case '[':
case '\\':
case ']':
case '^':
case '`':
case '{':
case '|':
case '}':
case '~':
case '=':
case '?':
case '_':
return 1;
default:
if (((unsigned char)*cur) >= 128) {