From 6c24cd4862cd06f3364810b73e3de1bab411f31a Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sun, 12 Nov 2023 21:17:01 +0000 Subject: Move tracker protocol into it's own file Change piece_length -> u32, protocol constrains that anyway annnnd, successfully download a file (albeit from one torrent only with no checking of a ton of stuff) --- src/metainfo.zig | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/metainfo.zig') diff --git a/src/metainfo.zig b/src/metainfo.zig index cd00438..04e8f64 100644 --- a/src/metainfo.zig +++ b/src/metainfo.zig @@ -12,7 +12,7 @@ pub const Info = struct { md5sum: ?[]const u8 = null, }; - piece_length: u64, + piece_length: u32, pieces: []const u8, files: []File, private: ?bool, @@ -68,7 +68,7 @@ pub const Info = struct { if (ps.len % 20 != 0) return error.Malformatted; return .{ - .piece_length = pl.asInt(u64) catch return error.Malformatted, + .piece_length = pl.asInt(u32) catch return error.Malformatted, .pieces = ps, .files = try files.toOwnedSlice(), .private = priv, @@ -116,10 +116,15 @@ pub const Info = struct { try b.bencode(w); return sha1.finalResult(); } - pub fn pieceHash(self: Info, ix: usize) ?[]const u8 { + pub fn pieceHash(self: Info, ix: usize) ?[20]u8 { const start = 20 * ix; if (start >= self.pieces.len) return null; - return self.pieces[start .. start + 20]; + var res: [20]u8 = undefined; + @memcpy(&res, self.pieces[start .. start + 20]); + return res; + } + pub fn pieceCount(self: Info) u32 { + return @as(u32, @intCast(self.pieces.len)) / 20; } }; -- cgit v1.2.3-ZIG