Module: ELFTools::Util::ClassMethods
- Included in:
- ELFTools::Util
- Defined in:
- lib/elftools/util.rb
Overview
Class methods.
Instance Method Summary collapse
-
#align(num, bit) ⇒ Integer
Round up the number to be multiple of 2**bit.
-
#cstring(stream, offset) ⇒ String
Read from stream until reach a null-byte.
-
#select_by_type(enum, type) ⇒ Array<Object>
Select objects from enumerator with
.type
property equals totype
. -
#to_constant(mod, val) ⇒ Integer
Fetch the correct value from module
mod
.
Instance Method Details
#align(num, bit) ⇒ Integer
Round up the number to be multiple of 2**bit.
19 20 21 22 23 24 |
# File 'lib/elftools/util.rb', line 19 def align(num, bit) n = 2**bit return num if (num % n).zero? (num + n) & ~(n - 1) end |
#cstring(stream, offset) ⇒ String
Read from stream until reach a null-byte.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/elftools/util.rb', line 61 def cstring(stream, offset) stream.pos = offset # read until "\x00" ret = '' loop do c = stream.read(1) return nil if c.nil? # reach EOF break if c == "\x00" ret += c end ret end |
#select_by_type(enum, type) ⇒ Array<Object>
Select objects from enumerator with .type
property equals to type
.
Different from naive Array#select is this method will yield block whenever find a desired object.
This method is used to simplify the same logic in methods ELFFile#sections_by_type, ELFFile#segments_by_type, etc.
88 89 90 91 92 93 94 95 |
# File 'lib/elftools/util.rb', line 88 def select_by_type(enum, type) enum.select do |obj| if obj.type == type yield obj if block_given? true end end end |
#to_constant(mod, val) ⇒ Integer
Fetch the correct value from module mod
.
See ELFFile#segment_by_type for how to use this method.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/elftools/util.rb', line 36 def to_constant(mod, val) # Ignore the outest name. module_name = mod.name.sub('ELFTools::', '') # if val is an integer, check if exists in mod if val.is_a?(Integer) return val if mod.constants.any? { |c| mod.const_get(c) == val } raise ArgumentError, "No constants in #{module_name} is #{val}" end val = val.to_s.upcase prefix = module_name.split('::')[-1] val = "#{prefix}_#{val}" unless val.start_with?(prefix) val = val.to_sym raise ArgumentError, "No constants in #{module_name} named \"#{val}\"" unless mod.const_defined?(val) mod.const_get(val) end |