2 * Sylpheed -- 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/>.
26 #define MAX_LINELEN 76
28 #define IS_LBREAK(p) \
29 ((p)[0] == '\n' ? 1 : ((p)[0] == '\r' && (p)[1] == '\n') ? 2 : 0)
31 gint qp_encode(gboolean text, gchar *out, const guchar *in, gint len)
33 /* counters of input/output characters */
35 gint outc = 1; /* one character reserved for '=' soft line break */
38 /* allow literal linebreaks in text */
41 /* inserting linebreaks is the job of our caller */
42 g_assert(outc <= MAX_LINELEN);
44 return inc + IS_LBREAK(in);
47 /* free the reserved character since no softbreak
48 * will be needed after the current character */
50 /* guard against whitespace before a literal linebreak */
51 if(*in == ' ' || *in == '\t') {
59 /* Cave: Whitespace is unconditionally output literally,
60 * but according to the RFC it must not be output before a
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) {
74 if(outc + 3 <= MAX_LINELEN) {
77 get_hex_str(out, *in);
86 g_assert(outc <= MAX_LINELEN);
92 void qp_encode_line(gchar *out, const guchar *in) {
94 in += qp_encode(TRUE, out, in, strlen(in));
96 while(*out != '\0') out++;
102 gint qp_decode_line(gchar *str)
104 gchar *inp = str, *outp = str;
106 while (*inp != '\0') {
108 if (inp[1] && inp[2] &&
109 get_hex_value((guchar *)outp, inp[1], inp[2])
112 } else if (inp[1] == '\0' || g_ascii_isspace(inp[1])) {
113 /* soft line break */
116 /* broken QP string */
130 gint qp_decode_const(gchar *out, gint avail, const gchar *str)
132 const gchar *inp = str;
135 while (*inp != '\0' && avail > 0) {
137 if (inp[1] && inp[2] &&
138 get_hex_value((guchar *)outp, inp[1], inp[2])
141 } else if (inp[1] == '\0' || g_ascii_isspace(inp[1])) {
142 /* soft line break */
145 /* broken QP string */
160 gint qp_decode_q_encoding(guchar *out, const gchar *in, gint inlen)
162 const gchar *inp = in;
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) {
175 } else if (*inp == '_') {
189 gint qp_get_q_encoding_len(const guchar *str)
191 const guchar *inp = str;
194 while (*inp != '\0') {
197 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
198 *inp < 32 || *inp > 127 || g_ascii_isspace(*inp))
209 void qp_q_encode(gchar *out, const guchar *in)
211 const guchar *inp = in;
214 while (*inp != '\0') {
217 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
218 *inp < 32 || *inp > 127 || g_ascii_isspace(*inp)) {
220 get_hex_str(outp, *inp);