Class: Ronin::Support::Binary::Stream

Inherits:
Object
  • Object
show all
Includes:
CTypes::Mixin, Methods
Defined in:
lib/ronin/support/binary/stream.rb,
lib/ronin/support/binary/stream/methods.rb

Overview

Represents a binary stream of data.

Examples

Creates a binary stream around a file:

stream = Stream.open('path/to/file.bin')
stream.read_uint32
# => 1234

Creates a binary stream around a socket:

socket = TCPSocket.new('example.com',1337)
stream = Stream.new(socket)
stream.read_uint32
# => 1234
stream.write_uint32(0xffffffff)

Since:

  • 1.0.0

Defined Under Namespace

Modules: Methods

Instance Attribute Summary collapse

Attributes included from CTypes::Mixin

#arch, #endian, #os, #type_resolver, #type_system

IO Compatibility Methods collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

#read_array, #read_array_of, #read_array_of_byte, #read_array_of_char, #read_array_of_double, #read_array_of_float, #read_array_of_float32, #read_array_of_float64, #read_array_of_int, #read_array_of_int16, #read_array_of_int32, #read_array_of_int64, #read_array_of_int8, #read_array_of_long, #read_array_of_long_long, #read_array_of_short, #read_array_of_uchar, #read_array_of_uint, #read_array_of_uint16, #read_array_of_uint32, #read_array_of_uint64, #read_array_of_uint8, #read_array_of_ulong, #read_array_of_ulong_long, #read_array_of_ushort, #read_buffer, #read_byte, #read_char, #read_double, #read_float, #read_float32, #read_float64, #read_int, #read_int16, #read_int32, #read_int64, #read_int8, #read_into, #read_long, #read_long_long, #read_short, #read_string, #read_struct, #read_uchar, #read_uint, #read_uint16, #read_uint32, #read_uint64, #read_uint8, #read_ulong, #read_ulong_long, #read_union, #read_ushort, #read_value, #write_array_of, #write_array_of_byte, #write_array_of_char, #write_array_of_double, #write_array_of_float, #write_array_of_float32, #write_array_of_float64, #write_array_of_int, #write_array_of_int16, #write_array_of_int32, #write_array_of_int64, #write_array_of_int8, #write_array_of_long, #write_array_of_long_long, #write_array_of_short, #write_array_of_uchar, #write_array_of_uint, #write_array_of_uint16, #write_array_of_uint32, #write_array_of_uint64, #write_array_of_uint8, #write_array_of_ulong, #write_array_of_ulong_long, #write_array_of_ushort, #write_byte, #write_char, #write_double, #write_float, #write_float32, #write_float64, #write_int, #write_int16, #write_int32, #write_int64, #write_int8, #write_long, #write_long_long, #write_short, #write_string, #write_uchar, #write_uint, #write_uint16, #write_uint32, #write_uint64, #write_uint8, #write_ulong, #write_ulong_long, #write_ushort, #write_value

Methods included from CTypes::Mixin

#initialize_type_system

Constructor Details

#initialize(io, **kwargs) ⇒ Stream

Initializes the stream.

The desired architecture for the values of the IO stream.

Parameters:

  • io (IO, StringIO)

    The underlying IO stream.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :endian (:little, :big, :net, nil)

    The desired endianness of the values of the IO stream.

  • :arch (:x86, :x86_64, :ppc, :ppc64, :mips, :mips_le, :mips_be, :mips64, :mips64_le, :mips64_be, :arm, :arm_le, :arm_be, :arm64, :arm64_le, :arm64_be)

Since:

  • 1.0.0



78
79
80
81
82
# File 'lib/ronin/support/binary/stream.rb', line 78

def initialize(io, **kwargs)
  initialize_type_system(**kwargs)

  @io = io
end

Instance Attribute Details

#ioIO, StringIO (readonly)

The underlying IO stream.

Returns:

  • (IO, StringIO)

Since:

  • 1.0.0



56
57
58
# File 'lib/ronin/support/binary/stream.rb', line 56

def io
  @io
end

Class Method Details

.open(path, mode = 'r') ⇒ Stream

Opens a file in binary mode and returns a stream.

Parameters:

  • path (String)

    The path to the file.

  • mode ("r", "w", "r+", "w+") (defaults to: 'r')

    The mode to open the file in.

Returns:

  • (Stream)

    The newly created stream.

Since:

  • 1.0.0



96
97
98
# File 'lib/ronin/support/binary/stream.rb', line 96

def self.open(path,mode='r')
  new(File.new(path,"#{mode}b"))
end

Instance Method Details

#eof?Boolean

Determines if EOF has been reached.

Returns:

  • (Boolean)

Since:

  • 1.0.0



118
119
120
# File 'lib/ronin/support/binary/stream.rb', line 118

def eof?
  @io.eof?
end

#external_encodingEncoding

Returns the external encoding for the stream.

Returns:

Since:

  • 1.0.0



109
110
111
# File 'lib/ronin/support/binary/stream.rb', line 109

def external_encoding
  @io.external_encoding
end

#read(length = nil) ⇒ String

Reads a string from the IO stream.

Parameters:

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

    The desired length of the string.

Returns:

  • (String)

    The read String.

Since:

  • 1.0.0



131
132
133
# File 'lib/ronin/support/binary/stream.rb', line 131

def read(length=nil)
  @io.read(length)
end

#write(data) ⇒ Integer

Writes String to the IO stream.

Parameters:

  • data (String)

    The data to write to the IO stream.

Returns:

  • (Integer)

    The number of bytes written to the IO stream.

Since:

  • 1.0.0



144
145
146
# File 'lib/ronin/support/binary/stream.rb', line 144

def write(data)
  @io.write(data)
end