From e3c5be0a33e66b124ff261479cffa9f95ce53dc0 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sat, 11 Nov 2023 22:17:31 +0000 Subject: Make request to tracker, get and parse response to find peers list --- src/metainfo.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/metainfo.zig') 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, -- cgit v1.2.3-ZIG