Tuesday, February 8, 2011

Working with binary in ruby...without bit-struct etc

test = String.new

File.open('../data/sourcefile').each do |line|
#combine the strings into one long string
test = test + line
end

#get hex and binary representations of the file...
#no I am not easy on memory
testa = test.unpack('H*')
testb = test.unpack('B*')
#this is so we get the strings and can do substring stuff.
testc = testa[0]
testd = testb[0]
teste = Array.new
testf = Array.new

#in here i was testing how to set a single bit
p testd
p testd[0..7]
testd[7] = '0'
p testd
teste[0] = testc
testf[0] = testd
#and reverse the transform
testg = teste.pack('H*')
testh = testf.pack('B*')
#i tested it...oddly enough it preserves newlines and nulls
#even though my conversion was...lacking finesse
#but it's the easiest way i could find
p testg
p testh


-------------
Thinking if I wanted to add a single bit to the end of a binary string I could to a pack on a single 1 and add the strings together...Hmm,  anyway.  Using this snippet in a targeted fuzzer I am trying to put together.  Need to make some conversion classes and the like, but you get the idea.  That's all for now.

No comments:

Post a Comment