diff options
author | Rutger Broekhoff | 2021-05-22 16:56:57 +0200 |
---|---|---|
committer | Rutger Broekhoff | 2021-05-22 16:56:57 +0200 |
commit | d52705d2587a7cc96e535b9d9730ea8cab5594c8 (patch) | |
tree | 517bdc45c0acd2eb52a6514b0b99181d97f88b96 /src/nkeys.zig | |
parent | 6bcacd5e07d715930d43dbfb9ac5cb3d723b4df8 (diff) | |
download | zig-nkeys-d52705d2587a7cc96e535b9d9730ea8cab5594c8.tar.gz zig-nkeys-d52705d2587a7cc96e535b9d9730ea8cab5594c8.zip |
Rewrite Base32 decoder
Diffstat (limited to 'src/nkeys.zig')
-rw-r--r-- | src/nkeys.zig | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/src/nkeys.zig b/src/nkeys.zig index 818fd98..8806a81 100644 --- a/src/nkeys.zig +++ b/src/nkeys.zig | |||
@@ -289,7 +289,7 @@ fn decode( | |||
289 | ) !DecodedNKey(prefix_len, data_len) { | 289 | ) !DecodedNKey(prefix_len, data_len) { |
290 | var raw: [prefix_len + data_len + 2]u8 = undefined; | 290 | var raw: [prefix_len + data_len + 2]u8 = undefined; |
291 | defer wipeBytes(&raw); | 291 | defer wipeBytes(&raw); |
292 | std.debug.assert((try base32.decode(text[0..], &raw)) == raw.len); | 292 | _ = try base32.Decoder.decode(&raw, text[0..]); |
293 | 293 | ||
294 | var checksum = mem.readIntLittle(u16, raw[raw.len - 2 .. raw.len]); | 294 | var checksum = mem.readIntLittle(u16, raw[raw.len - 2 .. raw.len]); |
295 | try crc16.validate(raw[0 .. raw.len - 2], checksum); | 295 | try crc16.validate(raw[0 .. raw.len - 2], checksum); |
@@ -347,25 +347,19 @@ pub fn fromSeed(text: *const text_seed) !SeedKeyPair { | |||
347 | pub fn isValidEncoding(text: []const u8) bool { | 347 | pub fn isValidEncoding(text: []const u8) bool { |
348 | if (text.len < 4) return false; | 348 | if (text.len < 4) return false; |
349 | var made_crc: u16 = 0; | 349 | var made_crc: u16 = 0; |
350 | var dec = base32.Decoder{}; | 350 | var dec = base32.Decoder.init(text); |
351 | var crc_buf: [2]u8 = undefined; | 351 | var crc_buf: [2]u8 = undefined; |
352 | var crc_buf_len: u8 = 0; | 352 | var crc_buf_len: u8 = 0; |
353 | var expect_len: usize = base32.decodedLen(text.len); | 353 | var expect_len: usize = base32.Decoder.calcSize(text.len); |
354 | var wrote_n_total: usize = 0; | 354 | var wrote_n_total: usize = 0; |
355 | for (text) |c, i| { | 355 | while (dec.next() catch return false) |b| { |
356 | var b = (dec.read(c) catch return false) orelse continue; | ||
357 | wrote_n_total += 1; | 356 | wrote_n_total += 1; |
358 | if (crc_buf_len == 2) made_crc = crc16.update(made_crc, &.{crc_buf[0]}); | 357 | if (crc_buf_len == 2) made_crc = crc16.update(made_crc, &.{crc_buf[0]}); |
359 | crc_buf[0] = crc_buf[1]; | 358 | crc_buf[0] = crc_buf[1]; |
360 | crc_buf[1] = b; | 359 | crc_buf[1] = b; |
361 | if (crc_buf_len != 2) crc_buf_len += 1; | 360 | if (crc_buf_len != 2) crc_buf_len += 1; |
362 | } | 361 | } |
363 | if (dec.out_off != 0 and wrote_n_total < expect_len) { | 362 | std.debug.assert(wrote_n_total == expect_len); |
364 | if (crc_buf_len == 2) made_crc = crc16.update(made_crc, &.{crc_buf[0]}); | ||
365 | crc_buf[0] = crc_buf[1]; | ||
366 | crc_buf[1] = dec.buf; | ||
367 | if (crc_buf_len != 2) crc_buf_len += 1; | ||
368 | } | ||
369 | if (crc_buf_len != 2) unreachable; | 363 | if (crc_buf_len != 2) unreachable; |
370 | var got_crc = mem.readIntLittle(u16, &crc_buf); | 364 | var got_crc = mem.readIntLittle(u16, &crc_buf); |
371 | return made_crc == got_crc; | 365 | return made_crc == got_crc; |