diff options
Diffstat (limited to 'src/base32.zig')
-rw-r--r-- | src/base32.zig | 40 |
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 | ||
124 | pub 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 | |||
129 | pub fn decodedLen(enc_len: usize) usize { | 145 | pub 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 | ||
134 | pub 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 | |||
142 | pub fn decode(ps: []const u8, out: []u8) DecodeError!usize { | 150 | pub 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; |