diff --git a/src/main/java/com/romraider/maps/checksum/CalculateALT2.java b/src/main/java/com/romraider/maps/checksum/CalculateALT2.java new file mode 100644 index 00000000..8a08e969 --- /dev/null +++ b/src/main/java/com/romraider/maps/checksum/CalculateALT2.java @@ -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 range, + byte[] binData, + Map 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); + } +} diff --git a/src/main/java/com/romraider/maps/checksum/CalculateSTD.java b/src/main/java/com/romraider/maps/checksum/CalculateSTD.java new file mode 100644 index 00000000..95f5db3a --- /dev/null +++ b/src/main/java/com/romraider/maps/checksum/CalculateSTD.java @@ -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 range, + byte[] binData, + Map 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); + } +} diff --git a/src/main/java/com/romraider/maps/checksum/Calculator.java b/src/main/java/com/romraider/maps/checksum/Calculator.java new file mode 100644 index 00000000..62fd763e --- /dev/null +++ b/src/main/java/com/romraider/maps/checksum/Calculator.java @@ -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 range, byte[] binData, Map results); +} diff --git a/src/main/java/com/romraider/maps/checksum/ChecksumALT.java b/src/main/java/com/romraider/maps/checksum/ChecksumALT.java new file mode 100644 index 00000000..37082569 --- /dev/null +++ b/src/main/java/com/romraider/maps/checksum/ChecksumALT.java @@ -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(); + } +} diff --git a/src/main/java/com/romraider/maps/checksum/ChecksumALT2.java b/src/main/java/com/romraider/maps/checksum/ChecksumALT2.java new file mode 100644 index 00000000..a492c407 --- /dev/null +++ b/src/main/java/com/romraider/maps/checksum/ChecksumALT2.java @@ -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 vars) { + super.configure(vars); + if (vars.containsKey(SKIPLOC)) { + range.put(SKIPLOC, HexUtil.hexToInt(vars.get(SKIPLOC))); + } + else { + range.put(SKIPLOC, 0x20000); + } + } +} diff --git a/src/main/java/com/romraider/maps/checksum/ChecksumSTD.java b/src/main/java/com/romraider/maps/checksum/ChecksumSTD.java index a8027235..faf795ce 100644 --- a/src/main/java/com/romraider/maps/checksum/ChecksumSTD.java +++ b/src/main/java/com/romraider/maps/checksum/ChecksumSTD.java @@ -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 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(); } } diff --git a/src/main/java/com/romraider/maps/checksum/NissanChecksum.java b/src/main/java/com/romraider/maps/checksum/NissanChecksum.java new file mode 100644 index 00000000..e77625f1 --- /dev/null +++ b/src/main/java/com/romraider/maps/checksum/NissanChecksum.java @@ -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 range = new HashMap(); + protected final Map results = new HashMap(); + protected Calculator calculator; + + public void configure(Map 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); + } +}