From 662dda106f30fd5dd1642481eb5529a7cbd84a32 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Fri, 13 Mar 2020 15:34:34 -0700 Subject: [PATCH] Add address translate helper function This makes it a little simpler for plugins to read data from an arbitrary address when the file data is discontiguous. --- PluginCommon/AddressTranslate.cs | 44 ++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/PluginCommon/AddressTranslate.cs b/PluginCommon/AddressTranslate.cs index 69edc9c..b3ae29d 100644 --- a/PluginCommon/AddressTranslate.cs +++ b/PluginCommon/AddressTranslate.cs @@ -35,6 +35,15 @@ namespace PluginCommon { mAddrMap = addrMap; } + /// + /// Converts a file offset to an address. + /// + /// File offset. + /// 24-bit address. + public int OffsetToAddress(int offset) { + return mAddrMap.OffsetToAddress(offset); + } + /// /// Determines the file offset that best contains the specified target address. /// @@ -47,12 +56,37 @@ namespace PluginCommon { } /// - /// Converts a file offset to an address. + /// Returns the data found at the specified address. If the address is out + /// of bounds this throws an AddressException. /// - /// File offset. - /// 24-bit address. - public int OffsetToAddress(int offset) { - return mAddrMap.OffsetToAddress(offset); + /// Data array. + /// Offset of the address reference. Only matters when + /// multiple file offsets map to the same address. + /// Data address. + /// Data found. + public byte GetDataAtAddress(byte[] data, int srcOffset, int address) { + int offset = AddressToOffset(srcOffset, address); + if (offset == -1) { + Exception ex = new AddressTranslateException("Address $" + address.ToString("X4") + + " is outside the file bounds"); + ex.Data.Add("Address", address); + throw ex; + } + try { + byte foo = data[offset]; + } catch (Exception ex) { + throw new AddressTranslateException("FAILED at srcOff=$" + srcOffset.ToString("x4") + + " addr=$" + address.ToString("x4")); + } + return data[offset]; } } + + /// + /// Exception thrown by AddressTranslate's GetDataAtAddress(). + /// + public class AddressTranslateException : Exception { + public AddressTranslateException() : base() { } + public AddressTranslateException(string msg) : base(msg) { } + } }