diff options
| -rw-r--r-- | build.zig | 49 | ||||
| -rw-r--r-- | src/base32.zig | 14 | ||||
| -rw-r--r-- | src/crc16.zig | 4 | ||||
| -rw-r--r-- | src/main.zig | 12 | ||||
| -rw-r--r-- | tool/znk.zig | 4 | 
5 files changed, 49 insertions, 34 deletions
| @@ -1,41 +1,56 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); | 
| 2 | 2 | ||
| 3 | pub fn build(b: *std.build.Builder) !void { | 3 | pub fn build(b: *std.build.Builder) !void { | 
| 4 | const mode = b.standardReleaseOptions(); | ||
| 5 | const target = b.standardTargetOptions(.{}); | 4 | const target = b.standardTargetOptions(.{}); | 
| 5 | const optimize = b.standardOptimizeOption(.{}); | ||
| 6 | 6 | ||
| 7 | const lib_tests = b.addTest("src/main.zig"); | 7 | const lib_tests = b.addTest(.{ | 
| 8 | lib_tests.setBuildMode(mode); | 8 | .root_source_file = .{ .path = "src/main.zig" }, | 
| 9 | .target = target, | ||
| 10 | .optimize = optimize, | ||
| 11 | }); | ||
| 12 | const run_lib_tests = b.addRunArtifact(lib_tests); | ||
| 9 | 13 | ||
| 10 | const test_step = b.step("test", "Run library tests"); | 14 | const test_step = b.step("test", "Run library tests"); | 
| 11 | test_step.dependOn(&lib_tests.step); | 15 | test_step.dependOn(&run_lib_tests.step); | 
| 12 | 16 | ||
| 13 | const znk_version = "0.2.1"; | 17 | const znk_version = "0.2.1"; | 
| 14 | 18 | ||
| 15 | const znk_options = b.addOptions(); | 19 | const znk_options = b.addOptions(); | 
| 16 | znk_options.addOption([]const u8, "version", znk_version); | 20 | znk_options.addOption([]const u8, "version", znk_version); | 
| 17 | 21 | ||
| 18 | const znk_tests = b.addTest("tool/znk.zig"); | 22 | const nkeys_module = b.addModule("nkeys", .{ | 
| 19 | znk_tests.setBuildMode(mode); | 23 | .source_file = .{ .path = "src/main.zig" }, | 
| 20 | znk_tests.addPackagePath("nkeys", "src/main.zig"); | 24 | }); | 
| 25 | |||
| 26 | const znk_tests = b.addTest(.{ | ||
| 27 | .root_source_file = .{ .path = "tool/znk.zig" }, | ||
| 28 | .target = target, | ||
| 29 | .optimize = optimize, | ||
| 30 | }); | ||
| 31 | znk_tests.addModule("nkeys", nkeys_module); | ||
| 21 | znk_tests.addOptions("build_options", znk_options); | 32 | znk_tests.addOptions("build_options", znk_options); | 
| 33 | const run_znk_tests = b.addRunArtifact(znk_tests); | ||
| 22 | 34 | ||
| 23 | const znk_test_step = b.step("test-znk", "Run znk tests"); | 35 | const znk_test_step = b.step("test-znk", "Run znk tests"); | 
| 24 | znk_test_step.dependOn(&znk_tests.step); | 36 | znk_test_step.dependOn(&run_znk_tests.step); | 
| 25 | 37 | ||
| 26 | const znk = b.addExecutable("znk", "tool/znk.zig"); | 38 | const znk = b.addExecutable(.{ | 
| 27 | znk.setBuildMode(mode); | 39 | .name = "znk", | 
| 28 | znk.setTarget(target); | 40 | .root_source_file = .{ .path = "tool/znk.zig" }, | 
| 29 | znk.addPackagePath("nkeys", "src/main.zig"); | 41 | .target = target, | 
| 42 | .optimize = optimize, | ||
| 43 | }); | ||
| 44 | znk.addModule("nkeys", nkeys_module); | ||
| 30 | znk.addOptions("build_options", znk_options); | 45 | znk.addOptions("build_options", znk_options); | 
| 31 | 46 | ||
| 32 | const znk_install = b.addInstallArtifact(znk); | 47 | b.installArtifact(znk); | 
| 33 | 48 | ||
| 34 | const znk_step = b.step("znk", "Build znk"); | 49 | const znk_step = b.step("znk", "Build znk"); | 
| 35 | znk_step.dependOn(&znk_install.step); | 50 | znk_step.dependOn(b.getInstallStep()); | 
| 36 | 51 | ||
| 37 | const znk_run_cmd = znk.run(); | 52 | const znk_run_cmd = b.addRunArtifact(znk); | 
| 38 | znk_run_cmd.step.dependOn(&znk_install.step); | 53 | znk_run_cmd.step.dependOn(b.getInstallStep()); | 
| 39 | if (b.args) |args| { | 54 | if (b.args) |args| { | 
| 40 | znk_run_cmd.addArgs(args); | 55 | znk_run_cmd.addArgs(args); | 
| 41 | } | 56 | } | 
| diff --git a/src/base32.zig b/src/base32.zig index 4e79a73..bce6254 100644 --- a/src/base32.zig +++ b/src/base32.zig | |||
| @@ -66,8 +66,8 @@ pub const Encoder = struct { | |||
| 66 | fn frontBits(self: *const Self) ?u5 { | 66 | fn frontBits(self: *const Self) ?u5 { | 
| 67 | const index = self.index orelse return null; | 67 | const index = self.index orelse return null; | 
| 68 | const bits = self.buffer[index]; | 68 | const bits = self.buffer[index]; | 
| 69 | if (self.bit_off >= 4) return @truncate(u5, bits << (self.bit_off - 3)); | 69 | if (self.bit_off >= 4) return @as(u5, @truncate(bits << (self.bit_off - 3))); | 
| 70 | return @truncate(u5, bits >> (3 - self.bit_off)); | 70 | return @as(u5, @truncate(bits >> (3 - self.bit_off))); | 
| 71 | } | 71 | } | 
| 72 | 72 | ||
| 73 | /// Get the bits of `self.buffer[self.index]` with the maximum amount specified by the `bits` parameter, | 73 | /// Get the bits of `self.buffer[self.index]` with the maximum amount specified by the `bits` parameter, | 
| @@ -85,7 +85,7 @@ pub const Encoder = struct { | |||
| 85 | fn backBits(self: *const Self, bits: u3) u5 { | 85 | fn backBits(self: *const Self, bits: u3) u5 { | 
| 86 | std.debug.assert(bits <= 5); | 86 | std.debug.assert(bits <= 5); | 
| 87 | if (bits == 0 or self.index == null) return 0; | 87 | if (bits == 0 or self.index == null) return 0; | 
| 88 | return @truncate(u5, self.buffer[self.index.?] >> (7 - bits + 1)); | 88 | return @as(u5, @truncate(self.buffer[self.index.?] >> (7 - bits + 1))); | 
| 89 | } | 89 | } | 
| 90 | 90 | ||
| 91 | /// Get the next 5-bit integer, read from `self.buffer`. | 91 | /// Get the next 5-bit integer, read from `self.buffer`. | 
| @@ -172,10 +172,10 @@ pub const Decoder = struct { | |||
| 172 | /// Get a character from the buffer. | 172 | /// Get a character from the buffer. | 
| 173 | fn decodeChar(c: u8) DecodeError!u5 { | 173 | fn decodeChar(c: u8) DecodeError!u5 { | 
| 174 | if (c >= 'A' and c <= 'Z') { | 174 | if (c >= 'A' and c <= 'Z') { | 
| 175 | return @truncate(u5, c - @as(u8, 'A')); | 175 | return @as(u5, @truncate(c - @as(u8, 'A'))); | 
| 176 | } else if (c >= '2' and c <= '9') { | 176 | } else if (c >= '2' and c <= '9') { | 
| 177 | // '2' -> 26 | 177 | // '2' -> 26 | 
| 178 | return @truncate(u5, c - @as(u8, '2') + 26); | 178 | return @as(u5, @truncate(c - @as(u8, '2') + 26)); | 
| 179 | } else { | 179 | } else { | 
| 180 | return error.CorruptInput; | 180 | return error.CorruptInput; | 
| 181 | } | 181 | } | 
| @@ -200,7 +200,7 @@ pub const Decoder = struct { | |||
| 200 | // Calculate how many bits of the decoded remain when we've written part of it to the buffer. | 200 | // Calculate how many bits of the decoded remain when we've written part of it to the buffer. | 
| 201 | const c_remaining_len = 5 - buf_write_len; | 201 | const c_remaining_len = 5 - buf_write_len; | 
| 202 | // Write (the first) part of the decoded character to the buffer. | 202 | // Write (the first) part of the decoded character to the buffer. | 
| 203 | self.buf |= (@as(u8, c) << 3) >> @truncate(u3, self.buf_len); | 203 | self.buf |= (@as(u8, c) << 3) >> @as(u3, @truncate(self.buf_len)); | 
| 204 | self.buf_len += buf_write_len; | 204 | self.buf_len += buf_write_len; | 
| 205 | if (self.buf_len == 8) { | 205 | if (self.buf_len == 8) { | 
| 206 | // The buffer is full, we can return a byte. | 206 | // The buffer is full, we can return a byte. | 
| @@ -210,7 +210,7 @@ pub const Decoder = struct { | |||
| 210 | if (buf_write_len != 5) { | 210 | if (buf_write_len != 5) { | 
| 211 | // We didn't write the entire decoded character to the buffer. | 211 | // We didn't write the entire decoded character to the buffer. | 
| 212 | // Write the remaining part to the beginning of the buffer. | 212 | // Write the remaining part to the beginning of the buffer. | 
| 213 | self.buf = @as(u8, c) << @truncate(u3, buf_write_len + 3); | 213 | self.buf = @as(u8, c) << @as(u3, @truncate(buf_write_len + 3)); | 
| 214 | } | 214 | } | 
| 215 | return ret; | 215 | return ret; | 
| 216 | } | 216 | } | 
| diff --git a/src/crc16.zig b/src/crc16.zig index 3ea5a15..1257ea2 100644 --- a/src/crc16.zig +++ b/src/crc16.zig | |||
| @@ -7,7 +7,7 @@ const crc16tab: [256]u16 = tab: { | |||
| 7 | const poly: u32 = 0x1021; | 7 | const poly: u32 = 0x1021; | 
| 8 | var table: [256]u16 = undefined; | 8 | var table: [256]u16 = undefined; | 
| 9 | 9 | ||
| 10 | for (table) |*crc, i| { | 10 | for (&table, 0..) |*crc, i| { | 
| 11 | crc.* = @as(u16, i) << 8; | 11 | crc.* = @as(u16, i) << 8; | 
| 12 | var j = 0; | 12 | var j = 0; | 
| 13 | while (j < 8) : (j += 1) { | 13 | while (j < 8) : (j += 1) { | 
| @@ -25,7 +25,7 @@ const crc16tab: [256]u16 = tab: { | |||
| 25 | pub fn update(crc: u16, with_data: []const u8) u16 { | 25 | pub fn update(crc: u16, with_data: []const u8) u16 { | 
| 26 | var new_crc = crc; | 26 | var new_crc = crc; | 
| 27 | for (with_data) |b| { | 27 | for (with_data) |b| { | 
| 28 | new_crc = (new_crc << 8) ^ crc16tab[@truncate(u8, new_crc >> 8) ^ b]; | 28 | new_crc = (new_crc << 8) ^ crc16tab[@as(u8, @truncate(new_crc >> 8)) ^ b]; | 
| 29 | } | 29 | } | 
| 30 | return new_crc; | 30 | return new_crc; | 
| 31 | } | 31 | } | 
| diff --git a/src/main.zig b/src/main.zig index 6f68295..18c86d1 100644 --- a/src/main.zig +++ b/src/main.zig | |||
| @@ -393,7 +393,7 @@ pub fn areKeySectionContentsValid(contents: []const u8) bool { | |||
| 393 | return true; | 393 | return true; | 
| 394 | } | 394 | } | 
| 395 | 395 | ||
| 396 | pub fn findKeySection(line_it: *std.mem.SplitIterator(u8)) ?[]const u8 { | 396 | pub fn findKeySection(line_it: *std.mem.SplitIterator(u8, .scalar)) ?[]const u8 { | 
| 397 | while (true) { | 397 | while (true) { | 
| 398 | const opening_line = line_it.next() orelse return null; | 398 | const opening_line = line_it.next() orelse return null; | 
| 399 | if (!isKeySectionBarrier(opening_line, true)) continue; | 399 | if (!isKeySectionBarrier(opening_line, true)) continue; | 
| @@ -409,12 +409,12 @@ pub fn findKeySection(line_it: *std.mem.SplitIterator(u8)) ?[]const u8 { | |||
| 409 | } | 409 | } | 
| 410 | 410 | ||
| 411 | pub fn parseDecoratedJwt(contents: []const u8) []const u8 { | 411 | pub fn parseDecoratedJwt(contents: []const u8) []const u8 { | 
| 412 | var line_it = mem.split(u8, contents, "\n"); | 412 | var line_it = mem.splitScalar(u8, contents, '\n'); | 
| 413 | return findKeySection(&line_it) orelse return contents; | 413 | return findKeySection(&line_it) orelse return contents; | 
| 414 | } | 414 | } | 
| 415 | 415 | ||
| 416 | pub fn parseDecoratedNkey(contents: []const u8) NoNkeySeedFoundError!SeedKeyPair { | 416 | pub fn parseDecoratedNkey(contents: []const u8) NoNkeySeedFoundError!SeedKeyPair { | 
| 417 | var line_it = mem.split(u8, contents, "\n"); | 417 | var line_it = mem.splitScalar(u8, contents, '\n'); | 
| 418 | var seed: ?[]const u8 = null; | 418 | var seed: ?[]const u8 = null; | 
| 419 | if (findKeySection(&line_it) != null) | 419 | if (findKeySection(&line_it) != null) | 
| 420 | seed = findKeySection(&line_it); | 420 | seed = findKeySection(&line_it); | 
| @@ -444,8 +444,8 @@ fn isValidCredsNkey(text: []const u8) bool { | |||
| 444 | fn findNkey(text: []const u8) ?[]const u8 { | 444 | fn findNkey(text: []const u8) ?[]const u8 { | 
| 445 | var line_it = std.mem.split(u8, text, "\n"); | 445 | var line_it = std.mem.split(u8, text, "\n"); | 
| 446 | while (line_it.next()) |line| { | 446 | while (line_it.next()) |line| { | 
| 447 | for (line) |c, i| { | 447 | for (line, 0..) |c, i| { | 
| 448 | if (!ascii.isSpace(c)) { | 448 | if (!ascii.isWhitespace(c)) { | 
| 449 | if (isValidCredsNkey(line[i..])) return line[i..]; | 449 | if (isValidCredsNkey(line[i..])) return line[i..]; | 
| 450 | break; | 450 | break; | 
| 451 | } | 451 | } | 
| @@ -554,7 +554,7 @@ test "different key types" { | |||
| 554 | 554 | ||
| 555 | test "validation" { | 555 | test "validation" { | 
| 556 | const roles = @typeInfo(Role).Enum.fields; | 556 | const roles = @typeInfo(Role).Enum.fields; | 
| 557 | inline for (roles) |field, i| { | 557 | inline for (roles, 0..) |field, i| { | 
| 558 | const role = @field(Role, field.name); | 558 | const role = @field(Role, field.name); | 
| 559 | const next_role = next: { | 559 | const next_role = next: { | 
| 560 | const next_field_i = if (i == roles.len - 1) 0 else i + 1; | 560 | const next_field_i = if (i == roles.len - 1) 0 else i + 1; | 
| diff --git a/tool/znk.zig b/tool/znk.zig index fcd03ff..554f191 100644 --- a/tool/znk.zig +++ b/tool/znk.zig | |||
| @@ -38,7 +38,7 @@ const usage = | |||
| 38 | 38 | ||
| 39 | pub fn main() anyerror!void { | 39 | pub fn main() anyerror!void { | 
| 40 | var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; | 40 | var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; | 
| 41 | defer std.debug.assert(!general_purpose_allocator.deinit()); | 41 | defer std.debug.assert(general_purpose_allocator.deinit() == .ok); | 
| 42 | const gpa = general_purpose_allocator.allocator(); | 42 | const gpa = general_purpose_allocator.allocator(); | 
| 43 | 43 | ||
| 44 | var arena_instance = std.heap.ArenaAllocator.init(gpa); | 44 | var arena_instance = std.heap.ArenaAllocator.init(gpa); | 
| @@ -475,7 +475,7 @@ fn PrefixKeyGenerator(comptime EntropyReaderType: type) type { | |||
| 475 | 475 | ||
| 476 | fn toUpper(allocator: Allocator, slice: []const u8) ![]u8 { | 476 | fn toUpper(allocator: Allocator, slice: []const u8) ![]u8 { | 
| 477 | const result = try allocator.alloc(u8, slice.len); | 477 | const result = try allocator.alloc(u8, slice.len); | 
| 478 | for (slice) |c, i| result[i] = ascii.toUpper(c); | 478 | for (slice, 0..) |c, i| result[i] = ascii.toUpper(c); | 
| 479 | return result; | 479 | return result; | 
| 480 | } | 480 | } | 
| 481 | 481 | ||