rbkb 0.7.2 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/Gemfile.lock +21 -13
  4. data/bin/b64 +2 -2
  5. data/bin/bgrep +2 -2
  6. data/bin/blit +2 -2
  7. data/bin/c +2 -2
  8. data/bin/crc32 +2 -2
  9. data/bin/d64 +2 -2
  10. data/bin/dedump +2 -2
  11. data/bin/feed +2 -2
  12. data/bin/hexify +2 -2
  13. data/bin/len +2 -2
  14. data/bin/plugsrv +66 -76
  15. data/bin/rex +1 -2
  16. data/bin/rstrings +2 -2
  17. data/bin/slice +2 -2
  18. data/bin/telson +2 -2
  19. data/bin/unhexify +2 -2
  20. data/bin/urldec +2 -2
  21. data/bin/urlenc +2 -2
  22. data/bin/xor +2 -2
  23. data/lib/rbkb/cli/b64.rb +6 -7
  24. data/lib/rbkb/cli/bgrep.rb +16 -17
  25. data/lib/rbkb/cli/blit.rb +29 -31
  26. data/lib/rbkb/cli/chars.rb +1 -2
  27. data/lib/rbkb/cli/crc32.rb +8 -10
  28. data/lib/rbkb/cli/d64.rb +3 -4
  29. data/lib/rbkb/cli/dedump.rb +15 -18
  30. data/lib/rbkb/cli/feed.rb +58 -64
  31. data/lib/rbkb/cli/hexify.rb +15 -16
  32. data/lib/rbkb/cli/len.rb +19 -27
  33. data/lib/rbkb/cli/rstrings.rb +41 -44
  34. data/lib/rbkb/cli/slice.rb +8 -15
  35. data/lib/rbkb/cli/telson.rb +33 -37
  36. data/lib/rbkb/cli/unhexify.rb +10 -11
  37. data/lib/rbkb/cli/urldec.rb +8 -9
  38. data/lib/rbkb/cli/urlenc.rb +8 -8
  39. data/lib/rbkb/cli/xor.rb +13 -14
  40. data/lib/rbkb/cli.rb +52 -65
  41. data/lib/rbkb/extends/array.rb +7 -8
  42. data/lib/rbkb/extends/common.rb +3 -5
  43. data/lib/rbkb/extends/enumerable.rb +9 -9
  44. data/lib/rbkb/extends/float.rb +1 -2
  45. data/lib/rbkb/extends/numeric.rb +29 -30
  46. data/lib/rbkb/extends/object.rb +1 -2
  47. data/lib/rbkb/extends/string.rb +172 -189
  48. data/lib/rbkb/extends/symbol.rb +2 -4
  49. data/lib/rbkb/extends.rb +0 -1
  50. data/lib/rbkb/plug/blit.rb +66 -69
  51. data/lib/rbkb/plug/cli.rb +22 -25
  52. data/lib/rbkb/plug/feed_import.rb +17 -21
  53. data/lib/rbkb/plug/peer.rb +11 -13
  54. data/lib/rbkb/plug/plug.rb +83 -91
  55. data/lib/rbkb/plug/proxy.rb +4 -8
  56. data/lib/rbkb/plug/unix_domain.rb +12 -14
  57. data/lib/rbkb/plug.rb +5 -6
  58. data/lib/rbkb/version.rb +1 -1
  59. data/lib/rbkb.rb +2 -3
  60. data/rbkb.gemspec +3 -3
  61. data/test/disabled_test_cli_blit.rb +1 -2
  62. data/test/disabled_test_cli_feed.rb +1 -2
  63. data/test/disabled_test_cli_telson.rb +1 -2
  64. data/test/test_cli_crc32.rb +1 -1
  65. data/test/test_cli_dedump.rb +83 -79
  66. data/test/test_cli_len.rb +25 -24
  67. data/test/test_cli_slice.rb +22 -22
  68. data/test/test_cli_xor.rb +4 -4
  69. metadata +18 -23
