2012-07-03 [mir] 3.8.1cvs2
[claws.git] / src / common / quoted-printable.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #include <glib.h>
21 #include <ctype.h>
22
23 #include "utils.h"
24
25
26 #define MAX_LINELEN     76
27
28 #define IS_LBREAK(p) \
29         ((p)[0] == '\n' ? 1 : ((p)[0] == '\r' && (p)[1] == '\n') ? 2 : 0)
30
31 gint qp_encode(gboolean text, gchar *out, const guchar *in, gint len)
32 {
33         /* counters of input/output characters */
34         gint inc = 0;
35         gint outc = 1; /* one character reserved for '=' soft line break */
36
37         while(inc < len) {
38                 /* allow literal linebreaks in text */
39                 if(text) {
40                         if(IS_LBREAK(in)) {
41                                 /* inserting linebreaks is the job of our caller */
42                                 g_assert(outc <= MAX_LINELEN);
43                                 *out = '\0';
44                                 return inc + IS_LBREAK(in);
45                         }
46                         if(IS_LBREAK(in+1)) {
47                                 /* free the reserved character since no softbreak
48                                  * will be needed after the current character */
49                                 outc--;
50                                 /* guard against whitespace before a literal linebreak */
51                                 if(*in == ' ' || *in == '\t') {
52                                         goto escape;
53                                 }
54                         }
55                 }
56                 if(*in == '=') {
57                         goto escape;
58                 }
59                 /* Cave: Whitespace is unconditionally output literally,
60                  * but according to the RFC it must not be output before a
61                  * linebreak. 
62                  * This requirement is obeyed by quoting all linebreaks
63                  * and therefore ending all lines with '='. */
64                 else if((*in >= ' ' && *in <= '~') || *in == '\t') {
65                         if(outc + 1 <= MAX_LINELEN) {
66                                 *out++ = *in++;
67                                 outc++;
68                                 inc++;
69                         }
70                         else break;
71                 }
72                 else {
73 escape:
74                         if(outc + 3 <= MAX_LINELEN) {
75                                 *out++ = '=';
76                                 outc++;
77                                 get_hex_str(out, *in);
78                                 out += 2;
79                                 outc += 2;
80                                 in++;
81                                 inc++;
82                         }
83                         else break;
84                 }
85         }
86         g_assert(outc <= MAX_LINELEN);
87         *out++ = '=';
88         *out = '\0';
89         return inc;
90 }
91
92 void qp_encode_line(gchar *out, const guchar *in) {
93         while (*in != '\0') {
94                 in += qp_encode(TRUE, out, in, strlen(in));
95
96                 while(*out != '\0') out++;
97                 *out++ = '\n';
98                 *out++ = '\0';
99         }
100 }
101
102 gint qp_decode_line(gchar *str)
103 {
104         gchar *inp = str, *outp = str;
105
106         while (*inp != '\0') {
107                 if (*inp == '=') {
108                         if (inp[1] && inp[2] &&
109                             get_hex_value((guchar *)outp, inp[1], inp[2])
110                             == TRUE) {
111                                 inp += 3;
112                         } else if (inp[1] == '\0' || g_ascii_isspace(inp[1])) {
113                                 /* soft line break */
114                                 break;
115                         } else {
116                                 /* broken QP string */
117                                 *outp = *inp++;
118                         }
119                 } else {
120                         *outp = *inp++;
121                 }
122                 outp++;
123         }
124
125         *outp = '\0';
126
127         return outp - str;
128 }
129
130 gint qp_decode_const(gchar *out, gint avail, const gchar *str)
131 {
132         const gchar *inp = str;
133         gchar *outp = out;
134
135         while (*inp != '\0' && avail > 0) {
136                 if (*inp == '=') {
137                         if (inp[1] && inp[2] &&
138                             get_hex_value((guchar *)outp, inp[1], inp[2])
139                             == TRUE) {
140                                 inp += 3;
141                         } else if (inp[1] == '\0' || g_ascii_isspace(inp[1])) {
142                                 /* soft line break */
143                                 break;
144                         } else {
145                                 /* broken QP string */
146                                 *outp = *inp++;
147                         }
148                 } else {
149                         *outp = *inp++;
150                 }
151                 outp++;
152                 avail--;
153         }
154
155         *outp = '\0';
156
157         return outp - out;
158 }
159
160 gint qp_decode_q_encoding(guchar *out, const gchar *in, gint inlen)
161 {
162         const gchar *inp = in;
163         guchar *outp = out;
164
165         if (inlen < 0)
166                 inlen = G_MAXINT;
167
168         while (inp - in < inlen && *inp != '\0') {
169                 if (*inp == '=' && inp + 3 - in <= inlen) {
170                         if (get_hex_value(outp, inp[1], inp[2]) == TRUE) {
171                                 inp += 3;
172                         } else {
173                                 *outp = *inp++;
174                         }
175                 } else if (*inp == '_') {
176                         *outp = ' ';
177                         inp++;
178                 } else {
179                         *outp = *inp++;
180                 }
181                 outp++;
182         }
183
184         *outp = '\0';
185
186         return outp - out;
187 }
188
189 gint qp_get_q_encoding_len(const guchar *str)
190 {
191         const guchar *inp = str;
192         gint len = 0;
193
194         while (*inp != '\0') {
195                 if (*inp == 0x20)
196                         len++;
197                 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
198                          *inp < 32 || *inp > 127 || g_ascii_isspace(*inp))
199                         len += 3;
200                 else
201                         len++;
202
203                 inp++;
204         }
205
206         return len;
207 }
208
209 void qp_q_encode(gchar *out, const guchar *in)
210 {
211         const guchar *inp = in;
212         gchar *outp = out;
213
214         while (*inp != '\0') {
215                 if (*inp == 0x20)
216                         *outp++ = '_';
217                 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
218                          *inp < 32 || *inp > 127 || g_ascii_isspace(*inp)) {
219                         *outp++ = '=';
220                         get_hex_str(outp, *inp);
221                         outp += 2;
222                 } else
223                         *outp++ = *inp;
224
225                 inp++;
226         }
227
228         *outp = '\0';
229 }