private string ReadMbString(LoadedImage img, TWord pstrLibName) { int iStart = (int)(pstrLibName - img.BaseAddress.ToLinear()); int i = iStart; for (i = iStart; img.Bytes[i] != 0; ++i) { } return(Encoding.ASCII.GetString(img.Bytes, iStart, i - iStart)); }
private void Rol(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); byte sh = (byte)Read(src); TWord r = (l << sh) | (l >> (32 - sh)); Write(dst, r); Flags = (r == 0 ? Zmask : 0u); // Zero }
private void Shr(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); byte sh = (byte)Read(src); TWord r = l >> sh; Write(dst, r); Flags = (r == 0 ? Zmask : 0u); // Zero }
private void Scasb() { byte al = (byte)ReadRegister(X86.Registers.al); TWord edi = ReadRegister(X86.Registers.edi); byte mem = (byte)(al - img.Bytes[edi - img.BaseAddress.ToLinear()]); WriteRegister(X86.Registers.edi, edi + 1); //$BUG: Direction flag not respected Flags = (mem == 0 ? Zmask : 0u); }
public bool InterceptCall(IProcessorEmulator emu, TWord l) { ExternalProcedure epProc; if (!this.InterceptedCalls.TryGetValue(l, out epProc)) { return(false); } ((SimulatedProc)epProc).Emulator(emu); return(true); }
public Win32Emulator(LoadedImage img, Platform platform, Dictionary <Address, ImportReference> importReferences) { this.img = img; this.platform = platform; this.uPseudoFn = 0xDEAD0000u; // unlikely to be a real pointer to a function this.InterceptedCalls = new Dictionary <uint, ExternalProcedure>(); modules = new Dictionary <string, Module>(StringComparer.InvariantCultureIgnoreCase); AddWellKnownProcedures(); InterceptCallsToImports(importReferences); }
public Win32Emulator(LoadedImage img, Platform platform, Dictionary<Address, ImportReference> importReferences) { this.img = img; this.platform = platform; this.uPseudoFn = 0xDEAD0000u; // unlikely to be a real pointer to a function this.InterceptedCalls = new Dictionary<uint, ExternalProcedure>(); modules = new Dictionary<string, Module>(StringComparer.InvariantCultureIgnoreCase); AddWellKnownProcedures(); InterceptCallsToImports(importReferences); }
private void Run() { int counter = 0; try { while (running && dasm.MoveNext()) { // Debug.Print("emu: {0} {1,-15} {2}", dasm.Current.Address, dasm.Current, DumpRegs()); Action bpAction; TWord eip = (uint)dasm.Current.Address.ToLinear(); if (bpExecute.TryGetValue(eip, out bpAction)) { ++counter; stepOverAddress = 0; stepInto = false; bpAction(); if (!running) { break; } } else if (stepInto) { stepInto = false; var s = stepAction; stepAction = null; s(); if (!running) { break; } } else if (stepOverAddress == eip) { stepOverAddress = 0; var s = stepAction; stepAction = null; s(); if (!running) { break; } } Execute(dasm.Current); } } catch (Exception ex) { Debug.Print("Emulator exception when executing {0}. {1}\r\n{2}", dasm.Current, ex.Message, ex.StackTrace); ExceptionRaised.Fire(this); } }
private void Call(MachineOperand op) { Push(InstructionPointer.ToLinear() + (uint)dasm.Current.Length); // Push return value on stack TWord l = Read(op); if (envEmulator.InterceptCall(this, l)) { return; } InstructionPointer = Address.Ptr32(l); }
public static TPage ToPRD(PageOcrData ocrData, String saveToPrdPath) #endif { try { if (ocrData != null) { TPage res = new TPage(); //-- iterate data lines --\\ foreach (LineOcrData lineOcr in ocrData.Lines) { TLine currentLine = new TLine(); //-- Itarate the line's words --\\ foreach (WordOcrData wordOcr in lineOcr.Words) { //-- Iterate the word's chars --\\ TWord wordPrd = new TWord(); foreach (CharOcrData charOcr in wordOcr.GetAsChars(true)) { wordPrd.AddChar(new TChar(charOcr.Value, (short)charOcr.Confidence, new TOCRRect(charOcr.Rect))); } currentLine.AddWord(wordPrd); } if (currentLine != null && currentLine.Words != null && currentLine.Words.Count > 0) { res.AddLine(currentLine); } } //-- Save the contents to a PRD file if 'saveToPrdPath' is specified --\\ if (!String.IsNullOrEmpty(saveToPrdPath)) { if (!Directory.Exists(Path.GetDirectoryName(saveToPrdPath))) { Directory.CreateDirectory(Path.GetDirectoryName(saveToPrdPath)); } TPage.SaveToPRD(res, saveToPrdPath); } return(res); } else { throw new Exception(String.Format("Invalid input PRD object in: [{0}]", MethodBase.GetCurrentMethod().Name)); } } catch (Exception ex) { ILog.LogError(ex); } return(null); }
// Copy part of word - return the new word private static TWord CopyFromWord(TWord word, int strtIdx, int len) { var subWord = new TWord(); for (int i = strtIdx; i < len; i++) { subWord.AddChar(word.Chars[i].Clone() as TChar); } subWord.Confidance = word.Confidance; subWord.AddStyle(word.Style); return(subWord); }
private void Inc(MachineOperand op) { TWord old = Read(op); TWord gnu = old + 1; Write(op, gnu); uint ov = ((old ^ gnu) & gnu & 0x80000000u) >> 20; Flags = Flags & Cmask | // Carry preserved (gnu == 0 ? Zmask : 0u) | // Zero ov; //$BUG: }
public void AddWordTest() { TPage target = new TPage(); RecognitionCommonTests.TiS_Recognition_Common_TPageAccessor accessor = new RecognitionCommonTests.TiS_Recognition_Common_TPageAccessor(target); TWord oNewWord = new TWord(); accessor.AddWord(oNewWord); Assert.IsTrue(target.Lines.Count == 1, "number of lines is wrong"); Assert.IsTrue(target.Lines[0].Words.Contains(oNewWord) == true, "Target must contain added word"); }
public void WordTest() { string sPRDFileName = "P0001.PRD"; TPage oPage = TPage.LoadFromPRD(sPRDFileName);; int iLine = 0; int iWord = 0; TWord expected = oPage.Lines[iLine].Words[iWord];; TWord actual; actual = oPage.Word(iLine, iWord); Assert.AreEqual(expected, actual, "TiS.Recognition.Common.TPage.Word did not return the expected value."); }
// Split word into 2 words (if necessary) private static IEnumerable <TWord> SplitWord(TWord word) { var colonIdx = word.Data.IndexOf(':'); if (colonIdx > 0 && colonIdx + 1 < word.Chars.Count) { yield return(CopyFromWord(word, 0, colonIdx + 1)); yield return(CopyFromWord(word, colonIdx + 1, word.Chars.Count)); } else { yield return(word); } }
/// <summary> ///A Create PRD ///</summary> public TPage CreatePRD() { TPage page = new TPage(); TLine line1 = new TLine(); TLine line2 = new TLine(); TLine line3 = new TLine(); TWord word1 = new TWord(); TWord word2 = new TWord(); TWord word3 = new TWord(); TWord word4 = new TWord(); TWord word5 = new TWord(); word1.AddChar(new TChar('A', 100, new TOCRRect(10, 10, 20, 20))); word1.AddChar(new TChar('B', 100, new TOCRRect(40, 10, 20, 20))); word1.AddChar(new TChar('C', 100, new TOCRRect(70, 10, 20, 20))); word2.AddChar(new TChar('D', 100, new TOCRRect(100, 10, 20, 20))); word2.AddChar(new TChar('E', 100, new TOCRRect(140, 10, 20, 20))); word2.AddChar(new TChar('F', 100, new TOCRRect(170, 10, 20, 20))); word3.AddChar(new TChar('1', 100, new TOCRRect(10, 40, 20, 20))); word3.AddChar(new TChar('2', 100, new TOCRRect(40, 40, 20, 20))); word3.AddChar(new TChar('3', 100, new TOCRRect(70, 40, 20, 20))); word4.AddChar(new TChar('X', 100, new TOCRRect(10, 70, 20, 20))); word4.AddChar(new TChar('Y', 100, new TOCRRect(40, 70, 20, 20))); word4.AddChar(new TChar('Z', 100, new TOCRRect(70, 70, 20, 20))); word5.AddChar(new TChar('W', 100, new TOCRRect(110, 70, 20, 20))); word5.AddChar(new TChar('V', 100, new TOCRRect(140, 70, 20, 20))); word5.AddChar(new TChar('Q', 100, new TOCRRect(170, 70, 20, 20))); line1.AddWord(word1); line1.AddWord(word2); line2.AddWord(word3); line3.AddWord(word4); line3.AddWord(word5); page.AddLine(line1); page.AddLine(line2); page.AddLine(line3); return(page); }
private void Scasb() { byte al = (byte)ReadRegister(X86.Registers.al); TWord edi = ReadRegister(X86.Registers.edi); var addr = Address.Ptr32(edi); ImageSegment seg; if (!map.TryFindSegment(addr, out seg)) { throw new AccessViolationException(); } byte mem = (byte)(al - seg.MemoryArea.Bytes[edi - (uint)seg.MemoryArea.BaseAddress.ToLinear()]); WriteRegister(X86.Registers.edi, edi + 1); //$BUG: Direction flag not respected Flags = (mem == 0 ? Zmask : 0u); }
private void And(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) { r = (TWord)(sbyte)r; } var and = l & r; Write(dst, and); Flags = 0 | // Clear Carry (and == 0 ? Zmask : 0u) | // Zero 0; // Clear Overflow }
private void Xor(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) { r = (TWord)(sbyte)r; } var xor = l ^ r; Write(dst, xor); Flags = 0 | // Carry (xor == 0 ? Zmask : 0u) | // Zero 0; // Overflow }
/// <summary> /// Sets the next character. /// </summary> /// <param name="cCharData">The c character data.</param> /// <param name="iConfidence">The i confidence.</param> /// <param name="iLeft">The i left.</param> /// <param name="iTop">The i top.</param> /// <param name="iWidth">Width of the i.</param> /// <param name="iHeight">Height of the i.</param> /// <param name="iLine">The i line.</param> /// <param name="iWord">The i word.</param> public void SetNextChar(char cCharData, short iConfidence, int iLeft, int iTop, int iWidth, int iHeight, int iLine, int iWord) { if ((cCharData == ' ')) { m_oSetLine.AddWord(m_oSetWord); m_oSetWord = new TWord(); } else if (cCharData == '\n') { m_oPage.AddLine(m_oSetLine); m_oSetLine = new TLine(); } else { TChar oSetChar = new TChar(cCharData, iConfidence, new TOCRRect(iLeft, iTop, iWidth, iHeight)); m_oSetWord.AddChar(oSetChar); } }
private void Adc(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); TWord sum = l + r + (Flags & 1); Write(dst, sum); var newCy = ((l & r) | ((l | r) & (~(sum)))) >> 31; uint ov = ((~(l ^ r) & (l ^ sum)) & 0x80000000u) >> 20; Flags = (newCy) | // Carry (sum == 0 ? 1u << 6 : 0u) | // Zero (ov) // Overflow ; }
private TWord GetEffectiveAddress(MemoryOperand m) { TWord ea = 0; if (m.Offset.IsValid) { ea += m.Offset.ToUInt32(); } if (m.Index != RegisterStorage.None) { ea += ReadRegister(m.Index) * m.Scale; } if (m.Base != null && m.Base != RegisterStorage.None) { ea += ReadRegister(m.Base); } return(ea); }
private TWord Read(MachineOperand op) { var r = op as RegisterOperand; if (r != null) { return(ReadRegister(r.Register)); } var i = op as ImmediateOperand; if (i != null) { return(i.Value.ToUInt32()); } var a = op as AddressOperand; if (a != null) { return(a.Address.ToUInt32()); } var m = op as MemoryOperand; if (m != null) { TWord ea = GetEffectiveAddress(m); byte b; switch (op.Width.Size) { case 1: if (!TryReadByte(Address.Ptr32(ea), out b)) { throw new IndexOutOfRangeException(); } else { return(b); } case 4: return(ReadLeUInt32(Address.Ptr32(ea))); } throw new NotImplementedException(); } throw new NotImplementedException(); }
private void Cmp(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) { r = (TWord)(sbyte)r; } r = ~r + 1u; TWord diff = l + r; uint ov = ((~(l ^ r) & (l ^ diff)) & 0x80000000u) >> 20; Flags = (l < diff ? 1u : 0u) | // Carry (diff == 0 ? Zmask : 0u) | // Zero (ov) // Overflow ; }
private void Write(MachineOperand op, TWord w) { if (op is RegisterOperand r) { WriteRegister(r.Register, w); return; } if (op is MemoryOperand m) { var ea = GetEffectiveAddress(m); switch (op.Width.Size) { case 1: WriteByte(Address.Ptr32(ea), (byte)w); return; case 4: WriteLeUInt32(Address.Ptr32(ea), (UInt32)w); return; } throw new NotImplementedException(); } throw new NotImplementedException(); }
private void Add(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) { r = (TWord)(sbyte)r; } TWord sum = l + r; Write(dst, sum); uint ov = ((~(l ^ r) & (l ^ sum)) & 0x80000000u) >> 20; Flags = (r > sum ? 1u : 0u) | // Carry (sum == 0 ? 1u << 6: 0u) | // Zero (ov) // Overflow ; }
private void Sub(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) { r = (TWord)(sbyte)r; } r = ~r + 1u; // Two's complement subtraction. TWord diff = l + r; Write(dst, diff); uint ov = ((~(l ^ r) & (l ^ diff)) & 0x80000000u) >> 20; Flags = (l < diff ? 1u : 0u) | // Carry (diff == 0 ? Zmask : 0u) | // Zero (ov) // Overflow ; }
public void WordAtPointTest() { TPage page = m_TestPage; DataLocator target = new DataLocator(page); Point point = new Point(); // TODO: Initialize to an appropriate value TWord expected = null; TWord actual; actual = target.WordAtPoint(point); Assert.AreEqual(expected, actual, "Recognition.Locator.DataLocator.WordAtPoint did not return the expected value."); point = new Point(325, 285); expected = page.Lines[0].Words[0]; actual = target.WordAtPoint(point); Assert.AreEqual(expected, actual, "Recognition.Locator.DataLocator.HasLineAtPoint did not return the expected value." + ""); }
private void Write(MachineOperand op, TWord w) { var r = op as RegisterOperand; if (r != null) { WriteRegister(r.Register, w); return; } var m = op as MemoryOperand; if (m != null) { var ea = GetEffectiveAddress(m); switch (op.Width.Size) { case 1: img.WriteByte(Address.Ptr32(ea), (byte)w); return; case 4: img.WriteLeUInt32(Address.Ptr32(ea), (UInt32)w); return; } throw new NotImplementedException(); } throw new NotImplementedException(); }
private void Scasb() { byte al = (byte) ReadRegister(X86.Registers.al); TWord edi = ReadRegister(X86.Registers.edi); byte mem = (byte)(al - img.Bytes[edi - img.BaseAddress.ToLinear()]); WriteRegister(X86.Registers.edi, edi + 1); //$BUG: Direction flag not respected Flags = (mem == 0 ? Zmask : 0u); }
private void Add(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) r = (TWord)(sbyte)r; TWord sum = l + r; Write(dst, sum); uint ov = ((~(l ^ r) & (l ^ sum)) & 0x80000000u) >> 20; Flags = (r > sum ? 1u : 0u) | // Carry (sum == 0 ? 1u << 6: 0u) | // Zero (ov) // Overflow ; }
private void Sub(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) r = (TWord)(sbyte)r; r = ~r + 1u; // Two's complement subtraction. TWord diff = l + r; Write(dst, diff); uint ov = ((~(l ^ r) & (l ^ diff)) & 0x80000000u) >> 20; Flags = (l < diff ? 1u : 0u) | // Carry (diff == 0 ? Zmask : 0u) | // Zero (ov) // Overflow ; }
public void WriteRegister(RegisterStorage r, TWord value) { Registers[r.Number] = (Registers[r.Number] & ~r.BitMask) | (value << (int)r.BitAddress); }
private void Scasb() { byte al = (byte) ReadRegister(X86.Registers.al); TWord edi = ReadRegister(X86.Registers.edi); var addr = Address.Ptr32(edi); ImageSegment seg; if (!map.TryFindSegment(addr, out seg)) throw new AccessViolationException(); byte mem = (byte)(al - seg.MemoryArea.Bytes[edi - (uint)seg.MemoryArea.BaseAddress.ToLinear()]); WriteRegister(X86.Registers.edi, edi + 1); //$BUG: Direction flag not respected Flags = (mem == 0 ? Zmask : 0u); }
private void Cmp(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) r = (TWord)(sbyte)r; r = ~r + 1u; TWord diff = l + r; uint ov = ((~(l ^ r) & (l ^ diff)) & 0x80000000u) >> 20; Flags = (l < diff ? 1u : 0u) | // Carry (diff == 0 ? Zmask : 0u) | // Zero (ov) // Overflow ; }
/// <summary> /// Requests the emulator to place itself in step-over mode. This means /// it should execute the next instruction then call the provided /// <paramref name="callback" />. If the instruction /// is a CALL or a REP[NZ] the call will be taken and the REP will be /// carried out before resuming. /// </summary> public void StepOver(Action callback) { stepOverAddress = (TWord)((long)dasm.Current.Address.ToLinear() + dasm.Current.Length); stepAction = callback; }
private void And(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) r = (TWord)(sbyte)r; var and = l & r; Write(dst, and); Flags = 0 | // Clear Carry (and == 0 ? Zmask : 0u) | // Zero 0; // Clear Overflow }
private string ReadMbString(LoadedImage img, TWord pstrLibName) { int iStart = (int)(pstrLibName - img.BaseAddress.ToLinear()); int i = iStart; for (i = iStart; img.Bytes[i] != 0; ++i) { } return Encoding.ASCII.GetString(img.Bytes, iStart, i - iStart); }
private void Xor(MachineOperand dst, MachineOperand src) { TWord l = Read(dst); TWord r = Read(src); if (src.Width.Size < dst.Width.Size) r = (TWord)(sbyte)r; var xor = l ^ r; Write(dst, xor); Flags = 0 | // Carry (xor == 0 ? Zmask : 0u) | // Zero 0; // Overflow }
private void Run() { int counter = 0; try { while (running && dasm.MoveNext()) { // Debug.Print("emu: {0} {1,-15} {2}", dasm.Current.Address, dasm.Current, DumpRegs()); Action bpAction; TWord eip = (uint)dasm.Current.Address.ToLinear(); if (bpExecute.TryGetValue(eip, out bpAction)) { ++counter; stepOverAddress = 0; stepInto = false; bpAction(); if (!running) break; } else if (stepInto) { stepInto = false; var s = stepAction; stepAction = null; s(); if (!running) break; } else if (stepOverAddress == eip) { stepOverAddress = 0; var s = stepAction; stepAction = null; s(); if (!running) break; } Execute(dasm.Current); } } catch (Exception ex) { Debug.Print("Emulator exception when executing {0}. {1}\r\n{2}", dasm.Current, ex.Message, ex.StackTrace); ExceptionRaised.Fire(this); } }
public void WriteRegister(RegisterStorage r, TWord value) { ((IntelRegister)r).SetRegisterFileValues(Registers, value, Valid); }
public ElfReloc(elf_rela <TWord> rela, TWord symbolTable) : this(new elf_rel <TWord> { r_info = rela.r_info, r_offset = rela.r_offset }, symbolTable) =>
/// <summary> /// Initializes a new instance of the <see cref="OCRWordResult"/> class. /// </summary> /// <param name="oWord">The o word.</param> public OCRWordResult(TWord oWord) { m_oWord = oWord; }
public bool InterceptCall(IProcessorEmulator emu, TWord l) { ExternalProcedure epProc; if (!this.InterceptedCalls.TryGetValue(l, out epProc)) return false; ((SimulatedProc)epProc).Emulator(emu); return true; }