💾 Archived View for gemini.rmf-dev.com › repo › Vaati › powShield › files › 19b6912773e2064adf62f469… captured on 2023-05-24 at 18:47:50. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
0 /*
1 * Base64 encoding/decoding (RFC1341)
2 * Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
3 *
4 * This software may be distributed under the terms of the BSD license.
5 * See README for more details.
6 */
7
8 static const unsigned char base64_table[65] =
9 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
10
11 /**
12 * base64_encode - Base64 encode
13 * @src: Data to be encoded
14 * @len: Length of the data to be encoded
15 * @out_len: Pointer to output length variable, or %NULL if not used
16 * Returns: Allocated buffer of out_len bytes of encoded data,
17 * or %NULL on failure
18 *
19 * Caller is responsible for freeing the returned buffer. Returned buffer is
20 * nul terminated to make it easier to use as a C string. The nul terminator is
21 * not included in out_len.
22 */
23 int base64_encode(const unsigned char *src, size_t len,
24 unsigned char *dst, size_t dst_len)
25 {
26 unsigned char *out, *pos;
27 const unsigned char *end, *in;
28 size_t olen;
29
30 olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
31 olen += olen / 72; /* line feeds */
32 olen++; /* nul termination */
33 if (olen < len || olen >= dst_len)
34 return -1; /* integer overflow */
35 out = dst;
36
37 end = src + len;
38 in = src;
39 pos = out;
40 while (end - in >= 3) {
41 *pos++ = base64_table[in[0] >> 2];
42 *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
43 *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
44 *pos++ = base64_table[in[2] & 0x3f];
45 in += 3;
46 }
47
48 if (end - in) {
49 *pos++ = base64_table[in[0] >> 2];
50 if (end - in == 1) {
51 *pos++ = base64_table[(in[0] & 0x03) << 4];
52 *pos++ = '=';
53 } else {
54 *pos++ = base64_table[((in[0] & 0x03) << 4) |
55 (in[1] >> 4)];
56 *pos++ = base64_table[(in[1] & 0x0f) << 2];
57 }
58 *pos++ = '=';
59 }
60
61 *pos = '\0';
62 return pos - out;
63 }
64