From 19c48f9d58e790f631379750ada9c5c647240ee4 Mon Sep 17 00:00:00 2001 From: Thomas Lindner Date: Sun, 6 Feb 2022 20:41:28 +0100 Subject: [PATCH] solve Challenge 2 of Set 1 --- set1/challenge2_fixed_xor/build.zig | 27 ++++++++++++++ set1/challenge2_fixed_xor/src/main.zig | 50 ++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 set1/challenge2_fixed_xor/build.zig create mode 100644 set1/challenge2_fixed_xor/src/main.zig diff --git a/set1/challenge2_fixed_xor/build.zig b/set1/challenge2_fixed_xor/build.zig new file mode 100644 index 0000000..727e5b0 --- /dev/null +++ b/set1/challenge2_fixed_xor/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("challenge2_fixed_xor", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/set1/challenge2_fixed_xor/src/main.zig b/set1/challenge2_fixed_xor/src/main.zig new file mode 100644 index 0000000..b9251ae --- /dev/null +++ b/set1/challenge2_fixed_xor/src/main.zig @@ -0,0 +1,50 @@ +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(); + 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 { + std.log.err("not a valid hexadecimal", .{}); + continue; + }; + return resultbuffer.toOwnedSlice(); + } +} + +pub fn main() anyerror!void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + const stdout = std.io.getStdOut().writer(); + + while (true) { + const input1 = try readHex(allocator, 4096); + defer allocator.free(input1); + + const input2 = try readHex(allocator, 4096); + defer allocator.free(input2); + + if (input1.len != input2.len) { + std.log.err("not the same length", .{}); + continue; + } + + var output = try std.ArrayList(u8).initCapacity(allocator, input1.len); + defer output.deinit(); + + var i: usize = 0; + while (i < input1.len) : (i += 1) { + try output.append(input1[i] ^ input2[i]); + } + + try stdout.print("{}\n", .{std.fmt.fmtSliceHexLower(output.items)}); + } +}