private bool IsCorrectType(long current, byte[] data, byte[] searchBytes) { bool retVal = false; long loc = BinSearch.GetLocationOfGivenBytes(current, searchBytes, data, 10); if (loc == current) { retVal = true; } return(retVal); }
private string GetInfo() { string retVal = ""; long loc = BinSearch.GetLocationOfGivenBytes(mLocation, ScriptSearchForm.BodyBytes, mData); long infoLoc = BinSearch.GetLocationOfGivenBytesBackup(loc, ASCIIEncoding.ASCII.GetBytes("INFO"), mData, 20); if (infoLoc > -1) { retVal = String.Format("-- INFO 0x{0:x} 0x{1:x} 0x{2:x} 0x{3:x} 0x{4:x} 0x{5:x} 0x{6:x} 0x{7:x} ", mData[infoLoc + 4], mData[infoLoc + 5], mData[infoLoc + 6], mData[infoLoc + 7], mData[infoLoc + 8], mData[infoLoc + 9], mData[infoLoc + 10], mData[infoLoc + 11]); } return(retVal); }
/// <summary> /// Searched through 'data' for the givenBytes. /// </summary> /// <param name="startLocation">Where to start in 'data'</param> /// <param name="givenBytes">the stuff to look for</param> /// <param name="data">the big data to look through</param> /// <returns>location of the givenBytes, -1L if not found.</returns> public static long GetLocationOfGivenBytes(long startLocation, byte[] givenBytes, byte[] data) { long num = (long)(data.Length - givenBytes.Length); for (long location = startLocation; location < num; ++location) { if (BinSearch.Find(givenBytes, location, data)) { return(location); } } return(-1L); }
public string GetBinaryRepresentationString() { string ret = ""; try { ret = BinSearch.GetBinaryRepresentationString(GetAssetData()); } catch (Exception e) { MessageBox.Show("Error! " + e.Message + "\n" + e.StackTrace); } return(ret); }
public static List <long> GetLocationsOfGivenBytes(long startLocation, byte[] givenBytes, byte[] data) { List <long> retVal = new List <long>(); long num = (long)(data.Length - givenBytes.Length); for (long location = startLocation; location < num; ++location) { if (BinSearch.Find(givenBytes, location, data)) { retVal.Add(location); location += givenBytes.Length; } } return(retVal); }
/// <summary> /// Searched through 'data' for the givenBytes going backwards. /// </summary> /// <param name="startLocation">Where to start in 'data'</param> /// <param name="givenBytes">the stuff to look for</param> /// <param name="data">the big data to look through</param> /// <param name="maxLookback">the maximum number of bytes to backup.</param> /// <returns>location of the givenBytes, -1L if not found.</returns> public static long GetLocationOfGivenBytesBackup(long startLocation, byte[] givenBytes, byte[] data, int maxLookback) { //long num = (long)(data.Length - givenBytes.Length); for (long location = startLocation; location > 0; --location) { if (BinSearch.Find(givenBytes, location, data)) { return(location); } else if (location < (startLocation - maxLookback)) { break; } } return(-1L); }
private void mSearchButton_Click(object sender, EventArgs e) { if (File.Exists(mBigFileTextBox.Text)) { byte[] bigFile = File.ReadAllBytes(mBigFileTextBox.Text); byte[] givenFileBytes = null; switch (mTypeComboBox.SelectedIndex) { case 0: // search for a file in a bigger file. if (File.Exists(mGivenFileTextBox.Text)) { givenFileBytes = File.ReadAllBytes(mGivenFileTextBox.Text); } else { MessageBox.Show("File: " + mGivenFileTextBox.Text + " Not Found!"); return; } break; case 1: // ASCII string givenFileBytes = ASCIIEncoding.ASCII.GetBytes(mGivenFileTextBox.Text); break; case 2: // unicode string givenFileBytes = UnicodeEncoding.Unicode.GetBytes(mGivenFileTextBox.Text); break; } List <long> locations = BinSearch.GetLocationsOfGivenBytes(0L, givenFileBytes, bigFile); if (locations.Count > 0) { StringBuilder builder = new StringBuilder(); foreach (long loc in locations) { builder.Append(String.Format("0x{0:X2}\n", loc)); } mFileLocationTextBox.Text = "Locations:\n " + builder.ToString(); } else { mFileLocationTextBox.Text = mGivenFileTextBox.Text + ": Not Found."; } } }
public byte[] GetAssetData() { byte[] assetData = null; if (BinSearch.GetLocationOfGivenBytes(mLocation, ASCIIEncoding.ASCII.GetBytes("lvl_"), mData) == mLocation) { int bodyLen = GetLengthAtLocation(mLocation + 4); assetData = BinSearch.GetArrayChunk(mData, mLocation, bodyLen + 8); } else { long loc = BinSearch.GetLocationOfGivenBytes(mLocation, ScriptSearchForm.BodyBytes, mData); int bodyLen = GetLengthAtLocation(loc + 4); long bodyStart = loc + 8; long bodyEnd = loc + 8 + bodyLen; assetData = BinSearch.GetArrayChunk(mData, bodyStart, bodyLen); } return(assetData); }
/// <summary> /// Searched through 'data' for the givenBytes. /// </summary> /// <param name="startLocation">Where to start in 'data'</param> /// <param name="givenBytes">the stuff to look for</param> /// <param name="data">the big data to look through</param> /// <returns>location of the givenBytes, -1L if not found.</returns> public static long GetLocationOfGivenBytes(long startLocation, byte[] givenBytes, byte[] data, long maxDistance) { long retVal = -1L; long num = (long)(data.Length - givenBytes.Length); for (long location = startLocation; location < num; ++location) { if (BinSearch.Find(givenBytes, location, data)) { retVal = location; break; } else if (maxDistance < location - startLocation) { break; } } return(retVal); }
private string FindTgaFileNames(string fileName) { String retVal = ""; mMshPath = fileName.Substring(0, fileName.LastIndexOf('\\') + 1); byte[] bytes = File.ReadAllBytes(fileName); byte[] search = Encoding.ASCII.GetBytes(".tga"); List <long> locs = BinSearch.GetLocationsOfGivenBytes(0, search, bytes); for (int i = 0; i < locs.Count; i++) { retVal = retVal + GetStringFromData(bytes, locs[i]) + ".tga"; if (i != locs.Count - 1) { retVal += "; "; } } return(retVal); }
/// <summary> /// Returns AssetList items matching the search criteria /// </summary> /// <param name="data">The file bytes; something like 'File.ReadAllBytes(fileName);'</param> /// <param name="searchBytes">asset type; should be something like 'src_', 'tex_' or 'fx__'; but "LuaP" also works; /// I use 'ASCIIEncoding.ASCII.GetBytes("LuaP")' often. /// 1-line usage: /// 'GetItems(File.ReadAllBytes(fileName),ASCIIEncoding.ASCII.GetBytes("LuaP"));' /// </param> /// <returns></returns> public static List <AssetListItem> GetItems(byte[] data, byte[] searchBytes) { List <long> locations = BinSearch.GetLocationsOfGivenBytes(0L, searchBytes, data); List <AssetListItem> retVal = new List <AssetListItem>(); AssetListItem item = null; foreach (long loc in locations) { item = new AssetListItem(loc, data, searchBytes); if (BinSearch.GetLocationOfGivenBytes(loc, BodyBytes, data, 80L) > 0) { retVal.Add(item); } else { // NEED TO FIND OUT WHAT SOME OF THESE ARE!!! // System.Diagnostics.Debugger.Log(1, "INFO", "Not adding item:" + item.ToString()); Console.Error.WriteLine("I don't know how to classify this item: " + item.ToString()); retVal.Add(item); } } return(retVal); }
/*public string GetName() * { * long loc = BinSearch.GetLocationOfGivenBytes(mLocation, ASCIIEncoding.ASCII.GetBytes("NAME"), mData, 80); * if (loc > -1) return GetName(loc, mData); * return ""; * }*/ /// <summary> Gets the name of the source file that was munged into this data. </summary> public string GetName() { int headChunkLength = -1; string name = ""; long loc1 = -1; long loc = BinSearch.GetLocationOfGivenBytes(mLocation, ASCIIEncoding.ASCII.GetBytes("scr_"), mData, 40); if (loc < 0) { loc1 = BinSearch.GetLocationOfGivenBytes(mLocation, ASCIIEncoding.ASCII.GetBytes("mcfg"), mData, 80); if (loc1 > 0) { headChunkLength = ScriptSearchForm.GetLengthAtLocation(loc1 + 4, mData); loc1 = BinSearch.GetLocationOfGivenBytes(loc1 + headChunkLength - 8, ASCIIEncoding.ASCII.GetBytes("scr_"), mData, 80); } } else { loc1 = loc; } if (loc1 > 0) { loc = BinSearch.GetLocationOfGivenBytes(loc1, ASCIIEncoding.ASCII.GetBytes("NAME"), mData, 80); } if (loc > -1) { int nameLen = mData[(int)loc + 4] - 1; // -1 for null byte if (loc > 0) { // NAME + 4 bytes later = 8 name = Encoding.ASCII.GetString(mData, (int)loc + 8, (int)nameLen); } } return(name); }
private static byte[] StripDC(byte[] data) { byte b = 0; byte[] stripMe = ASCIIEncoding.ASCII.GetBytes("dc:"); List <long> locations = BinSearch.GetLocationsOfGivenBytes(0, stripMe, data); if (locations.Count == 0) { return(data); } List <byte> byteList = new List <byte>(data); for (int i = locations.Count - 1; i > -1; i--) { byteList.RemoveRange((int)locations[i], 3); // now, fix up the length of the string. b = byteList[(int)locations[i] - 4]; b -= 3; byteList[(int)locations[i] - 4] = b; } return(byteList.ToArray()); }
private string GetBody() { long loc = BinSearch.GetLocationOfGivenBytes(mLocation, ScriptSearchForm.BodyBytes, mData); int bodyLen = GetLengthAtLocation(loc + 4); string name = GetName(); string retVal = String.Format("-- NAME: {0} mLocation: 0x{1:x}; Body Length: {2}, Body Start: 0x{3:x}, Body End: 0x{4:x}", name, mLocation, bodyLen, loc + 8, loc + 8 + bodyLen); if (IsLuaCode) { if (ScriptSearchForm.ShowPcLuaCode) { string sourceFileName = FindSourceFile(name); string code = LookupPCcode(sourceFileName); int sz = ScriptSearchForm.LuacCodeSize(sourceFileName); if (bodyLen == sz) { retVal += "\n-- ********* LUAC Code Size MATCH!!! ***********"; byte[] b = File.ReadAllBytes(".\\tmp.luac"); byte[] c = this.GetAssetData(); int i = 0; for (i = 0; i < c.Length; i++) { if (b[i] != c[i]) { break; } } if (i == c.Length) { retVal += "\n-- ********* Binary Equal !!! ***********"; } } retVal = retVal + string.Format("\n-- {0}\n-- PC luac code size = {1}; PC code:\n{2}", sourceFileName, sz, code); } else if (ScriptSearchForm.ShowDecompiledLuaCode) { string listing = ShowLuaListing(this.GetAssetData()); string luaCode = ""; try { luaCode = DecompileLuacListing(listing); } catch (Exception ex) { luaCode = ex.Message + "\n" + ex.StackTrace; } retVal = string.Format("\n-- {0}\n{1}", GetName(), luaCode); } else { string listing = ShowLuaListing(this.GetAssetData()); retVal = string.Format("\n-- {0}\n-- luac -l listing \n{1}", GetName(), listing); } } else { retVal = retVal + String.Format("\n-- 50 bytes, including the previous 30 bytes {0}", BinSearch.GetByteString(mLocation - 30, mData, 50)); } return(retVal); }
//private static byte[] data = (byte[])null; #region OldMain private static int SearchMain(string[] args) { if (args.Length < 2) { Console.Error.WriteLine("USAGE: <FileName> <hex search number>"); Console.Error.WriteLine("OR: <FileName> <hex search number> <hex search number2> offset"); return(1); } byte[] data = (byte[])null; bool useOffset = false; string str = args[0]; string lower1 = args[1].ToLower(); if (args.Length == 4) { useOffset = true; } if (!File.Exists(str)) { Console.Error.WriteLine("File '{0}' Does not exist!!"); return(2); } Match match = new Regex("0x([0-9a-f]+)$").Match(lower1); match.Groups[1].ToString(); if (match == Match.Empty) { Console.Error.WriteLine("You must pass a valid hex number"); return(3); } if (lower1.Length % 2 == 1) { Console.Error.WriteLine("ERROR!!! You must pass a number with an even amount of digits."); return(4); } try { long length = new FileInfo(str).Length; FileStream fileStream = new FileStream(str, FileMode.Open); byte[] buffer = new byte[(int)length]; fileStream.Read(buffer, 0, (int)length); fileStream.Close(); data = buffer; } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); } byte[] hexNumber1 = BinSearch.GetHexNumber(lower1); long num = (long)(data.Length - hexNumber1.Length); if (useOffset) { string lower2 = args[2].ToLower(); string s = args[3].ToLower(); if (s.StartsWith("0x")) { s = s.Substring(2); } byte[] hexNumber2 = BinSearch.GetHexNumber(lower2); int offset = int.Parse(s, NumberStyles.AllowHexSpecifier); for (long location = 0; location < num; ++location) { if (BinSearch.Find_Offset(hexNumber1, hexNumber2, offset, location, data)) { Console.WriteLine("{0:x}", (object)location); } } } else { long location = GetLocationOfGivenBytes(0, hexNumber1, data); if (location > -1L) { Console.WriteLine("{0:x}", (object)location); } } return(0); }