data/lib/rbkb/cli.rb CHANGED
@@ -2,16 +2,14 @@ require 'rbkb'
2
2
  require 'rbkb/extends'
3
3
  require 'optparse'
4
4
 
5
- # Copyright 2009 emonti at matasano.com
5
+ # Copyright 2009 emonti at matasano.com
6
6
  # See README.rdoc for license information
7
7
  #
8
8
  module Rbkb::Cli
9
-
10
9
  # Rbkb::Cli::Executable is an abstract class for creating command line
11
10
  # executables using the Ruby Black Bag framework.
12
11
  class Executable
13
-
14
- def self.run(param={})
12
+ def self.run(param = {})
15
13
  new(param).go
16
14
  end
17
15
 
@@ -26,42 +24,39 @@ module Rbkb::Cli
26
24
  # :argv - An array of cli arguments (default ARGV)
27
25
  # :opts - executable/function options for use when running 'go'
28
26
  # :stdout, - IO redirection (mostly for unit tests)
29
- # :stderr,
27
+ # :stderr,
30
28
  # :stdin
31
29
  #
32
30
  #
33
31
  # The above keys are deleted from the 'param' hash and stored as instance
34
32
  # variables with attr_accessors. All other parameters are ignored.
35
- def initialize(param={})
33
+ def initialize(param = {})
36
34
  @argv ||= param.delete(:argv) || ARGV
37
35
  @stdout ||= param.delete(:stdout) || STDOUT
38
36
  @stderr ||= param.delete(:stderr) || STDERR
39
37
  @stdin ||= param.delete(:stdin) || STDIN
40
38
  @opts ||= param.delete(:opts) || {}
41
- @parser_got_range=nil
39
+ @parser_got_range = nil
42
40
  yield self if block_given?
43
- make_parser()
41
+ make_parser
44
42
  end
45
43
 
46
-
47
44
  # Wrapper for Kernel.exit() so we can unit test cli tools
48
45
  def exit(ret)
49
46
  @exit_status = ret
50
47
  if defined? Rbkb::Cli::TESTING
51
- throw(((ret==0)? :exit_zero : :exit_err), ret)
48
+ throw((ret == 0 ? :exit_zero : :exit_err), ret)
52
49
  else
53
50
  Kernel.exit(ret)
54
51
  end
55
52
  end
56
53
 
57
-
58
54
  # This method exits with a message on stderr
59
55
  def bail(msg)
60
56
  @stderr.puts msg if msg
61
57
  self.exit(1)
62
58
  end
63
59
 
64
-
65
60
  # This method wraps a 'bail' with a basic argument error mesage and hint
66
61
  # for the '-h or --help' flag
67
62
  # The 'arg_err' parameter is a string with the erroneous arguments
@@ -69,62 +64,63 @@ module Rbkb::Cli
69
64
  bail "Error: bad arguments - #{arg_err}\n Hint: Use -h or --help"
70
65
  end
71
66
 
72
-
73
67
  # Prepares an OptionsParser object with blackbag standard options
74
68
  # This is called from within initialize() and should be overridden in
75
69
  # inherited classes to add additional OptionParser-based parsers.
76
70
  #
77
71
  # See parse for actual parsing.
78
72
  def make_parser
79
- @oparse ||= OptionParser.new
73
+ @oparse ||= OptionParser.new
80
74
  @oparse.banner = "Usage: #{File.basename $0} [options]"
81
75
 
82
- @oparse.on("-h", "--help", "Show this message") do
76
+ @oparse.on('-h', '--help', 'Show this message') do
83
77
  bail(@oparse)
84
78
  end
85
79
 
86
- @oparse.on("-v", "--version", "Show version and exit") do
80
+ @oparse.on('-v', '--version', 'Show version and exit') do
87
81
  @stdout.puts("Ruby BlackBag version #{Rbkb::VERSION}")
