diff --git a/set1/challenge2_fixed_xor/src/main.zig b/set1/challenge2_fixed_xor/src/main.zig index b9251ae..45ec134 100644 --- a/set1/challenge2_fixed_xor/src/main.zig +++ b/set1/challenge2_fixed_xor/src/main.zig @@ -1,17 +1,15 @@ const std = @import("std"); -fn readHex(allocator: std.mem.Allocator, max_size: usize) ![]u8 { - const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader(); - - var inputbuffer = std.ArrayList(u8).init(allocator); - defer inputbuffer.deinit(); +fn readHex(allocator: std.mem.Allocator, reader: anytype, max_size: usize) !?[]u8 { var resultbuffer = std.ArrayList(u8).init(allocator); defer resultbuffer.deinit(); while (true) { - try stdin.readUntilDelimiterArrayList(&inputbuffer, '\n', max_size * 2); - try resultbuffer.resize(inputbuffer.items.len / 2); - _ = std.fmt.hexToBytes(resultbuffer.items, inputbuffer.items) catch { + const line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', max_size * 2); + const hex = line orelse return null; + defer allocator.free(hex); + try resultbuffer.resize(hex.len / 2); + _ = std.fmt.hexToBytes(resultbuffer.items, hex) catch { std.log.err("not a valid hexadecimal", .{}); continue; }; @@ -23,13 +21,14 @@ pub fn main() anyerror!void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = gpa.allocator(); + const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader(); const stdout = std.io.getStdOut().writer(); while (true) { - const input1 = try readHex(allocator, 4096); + const input1 = (try readHex(allocator, stdin, 4096)) orelse break; defer allocator.free(input1); - const input2 = try readHex(allocator, 4096); + const input2 = (try readHex(allocator, stdin, 4096)) orelse break; defer allocator.free(input2); if (input1.len != input2.len) { diff --git a/set1/challenge3_xor_cipher/src/main.zig b/set1/challenge3_xor_cipher/src/main.zig index a9193a4..53ea31c 100644 --- a/set1/challenge3_xor_cipher/src/main.zig +++ b/set1/challenge3_xor_cipher/src/main.zig @@ -1,17 +1,15 @@ const std = @import("std"); -fn readHex(allocator: std.mem.Allocator, max_size: usize) ![]u8 { - const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader(); - - var inputbuffer = std.ArrayList(u8).init(allocator); - defer inputbuffer.deinit(); +fn readHex(allocator: std.mem.Allocator, reader: anytype, max_size: usize) !?[]u8 { var resultbuffer = std.ArrayList(u8).init(allocator); defer resultbuffer.deinit(); while (true) { - try stdin.readUntilDelimiterArrayList(&inputbuffer, '\n', max_size * 2); - try resultbuffer.resize(inputbuffer.items.len / 2); - _ = std.fmt.hexToBytes(resultbuffer.items, inputbuffer.items) catch { + const line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', max_size * 2); + const hex = line orelse return null; + defer allocator.free(hex); + try resultbuffer.resize(hex.len / 2); + _ = std.fmt.hexToBytes(resultbuffer.items, hex) catch { std.log.err("not a valid hexadecimal", .{}); continue; }; @@ -42,10 +40,11 @@ pub fn main() anyerror!void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = gpa.allocator(); + const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader(); const stdout = std.io.getStdOut().writer(); while (true) { - const ciphertext = try readHex(allocator, 4096); + const ciphertext = (try readHex(allocator, stdin, 4096)) orelse break; defer allocator.free(ciphertext); var plaintext = try allocator.alloc(u8, ciphertext.len);