Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib/base64: rework encode/decode for speed and stricter validation

The old base64 implementation relied on a bit-accumulator loop, which was
slow for larger inputs and too permissive in validation. It would accept
extra '=', missing '=', or even '=' appearing in the middle of the input,
allowing malformed strings to pass. This patch reworks the internals to
improve performance and enforce stricter validation.

Changes:
- Encoder:
* Process input in 3-byte blocks, mapping 24 bits into four 6-bit
symbols, avoiding bit-by-bit shifting and reducing loop iterations.
* Handle the final 1-2 leftover bytes explicitly and emit '=' only when
requested.
- Decoder:
* Based on the reverse lookup tables from the previous patch, decode
input in 4-character groups.
* Each group is looked up directly, converted into numeric values, and
combined into 3 output bytes.
* Explicitly handle padded and unpadded forms:
- With padding: input length must be a multiple of 4, and '=' is
allowed only in the last two positions. Reject stray or early '='.
- Without padding: validate tail lengths (2 or 3 chars) and require
unused low bits to be zero.
* Removed the bit-accumulator style loop to reduce loop iterations.

Performance (x86_64, Intel Core i7-10700 @ 2.90GHz, avg over 1000 runs,
KUnit):

Encode:
64B ~90ns -> ~32ns (~2.8x)
1KB ~1332ns -> ~510ns (~2.6x)

Decode:
64B ~1530ns -> ~35ns (~43.7x)
1KB ~27726ns -> ~530ns (~52.3x)

[akpm@linux-foundation.org: remove u32 casts, per David and Guan-Chun]
Link: https://lkml.kernel.org/r/20251114060132.89279-1-409411716@gms.tku.edu.tw
Co-developed-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Co-developed-by: Yu-Sheng Huang <home7438072@gmail.com>
Signed-off-by: Yu-Sheng Huang <home7438072@gmail.com>
Signed-off-by: Guan-Chun Wu <409411716@gms.tku.edu.tw>
Reviewed-by: David Laight <david.laight.linux@gmail.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Eric Biggers <ebiggers@kernel.org>
Cc: Ilya Dryomov <idryomov@gmail.com>
Cc: Jaegeuk Kim <jaegeuk@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Keith Busch <kbusch@kernel.org>
Cc: Sagi Grimberg <sagi@grimberg.me>
Cc: "Theodore Y. Ts'o" <tytso@mit.edu>
Cc: Viacheslav Dubeyko <Slava.Dubeyko@ibm.com>
Cc: Xiubo Li <xiubli@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Guan-Chun Wu and committed by
Andrew Morton
9c7d3cf9 c4eb7ad3

+68 -41
+68 -41
lib/base64.c
··· 80 80 int base64_encode(const u8 *src, int srclen, char *dst, bool padding, enum base64_variant variant) 81 81 { 82 82 u32 ac = 0; 83 - int bits = 0; 84 - int i; 85 83 char *cp = dst; 86 84 const char *base64_table = base64_tables[variant]; 87 85 88 - for (i = 0; i < srclen; i++) { 89 - ac = (ac << 8) | src[i]; 90 - bits += 8; 91 - do { 92 - bits -= 6; 93 - *cp++ = base64_table[(ac >> bits) & 0x3f]; 94 - } while (bits >= 6); 86 + while (srclen >= 3) { 87 + ac = src[0] << 16 | src[1] << 8 | src[2]; 88 + *cp++ = base64_table[ac >> 18]; 89 + *cp++ = base64_table[(ac >> 12) & 0x3f]; 90 + *cp++ = base64_table[(ac >> 6) & 0x3f]; 91 + *cp++ = base64_table[ac & 0x3f]; 92 + 93 + src += 3; 94 + srclen -= 3; 95 95 } 96 - if (bits) { 97 - *cp++ = base64_table[(ac << (6 - bits)) & 0x3f]; 98 - bits -= 6; 99 - } 100 - if (padding) { 101 - while (bits < 0) { 96 + 97 + switch (srclen) { 98 + case 2: 99 + ac = src[0] << 16 | src[1] << 8; 100 + *cp++ = base64_table[ac >> 18]; 101 + *cp++ = base64_table[(ac >> 12) & 0x3f]; 102 + *cp++ = base64_table[(ac >> 6) & 0x3f]; 103 + if (padding) 102 104 *cp++ = '='; 103 - bits += 2; 105 + break; 106 + case 1: 107 + ac = src[0] << 16; 108 + *cp++ = base64_table[ac >> 18]; 109 + *cp++ = base64_table[(ac >> 12) & 0x3f]; 110 + if (padding) { 111 + *cp++ = '='; 112 + *cp++ = '='; 104 113 } 114 + break; 105 115 } 106 116 return cp - dst; 107 117 } ··· 127 117 * 128 118 * Decodes a string using the selected Base64 variant. 129 119 * 130 - * This implementation hasn't been optimized for performance. 131 - * 132 120 * Return: the length of the resulting decoded binary data in bytes, 133 121 * or -1 if the string isn't a valid Base64 string. 134 122 */ 135 123 int base64_decode(const char *src, int srclen, u8 *dst, bool padding, enum base64_variant variant) 136 124 { 137 - u32 ac = 0; 138 - int bits = 0; 139 - int i; 140 125 u8 *bp = dst; 141 - s8 ch; 126 + s8 input[4]; 127 + s32 val; 128 + const u8 *s = (const u8 *)src; 129 + const s8 *base64_rev_tables = base64_rev_maps[variant]; 142 130 143 - for (i = 0; i < srclen; i++) { 144 - if (padding) { 145 - if (src[i] == '=') { 146 - ac = (ac << 6); 147 - bits += 6; 148 - if (bits >= 8) 149 - bits -= 8; 150 - continue; 151 - } 131 + while (srclen >= 4) { 132 + input[0] = base64_rev_tables[s[0]]; 133 + input[1] = base64_rev_tables[s[1]]; 134 + input[2] = base64_rev_tables[s[2]]; 135 + input[3] = base64_rev_tables[s[3]]; 136 + 137 + val = input[0] << 18 | input[1] << 12 | input[2] << 6 | input[3]; 138 + 139 + if (unlikely(val < 0)) { 140 + if (!padding || srclen != 4 || s[3] != '=') 141 + return -1; 142 + padding = 0; 143 + srclen = s[2] == '=' ? 2 : 3; 144 + break; 152 145 } 153 - ch = base64_rev_maps[variant][(u8)src[i]]; 154 - if (ch == -1) 155 - return -1; 156 - ac = (ac << 6) | ch; 157 - bits += 6; 158 - if (bits >= 8) { 159 - bits -= 8; 160 - *bp++ = (u8)(ac >> bits); 161 - } 146 + 147 + *bp++ = val >> 16; 148 + *bp++ = val >> 8; 149 + *bp++ = val; 150 + 151 + s += 4; 152 + srclen -= 4; 162 153 } 163 - if (ac & ((1 << bits) - 1)) 154 + 155 + if (likely(!srclen)) 156 + return bp - dst; 157 + if (padding || srclen == 1) 164 158 return -1; 159 + 160 + val = (base64_rev_tables[s[0]] << 12) | (base64_rev_tables[s[1]] << 6); 161 + *bp++ = val >> 10; 162 + 163 + if (srclen == 2) { 164 + if (val & 0x800003ff) 165 + return -1; 166 + } else { 167 + val |= base64_rev_tables[s[2]]; 168 + if (val & 0x80000003) 169 + return -1; 170 + *bp++ = val >> 2; 171 + } 165 172 return bp - dst; 166 173 } 167 174 EXPORT_SYMBOL_GPL(base64_decode);