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.
7 /** \file base64.c Uudecode and base64decode implementation. */
16 static const char Base64[] =
17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
18 static const char Pad64 = '=';
20 /** Maximal possible size of the decoded data. */
21 #define BASE64_MAX_DECODED_SIZE(_encoded_size) ((_encoded_size) / 4 * 3)
24 * base64-decode a buffer.
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.
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
35 * It is OK to pass a \p NULL pointer as \a decoded_size. The result is
36 * terminated with a zero byte.
38 * \return Standard. The contents of result \a and \a decoded_size are
39 * undefined on failure.
41 int base64_decode(char const *src, size_t encoded_size, char **result,
44 unsigned int tarindex, state;
47 const char *end = src + encoded_size;
48 unsigned char *target;
50 if (encoded_size == (size_t)-1)
51 encoded_size = strlen(src);
52 target = para_malloc(BASE64_MAX_DECODED_SIZE(encoded_size) + 1);
59 if (para_isspace(ch)) /* Skip whitespace anywhere. */
65 pos = strchr(Base64, ch);
66 if (pos == NULL) /* A non-base64 character. */
71 target[tarindex] = (pos - Base64) << 2;
75 target[tarindex] |= (pos - Base64) >> 4;
76 target[tarindex + 1] = ((pos - Base64) & 0x0f) << 4;
81 target[tarindex] |= (pos - Base64) >> 2;
82 target[tarindex + 1] = ((pos - Base64) & 0x03) << 6;
87 target[tarindex] |= pos - Base64;
95 * We are done decoding Base-64 chars. Let's see if we ended
96 * on a byte boundary, and/or with erroneous trailing characters.
99 if (*src == Pad64) { /* We got a pad char. */
100 ch = *src++; /* Skip it, get next. */
102 case 0: /* Invalid = in first position */
103 case 1: /* Invalid = in second position */
106 case 2: /* Valid, means one byte of info */
107 /* Skip any number of spaces. */
108 for (; ch != '\0'; ch = *src++)
111 /* Make sure there is another trailing = sign. */
114 ch = *src++; /* Skip the = */
115 /* Fall through to "single trailing =" case. */
118 case 3: /* Valid, means two bytes of info */
120 * We know this char is an =. Is there anything but
121 * whitespace after it?
123 for (; ch != '\0'; ch = *src++)
128 * Now make sure for cases 2 and 3 that the "extra"
129 * bits that slopped past the last full byte were
130 * zeros. If we don't check them, they become a
131 * subliminal channel.
133 if (target[tarindex] != 0)
138 * We ended by seeing the end of the string. Make sure we
139 * have no partial bytes lying around.
145 target[tarindex] = '\0'; /* just to be sure */
146 *result = (char *)target;
148 *decoded_size = tarindex;
156 * Decode a buffer using the uuencode Base64 algorithm.
158 * \param src The buffer to decode.
159 * \param encoded_size Number of input bytes in the source buffer.
160 * \param result Contains the decoded data on success.
161 * \param decoded_size Number of output bytes on success.
163 * This is just a simple wrapper for \ref base64_decode() which strips
166 * \return The return value of the underlying call to \ref base64_decode().
168 * \sa uuencode(1), uudecode(1).
170 int uudecode(char const *src, size_t encoded_size, char **result,
171 size_t *decoded_size)
173 const char *end = src + encoded_size, *p;
175 /* skip whitespace and data */
176 for (p = src; p < end && (*p == ' ' || *p == '\t'); p++)
178 for (; p < end && *p != '\0' && *p != ' ' && *p != '\t'; p++)
180 /* and remove trailing whitespace because base64_decode needs this */
181 return base64_decode(src, p - src, result, decoded_size);