Class: MemoryIO::Context
- Inherits:
-
Object
- Object
- MemoryIO::Context
- Defined in:
- lib/memory_io/context.rb
Overview
Describes how the memory being accessed lays out its data.
The context belongs to the memory, not to the machine running this library. They only coincide when the memory belongs to a process on the same host.
Constant Summary collapse
- ENDIANS =
Byte orders that can be asked for.
:nativeresolves to the byte order of the host, which is the right answer whenever the memory belongs to a process running on it. %i[little big native].freeze
- NATIVE_ENDIAN =
The byte order of the host.
[1].pack('S') == "\x01\x00".b ? :little : :big
- DEFAULT_POINTER_SIZE =
Assumed when nothing more specific is known.
8
Instance Attribute Summary collapse
-
#endian ⇒ :little, :big
readonly
Byte order of the memory.
-
#pointer_size ⇒ Integer
readonly
Size of a pointer, in bytes.
Class Method Summary collapse
-
.default ⇒ MemoryIO::Context
Used when a stream carries no context of its own.
-
.from_elf(path) ⇒ MemoryIO::Context?
Derive a context from an ELF file, which describes the memory it is loaded into.
-
.of(stream) ⇒ MemoryIO::Context
The context
streamwas tagged with, or Context.default when it carries none.
Instance Method Summary collapse
-
#initialize(endian: :native, pointer_size: DEFAULT_POINTER_SIZE) ⇒ Context
constructor
A new instance of Context.
-
#to_h ⇒ Hash
The attributes, in the form #initialize accepts.
Constructor Details
#initialize(endian: :native, pointer_size: DEFAULT_POINTER_SIZE) ⇒ Context
Returns a new instance of Context.
49 50 51 52 53 54 55 |
# File 'lib/memory_io/context.rb', line 49 def initialize(endian: :native, pointer_size: DEFAULT_POINTER_SIZE) raise ArgumentError, "endian must be one of #{ENDIANS.inspect}, got #{endian.inspect}" \ unless ENDIANS.include?(endian) @endian = endian == :native ? NATIVE_ENDIAN : endian @pointer_size = pointer_size end |
Instance Attribute Details
#endian ⇒ :little, :big (readonly)
Returns Byte order of the memory. :native has already been resolved.
26 27 28 |
# File 'lib/memory_io/context.rb', line 26 def endian @endian end |
#pointer_size ⇒ Integer (readonly)
Returns Size of a pointer, in bytes.
30 31 32 |
# File 'lib/memory_io/context.rb', line 30 def pointer_size @pointer_size end |
Class Method Details
.default ⇒ MemoryIO::Context
Returns Used when a stream carries no context of its own.
60 61 62 |
# File 'lib/memory_io/context.rb', line 60 def default @default ||= new end |
.from_elf(path) ⇒ MemoryIO::Context?
Derive a context from an ELF file, which describes the memory it is loaded into.
85 86 87 88 89 90 91 92 |
# File 'lib/memory_io/context.rb', line 85 def from_elf(path) ::File.open(path, 'rb') do |file| elf = ELFTools::ELFFile.new(file) new(endian: elf.endian, pointer_size: elf.elf_class / 8) end rescue SystemCallError, ELFTools::ELFError nil end |
.of(stream) ⇒ MemoryIO::Context
Returns The context stream was tagged with, or default when it carries none.
69 70 71 |
# File 'lib/memory_io/context.rb', line 69 def of(stream) stream.is_a?(MemoryIO::Stream) ? stream.context : default end |
Instance Method Details
#to_h ⇒ Hash
Returns The attributes, in the form #initialize accepts.
34 35 36 |
# File 'lib/memory_io/context.rb', line 34 def to_h { endian: endian, pointer_size: pointer_size } end |