ronin-support 0.4.1 → 0.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/ChangeLog.md +75 -32
  2. data/Gemfile +17 -18
  3. data/README.md +9 -10
  4. data/Rakefile +10 -2
  5. data/gemspec.yml +1 -1
  6. data/lib/ronin/binary.rb +21 -0
  7. data/lib/ronin/binary/hexdump.rb +20 -0
  8. data/lib/ronin/binary/hexdump/parser.rb +411 -0
  9. data/lib/ronin/binary/struct.rb +579 -0
  10. data/lib/ronin/binary/template.rb +437 -0
  11. data/lib/ronin/extensions/ip_addr.rb +17 -13
  12. data/lib/ronin/extensions/regexp.rb +45 -0
  13. data/lib/ronin/extensions/string.rb +3 -3
  14. data/lib/ronin/formatting/extensions/binary.rb +1 -0
  15. data/lib/ronin/formatting/extensions/binary/array.rb +63 -0
  16. data/lib/ronin/formatting/extensions/binary/base64.rb +106 -0
  17. data/lib/ronin/formatting/extensions/binary/file.rb +39 -6
  18. data/lib/ronin/formatting/extensions/binary/float.rb +65 -0
  19. data/lib/ronin/formatting/extensions/binary/integer.rb +56 -43
  20. data/lib/ronin/formatting/extensions/binary/string.rb +75 -187
  21. data/lib/ronin/formatting/extensions/text/string.rb +61 -0
  22. data/lib/ronin/fuzzing/extensions/string.rb +21 -8
  23. data/lib/ronin/fuzzing/fuzzing.rb +19 -17
  24. data/lib/ronin/network.rb +2 -1
  25. data/lib/ronin/network/dns.rb +57 -15
  26. data/lib/ronin/network/extensions.rb +0 -1
  27. data/lib/ronin/network/ftp.rb +145 -0
  28. data/lib/ronin/network/http/http.rb +13 -14
  29. data/lib/ronin/network/imap.rb +11 -10
  30. data/lib/ronin/network/mixins.rb +1 -0
  31. data/lib/ronin/network/mixins/ftp.rb +155 -0
  32. data/lib/ronin/network/mixins/ssl.rb +1 -1
  33. data/lib/ronin/network/mixins/tcp.rb +39 -6
  34. data/lib/ronin/network/mixins/udp.rb +121 -1
  35. data/lib/ronin/network/mixins/unix.rb +279 -0
  36. data/lib/ronin/network/pop3.rb +5 -5
  37. data/lib/ronin/network/proxy.rb +578 -0
  38. data/lib/ronin/network/smtp/email.rb +1 -1
  39. data/lib/ronin/network/smtp/smtp.rb +7 -8
  40. data/lib/ronin/network/ssl.rb +1 -6
  41. data/lib/ronin/network/tcp.rb +2 -305
  42. data/lib/ronin/network/tcp/proxy.rb +377 -0
  43. data/lib/ronin/network/tcp/tcp.rb +435 -0
  44. data/lib/ronin/network/telnet.rb +27 -23
  45. data/lib/ronin/network/udp.rb +2 -266
  46. data/lib/ronin/network/udp/proxy.rb +169 -0
  47. data/lib/ronin/network/udp/udp.rb +442 -0
  48. data/lib/ronin/network/unix.rb +287 -0
  49. data/lib/ronin/path.rb +2 -2
  50. data/lib/ronin/spec/ui/output.rb +1 -7
  51. data/lib/ronin/support.rb +1 -0
  52. data/lib/ronin/support/inflector.rb +3 -7
  53. data/lib/ronin/support/support.rb +2 -1
  54. data/lib/ronin/support/version.rb +1 -1
  55. data/lib/ronin/ui/output/helpers.rb +13 -15
  56. data/lib/ronin/ui/output/output.rb +2 -2
  57. data/lib/ronin/ui/output/terminal/color.rb +10 -4
  58. data/lib/ronin/wordlist.rb +92 -17
  59. data/ronin-support.gemspec +38 -109
  60. data/spec/binary/hexdump/helpers/hexdumps.rb +13 -0
  61. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/ascii.bin +0 -0
  62. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_decimal_shorts.txt +0 -0
  63. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_bytes.txt +0 -0
  64. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_shorts.txt +0 -0
  65. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_bytes.txt +0 -0
  66. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_shorts.txt +0 -0
  67. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_repeated.txt +0 -0
  68. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_bytes.txt +0 -0
  69. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_ints.txt +0 -0
  70. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_quads.txt +0 -0
  71. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_shorts.txt +0 -0
  72. data/spec/binary/hexdump/helpers/hexdumps/od_doubles.txt +17 -0
  73. data/spec/binary/hexdump/helpers/hexdumps/od_floats.txt +17 -0
  74. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_bytes.txt +0 -0
  75. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_ints.txt +0 -0
  76. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_quads.txt +0 -0
  77. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_shorts.txt +0 -0
  78. data/spec/binary/hexdump/helpers/hexdumps/od_named_chars.txt +17 -0
  79. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_bytes.txt +0 -0
  80. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_ints.txt +0 -0
  81. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_quads.txt +0 -0
  82. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_shorts.txt +0 -0
  83. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_repeated.txt +0 -0
  84. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/repeated.bin +0 -0
  85. data/spec/binary/hexdump/parser_spec.rb +302 -0
  86. data/spec/binary/struct_spec.rb +496 -0
  87. data/spec/binary/template_spec.rb +400 -0
  88. data/spec/extensions/ip_addr_spec.rb +58 -32
  89. data/spec/extensions/regexp_spec.rb +60 -0
  90. data/spec/extensions/string_spec.rb +1 -1
  91. data/spec/formatting/binary/array_spec.rb +22 -0
  92. data/spec/formatting/binary/base64_spec.rb +50 -0
  93. data/spec/formatting/binary/float_spec.rb +30 -0
  94. data/spec/formatting/binary/integer_spec.rb +54 -40
  95. data/spec/formatting/binary/string_spec.rb +69 -182
  96. data/spec/formatting/text/string_spec.rb +30 -0
  97. data/spec/network/dns_spec.rb +64 -0
  98. data/spec/network/ftp_spec.rb +65 -0
  99. data/spec/network/proxy_spec.rb +121 -0
  100. data/spec/network/shared/unix_server.rb +31 -0
  101. data/spec/network/tcp/proxy_spec.rb +116 -0
  102. data/spec/network/{tcp_spec.rb → tcp/tcp_spec.rb} +24 -1
  103. data/spec/network/telnet_spec.rb +67 -0
  104. data/spec/network/{udp_spec.rb → udp/udp_spec.rb} +24 -1
  105. data/spec/network/unix_spec.rb +183 -0
  106. data/spec/wordlist_spec.rb +74 -13
  107. metadata +129 -85
  108. data/spec/formatting/binary/helpers/hexdumps.rb +0 -16
