2005-12-17 jrandom
* Use our faster SHA1, rather than the JVM's within I2PSnark, and let 'piece' sizes grow larger than before.
This commit is contained in:
@ -35,6 +35,7 @@ import java.util.HashMap;
|
||||
import org.klomp.snark.bencode.*;
|
||||
import net.i2p.data.Base64;
|
||||
import net.i2p.util.Log;
|
||||
import net.i2p.crypto.SHA1;
|
||||
|
||||
/**
|
||||
* Note: this class is buggy, as it doesn't propogate custom meta fields into the bencoded
|
||||
@ -298,6 +299,12 @@ public class MetaInfo
|
||||
*/
|
||||
public boolean checkPiece(int piece, byte[] bs, int off, int length)
|
||||
{
|
||||
if (true)
|
||||
return fast_checkPiece(piece, bs, off, length);
|
||||
else
|
||||
return orig_checkPiece(piece, bs, off, length);
|
||||
}
|
||||
private boolean orig_checkPiece(int piece, byte[] bs, int off, int length) {
|
||||
// Check digest
|
||||
MessageDigest sha1;
|
||||
try
|
||||
@ -317,6 +324,17 @@ public class MetaInfo
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean fast_checkPiece(int piece, byte[] bs, int off, int length) {
|
||||
SHA1 sha1 = new SHA1();
|
||||
|
||||
sha1.update(bs, off, length);
|
||||
byte[] hash = sha1.digest();
|
||||
for (int i = 0; i < 20; i++)
|
||||
if (hash[i] != piece_hashes[20 * piece + i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the total length of the torrent in bytes.
|
||||
*/
|
||||
|
@ -319,7 +319,7 @@ public class SnarkManager implements Snark.CompleteListener {
|
||||
return "Too many files in " + info.getName() + " (" + files.size() + "), deleting it";
|
||||
} else if (info.getPieces() <= 0) {
|
||||
return "No pieces in " + info.getName() + "? deleting it";
|
||||
} else if (info.getPieceLength(0) > 1024*1024) {
|
||||
} else if (info.getPieceLength(0) > 10*1024*1024) {
|
||||
return "Pieces are too large in " + info.getName() + " (" + info.getPieceLength(0)/1024 + "KB, deleting it";
|
||||
} else if (info.getTotalLength() > 10*1024*1024*1024l) {
|
||||
System.out.println("torrent info: " + info.toString());
|
||||
|
@ -25,6 +25,8 @@ import java.util.*;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import net.i2p.crypto.SHA1;
|
||||
|
||||
/**
|
||||
* Maintains pieces on disk. Can be used to store and retrieve pieces.
|
||||
*/
|
||||
@ -129,6 +131,14 @@ public class Storage
|
||||
// Creates piece hases for a new storage.
|
||||
private void create() throws IOException
|
||||
{
|
||||
if (true) {
|
||||
fast_digestCreate();
|
||||
} else {
|
||||
orig_digestCreate();
|
||||
}
|
||||
}
|
||||
|
||||
private void orig_digestCreate() throws IOException {
|
||||
// Calculate piece_hashes
|
||||
MessageDigest digest = null;
|
||||
try
|
||||
@ -164,6 +174,34 @@ public class Storage
|
||||
metainfo = metainfo.reannounce(metainfo.getAnnounce());
|
||||
}
|
||||
|
||||
private void fast_digestCreate() throws IOException {
|
||||
// Calculate piece_hashes
|
||||
SHA1 digest = new SHA1();
|
||||
|
||||
byte[] piece_hashes = metainfo.getPieceHashes();
|
||||
|
||||
byte[] piece = new byte[piece_size];
|
||||
for (int i = 0; i < pieces; i++)
|
||||
{
|
||||
int length = getUncheckedPiece(i, piece, 0);
|
||||
digest.update(piece, 0, length);
|
||||
byte[] hash = digest.digest();
|
||||
for (int j = 0; j < 20; j++)
|
||||
piece_hashes[20 * i + j] = hash[j];
|
||||
|
||||
bitfield.set(i);
|
||||
|
||||
if (listener != null)
|
||||
listener.storageChecked(this, i, true);
|
||||
}
|
||||
|
||||
if (listener != null)
|
||||
listener.storageAllChecked(this);
|
||||
|
||||
// Reannounce to force recalculating the info_hash.
|
||||
metainfo = metainfo.reannounce(metainfo.getAnnounce());
|
||||
}
|
||||
|
||||
private void getFiles(File base) throws IOException
|
||||
{
|
||||
ArrayList files = new ArrayList();
|
||||
|
@ -1,4 +1,8 @@
|
||||
$Id: history.txt,v 1.360 2005/12/16 18:18:56 jrandom Exp $
|
||||
$Id: history.txt,v 1.361 2005/12/16 22:47:04 jrandom Exp $
|
||||
|
||||
2005-12-17 jrandom
|
||||
* Use our faster SHA1, rather than the JVM's within I2PSnark, and let
|
||||
'piece' sizes grow larger than before.
|
||||
|
||||
2005-12-16 jrandom
|
||||
* Added some I2PSnark sanity checks, an OOMListener when running
|
||||
|
Reference in New Issue
Block a user