lib/rex/text.rb in librex-0.0.6 vs lib/rex/text.rb in librex-0.0.7
- old
+ new
@@ -235,11 +235,11 @@
# Returns the string with nonprintable hex characters sanitized to ascii. Similiar to to_hex,
# but regular ASCII is not translated if count is 1.
#
def self.to_hex_ascii(str, prefix = "\\x", count = 1, suffix=nil)
raise ::RuntimeError, "unable to chunk into #{count} byte chunks" if ((str.length % count) > 0)
- return str.unpack('H*')[0].gsub(Regexp.new(".{#{count * 2}}", nil, 'n')) { |s|
+ return str.unpack('H*')[0].gsub(Regexp.new(".{#{count * 2}}", nil, 'n')) { |s|
(0x20..0x7e) === s.to_i(16) ? s.to_i(16).chr : prefix + s + suffix.to_s
}
end
#
@@ -413,10 +413,37 @@
raise TypeError, 'invalid utf type'
end
end
#
+ # Converts a unicode string to standard ASCII text.
+ #
+ def self.to_ascii(str='', type = 'utf-16le', mode = '', size = '')
+ return '' if not str
+ case type
+ when 'utf-16le'
+ return str.unpack('v*').pack('C*')
+ when 'utf-16be'
+ return str.unpack('n*').pack('C*')
+ when 'utf-32le'
+ return str.unpack('V*').pack('C*')
+ when 'utf-32be'
+ return str.unpack('N*').pack('C*')
+ when 'utf-7'
+ raise TypeError, 'invalid utf type, not yet implemented'
+ when 'utf-8'
+ raise TypeError, 'invalid utf type, not yet implemented'
+ when 'uhwtfms' # suggested name from HD :P
+ raise TypeError, 'invalid utf type, not yet implemented'
+ when 'uhwtfms-half' # suggested name from HD :P
+ raise TypeError, 'invalid utf type, not yet implemented'
+ else
+ raise TypeError, 'invalid utf type'
+ end
+ end
+
+ #
# Encode a string in a manor useful for HTTP URIs and URI Parameters.
#
def self.uri_encode(str, mode = 'hex-normal')
return "" if str == nil
@@ -793,11 +820,11 @@
sets = sets.map {|a| a.split(//)}
sets.size.times { counter << 0}
0.upto(len-1) do |i|
setnum = i % sets.size
-
+
puts counter.inspect
end
return buf
end
@@ -889,11 +916,12 @@
def self.gzip(str, level = 9)
raise RuntimeError, "Gzip support is not present." if (!zlib_present?)
raise RuntimeError, "Invalid gzip compression level" if (level < 1 or level > 9)
s = ""
- gz = Zlib::GzipWriter.new(StringIO.new(s), level)
+ s.force_encoding('ASCII-8BIT') if s.respond_to?(:encoding)
+ gz = Zlib::GzipWriter.new(StringIO.new(s, 'wb'), level)
gz << str
gz.close
return s
end
@@ -902,11 +930,12 @@
#
def self.ungzip(str)
raise RuntimeError, "Gzip support is not present." if (!zlib_present?)
s = ""
- gz = Zlib::GzipReader.new(StringIO.new(str))
+ s.force_encoding('ASCII-8BIT') if s.respond_to?(:encoding)
+ gz = Zlib::GzipReader.new(StringIO.new(str, 'rb'))
s << gz.read
gz.close
return s
end
@@ -1032,9 +1061,31 @@
bits.push( bits.shift )
end
[bits.join].pack("B32").unpack("N")[0]
end
+ #
+ # Split a string by n charachter into an array
+ #
+ def self.split_to_a(str, n)
+ if n > 0
+ s = str.dup
+ until s.empty?
+ (ret ||= []).push s.slice!(0, n)
+ end
+ else
+ ret = str
+ end
+ ret
+ end
+
+ #
+ #Pack a value as 64 bit litle endian; does not exist for Array.pack
+ #
+ def self.pack_int64le(val)
+ [val & 0x00000000ffffffff, val >> 32].pack("V2")
+ end
+
protected
def self.converge_sets(sets, idx, offsets, length) # :nodoc:
buf = sets[idx][offsets[idx]].chr