示例#1
0
        public int GetAllocatedMemorySize(FarPtr ptr)
        {
            // ptr should have 0 offset, but we need to reconvert back to segment 0x1000 base.
            var adjustedPtr = new FarPtr(_heapBaseSegment, (ushort)(ptr.Offset + ((ptr.Segment - _heapBaseSegment) << 4)));

            return(_memoryAllocator.GetAllocatedMemorySize(adjustedPtr));
        }
示例#2
0
        /// <summary>
        ///     Returns a newly allocated Segment in "Real Mode" memory
        /// </summary>
        /// <returns></returns>
        public FarPtr AllocateRealModeSegment(ushort segmentSize = ushort.MaxValue)
        {
            _currentRealModePointer.Segment++;
            var realModeSegment = new FarPtr(_currentRealModePointer);

            AddSegment(realModeSegment.Segment, segmentSize);
            return(realModeSegment);
        }
示例#3
0
        public MemoryCore()
        {
            _variablePointerDictionary = new Dictionary <string, FarPtr>();
            _currentVariablePointer    = new FarPtr(VARIABLE_BASE_SEGMENT, 0);
            _currentRealModePointer    = new FarPtr(REALMODE_BASE_SEGMENT, 0);
            _bigMemoryBlocks           = new PointerDictionary <Dictionary <ushort, FarPtr> >();

            //Add Segment 0 by default, stack segment
            AddSegment(0);
        }
示例#4
0
        /// <summary>
        ///     Deletes all defined Segments from Memory
        /// </summary>
        public void Clear()
        {
            Array.Clear(_memorySegments, 0, _memorySegments.Length);
            Array.Clear(_segments, 0, _segments.Length);
            Array.Clear(_decompiledSegments, 0, _decompiledSegments.Length);

            _variablePointerDictionary.Clear();
            _currentVariablePointer = new FarPtr(VARIABLE_BASE_SEGMENT, 0);
            _currentRealModePointer = new FarPtr(REALMODE_BASE_SEGMENT, 0);
            _bigMemoryBlocks.Clear();
        }
示例#5
0
        /// <summary>
        ///     Safe retrieval of a pointer to a defined variable
        ///
        ///     Returns false if the variable isn't defined
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pointer"></param>
        /// <returns></returns>
        public bool TryGetVariablePointer(string name, out FarPtr pointer)
        {
            if (!_variablePointerDictionary.TryGetValue(name, out var result))
            {
                pointer = null;
                return(false);
            }

            pointer = result;
            return(true);
        }
示例#6
0
        public override void Free(FarPtr ptr)
        {
            if (ptr.IsNull())
            {
                return;
            }

            // ptr should have 0 offset, but we need to reconvert back to segment 0x1000 base.
            var adjustedPtr = new FarPtr(_heapBaseSegment, (ushort)(ptr.Offset + ((ptr.Segment - _heapBaseSegment) << 4)));

            _memoryAllocator.Free(adjustedPtr);
        }
示例#7
0
        /// <summary>
        ///     Allocates the specified variable name with the desired size
        /// </summary>
        /// <param name="name"></param>
        /// <param name="size"></param>
        /// <param name="declarePointer"></param>
        /// <returns></returns>
        public FarPtr AllocateVariable(string name, ushort size, bool declarePointer = false)
        {
            if (!string.IsNullOrEmpty(name) && _variablePointerDictionary.ContainsKey(name))
            {
                _logger.Warn($"Attempted to re-allocate variable: {name}");
                return(_variablePointerDictionary[name]);
            }

            //Do we have enough room in the current segment?
            //If not, declare a new segment and start there
            if (size + _currentVariablePointer.Offset >= ushort.MaxValue)
            {
                _currentVariablePointer.Segment++;
                _currentVariablePointer.Offset = 0;
                AddSegment(_currentVariablePointer.Segment);
            }

            if (!HasSegment(_currentVariablePointer.Segment))
            {
                AddSegment(_currentVariablePointer.Segment);
            }

#if DEBUG
            //_logger.Debug(
            //    $"Variable {name ?? "NULL"} allocated {size} bytes of memory in Host Memory Segment {_currentVariablePointer.Segment:X4}:{_currentVariablePointer.Offset:X4}");
#endif
            var currentOffset = _currentVariablePointer.Offset;
            _currentVariablePointer.Offset += (ushort)(size + 1);

            var newPointer = new FarPtr(_currentVariablePointer.Segment, currentOffset);

            if (declarePointer && string.IsNullOrEmpty(name))
            {
                throw new Exception("Unsupported operation, declaring pointer type for NULL named variable");
            }

            if (!string.IsNullOrEmpty(name))
            {
                _variablePointerDictionary[name] = newPointer;

                if (declarePointer)
                {
                    var variablePointer = AllocateVariable($"*{name}", 0x4, false);
                    SetArray(variablePointer, newPointer.Data);
                }
            }

            return(newPointer);
        }
