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+/";
19 /** Maximal possible size of the decoded data. */
20 #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;
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.
97 if (*src == PAD64) { /* We got a pad char. */
98 ch = *src++; /* Skip it, get next. */
100 case 0: /* Invalid = in first position */
101 case 1: /* Invalid = in second position */
104 case 2: /* Valid, means one byte of info */
105 /* Skip any number of spaces. */
106 for (; ch != '\0'; ch = *src++)
109 /* Make sure there is another trailing = sign. */
112 ch = *src++; /* Skip the = */
113 /* Fall through to "single trailing =" case. */
115 case 3: /* Valid, means two bytes of info */
117 * We know this char is an =. Is there anything but
118 * whitespace after it?
120 for (; ch != '\0'; ch = *src++)
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.
129 if (target[tarindex] != 0)
134 * We ended by seeing the end of the string. Make sure we
135 * have no partial bytes lying around.
141 target[tarindex] = '\0'; /* just to be sure */
142 *result = (char *)target;
144 *decoded_size = tarindex;
152 * Decode a buffer using the uuencode Base64 algorithm.
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.
159 * This is just a simple wrapper for \ref base64_decode() which strips
162 * \return The return value of the underlying call to \ref base64_decode().
164 * \sa uuencode(1), uudecode(1).
166 int uudecode(char const *src, size_t encoded_size, char **result,
167 size_t *decoded_size)
169 const char *end = src + encoded_size, *p;
171 /* skip whitespace and data */
172 for (p = src; p < end && (*p == ' ' || *p == '\t'); p++)
174 for (; p < end && *p != '\0' && *p != ' ' && *p != '\t'; p++)
176 /* and remove trailing whitespace because base64_decode needs this */
177 return base64_decode(src, p - src, result, decoded_size);