2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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.
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.
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/>.
27 #define MAX_LINELEN 76
29 #define IS_LBREAK(p) \
30 (*(p) == '\0' || *(p) == '\n' || (*(p) == '\r' && *((p) + 1) == '\n'))
32 #define SOFT_LBREAK_IF_REQUIRED(n) \
33 if (len + (n) > MAX_LINELEN || \
34 (len + (n) == MAX_LINELEN && (!IS_LBREAK(inp + 1)))) { \
40 void qp_encode_line(gchar
*out
, const guchar
*in
)
42 const guchar
*inp
= in
;
47 while (*inp
!= '\0') {
56 } else if (ch
== '\t' || ch
== ' ') {
57 if (IS_LBREAK(inp
+ 1)) {
58 SOFT_LBREAK_IF_REQUIRED(3);
60 get_hex_str(outp
, ch
);
65 SOFT_LBREAK_IF_REQUIRED(1);
69 } else if ((ch
>= 33 && ch
<= 60) || (ch
>= 62 && ch
<= 126)) {
70 SOFT_LBREAK_IF_REQUIRED(1);
74 SOFT_LBREAK_IF_REQUIRED(3);
76 get_hex_str(outp
, ch
);
89 gint
qp_decode_line(gchar
*str
)
91 gchar
*inp
= str
, *outp
= str
;
93 while (*inp
!= '\0') {
95 if (inp
[1] && inp
[2] &&
96 get_hex_value((guchar
*)outp
, inp
[1], inp
[2])
99 } else if (inp
[1] == '\0' || g_ascii_isspace(inp
[1])) {
100 /* soft line break */
103 /* broken QP string */
117 gint
qp_decode_const(gchar
*out
, gint avail
, const gchar
*str
)
119 const gchar
*inp
= str
;
122 while (*inp
!= '\0' && avail
> 0) {
124 if (inp
[1] && inp
[2] &&
125 get_hex_value((guchar
*)outp
, inp
[1], inp
[2])
128 } else if (inp
[1] == '\0' || g_ascii_isspace(inp
[1])) {
129 /* soft line break */
132 /* broken QP string */
147 gint
qp_decode_q_encoding(guchar
*out
, const gchar
*in
, gint inlen
)
149 const gchar
*inp
= in
;
155 while (inp
- in
< inlen
&& *inp
!= '\0') {
156 if (*inp
== '=' && inp
+ 3 - in
<= inlen
) {
157 if (get_hex_value(outp
, inp
[1], inp
[2]) == TRUE
) {
162 } else if (*inp
== '_') {
176 gint
qp_get_q_encoding_len(const guchar
*str
)
178 const guchar
*inp
= str
;
181 while (*inp
!= '\0') {
184 else if (*inp
== '=' || *inp
== '?' || *inp
== '_' ||
185 *inp
< 32 || *inp
> 127 || g_ascii_isspace(*inp
))
196 void qp_q_encode(gchar
*out
, const guchar
*in
)
198 const guchar
*inp
= in
;
201 while (*inp
!= '\0') {
204 else if (*inp
== '=' || *inp
== '?' || *inp
== '_' ||
205 *inp
< 32 || *inp
> 127 || g_ascii_isspace(*inp
)) {
207 get_hex_str(outp
, *inp
);