88
82
  self.exit(0)
89
83
  end
90
84
 
91
- return @oparse
85
+ @oparse
92
86
  end
93
87
 
94
-
95
- # Abstract argument parser. Override this method with super() from
88
+ # Abstract argument parser. Override this method with super() from
96
89
  # inherited executables. The base method just calls OptionParser.parse!
97
90
  # on the internal @oparse object.
98
91
  def parse
99
92
  # parse flag arguments
100
- @oparse.parse!(@argv) rescue(bail_args($!))
101
- @parsed=true
93
+ begin
94
+ @oparse.parse!(@argv)
95
+ rescue StandardError
96
+ (bail_args($!))
97
+ end
98
+ @parsed = true
102
99
 
103
100
  # the overriding class may implement additional arguments from here
104
101
  end
105
-
106
102
 
107
103
  # Abstract 'runner'. Override this method with super() from inherited
108
104
  # executables. The base method just slurps in an optional argv and
109
105
  # runs 'parse' if it hasn't already
110
- def go(argv=nil)
106
+ def go(argv = nil)
111
107
  @exit_status = nil
112
- @argv = argv if argv
108
+ @argv = argv if argv
113
109
 
114
110
  parse
115
111
 
116
112
  # the overriding class implements actual functionality beyond here
117
113
  end
118
114
 
119
-
120
115
  private
121
116
 
122
117
  # Wraps a file read with a standard bail error message
123
118
  def do_file_read(f)
124
- File.read(f) rescue(bail "File Read Error: #{$!}")
119
+ File.read(f)
120
+ rescue StandardError
121
+ (bail "File Read Error: #{$!}")
125
122
  end
126
123
 
127
-
128
124
  # Implements a basic input file argument. File reading is handled
129
125
  # by do_file_read().
130
126
  #
@@ -132,13 +128,12 @@ module Rbkb::Cli
132
128
  # the file data into.
133
129
  # (Used commonly throughout several executables)
134
130
  def add_std_file_opt(inkey)
135
- @oparse.on("-f", "--file FILENAME", "Input from FILENAME") do |f|
131
+ @oparse.on('-f', '--file FILENAME', 'Input from FILENAME') do |f|
136
132
  @opts[inkey] = do_file_read(f)
137
133
  end
138
- return @oparse
134
+ @oparse
139
135
  end
140
136
 
141
-
142
137
  # Implements numeric and hex range options via '-r' and '-x'
143
138
  #
144
139
  # Takes two arguments which are the @opts hash key names for
@@ -146,64 +141,58 @@ module Rbkb::Cli
146
141
  #
147
142
  # (Used commonly throughout several executables)
148
143
  def add_range_opts(fkey, lkey)
149
- @oparse.on("-r", "--range=START[:END]",
150
- "Start and optional end range") do |r|
144
+ @oparse.on('-r', '--range=START[:END]',
145
+ 'Start and optional end range') do |r|
146
+ raise '-x and -r are mutually exclusive' if @parser_got_range
151
147
 
152
- raise "-x and -r are mutually exclusive" if @parser_got_range
153
- @parser_got_range=true
148
+ @parser_got_range = true
154
149
 
155
- unless /^(-?[0-9]+)(?::(-?[0-9]+))?$/.match(r)
156
- raise "invalid range #{r.inspect}"
157
- end
150
+ raise "invalid range #{r.inspect}" unless /^(-?[0-9]+)(?::(-?[0-9]+))?$/.match(r)
158
151
 
159
- @opts[fkey] = $1.to_i
160
- @opts[lkey] = $2.to_i if $2
152
+ @opts[fkey] = ::Regexp.last_match(1).to_i
153
+ @opts[lkey] = ::Regexp.last_match(2).to_i if ::Regexp.last_match(2)
161
154
  end
162
155
 
