Skip to content

Instantly share code, notes, and snippets.

@clicube
Last active December 19, 2015 22:59
Show Gist options
  • Save clicube/6031642 to your computer and use it in GitHub Desktop.
Save clicube/6031642 to your computer and use it in GitHub Desktop.
binary file to ruby script file
# usage: ruby compress.rb icon.gif > myicon.rb
bit = 15
data = File.open(ARGV[0]){|f| f.read }
encoded_data = nil
offset = 0
bit_str = data.unpack("B*")[0]
(0x0000..0xffff).each do |i|
chars = bit_str.split(//).each_slice(bit).map(&:join).map{|s|[s.to_i(2)+i].pack("U")}
if chars.all?{|c|c.inspect.length==3}
encoded_data = chars.join
offset = i
$stderr.puts "length: #{chars.length}"
$stderr.puts "offset: #{"%#4x"%[offset]}"
break
end
end
if !encoded_data
$stderr.puts "offset not found"
exit 1
end
script = ""
script << "print [\'"
script << encoded_data
script << "\'.unpack('U*').map{|n|'%#{bit}b'%[n-#{offset}]}.join].pack('B*')"
$stderr.puts "script length: #{script.length}"
print script
# usage: ruby compress.rb icon.gif > myicon.rb
data = File.open(ARGV[0]){|f| f.read }
chunk_length = nil
encoded_data = nil
offset = 0
bit_str = data.unpack("B*")[0]
(12..16).to_a.reverse.each do |bit|
(0x0026..0xffff).each do |i|
chars = bit_str.split(//).each_slice(bit).map(&:join).map{|s|[s.to_i(2)+i].pack("U")}
if chars.all?{|c|c.inspect.length==3 && c != '`'}
encoded_data = chars.join
offset = i
chunk_length = bit
$stderr.puts "chunk : #{bit}"
$stderr.puts "length: #{chars.length}"
$stderr.puts "offset: #{"%#4x"%[offset]}"
break
end
end
break if offset > 0
end
if offset == 0
$stderr.puts "offset not found"
exit 1
end
script = ""
script << "print [\'"
script << encoded_data
if chunk_length == 16
script << ".unpack('U*').map{|n|n-#{offset}}.pack('n*')"
else
script << "\'.unpack('U*').map{|n|'%#{bit}b'%[n-#{offset}]}.join].pack('B*')"
end
$stderr.puts "script length: #{script.length}"
print script
puts
print ['☸吢ী᎔ٜ̔ʔʔˇ塨ᴿ⣳䦠䊋ဟ芓舔ʔʔʖቜኘʔʔᢔʔʖʕʔʡ⣥ⵤ膤Μ䗂䟚㘢滝㠔忘❒ୀठ橌叅↥儇䧔㭣焲罛塿ν♦ᠡ⏡厭伱篼වԸഥ⫨礊చⱅ悆ᙅے〈ⷕ滀䲗禥拔ˏ'.unpack('U*').map{|n|'%15b'%[n-660]}.join].pack('B*')
print '䝮䙝㦆ဥဥ%%杼僺媊句逢웪𐀤E%%F朗ĥ%Q%%ဥဥ(卍ꫵ﹅чᦙ咋爀ቿ삂䑮籇끙暠詋⟩杞읥燄몠廕╍웆䷇㍗矰䃗쁹苉呏皛ጱꛫ㬵蟰崺ꄑⲹࠁ䜧`'.unpack('U*').map{|n|n-37}.pack('n*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment