Fixed CheckSumInputStream with help of iKilem on the forum, thanks! Everything works I believe, need to update java_client and release for testing.

This commit is contained in:
Travis Burtrum 2011-06-12 14:06:09 -04:00 committed by moparisthebest
parent c57253690a
commit 6dbcd282cf
5 changed files with 9 additions and 129 deletions

View File

@ -1,105 +0,0 @@
/*
* Copyright (C) 2011 moparisthebest
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Official forums are http://www.moparscape.org/smf/
* Email me at admin@moparisthebest.com , I read it but don't usually respond.
*/
import java.io.*;
import java.net.URL;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipInputStream;
public class HelpMe {
public static void main(String[] args) throws Exception {
String savePath = "./";
String fileName = "java_client.win32.exe";
File gzipFile = new File(savePath + "java_client.win32.exe.gz");
if (!gzipFile.exists()){
System.out.println("downloading gzip file...");
writeStream(new URL("http://www.moparscape.org/libs/java_client.win32.exe.gz").openConnection().getInputStream(),
new FileOutputStream(gzipFile));
}
InputStream is = new FileInputStream(gzipFile);
System.out.println("fileName: " + fileName);
System.out.println("crc should be: 1752045540");
CRC32 cr = new CRC32();
System.out.println("extracting file and calculating CRC:");
writeStream(new ChecksumInputStream(new GZIPInputStream(is), cr), new FileOutputStream(savePath + fileName));
System.out.println("crc32: " + cr.getValue());
cr.reset();
//cr = new CRC32(); // same as above, but I tried
System.out.println("calculating CRC of the file we just extracted (should be the exact same!):");
writeStream(new ChecksumInputStream(new FileInputStream(savePath + fileName), cr), new FileOutputStream(savePath + fileName + ".bob"));
System.out.println("crc32: " + cr.getValue());
}
public static final int bufferSize = 512;
public static void writeStream(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[bufferSize];
int len;
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
//if(in instanceof ZipInputStream) try{ Thread.sleep(1); }catch(InterruptedException e){ e.printStackTrace(); }
}
// if its a ZipInputStream we don't want to close it
if (!(in instanceof ZipInputStream))
in.close();
out.close();
}
public static class ChecksumInputStream extends FilterInputStream {
private Checksum cs;
public ChecksumInputStream(InputStream in, Checksum cs) {
super(in);
this.cs = cs;
}
public long getValue() {
return cs.getValue();
}
@Override
public int read() throws IOException {
int byteValue = super.read();
if (byteValue != -1) cs.update(byteValue);
return byteValue;
}
@Override
public int read(byte[] b) throws IOException {
int bytesRead = super.read(b);
if (bytesRead != -1) cs.update(b, 0, b.length);
return bytesRead;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int bytesRead = super.read(b, off, len);
if (bytesRead != -1) cs.update(b, off, len);
return bytesRead;
}
}
}

View File

@ -60,18 +60,18 @@ public class ChecksumInputStream extends FilterInputStream {
if (byteValue != -1) cs.update(byteValue);
return byteValue;
}
/*
@Override
public int read(byte[] b) throws IOException {
int bytesRead = super.read(b);
if (bytesRead != -1) cs.update(b, 0, b.length);
return bytesRead;
}
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
int bytesRead = super.read(b, off, len);
if (bytesRead != -1) cs.update(b, off, len);
if (bytesRead != -1) cs.update(b, off, bytesRead);
return bytesRead;
}

View File

@ -322,14 +322,12 @@ public class ResourceGrabber {
Downloader.extractFile(file, savePath, this);
// check crc if we are supposed to
// TODO: should we only check filesDownloaded?
System.out.println("savePath: "+savePath);
// TODO: should we only check filesDownloaded? Then we can't specify extracted files to CRC only.
//System.out.println("savePath: "+savePath);
if (ci != null && !ci.checksumMatch(savePath))
error(String.format("CRC Mismatch. expected: %d actual: %d", ci.getExpectedChecksum(), ci.getChecksum()), null);
else
super.finished(savePath, filesDownloaded);
System.out.println(String.format("CRC succeded. expected: %d actual: %d", ci.getExpectedChecksum(), ci.getChecksum()));
}
public boolean download(String url, String savePath, boolean extract, ChecksumInfo ci) throws Exception {

View File

@ -25,7 +25,7 @@ package org.moparscape.res.impl;
*/
public class BTDownloaderCRCs {
private static final long[] crcs = new long[]{4019024161L /*-java_client.linux.x86*/, 457499165L /*-java_client.osx.i386*/, 270479122L /*-java_client.osx.ppc*/, 1752045540L /*-java_client.win32.exe*/};
private static final long[] crcs = new long[]{3695618219L /*-java_client.linux.x86*/, 3690443921L /*-java_client.osx.i386*/, 4170663209L /*-java_client.osx.ppc*/, 415399000L /*-java_client.win32.exe*/};
public static final int LINUX = 0;
public static final int OSX386 = 1;

View File

@ -122,28 +122,15 @@ public abstract class Downloader {
fileName = fileName.substring(0, fileName.length() - 3);
// exception for java_client.exefalse &&
if (badExtension(fileName)){
is.close();
is = null;
is = new FileInputStream(file);
// input stream to store uncompressed data in, no use in uncompressing twice
// we could write this to temporary file on the system, and delete it if its bad
// but I really don't ever want a potentially malicious binary on the end-users system
// so we will just store it in memory (java_client.win32.exe is fairly small anyhow)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.out.println("fileName: "+fileName);
ChecksumInfo ci = new ChecksumInfo(BTDownloaderCRCs.getCRC(BTDownloaderCRCs.WINDOWS));
CRC32 cr = new CRC32();
Downloader.writeStream(new ChecksumInputStream(new GZIPInputStream(is), cr), new FileOutputStream(savePath + fileName));
System.out.println("crc32: "+cr.getValue());
cr.reset();
Downloader.writeStream(new ChecksumInputStream(new FileInputStream(savePath + fileName), cr), new FileOutputStream(savePath + fileName + ".bob"));
System.out.println("crc32: "+cr.getValue());
file.delete();
System.exit(0);
//ChecksumInfo ci = new ChecksumInfo(1311801406);
if( (!fileName.endsWith("java_client.win32.exe")) || (!ci.checksumMatch(new GZIPInputStream(new FileInputStream(file)), baos)) ){
System.out.println("bad extension!");
System.out.println(String.format("CRC Mismatch. expected: %d actual: %d", ci.getExpectedChecksum(), ci.getChecksum()));
if( (!fileName.endsWith("java_client.win32.exe")) || (!ci.checksumMatch(new GZIPInputStream(is), baos)) ){
if(fileName.endsWith("java_client.win32.exe"))
System.out.println(String.format("CRC Mismatch for java_client.win32.exe, expected: %d actual: %d", ci.getExpectedChecksum(), ci.getChecksum()));
// then no exception, just return with error
if (callback != null)