claws-mail/src/unmime.c

208 lines
4.5 KiB
C
Raw Normal View History

2001-04-19 14:21:46 +02:00
/*
2002-07-11 09:48:24 +02:00
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
* Copyright (C) 1999-2002 Hiroyuki Yamamoto
2001-04-19 14:21:46 +02:00
*
2002-07-11 09:48:24 +02:00
* 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 2 of the License, or
* (at your option) any later version.
2001-04-19 14:21:46 +02:00
*
2002-07-11 09:48:24 +02:00
* 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.
2001-04-19 14:21:46 +02:00
*
2002-07-11 09:48:24 +02:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2001-04-19 14:21:46 +02:00
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2002-07-11 09:48:24 +02:00
#include <glib.h>
2001-04-19 14:21:46 +02:00
#include <string.h>
#include <ctype.h>
2002-07-11 09:48:24 +02:00
#include "codeconv.h"
2001-04-19 14:21:46 +02:00
#include "base64.h"
2002-07-11 09:48:24 +02:00
#define ENCODED_WORD_BEGIN "=?"
#define ENCODED_WORD_END "?="
2001-04-19 14:21:46 +02:00
2002-07-11 09:48:24 +02:00
static gboolean get_hex_value(gchar *out, gchar c1, gchar c2);
2001-04-19 14:21:46 +02:00
2002-07-11 09:48:24 +02:00
/* Decodes headers based on RFC2045 and RFC2047. */
2001-04-19 14:21:46 +02:00
2002-07-11 09:48:24 +02:00
void unmime_header(gchar *out, const gchar *str)
2001-04-19 14:21:46 +02:00
{
2002-07-11 09:48:24 +02:00
const gchar *p = str;
gchar *outp = out;
const gchar *sp;
const gchar *eword_begin_p, *encoding_begin_p, *text_begin_p,
*eword_end_p;
2002-07-12 10:07:08 +02:00
gchar charset[32];
2002-07-11 09:48:24 +02:00
gchar encoding;
gchar *conv_str;
gint len;
while (*p != '\0') {
gchar *decoded_text = NULL;
eword_begin_p = strstr(p, ENCODED_WORD_BEGIN);
if (!eword_begin_p) {
strcpy(outp, p);
return;
}
encoding_begin_p = strchr(eword_begin_p + 2, '?');
if (!encoding_begin_p) {
strcpy(outp, p);
return;
}
text_begin_p = strchr(encoding_begin_p + 1, '?');
if (!text_begin_p) {
strcpy(outp, p);
return;
}
eword_end_p = strstr(text_begin_p + 1, ENCODED_WORD_END);
if (!eword_end_p) {
strcpy(outp, p);
return;
}
if (p == str) {
memcpy(outp, p, eword_begin_p - p);
outp += eword_begin_p - p;
p = eword_begin_p;
} else {
/* ignore spaces between encoded words */
for (sp = p; sp < eword_begin_p; sp++) {
if (!isspace(*sp)) {
memcpy(outp, p, eword_begin_p - p);
outp += eword_begin_p - p;
p = eword_begin_p;
break;
}
}
}
2002-07-12 10:07:08 +02:00
len = MIN(sizeof(charset) - 1,
encoding_begin_p - (eword_begin_p + 2));
memcpy(charset, eword_begin_p + 2, len);
charset[len] = '\0';
2002-07-11 09:48:24 +02:00
encoding = toupper(*(encoding_begin_p + 1));
if (encoding == 'B') {
decoded_text = g_malloc
(eword_end_p - (text_begin_p + 1) + 1);
2002-07-12 10:07:08 +02:00
len = base64_decode(decoded_text, text_begin_p + 1,
eword_end_p - (text_begin_p + 1));
2002-07-11 09:48:24 +02:00
decoded_text[len] = '\0';
} else if (encoding == 'Q') {
const gchar *ep = text_begin_p + 1;
gchar *dp;
dp = decoded_text = g_malloc(eword_end_p - ep + 1);
while (ep < eword_end_p) {
if (*ep == '=' && ep + 3 <= eword_end_p) {
if (get_hex_value(dp, ep[1], ep[2])
== TRUE) {
ep += 3;
} else {
*dp = *ep++;
}
} else if (*ep == '_') {
*dp = ' ';
ep++;
} else {
*dp = *ep++;
}
dp++;
}
*dp = '\0';
} else {
memcpy(outp, p, eword_end_p + 2 - p);
outp += eword_end_p + 2 - p;
p = eword_end_p + 2;
continue;
}
/* convert to locale encoding */
conv_str = conv_codeset_strdup(decoded_text, charset, NULL);
if (conv_str) {
len = strlen(conv_str);
memcpy(outp, conv_str, len);
g_free(conv_str);
} else {
len = strlen(decoded_text);
memcpy(outp, decoded_text, len);
}
outp += len;
g_free(decoded_text);
p = eword_end_p + 2;
2001-04-19 14:21:46 +02:00
}
2002-07-11 09:48:24 +02:00
*outp = '\0';
2001-04-19 14:21:46 +02:00
}
2002-07-11 09:48:24 +02:00
gint unmime_quoted_printable_line(gchar *str)
2001-04-19 14:21:46 +02:00
{
2002-07-11 09:48:24 +02:00
gchar *inp = str, *outp = str;
while (*inp != '\0') {
if (*inp == '=') {
if (inp[1] && inp[2] &&
get_hex_value(outp, inp[1], inp[2]) == TRUE) {
inp += 3;
} else if (inp[1] == '\0' || isspace(inp[1])) {
/* soft line break */
break;
} else {
/* broken QP string */
*outp = *inp++;
}
} else {
*outp = *inp++;
}
outp++;
2001-04-19 14:21:46 +02:00
}
2002-07-11 09:48:24 +02:00
*outp = '\0';
2001-04-19 14:21:46 +02:00
2002-07-11 09:48:24 +02:00
return outp - str;
2001-04-19 14:21:46 +02:00
}
2002-07-11 09:48:24 +02:00
#define HEX_TO_INT(val, hex) \
{ \
gchar c = hex; \
\
if ('0' <= c && c <= '9') { \
val = c - '0'; \
} else if ('a' <= c && c <= 'f') { \
val = c - 'a' + 10; \
} else if ('A' <= c && c <= 'F') { \
val = c - 'A' + 10; \
} else { \
val = -1; \
} \
}
2001-04-19 14:21:46 +02:00
2002-07-11 09:48:24 +02:00
static gboolean get_hex_value(gchar *out, gchar c1, gchar c2)
2001-04-19 14:21:46 +02:00
{
2002-07-11 09:48:24 +02:00
gint hi, lo;
2001-04-19 14:21:46 +02:00
2002-07-11 09:48:24 +02:00
HEX_TO_INT(hi, c1);
HEX_TO_INT(lo, c2);
2001-04-19 14:21:46 +02:00
2002-07-11 09:48:24 +02:00
if (hi == -1 || lo == -1)
return FALSE;
2001-04-19 14:21:46 +02:00
2002-07-11 09:48:24 +02:00
*out = (hi << 4) + lo;
return TRUE;
2001-04-19 14:21:46 +02:00
}