|
| 1 | +const std = @import("std"); |
| 2 | +const block = @import("xz/block.zig"); |
| 3 | +const Allocator = std.mem.Allocator; |
| 4 | +const Crc32 = std.hash.Crc32; |
| 5 | + |
| 6 | +pub const Check = enum(u4) { |
| 7 | + none = 0x00, |
| 8 | + crc32 = 0x01, |
| 9 | + crc64 = 0x04, |
| 10 | + sha256 = 0x0A, |
| 11 | + _, |
| 12 | +}; |
| 13 | + |
| 14 | +fn readStreamFlags(reader: anytype, check: *Check) !void { |
| 15 | + var bit_reader = std.io.bitReader(.Little, reader); |
| 16 | + |
| 17 | + const reserved1 = try bit_reader.readBitsNoEof(u8, 8); |
| 18 | + if (reserved1 != 0) |
| 19 | + return error.CorruptInput; |
| 20 | + |
| 21 | + check.* = @intToEnum(Check, try bit_reader.readBitsNoEof(u4, 4)); |
| 22 | + |
| 23 | + const reserved2 = try bit_reader.readBitsNoEof(u4, 4); |
| 24 | + if (reserved2 != 0) |
| 25 | + return error.CorruptInput; |
| 26 | +} |
| 27 | + |
| 28 | +pub fn decompress(allocator: Allocator, reader: anytype) !Decompress(@TypeOf(reader)) { |
| 29 | + return Decompress(@TypeOf(reader)).init(allocator, reader); |
| 30 | +} |
| 31 | + |
| 32 | +pub fn Decompress(comptime ReaderType: type) type { |
| 33 | + return struct { |
| 34 | + const Self = @This(); |
| 35 | + |
| 36 | + pub const Error = ReaderType.Error || block.Decoder(ReaderType).Error; |
| 37 | + pub const Reader = std.io.Reader(*Self, Error, read); |
| 38 | + |
| 39 | + allocator: Allocator, |
| 40 | + block_decoder: block.Decoder(ReaderType), |
| 41 | + in_reader: ReaderType, |
| 42 | + |
| 43 | + fn init(allocator: Allocator, source: ReaderType) !Self { |
| 44 | + const magic = try source.readBytesNoEof(6); |
| 45 | + if (!std.mem.eql(u8, &magic, &.{ 0xFD, '7', 'z', 'X', 'Z', 0x00 })) |
| 46 | + return error.BadHeader; |
| 47 | + |
| 48 | + var check: Check = undefined; |
| 49 | + const hash_a = blk: { |
| 50 | + var hasher = std.compress.hashedReader(source, Crc32.init()); |
| 51 | + try readStreamFlags(hasher.reader(), &check); |
| 52 | + break :blk hasher.hasher.final(); |
| 53 | + }; |
| 54 | + |
| 55 | + const hash_b = try source.readIntLittle(u32); |
| 56 | + if (hash_a != hash_b) |
| 57 | + return error.WrongChecksum; |
| 58 | + |
| 59 | + return Self{ |
| 60 | + .allocator = allocator, |
| 61 | + .block_decoder = try block.decoder(allocator, source, check), |
| 62 | + .in_reader = source, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + pub fn deinit(self: *Self) void { |
| 67 | + self.block_decoder.deinit(); |
| 68 | + } |
| 69 | + |
| 70 | + pub fn reader(self: *Self) Reader { |
| 71 | + return .{ .context = self }; |
| 72 | + } |
| 73 | + |
| 74 | + pub fn read(self: *Self, buffer: []u8) Error!usize { |
| 75 | + if (buffer.len == 0) |
| 76 | + return 0; |
| 77 | + |
| 78 | + const r = try self.block_decoder.read(buffer); |
| 79 | + if (r != 0) |
| 80 | + return r; |
| 81 | + |
| 82 | + const index_size = blk: { |
| 83 | + var hasher = std.compress.hashedReader(self.in_reader, Crc32.init()); |
| 84 | + hasher.hasher.update(&[1]u8{0x00}); |
| 85 | + |
| 86 | + var counter = std.io.countingReader(hasher.reader()); |
| 87 | + counter.bytes_read += 1; |
| 88 | + |
| 89 | + const counting_reader = counter.reader(); |
| 90 | + |
| 91 | + const record_count = try std.leb.readULEB128(u64, counting_reader); |
| 92 | + if (record_count != self.block_decoder.block_count) |
| 93 | + return error.CorruptInput; |
| 94 | + |
| 95 | + var i: usize = 0; |
| 96 | + while (i < record_count) : (i += 1) { |
| 97 | + // TODO: validate records |
| 98 | + _ = try std.leb.readULEB128(u64, counting_reader); |
| 99 | + _ = try std.leb.readULEB128(u64, counting_reader); |
| 100 | + } |
| 101 | + |
| 102 | + while (counter.bytes_read % 4 != 0) { |
| 103 | + if (try counting_reader.readByte() != 0) |
| 104 | + return error.CorruptInput; |
| 105 | + } |
| 106 | + |
| 107 | + const hash_a = hasher.hasher.final(); |
| 108 | + const hash_b = try counting_reader.readIntLittle(u32); |
| 109 | + if (hash_a != hash_b) |
| 110 | + return error.WrongChecksum; |
| 111 | + |
| 112 | + break :blk counter.bytes_read; |
| 113 | + }; |
| 114 | + |
| 115 | + const hash_a = try self.in_reader.readIntLittle(u32); |
| 116 | + |
| 117 | + const hash_b = blk: { |
| 118 | + var hasher = std.compress.hashedReader(self.in_reader, Crc32.init()); |
| 119 | + const hashed_reader = hasher.reader(); |
| 120 | + |
| 121 | + const backward_size = (try hashed_reader.readIntLittle(u32) + 1) * 4; |
| 122 | + if (backward_size != index_size) |
| 123 | + return error.CorruptInput; |
| 124 | + |
| 125 | + var check: Check = undefined; |
| 126 | + try readStreamFlags(hashed_reader, &check); |
| 127 | + |
| 128 | + break :blk hasher.hasher.final(); |
| 129 | + }; |
| 130 | + |
| 131 | + if (hash_a != hash_b) |
| 132 | + return error.WrongChecksum; |
| 133 | + |
| 134 | + const magic = try self.in_reader.readBytesNoEof(2); |
| 135 | + if (!std.mem.eql(u8, &magic, &.{ 'Y', 'Z' })) |
| 136 | + return error.CorruptInput; |
| 137 | + |
| 138 | + return 0; |
| 139 | + } |
| 140 | + }; |
| 141 | +} |
| 142 | + |
| 143 | +test { |
| 144 | + _ = @import("xz/test.zig"); |
| 145 | +} |
0 commit comments