2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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.
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 static gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
24 static void get_hex_str(gchar *out, guchar ch);
26 #define MAX_LINELEN 76
28 #define IS_LBREAK(p) \
29 (*(p) == '\0' || *(p) == '\n' || (*(p) == '\r' && *((p) + 1) == '\n'))
31 #define SOFT_LBREAK_IF_REQUIRED(n) \
32 if (len + (n) > MAX_LINELEN || \
33 (len + (n) == MAX_LINELEN && (!IS_LBREAK(inp + 1)))) { \
39 void qp_encode_line(gchar *out, const guchar *in)
41 const guchar *inp = in;
46 while (*inp != '\0') {
55 } else if (ch == '\t' || ch == ' ') {
56 if (IS_LBREAK(inp + 1)) {
57 SOFT_LBREAK_IF_REQUIRED(3);
59 get_hex_str(outp, ch);
64 SOFT_LBREAK_IF_REQUIRED(1);
68 } else if ((ch >= 33 && ch <= 60) || (ch >= 62 && ch <= 126)) {
69 SOFT_LBREAK_IF_REQUIRED(1);
73 SOFT_LBREAK_IF_REQUIRED(3);
75 get_hex_str(outp, ch);
88 gint qp_decode_line(gchar *str)
90 gchar *inp = str, *outp = str;
92 while (*inp != '\0') {
94 if (inp[1] && inp[2] &&
95 get_hex_value(outp, inp[1], inp[2]) == TRUE) {
97 } else if (inp[1] == '\0' || isspace((guchar)inp[1])) {
101 /* broken QP string */
115 gint qp_decode_q_encoding(guchar *out, const gchar *in, gint inlen)
117 const gchar *inp = in;
123 while (inp - in < inlen && *inp != '\0') {
124 if (*inp == '=' && inp + 3 - in <= inlen) {
125 if (get_hex_value(outp, inp[1], inp[2]) == TRUE) {
130 } else if (*inp == '_') {
144 gint qp_get_q_encoding_len(const guchar *str)
146 const guchar *inp = str;
149 while (*inp != '\0') {
152 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
153 *inp < 32 || *inp > 127 || isspace(*inp))
164 void qp_q_encode(gchar *out, const guchar *in)
166 const guchar *inp = in;
169 while (*inp != '\0') {
172 else if (*inp == '=' || *inp == '?' || *inp == '_' ||
173 *inp < 32 || *inp > 127 || isspace(*inp)) {
175 get_hex_str(outp, *inp);
186 #define HEX_TO_INT(val, hex) \
190 if ('0' <= c && c <= '9') { \
192 } else if ('a' <= c && c <= 'f') { \
193 val = c - 'a' + 10; \
194 } else if ('A' <= c && c <= 'F') { \
195 val = c - 'A' + 10; \
201 static gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
208 if (hi == -1 || lo == -1)
211 *out = (hi << 4) + lo;
215 #define INT_TO_HEX(hex, val) \
220 hex = 'A' + (val) - 10; \
223 static void get_hex_str(gchar *out, guchar ch)
227 INT_TO_HEX(hex, ch >> 4);
229 INT_TO_HEX(hex, ch & 0x0f);