From 27f2907eae9f2576002860d5ae9a997d008ffdb1 Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Sun, 27 Aug 2023 16:24:14 +0200 Subject: [PATCH] chore: Add zb64 --- .gitignore | 2 + build.zig | 76 ++++++++++++++++++++++++++++++++++++ build.zig.zon | 10 +++++ src/main.zig | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 .gitignore create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 src/main.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af60419 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-out/* +zig-cache/* diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..641555b --- /dev/null +++ b/build.zig @@ -0,0 +1,76 @@ +const std = @import("std"); + +// Although this function looks imperative, note that its job is to +// declaratively construct a build graph that will be executed by an external +// runner. +pub fn build(b: *std.Build) 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 optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); + + const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize }); + + const clap = clap_dep.module("clap"); + + const exe = b.addExecutable(.{ + .name = "zb64", + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + exe.addModule("clap", clap); + + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); + + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // By making the run step depend on the install step, it will be run from the + // installation directory rather than directly from within the cache directory. + // This is not necessary, however, if the application depends on other installed + // files, this ensures they will be present and in the expected location. + run_cmd.step.dependOn(b.getInstallStep()); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + // Creates a step for unit testing. This only builds the test executable + // but does not run it. + const unit_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + const run_unit_tests = b.addRunArtifact(unit_tests); + + // Similar to creating the run step earlier, this exposes a `test` step to + // the `zig build --help` menu, providing a way for the user to request + // running the unit tests. + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_unit_tests.step); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..9f0f87c --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,10 @@ +.{ + .name = "zb64", + .version = "0.0.1", + .dependencies = .{ + .clap = .{ + .url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.7.0.tar.gz", + .hash = "1220f48518ce22882e102255ed3bcdb7aeeb4891f50b2cdd3bd74b5b2e24d3149ba2" + }, + }, +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..90233b4 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,105 @@ +const std = @import("std"); +const clap = @import("clap"); +const base64 = std.base64; + +const debug = std.debug; +const io = std.io; + +//const allocator = std.heap.GeneralPurposeAllocator(); + +pub fn main() !void { + const params = comptime clap.parseParamsComptime( + \\-h, --help Display this help and exit. + \\-d, --decode Decode base64 to string. + \\-e, --encode Encode string to base64. + \\ + ); + + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + + var diag = clap.Diagnostic{}; + var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ + .diagnostic = &diag, + }) catch |err| { + // Report useful error and exit + diag.report(io.getStdErr().writer(), err) catch {}; + return err; + }; + + defer res.deinit(); + + const stdout = std.io.getStdOut(); + + if (res.args.help != 0) + try clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{}); + + if (res.args.encode) |e| { + debug.print("--encode = {s}\n", .{e}); + + const encoder = std.base64.standard.Encoder; + const encoded = try allocator.alloc(u8, encoder.calcSize(e.len)); + + defer allocator.free(encoded); + + try stdout.writer().print("Encoded string: {s}", .{encoder.encode(encoded, e)}); + } + + if (res.args.decode) |d| { + debug.print("--decode = {s}\n", .{d}); + + // const decoder = std.base64.standard.Decoder; + // var decodeSize = decoder.calcSizeForSlice(d) catch |err| { + // std.debug.print("unable decode string: {}\n", .{err}); + // return; + // }; + + // const decoded = allocator.alloc(u8, decodeSize) catch |err| { + // std.debug.print("unable allocate: {}\n", .{err}); + // return; + // }; + + // defer allocator.free(decoded); + + // try decoder.decode(decoded, d); + const decodedString = try base64_decode(allocator, d); + + defer allocator.free(decodedString); + + try stdout.writer().print("Decoded string: {s}", .{decodedString}); + + return; + } + + for (res.positionals) |pos| + debug.print("{s}\n", .{pos}); +} +fn base64_decode(allocator: std.mem.Allocator, string: []const u8) ![]u8 { + const decoder = std.base64.standard.Decoder; + var decodeSize = decoder.calcSizeForSlice(string) catch |err| { + return err; + }; + + const decoded = allocator.alloc(u8, decodeSize) catch |err| { + return err; + }; + + defer allocator.free(decoded); + + decoder.decode(decoded, string) catch |err| { + return err; + }; + + var decodedString = try allocator.dupe(u8, decoded); + + return decodedString; +} + +test "base64_decode returns decoded string" { + var base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA=="; + var expectedString = "hej lksjdlkajsd laksjdlkjasd"; + + const decodedString = base64_decode(base64String); + try std.testing.expectEqual(expectedString, decodedString); +}