163
- @oparse.on("-x", "--hexrange=START[:END]",
164
- "Start and optional end range in hex") do |r|
156
+ @oparse.on('-x', '--hexrange=START[:END]',
157
+ 'Start and optional end range in hex') do |r|
158
+ raise '-x and -r are mutually exclusive' if @parser_got_range
165
159
 
166
- raise "-x and -r are mutually exclusive" if @parser_got_range
167
- @parser_got_range=true
160
+ @parser_got_range = true
168
161
 
169
- unless /^(-?[0-9a-f]+)(?::(-?[0-9a-f]+))?$/i.match(r)
170
- raise "invalid range #{r.inspect}"
171
- end
162
+ raise "invalid range #{r.inspect}" unless /^(-?[0-9a-f]+)(?::(-?[0-9a-f]+))?$/i.match(r)
172
163
 
173
- @opts[fkey] =
174
- if ($1[0,1] == '-')
175
- ($1[1..-1]).hex_to_num * -1
164
+ @opts[fkey] =
165
+ if ::Regexp.last_match(1)[0, 1] == '-'
166
+ ::Regexp.last_match(1)[1..-1].hex_to_num * -1
176
167
  else
177
- $1.hex_to_num
168
+ ::Regexp.last_match(1).hex_to_num
178
169
  end
179
170
 
180
- if $2
181
- @opts[lkey] =
182
- if($2[0,1] == '-')
183
- $2[1..-1].hex_to_num * -1
171
+ if ::Regexp.last_match(2)
172
+ @opts[lkey] =
173
+ if ::Regexp.last_match(2)[0, 1] == '-'
174
+ ::Regexp.last_match(2)[1..-1].hex_to_num * -1
184
175
  else
185
- $2.hex_to_num
176
+ ::Regexp.last_match(2).hex_to_num
186
177
  end
187
178
  end
188
179
  end
189
180
  end
190
181
 
191
-
192
- # Conditionally parses a string argument. Uses 'key' to first check for
182
+ # Conditionally parses a string argument. Uses 'key' to first check for
193
183
  # then store it in @opts hash if it is not yet there.
194
184
  # (Used commonly throughout several executables)
195
185
  def parse_string_argument(key)
196
- if @opts[key].nil? and s=@argv.shift
197
- @opts[key] = s.dup
186
+ if @opts[key].nil? and s = @argv.shift
187
+ @opts[key] = s.dup
198
188
  end
199
189
  end
200
190
 
201
-
202
- # Conditionally parses a file argument. Uses 'key' to first check for
191
+ # Conditionally parses a file argument. Uses 'key' to first check for
203
192
  # then store it in @opts hash if it is not yet there.
204
193
  # (Used commonly throughout several executables)
205
194
  def parse_file_argument(key)
206
- if @opts[key].nil? and f=@argv.shift
195
+ if @opts[key].nil? and f = @argv.shift
207
196
  @opts[key] = do_file_read(f)
208
197
  end
209
198
  end
@@ -212,9 +201,7 @@ module Rbkb::Cli
212
201
  # arguments if there are extra arguments.
213
202
  # (Used commonly throughout several executables)
214
203
  def parse_catchall
215
- bail_args(@argv.join(' ')) if(@argv.length != 0)
204
+ bail_args(@argv.join(' ')) if @argv.length != 0
216
205
  end
217
-
218
206
  end
219
207
  end
220
-
@@ -1,6 +1,4 @@
1
-
2
1
  class Array
3
-
4
2
  # Should be in the std library.
5
3
  #
6
4
  # keys = [:one, :two, :three]
@@ -11,19 +9,20 @@ class Array
11
9
  #
12
10
  # keys.to_hash(vals)
13
11
  # #=> {:two=>2, :three=>3, :one=>1}})
14
- def to_hash(vals=nil)
15
- a = vals ? self.zip(vals) : self
16
- a.inject({}) {|hash, i| hash[i[0]] = i[1]; hash}
12
+ def to_hash(vals = nil)
13
+ a = vals ? zip(vals) : self
14
+ a.each_with_object({}) do |i, hash|
15
+ hash[i[0]] = i[1]
16
+ end
17
17
  end
