Class: Ronin::Support::Binary::Memory Private
- Inherits:
-
Object
- Object
- Ronin::Support::Binary::Memory
- 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.
Instance Attribute Summary collapse
-
#string ⇒ String, ByteSlice
readonly
private
The underlying String buffer.
Instance Method Summary collapse
-
#+(offset) ⇒ ByteSlice
private
Returns a byte slice of the memory at the given offset and for the remainder of the memory.
-
#[](index_or_range, length = nil) ⇒ String?
private
Reads a character or a substring from the underlying buffer at the given index.
-
#[]=(index_or_range, length = nil, value) ⇒ String
private
Writes a value to the underlying buffer at the given index.
-
#byteslice(offset, length = 1) ⇒ ByteSlice
private
Creates a new byte slice within the memory.
-
#clear ⇒ self
private
Clears the memory by setting each byte to 0.
-
#copy_from(src, count = size) ⇒ Object
private
Copies data from the other memory object into this memory object.
-
#copy_to(dest, count = size) ⇒ Object
private
Copies data from this memory object into another memory object.
-
#initialize(size_or_string) ⇒ Memory
constructor
private
Initializes the memory.
-
#pack ⇒ String
(also: #to_s, #to_str)
private
Converts the buffer to a String.
-
#read_from(io) ⇒ self
private
Reads #size bytes from the given IO stream.
-
#size ⇒ Integer
private
The size of the underlying buffer.
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.
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
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.
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.
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.
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.
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 |
#clear ⇒ 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.
Clears the memory by setting each byte to 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.
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.
220 221 222 |
# File 'lib/ronin/support/binary/memory.rb', line 220 def copy_to(dest,count=size) dest[0,count] = @string end |
#pack ⇒ String 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.
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.
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 |
#size ⇒ Integer
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.
66 67 68 |
# File 'lib/ronin/support/binary/memory.rb', line 66 def size @string.bytesize end |