ronin-support 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. data/.document +4 -0
  2. data/.rspec +1 -0
  3. data/.yardopts +1 -0
  4. data/COPYING.txt +504 -0
  5. data/ChangeLog.md +4 -0
  6. data/Gemfile +23 -0
  7. data/README.md +98 -0
  8. data/Rakefile +29 -0
  9. data/gemspec.yml +20 -0
  10. data/lib/ronin/extensions.rb +28 -0
  11. data/lib/ronin/extensions/file.rb +63 -0
  12. data/lib/ronin/extensions/ip_addr.rb +220 -0
  13. data/lib/ronin/extensions/kernel.rb +45 -0
  14. data/lib/ronin/extensions/meta.rb +22 -0
  15. data/lib/ronin/extensions/meta/object.rb +24 -0
  16. data/lib/ronin/extensions/string.rb +200 -0
  17. data/lib/ronin/formatting.rb +26 -0
  18. data/lib/ronin/formatting/binary.rb +22 -0
  19. data/lib/ronin/formatting/digest.rb +22 -0
  20. data/lib/ronin/formatting/extensions.rb +25 -0
  21. data/lib/ronin/formatting/extensions/binary.rb +24 -0
  22. data/lib/ronin/formatting/extensions/binary/file.rb +35 -0
  23. data/lib/ronin/formatting/extensions/binary/integer.rb +147 -0
  24. data/lib/ronin/formatting/extensions/binary/string.rb +363 -0
  25. data/lib/ronin/formatting/extensions/digest.rb +23 -0
  26. data/lib/ronin/formatting/extensions/digest/file.rb +117 -0
  27. data/lib/ronin/formatting/extensions/digest/string.rb +80 -0
  28. data/lib/ronin/formatting/extensions/http.rb +23 -0
  29. data/lib/ronin/formatting/extensions/http/integer.rb +57 -0
  30. data/lib/ronin/formatting/extensions/http/string.rb +102 -0
  31. data/lib/ronin/formatting/extensions/text.rb +23 -0
  32. data/lib/ronin/formatting/extensions/text/array.rb +125 -0
  33. data/lib/ronin/formatting/extensions/text/string.rb +206 -0
  34. data/lib/ronin/formatting/http.rb +22 -0
  35. data/lib/ronin/formatting/text.rb +22 -0
  36. data/lib/ronin/network.rb +29 -0
  37. data/lib/ronin/network/esmtp.rb +22 -0
  38. data/lib/ronin/network/extensions.rb +29 -0
  39. data/lib/ronin/network/extensions/esmtp.rb +22 -0
  40. data/lib/ronin/network/extensions/esmtp/net.rb +100 -0
  41. data/lib/ronin/network/extensions/http.rb +22 -0
  42. data/lib/ronin/network/extensions/http/net.rb +661 -0
  43. data/lib/ronin/network/extensions/imap.rb +22 -0
  44. data/lib/ronin/network/extensions/imap/net.rb +124 -0
  45. data/lib/ronin/network/extensions/pop3.rb +22 -0
  46. data/lib/ronin/network/extensions/pop3/net.rb +92 -0
  47. data/lib/ronin/network/extensions/smtp.rb +22 -0
  48. data/lib/ronin/network/extensions/smtp/net.rb +110 -0
  49. data/lib/ronin/network/extensions/ssl.rb +22 -0
  50. data/lib/ronin/network/extensions/ssl/net.rb +147 -0
  51. data/lib/ronin/network/extensions/tcp.rb +22 -0
  52. data/lib/ronin/network/extensions/tcp/net.rb +304 -0
  53. data/lib/ronin/network/extensions/telnet.rb +22 -0
  54. data/lib/ronin/network/extensions/telnet/net.rb +156 -0
  55. data/lib/ronin/network/extensions/udp.rb +22 -0
  56. data/lib/ronin/network/extensions/udp/net.rb +226 -0
  57. data/lib/ronin/network/http.rb +24 -0
  58. data/lib/ronin/network/http/exceptions.rb +22 -0
  59. data/lib/ronin/network/http/exceptions/unknown_request.rb +29 -0
  60. data/lib/ronin/network/http/http.rb +290 -0
  61. data/lib/ronin/network/http/proxy.rb +307 -0
  62. data/lib/ronin/network/imap.rb +49 -0
  63. data/lib/ronin/network/network.rb +41 -0
  64. data/lib/ronin/network/pop3.rb +49 -0
  65. data/lib/ronin/network/smtp.rb +24 -0
  66. data/lib/ronin/network/smtp/email.rb +143 -0
  67. data/lib/ronin/network/smtp/smtp.rb +68 -0
  68. data/lib/ronin/network/ssl.rb +47 -0
  69. data/lib/ronin/network/tcp.rb +22 -0
  70. data/lib/ronin/network/telnet.rb +109 -0
  71. data/lib/ronin/network/udp.rb +22 -0
  72. data/lib/ronin/path.rb +132 -0
  73. data/lib/ronin/support.rb +28 -0
  74. data/lib/ronin/support/inflector.rb +40 -0
  75. data/lib/ronin/support/version.rb +27 -0
  76. data/lib/ronin/templates.rb +23 -0
  77. data/lib/ronin/templates/erb.rb +75 -0
  78. data/lib/ronin/templates/template.rb +161 -0
  79. data/ronin-support.gemspec +10 -0
  80. data/spec/extensions/file_spec.rb +24 -0
  81. data/spec/extensions/ip_addr_spec.rb +171 -0
  82. data/spec/extensions/kernel_spec.rb +30 -0
  83. data/spec/extensions/string_spec.rb +177 -0
  84. data/spec/formatting/binary/helpers/hexdumps.rb +16 -0
  85. data/spec/formatting/binary/helpers/hexdumps/ascii.bin +0 -0
  86. data/spec/formatting/binary/helpers/hexdumps/hexdump_decimal_shorts.txt +17 -0
  87. data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_bytes.txt +17 -0
  88. data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_shorts.txt +17 -0
  89. data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_bytes.txt +17 -0
  90. data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_shorts.txt +17 -0
  91. data/spec/formatting/binary/helpers/hexdumps/hexdump_repeated.txt +6 -0
  92. data/spec/formatting/binary/helpers/hexdumps/od_decimal_bytes.txt +17 -0
  93. data/spec/formatting/binary/helpers/hexdumps/od_decimal_ints.txt +17 -0
  94. data/spec/formatting/binary/helpers/hexdumps/od_decimal_quads.txt +17 -0
  95. data/spec/formatting/binary/helpers/hexdumps/od_decimal_shorts.txt +17 -0
  96. data/spec/formatting/binary/helpers/hexdumps/od_hex_bytes.txt +17 -0
  97. data/spec/formatting/binary/helpers/hexdumps/od_hex_ints.txt +17 -0
  98. data/spec/formatting/binary/helpers/hexdumps/od_hex_quads.txt +17 -0
  99. data/spec/formatting/binary/helpers/hexdumps/od_hex_shorts.txt +17 -0
  100. data/spec/formatting/binary/helpers/hexdumps/od_octal_bytes.txt +17 -0
  101. data/spec/formatting/binary/helpers/hexdumps/od_octal_ints.txt +17 -0
  102. data/spec/formatting/binary/helpers/hexdumps/od_octal_quads.txt +17 -0
  103. data/spec/formatting/binary/helpers/hexdumps/od_octal_shorts.txt +17 -0
  104. data/spec/formatting/binary/helpers/hexdumps/od_repeated.txt +6 -0
  105. data/spec/formatting/binary/helpers/hexdumps/repeated.bin +1 -0
  106. data/spec/formatting/binary/integer_spec.rb +140 -0
  107. data/spec/formatting/binary/string_spec.rb +306 -0
  108. data/spec/formatting/digest/string_spec.rb +82 -0
  109. data/spec/formatting/http/integer_spec.rb +42 -0
  110. data/spec/formatting/http/string_spec.rb +76 -0
  111. data/spec/formatting/text/array_spec.rb +105 -0
  112. data/spec/formatting/text/string_spec.rb +180 -0
  113. data/spec/network/http/http_spec.rb +280 -0
  114. data/spec/network/http/proxy_spec.rb +150 -0
  115. data/spec/network/network_spec.rb +8 -0
  116. data/spec/network/ssl_spec.rb +14 -0
  117. data/spec/path_spec.rb +84 -0
  118. data/spec/spec_helper.rb +4 -0
  119. data/spec/support_spec.rb +8 -0
  120. data/spec/templates/classes/example_erb.rb +11 -0
  121. data/spec/templates/classes/example_template.rb +35 -0
  122. data/spec/templates/erb_spec.rb +21 -0
  123. data/spec/templates/helpers/data.rb +9 -0
  124. data/spec/templates/helpers/data/includes/_relative.erb +1 -0
  125. data/spec/templates/helpers/data/templates/example.erb +1 -0
  126. data/spec/templates/template_spec.rb +54 -0
  127. metadata +286 -0
