Add Nissan checksum ALT & ALT2 calculator Class

This commit is contained in:
Dale Schultz 2019-03-27 19:49:22 -04:00
parent 49c912cd0b
commit c8acdee563
7 changed files with 299 additions and 56 deletions

View File

@ -0,0 +1,59 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2019 RomRaider.com
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.maps.checksum;
import static com.romraider.maps.checksum.ChecksumALT2.SKIPLOC;
import static com.romraider.maps.checksum.NissanChecksum.END;
import static com.romraider.maps.checksum.NissanChecksum.START;
import static com.romraider.maps.checksum.NissanChecksum.SUMLOC;
import static com.romraider.maps.checksum.NissanChecksum.SUMT;
import static com.romraider.maps.checksum.NissanChecksum.XORLOC;
import static com.romraider.maps.checksum.NissanChecksum.XORT;
import static com.romraider.xml.RomAttributeParser.parseByteValue;
import java.util.Map;
import com.romraider.Settings;
public final class CalculateALT2 implements Calculator {
public CalculateALT2() {
}
public final void calculate(
Map<String, Integer> range,
byte[] binData,
Map<String, Integer> results) {
int sumt = 0;
int xort = 0;
int dw = 0;
for (int i = range.get(START); i < range.get(END); i += 4) {
if ((i == range.get(SUMLOC))
|| (i == range.get(XORLOC)
|| (i == range.get(SKIPLOC)))) continue;
dw = (int)parseByteValue(binData, Settings.Endian.BIG, i, 4, true);
sumt += dw;
xort ^= dw;
}
results.put(SUMT, sumt);
results.put(XORT, xort);
}
}

View File

@ -0,0 +1,56 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2019 RomRaider.com
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.maps.checksum;
import static com.romraider.maps.checksum.NissanChecksum.END;
import static com.romraider.maps.checksum.NissanChecksum.START;
import static com.romraider.maps.checksum.NissanChecksum.SUMLOC;
import static com.romraider.maps.checksum.NissanChecksum.SUMT;
import static com.romraider.maps.checksum.NissanChecksum.XORLOC;
import static com.romraider.maps.checksum.NissanChecksum.XORT;
import static com.romraider.xml.RomAttributeParser.parseByteValue;
import java.util.Map;
import com.romraider.Settings;
public final class CalculateSTD implements Calculator {
public CalculateSTD() {
}
public final void calculate(
Map<String, Integer> range,
byte[] binData,
Map<String, Integer> results) {
int sumt = 0;
int xort = 0;
int dw = 0;
for (int i = range.get(START); i < range.get(END); i += 4) {
if ((i == range.get(SUMLOC)) || (i == range.get(XORLOC))) continue;
dw = (int)parseByteValue(binData, Settings.Endian.BIG, i, 4, true);
sumt += dw;
xort ^= dw;
}
results.put(SUMT, sumt);
results.put(XORT, xort);
}
}

View File

@ -0,0 +1,38 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2019 RomRaider.com
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.maps.checksum;
import java.util.Map;
/**
* The Calculator interface is implemented by CalculateXXX classes
* which perform the actual checksum calculation.
*/
public interface Calculator {
/**
* Calculate the sum and xor total over the address range provided.
* @param range - a map of with the address ranges use.
* @param binData - the binary data t calculate over.
* @param results - a map containing the keys for sumt and xort
*/
void calculate(
Map<String, Integer> range, byte[] binData, Map<String, Integer> results);
}

View File

@ -0,0 +1,31 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2019 RomRaider.com
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.maps.checksum;
/**
* This class implements the "alt" checksum validation and calculations
* for Nissan ROMs.
*/
public final class ChecksumALT extends NissanChecksum {
public ChecksumALT() {
calculator = new CalculateSTD();
}
}

View File

@ -0,0 +1,46 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2019 RomRaider.com
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.maps.checksum;
import java.util.Map;
import com.romraider.util.HexUtil;
/**
* This class implements the "alt2" checksum validation and calculations
* for Nissan ROMs.
*/
public final class ChecksumALT2 extends NissanChecksum {
protected static final String SKIPLOC = "skiploc";
public ChecksumALT2() {
calculator = new CalculateALT2();
}
public void configure(Map<String, String> vars) {
super.configure(vars);
if (vars.containsKey(SKIPLOC)) {
range.put(SKIPLOC, HexUtil.hexToInt(vars.get(SKIPLOC)));
}
else {
range.put(SKIPLOC, 0x20000);
}
}
}

