-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.zig
113 lines (96 loc) · 3.08 KB
/
decode.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const std = @import("std");
const Allocator = std.mem.Allocator;
const testing = std.testing;
const Node = @import("node.zig").Node;
const BitVec = @import("./bitvec.zig");
const Self = @This();
input: []const u8,
allocator: Allocator,
pub const DecodeError = error{ UnexpectedEof, OutOfMemory, InvalidTreePath };
pub fn init(allocator: Allocator, input: []const u8) Self {
return .{ .allocator = allocator, .input = input };
}
pub fn decode(self: *Self) DecodeError!ArrayList(u8) {
var bits = BitVec.init(self.allocator);
defer bits.deinit();
bits.bit_len = self.input.len << 3;
try bits.bytes.appendSlice(self.input);
var bits_iter = bits.iterator();
const byte_len: u64 = bits_iter.take(64) catch {
return DecodeError.UnexpectedEof;
};
const root = try self.parseTree(&bits_iter);
defer root.deinit(self.allocator);
return self.decodeBytes(root, &bits_iter, byte_len);
}
fn parseTree(self: *Self, bits_iter: *BitVec.Iterator) DecodeError!*const Node {
const bit = bits_iter.next() orelse {
return DecodeError.UnexpectedEof;
};
switch (bit) {
0b1 => {
const byte: u8 = bits_iter.take(8) catch {
return DecodeError.UnexpectedEof;
};
return Node.leaf(self.allocator, byte);
},
0b0 => {
const left = try self.parseTree(bits_iter);
const right = try self.parseTree(bits_iter);
return Node.internal(self.allocator, left, right);
},
}
}
const ArrayList = std.ArrayList;
fn decodeBytes(
self: *Self,
root: *const Node,
bits_iter: *BitVec.Iterator,
length: u64,
) DecodeError!ArrayList(u8) {
var bytes = try ArrayList(u8).initCapacity(
self.allocator,
@as(usize, @intCast(length)),
);
var bytes_left = length;
var current_node = root;
while (true) {
switch (current_node.*) {
.leaf => |leaf_node| {
try bytes.append(leaf_node.byte);
bytes_left -= 1;
current_node = root;
if (bytes_left == 0) {
break;
}
},
.internal => |internal_node| {
const bit = bits_iter.next() orelse {
return DecodeError.UnexpectedEof;
};
current_node = switch (bit) {
0b1 => internal_node.right,
0b0 => internal_node.left,
};
},
}
}
return bytes;
}
test "parse tree" {
const serialized_tree = [_]u8{
0b01001000, 0b00010110, 0b11001101, 0b10110010,
0b01110111, 0b10110010, 0b11111001, 0b00100110,
0b01001000, 0b0111001,
};
var bitvec = BitVec.init(testing.allocator);
defer bitvec.deinit();
for (serialized_tree) |byte| {
try bitvec.pushBits(8, byte);
}
var it = bitvec.iterator();
var decoder = Self.init(testing.allocator, "any");
var root = try decoder.parseTree(&it);
defer root.deinit(testing.allocator);
std.debug.print("{s}", .{root});
}