@@ -19,9 +19,19 @@
19
19
 
20
20
  require 'ronin/extensions/resolv'
21
21
 
22
+ #
23
+ # @since 0.3.0
24
+ #
22
25
  class Regexp
23
26
 
27
+ # Regular expression for finding words
28
+ #
29
+ # @since 0.5.0
30
+ WORD = /[A-Za-z][A-Za-z'\-\.]*[A-Za-z]/
31
+
24
32
  # Regular expression for finding a decimal octet (0 - 255)
33
+ #
34
+ # @since 0.4.0
25
35
  OCTET = /25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]/
26
36
 
27
37
  # Regular expression for finding MAC addresses in text
@@ -64,49 +74,84 @@ class Regexp
64
74
  # Regular expression to find email addresses in text
65
75
  EMAIL_ADDR = /#{USER_NAME}\@#{HOST_NAME}/
66
76
 
77
+ # Regular expression to find phone numbers in text
78
+ #
79
+ # @since 0.5.0
80
+ PHONE_NUMBER = /(?:\d[ \-\.]?)?(?:\d{3}[ \-\.]?)?\d{3}[ \-\.]?\d{4}(?:x\d+)?/
81
+
67
82
  # Regular expression to find deliminators in text
83
+ #
84
+ # @since 0.4.0
68
85
  DELIM = /[;&\n\r]/
