aboutsummaryrefslogtreecommitdiff
path: root/src/metainfo.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-11-12 21:17:01 +0000
committerMartin Ashby <martin@ashbysoft.com>2023-11-12 21:17:01 +0000
commit6c24cd4862cd06f3364810b73e3de1bab411f31a (patch)
tree1ef0d35963d960e6f885ceca172e22c8de1c5217 /src/metainfo.zig
parent536837b44823aedf3dab0b8ef844d57cbae7af74 (diff)
downloadzbt-6c24cd4862cd06f3364810b73e3de1bab411f31a.tar.gz
zbt-6c24cd4862cd06f3364810b73e3de1bab411f31a.tar.bz2
zbt-6c24cd4862cd06f3364810b73e3de1bab411f31a.tar.xz
zbt-6c24cd4862cd06f3364810b73e3de1bab411f31a.zip
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)
Diffstat (limited to 'src/metainfo.zig')
-rw-r--r--src/metainfo.zig13
1 files changed, 9 insertions, 4 deletions
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;
}
};