Author |
Message |
Smong Server Help Squatter

Joined: 1043048991 Posts: 0x91E Offline
|
Posted: Sun Mar 23, 2003 2:49 pm Post subject: Checksum in Java |
 |
|
|
|
I'm trying to get the checksums for the map and news.txt in Java. So far I've made a class (validator) to get a checksum from a file. Because Java does not support unsigned variables it returns a long. getInt32() returns an int which is signed so I 0xFFFFFFFFL to make it unsigned each time I want to use it.
As you can probably guess the checksums don't match, any ideas? I used smallmap.lvl that came with the ASSS server.
...
public static int getInt32(byte[] msg, int pos) {
return (int)(
((msg[pos+3] & 0xFF) << 24) +
((msg[pos+2] & 0xFF) << 16) +
((msg[pos+1] & 0xFF) << 8) +
(msg[pos ] & 0xFF)
);
}
...
case 0x29: //Map checksum
System.out.println("Receieved: Map checksum");
String mapName = new String(data, 1, 16);
long mapCRC = validator.getChecksum(mapName.trim());
if (mapCRC == -1) break; //File not found
long mCrc = getInt32(data, 17) & 0xFFFFFFFFL;
System.out.println(" map " + mapCRC + "/" + mCrc);
if (mapCRC != mCrc)
System.out.println(" " + mapName.trim() + " out of sync");
//Receieved: Map checksum
// map 1087398166/3002814809
// smallmap.lvl out of sync
...
|
import java.io.*;
import java.util.zip.*;
public class validator {
static final int version = 1;
public validator() {
System.out.println("-- validator v" + version + " --");
}
public static long getChecksum(byte[] file) {
CRC32 crc = new CRC32();
crc.reset();
crc.update(file);
return crc.getValue();
}
public static long getChecksum(String fileName) {
byte[] file = {0};
try {
RandomAccessFile raf = new RandomAccessFile(fileName, "r");
raf.readFully(file);
raf.close();
} catch(SecurityException e) {
System.out.println("-- Err: Security settings too high to read local files --");
} catch(FileNotFoundException e) {
System.out.println("-- Could not locate " + fileName + " --");
return -1;
} catch(EOFException e) {
System.err.println("-- Err: End of File exception --");
} catch(IOException e) {
System.err.println("-- Err: IO exception --");
}
//CRC32 crc = new CRC32();
//crc.reset();
//crc.update(file);
//return crc.getValue();
return getChecksum(file);
}
}
|
Feel free to nick my code (if you can get it to work, :?). |
|
Back to top |
|
 |
SOS Server Help Squatter
Joined: Dec 02 2002 Posts: 329 Offline
|
Posted: Wed Mar 26, 2003 10:06 am Post subject: |
 |
|
|
|
Java doesn't support unsigned?? OMG my opinion of Java dropped below VB. And doing & 0xffffffff sounds weird to me, but I guess Java might be weird like that
Anyway, maybe ask Dock? He made a Java bot, hmm? _________________ 3y3 4m l33t h4x0r
j0! 3y3 4m t4lking to j00!
fux0red 5cr1pt k1dd13.
-"l33t h41ku" |
|
Back to top |
|
 |
Smong Server Help Squatter

Joined: 1043048991 Posts: 0x91E Offline
|
Posted: Thu Mar 27, 2003 5:12 pm Post subject: |
 |
|
|
|
maybe..whats even worse is that you can't do pointer arithmetic or get a byte pointer to the beginning of a class (so for each packet you have to set each byte individually!! and it won't tell you how big the UDP packet received is, v. tricky with chunk packets..blah blah blah...). |
|
Back to top |
|
 |
|