@@ -0,0 +1,23 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/formatting/extensions/digest/string'
23
+ require 'ronin/formatting/extensions/digest/file'
@@ -0,0 +1,117 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'digest/md5'
23
+ require 'digest/sha1'
24
+ require 'digest/sha2'
25
+
26
+ class File
27
+
28
+ #
29
+ # Calculates the MD5 checksum of a file.
30
+ #
31
+ # @param [String] path
32
+ # The path to the file.
33
+ #
34
+ # @return [String]
35
+ # The MD5 checksum of the file.
36
+ #
37
+ # @example
38
+ # File.md5('data.txt')
39
+ # # => "5d41402abc4b2a76b9719d911017c592"
40
+ #
41
+ def File.md5(path)
42
+ Digest::MD5.file(path).hexdigest
43
+ end
44
+
45
+ #
46
+ # Calculates the SHA1 checksum of a file.
47
+ #
48
+ # @param [String] path
49
+ # The path to the file.
50
+ #
51
+ # @return [String]
52
+ # The SHA1 checksum of the file.
53
+ #
54
+ # @example
55
+ # File.sha1('data.txt')
56
+ # # => "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"
57
+ #
58
+ def File.sha1(path)
59
+ Digest::SHA1.file(path).hexdigest
60
+ end
61
+
62
+ #
63
+ # @see File.sha1
64
+ #
65
+ def File.sha128(path)
66
+ File.sha1(path)
67
+ end
68
+
69
+ #
70
+ # Calculates the SHA256 checksum of a file.
71
+ #
72
+ # @param [String] path
73
+ # The path to the file.
74
+ #
75
+ # @return [String]
76
+ # The SHA256 checksum of the file.
77
+ #
78
+ # @example
79
+ # File.sha256('data.txt')
80
+ # # => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
81
+ #
82
+ def File.sha256(path)
83
+ Digest::SHA256.file(path).hexdigest
84
+ end
85
+
86
+ #
87
+ # @see File.sha256
88
+ #
89
+ def File.sha2(path)
90
+ File.sha256(path)
91
+ end
92
+
93
+ #
94
+ # Calculates the SHA512 checksum of a file.
95
+ #
96
+ # @param [String] path
97
+ # The path to the file.
98
+ #
99
+ # @return [String]
100
+ # The SHA512 checksum of the file.
101
+ #
102
+ # @example
103
+ # File.sha512('data.txt')
104
+ # # => "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043"
105
+ #
106
+ def File.sha512(path)
107
+ Digest::SHA512.file(path).hexdigest
108
+ end
109
+
110
+ #
111
+ # @see File.sha512
112
+ #
113
+ def File.sha5(path)
114
+ File.sha512(path)
115
+ end
116
+
117
+ end
@@ -0,0 +1,80 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'digest/md5'
23
+ require 'digest/sha1'
24
+ require 'digest/sha2'
25
+
26
+ class String
27
+
28
+ #
29
+ # @return [String]
30
+ # The MD5 checksum of the String.
31
+ #
32
+ # @example
33
+ # "hello".md5
34
+ # # => "5d41402abc4b2a76b9719d911017c592"
35
+ #
36
+ def md5
37
+ Digest::MD5.hexdigest(self)
38
+ end
39
+
40
+ #
41
+ # @return [String]
42
+ # The SHA1 checksum of the String.
43
+ #
44
+ # @example
45
+ # "hello".sha1
46
+ # # => "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"
47
+ #
48
+ def sha1
49
+ Digest::SHA1.hexdigest(self)
50
+ end
51
+
52
+ alias sha128 sha1
53
+
54
+ #
55
+ # @return [String]
56
+ # The SHA2 checksum of the String.
57
+ #
58
+ # @example
59
+ # "hello".sha2
60
+ # # => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
61
+ #
62
+ def sha256
63
+ Digest::SHA256.hexdigest(self)
64
+ end
65
+
66
+ alias sha2 sha256
67
+
68
+ #
69
+ # @return [String]
70
+ # The SHA512 checksum of the String.
71
+ #
72
+ # @example
73
+ # "hello".sha512
74
+ # # => "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043"
75
+ #
76
+ def sha512
77
+ Digest::SHA512.hexdigest(self)
78
+ end
79
+
80
+ end
@@ -0,0 +1,23 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/formatting/extensions/http/integer'
23
+ require 'ronin/formatting/extensions/http/string'
@@ -0,0 +1,57 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'uri'
23
+ require 'cgi'
24
+
25
+ class Integer
26
+
27
+ #
28
+ # URI encodes the byte.
29
+ #
30
+ # @return [String]
31
+ # The URI encoded byte.
32
+ #
33
+ def uri_encode
34
+ URI.encode(self.chr)
35
+ end
36
+
37
+ #
38
+ # URI escapes the byte.
39
+ #
40
+ # @return [String]
41
+ # The URI escaped byte.
42
+ #
43
+ def uri_escape
44
+ CGI.escape(self.chr)
45
+ end
46
+
47
+ #
48
+ # Formats the byte for HTTP.
49
+ #
50
+ # @return [String]
51
+ # The formatted byte.
52
+ #
53
+ def format_http
54
+ "%%%x" % self
55
+ end
56
+
57
+ end
@@ -0,0 +1,102 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/formatting/extensions/http/integer'
23
+ require 'ronin/formatting/extensions/text'
24
+
25
+ require 'uri'
26
+ require 'cgi'
27
+
28
+ class String
29
+
30
+ #
31
+ # URI encodes the String.
32
+ #
33
+ # @return [String]
34
+ # The URI encoded form of the String.
35
+ #
36
+ # @example
37
+ # "art is graffiti".uri_encode
38
+ # # => "art%20is%20graffiti"
39
+ #
40
+ def uri_encode
41
+ URI.encode(self)
42
+ end
43
+
44
+ #
45
+ # URI decodes the String.
46
+ #
47
+ # @return [String]
48
+ # The decoded URI form of the String.
49
+ #
50
+ # @example
51
+ # "genre%3f".uri_decode
52
+ # # => "genre?"
53
+ #
54
+ def uri_decode
55
+ URI.decode(self)
56
+ end
57
+
58
+ #
59
+ # URI escapes the String.
60
+ #
61
+ # @return [String]
62
+ # The URI escaped form of the String.
63
+ #
64
+ # @example
65
+ # "x > y".uri_escape
66
+ # # => "x+%3E+y"
67
+ #
68
+ def uri_escape
69
+ CGI.escape(self)
70
+ end
71
+
72
+ #
73
+ # URI unescapes the String.
74
+ #
75
+ # @return [String]
76
+ # The unescaped URI form of the String.
77
+ #
78
+ # @example
79
+ # "sweet+%26+sour".uri_unescape
80
+ # # => "sweet & sour"
81
+ #
82
+ def uri_unescape
83
+ CGI.unescape(self)
84
+ end
85
+
86
+ #
87
+ # Formats the bytes of the String.
88
+ #
89
+ # @return [String]
90
+ # The HTTP hexidecimal encoded form of the String.
91
+ #
92
+ # @example
93
+ # "hello".format_http
94
+ # # => "%68%65%6c%6c%6f"
95
+ #
96
+ # @see String#format_bytes
97
+ #
98
+ def format_http(options={})
99
+ format_bytes(options) { |b| b.format_http }
100
+ end
101
+
102
+ end
@@ -0,0 +1,23 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/formatting/extensions/text/string'
23
+ require 'ronin/formatting/extensions/text/array'
@@ -0,0 +1,125 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ class Array
23
+
24
+ #
25
+ # Decodes the bytes contained within the Array. The Array may contain
26
+ # both Integer and String objects.
27
+ #
28
+ # @return [Array]
29
+ # The bytes contained within the Array.
30
+ #
31
+ # @example
32
+ # [0x41, 0x41, 0x20].bytes
33
+ # # => [0x41, 0x41, 0x20]
34
+ #
35
+ # @example
36
+ # ['A', 'BB', 0x90].bytes
37
+ # # => [0x41, 0x42, 0x42, 0x90]
38
+ #
39
+ def bytes
40
+ self.inject([]) do |accum,elem|
41
+ if elem.kind_of?(Integer)
42
+ accum << elem
43
+ else
44
+ elem.to_s.each_byte { |b| accum << b }
45
+ end
46
+
47
+ accum
48
+ end
49
+ end
50
+
51
+ #
52
+ # Decodes the characters contained within the Array. The Array may contain
53
+ # either Integer or String objects.
54
+ #
55
+ # @return [Array]
56
+ # The characters generated from the array.
57
+ #
58
+ # @example
59
+ # [0x41, 0x41, 0x20].chars
60
+ # # => ["A", "A", " "]
61
+ #
62
+ def chars
63
+ array_bytes = self.bytes
64
+
65
+ array_bytes.map! { |b| b.chr }
66
+ return array_bytes
67
+ end
68
+
69
+ #
70
+ # @return [String]
71
+ # The String created from the characters within the Array.
72
+ #
73
+ # @example
74
+ # [0x41, 0x41, 0x20].char_string
75
+ # # => "AA "
76
+ #
77
+ def char_string
78
+ chars.join
79
+ end
80
+
81
+ #
82
+ # Decodes the bytes contained with the Array, and escapes them as
83
+ # hexadecimal characters.
84
+ #
85
+ # @return [Array<String>]
86
+ # The hexidecimal characters contained within the Array.
87
+ #
88
+ # @example
89
+ # [0x41, 0x41, 0x20].bytes
90
+ # # => ['\x41', '\x41', '\x20']
91
+ #
92
+ # @example
93
+ # ['A', 'BB', 0x90].bytes
94
+ # # => ['\x41', '\x42', '\x42', '\x90']
95
+ #
96
+ def hex_chars
97
+ array_bytes = self.bytes
98
+
99
+ array_bytes.map! { |b| '\x%x' % b }
100
+ return array_bytes
101
+ end
102
+
103
+ #
104
+ # Decodes the bytes contained with the Array, and escapes them as
105
+ # hexadecimal integers.
106
+ #
107
+ # @return [Array<String>]
108
+ # The hexidecimal integers contained within the Array.
109
+ #
110
+ # @example
111
+ # [0x41, 0x41, 0x20].bytes
112
+ # # => ['0x41', '0x41', '0x20']
113
+ #
114
+ # @example
115
+ # ['A', 'BB', 0x90].bytes
116
+ # # => ['0x41', '0x42', '0x42', '0x90']
117
+ #
118
+ def hex_integers
119
+ array_bytes = self.bytes
120
+
121
+ array_bytes.map! { |b| '0x%x' % b }
122
+ return array_bytes
123
+ end
124
+
125
+ end