4809a2f285f63dd83132f693be23f68945fce21d
[paraslash.git] / base64.c
1 /*
2  * The code in this file was taken from openssh-5.2p1, Copyright (c) 1996 by
3  * Internet Software Consortium.  Portions Copyright (c) 1995 by International
4  * Business Machines, Inc.
5  */
6
7 /** \file base64.c Uudecode and base64decode implementation. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "error.h"
13 #include "base64.h"
14 #include "string.h"
15
16 static const char Base64[] =
17         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
18 static const char Pad64 = '=';
19
20 /** Maximal possible size of the decoded data. */
21 #define BASE64_MAX_DECODED_SIZE(_encoded_size) ((_encoded_size) / 4 * 3)
22
23 /**
24  * base64-decode a buffer.
25  *
26  * \param src The buffer to decode.
27  * \param encoded_size The special value -1 means: look for terminating zero byte.
28  * \param result Points to dynamically allocated target buffer on success.
29  * \param decoded_size Number of bytes written to \a result.
30  *
31  * Skips all whitespace anywhere. Converts characters, four at a time, starting
32  * at (or after) src from base - 64 numbers into three 8 bit bytes in the
33  * target area.
34  *
35  * It is OK to pass a \p NULL pointer as \a decoded_size. The result is
36  * terminated with a zero byte.
37  *
38  * \return Standard. The contents of result \a and \a decoded_size are
39  * undefined on failure.
40  */
41 int base64_decode(char const *src, size_t encoded_size, char **result,
42                 size_t *decoded_size)
43 {
44         unsigned int tarindex, state;
45         int ch;
46         char *pos;
47         const char *end = src + encoded_size;
48         unsigned char *target;
49         size_t targsize;
50
51         if (encoded_size == (size_t)-1)
52                 encoded_size = strlen(src);
53         targsize = BASE64_MAX_DECODED_SIZE(encoded_size);
54         target = para_malloc(targsize + 1);
55
56         state = 0;
57         tarindex = 0;
58
59         while (src < end) {
60                 ch = *src++;
61                 if (para_isspace(ch)) /* Skip whitespace anywhere. */
62                         continue;
63
64                 if (ch == Pad64)
65                         break;
66
67                 pos = strchr(Base64, ch);
68                 if (pos == NULL) /* A non-base64 character. */
69                         goto fail;
70
71                 switch (state) {
72                 case 0:
73                         if (tarindex >= targsize)
74                                 goto fail;
75                         target[tarindex] = (pos - Base64) << 2;
76                         state = 1;
77                         break;
78                 case 1:
79                         if (tarindex + 1 >= targsize)
80                                 goto fail;
81                         target[tarindex] |= (pos - Base64) >> 4;
82                         target[tarindex + 1] = ((pos - Base64) & 0x0f) << 4;
83                         tarindex++;
84                         state = 2;
85                         break;
86                 case 2:
87                         if (tarindex + 1 >= targsize)
88                                 goto fail;
89                         target[tarindex] |= (pos - Base64) >> 2;
90                         target[tarindex + 1] = ((pos - Base64) & 0x03) << 6;
91                         tarindex++;
92                         state = 3;
93                         break;
94                 case 3:
95                         if (tarindex >= targsize)
96                                 goto fail;
97                         target[tarindex] |= pos - Base64;
98                         tarindex++;
99                         state = 0;
100                         break;
101                 }
102         }
103
104         /*
105          * We are done decoding Base-64 chars.  Let's see if we ended
106          * on a byte boundary, and/or with erroneous trailing characters.
107          */
108
109         if (*src == Pad64) {            /* We got a pad char. */
110                 ch = *src++;            /* Skip it, get next. */
111                 switch (state) {
112                 case 0:         /* Invalid = in first position */
113                 case 1:         /* Invalid = in second position */
114                         goto fail;
115
116                 case 2:         /* Valid, means one byte of info */
117                         /* Skip any number of spaces. */
118                         for (; ch != '\0'; ch = *src++)
119                                 if (!isspace(ch))
120                                         break;
121                         /* Make sure there is another trailing = sign. */
122                         if (ch != Pad64)
123                                 goto fail;
124                         ch = *src++;            /* Skip the = */
125                         /* Fall through to "single trailing =" case. */
126                         /* FALLTHROUGH */
127
128                 case 3:         /* Valid, means two bytes of info */
129                         /*
130                          * We know this char is an =.  Is there anything but
131                          * whitespace after it?
132                          */
133                         for (; ch != '\0'; ch = *src++)
134                                 if (!isspace(ch))
135                                         goto fail;
136
137                         /*
138                          * Now make sure for cases 2 and 3 that the "extra"
139                          * bits that slopped past the last full byte were
140                          * zeros.  If we don't check them, they become a
141                          * subliminal channel.
142                          */
143                         if (target[tarindex] != 0)
144                                 goto fail;
145                 }
146         } else {
147                 /*
148                  * We ended by seeing the end of the string.  Make sure we
149                  * have no partial bytes lying around.
150                  */
151                 if (state != 0)
152                         goto fail;
153         }
154         /* success */
155         target[tarindex] = '\0'; /* just to be sure */
156         *result = (char *)target;
157         if (decoded_size)
158                 *decoded_size = tarindex;
159         return 1;
160 fail:
161         free(target);
162         return -E_BASE64;
163 }
164
165 /**
166  * Decode a buffer using the uuencode Base64 algorithm.
167  *
168  * \param src The buffer to decode.
169  * \param encoded_size Number of input bytes in the source buffer.
170  * \param result Contains the decoded data on success.
171  * \param decoded_size Number of output bytes on success.
172  *
173  * This is just a simple wrapper for \ref base64_decode() which strips
174  * whitespace.
175  *
176  * \return The return value of the underlying call to \ref base64_decode().
177  *
178  * \sa uuencode(1), uudecode(1).
179  */
180 int uudecode(char const *src, size_t encoded_size, char **result,
181                 size_t *decoded_size)
182 {
183         const char *end = src + encoded_size, *p;
184
185         /* skip whitespace and data */
186         for (p = src; p < end && (*p == ' ' || *p == '\t'); p++)
187                 ;
188         for (; p < end && *p != '\0' && *p != ' ' && *p != '\t'; p++)
189                 ;
190         /* and remove trailing whitespace because base64_decode needs this */
191         return base64_decode(src, p - src, result, decoded_size);
192 }