示例#8
0
 /// <summary>
 ///     Writes the specified byte the specified number of times starting at the specified pointer
 /// </summary>
 /// <param name="pointer"></param>
 /// <param name="count"></param>
 /// <param name="value"></param>
 public void FillArray(FarPtr pointer, ushort count, byte value) =>
 FillArray(pointer.Segment, pointer.Offset, count, value);
示例#9
0
 public override FarPtr GetBigMemoryBlock(FarPtr block, ushort index) => throw new NotImplementedException();
示例#10
0
 public uint GetDWord(FarPtr pointer) => GetWord(pointer.Segment, pointer.Offset);
示例#11
0
 public FarPtr GetPointer(FarPtr pointer) => new FarPtr(GetArray(pointer, 4));
示例#12
0
        public void SetZero(FarPtr pointer, int length)
        {
            var destinationSpan = new Span <byte>(_memorySegments[pointer.Segment], pointer.Offset, length);

            destinationSpan.Fill(0);
        }
示例#13
0
 public FarPtr GetBigMemoryBlock(FarPtr block, ushort index) => _bigMemoryBlocks[block.Offset][index];
示例#14
0
 public byte GetByte(FarPtr pointer) => GetByte(pointer.Segment, pointer.Offset);
示例#15
0
 public void SetPointer(ushort segment, ushort offset, FarPtr value) => SetArray(segment, offset, value.Data);
示例#16
0
 public virtual bool TryGetVariablePointer(string name, out FarPtr pointer)
 {
     throw new NotImplementedException();
 }
示例#17
0
 public virtual int GetAllocatedMemorySize(FarPtr ptr)
 {
     throw new NotImplementedException();
 }
示例#18
0
 public void SetArray(FarPtr pointer, ReadOnlySpan <byte> array) =>
 SetArray(pointer.Segment, pointer.Offset, array);
示例#19
0
 public ReadOnlySpan <byte> GetArray(FarPtr pointer, ushort count) =>
 GetArray(pointer.Segment, pointer.Offset, count);
示例#20
0
 public void SetDWord(FarPtr pointer, uint value) => SetDWord(pointer.Segment, pointer.Offset, value);
示例#21
0
 public void SetByte(FarPtr pointer, byte value) => SetByte(pointer.Segment, pointer.Offset, value);
示例#22
0
 public ReadOnlySpan <byte> GetString(FarPtr pointer, bool stripNull = false) =>
 GetString(pointer.Segment, pointer.Offset, stripNull);
示例#23
0
 public virtual void Free(FarPtr ptr)
 {
     throw new NotImplementedException();
 }
示例#24
0
 public void SetPointer(FarPtr pointer, FarPtr value) => SetArray(pointer, value.Data);
示例#25
0
 public virtual FarPtr GetBigMemoryBlock(FarPtr block, ushort index)
 {
     throw new NotImplementedException();
 }
示例#26
0
 public void SetPointer(string variableName, FarPtr value) =>
 SetArray(GetVariablePointer(variableName), value.Data);