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