18
18
 
19
19
  # randomizes the order of contents in the Array (self)
20
20
  def randomize
21
- self.sort_by{ rand }
21
+ sort_by { rand }
22
22
  end
23
23
 
24
24
  # Returns a randomly chosen element from self.
25
25
  def rand_elem
26
- self[rand(self.count)]
26
+ self[rand(count)]
27
27
  end
28
28
  end
29
-
@@ -1,13 +1,11 @@
1
- require "stringio"
1
+ require 'stringio'
2
2
  require 'zlib'
3
3
  require 'open3'
4
- require 'enumerator'
5
4
  require 'digest/md5'
6
5
  require 'digest/sha1'
7
6
  require 'digest/sha2'
8
7
 
9
8
  module Rbkb
10
- DEFAULT_BYTE_ORDER=:big
11
- HEXCHARS = [("0".."9").to_a, ("a".."f").to_a].flatten
9
+ DEFAULT_BYTE_ORDER = :big
10
+ HEXCHARS = [('0'..'9').to_a, ('a'..'f').to_a].flatten
12
11
  end
13
-
@@ -1,27 +1,27 @@
1
-
2
1
  module Enumerable
3
2
  def each_recursive(&block)
4
- self.each do |n|
3
+ each do |n|
5
4
  block.call(n)
6
- n.each_recursive(&block) if Enumerable === n
5
+ n.each_recursive(&block) if n.is_a?(Enumerable)
7
6
  end
8
7
  end
9
8
 
10
9
  def sum
11
- return self.inject(0){|accum, i| accum + i }
10
+ inject(0) { |accum, i| accum + i }
12
11
  end
13
12
 
14
13
  def mean
15
- return self.sum / self.length.to_f
14
+ sum / length.to_f
16
15
  end
17
16
 
18
17
  def sample_variance
19
- m = self.mean
20
- sum = self.inject(0){|accum, i| accum + (i - m) ** 2 }
21
- return sum / (self.length - 1).to_f
18
+ m = mean
19
+ sum = inject(0) { |accum, i| accum + (i - m)**2 }
20
+ sum / (length - 1).to_f
22
21
  end
23
22
 
24
23
  def standard_deviation
25
- return Math.sqrt(self.sample_variance)
24
+ Math.sqrt(sample_variance)
26
25
  end
27
26
  end
27
+
@@ -4,7 +4,7 @@ module Rbkb
4
4
  module Extends
5
5
  module Float
6
6
  def log2
7
- Math.log(self)/Math.log(2)
7
+ Math.log(self) / Math.log(2)
8
8
  end
9
9
  end
10
10
  end
@@ -14,4 +14,3 @@ end
14
14
  class Float
15
15
  include Rbkb::Extends::Float
16
16
  end
17
-
@@ -1,15 +1,17 @@
1
1
  require 'rbkb/extends/common'
2
2
 
3
3
  class Numeric
4
-
5
4
  # calculate padding based on alignment(a)
6
5
  def pad(a)
7
- raise "bad alignment #{a.inspect}" unless a.kind_of? Numeric and a > 0
8
- return self < 1 ? a + self : (a-1) - (self-1) % a
6
+ raise "bad alignment #{a.inspect}" unless a.is_a? Numeric and a > 0
7
+
8
+ self < 1 ? a + self : (a - 1) - (self - 1) % a
9
9
  end
10
10
 
11
11
  # tells you whether a number is within printable range
12
- def printable?; self >= 0x20 and self <= 0x7e; end
12
+ def printable?
13
+ self >= 0x20 and self <= 0x7e
14
+ end
13
15
 
14
16
  # shortcut for packing a single number... wtf...
15
17
  def pack(arg)
@@ -24,34 +26,34 @@ class Numeric
24
26
  # Accepts a block for some transformation on each byte.
