PageRenderTime 204ms CodeModel.GetById 10ms app.highlight 180ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/rex/text.rb

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 1430 lines | 958 code | 168 blank | 304 comment | 107 complexity | ce9a8e839d6546f38362db41b70b3eb4 MD5 | raw file
   1# -*- coding: binary -*-
   2require 'digest/md5'
   3require 'digest/sha1'
   4require 'stringio'
   5require 'cgi'
   6
   7begin
   8	old_verbose = $VERBOSE
   9	$VERBOSE = nil
  10	require 'iconv'
  11	require 'zlib'
  12rescue ::LoadError
  13ensure
  14	$VERBOSE = old_verbose
  15end
  16
  17module Rex
  18
  19###
  20#
  21# This class formats text in various fashions and also provides
  22# a mechanism for wrapping text at a given column.
  23#
  24###
  25module Text
  26	@@codepage_map_cache = nil
  27
  28	##
  29	#
  30	# Constants
  31	#
  32	##
  33
  34	States = ["AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DE", "FL", "GA", "HI",
  35		"IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN",
  36		"MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH",
  37		"OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA",
  38		"WI", "WV", "WY"]
  39	UpperAlpha   = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  40	LowerAlpha   = "abcdefghijklmnopqrstuvwxyz"
  41	Numerals     = "0123456789"
  42	Base32	     = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
  43	Alpha	     = UpperAlpha + LowerAlpha
  44	AlphaNumeric = Alpha + Numerals
  45	HighAscii    = [*(0x80 .. 0xff)].pack("C*")
  46	LowAscii     = [*(0x00 .. 0x1f)].pack("C*")
  47	DefaultWrap  = 60
  48	AllChars     = [*(0x00 .. 0xff)].pack("C*")
  49	Punctuation  = ( [*(0x21 .. 0x2f)] + [*(0x3a .. 0x3F)] + [*(0x5b .. 0x60)] + [*(0x7b .. 0x7e)] ).flatten.pack("C*")
  50
  51	DefaultPatternSets = [ Rex::Text::UpperAlpha, Rex::Text::LowerAlpha, Rex::Text::Numerals ]
  52
  53	# In case Iconv isn't loaded
  54	Iconv_EBCDIC = ["\x00", "\x01", "\x02", "\x03", "7", "-", ".", "/", "\x16", "\x05", "%", "\v", "\f", "\r", "\x0E", "\x0F", "\x10", "\x11", "\x12", "\x13", "<", "=", "2", "&", "\x18", "\x19", "?", "'", "\x1C", "\x1D", "\x1E", "\x1F", "@", "Z", "\x7F", "{", "[", "l", "P", "}", "M", "]", "\\", "N", "k", "`", "K", "a", "\xF0", "\xF1", "\xF2", "\xF3", "\xF4", "\xF5", "\xF6", "\xF7", "\xF8", "\xF9", "z", "^", "L", "~", "n", "o", "|", "\xC1", "\xC2", "\xC3", "\xC4", "\xC5", "\xC6", "\xC7", "\xC8", "\xC9", "\xD1", "\xD2", "\xD3", "\xD4", "\xD5", "\xD6", "\xD7", "\xD8", "\xD9", "\xE2", "\xE3", "\xE4", "\xE5", "\xE6", "\xE7", "\xE8", "\xE9", nil, "\xE0", nil, nil, "m", "y", "\x81", "\x82", "\x83", "\x84", "\x85", "\x86", "\x87", "\x88", "\x89", "\x91", "\x92", "\x93", "\x94", "\x95", "\x96", "\x97", "\x98", "\x99", "\xA2", "\xA3", "\xA4", "\xA5", "\xA6", "\xA7", "\xA8", "\xA9", "\xC0", "O", "\xD0", "\xA1", "\a", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
  55	Iconv_ASCII  = ["\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\a", "\b", "\t", "\n", "\v", "\f", "\r", "\x0E", "\x0F", "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", "\x18", "\x19", "\x1A", "\e", "\x1C", "\x1D", "\x1E", "\x1F", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", nil, "\\", nil, nil, "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "\x7F", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
  56
  57	##
  58	#
  59	# Serialization
  60	#
  61	##
  62
  63	#
  64	# Converts a raw string into a ruby buffer
  65	#
  66	def self.to_ruby(str, wrap = DefaultWrap, name = "buf")
  67		return hexify(str, wrap, '"', '" +', "#{name} = \n", '"')
  68	end
  69
  70	#
  71	# Creates a ruby-style comment
  72	#
  73	def self.to_ruby_comment(str, wrap = DefaultWrap)
  74		return wordwrap(str, 0, wrap, '', '# ')
  75	end
  76
  77	#
  78	# Converts a raw string into a C buffer
  79	#
  80	def self.to_c(str, wrap = DefaultWrap, name = "buf")
  81		return hexify(str, wrap, '"', '"', "unsigned char #{name}[] = \n", '";')
  82	end
  83
  84	#
  85	# Creates a c-style comment
  86	#
  87	def self.to_c_comment(str, wrap = DefaultWrap)
  88		return "/*\n" + wordwrap(str, 0, wrap, '', ' * ') + " */\n"
  89	end
  90
  91	#
  92	# Creates a javascript-style comment
  93	#
  94	def self.to_js_comment(str, wrap = DefaultWrap)
  95		return wordwrap(str, 0, wrap, '', '// ')
  96	end
  97
  98	#
  99	# Converts a raw string into a perl buffer
 100	#
 101	def self.to_perl(str, wrap = DefaultWrap, name = "buf")
 102		return hexify(str, wrap, '"', '" .', "my $#{name} = \n", '";')
 103	end
 104
 105	#
 106	# Converts a raw string into a Bash buffer
 107	#
 108	def self.to_bash(str, wrap = DefaultWrap, name = "buf")
 109		return hexify(str, wrap, '$\'', '\'\\', "export #{name}=\\\n", '\'')
 110	end
 111
 112	#
 113	# Converts a raw string into a java byte array
 114	#
 115	def self.to_java(str, name = "shell")
 116		buff = "byte #{name}[] = new byte[]\n{\n"
 117		cnt = 0
 118		max = 0
 119		str.unpack('C*').each do |c|
 120			buff << ", " if max > 0
 121			buff << "\t" if max == 0
 122			buff << sprintf('(byte) 0x%.2x', c)
 123			max +=1
 124			cnt +=1
 125
 126			if (max > 7)
 127				buff << ",\n" if cnt != str.length
 128				max = 0
 129			end
 130		end
 131		buff << "\n};\n"
 132		return buff
 133	end
 134
 135	#
 136	# Creates a perl-style comment
 137	#
 138	def self.to_perl_comment(str, wrap = DefaultWrap)
 139		return wordwrap(str, 0, wrap, '', '# ')
 140	end
 141
 142	#
 143	# Creates a Bash-style comment
 144	#
 145	def self.to_bash_comment(str, wrap = DefaultWrap)
 146		return wordwrap(str, 0, wrap, '', '# ')
 147	end
 148
 149	#
 150	# Returns the raw string
 151	#
 152	def self.to_raw(str)
 153		return str
 154	end
 155
 156	#
 157	# Converts ISO-8859-1 to UTF-8
 158	#
 159	def self.to_utf8(str)
 160		begin
 161			Iconv.iconv("utf-8","iso-8859-1", str).join(" ")
 162		rescue
 163			raise ::RuntimeError, "Your installation does not support iconv (needed for utf8 conversion)"
 164		end
 165	end
 166
 167	#
 168	# Converts ASCII to EBCDIC
 169	#
 170	class IllegalSequence < ArgumentError; end
 171
 172	# A native implementation of the ASCII->EBCDIC table, used to fall back from using
 173	# Iconv
 174	def self.to_ebcdic_rex(str)
 175		new_str = []
 176		str.each_byte do |x|
 177			if Iconv_ASCII.index(x.chr)
 178				new_str << Iconv_EBCDIC[Iconv_ASCII.index(x.chr)]
 179			else
 180				raise Rex::Text::IllegalSequence, ("\\x%x" % x)
 181			end
 182		end
 183		new_str.join
 184	end
 185
 186	# A native implementation of the EBCDIC->ASCII table, used to fall back from using
 187	# Iconv
 188	def self.from_ebcdic_rex(str)
 189		new_str = []
 190		str.each_byte do |x|
 191			if Iconv_EBCDIC.index(x.chr)
 192				new_str << Iconv_ASCII[Iconv_EBCDIC.index(x.chr)]
 193			else
 194				raise Rex::Text::IllegalSequence, ("\\x%x" % x)
 195			end
 196		end
 197		new_str.join
 198	end
 199
 200	def self.to_ebcdic(str)
 201		begin
 202			Iconv.iconv("EBCDIC-US", "ASCII", str).first
 203		rescue ::Iconv::IllegalSequence => e
 204			raise e
 205		rescue
 206			self.to_ebcdic_rex(str)
 207		end
 208	end
 209
 210	#
 211	# Converts EBCIDC to ASCII
 212	#
 213	def self.from_ebcdic(str)
 214		begin
 215			Iconv.iconv("ASCII", "EBCDIC-US", str).first
 216		rescue ::Iconv::IllegalSequence => e
 217			raise e
 218		rescue
 219			self.from_ebcdic_rex(str)
 220		end
 221	end
 222
 223	#
 224	# Returns the words in +str+ as an Array.
 225	#
 226	# strict - include *only* words, no boundary characters (like spaces, etc.)
 227	#
 228	def self.to_words( str, strict = false )
 229		splits = str.split( /\b/ )
 230		splits.reject! { |w| !(w =~ /\w/) } if strict
 231		splits
 232	end
 233
 234	#
 235	# Removes noise from 2 Strings and return a refined String version.
 236	#
 237	def self.refine( str1, str2 )
 238		return str1 if str1 == str2
 239
 240		# get the words of the first str in an array
 241		s_words = to_words( str1 )
 242
 243		# get the words of the second str in an array
 244		o_words = to_words( str2 )
 245
 246		# get what hasn't changed (the rdiff, so to speak) as a string
 247		(s_words - (s_words - o_words)).join
 248	end
 249
 250	#
 251	# Returns a unicode escaped string for Javascript
 252	#
 253	def self.to_unescape(data, endian=ENDIAN_LITTLE)
 254		data << "\x41" if (data.length % 2 != 0)
 255		dptr = 0
 256		buff = ''
 257		while (dptr < data.length)
 258			c1 = data[dptr,1].unpack("C*")[0]
 259			dptr += 1
 260			c2 = data[dptr,1].unpack("C*")[0]
 261			dptr += 1
 262
 263			if (endian == ENDIAN_LITTLE)
 264				buff << sprintf('%%u%.2x%.2x', c2, c1)
 265			else
 266				buff << sprintf('%%u%.2x%.2x', c1, c2)
 267			end
 268		end
 269		return buff
 270	end
 271
 272	def self.to_octal(str, prefix = "\\")
 273		octal = ""
 274		str.each_byte { |b|
 275			octal << "#{prefix}#{b.to_s 8}"
 276		}
 277
 278		return octal
 279	end
 280
 281	#
 282	# Returns the hex version of the supplied string
 283	#
 284	def self.to_hex(str, prefix = "\\x", count = 1)
 285		raise ::RuntimeError, "unable to chunk into #{count} byte chunks" if ((str.length % count) > 0)
 286
 287		# XXX: Regexp.new is used here since using /.{#{count}}/o would compile
 288		# the regex the first time it is used and never check again.  Since we
 289		# want to know how many to capture on every instance, we do it this
 290		# way.
 291		return str.unpack('H*')[0].gsub(Regexp.new(".{#{count * 2}}", nil, 'n')) { |s| prefix + s }
 292	end
 293
 294	#
 295	# Returns the string with nonprintable hex characters sanitized to ascii. Similiar to to_hex,
 296	# but regular ASCII is not translated if count is 1.
 297	#
 298	def self.to_hex_ascii(str, prefix = "\\x", count = 1, suffix=nil)
 299		raise ::RuntimeError, "unable to chunk into #{count} byte chunks" if ((str.length % count) > 0)
 300		return str.unpack('H*')[0].gsub(Regexp.new(".{#{count * 2}}", nil, 'n')) { |s|
 301			(0x20..0x7e) === s.to_i(16) ? s.to_i(16).chr : prefix + s + suffix.to_s
 302		}
 303	end
 304
 305	#
 306	# Converts standard ASCII text to a unicode string.
 307	#
 308	# Supported unicode types include: utf-16le, utf16-be, utf32-le, utf32-be, utf-7, and utf-8
 309	#
 310	# Providing 'mode' provides hints to the actual encoder as to how it should encode the string.	Only UTF-7 and UTF-8 use "mode".
 311	#
 312	# utf-7 by default does not encode alphanumeric and a few other characters.  By specifying the mode of "all", then all of the characters are encoded, not just the non-alphanumeric set.
 313	#	to_unicode(str, 'utf-7', 'all')
 314	#
 315	# utf-8 specifies that alphanumeric characters are used directly, eg "a" is just "a".  However, there exist 6 different overlong encodings of "a" that are technically not valid, but parse just fine in most utf-8 parsers.  (0xC1A1, 0xE081A1, 0xF08081A1, 0xF8808081A1, 0xFC80808081A1, 0xFE8080808081A1).  How many bytes to use for the overlong enocding is specified providing 'size'.
 316	#	to_unicode(str, 'utf-8', 'overlong', 2)
 317	#
 318	# Many utf-8 parsers also allow invalid overlong encodings, where bits that are unused when encoding a single byte are modified.  Many parsers will ignore these bits, rendering simple string matching to be ineffective for dealing with UTF-8 strings.  There are many more invalid overlong encodings possible for "a".  For example, three encodings are available for an invalid 2 byte encoding of "a". (0xC1E1 0xC161 0xC121).	By specifying "invalid", a random invalid encoding is chosen for the given byte size.
 319	#	to_unicode(str, 'utf-8', 'invalid', 2)
 320	#
 321	# utf-7 defaults to 'normal' utf-7 encoding
 322	# utf-8 defaults to 2 byte 'normal' encoding
 323	#
 324	def self.to_unicode(str='', type = 'utf-16le', mode = '', size = '')
 325		return '' if not str
 326		case type
 327		when 'utf-16le'
 328			return str.unpack('C*').pack('v*')
 329		when 'utf-16be'
 330			return str.unpack('C*').pack('n*')
 331		when 'utf-32le'
 332			return str.unpack('C*').pack('V*')
 333		when 'utf-32be'
 334			return str.unpack('C*').pack('N*')
 335		when 'utf-7'
 336			case mode
 337			when 'all'
 338				return str.gsub(/./){ |a|
 339					out = ''
 340					if 'a' != '+'
 341						out = encode_base64(to_unicode(a, 'utf-16be')).gsub(/[=\r\n]/, '')
 342					end
 343					'+' + out + '-'
 344				}
 345			else
 346				return str.gsub(/[^\n\r\t\ A-Za-z0-9\'\(\),-.\/\:\?]/){ |a|
 347					out = ''
 348					if a != '+'
 349						out = encode_base64(to_unicode(a, 'utf-16be')).gsub(/[=\r\n]/, '')
 350					end
 351					'+' + out + '-'
 352				}
 353			end
 354		when 'utf-8'
 355			if size == ''
 356				size = 2
 357			end
 358
 359			if size >= 2 and size <= 7
 360				string = ''
 361				str.each_byte { |a|
 362					if (a < 21 || a > 0x7f) || mode != ''
 363						# ugh.	turn a single byte into the binary representation of it, in array form
 364						bin = [a].pack('C').unpack('B8')[0].split(//)
 365
 366						# even more ugh.
 367						bin.collect!{|a_| a_.to_i}
 368
 369						out = Array.new(8 * size, 0)
 370
 371						0.upto(size - 1) { |i|
 372							out[i] = 1
 373							out[i * 8] = 1
 374						}
 375
 376						i = 0
 377						byte = 0
 378						bin.reverse.each { |bit|
 379							if i < 6
 380								mod = (((size * 8) - 1) - byte * 8) - i
 381								out[mod] = bit
 382							else
 383								byte = byte + 1
 384								i = 0
 385								redo
 386							end
 387							i = i + 1
 388						}
 389
 390						if mode != ''
 391							case mode
 392							when 'overlong'
 393								# do nothing, since we already handle this as above...
 394							when 'invalid'
 395								done = 0
 396								while done == 0
 397									# the ghetto...
 398									bits = [7, 8, 15, 16, 23, 24, 31, 32, 41]
 399									bits.each { |bit|
 400										bit = (size * 8) - bit
 401										if bit > 1
 402											set = rand(2)
 403											if out[bit] != set
 404												out[bit] = set
 405												done = 1
 406											end
 407										end
 408									}
 409								end
 410							else
 411								raise TypeError, 'Invalid mode.  Only "overlong" and "invalid" are acceptable modes for utf-8'
 412							end
 413						end
 414						string << [out.join('')].pack('B*')
 415					else
 416						string << [a].pack('C')
 417					end
 418				}
 419				return string
 420			else
 421				raise TypeError, 'invalid utf-8 size'
 422			end
 423		when 'uhwtfms' # suggested name from HD :P
 424			load_codepage()
 425
 426			string = ''
 427			# overloading mode as codepage
 428			if mode == ''
 429				mode = 1252 # ANSI - Latan 1, default for US installs of MS products
 430			else
 431				mode = mode.to_i
 432			end
 433			if @@codepage_map_cache[mode].nil?
 434				raise TypeError, "Invalid codepage #{mode}"
 435			end
 436			str.each_byte {|byte|
 437				char = [byte].pack('C*')
 438				possible = @@codepage_map_cache[mode]['data'][char]
 439				if possible.nil?
 440					raise TypeError, "codepage #{mode} does not provide an encoding for 0x#{char.unpack('H*')[0]}"
 441				end
 442				string << possible[ rand(possible.length) ]
 443			}
 444			return string
 445		when 'uhwtfms-half' # suggested name from HD :P
 446			load_codepage()
 447			string = ''
 448			# overloading mode as codepage
 449			if mode == ''
 450				mode = 1252 # ANSI - Latan 1, default for US installs of MS products
 451			else
 452				mode = mode.to_i
 453			end
 454			if mode != 1252
 455				raise TypeError, "Invalid codepage #{mode}, only 1252 supported for uhwtfms_half"
 456			end
 457			str.each_byte {|byte|
 458				if ((byte >= 33 && byte <= 63) || (byte >= 96 && byte <= 126))
 459					string << "\xFF" + [byte ^ 32].pack('C')
 460				elsif (byte >= 64 && byte <= 95)
 461					string << "\xFF" + [byte ^ 96].pack('C')
 462				else
 463					char = [byte].pack('C')
 464					possible = @@codepage_map_cache[mode]['data'][char]
 465					if possible.nil?
 466						raise TypeError, "codepage #{mode} does not provide an encoding for 0x#{char.unpack('H*')[0]}"
 467					end
 468					string << possible[ rand(possible.length) ]
 469				end
 470			}
 471			return string
 472		else
 473			raise TypeError, 'invalid utf type'
 474		end
 475	end
 476
 477	#
 478	# Converts a unicode string to standard ASCII text.
 479	#
 480	def self.to_ascii(str='', type = 'utf-16le', mode = '', size = '')
 481		return '' if not str
 482		case type
 483		when 'utf-16le'
 484			return str.unpack('v*').pack('C*')
 485		when 'utf-16be'
 486			return str.unpack('n*').pack('C*')
 487		when 'utf-32le'
 488			return str.unpack('V*').pack('C*')
 489		when 'utf-32be'
 490			return str.unpack('N*').pack('C*')
 491		when 'utf-7'
 492			raise TypeError, 'invalid utf type, not yet implemented'
 493		when 'utf-8'
 494			raise TypeError, 'invalid utf type, not yet implemented'
 495		when 'uhwtfms' # suggested name from HD :P
 496			raise TypeError, 'invalid utf type, not yet implemented'
 497		when 'uhwtfms-half' # suggested name from HD :P
 498			raise TypeError, 'invalid utf type, not yet implemented'
 499		else
 500			raise TypeError, 'invalid utf type'
 501		end
 502	end
 503
 504	#
 505	# Encode a string in a manor useful for HTTP URIs and URI Parameters.
 506	#
 507	def self.uri_encode(str, mode = 'hex-normal')
 508		return "" if str == nil
 509
 510		return str if mode == 'none' # fast track no encoding
 511
 512		all = /[^\/\\]+/
 513		normal = /[^a-zA-Z0-9\/\\\.\-]+/
 514		normal_na = /[a-zA-Z0-9\/\\\.\-]/
 515
 516		case mode
 517		when 'hex-normal'
 518			return str.gsub(normal) { |s| Rex::Text.to_hex(s, '%') }
 519		when 'hex-all'
 520			return str.gsub(all) { |s| Rex::Text.to_hex(s, '%') }
 521		when 'hex-random'
 522			res = ''
 523			str.each_byte do |c|
 524				b = c.chr
 525				res << ((rand(2) == 0) ?
 526					b.gsub(all)   { |s| Rex::Text.to_hex(s, '%') } :
 527					b.gsub(normal){ |s| Rex::Text.to_hex(s, '%') } )
 528			end
 529			return res
 530		when 'u-normal'
 531			return str.gsub(normal) { |s| Rex::Text.to_hex(Rex::Text.to_unicode(s, 'uhwtfms'), '%u', 2) }
 532		when 'u-all'
 533			return str.gsub(all) { |s| Rex::Text.to_hex(Rex::Text.to_unicode(s, 'uhwtfms'), '%u', 2) }
 534		when 'u-random'
 535			res = ''
 536			str.each_byte do |c|
 537				b = c.chr
 538				res << ((rand(2) == 0) ?
 539					b.gsub(all)   { |s| Rex::Text.to_hex(Rex::Text.to_unicode(s, 'uhwtfms'), '%u', 2) } :
 540					b.gsub(normal){ |s| Rex::Text.to_hex(Rex::Text.to_unicode(s, 'uhwtfms'), '%u', 2) } )
 541			end
 542			return res
 543		when 'u-half'
 544			return str.gsub(all) { |s| Rex::Text.to_hex(Rex::Text.to_unicode(s, 'uhwtfms-half'), '%u', 2) }
 545		else
 546			raise TypeError, 'invalid mode'
 547		end
 548	end
 549
 550	#
 551	# Encode a string in a manner useful for HTTP URIs and URI Parameters.
 552	#
 553	def self.html_encode(str, mode = 'hex')
 554		case mode
 555		when 'hex'
 556			return str.unpack('C*').collect{ |i| "&#x" + ("%.2x" % i) + ";"}.join
 557		when 'int'
 558			return str.unpack('C*').collect{ |i| "&#" + i.to_s + ";"}.join
 559		when 'int-wide'
 560			return str.unpack('C*').collect{ |i| "&#" + ("0" * (7 - i.to_s.length)) + i.to_s + ";" }.join
 561		else
 562			raise TypeError, 'invalid mode'
 563		end
 564	end
 565
 566	#
 567	# Decode a string that's html encoded
 568	#
 569	def self.html_decode(str)
 570		decoded_str = CGI.unescapeHTML(str)
 571		return decoded_str
 572	end
 573
 574	#
 575	# Encode an ASCII string so it's safe for XML. It's a wrapper for to_hex_ascii.
 576	#
 577	def self.xml_char_encode(str)
 578		self.to_hex_ascii(str, "&#x", 1, ";")
 579	end
 580
 581	#
 582	# Decode a URI encoded string
 583	#
 584	def self.uri_decode(str)
 585		str.gsub(/(%[a-z0-9]{2})/i){ |c| [c[1,2]].pack("H*") }
 586	end
 587
 588	#
 589	# Converts a string to random case
 590	#
 591	def self.to_rand_case(str)
 592		buf = str.dup
 593		0.upto(str.length) do |i|
 594			buf[i,1] = rand(2) == 0 ? str[i,1].upcase : str[i,1].downcase
 595		end
 596		return buf
 597	end
 598
 599	#
 600	# Takes a string, and returns an array of all mixed case versions.
 601	#
 602	# Example:
 603	#
 604	#    >> Rex::Text.to_mixed_case_array "abc1"
 605	#    => ["abc1", "abC1", "aBc1", "aBC1", "Abc1", "AbC1", "ABc1", "ABC1"]
 606	#
 607	def self.to_mixed_case_array(str)
 608		letters = []
 609		str.scan(/./).each { |l| letters << [l.downcase, l.upcase] }
 610		coords = []
 611		(1 << str.size).times { |i| coords << ("%0#{str.size}b" % i) }
 612		mixed = []
 613		coords.each do |coord|
 614			c = coord.scan(/./).map {|x| x.to_i}
 615			this_str = ""
 616			c.each_with_index { |d,i| this_str << letters[i][d] }
 617			mixed << this_str
 618		end
 619		return mixed.uniq
 620	end
 621
 622	#
 623	# Converts a string a nicely formatted hex dump
 624	#
 625	def self.to_hex_dump(str, width=16)
 626		buf = ''
 627		idx = 0
 628		cnt = 0
 629		snl = false
 630		lst = 0
 631
 632		while (idx < str.length)
 633
 634			chunk = str[idx, width]
 635			line  = chunk.unpack("H*")[0].scan(/../).join(" ")
 636			buf << line
 637
 638			if (lst == 0)
 639				lst = line.length
 640				buf << " " * 4
 641			else
 642				buf << " " * ((lst - line.length) + 4).abs
 643			end
 644
 645			chunk.unpack("C*").each do |c|
 646				if (c >	0x1f and c < 0x7f)
 647					buf << c.chr
 648				else
 649					buf << "."
 650				end
 651			end
 652
 653			buf << "\n"
 654
 655			idx += width
 656		end
 657
 658		buf << "\n"
 659	end
 660
 661	#
 662	# Converts a string a nicely formatted and addressed ex dump
 663	#
 664	def self.to_addr_hex_dump(str, start_addr=0, width=16)
 665		buf = ''
 666		idx = 0
 667		cnt = 0
 668		snl = false
 669		lst = 0
 670		addr = start_addr
 671
 672		while (idx < str.length)
 673
 674			buf << "%08x" % addr
 675			buf << " " * 4
 676			chunk = str[idx, width]
 677			line  = chunk.unpack("H*")[0].scan(/../).join(" ")
 678			buf << line
 679
 680			if (lst == 0)
 681				lst = line.length
 682				buf << " " * 4
 683			else
 684				buf << " " * ((lst - line.length) + 4).abs
 685			end
 686
 687			chunk.unpack("C*").each do |c|
 688				if (c > 0x1f and c < 0x7f)
 689					buf << c.chr
 690				else
 691					buf << "."
 692				end
 693			end
 694
 695			buf << "\n"
 696
 697			idx += width
 698			addr += width
 699		end
 700
 701		buf << "\n"
 702	end
 703
 704	#
 705	# Converts a hex string to a raw string
 706	#
 707	def self.hex_to_raw(str)
 708		[ str.downcase.gsub(/'/,'').gsub(/\\?x([a-f0-9][a-f0-9])/, '\1') ].pack("H*")
 709	end
 710
 711	#
 712	# Turn non-printable chars into hex representations, leaving others alone
 713	#
 714	# If +whitespace+ is true, converts whitespace (0x20, 0x09, etc) to hex as
 715	# well.
 716	#
 717	def self.ascii_safe_hex(str, whitespace=false)
 718		if whitespace
 719			str.gsub(/([\x00-\x20\x80-\xFF])/){ |x| "\\x%.2x" % x.unpack("C*")[0] }
 720		else
 721			str.gsub(/([\x00-\x08\x0b\x0c\x0e-\x1f\x80-\xFF])/n){ |x| "\\x%.2x" % x.unpack("C*")[0]}
 722		end
 723	end
 724
 725	#
 726	# Wraps text at a given column using a supplied indention
 727	#
 728	def self.wordwrap(str, indent = 0, col = DefaultWrap, append = '', prepend = '')
 729		return str.gsub(/.{1,#{col - indent}}(?:\s|\Z)/){
 730			( (" " * indent) + prepend + $& + append + 5.chr).gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
 731	end
 732
 733	#
 734	# Converts a string to a hex version with wrapping support
 735	#
 736	def self.hexify(str, col = DefaultWrap, line_start = '', line_end = '', buf_start = '', buf_end = '')
 737		output	 = buf_start
 738		cur	 = 0
 739		count	 = 0
 740		new_line = true
 741
 742		# Go through each byte in the string
 743		str.each_byte { |byte|
 744			count  += 1
 745			append	= ''
 746
 747			# If this is a new line, prepend with the
 748			# line start text
 749			if (new_line == true)
 750				append	 << line_start
 751				new_line  = false
 752			end
 753
 754			# Append the hexified version of the byte
 755			append << sprintf("\\x%.2x", byte)
 756			cur    += append.length
 757
 758			# If we're about to hit the column or have gone past it,
 759			# time to finish up this line
 760			if ((cur + line_end.length >= col) or (cur + buf_end.length  >= col))
 761				new_line  = true
 762				cur	  = 0
 763
 764				# If this is the last byte, use the buf_end instead of
 765				# line_end
 766				if (count == str.length)
 767					append << buf_end + "\n"
 768				else
 769					append << line_end + "\n"
 770				end
 771			end
 772
 773			output << append
 774		}
 775
 776		# If we were in the middle of a line, finish the buffer at this point
 777		if (new_line == false)
 778			output << buf_end + "\n"
 779		end
 780
 781		return output
 782	end
 783
 784	##
 785	#
 786	# Transforms
 787	#
 788	##
 789
 790	#
 791	# Base32 code
 792	#
 793
 794	# Based on --> https://github.com/stesla/base32
 795
 796	# Copyright (c) 2007-2011 Samuel Tesla
 797
 798	# Permission is hereby granted, free of charge, to any person obtaining a copy
 799	# of this software and associated documentation files (the "Software"), to deal
 800	# in the Software without restriction, including without limitation the rights
 801	# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 802	# copies of the Software, and to permit persons to whom the Software is
 803	# furnished to do so, subject to the following conditions:
 804
 805	# The above copyright notice and this permission notice shall be included in
 806	# all copies or substantial portions of the Software.
 807
 808	# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 809	# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 810	# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 811	# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 812	# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 813	# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 814	# THE SOFTWARE.
 815
 816
 817	#
 818	# Base32 encoder
 819	#
 820	def self.b32encode(bytes_in)
 821		n = (bytes_in.length * 8.0 / 5.0).ceil
 822		p = n < 8 ? 5 - (bytes_in.length * 8) % 5 : 0
 823		c = bytes_in.inject(0) {|m,o| (m << 8) + o} << p
 824		[(0..n-1).to_a.reverse.collect {|i| Base32[(c >> i * 5) & 0x1f].chr},
 825		("=" * (8-n))]
 826	end
 827
 828	def self.encode_base32(str)
 829		bytes = str.bytes
 830		result = ''
 831		size= 5
 832		while bytes.any? do
 833			bytes.each_slice(size) do |a|
 834			bytes_out = b32encode(a).flatten.join
 835			result << bytes_out
 836			bytes = bytes.drop(size)
 837			end
 838		end
 839		return result
 840	end
 841
 842	#
 843	# Base32 decoder
 844	#
 845	def self.b32decode(bytes_in)
 846		bytes = bytes_in.take_while {|c| c != 61} # strip padding
 847		n = (bytes.length * 5.0 / 8.0).floor
 848		p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0
 849		c = bytes.inject(0) {|m,o| (m << 5) + Base32.index(o.chr)} >> p
 850		(0..n-1).to_a.reverse.collect {|i| ((c >> i * 8) & 0xff).chr}
 851	end
 852
 853	def self.decode_base32(str)
 854		bytes = str.bytes
 855		result = ''
 856		size= 8
 857		while bytes.any? do
 858			bytes.each_slice(size) do |a|
 859			bytes_out = b32decode(a).flatten.join
 860			result << bytes_out
 861			bytes = bytes.drop(size)
 862			end
 863		end
 864		return result
 865	end
 866
 867	#
 868	# Base64 encoder
 869	#
 870	def self.encode_base64(str, delim='')
 871		[str.to_s].pack("m").gsub(/\s+/, delim)
 872	end
 873
 874	#
 875	# Base64 decoder
 876	#
 877	def self.decode_base64(str)
 878		str.to_s.unpack("m")[0]
 879	end
 880
 881	#
 882	# Raw MD5 digest of the supplied string
 883	#
 884	def self.md5_raw(str)
 885		Digest::MD5.digest(str)
 886	end
 887
 888	#
 889	# Hexidecimal MD5 digest of the supplied string
 890	#
 891	def self.md5(str)
 892		Digest::MD5.hexdigest(str)
 893	end
 894
 895	#
 896	# Raw SHA1 digest of the supplied string
 897	#
 898	def self.sha1_raw(str)
 899		Digest::SHA1.digest(str)
 900	end
 901
 902	#
 903	# Hexidecimal SHA1 digest of the supplied string
 904	#
 905	def self.sha1(str)
 906		Digest::SHA1.hexdigest(str)
 907	end
 908
 909	#
 910	# Convert hex-encoded characters to literals.
 911	# Example: "AA\\x42CC" becomes "AABCC"
 912	#
 913	def self.dehex(str)
 914		return str unless str.respond_to? :match
 915		return str unless str.respond_to? :gsub
 916		regex = /\x5cx[0-9a-f]{2}/mi
 917		if str.match(regex)
 918			str.gsub(regex) { |x| x[2,2].to_i(16).chr }
 919		else
 920			str
 921		end
 922	end
 923
 924	#
 925	# Convert and replace hex-encoded characters to literals.
 926	#
 927	def self.dehex!(str)
 928		return str unless str.respond_to? :match
 929		return str unless str.respond_to? :gsub
 930		regex = /\x5cx[0-9a-f]{2}/mi
 931		str.gsub!(regex) { |x| x[2,2].to_i(16).chr }
 932	end
 933
 934	##
 935	#
 936	# Generators
 937	#
 938	##
 939
 940
 941	# Generates a random character.
 942	def self.rand_char(bad, chars = AllChars)
 943		rand_text(1, bad, chars)
 944	end
 945
 946	# Base text generator method
 947	def self.rand_base(len, bad, *foo)
 948		cset = (foo.join.unpack("C*") - bad.to_s.unpack("C*")).uniq
 949		return "" if cset.length == 0
 950		outp = []
 951		len.times { outp << cset[rand(cset.length)] }
 952		outp.pack("C*")
 953	end
 954
 955	# Generate random bytes of data
 956	def self.rand_text(len, bad='', chars = AllChars)
 957		foo = chars.split('')
 958		rand_base(len, bad, *foo)
 959	end
 960
 961	# Generate random bytes of alpha data
 962	def self.rand_text_alpha(len, bad='')
 963		foo = []
 964		foo += ('A' .. 'Z').to_a
 965		foo += ('a' .. 'z').to_a
 966		rand_base(len, bad, *foo )
 967	end
 968
 969	# Generate random bytes of lowercase alpha data
 970	def self.rand_text_alpha_lower(len, bad='')
 971		rand_base(len, bad, *('a' .. 'z').to_a)
 972	end
 973
 974	# Generate random bytes of uppercase alpha data
 975	def self.rand_text_alpha_upper(len, bad='')
 976		rand_base(len, bad, *('A' .. 'Z').to_a)
 977	end
 978
 979	# Generate random bytes of alphanumeric data
 980	def self.rand_text_alphanumeric(len, bad='')
 981		foo = []
 982		foo += ('A' .. 'Z').to_a
 983		foo += ('a' .. 'z').to_a
 984		foo += ('0' .. '9').to_a
 985		rand_base(len, bad, *foo )
 986	end
 987
 988	# Generate random bytes of alphanumeric hex.
 989	def self.rand_text_hex(len, bad='')
 990		foo = []
 991		foo += ('0' .. '9').to_a
 992		foo += ('a' .. 'f').to_a
 993		rand_base(len, bad, *foo)
 994	end
 995
 996	# Generate random bytes of numeric data
 997	def self.rand_text_numeric(len, bad='')
 998		foo = ('0' .. '9').to_a
 999		rand_base(len, bad, *foo )
1000	end
1001
1002	# Generate random bytes of english-like data
1003	def self.rand_text_english(len, bad='')
1004		foo = []
1005		foo += (0x21 .. 0x7e).map{ |c| c.chr }
1006		rand_base(len, bad, *foo )
1007	end
1008
1009	# Generate random bytes of high ascii data
1010	def self.rand_text_highascii(len, bad='')
1011		foo = []
1012		foo += (0x80 .. 0xff).map{ |c| c.chr }
1013		rand_base(len, bad, *foo )
1014	end
1015
1016	# Generate a random GUID, of the form {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
1017	def self.rand_guid
1018		"{#{[8,4,4,4,12].map {|a| rand_text_hex(a) }.join("-")}}"
1019	end
1020
1021	#
1022	# Creates a pattern that can be used for offset calculation purposes.  This
1023	# routine is capable of generating patterns using a supplied set and a
1024	# supplied number of identifiable characters (slots).  The supplied sets
1025	# should not contain any duplicate characters or the logic will fail.
1026	#
1027	def self.pattern_create(length, sets = nil)
1028		buf = ''
1029		idx = 0
1030		offsets = []
1031
1032		# Make sure there's something in sets even if we were given an explicit nil
1033		sets ||= [ UpperAlpha, LowerAlpha, Numerals ]
1034
1035		# Return stupid uses
1036		return "" if length.to_i < 1
1037		return sets[0][0].chr * length if sets.size == 1 and sets[0].size == 1
1038
1039		sets.length.times { offsets << 0 }
1040
1041		until buf.length >= length
1042			begin
1043				buf << converge_sets(sets, 0, offsets, length)
1044			end
1045		end
1046
1047		# Maximum permutations reached, but we need more data
1048		if (buf.length < length)
1049			buf = buf * (length / buf.length.to_f).ceil
1050		end
1051
1052		buf[0,length]
1053	end
1054
1055	# Step through an arbitrary number of sets of bytes to build up a findable pattern.
1056	# This is mostly useful for experimentially determining offset lengths into memory
1057	# structures. Note that the supplied sets should never contain duplicate bytes, or
1058	# else it can become impossible to measure the offset accurately.
1059	def self.patt2(len, sets = nil)
1060		buf = ""
1061		counter = []
1062		sets ||= [ UpperAlpha, LowerAlpha, Numerals ]
1063		len ||= len.to_i
1064		return "" if len.zero?
1065
1066		sets = sets.map {|a| a.split(//)}
1067		sets.size.times { counter << 0}
1068		0.upto(len-1) do |i|
1069			setnum = i % sets.size
1070
1071			#puts counter.inspect
1072		end
1073
1074		return buf
1075	end
1076
1077	#
1078	# Calculate the offset to a pattern
1079	#
1080	def self.pattern_offset(pattern, value, start=0)
1081		if (value.kind_of?(String))
1082			pattern.index(value, start)
1083		elsif (value.kind_of?(Fixnum) or value.kind_of?(Bignum))
1084			pattern.index([ value ].pack('V'), start)
1085		else
1086			raise ::ArgumentError, "Invalid class for value: #{value.class}"
1087		end
1088	end
1089
1090	#
1091	# Compresses a string, eliminating all superfluous whitespace before and
1092	# after lines and eliminating all lines.
1093	#
1094	def self.compress(str)
1095		str.gsub(/\n/m, ' ').gsub(/\s+/, ' ').gsub(/^\s+/, '').gsub(/\s+$/, '')
1096	end
1097
1098	#
1099	# Randomize the whitespace in a string
1100	#
1101	def self.randomize_space(str)
1102		str.gsub(/\s+/) { |s|
1103			len = rand(50)+2
1104			set = "\x09\x20\x0d\x0a"
1105			buf = ''
1106			while (buf.length < len)
1107				buf << set[rand(set.length),1]
1108			end
1109
1110			buf
1111		}
1112	end
1113
1114	# Returns true if zlib can be used.
1115	def self.zlib_present?
1116		begin
1117			temp = Zlib
1118			return true
1119		rescue
1120			return false
1121		end
1122	end
1123
1124	# backwards compat for just a bit...
1125	def self.gzip_present?
1126		self.zlib_present?
1127	end
1128
1129	#
1130	# Compresses a string using zlib
1131	#
1132	def self.zlib_deflate(str, level = Zlib::BEST_COMPRESSION)
1133		if self.zlib_present?
1134			z = Zlib::Deflate.new(level)
1135			dst = z.deflate(str, Zlib::FINISH)
1136			z.close
1137			return dst
1138		else
1139			raise RuntimeError, "Gzip support is not present."
1140		end
1141	end
1142
1143	#
1144	# Uncompresses a string using zlib
1145	#
1146	def self.zlib_inflate(str)
1147		if(self.zlib_present?)
1148			zstream = Zlib::Inflate.new
1149			buf = zstream.inflate(str)
1150			zstream.finish
1151			zstream.close
1152			return buf
1153		else
1154			raise RuntimeError, "Gzip support is not present."
1155		end
1156	end
1157
1158	#
1159	# Compresses a string using gzip
1160	#
1161	def self.gzip(str, level = 9)
1162		raise RuntimeError, "Gzip support is not present." if (!zlib_present?)
1163		raise RuntimeError, "Invalid gzip compression level" if (level < 1 or level > 9)
1164
1165		s = ""
1166		s.force_encoding('ASCII-8BIT') if s.respond_to?(:encoding)
1167		gz = Zlib::GzipWriter.new(StringIO.new(s, 'wb'), level)
1168		gz << str
1169		gz.close
1170		return s
1171	end
1172
1173	#
1174	# Uncompresses a string using gzip
1175	#
1176	def self.ungzip(str)
1177		raise RuntimeError, "Gzip support is not present." if (!zlib_present?)
1178
1179		s = ""
1180		s.force_encoding('ASCII-8BIT') if s.respond_to?(:encoding)
1181		gz = Zlib::GzipReader.new(StringIO.new(str, 'rb'))
1182		s << gz.read
1183		gz.close
1184		return s
1185	end
1186
1187	#
1188	# Return the index of the first badchar in data, otherwise return
1189	# nil if there wasn't any badchar occurences.
1190	#
1191	def self.badchar_index(data, badchars = '')
1192		badchars.unpack("C*").each { |badchar|
1193			pos = data.index(badchar.chr)
1194			return pos if pos
1195		}
1196		return nil
1197	end
1198
1199	#
1200	# This method removes bad characters from a string.
1201	#
1202	def self.remove_badchars(data, badchars = '')
1203		data.delete(badchars)
1204	end
1205
1206	#
1207	# This method returns all chars but the supplied set
1208	#
1209	def self.charset_exclude(keepers)
1210		[*(0..255)].pack('C*').delete(keepers)
1211	end
1212
1213	#
1214	#  Shuffles a byte stream
1215	#
1216	def self.shuffle_s(str)
1217		shuffle_a(str.unpack("C*")).pack("C*")
1218	end
1219
1220	#
1221	# Performs a Fisher-Yates shuffle on an array
1222	#
1223	def self.shuffle_a(arr)
1224		len = arr.length
1225		max = len - 1
1226		cyc = [* (0..max) ]
1227		for d in cyc
1228			e = rand(d+1)
1229			next if e == d
1230			f = arr[d];
1231			g = arr[e];
1232			arr[d] = g;
1233			arr[e] = f;
1234		end
1235		return arr
1236	end
1237
1238	# Permute the case of a word
1239	def self.permute_case(word, idx=0)
1240		res = []
1241
1242		if( (UpperAlpha+LowerAlpha).index(word[idx,1]))
1243
1244			word_ucase = word.dup
1245			word_ucase[idx, 1] = word[idx, 1].upcase
1246
1247			word_lcase = word.dup
1248			word_lcase[idx, 1] = word[idx, 1].downcase
1249
1250			if (idx == word.length)
1251				return [word]
1252			else
1253				res << permute_case(word_ucase, idx+1)
1254				res << permute_case(word_lcase, idx+1)
1255			end
1256		else
1257			res << permute_case(word, idx+1)
1258		end
1259
1260		res.flatten
1261	end
1262
1263	# Generate a random hostname
1264	def self.rand_hostname
1265		host = []
1266		(rand(5) + 1).times {
1267			host.push(Rex::Text.rand_text_alphanumeric(rand(10) + 1))
1268		}
1269		d = ['com', 'net', 'org', 'gov']
1270		host.push(d[rand(d.size)])
1271		host.join('.').downcase
1272	end
1273
1274	# Generate a state
1275	def self.rand_state()
1276		States[rand(States.size)]
1277	end
1278
1279
1280	#
1281	# Calculate the ROR13 hash of a given string
1282	#
1283	def self.ror13_hash(name)
1284		hash = 0
1285		name.unpack("C*").each {|c| hash = ror(hash, 13); hash += c }
1286		hash
1287	end
1288
1289	#
1290	# Rotate a 32-bit value to the right by cnt bits
1291	#
1292	def self.ror(val, cnt)
1293		bits = [val].pack("N").unpack("B32")[0].split(//)
1294		1.upto(cnt) do |c|
1295			bits.unshift( bits.pop )
1296		end
1297		[bits.join].pack("B32").unpack("N")[0]
1298	end
1299
1300	#
1301	# Rotate a 32-bit value to the left by cnt bits
1302	#
1303	def self.rol(val, cnt)
1304		bits = [val].pack("N").unpack("B32")[0].split(//)
1305		1.upto(cnt) do |c|
1306			bits.push( bits.shift )
1307		end
1308		[bits.join].pack("B32").unpack("N")[0]
1309	end
1310
1311	#
1312	# Split a string by n charachter into an array
1313	#
1314	def self.split_to_a(str, n)
1315		if n > 0
1316			s = str.dup
1317			until s.empty?
1318				(ret ||= []).push s.slice!(0, n)
1319			end
1320		else
1321			ret = str
1322		end
1323		ret
1324	end
1325
1326	#
1327	#Pack a value as 64 bit litle endian; does not exist for Array.pack
1328	#
1329	def self.pack_int64le(val)
1330		[val & 0x00000000ffffffff, val >> 32].pack("V2")
1331	end
1332
1333
1334	#
1335	# A custom unicode filter for dealing with multi-byte strings on a 8-bit console
1336	# Punycode would have been more "standard", but it requires valid Unicode chars
1337	#
1338	def self.unicode_filter_encode(str)
1339		if (str.to_s.unpack("C*") & ( LowAscii + HighAscii + "\x7f" ).unpack("C*")).length > 0
1340			str = "$U$" + str.unpack("C*").select{|c| c < 0x7f and c > 0x1f and c != 0x2d}.pack("C*") + "-0x" + str.unpack("H*")[0]
1341		else
1342			str
1343		end
1344	end
1345
1346	def self.unicode_filter_decode(str)
1347		str.to_s.gsub( /\$U\$([\x20-\x2c\x2e-\x7E]*)\-0x([A-Fa-f0-9]+)/ ){|m| [$2].pack("H*") }
1348	end
1349
1350protected
1351
1352	def self.converge_sets(sets, idx, offsets, length) # :nodoc:
1353		buf = sets[idx][offsets[idx]].chr
1354
1355		# If there are more sets after use, converage with them.
1356		if (sets[idx + 1])
1357			buf << converge_sets(sets, idx + 1, offsets, length)
1358		else
1359			# Increment the current set offset as well as previous ones if we
1360			# wrap back to zero.
1361			while (idx >= 0 and ((offsets[idx] = (offsets[idx] + 1) % sets[idx].length)) == 0)
1362				idx -= 1
1363			end
1364
1365			# If we reached the point where the idx fell below zero, then that
1366			# means we've reached the maximum threshold for permutations.
1367			if (idx < 0)
1368				return buf
1369			end
1370
1371		end
1372
1373		buf
1374	end
1375
1376	def self.load_codepage()
1377		return if (!@@codepage_map_cache.nil?)
1378		file = File.join(File.dirname(__FILE__),'codepage.map')
1379		page = ''
1380		name = ''
1381		map = {}
1382		File.open(file).each { |line|
1383			next if line =~ /^#/
1384			next if line =~ /^\s*$/
1385			data = line.split
1386			if data[1] =~ /^\(/
1387				page = data.shift.to_i
1388				name = data.join(' ').sub(/^\(/,'').sub(/\)$/,'')
1389				map[page] = {}
1390				map[page]['name'] = name
1391				map[page]['data'] = {}
1392			else
1393				data.each { |entry|
1394					wide, char = entry.split(':')
1395					char = [char].pack('H*')
1396					wide = [wide].pack('H*')
1397					if map[page]['data'][char].nil?
1398						map[page]['data'][char] = [wide]
1399					else
1400						map[page]['data'][char].push(wide)
1401					end
1402				}
1403			end
1404		}
1405		@@codepage_map_cache = map
1406	end
1407
1408	def self.checksum8(str)
1409		str.unpack("C*").inject(:+) % 0x100
1410	end
1411
1412	def self.checksum16_le(str)
1413		str.unpack("v*").inject(:+) % 0x10000
1414	end
1415
1416	def self.checksum16_be(str)
1417		str.unpack("n*").inject(:+) % 0x10000
1418	end
1419
1420	def self.checksum32_le(str)
1421		str.unpack("V*").inject(:+) % 0x100000000
1422	end
1423
1424	def self.checksum32_be(str)
1425		str.unpack("N*").inject(:+) % 0x100000000
1426	end
1427
1428end
1429end
1430