Class: MachO::LoadCommands::LoadCommand::LCStr

Inherits:
Object
  • Object
show all
Defined in:
lib/macho/load_commands.rb

Overview

Represents a Load Command string. A rough analogue to the lc_str struct used internally by OS X. This class allows ruby-macho to pretend that strings stored in LCs are immediately available without explicit operations on the raw Mach-O data.

Instance Method Summary collapse

Constructor Details

#initialize(lc, lc_str) ⇒ LCStr

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.

TODO:

devise a solution such that the lc_str parameter is not interpreted differently depending on lc.view. The current behavior is a hack to allow viewless load command creation.

Returns a new instance of LCStr.

Parameters:

  • lc (LoadCommand)

    the load command

  • lc_str (Integer, String)

    the offset to the beginning of the string, or the string itself if not being initialized with a view.

Raises:



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/macho/load_commands.rb', line 300

def initialize(lc, lc_str)
  view = lc.view

  if view
    lc_str_abs = view.offset + lc_str
    lc_end = view.offset + lc.cmdsize - 1
    raw_string = view.raw_data.slice(lc_str_abs..lc_end)
    @string, null_byte, _padding = raw_string.partition("\x00")

    raise LCStrMalformedError, lc if null_byte.empty?

    @string_offset = lc_str
  else
    @string = lc_str
    @string_offset = 0
  end
end

Instance Method Details

#to_hHash

Returns a hash representation of this MachO::LoadCommands::LoadCommand::LCStr.

Returns:



330
331
332
333
334
335
# File 'lib/macho/load_commands.rb', line 330

def to_h
  {
    "string" => to_s,
    "offset" => to_i,
  }
end

#to_iInteger

Returns the offset to the beginning of the string in the load command.

Returns:

  • (Integer)

    the offset to the beginning of the string in the load command



325
326
327
# File 'lib/macho/load_commands.rb', line 325

def to_i
  @string_offset
end

#to_sString

Returns a string representation of the LCStr.

Returns:

  • (String)

    a string representation of the LCStr



319
320
321
# File 'lib/macho/load_commands.rb', line 319

def to_s
  @string
end