more work on the algorithm

This commit is contained in:
Andre Henriques 2023-06-12 22:02:53 +01:00
parent ed4277ac8f
commit 3e9ddba3a3

View File

@ -59,12 +59,14 @@ const BitWalker = struct {
while (j < new_byte_pos) : (j += 1) { while (j < new_byte_pos) : (j += 1) {
new_mask = @shlExact(new_mask, 1) + 1; new_mask = @shlExact(new_mask, 1) + 1;
} }
//print("{} mask: {b}, new_mask: {b}", .{ bits, old_mask, new_mask });
//print("here {b} {b}", .{ byte, old_mask });
//print("here_new {b} {b}", .{ next_byte, new_mask });
var result = @shrExact(byte & old_mask, self.in_byte_position) + @shlExact(next_byte & new_mask, @intCast(u3, 8 - @as(u4, self.in_byte_position))); var result = @shrExact(byte & old_mask, self.in_byte_position) + @shlExact(next_byte & new_mask, @intCast(u3, 8 - @as(u4, self.in_byte_position)));
print("mask: {b}, new_mask: {b}", .{ old_mask, new_mask });
print("here {b} {b}", .{ byte, old_mask });
print("here_new {b} {b}", .{ next_byte, new_mask });
print("result {}", .{result});
self.position += 1; self.position += 1;
self.in_byte_position = new_byte_pos; self.in_byte_position = new_byte_pos;
@ -93,6 +95,48 @@ const BitWalker = struct {
} }
}; };
fn HuffmanGraph(valueType: type) type {
return struct {
const Node = struct {
left: ?Node,
right: ?Node,
value: valueType,
fn init() Node {}
};
head: Node,
};
}
const DynamicDecoder = struct {
const Self = @This();
len_to_read: usize,
codes: [19]u3,
walker: *BitWalker,
fn init(walker: *BitWalker, len_to_read: usize) !Self {
const list: [19]u8 = .{ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
var codes: [19]u3 = .{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (0..(len_to_read + 4)) |i| {
print("{} {}", .{ walker.position, walker.in_byte_position });
var data: u3 = @intCast(u3, try walker.walk(3));
if (data == 0) {
continue;
}
print("{} - {}", .{ list[i], data });
codes[i] = data;
}
return .{
.len_to_read = len_to_read,
.codes = codes,
.walker = walker,
};
}
};
const LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50; const LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
const ZipFileHeader = struct { const ZipFileHeader = struct {
version: u16, version: u16,
@ -171,9 +215,6 @@ const ZipFileHeader = struct {
var lastBlock = try bitw.walk(1) == 1; var lastBlock = try bitw.walk(1) == 1;
var blockType = try bitw.walk(2); var blockType = try bitw.walk(2);
var number_of_literal_codes = try bitw.walk(5);
var number_of_dist_codes = try bitw.walk(5);
var number_of_length_codes = try bitw.walk(5);
if (lastBlock) { if (lastBlock) {
print("last block", .{}); print("last block", .{});
@ -187,9 +228,17 @@ const ZipFileHeader = struct {
return error.unsuported_block_type; return error.unsuported_block_type;
} }
var number_of_literal_codes = try bitw.walk(5);
var number_of_dist_codes = try bitw.walk(5);
var number_of_length_codes = try bitw.walk(5);
print("number of literal codes: {}", .{number_of_literal_codes}); print("number of literal codes: {}", .{number_of_literal_codes});
print("number of dist codes: {}", .{number_of_dist_codes}); print("number of dist codes: {}", .{number_of_dist_codes});
print("number_of_length_coes: {}", .{number_of_length_codes}); print("number_of_length_codes: {}", .{number_of_length_codes});
var dynamic_decoder = try DynamicDecoder.init(&bitw, number_of_length_codes);
_ = dynamic_decoder;
self.decompressed = true; self.decompressed = true;
} }