chore: Add zb64

This commit is contained in:
Kalle Carlbark 2023-08-27 16:24:14 +02:00
commit 27f2907eae
No known key found for this signature in database
4 changed files with 193 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
zig-out/*
zig-cache/*

76
build.zig Normal file
View file

@ -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);
}

10
build.zig.zon Normal file
View file

@ -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"
},
},
}

105
src/main.zig Normal file
View file

@ -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 <str> Decode base64 to string.
\\-e, --encode <str> Encode string to base64.
\\<str>
);
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var diag = clap.Diagnostic{};
var res = clap.parse(clap.Help, &params, 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, &params, .{});
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);
}