sync with 0.8.6cvs17
[claws.git] / src / common / quoted-printable.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <ctype.h>
22
23 static gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
24 static void get_hex_str(gchar *out, guchar ch);
25
26 gint qp_decode_line(gchar *str)
27 {
28         gchar *inp = str, *outp = str;
29
30         while (*inp != '\0') {
31                 if (*inp == '=') {
32                         if (inp[1] && inp[2] &&
33                             get_hex_value(outp, inp[1], inp[2]) == TRUE) {
34                                 inp += 3;
35                         } else if (inp[1] == '\0' || isspace(inp[1])) {
36                                 /* soft line break */
37                                 break;
38                         } else {
39                                 /* broken QP string */
40                                 *outp = *inp++;
41                         }
42                 } else {
43                         *outp = *inp++;
44                 }
45                 outp++;
46         }
47
48         *outp = '\0';
49
50         return outp - str;
51 }
52
53 gint qp_decode_q_encoding(guchar *out, const gchar *in, gint inlen)
54 {
55         const gchar *inp = in;
56         guchar *outp = out;
57
58         if (inlen < 0)
59                 inlen = G_MAXINT;
60
61         while (inp - in < inlen && *inp != '\0') {
62                 if (*inp == '=' && inp + 3 - in <= inlen) {
63                         if (get_hex_value(outp, inp[1], inp[2]) == TRUE) {
64                                 inp += 3;
65                         } else {
66                                 *outp = *inp++;
67                         }
68                 } else if (*inp == '_') {
69                         *outp = ' ';
70                         inp++;
71                 } else {
72                         *outp = *inp++;
73                 }
74                 outp++;
75         }
76
77         *outp = '\0';
78
79         return outp - out;
80 }
81
82 gint qp_get_q_encoding_len(const guchar *str)
83 {
84         const guchar *inp = str;
85         gint len = 0;
86
87         while (*inp != '\0') {
88                 if (*inp == 0x20)
89                         len++;
90                 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
91                          *inp < 32 || *inp > 127 || isspace(*inp))
92                         len += 3;
93                 else
94                         len++;
95
96                 inp++;
97         }
98
99         return len;
100 }
101
102 void qp_q_encode(gchar *out, const guchar *in)
103 {
104         const guchar *inp = in;
105         gchar *outp = out;
106
107         while (*inp != '\0') {
108                 if (*inp == 0x20)
109                         *outp++ = '_';
110                 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
111                          *inp < 32 || *inp > 127 || isspace(*inp)) {
112                         *outp++ = '=';
113                         get_hex_str(outp, *inp);
114                         outp += 2;
115                 } else
116                         *outp++ = *inp;
117
118                 inp++;
119         }
120
121         *outp = '\0';
122 }
123
124 #define HEX_TO_INT(val, hex)                    \
125 {                                               \
126         gchar c = hex;                          \
127                                                 \
128         if ('0' <= c && c <= '9') {             \
129                 val = c - '0';                  \
130         } else if ('a' <= c && c <= 'f') {      \
131                 val = c - 'a' + 10;             \
132         } else if ('A' <= c && c <= 'F') {      \
133                 val = c - 'A' + 10;             \
134         } else {                                \
135                 val = -1;                       \
136         }                                       \
137 }
138
139 static gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
140 {
141         gint hi, lo;
142
143         HEX_TO_INT(hi, c1);
144         HEX_TO_INT(lo, c2);
145
146         if (hi == -1 || lo == -1)
147                 return FALSE;
148
149         *out = (hi << 4) + lo;
150         return TRUE;
151 }
152
153 #define INT_TO_HEX(hex, val)            \
154 {                                       \
155         if ((val) < 10)                 \
156                 hex = '0' + (val);      \
157         else                            \
158                 hex = 'A' + (val) - 10; \
159 }
160
161 static void get_hex_str(gchar *out, guchar ch)
162 {
163         gchar hex;
164
165         INT_TO_HEX(hex, ch >> 4);
166         *out++ = hex;
167         INT_TO_HEX(hex, ch & 0x0f);
168         *out++ = hex;
169 }