25
27
  # (used by to_bytes and to_hex under the hood)
26
28
  #
27
- # args:
29
+ # args:
28
30
  # order: byte order - :big or :little
29
31
  # (only :big has meaning)
30
32
  # siz: pack to this size. larger numbers will wrap
31
- def to_chars(order=nil, siz=nil)
33
+ def to_chars(order = nil, siz = nil)
32
34
  order ||= Rbkb::DEFAULT_BYTE_ORDER
33
- n=self
34
- siz ||= self.size
35
- ret=[]
36
- siz.times do
35
+ n = self
36
+ siz ||= size
37
+ ret = []
38
+ siz.times do
37
39
  c = (n % 256)
38
40
  if block_given? then (c = yield(c)) end
39
41
  ret << c
40
- n=(n >> 8)
42
+ n = (n >> 8)
41
43
  end
42
- return ((order == :big)? ret.reverse : ret)
44
+ (order == :big ? ret.reverse : ret)
43
45
  end
44
46
 
45
47
  # "packs" a number into bytes using bit-twiddling instead of pack()
46
48
  #
47
49
  # Uses to_chars under the hood. See also: to_hex
48
50
  #
49
- # args:
51
+ # args:
50
52
  # siz: pack to this size. larger numbers will wrap
51
53
  # order: byte order - :big or :little
52
54
  # (only :big has meaning)
53
- def to_bytes(order=nil, siz=nil)
54
- to_chars(order,siz) {|c| c.chr }.join
55
+ def to_bytes(order = nil, siz = nil)
56
+ to_chars(order, siz) { |c| c.chr }.join
55
57
  end
56
58
 
57
59
  # Converts a number to hex string with width and endian options.
@@ -59,25 +61,22 @@ class Numeric
59
61
  #
60
62
  # Uses to_chars under the hood. See also: to_bytes
61
63
  #
62
- # args:
64
+ # args:
63
65
  # siz: pack to this size. larger numbers will wrap
64
66
  # order: byte order - :big or :little
65
67
  # (only :big has meaning)
66
68
  #
67
- def to_hex(o=nil, s=nil)
68
- to_chars(o,s) {|c|
69
- Rbkb::HEXCHARS[c.clear_bits(0xf) >> 4]+Rbkb::HEXCHARS[c.clear_bits(0xf0)]
70
- }.join
69
+ def to_hex(o = nil, s = nil)
70
+ to_chars(o, s) do |c|
71
+ Rbkb::HEXCHARS[c.clear_bits(0xf) >> 4] + Rbkb::HEXCHARS[c.clear_bits(0xf0)]
72
+ end.join
71
73
  end
72
74
 
73
- # TODO Fix Numeric.to_guid for new to_bytes/char etc.
74
- # def to_guid(order=Rbkb::DEFAULT_BYTE_ORDER)
75
- # raw = self.to_bytes(order, 16)
76
- # a,b,c,d,*e = raw.unpack("VvvnC6").map{|x| x.to_hex}
77
- # e = e.join
78
- # [a,b,c,d,e].join("-").upcase
79
- # end
80
-
75
+ # TODO: Fix Numeric.to_guid for new to_bytes/char etc.
76
+ # def to_guid(order=Rbkb::DEFAULT_BYTE_ORDER)
77
+ # raw = self.to_bytes(order, 16)
78
+ # a,b,c,d,*e = raw.unpack("VvvnC6").map{|x| x.to_hex}
79
+ # e = e.join
80
+ # [a,b,c,d,e].join("-").upcase
81
+ # end
81
82
  end # class Numeric
82
-
83
-
@@ -1,7 +1,6 @@
1
-
2
1
  class Object
3
2
  ## This is from Topher Cyll's Stupd IRB tricks
4
3
  def mymethods
5
- (self.methods - self.class.superclass.methods).sort
4
+ (methods - self.class.superclass.methods).sort
6
5
  end
7
6
  end