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