sync with 0.8.8cvs8
[claws.git] / src / common / quoted-printable.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 #define MAX_LINELEN     76
27
28 #define IS_LBREAK(p) \
29         (*(p) == '\0' || *(p) == '\n' || (*(p) == '\r' && *((p) + 1) == '\n'))
30
31 #define SOFT_LBREAK_IF_REQUIRED(n)                                      \
32         if (len + (n) > MAX_LINELEN ||                                  \
33             (len + (n) == MAX_LINELEN && (!IS_LBREAK(inp + 1)))) {      \
34                 *outp++ = '=';                                          \
35                 *outp++ = '\n';                                         \
36                 len = 0;                                                \
37         }
38
39 void qp_encode_line(gchar *out, const guchar *in)
40 {
41         const guchar *inp = in;
42         gchar *outp = out;
43         guchar ch;
44         gint len = 0;
45
46         while (*inp != '\0') {
47                 ch = *inp;
48
49                 if (IS_LBREAK(inp)) {
50                         *outp++ = '\n';
51                         len = 0;
52                         inp++;
53                 } else if (ch == '\t' || ch == ' ') {
54                         if (IS_LBREAK(inp + 1)) {
55                                 SOFT_LBREAK_IF_REQUIRED(3);
56                                 *outp++ = '=';
57                                 get_hex_str(outp, ch);
58                                 outp += 2;
59                                 len += 3;
60                                 inp++;
61                         } else {
62                                 SOFT_LBREAK_IF_REQUIRED(1);
63                                 *outp++ = *inp++;
64                                 len++;
65                         }
66                 } else if ((ch >= 33 && ch <= 60) || (ch >= 62 && ch <= 126)) {
67                         SOFT_LBREAK_IF_REQUIRED(1);
68                         *outp++ = *inp++;
69                         len++;
70                 } else {
71                         SOFT_LBREAK_IF_REQUIRED(3);
72                         *outp++ = '=';
73                         get_hex_str(outp, ch);
74                         outp += 2;
75                         len += 3;
76                         inp++;
77                 }
78         }
79
80         if (len > 0)
81                 *outp++ = '\n';
82
83         *outp = '\0';
84 }
85
86 gint qp_decode_line(gchar *str)
87 {
88         gchar *inp = str, *outp = str;
89
90         while (*inp != '\0') {
91                 if (*inp == '=') {
92                         if (inp[1] && inp[2] &&
93                             get_hex_value(outp, inp[1], inp[2]) == TRUE) {
94                                 inp += 3;
95                         } else if (inp[1] == '\0' || isspace(inp[1])) {
96                                 /* soft line break */
97                                 break;
98                         } else {
99                                 /* broken QP string */
100                                 *outp = *inp++;
101                         }
102                 } else {
103                         *outp = *inp++;
104                 }
105                 outp++;
106         }
107
108         *outp = '\0';
109
110         return outp - str;
111 }
112
113 gint qp_decode_q_encoding(guchar *out, const gchar *in, gint inlen)
114 {
115         const gchar *inp = in;
116         guchar *outp = out;
117
118         if (inlen < 0)
119                 inlen = G_MAXINT;
120
121         while (inp - in < inlen && *inp != '\0') {
122                 if (*inp == '=' && inp + 3 - in <= inlen) {
123                         if (get_hex_value(outp, inp[1], inp[2]) == TRUE) {
124                                 inp += 3;
125                         } else {
126                                 *outp = *inp++;
127                         }
128                 } else if (*inp == '_') {
129                         *outp = ' ';
130                         inp++;
131                 } else {
132                         *outp = *inp++;
133                 }
134                 outp++;
135         }
136
137         *outp = '\0';
138
139         return outp - out;
140 }
141
142 gint qp_get_q_encoding_len(const guchar *str)
143 {
144         const guchar *inp = str;
145         gint len = 0;
146
147         while (*inp != '\0') {
148                 if (*inp == 0x20)
149                         len++;
150                 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
151                          *inp < 32 || *inp > 127 || isspace(*inp))
152                         len += 3;
153                 else
154                         len++;
155
156                 inp++;
157         }
158
159         return len;
160 }
161
162 void qp_q_encode(gchar *out, const guchar *in)
163 {
164         const guchar *inp = in;
165         gchar *outp = out;
166
167         while (*inp != '\0') {
168                 if (*inp == 0x20)
169                         *outp++ = '_';
170                 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
171                          *inp < 32 || *inp > 127 || isspace(*inp)) {
172                         *outp++ = '=';
173                         get_hex_str(outp, *inp);
174                         outp += 2;
175                 } else
176                         *outp++ = *inp;
177
178                 inp++;
179         }
180
181         *outp = '\0';
182 }
183
184 #define HEX_TO_INT(val, hex)                    \
185 {                                               \
186         gchar c = hex;                          \
187                                                 \
188         if ('0' <= c && c <= '9') {             \
189                 val = c - '0';                  \
190         } else if ('a' <= c && c <= 'f') {      \
191                 val = c - 'a' + 10;             \
192         } else if ('A' <= c && c <= 'F') {      \
193                 val = c - 'A' + 10;             \
194         } else {                                \
195                 val = -1;                       \
196         }                                       \
197 }
198
199 static gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
200 {
201         gint hi, lo;
202
203         HEX_TO_INT(hi, c1);
204         HEX_TO_INT(lo, c2);
205
206         if (hi == -1 || lo == -1)
207                 return FALSE;
208
209         *out = (hi << 4) + lo;
210         return TRUE;
211 }
212
213 #define INT_TO_HEX(hex, val)            \
214 {                                       \
215         if ((val) < 10)                 \
216                 hex = '0' + (val);      \
217         else                            \
218                 hex = 'A' + (val) - 10; \
219 }
220
221 static void get_hex_str(gchar *out, guchar ch)
222 {
223         gchar hex;
224
225         INT_TO_HEX(hex, ch >> 4);
226         *out++ = hex;
227         INT_TO_HEX(hex, ch & 0x0f);
228         *out++ = hex;
229 }