-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin2words.rb
executable file
·106 lines (96 loc) · 3.08 KB
/
bin2words.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env ruby
# bin2words.rb by Patrick Webster <patrick@aushack.com>
# git clone https://github.com/aushack/bin2words
#
# version 1.0 - initial release - 20140403
# Dependencies.
require 'getoptlong' # Should be there
begin
require 'inifile'
rescue Exception => e
puts "The 'inifile' gem is required but not installed. Try 'gem install inifile'."
exit 1
end
# Set values first.
inifile = IniFile.load('bin2words.ini') # Default key file.
wrap = true # Default is to limit line length
wraplen = 60 # Max line length
# Options
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--gen', '-g', GetoptLong::NO_ARGUMENT ],
[ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT ],
[ '--nowrap', '-n', GetoptLong::NO_ARGUMENT ],
[ '--encode', '-e', GetoptLong::REQUIRED_ARGUMENT ],
[ '--decode', '-d', GetoptLong::REQUIRED_ARGUMENT ],
)
begin
opts.each do |opt, arg|
case opt
when '--gen' # Make a blank ini file template. Perhaps generate, take a word list, or http URL for word input?
print "[bin2words]\n"
0.upto(255) do |i|
print "#{i}=\n"
end
exit 0
when '--nowrap'
wrap = false
when '--key'
if not File.exist?(arg)
puts "ERROR: specified key file #{arg} does not exist!\nHint: try \"-k bin2words.ini\" for the default key file."
exit 1
end
inifile = IniFile.load(arg)
when '--help'
puts <<-EOF
Example: #{$0} -e FILE > OUTPUT
-h, --help: Self explanatory.
-g, --gen: Generate an empty ini file for population.
-k, --key: Encode/decode key file. Otherwise default used (bin2words.ini)
-n, --nowrap: Disable line length limit (wrap). Default is #{wraplen} characters.
-e, --encode: Convert any file from binary (or text) to words.
-d, --decode: Convert a bin2words encoded file back to binary file.
FILE: Source file to encode or decode.
OUTPUT: Script output is sent to STDOUT. Use > to write to file.
EOF
when '--encode'
begin
open(arg,"rb"){|f|
len = 0
while buf = f.read(4096)
buf.each_byte { |bin|
if ((inifile["bin2words"][bin.to_s]).nil?)
print "" # If not defined within the ini file, print null
else
print inifile["bin2words"][bin.to_s] + " " # Spaces after all words is a must.
if (wrap)
if (len += inifile["bin2words"][bin.to_s].length) > wraplen # wrap at ~60 characters
len = 0
print "\n"
end
end
end
}
end
print "\n"
}
rescue => err
puts "Exception: #{err}"
end
when '--decode'
inifile = inifile["bin2words"].invert # Reverse the hash map.
begin
open(arg,"rb"){|f|
f.read.scan(/[\w']+/) do |word| # Split words by the spaces.
print inifile[word.to_s].to_i.chr
end
}
rescue => err
puts "Exception: #{err}"
end
end
end
rescue => err
err # Catch invalid getoptlong input etc.
end
# EOF