cee51571a7416b396583932be2376398e0670573
[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
19 /** Maximal possible size of the decoded data. */
20 #define BASE64_MAX_DECODED_SIZE(_encoded_size) ((_encoded_size) / 4 * 3)
21
22 #define PAD64 '='
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
50 if (encoded_size == (size_t)-1)
51 encoded_size = strlen(src);
52 target = para_malloc(BASE64_MAX_DECODED_SIZE(encoded_size) + 1);
53
54 state = 0;
55 tarindex = 0;
56
57 while (src < end) {
58 ch = *src++;
59 if (para_isspace(ch)) /* Skip whitespace anywhere. */
60 continue;
61
62 if (ch == PAD64)
63 break;
64
65 pos = strchr(Base64, ch);
66 if (pos == NULL) /* A non-base64 character. */
67 goto fail;
68
69 switch (state) {
70 case 0:
71 target[tarindex] = (pos - Base64) << 2;
72 state = 1;
73 break;
74 case 1:
75 target[tarindex] |= (pos - Base64) >> 4;
76 target[tarindex + 1] = ((pos - Base64) & 0x0f) << 4;
77 tarindex++;
78 state = 2;
79 break;
80 case 2:
81 target[tarindex] |= (pos - Base64) >> 2;
82 target[tarindex + 1] = ((pos - Base64) & 0x03) << 6;
83 tarindex++;
84 state = 3;
85 break;
86 case 3:
87 target[tarindex] |= pos - Base64;
88 tarindex++;
89 state = 0;
90 break;
91 }
92 }
93 /*
94 * We are done decoding Base-64 chars. Let's see if we ended
95 * on a byte boundary, and/or with erroneous trailing characters.
96 */
97 if (*src == PAD64) { /* We got a pad char. */
98 ch = *src++; /* Skip it, get next. */
99 switch (state) {
100 case 0: /* Invalid = in first position */
101 case 1: /* Invalid = in second position */
102 goto fail;
103
104 case 2: /* Valid, means one byte of info */
105 /* Skip any number of spaces. */
106 for (; ch != '\0'; ch = *src++)
107 if (!para_isspace(ch))
108 break;
109 /* Make sure there is another trailing = sign. */
110 if (ch != PAD64)
111 goto fail;
112 ch = *src++; /* Skip the = */
113 /* Fall through to "single trailing =" case. */
114
115 case 3: /* Valid, means two bytes of info */
116 /*
117 * We know this char is an =. Is there anything but
118 * whitespace after it?
119 */
120 for (; ch != '\0'; ch = *src++)
121 if (!para_isspace(ch))
122 goto fail;
123 /*
124 * Now make sure for cases 2 and 3 that the "extra"
125 * bits that slopped past the last full byte were
126 * zeros. If we don't check them, they become a
127 * subliminal channel.
128 */
129 if (target[tarindex] != 0)
130 goto fail;
131 }
132 } else {
133 /*
134 * We ended by seeing the end of the string. Make sure we
135 * have no partial bytes lying around.
136 */
137 if (state != 0)
138 goto fail;
139 }
140 /* success */
141 target[tarindex] = '\0'; /* just to be sure */
142 *result = (char *)target;
143 if (decoded_size)
144 *decoded_size = tarindex;
145 return 1;
146 fail:
147 free(target);
148 return -E_BASE64;
149 }
150
151 /**
152 * Decode a buffer using the uuencode Base64 algorithm.
153 *
154 * \param src The buffer to decode.
155 * \param encoded_size Number of input bytes in the source buffer.
156 * \param result Contains the decoded data on success.
157 * \param decoded_size Number of output bytes on success.
158 *
159 * This is just a simple wrapper for \ref base64_decode() which strips
160 * whitespace.
161 *
162 * \return The return value of the underlying call to \ref base64_decode().
163 *
164 * \sa uuencode(1), uudecode(1).
165 */
166 int uudecode(char const *src, size_t encoded_size, char **result,
167 size_t *decoded_size)
168 {
169 const char *end = src + encoded_size, *p;
170
171 /* skip whitespace and data */
172 for (p = src; p < end && (*p == ' ' || *p == '\t'); p++)
173 ;
174 for (; p < end && *p != '\0' && *p != ' ' && *p != '\t'; p++)
175 ;
176 /* and remove trailing whitespace because base64_decode needs this */
177 return base64_decode(src, p - src, result, decoded_size);
178 }