1/*-------------------------------------------------------------------------
4 * Encoding and decoding routines for base64 without whitespace.
6 * Copyright (c) 2001-2025, PostgreSQL Global Development Group
12 *-------------------------------------------------------------------------
28"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
34 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
35 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
36 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
37 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
38 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
44 * Encode the 'src' byte array into base64. Returns the length of the encoded
45 * string, and -1 in the event of an error with the result buffer zeroed for
62 buf |= *s << (pos << 3);
70 * Leave if there is an overflow in the area allocated for the
73 if ((p - dst + 4) > dstlen)
88 * Leave if there is an overflow in the area allocated for the encoded
91 if ((p - dst + 4) > dstlen)
96 *p++ = (pos == 0) ?
_base64[(
buf >> 6) & 0x3f] :
'=';
100 Assert((p - dst) <= dstlen);
104 memset(dst, 0, dstlen);
111 * Decode the given base64 string. Returns the length of the decoded
112 * string on success, and -1 in the event of an error with the result
113 * buffer zeroed for safety.
118 const char *srcend = src +
len,
131 /* Leave if a whitespace is found */
132 if (
c ==
' ' ||
c ==
'\t' ||
c ==
'\n' ||
c ==
'\r')
147 * Unexpected "=" character found while decoding base64
158 if (
c > 0 &&
c < 127)
162 /* invalid symbol found */
166 /* add it to buffer */
172 * Leave if there is an overflow in the area allocated for the
175 if ((p - dst + 1) > dstlen)
177 *p++ = (
buf >> 16) & 255;
179 if (end == 0 || end > 1)
182 if ((p - dst + 1) > dstlen)
184 *p++ = (
buf >> 8) & 255;
186 if (end == 0 || end > 2)
189 if ((p - dst + 1) > dstlen)
201 * base64 end sequence is invalid. Input data is missing padding, is
202 * truncated or is otherwise corrupted.
207 Assert((p - dst) <= dstlen);
211 memset(dst, 0, dstlen);
218 * Returns to caller the length of the string if it were encoded with
219 * base64 based on the length provided by caller. This is useful to
220 * estimate how large a buffer allocation needs to be done before doing
221 * the actual encoding.
226 /* 3 bytes will be converted to 4 */
227 return (srclen + 2) / 3 * 4;
233 * Returns to caller the length of the string if it were to be decoded
234 * with base64, based on the length given by caller. This is useful to
235 * estimate how large a buffer allocation needs to be done before doing
236 * the actual decoding.
241 return (srclen * 3) >> 2;
int pg_b64_enc_len(int srclen)
int pg_b64_encode(const uint8 *src, int len, char *dst, int dstlen)
int pg_b64_dec_len(int srclen)
int pg_b64_decode(const char *src, int len, uint8 *dst, int dstlen)
static const char _base64[]
static const int8 b64lookup[128]
Assert(PointerIsAligned(start, uint64))