remove unused code, simplifications, missing include
[apps/madmutt.git] / base64.c
1 /*
2  *     This program is free software; you can redistribute it
3  *     and/or modify it under the terms of the GNU General Public
4  *     License as published by the Free Software Foundation; either
5  *     version 2 of the License, or (at your option) any later
6  *     version.
7  * 
8  *     This program is distributed in the hope that it will be
9  *     useful, but WITHOUT ANY WARRANTY; without even the implied
10  *     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  *     PURPOSE.  See the GNU General Public License for more
12  *     details.
13  * 
14  *     You should have received a copy of the GNU General Public
15  *     License along with this program; if not, write to the Free
16  *     Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  *     Boston, MA 02110-1301, USA.
18  * 
19  */
20
21 /* 
22  * Base64 handling elsewhere in mutt should be modified to call
23  * these routines. These routines were written because IMAP's
24  * AUTHENTICATE protocol required them, and base64 handling
25  * elsewhere wasn't sufficiently generic.
26  * 
27  */
28
29 /* 
30  * This code is heavily modified from fetchmail (also GPL'd, of
31  * course) by Brendan Cully <brendan@kublai.com>.
32  * 
33  * Original copyright notice:
34  * 
35  * The code in the fetchmail distribution is Copyright 1997 by Eric
36  * S. Raymond.  Portions are also copyrighted by Carl Harris, 1993
37  * and 1995.  Copyright retained for the purpose of protecting free
38  * redistribution of source. 
39  * 
40  */
41
42 #if HAVE_CONFIG_H
43 # include "config.h"
44 #endif
45
46 #include <lib-mime/mime.h>
47
48 #include "mutt.h"
49
50 /* raw bytes to null-terminated base 64 string */
51 void mutt_to_base64 (unsigned char *out, const unsigned char *in, ssize_t len,
52                      ssize_t olen)
53 {
54   while (len >= 3 && olen > 10) {
55     *out++ = __m_b64chars[in[0] >> 2];
56     *out++ = __m_b64chars[((in[0] << 4) & 0x30) | (in[1] >> 4)];
57     *out++ = __m_b64chars[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
58     *out++ = __m_b64chars[in[2] & 0x3f];
59     olen -= 4;
60     len -= 3;
61     in += 3;
62   }
63
64   /* clean up remainder */
65   if (len > 0 && olen > 4) {
66     unsigned char fragment;
67
68     *out++ = __m_b64chars[in[0] >> 2];
69     fragment = (in[0] << 4) & 0x30;
70     if (len > 1)
71       fragment |= in[1] >> 4;
72     *out++ = __m_b64chars[fragment];
73     *out++ = (len < 2) ? '=' : __m_b64chars[(in[1] << 2) & 0x3c];
74     *out++ = '=';
75   }
76   *out = '\0';
77 }
78
79 /* Convert '\0'-terminated base 64 string to raw bytes.
80  * Returns length of returned buffer, or -1 on error */
81 int mutt_from_base64 (char *out, const char *in)
82 {
83   int len = 0;
84   register unsigned char digit1, digit2, digit3, digit4;
85
86   do {
87     digit1 = in[0];
88     if (base64val(digit1) < 0)
89       return -1;
90     digit2 = in[1];
91     if (base64val(digit2) < 0)
92       return -1;
93     digit3 = in[2];
94     if (digit3 != '=' && base64val(digit3) < 0)
95       return -1;
96     digit4 = in[3];
97     if (digit4 != '=' && base64val(digit4) < 0)
98       return -1;
99     in += 4;
100
101     /* digits are already sanity-checked */
102     *out++ = (base64val(digit1) << 2) | (base64val(digit2) >> 4);
103     len++;
104     if (digit3 != '=') {
105       *out++ = ((base64val(digit2) << 4) & 0xf0) | (base64val(digit3) >> 2);
106       len++;
107       if (digit4 != '=') {
108         *out++ = ((base64val(digit3) << 6) & 0xc0) | base64val(digit4);
109         len++;
110       }
111     }
112   } while (*in && digit4 != '=');
113
114   return len;
115 }