keep attachement when forwarding or reediting mails
[claws.git] / src / base64.c
1 /*
2  * base64.c -- base-64 conversion routines.
3  *
4  * For license terms, see the file COPYING in this directory.
5  *
6  * This base 64 encoding is defined in RFC2045 section 6.8,
7  * "Base64 Content-Transfer-Encoding", but lines must not be broken in the
8  * scheme used here.
9  *
10  * Modified by Hiroyuki Yamamoto <hiro-y@kcn.ne.jp>
11  */
12 #include <ctype.h>
13
14 static const char base64digits[] =
15    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
16
17 #define BAD     -1
18 static const char base64val[] = {
19     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
20     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
21     BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
22      52, 53, 54, 55,  56, 57, 58, 59,  60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
23     BAD,  0,  1,  2,   3,  4,  5,  6,   7,  8,  9, 10,  11, 12, 13, 14,
24      15, 16, 17, 18,  19, 20, 21, 22,  23, 24, 25,BAD, BAD,BAD,BAD,BAD,
25     BAD, 26, 27, 28,  29, 30, 31, 32,  33, 34, 35, 36,  37, 38, 39, 40,
26      41, 42, 43, 44,  45, 46, 47, 48,  49, 50, 51,BAD, BAD,BAD,BAD,BAD
27 };
28 #define DECODE64(c)  (isascii(c) ? base64val[c] : BAD)
29
30 void to64frombits(unsigned char *out, const unsigned char *in, int inlen)
31 /* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
32 {
33     for (; inlen >= 3; inlen -= 3)
34     {
35         *out++ = base64digits[in[0] >> 2];
36         *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
37         *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
38         *out++ = base64digits[in[2] & 0x3f];
39         in += 3;
40     }
41     if (inlen > 0)
42     {
43         unsigned char fragment;
44     
45         *out++ = base64digits[in[0] >> 2];
46         fragment = (in[0] << 4) & 0x30;
47         if (inlen > 1)
48             fragment |= in[1] >> 4;
49         *out++ = base64digits[fragment];
50         *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
51         *out++ = '=';
52     }
53     *out = '\0';
54 }
55
56 int from64tobits(char *out, const char *in)
57 /* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */
58 {
59     int len = 0;
60     register unsigned char digit1, digit2, digit3, digit4;
61
62     if (in[0] == '+' && in[1] == ' ')
63         in += 2;
64     if (*in == '\r' || *in == '\n')
65         return 0;
66
67     do {
68         digit1 = in[0];
69         if (DECODE64(digit1) == BAD)
70             return -1;
71         digit2 = in[1];
72         if (DECODE64(digit2) == BAD)
73             return -1;
74         digit3 = in[2];
75         if (digit3 != '=' && DECODE64(digit3) == BAD)
76             return -1; 
77         digit4 = in[3];
78         if (digit4 != '=' && DECODE64(digit4) == BAD)
79             return -1;
80         in += 4;
81         *out++ = (DECODE64(digit1) << 2) | ((DECODE64(digit2) >> 4) & 0x03);
82         ++len;
83         if (digit3 != '=')
84         {
85             *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
86             ++len;
87             if (digit4 != '=')
88             {
89                 *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
90                 ++len;
91             }
92         }
93     } while (*in && *in != '\r' && *in != '\n' && digit4 != '=');
94
95     return len;
96 }
97
98 /* base64.c ends here */