From 8801546854e6dc01677beb5a9d112ab5c753d845 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 28 Oct 2019 23:36:40 +0000 Subject: [PATCH] tighten piece size range --- core/src/main/groovy/com/muwire/core/files/FileHasher.groovy | 3 ++- .../main/groovy/com/muwire/core/search/ResultsParser.groovy | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy b/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy index 546e77fc..2e8776ab 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileHasher.groovy @@ -13,6 +13,7 @@ import java.security.NoSuchAlgorithmException class FileHasher { + public static final int MIN_PIECE_SIZE_POW2 = 17 public static final int MAX_PIECE_SIZE_POW2 = 37 /** max size of shared file is 128 GB */ public static final long MAX_SIZE = 0x1L << MAX_PIECE_SIZE_POW2 @@ -25,7 +26,7 @@ class FileHasher { */ static int getPieceSize(long size) { if (size <= 0x1 << 30) - return 17 + return MIN_PIECE_SIZE_POW2 for (int i = 31; i <= MAX_PIECE_SIZE_POW2; i++) { if (size <= 0x1L << i) { diff --git a/core/src/main/groovy/com/muwire/core/search/ResultsParser.groovy b/core/src/main/groovy/com/muwire/core/search/ResultsParser.groovy index 467c1046..198c1aee 100644 --- a/core/src/main/groovy/com/muwire/core/search/ResultsParser.groovy +++ b/core/src/main/groovy/com/muwire/core/search/ResultsParser.groovy @@ -35,7 +35,7 @@ class ResultsParser { throw new InvalidSearchResultException("length missing or invalid, $json.size") if (json.infohash == null) throw new InvalidSearchResultException("infohash missing") - if (json.pieceSize == null || json.pieceSize <= 0 || json.pieceSize > FileHasher.MAX_PIECE_SIZE_POW2) + if (json.pieceSize == null || json.pieceSize < FileHasher.MIN_PIECE_SIZE_POW2 || json.pieceSize > FileHasher.MAX_PIECE_SIZE_POW2) throw new InvalidSearchResultException("pieceSize missing or invalid, $json.pieceSize") if (!(json.hashList instanceof List)) throw new InvalidSearchResultException("hashlist not a list") @@ -76,7 +76,7 @@ class ResultsParser { throw new InvalidSearchResultException("length missing or invalid $json.size") if (json.infohash == null) throw new InvalidSearchResultException("infohash missing") - if (json.pieceSize == null || json.pieceSize <= 0 || json.pieceSize > FileHasher.MAX_PIECE_SIZE_POW2) + if (json.pieceSize == null || json.pieceSize < FileHasher.MIN_PIECE_SIZE_POW2 || json.pieceSize > FileHasher.MAX_PIECE_SIZE_POW2) throw new InvalidSearchResultException("pieceSize missing or invalid, $json.pieceSize") if (json.hashList != null) throw new InvalidSearchResultException("V2 result with hashlist")