Class: Ronin::Support::Binary::Memory Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/binary/memory.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Base class for all memory objects.

Since:

  • 1.0.0

Direct Known Subclasses

Array, Buffer, CString, Struct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size_or_string) ⇒ Memory

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the memory.

Parameters:

  • size_or_string (Integer, String, ByteSlice)

    The size of the buffer or an existing String which will be used as the underlying buffer.

Raises:

  • (ArgumentError)

    The argument was not an Integer, String, or ByteSlice.

Since:

  • 1.0.0



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ronin/support/binary/memory.rb', line 49

def initialize(size_or_string)
  case size_or_string
  when String, ByteSlice
    @string = size_or_string
  when Integer
    size    = size_or_string
    @string = String.new("\0" * size, encoding: Encoding::ASCII_8BIT)
  else
    raise(ArgumentError,"first argument must be either a size (Integer) or a buffer (String): #{size_or_string.inspect}")
  end
end

Instance Attribute Details

#stringString, ByteSlice (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The underlying String buffer.

Returns:

Since:

  • 1.0.0



37
38
39
# File 'lib/ronin/support/binary/memory.rb', line 37

def string
  @string
end

Instance Method Details

#+(offset) ⇒ ByteSlice

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a byte slice of the memory at the given offset and for the remainder of the memory.

Examples:

memory+10

Create a buffer starting at offset 10:

buffer = Buffer.new(memory+10)

Create an Array starting at offset 10:

array = Binary::Array.new(:int32_le, memory+10)

Parameters:

  • offset (Integer)

    The offset for the byte slice within the memory.

Returns:

Since:

  • 1.0.0



178
179
180
# File 'lib/ronin/support/binary/memory.rb', line 178

def +(offset)
  ByteSlice.new(@string, offset: offset, length: size - offset)
end

#[](index_or_range, length = nil) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a character or a substring from the underlying buffer at the given index.

Examples:

Reading a single char at the given index:

memory[0]
# => "\x00"

Reading multiple chars at the range of indexes:

memory[0..2]
# => "\x00\x00"

Reading multiple chars at the given index and length:

memory[0,2]
# => "\x00\x00"

Parameters:

  • index_or_range (Integer, (Integer, Integer), Range(Integer))

    The index or range within the buffer to read from.

  • length (Integer, Float::INFINITY, nil) (defaults to: nil)

    The optional length in bytes to read.

Returns:

  • (String, nil)

    The character or substring at the given index or range.

Since:

  • 1.0.0



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ronin/support/binary/memory.rb', line 95

def [](index_or_range,length=nil)
  case index_or_range
  when Range
    range = index_or_range

    @string[range]
  when Integer
    index  = index_or_range

    case length
    when Integer         then @string[index,length]
    when nil             then @string[index]
    when Float::INFINITY then @string[index,@string.length - index]
    else
      raise(ArgumentError,"invalid length (#{length.inspect}) must be an Integer, nil, or Float::INFINITY")
    end
  else
    raise(ArgumentError,"invalid index (#{index_or_range.inspect}) must be an Integer or a Range")
  end
end

#[]=(index_or_range, length = nil, value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Writes a value to the underlying buffer at the given index.

Examples:

Writing a single char:

buffer[0] = 'A'

Writing multiple characters to the given range of indexes:

buffer[0..3] = "AAA"

Parameters:

  • index_or_range (Integer, Range(Integer))

    The index within the string to write to.

  • length (Integer, Float::INFINITY, nil) (defaults to: nil)

    Optional additional length argument.

  • value (String)

    The integer, float, or character value to write to the buffer.

Returns:

  • (String)

    The string written into the buffer.

Since:

  • 1.0.0



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ronin/support/binary/memory.rb', line 137

def []=(index_or_range,length=nil,value)
  case index_or_range
  when Range
    range = index_or_range

    @string[range] = value
  when Integer
    index  = index_or_range

    case length
    when Integer then @string[index,length] = value
    when nil     then @string[index]        = value
    when Float::INFINITY
      @string[index,@string.length - index] = value
    else
      raise(ArgumentError,"invalid length (#{length.inspect}) must be an Integer, nil, or Float::INFINITY")
    end
  else
    raise(ArgumentError,"invalid index (#{index_or_range.inspect}) must be an Integer or a Range")
  end
end

#byteslice(offset, length = 1) ⇒ ByteSlice

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new byte slice within the memory.

Parameters:

  • offset (Integer)

    The offset of the new byte slice.

  • length (Integer) (defaults to: 1)

    The length of the new byte slice.

Returns:

Since:

  • 1.0.0



194
195
196
# File 'lib/ronin/support/binary/memory.rb', line 194

def byteslice(offset,length=1)
  ByteSlice.new(@string, offset: offset, length: length)
end

#clearself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clears the memory by setting each byte to 0.

Returns:

  • (self)

Since:

  • 1.0.0



203
204
205
206
207
208
209
# File 'lib/ronin/support/binary/memory.rb', line 203

def clear
  (0...@string.bytesize).each do |index|
    @string.setbyte(index,0)
  end

  return self
end

#copy_from(src, count = size) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copies data from the other memory object into this memory object.

Parameters:

  • src (Memory)

    The source memory object to copy the data from.

  • count (Integer) (defaults to: size)

    The number of bytes to copy.

Since:

  • 1.0.0



233
234
235
# File 'lib/ronin/support/binary/memory.rb', line 233

def copy_from(src,count=size)
  @string[0,count] = src[0,count]
end

#copy_to(dest, count = size) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copies data from this memory object into another memory object.

Parameters:

  • dest (Memory)

    The destination memory object to copy the data to.

  • count (Integer) (defaults to: size)

    The number of bytes to copy.

Since:

  • 1.0.0



220
221
222
# File 'lib/ronin/support/binary/memory.rb', line 220

def copy_to(dest,count=size)
  dest[0,count] = @string
end

#packString Also known as: to_s, to_str

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the buffer to a String.

Returns:

  • (String)

    The raw binary buffer.

Since:

  • 1.0.0



258
259
260
# File 'lib/ronin/support/binary/memory.rb', line 258

def pack
  @string.to_s
end

#read_from(io) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads #size bytes from the given IO stream.

Parameters:

  • io (IO)

    The IO stream to read from.

Returns:

  • (self)

Since:

  • 1.0.0



245
246
247
248
249
250
# File 'lib/ronin/support/binary/memory.rb', line 245

def read_from(io)
  data = io.read(size)

  @string[0,data.bytesize] = data
  return self
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The size of the underlying buffer.

Returns:

Since:

  • 1.0.0



66
67
68
# File 'lib/ronin/support/binary/memory.rb', line 66

def size
  @string.bytesize
end