module PatchELF::Helper

Helper methods for internal usage.

Constants

COLOR_CODE

Color codes for pretty print.

PAGE_SIZE

The size of one page.

Public Instance Methods

aligndown(val, align = PAGE_SIZE) click to toggle source

@param [Integer] val @param [Integer] align @return [Integer]

Aligned result.

@example

aligndown(0x1234)
#=> 4096
aligndown(0x33, 0x20)
#=> 32
aligndown(0x10, 0x8)
#=> 16
# File lib/patchelf/helper.rb, line 51
def aligndown(val, align = PAGE_SIZE)
  val - (val & (align - 1))
end
alignup(val, align = PAGE_SIZE) click to toggle source

@param [Integer] val @param [Integer] align @return [Integer]

Aligned result.

@example

alignup(0x1234)
#=> 8192
alignup(0x33, 0x20)
#=> 64
alignup(0x10, 0x8)
#=> 16
# File lib/patchelf/helper.rb, line 66
def alignup(val, align = PAGE_SIZE)
  (val & (align - 1)).zero? ? val : (aligndown(val, align) + align)
end
color_enabled?() click to toggle source

For {#colorize} to decide if need add color codes. @return [Boolean]

# File lib/patchelf/helper.rb, line 36
def color_enabled?
  $stderr.tty?
end
colorize(str, type) click to toggle source

For wrapping string with color codes for prettier inspect. @param [String] str

Content to colorize.

@param [Symbol] type

Specify which kind of color to use, valid symbols are defined in {.COLOR_CODE}.

@return [String]

String that wrapped with color codes.
# File lib/patchelf/helper.rb, line 26
def colorize(str, type)
  return str unless color_enabled?

  cc = COLOR_CODE
  color = cc.key?(type) ? cc[type] : ''
  "#{color}#{str.sub(COLOR_CODE[:esc_m], color)}#{cc[:esc_m]}"
end