diff options
Diffstat (limited to 'src/metainfo.zig')
-rw-r--r-- | src/metainfo.zig | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/metainfo.zig b/src/metainfo.zig index 29cc655..cd00438 100644 --- a/src/metainfo.zig +++ b/src/metainfo.zig @@ -63,9 +63,13 @@ pub const Info = struct { const pri = pr.asInt(u1) catch return error.Malformatted; priv = pri == 1; } + // Validation.... + const ps = pp.asString() catch return error.Malformatted; + if (ps.len % 20 != 0) return error.Malformatted; + return .{ .piece_length = pl.asInt(u64) catch return error.Malformatted, - .pieces = pp.asString() catch return error.Malformatted, + .pieces = ps, .files = try files.toOwnedSlice(), .private = priv, }; @@ -112,6 +116,11 @@ pub const Info = struct { try b.bencode(w); return sha1.finalResult(); } + pub fn pieceHash(self: Info, ix: usize) ?[]const u8 { + const start = 20 * ix; + if (start >= self.pieces.len) return null; + return self.pieces[start .. start + 20]; + } }; info: Info, |