69
86
 
70
87
  # Regular expression to find identifier in text
88
+ #
89
+ # @since 0.4.0
71
90
  IDENTIFIER = /[_]*[a-zA-Z]+[a-zA-Z0-9_-]*/
72
91
 
73
92
  # Regular expression to find File extensions in text
93
+ #
94
+ # @since 0.4.0
74
95
  FILE_EXT = /(?:\.[A-Za-z0-9]+)+/
75
96
 
76
97
  # Regular expression to find File names in text
98
+ #
99
+ # @since 0.4.0
77
100
  FILE_NAME = /(?:[^\/\\\. ]|\\[\/\\ ])+/
78
101
 
79
102
  # Regular expression to find Files in text
103
+ #
104
+ # @since 0.4.0
80
105
  FILE = /#{FILE_NAME}(?:#{FILE_EXT})?/
81
106
 
82
107
  # Regular expression to find Directory names in text
108
+ #
109
+ # @since 0.4.0
83
110
  DIRECTORY = /(?:\.\.|\.|#{FILE})/
84
111
 
85
112
  # Regular expression to find local UNIX Paths in text
113
+ #
114
+ # @since 0.4.0
86
115
  RELATIVE_UNIX_PATH = /(?:#{DIRECTORY}\/)+#{DIRECTORY}\/?/
87
116
 
88
117
  # Regular expression to find absolute UNIX Paths in text
118
+ #
119
+ # @since 0.4.0
89
120
  ABSOLUTE_UNIX_PATH = /(?:\/#{FILE})+\/?/
90
121
 
91
122
  # Regular expression to find UNIX Paths in text
123
+ #
124
+ # @since 0.4.0
92
125
  UNIX_PATH = /#{ABSOLUTE_UNIX_PATH}|#{RELATIVE_UNIX_PATH}/
93
126
 
94
127
  # Regular expression to find local Windows Paths in text
128
+ #
129
+ # @since 0.4.0
95
130
  RELATIVE_WINDOWS_PATH = /(?:#{DIRECTORY}\\)+#{DIRECTORY}\\?/
96
131
 
97
132
  # Regular expression to find absolute Windows Paths in text
133
+ #
134
+ # @since 0.4.0
98
135
  ABSOLUTE_WINDOWS_PATH = /[A-Za-z]:(?:\\#{FILE})+\\?/
99
136
 
100
137
  # Regular expression to find Windows Paths in text
138
+ #
139
+ # @since 0.4.0
101
140
  WINDOWS_PATH = /#{ABSOLUTE_WINDOWS_PATH}|#{RELATIVE_WINDOWS_PATH}/
102
141
 
103
142
  # Regular expression to find local Paths in text
143
+ #
144
+ # @since 0.4.0
104
145
  RELATIVE_PATH = /#{RELATIVE_UNIX_PATH}|#{RELATIVE_WINDOWS_PATH}/
105
146
 
106
147
  # Regular expression to find absolute Paths in text
148
+ #
149
+ # @since 0.4.0
107
150
  ABSOLUTE_PATH = /#{ABSOLUTE_UNIX_PATH}|#{ABSOLUTE_WINDOWS_PATH}/
108
151
 
109
152
  # Regular expression to find Paths in text
153
+ #
154
+ # @since 0.4.0
110
155
  PATH = /#{UNIX_PATH}|#{WINDOWS_PATH}/
111
156
 
112
157
  end
@@ -137,7 +137,7 @@ class String
137
137
  #
138
138
  # Finds the common suffix of the string and the specified other string.
139
139
  #
140
- # @param [String] pther
140
+ # @param [String] other
141
141
  # The other String to compare against.
142
142
  #
143
143
  # @return [String]
@@ -175,14 +175,14 @@ class String
175
175
  end
176
176
 
177
177
  #
178
- # Finds the uncommon substring within the specified other string,
178
+ # Finds the uncommon sub-string within the specified other string,
179
179
  # which does not occur within the string.
180
180
  #
181
181
  # @param [String] other
182
182
  # The other String to compare against.
183
183
  #
184
184
  # @return [String]
185
- # The uncommon substring between the two Strings.
185
+ # The uncommon sub-string between the two Strings.
186
186
  #
187
187
  # @api public
188
188
  #
@@ -18,6 +18,7 @@
18
18
  #
19
19
 
20
20
  require 'ronin/formatting/extensions/binary/integer'
21
+ require 'ronin/formatting/extensions/binary/float'
21
22
  require 'ronin/formatting/extensions/binary/string'
22
23
  require 'ronin/formatting/extensions/binary/file'
23
24
 
@@ -0,0 +1,63 @@
1
+ #
2
+ # Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ #
4
+ # This file is part of Ronin Support.
5
+ #
6
+ # Ronin Support is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Ronin Support is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ require 'ronin/binary/template'
21
+
22
+ class Array
23
+
24
+ alias pack_original pack
25
+
26
+ #
27
+ # Packs the Array into a String.
28
+ #
29
+ # @param [String, Array<Symbol>] arguments
30
+ # The `Array#pack` template or a list of {Ronin::Binary::Template} types.
31
+ #
32
+ # @return [String]
33
+ # The packed Array.
34
+ #
35
+ # @raise [ArgumentError]
36
+ # The arguments were not a String or a list of Symbols.
37
+ #
38
+ # @example using {Ronin::Binary::Template} types:
39
+ # [0x1234, "hello"].pack(:uint16_le, :string)
40
+ # # => "\x34\x12hello\0"
41
+ #
42
+ # @example using a `String#unpack` template:
43
+ # [0x1234, "hello"].pack('vZ*')
44
+ # # => "\x34\x12hello\0"
45
+ #
46
+ # @see http://rubydoc.info/stdlib/core/Array:pack
47
+ #
48
+ # @since 0.5.0
49
+ #
50
+ # @api public
51
+ #
52
+ def pack(*arguments)
53
+ case arguments.first
54
+ when String
55
+ pack_original(arguments.first)
56
+ when Symbol
57
+ pack_original(Ronin::Binary::Template.compile(arguments))
58
+ else
59
+ raise(ArgumentError,"first argument to Array#pack must be a String or Symbol")
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,106 @@
1
+ #
2
+ # Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ #
4
+ # This file is part of Ronin Support.
5
+ #
6
+ # Ronin Support is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Ronin Support is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ require 'base64'
21
+
22
+ #
23
+ # Adds Ruby 1.9 specific methods when running on Ruby 1.8.7.
24
+ #
25
+ module Base64
26
+ if RUBY_VERSION < '1.9.'
27
+ module_function
28
+
29
+ #
30
+ # Strictly encodes a base64 String.
31
+ #
32
+ # @param [String] bin
33
+ # The String to encode.
34
+ #
35
+ # @return [String]
36
+ # The strictly encoded base64 String.
37
+ #
38
+ # @note
39
+ # This method complies with RFC 4648.
40
+ # No line feeds are added.
41
+ #
42
+ def strict_encode64(bin)
43
+ encode64(bin).tr("\n",'')
44
+ end
45
+
46
+ #
47
+ # Decodes a strictly base64 encoded String.
48
+ #
49
+ # @param [String] str
50
+ # The strictly encoded base64 String.
51
+ #
52
+ # @return [String]
53
+ # The decoded String.
54
+ #
55
+ # @raise [ArgumentError]
56
+ # The String is incorrectly padded or contains non-alphabet characters.
57
+ # Note: CR or LF are also rejected.
58
+ #
59
+ # @note
60
+ # This method complies with RFC 4648.
61
+ #
62
+ def strict_decode64(str)
63
+ unless str.include?("\n")
64
+ decode64(str)
65
+ else
66
+ raise(ArgumentError,"invalid base64")
67
+ end
68
+ end
69
+
70
+ #
71
+ # Encodes a URL-safe base64 String.
72
+ #
73
+ # @param [String] bin
74
+ # The String to encode.
75
+ #
76
+ # @return [String]
77
+ # The URL-safe encoded base64 String.
78
+ #
79
+ # @note
80
+ # This method complies with ``Base 64 Encoding with URL and filename Safe
81
+ # Alphabet'' in RFC 4648.
82
+ # The alphabet uses '-' instead of '+' and '_' instead of '/'.
83
+ #
84
+ def urlsafe_encode64(bin)
85
+ strict_encode64(bin).tr("+/", "-_")
86
+ end
87
+
88
+ #
89
+ # Decodes a URL-safe base64 encoded String.
90
+ #
91
+ # @param [String] str
92
+ # The URL-safe encoded base64 String.
93
+ #
94
+ # @return [String]
95
+ # The decoded String.
96
+ #
97
+ # @note
98
+ # This method complies with ``Base 64 Encoding with URL and filename Safe
99
+ # Alphabet'' in RFC 4648.
100
+ # The alphabet uses '-' instead of '+' and '_' instead of '/'.
101
+ #
102
+ def urlsafe_decode64(str)
103
+ strict_decode64(str.tr("-_", "+/"))
104
+ end
105
+ end
106
+ end
@@ -17,7 +17,7 @@
17
17
  # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
18
  #
19
19
 
20
- require 'ronin/formatting/extensions/binary/string'
20
+ require 'ronin/binary/hexdump/parser'
21
21
 
22
22
  class File
23
23
 
@@ -28,17 +28,50 @@ class File
28
28
  # The path of the hexdump file.
29
29
  #
30
30
  # @param [Hash] options
31
- # Hexdump options.
31
+ # Additional options.
32
32
  #
33
- # @return [String]
34
- # The original binary data.
33
+ # @option options [Symbol] :format
34
+ # The expected format of the hexdump. Must be either `:od` or
35
+ # `:hexdump`.
36
+ #
37
+ # @option options [Symbol] :encoding
38
+ # Denotes the encoding used for the bytes within the hexdump.
39
+ # Must be one of the following:
40
+ #
41
+ # * `:binary`
42
+ # * `:octal`
43
+ # * `:octal_bytes`
44
+ # * `:octal_shorts`
45
+ # * `:octal_ints`
46
+ # * `:octal_quads` (Ruby 1.9 only)
47
+ # * `:decimal`
48
+ # * `:decimal_bytes`
49
+ # * `:decimal_shorts`
50
+ # * `:decimal_ints`
51
+ # * `:decimal_quads` (Ruby 1.9 only)
52
+ # * `:hex`
53
+ # * `:hex_chars`
54
+ # * `:hex_bytes`
55
+ # * `:hex_shorts`
56
+ # * `:hex_ints`
57
+ # * `:hex_quads`
58
+ # * `:named_chars` (Ruby 1.9 only)
59
+ # * `:floats`
60
+ # * `:doubles`
35
61
  #
36
- # @see String#unhexdump.
62
+ # @option options [:little, :big, :network] :endian (:little)
63
+ # The endianness of the words.
64
+ #
65
+ # @option options [Integer] :segment (16)
66
+ # The length in bytes of each segment in the hexdump.
67
+ #
68
+ # @return [String]
69
+ # The raw-data from the hexdump.
37
70
  #
38
71
  # @api public
39
72
  #
40
73
  def File.unhexdump(path,options={})
41
- File.read(path).unhexdump(options)
74
+ Ronin::Binary::Hexdump::Parser.new(options).parse(File.new(path))
42
75
  end
43
76
 
44
77
  end
@@ -0,0 +1,65 @@
1
+ #
2
+ # Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ #
4
+ # This file is part of Ronin Support.
5
+ #
6
+ # Ronin Support is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Ronin Support is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ require 'ronin/binary/template'
21
+
22
+ class Float
23
+
24
+ #
25
+ # Packs the Float into a String.
26
+ #
27
+ # @param [String, Symbol] argument
28
+ # The `Array#pack` String code or {Ronin::Binary::Template} type.
29
+ #
30
+ # @return [String]
31
+ # The packed float.
32
+ #
33
+ # @raise [ArgumentError]
34
+ # The argument was not a String code or a Symbol.
35
+ #
36
+ # @example using `Array#pack` template:
37
+ # 0.42.pack('F')
38
+ # # => =\n\xD7>"
39
+ #
40
+ # @example using {Ronin::Binary::Template} types:
41
+ # 0x42.pack(:float_be)
42
+ # # => ">\xD7\n="
43
+ #
44
+ # @see http://rubydoc.info/stdlib/core/Array:pack
45
+ #
46
+ # @since 0.5.0
47
+ #
48
+ # @api public
49
+ #
50
+ def pack(argument)
51
+ case argument
52
+ when String
53
+ [self].pack(argument)
54
+ when Symbol
55
+ unless Ronin::Binary::Template::FLOAT_TYPES.include?(argument)
56
+ raise(ArgumentError,"unsupported integer type: #{argument}")
57
+ end
58
+
59
+ [self].pack(Ronin::Binary::Template::TYPES[argument])
60
+ else
61
+ raise(ArgumentError,"argument must be a String or a Symbol")
62
+ end
63
+ end
64
+
65
+ end
@@ -17,21 +17,23 @@
17
17
  # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
18
  #
19
19
 
20
+ require 'ronin/binary/template'
21
+
20
22
  class Integer
21
23
 
22
24
  #
23
25
  # Extracts a sequence of bytes which represent the Integer.
24
26
  #
25
- # @param [Integer] address_length
27
+ # @param [Integer] length
26
28
  # The number of bytes to decode from the Integer.
27
29
  #
28
30
  # @param [Symbol, String] endian
29
31
  # The endianness to use while decoding the bytes of the Integer.
30
32
  # May be one of:
31
33
  #
32
- # * `:big` / `"big"`
33
- # * `:little` / `"little"`
34
- # * `:net` / `"net"`
34
+ # * `big`
35
+ # * `little`
36
+ # * `net`
35
37
  #
36
38
  # @return [Array]
37
39
  # The bytes decoded from the Integer.
@@ -39,9 +41,9 @@ class Integer
39
41
  # @raise [ArgumentError]
40
42
  # The given `endian` was not one of:
41
43
  #
42
- # * `:little` / `"little"`
43
- # * `:net` / `"net"`
44
- # * `:big` / `"big"`
44
+ # * `little`
45
+ # * `net`
46
+ # * `big`
45
47
  #
46
48
  # @example
47
49
  # 0xff41.bytes(2)
@@ -53,7 +55,7 @@ class Integer
53
55
  #
54
56
  # @api public
55
57
  #
56
- def bytes(address_length,endian=:little)
58
+ def bytes(length,endian=:little)
57
59
  endian = endian.to_sym
58
60
  buffer = []
59
61
 
@@ -62,17 +64,17 @@ class Integer
62
64
  mask = 0xff
63
65
  shift = 0
64
66
 
65
- address_length.times do |i|
67
+ length.times do |i|
66
68
  buffer << ((self & mask) >> shift)
67
69
 
68
70
  mask <<= 8
69
71
  shift += 8
70
72
  end
71
73
  when :big
72
- shift = ((address_length - 1) * 8)
74
+ shift = ((length - 1) * 8)
73
75
  mask = (0xff << shift)
74
76
 
75
- address_length.times do |i|
77
+ length.times do |i|
76
78
  buffer << ((self & mask) >> shift)
77
79
 
78
80
  mask >>= 8
@@ -86,63 +88,74 @@ class Integer
86
88
  end
87
89
 
88
90
  #
89
- # Packs the Integer into a String, for a specific architecture and
90
- # address-length.
91
- #
92
- # @param [Ronin::Arch, #endian, #address_length, String] arch
93
- # The architecture to pack the Integer for.
91
+ # Packs the Integer into a String.
94
92
  #
95
- # @param [Integer] address_length
96
- # The number of bytes to pack.
93
+ # @param [String, Symbol, arch] arguments
94
+ # The `Array#pack` code, {Ronin::Binary::Template} type or Architecture
95
+ # object with `#endian` and `#address_length` methods.
97
96
  #
98
97
  # @return [String]
99
98
  # The packed Integer.
100
99
  #
101
100
  # @raise [ArgumentError]
102
- # The given `arch` does not respond to the `endian` or
103
- # `address_length` methods.
101
+ # The arguments were not a String, Symbol or Architecture object.
102
+ #
103
+ # @example using a `Array#pack` template:
104
+ # 0x41.pack('V')
105
+ # # => "A\0\0\0"
106
+ #
107
+ # @example using {Ronin::Binary::Template} types:
108
+ # 0x41.pack(:uint32_le)
104
109
  #
105
- # @example using archs other than `Ronin::Arch`.
110
+ # @example using archs other than `Ronin::Arch` (**deprecated**):
106
111
  # arch = OpenStruct.new(:endian => :little, :address_length => 4)
107
- #
112
+ #
108
113
  # 0x41.pack(arch)
109
114
  # # => "A\0\0\0"
110
115
  #
111
- # @example using a `Ronin::Arch` arch.
116
+ # @example using a `Ronin::Arch` arch (**deprecated**):
112
117
  # 0x41.pack(Arch.i686)
113
118
  # # => "A\0\0\0"
114
119
  #
115
- # @example specifying a custom address-length.
120
+ # @example specifying a custom address-length (**deprecated**):
116
121
  # 0x41.pack(Arch.ppc,2)
117
122
  # # => "\0A"
118
123
  #
119
- # @example using a `Array#pack` template String for the arch.
120
- # 0x41.pack('L')
121
- # # => "A\0\0\0"
122
- #
123
- # @see http://ruby-doc.org/core/classes/Array.html#M002222
124
+ # @see http://rubydoc.info/stdlib/core/Array:pack
124
125
  #
125
126
  # @api public
126
127
  #
127
- def pack(arch,address_length=nil)
128
- if arch.kind_of?(String)
129
- return [self].pack(arch)
130
- end
128
+ def pack(*arguments)
129
+ argument = arguments.first
131
130
 
132
- unless arch.respond_to?(:address_length)
133
- raise(ArgumentError,"first argument to Ineger#pack must respond to address_length")
134
- end
131
+ case argument
132
+ when String
133
+ [self].pack(argument)
134
+ when Symbol
135
+ unless Ronin::Binary::Template::INT_TYPES.include?(argument)
136
+ raise(ArgumentError,"unsupported integer type: #{argument}")
137
+ end
135
138
 
136
- unless arch.respond_to?(:endian)
137
- raise(ArgumentError,"first argument to Ineger#pack must respond to endian")
138
- end
139
+ [self].pack(Ronin::Binary::Template::TYPES[argument])
140
+ else
141
+ # TODO: deprecate this calling convention
142
+ arch, address_length = arguments
139
143
 
140
- address_length ||= arch.address_length
144
+ unless arch.respond_to?(:address_length)
145
+ raise(ArgumentError,"first argument to Ineger#pack must respond to address_length")
146
+ end
141
147
 
142
- integer_bytes = bytes(address_length,arch.endian)
143
- integer_bytes.map! { |b| b.chr }
148
+ unless arch.respond_to?(:endian)
149
+ raise(ArgumentError,"first argument to Ineger#pack must respond to endian")
150
+ end
151
+
152
+ address_length ||= arch.address_length
144
153
 
145
- return integer_bytes.join
154
+ integer_bytes = bytes(address_length,arch.endian)
155
+ integer_bytes.map! { |b| b.chr }
156
+
157
+ return integer_bytes.join
158
+ end
146
159
  end
147
160
 
148
161
  #