From 5e0519cc06a204e029a6c57b2f147323e0ce1e3b Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 31 Oct 2020 02:05:19 +0000 Subject: [PATCH] add piece size and file length --- .../collections/FileCollectionBuilder.groovy | 2 +- .../core/collections/FileCollectionItem.groovy | 17 ++++++++++++++++- doc/collections.md | 6 ++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/collections/FileCollectionBuilder.groovy b/core/src/main/groovy/com/muwire/core/collections/FileCollectionBuilder.groovy index 8fc9ceb3..724b89bc 100644 --- a/core/src/main/groovy/com/muwire/core/collections/FileCollectionBuilder.groovy +++ b/core/src/main/groovy/com/muwire/core/collections/FileCollectionBuilder.groovy @@ -84,7 +84,7 @@ class FileCollectionBuilder { String comment = sf.getComment() // TODO: check comment encoding if (comment == null) comment = "" - def item = new FileCollectionItem(new InfoHash(sf.root), comment, sfPathElements.get(sf)) + def item = new FileCollectionItem(new InfoHash(sf.root), comment, sfPathElements.get(sf), (byte)sf.pieceSize, sf.getCachedLength()) files.add(item) } diff --git a/core/src/main/groovy/com/muwire/core/collections/FileCollectionItem.groovy b/core/src/main/groovy/com/muwire/core/collections/FileCollectionItem.groovy index 6b7ac745..d3ed214e 100644 --- a/core/src/main/groovy/com/muwire/core/collections/FileCollectionItem.groovy +++ b/core/src/main/groovy/com/muwire/core/collections/FileCollectionItem.groovy @@ -3,19 +3,24 @@ package com.muwire.core.collections import com.muwire.core.Constants import com.muwire.core.InfoHash import com.muwire.core.Name +import com.muwire.core.files.FileHasher class FileCollectionItem { private final InfoHash infoHash private final String comment private final File file + private final byte pieceSizePow2 + private final long length private volatile byte[] payload private final List pathElements = new ArrayList<>() - public FileCollectionItem(InfoHash infoHash, String comment, List pathElements) { + public FileCollectionItem(InfoHash infoHash, String comment, List pathElements, byte pieceSizePow2, long length) { this.infoHash = infoHash this.comment = comment this.pathElements = pathElements + this.pieceSizePow2 = pieceSizePow2 + this.length = length File f = null pathElements.each { @@ -38,6 +43,14 @@ class FileCollectionItem { dis.readFully(hash) infoHash = new InfoHash(hash) + pieceSizePow2 = dis.readByte() + if (pieceSizePow2 < FileHasher.MIN_PIECE_SIZE_POW2 || pieceSizePow2 > FileHasher.MAX_PIECE_SIZE_POW2) + throw new InvalidCollectionException("invalid piece size $pieceSizePow2") + + length = dis.readLong() + if (length < 1 || length > FileHasher.MAX_SIZE) + throw new InvalidCollectionException("invalid length $length") + int nPathElements = dis.readUnsignedByte() File f = null nPathElements.times { @@ -62,6 +75,8 @@ class FileCollectionItem { daos.writeByte(Constants.COLLECTION_ENTRY_VERSION) daos.write(infoHash.getRoot()) + daos.writeByte(pieceSizePow2) + daos.writeLong(length) daos.writeByte((byte) pathElements.size()) pathElements.each { def name = new Name(it) diff --git a/doc/collections.md b/doc/collections.md index ea23d7b3..f4cd9d79 100644 --- a/doc/collections.md +++ b/doc/collections.md @@ -24,8 +24,10 @@ The header is followed by a file entry for each file in the collection. The for ``` byte 0: File entry version, currently fixed at "1". byte 1-33: hash of the file -byte 34: Unsigned 8-bit number of path elements from root to where the file will ultimately be placed upon download. -bytes 35-N : UTF-8 encoded length-prefixed path elements. Each element can be at most 32kb long. The last element is the name of the file. +byte 34: piece size power of 2 +bytes 35-43: length of the file +byte 44: Unsigned 8-bit number of path elements from root to where the file will ultimately be placed upon download. +bytes 45-N : UTF-8 encoded length-prefixed path elements. Each element can be at most 32kb long. The last element is the name of the file. bytes N-M: length-prefixed free from description of the file (comment). Format is UTF-8, maximum size is 32kb. ```