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;
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);
61 if (para_isspace(ch)) /* Skip whitespace anywhere. */
67 pos = strchr(Base64, ch);
68 if (pos == NULL) /* A non-base64 character. */
73 if (tarindex >= targsize)
75 target[tarindex] = (pos - Base64) << 2;
79 if (tarindex + 1 >= targsize)
81 target[tarindex] |= (pos - Base64) >> 4;
82 target[tarindex + 1] = ((pos - Base64) & 0x0f) << 4;
87 if (tarindex + 1 >= targsize)
89 target[tarindex] |= (pos - Base64) >> 2;
90 target[tarindex + 1] = ((pos - Base64) & 0x03) << 6;
95 if (tarindex >= targsize)
97 target[tarindex] |= pos - Base64;
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.
109 if (*src == Pad64) { /* We got a pad char. */
110 ch = *src++; /* Skip it, get next. */
112 case 0: /* Invalid = in first position */
113 case 1: /* Invalid = in second position */
116 case 2: /* Valid, means one byte of info */
117 /* Skip any number of spaces. */
118 for (; ch != '\0'; ch = *src++)
121 /* Make sure there is another trailing = sign. */
124 ch = *src++; /* Skip the = */
125 /* Fall through to "single trailing =" case. */
128 case 3: /* Valid, means two bytes of info */
130 * We know this char is an =. Is there anything but
131 * whitespace after it?
133 for (; ch != '\0'; ch = *src++)
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.
143 if (target[tarindex] != 0)
148 * We ended by seeing the end of the string. Make sure we
149 * have no partial bytes lying around.
155 target[tarindex] = '\0'; /* just to be sure */
156 *result = (char *)target;
158 *decoded_size = tarindex;
166 * Decode a buffer using the uuencode Base64 algorithm.
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.
173 * This is just a simple wrapper for \ref base64_decode() which strips
176 * \return The return value of the underlying call to \ref base64_decode().
178 * \sa uuencode(1), uudecode(1).
180 int uudecode(char const *src, size_t encoded_size, char **result,
181 size_t *decoded_size)
183 const char *end = src + encoded_size, *p;
185 /* skip whitespace and data */
186 for (p = src; p < end && (*p == ' ' || *p == '\t'); p++)
188 for (; p < end && *p != '\0' && *p != ' ' && *p != '\t'; p++)
190 /* and remove trailing whitespace because base64_decode needs this */
191 return base64_decode(src, p - src, result, decoded_size);