Class: String

Inherits:
Object show all
Defined in:
lib/ragweed/utils.rb

Instance Method Summary collapse

Instance Method Details

#asciizObject

Sometimes string buffers passed through Win32 interfaces come with garbage after the trailing NUL; this method gets rid of that, like String#trim



109
110
111
112
113
114
115
# File 'lib/ragweed/utils.rb', line 109

def asciiz
  begin
    self[0..self.index("\x00")-1]
  rescue
    self
  end
end

#asciiz!Object



117
# File 'lib/ragweed/utils.rb', line 117

def asciiz!; replace asciiz; end

#dehexifyObject

Convert a string of raw hex characters (no %‘s or anything) into binary



125
126
127
# File 'lib/ragweed/utils.rb', line 125

def dehexify
  [self].pack("H*")
end

#from_utf16Object Also known as: to_utf8, to_ascii



89
90
91
92
93
94
# File 'lib/ragweed/utils.rb', line 89

def from_utf16
    ret = Iconv.iconv("utf-8", "utf-16le", self).first
    if ret[-1] == 0
        ret = ret[0..-2]
    end
end

#from_utf16_bufferObject



97
98
99
# File 'lib/ragweed/utils.rb', line 97

def from_utf16_buffer
    self[0..index("\0\0\0")+2].from_utf16
end

#hexifyObject

Convert a string into hex characters



120
121
122
# File 'lib/ragweed/utils.rb', line 120

def hexify
  self.unpack("H*").first
end

#shift(count = 1) ⇒ Object



101
102
103
104
# File 'lib/ragweed/utils.rb', line 101

def shift(count=1)
  return self if count == 0
  slice! 0..(count-1)
end

#shift_b16Object



84
# File 'lib/ragweed/utils.rb', line 84

def shift_b16; shift(2).to_b16; end

#shift_b32Object



82
# File 'lib/ragweed/utils.rb', line 82

def shift_b32; shift(4).to_b32; end

#shift_l16Object



83
# File 'lib/ragweed/utils.rb', line 83

def shift_l16; shift(2).to_l16; end

#shift_l32Object



81
# File 'lib/ragweed/utils.rb', line 81

def shift_l32; shift(4).to_l32; end

#shift_u8Object



85
# File 'lib/ragweed/utils.rb', line 85

def shift_u8; shift(1).to_u8; end

#to_b16Object

to big endian 16bit short



79
# File 'lib/ragweed/utils.rb', line 79

def to_b16; unpack("n").first; end

#to_b32Object

to big endian 32bit integer



75
# File 'lib/ragweed/utils.rb', line 75

def to_b32; unpack("N").first; end

#to_l16Object

to little endian 16bit short



77
# File 'lib/ragweed/utils.rb', line 77

def to_l16; unpack("v").first; end

#to_l32Object

to little endian 32bit integer



73
# File 'lib/ragweed/utils.rb', line 73

def to_l32; unpack("L").first; end

#to_u8Object



80
# File 'lib/ragweed/utils.rb', line 80

def to_u8; self[0]; end

#to_utf16Object



86
87
88
# File 'lib/ragweed/utils.rb', line 86

def to_utf16
    Iconv.iconv("utf-16LE", "utf-8", self).first + "\x00\x00"
end