Skip to content

Commit 69535ec

Browse files
aalesvdschultzca
authored andcommitted
Improve Checksum Fix processing
1 parent e7e8a9f commit 69535ec

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

src/main/java/com/romraider/maps/Rom.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void populateTables(byte[] binData, JProgressPane progress) {
271271
TableUpdateHandler.getInstance().registerTable(table);
272272

273273
if (null != table.getName() && table.getName().equalsIgnoreCase("Checksum Fix")){
274-
setEditStamp(binData, table.getStorageAddress());
274+
setEditStamp(binData, table.getStorageAddress() - table.getRamOffset());
275275
}
276276
i++;
277277
} catch (ArrayIndexOutOfBoundsException ex) {
@@ -396,8 +396,7 @@ private void showChecksumFixPopup(TableTreeNode checksum) {
396396
//TODO: Move to Subaru checksum
397397
calculateRomChecksum(
398398
binData,
399-
checksum.getTable().getStorageAddress(),
400-
checksum.getTable().getDataSize()
399+
checksum.getTable()
401400
);
402401
}
403402
}
@@ -406,14 +405,19 @@ private void showChecksumFixPopup(TableTreeNode checksum) {
406405
//It is only used to correct the Subaru Checksum. Should be moved somewhere else TODO
407406
public byte[] saveFile() {
408407

408+
//There can be more than 1 Checksum Fix tables, find them all
409409
final List<TableTreeNode> checksumTables = new ArrayList<TableTreeNode>();
410-
if(tableNodes.containsKey("checksum fix")) {
411-
checksumTables.add(tableNodes.get("checksum fix"));
410+
for (String name: tableNodes.keySet()) {
411+
if (name.startsWith("checksum fix")) {
412+
checksumTables.add(tableNodes.get(name));
413+
}
412414
}
413415

414416
if (checksumTables.size() == 1) {
415417
final TableTreeNode checksum = checksumTables.get(0);
416-
byte count = binData[checksum.getTable().getStorageAddress() + 207];
418+
int binDataPos = checksum.getTable().getStorageAddress() -
419+
checksum.getTable().getRamOffset();
420+
byte count = binData[binDataPos + 207];
417421
if (count == -1) {
418422
count = 1;
419423
}
@@ -427,18 +431,17 @@ public byte[] saveFile() {
427431
romStamp,
428432
0,
429433
binData,
430-
checksum.getTable().getStorageAddress() + 204,
434+
binDataPos + 204,
431435
4);
432-
setEditStamp(binData, checksum.getTable().getStorageAddress());
436+
setEditStamp(binData, binDataPos);
433437
}
434438

435439
for (TableTreeNode checksum : checksumTables) {
436440
if (!checksum.getTable().isLocked()) {
437441
//TODO: Move to Subaru checksum
438442
calculateRomChecksum(
439443
binData,
440-
checksum.getTable().getStorageAddress(),
441-
checksum.getTable().getDataSize()
444+
checksum.getTable()
442445
);
443446
}
444447
else if (checksum.getTable().isLocked() &&

src/main/java/com/romraider/maps/RomChecksum.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,59 @@
2626

2727
public class RomChecksum {
2828

29-
public static void calculateRomChecksum(byte[] input, int storageAddress, int dataSize) {
29+
public static void calculateRomChecksum(byte[] input, Table table)
30+
{
31+
calculateRomChecksum(
32+
input,
33+
table.getStorageAddress(),
34+
table.getDataSize(),
35+
table.getRamOffset()
36+
);
37+
}
38+
39+
public static int validateRomChecksum(byte[] input, Table table)
40+
{
41+
return validateRomChecksum(
42+
input,
43+
table.getStorageAddress(),
44+
table.getDataSize(),
45+
table.getRamOffset()
46+
);
47+
}
48+
49+
private static void calculateRomChecksum(byte[] input, int storageAddress, int dataSize, int offset) {
50+
storageAddress = storageAddress - offset;
3051
for (int i = storageAddress; i < storageAddress + dataSize; i+=12) {
52+
int startAddr = (int)parseByteValue(input, Settings.Endian.BIG, i , 4, true);
53+
int endAddr = (int)parseByteValue(input, Settings.Endian.BIG, i+4, 4, true);
54+
int off = offset;
55+
//0 means checksum is disabled, keep it
56+
if (startAddr == 0 && endAddr == 0) {
57+
off = 0;
58+
}
3159
byte[] newSum = calculateChecksum(input,
32-
(int)parseByteValue(input, Settings.Endian.BIG, i , 4, true),
33-
(int)parseByteValue(input, Settings.Endian.BIG, i+4, 4, true));
60+
startAddr - off,
61+
endAddr - off);
3462
System.arraycopy(newSum, 0, input, i + 8, 4);
3563
}
3664
}
3765

38-
public static int validateRomChecksum(byte[] input, int storageAddress, int dataSize) {
66+
private static int validateRomChecksum(byte[] input, int storageAddress, int dataSize, int offset) {
67+
storageAddress = storageAddress - offset;
3968
int result = 0;
4069
int[] results = new int[dataSize / 12];
4170
int j = 0;
4271
for (int i = storageAddress; i < storageAddress + dataSize; i+=12) {
4372
int startAddr = (int)parseByteValue(input, Settings.Endian.BIG, i , 4, true);
4473
int endAddr = (int)parseByteValue(input, Settings.Endian.BIG, i+4, 4, true);
4574
int diff = (int)parseByteValue(input, Settings.Endian.BIG, i+8, 4, true);
75+
int off = offset;
76+
//0 means checksum is disabled, keep it
77+
if (startAddr == 0 && endAddr == 0) {
78+
off = 0;
79+
}
80+
startAddr -= off;
81+
endAddr -= off;
4682
if (j == 0 &&
4783
startAddr == 0 &&
4884
endAddr == 0 &&

src/main/java/com/romraider/maps/TableSwitch.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public void populateTable(Rom rom) throws ArrayIndexOutOfBoundsException, IndexO
5454
// if the result is 0: all the checksums matched
5555
// if the result is -1: all the checksums have been previously disabled
5656
if (super.getName().contains("Checksum Fix")) {
57-
int result = validateRomChecksum(getDataCell(0).getBinary(),
58-
getStorageAddress(), getDataSize());
57+
int result = validateRomChecksum(getDataCell(0).getBinary(), this);
5958

6059
String message = MessageFormat.format(
6160
rb.getString("CHKSUMINVALID"), result, super.getName());

0 commit comments

Comments
 (0)