Lines Matching +full:data +full:- +full:bits
1 // SPDX-License-Identifier: GPL-2.0
3 * base64.c - RFC4648-compliant base64 encoding
8 * (which are using the URL-safe base64 encoding),
22 * base64_encode() - base64-encode some binary data
23 * @src: the binary data to encode
25 * @dst: (output) the base64-encoded string. Not NUL-terminated.
27 * Encodes data using base64 encoding, i.e. the "Base 64 Encoding" specified
28 * by RFC 4648, including the '='-padding.
30 * Return: the length of the resulting base64-encoded string in bytes.
35 int bits = 0; in base64_encode() local
41 bits += 8; in base64_encode()
43 bits -= 6; in base64_encode()
44 *cp++ = base64_table[(ac >> bits) & 0x3f]; in base64_encode()
45 } while (bits >= 6); in base64_encode()
47 if (bits) { in base64_encode()
48 *cp++ = base64_table[(ac << (6 - bits)) & 0x3f]; in base64_encode()
49 bits -= 6; in base64_encode()
51 while (bits < 0) { in base64_encode()
53 bits += 2; in base64_encode()
55 return cp - dst; in base64_encode()
60 * base64_decode() - base64-decode a string
61 * @src: the string to decode. Doesn't need to be NUL-terminated.
63 * @dst: (output) the decoded binary data
66 * specified by RFC 4648, including the '='-padding.
70 * Return: the length of the resulting decoded binary data in bytes,
71 * or -1 if the string isn't a valid base64 string.
76 int bits = 0; in base64_decode() local
85 bits += 6; in base64_decode()
86 if (bits >= 8) in base64_decode()
87 bits -= 8; in base64_decode()
91 return -1; in base64_decode()
92 ac = (ac << 6) | (p - base64_table); in base64_decode()
93 bits += 6; in base64_decode()
94 if (bits >= 8) { in base64_decode()
95 bits -= 8; in base64_decode()
96 *bp++ = (u8)(ac >> bits); in base64_decode()
99 if (ac & ((1 << bits) - 1)) in base64_decode()
100 return -1; in base64_decode()
101 return bp - dst; in base64_decode()