Skip to content

Lightpanda: the headless browser designed for AI and automation

base64: single-pass lookup table for forgiving decode

Adrià Arrufatc3f9341647db
diff --git a/src/browser/webapi/encoding/base64.zig b/src/browser/webapi/encoding/base64.zig
index 93bff058c..c72f15e59 100644
--- a/src/browser/webapi/encoding/base64.zig
+++ b/src/browser/webapi/encoding/base64.zig
@@ -51,81 +51,141 @@ pub fn encode(alloc: Allocator, in: BinInput) ![]const u8 {
///
/// std's decoders reject non-canonical trailing bits (e.g. "ab") and only trim
/// padding from the ends, neither of which match forgiving-base64 — so decode by
-/// hand: strip *all* ASCII whitespace, validate padding, tolerate trailing bits.
+/// hand in one pass: skip ASCII whitespace anywhere, accept up to two trailing
+/// "=", tolerate trailing bits.
pub fn decode(alloc: Allocator, in: BinInput) ![]const u8 {
const input = in.bytes();
- // Step 1: remove all ASCII whitespace (tab, LF, FF, CR, space) from anywhere.
- const buf = try alloc.alloc(u8, input.len);
- var n: usize = 0;
- for (input) |c| switch (c) {
- ' ', '\t', '\n', '\r', std.ascii.control_code.ff => {},
- else => {
- buf[n] = c;
- n += 1;
- },
- };
- var src = buf[0..n];
-
- // Step 2: only a multiple-of-4 length may carry (and shed) up to two "=".
- if (src.len % 4 == 0) {
- if (std.mem.endsWith(u8, src, "==")) {
- src = src[0 .. src.len - 2];
- } else if (std.mem.endsWith(u8, src, "=")) {
- src = src[0 .. src.len - 1];
+ // Peel the trailing "=" (and the whitespace around them) off the end, so
+ // the main loop only sees the alphabet and whitespace.
+ var end = input.len;
+ var pad: usize = 0;
+ while (end > 0) : (end -= 1) {
+ switch (decode_lut[input[end - 1]]) {
+ ws => {},
+ eq => {
+ if (pad == 2) break;
+ pad += 1;
+ },
+ else => break,
}
}
- // Step 3: a length % 4 == 1 can't represent valid base64.
- if (src.len % 4 == 1) return error.InvalidCharacterError;
- // Any "=" still present is misplaced padding.
- if (std.mem.findScalar(u8, src, '=') != null) return error.InvalidCharacterError;
-
- const out_len = src.len / 4 * 3 + switch (src.len % 4) {
- 0 => @as(usize, 0),
- 2 => 1,
- 3 => 2,
- else => unreachable,
- };
- const out = try alloc.alloc(u8, out_len);
+
+ const out = try alloc.alloc(u8, end / 4 * 3 + 2);
+ errdefer alloc.free(out);
var oi: usize = 0;
+ var acc: u32 = 0;
+ var n: usize = 0;
var i: usize = 0;
- while (i + 4 <= src.len) : (i += 4) {
- const a = try b64Val(src[i]);
- const b = try b64Val(src[i + 1]);
- const c = try b64Val(src[i + 2]);
- const d = try b64Val(src[i + 3]);
- out[oi] = (a << 2) | (b >> 4);
- out[oi + 1] = (b << 4) | (c >> 2);
- out[oi + 2] = (c << 6) | d;
- oi += 3;
+ while (i < end) {
+ if (n % 4 == 0 and i + 4 <= end) {
+ const a = decode_lut[input[i]];
+ const b = decode_lut[input[i + 1]];
+ const c = decode_lut[input[i + 2]];
+ const d = decode_lut[input[i + 3]];
+ if ((a | b | c | d) < 64) {
+ const v = @as(u32, a) << 18 | @as(u32, b) << 12 | @as(u32, c) << 6 | d;
+ out[oi] = @truncate(v >> 16);
+ out[oi + 1] = @truncate(v >> 8);
+ out[oi + 2] = @truncate(v);
+ oi += 3;
+ n += 4;
+ i += 4;
+ continue;
+ }
+ }
+ const v = decode_lut[input[i]];
+ i += 1;
+ if (v == ws) continue;
+ if (v >= 64) return error.InvalidCharacterError;
+ acc = acc << 6 | v;
+ n += 1;
+ if (n % 4 == 0) {
+ out[oi] = @truncate(acc >> 16);
+ out[oi + 1] = @truncate(acc >> 8);
+ out[oi + 2] = @truncate(acc);
+ oi += 3;
+ acc = 0;
+ }
}
- switch (src.len - i) {
+
+ // Padding is only stripped when it completes a quantum; otherwise the "="
+ // stays in the input and is invalid. A lone leftover sextet can't encode a byte.
+ if (pad > 0 and (n + pad) % 4 != 0) return error.InvalidCharacterError;
+ switch (n % 4) {
0 => {},
+ 1 => return error.InvalidCharacterError,
2 => {
- const a = try b64Val(src[i]);
- const b = try b64Val(src[i + 1]);
- out[oi] = (a << 2) | (b >> 4);
+ out[oi] = @truncate(acc >> 4);
+ oi += 1;
},
3 => {
- const a = try b64Val(src[i]);
- const b = try b64Val(src[i + 1]);
- const c = try b64Val(src[i + 2]);
- out[oi] = (a << 2) | (b >> 4);
- out[oi + 1] = (b << 4) | (c >> 2);
+ out[oi] = @truncate(acc >> 10);
+ out[oi + 1] = @truncate(acc >> 2);
+ oi += 2;
},
else => unreachable,
}
- return out;
+ return alloc.realloc(out, oi);
+}
+
+const ws = 0xfe;
+const eq = 0xfd;
+const invalid = 0xff;
+
+const decode_lut: [256]u8 = blk: {
+ var lut: [256]u8 = @splat(invalid);
+ for (std.base64.standard_alphabet_chars, 0..) |c, i| lut[c] = i;
+ for (" \t\n\r" ++ [_]u8{std.ascii.control_code.ff}) |c| lut[c] = ws;
+ lut['='] = eq;
+ break :blk lut;
+};
+
+fn testDecode(input: []const u8) ![]const u8 {
+ return decode(std.testing.allocator, .{ .raw = input });
}
-fn b64Val(c: u8) !u8 {
- return switch (c) {
- 'A'...'Z' => c - 'A',
- 'a'...'z' => c - 'a' + 26,
- '0'...'9' => c - '0' + 52,
- '+' => 62,
- '/' => 63,
- else => error.InvalidCharacterError,
+test "base64: forgiving decode" {
+ const cases = [_]struct { []const u8, []const u8 }{
+ .{ "", "" },
+ .{ " \t\n", "" },
+ .{ "YQ==", "a" },
+ .{ "YQ", "a" },
+ .{ "YWI=", "ab" },
+ .{ "YWI", "ab" },
+ .{ "YWJj", "abc" },
+ .{ " Y W\nJ j\r\x0c", "abc" },
+ .{ "YQ= =", "a" },
+ .{ "YQ==\n", "a" },
+ .{ "YR", "a" },
+ .{ "SGVsbG8gV29y\nbGQh", "Hello World!" },
+ .{ "/P3+/w==", "\xfc\xfd\xfe\xff" },
};
+ for (cases) |case| {
+ const out = try testDecode(case[0]);
+ defer std.testing.allocator.free(out);
+ try std.testing.expectEqualSlices(u8, case[1], out);
+ }
+}
+
+test "base64: forgiving decode rejects" {
+ const cases = [_][]const u8{ "Y", "YQ=", "YQ===", "YWI==", "=", "==", "Y=Q=", "YQ==YQ==", "YQ-_", "YQ\x00=", "YWJjZ" };
+ for (cases) |case| {
+ try std.testing.expectError(error.InvalidCharacterError, testDecode(case));
+ }
+}
+
+test "base64: decode matches std on canonical input" {
+ var prng: std.Random.DefaultPrng = .init(0);
+ const random = prng.random();
+ var raw: [300]u8 = undefined;
+ var enc: [400]u8 = undefined;
+ for (0..raw.len) |len| {
+ random.bytes(raw[0..len]);
+ const encoded = std.base64.standard.Encoder.encode(&enc, raw[0..len]);
+ const out = try testDecode(encoded);
+ defer std.testing.allocator.free(out);
+ try std.testing.expectEqualSlices(u8, raw[0..len], out);
+ }
}