const std = @import("std"); /// base64 encode a byte slice pub fn base64Encode(alloc: std.mem.Allocator, data: []const u8) ![]const u8 { const encoder = std.base64.standard.Encoder; const size = encoder.calcSize(data.len); const buf = try alloc.alloc(u8, size); return encoder.encode(buf, data); } /// base64 decode a string pub fn base64Decode(alloc: std.mem.Allocator, encoded: []const u8) ![]const u8 { const decoder = std.base64.standard.Decoder; const size = decoder.calcSizeForSlice(encoded) catch return error.InvalidBase64; const buf = try alloc.alloc(u8, size); errdefer alloc.free(buf); decoder.decode(buf, encoded) catch return error.InvalidBase64; return buf; } /// URL decode a percent-encoded string (handles %XX and + for space) pub fn urlDecode(alloc: std.mem.Allocator, encoded: []const u8) ![]const u8 { var result = std.ArrayListUnmanaged(u8).empty; errdefer result.deinit(alloc); var i: usize = 0; while (i < encoded.len) { if (encoded[i] == '%' and i + 2 < encoded.len) { const hex = encoded[i + 1 .. i + 3]; const byte = std.fmt.parseInt(u8, hex, 16) catch { try result.append(alloc, encoded[i]); i += 1; continue; }; try result.append(alloc, byte); i += 3; } else if (encoded[i] == '+') { try result.append(alloc, ' '); i += 1; } else { try result.append(alloc, encoded[i]); i += 1; } } return result.toOwnedSlice(alloc); } test "base64 decoding returns exactly owned bytes" { const cases = [_]struct { encoded: []const u8, decoded: []const u8 }{ .{ .encoded = "", .decoded = "" }, .{ .encoded = "Zg==", .decoded = "f" }, .{ .encoded = "Zm8=", .decoded = "fo" }, .{ .encoded = "Zm9v", .decoded = "foo" }, .{ .encoded = "AAEC", .decoded = &.{ 0, 1, 2 } }, }; for (cases) |case| { try std.testing.checkAllAllocationFailures(std.testing.allocator, struct { fn run(alloc: std.mem.Allocator, encoded: []const u8, expected: []const u8) !void { const decoded = try base64Decode(alloc, encoded); defer alloc.free(decoded); try std.testing.expectEqualStrings(expected, decoded); } }.run, .{ case.encoded, case.decoded }); } } test "invalid base64 releases allocated output" { for ([_][]const u8{ "!m9v", "a", "====", "Zg=", "Z===", "Zm9v====" }) |input| { try std.testing.expectError(error.InvalidBase64, base64Decode(std.testing.allocator, input)); } } test "base64 roundtrips arbitrary bytes and padding boundaries" { var prng = std.Random.DefaultPrng.init(0x62a5e64); var input: [256]u8 = undefined; for (0..input.len + 1) |len| { prng.random().bytes(input[0..len]); const encoded = try base64Encode(std.testing.allocator, input[0..len]); defer std.testing.allocator.free(encoded); const decoded = try base64Decode(std.testing.allocator, encoded); defer std.testing.allocator.free(decoded); try std.testing.expectEqualSlices(u8, input[0..len], decoded); } } test "URL decoding releases every allocation on failure" { try std.testing.checkAllAllocationFailures(std.testing.allocator, struct { fn run(alloc: std.mem.Allocator) !void { const decoded = try urlDecode(alloc, "hello+world%21%00%zz%" ++ @as([4096]u8, @splat('x'))); defer alloc.free(decoded); try std.testing.expectEqualStrings("hello world!\x00%zz%" ++ @as([4096]u8, @splat('x')), decoded); } }.run, .{}); }