View File

@ -1,6 +1,6 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2018 RomRaider.com
* Copyright (C) 2006-2019 RomRaider.com
*
* 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
@ -19,66 +19,13 @@
package com.romraider.maps.checksum;
import static com.romraider.xml.RomAttributeParser.parseByteValue;
import static com.romraider.xml.RomAttributeParser.parseIntegerValue;
import java.util.Map;
import com.romraider.Settings;
import com.romraider.util.HexUtil;
/**
* This class implements the "std" checksum validation and calculations
* for Nissan ROMs.
*/
public final class ChecksumSTD implements ChecksumManager {
private static final String START = "start";
private static final String END = "end";
private static final String SUMLOC = "sumloc";
private static final String XORLOC = "xorloc";
private int start;
private int end;
private int sumloc;
private int xorloc;
private int sumt;
private int xort;
public final class ChecksumSTD extends NissanChecksum {
public ChecksumSTD() {
}
@Override
public void configure(Map<String, String> vars) {
this.start = HexUtil.hexToInt(vars.get(START));
this.end = HexUtil.hexToInt(vars.get(END));
this.sumloc = HexUtil.hexToInt(vars.get(SUMLOC));
this.xorloc = HexUtil.hexToInt(vars.get(XORLOC));
}
@Override
public boolean validate(byte[] binData) {
calculate(binData);
final boolean valid =
(sumt == (int)parseByteValue(binData, Settings.Endian.BIG, sumloc, 4, true)) &&
(xort == (int)parseByteValue(binData, Settings.Endian.BIG, xorloc, 4, true));
return valid;
}
@Override
public void update(byte[] binData) {
calculate(binData);
System.arraycopy(parseIntegerValue(sumt, Settings.Endian.BIG, 4), 0, binData, sumloc, 4);
System.arraycopy(parseIntegerValue(xort, Settings.Endian.BIG, 4), 0, binData, xorloc, 4);
}
private void calculate(byte[] binData) {
sumt = 0;
xort = 0;
int dw = 0;
for (int i = start; i < end; i += 4) {
if ((i == sumloc) || (i == xorloc)) continue;
dw = (int)parseByteValue(binData, Settings.Endian.BIG, i, 4, true);
sumt += dw;
xort ^= dw;
}
calculator = new CalculateSTD();
}
}

View File

@ -0,0 +1,66 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2019 RomRaider.com
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.maps.checksum;
import static com.romraider.xml.RomAttributeParser.parseByteValue;
import static com.romraider.xml.RomAttributeParser.parseIntegerValue;
import java.util.HashMap;
import java.util.Map;
import com.romraider.Settings;
import com.romraider.util.HexUtil;
/**
* This class provides common methods implemented by different
* checksum calculation type classes.
*/
abstract class NissanChecksum implements ChecksumManager {
public static final String START = "start";
public static final String END = "end";
public static final String SUMLOC = "sumloc";
public static final String XORLOC = "xorloc";
public static final String SUMT = "sumt";
public static final String XORT = "xort";
protected final Map<String, Integer> range = new HashMap<String, Integer>();
protected final Map<String, Integer> results = new HashMap<String, Integer>();
protected Calculator calculator;
public void configure(Map<String, String> vars) {
range.put(START, HexUtil.hexToInt(vars.get(START)));
range.put(END, HexUtil.hexToInt(vars.get(END)));
range.put(SUMLOC, HexUtil.hexToInt(vars.get(SUMLOC)));
range.put(XORLOC, HexUtil.hexToInt(vars.get(XORLOC)));
}
public boolean validate(byte[] binData) {
calculator.calculate(range, binData, results);
final boolean valid =
(results.get(SUMT) == (int)parseByteValue(binData, Settings.Endian.BIG, range.get(SUMLOC), 4, true)) &&
(results.get(XORT) == (int)parseByteValue(binData, Settings.Endian.BIG, range.get(XORLOC), 4, true));
return valid;
}
public void update(byte[] binData) {
calculator.calculate(range, binData, results);
System.arraycopy(parseIntegerValue(results.get(SUMT), Settings.Endian.BIG, 4), 0, binData, range.get(SUMLOC), 4);
System.arraycopy(parseIntegerValue(results.get(XORT), Settings.Endian.BIG, 4), 0, binData, range.get(XORLOC), 4);
}
}