aboutsummaryrefslogtreecommitdiffstats
path: root/src/base32.zig
diff options
context:
space:
mode:
authorLibravatar Rutger Broekhoff2021-05-22 11:20:41 +0200
committerLibravatar Rutger Broekhoff2021-05-22 11:20:41 +0200
commitd33099ba17c6493addc1b320890cf8a98ec0e21d (patch)
tree7701d4f8b525a58bda8a6da6a8c4d9554e04d520 /src/base32.zig
parent39deabb251e81d3a4f4777cec3ce32f5356fe842 (diff)
downloadzig-nkeys-d33099ba17c6493addc1b320890cf8a98ec0e21d.tar.gz
zig-nkeys-d33099ba17c6493addc1b320890cf8a98ec0e21d.zip
Make Base32 encoder more like the standard library
Diffstat (limited to 'src/base32.zig')
-rw-r--r--src/base32.zig40
1 files changed, 24 insertions, 16 deletions
diff --git a/src/base32.zig b/src/base32.zig
index 26622d8..149e5f0 100644
--- a/src/base32.zig
+++ b/src/base32.zig
@@ -7,8 +7,26 @@ pub const Encoder = struct {
7 index: ?usize, 7 index: ?usize,
8 bit_off: u3, 8 bit_off: u3,
9 9
10 fn bitmask(self: *const Self) u8 { 10 pub fn init(buffer: []const u8) Encoder {
11 return @as(u8, 0b11111000) >> self.bit_off; 11 return .{
12 .buffer = buffer,
13 .index = 0,
14 .bit_off = 0,
15 };
16 }
17
18 pub fn calcSize(source_len: usize) usize {
19 const source_len_bits = source_len * 8;
20 return source_len_bits / 5 + (if (source_len_bits % 5 > 0) @as(usize, 1) else 0);
21 }
22
23 pub fn encode(dest: []u8, source: []const u8) []const u8 {
24 const out_len = calcSize(source.len);
25 std.debug.assert(dest.len >= out_len);
26
27 var e = init(source);
28 for (dest) |*b| b.* = e.next() orelse unreachable;
29 return dest[0..out_len];
12 } 30 }
13 31
14 fn n_front_bits(self: *const Self) u3 { 32 fn n_front_bits(self: *const Self) u3 {
@@ -34,7 +52,8 @@ pub const Encoder = struct {
34 // 5 0b00000111 2 0b11100 52 // 5 0b00000111 2 0b11100
35 // 6 0b00000011 3 0b11000 53 // 6 0b00000011 3 0b11000
36 // 7 0b00000001 4 0b10000 54 // 7 0b00000001 4 0b10000
37 const bits = self.buffer[index] & self.bitmask(); 55 const bitmask = @as(u8, 0b11111000) >> self.bit_off;
56 const bits = self.buffer[index] & bitmask;
38 if (self.bit_off >= 4) return @truncate(u5, bits << (self.bit_off - 3)); 57 if (self.bit_off >= 4) return @truncate(u5, bits << (self.bit_off - 3));
39 return @truncate(u5, bits >> (3 - self.bit_off)); 58 return @truncate(u5, bits >> (3 - self.bit_off));
40 } 59 }
@@ -66,10 +85,12 @@ pub const Encoder = struct {
66 return front_bits | back_bits; 85 return front_bits | back_bits;
67 } 86 }
68 87
88 // Returns the corresponding ASCII character for 5 bits of the input.
69 fn char(unencoded: u5) u8 { 89 fn char(unencoded: u5) u8 {
70 return unencoded + (if (unencoded < 26) @as(u8, 'A') else '2' - 26); 90 return unencoded + (if (unencoded < 26) @as(u8, 'A') else '2' - 26);
71 } 91 }
72 92
93 // Returns the next byte of the encoded buffer.
73 pub fn next(self: *Self) ?u8 { 94 pub fn next(self: *Self) ?u8 {
74 const unencoded = self.next_u5() orelse return null; 95 const unencoded = self.next_u5() orelse return null;
75 return char(unencoded); 96 return char(unencoded);
@@ -121,24 +142,11 @@ pub const Decoder = struct {
121 } 142 }
122}; 143};
123 144
124pub fn encodedLen(src_len: usize) usize {
125 const src_len_bits = src_len * 8;
126 return src_len_bits / 5 + (if (src_len_bits % 5 > 0) @as(usize, 1) else 0);
127}
128
129pub fn decodedLen(enc_len: usize) usize { 145pub fn decodedLen(enc_len: usize) usize {
130 const enc_len_bits = enc_len * 5; 146 const enc_len_bits = enc_len * 5;
131 return enc_len_bits / 8; 147 return enc_len_bits / 8;
132} 148}
133 149
134pub fn encode(bs: []const u8, out: []u8) usize {
135 var e = Encoder{ .buffer = bs, .index = 0, .bit_off = 0 };
136 for (out) |*b, i| {
137 b.* = e.next() orelse return i;
138 }
139 return out.len;
140}
141
142pub fn decode(ps: []const u8, out: []u8) DecodeError!usize { 150pub fn decode(ps: []const u8, out: []u8) DecodeError!usize {
143 var d = Decoder{}; 151 var d = Decoder{};
144 var i: usize = 0; 152 var i: usize = 0;