ronin-support 0.4.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +11 -0
- data/.github/workflows/ruby.yml +28 -0
- data/.ruby-version +1 -0
- data/.yardopts +1 -1
- data/ChangeLog.md +121 -33
- data/Gemfile +17 -18
- data/README.md +57 -33
- data/Rakefile +10 -3
- data/gemspec.yml +16 -7
- data/lib/ronin/binary.rb +21 -0
- data/lib/ronin/binary/hexdump.rb +20 -0
- data/lib/ronin/binary/hexdump/parser.rb +403 -0
- data/lib/ronin/binary/struct.rb +567 -0
- data/lib/ronin/binary/template.rb +454 -0
- data/lib/ronin/extensions.rb +5 -5
- data/lib/ronin/extensions/enumerable.rb +5 -5
- data/lib/ronin/extensions/file.rb +39 -33
- data/lib/ronin/extensions/ip_addr.rb +29 -31
- data/lib/ronin/extensions/kernel.rb +5 -5
- data/lib/ronin/extensions/meta.rb +5 -5
- data/lib/ronin/extensions/regexp.rb +50 -5
- data/lib/ronin/extensions/resolv.rb +7 -9
- data/lib/ronin/extensions/string.rb +10 -10
- data/lib/ronin/formatting.rb +5 -5
- data/lib/ronin/formatting/binary.rb +5 -5
- data/lib/ronin/formatting/digest.rb +5 -5
- data/lib/ronin/formatting/extensions.rb +5 -5
- data/lib/ronin/formatting/extensions/binary.rb +7 -5
- data/lib/ronin/formatting/extensions/binary/array.rb +61 -0
- data/lib/ronin/formatting/extensions/binary/base64.rb +106 -0
- data/lib/ronin/formatting/extensions/binary/file.rb +44 -11
- data/lib/ronin/formatting/extensions/binary/float.rb +65 -0
- data/lib/ronin/formatting/extensions/binary/integer.rb +66 -50
- data/lib/ronin/formatting/extensions/binary/string.rb +81 -205
- data/lib/ronin/formatting/extensions/digest.rb +5 -5
- data/lib/ronin/formatting/extensions/digest/file.rb +5 -5
- data/lib/ronin/formatting/extensions/digest/string.rb +5 -5
- data/lib/ronin/formatting/extensions/html.rb +5 -5
- data/lib/ronin/formatting/extensions/html/integer.rb +9 -13
- data/lib/ronin/formatting/extensions/html/string.rb +31 -39
- data/lib/ronin/formatting/extensions/http.rb +5 -5
- data/lib/ronin/formatting/extensions/http/integer.rb +6 -6
- data/lib/ronin/formatting/extensions/http/string.rb +7 -7
- data/lib/ronin/formatting/extensions/sql.rb +5 -5
- data/lib/ronin/formatting/extensions/sql/string.rb +22 -24
- data/lib/ronin/formatting/extensions/text.rb +5 -5
- data/lib/ronin/formatting/extensions/text/array.rb +13 -11
- data/lib/ronin/formatting/extensions/text/string.rb +70 -13
- data/lib/ronin/formatting/html.rb +5 -5
- data/lib/ronin/formatting/http.rb +5 -5
- data/lib/ronin/formatting/sql.rb +5 -5
- data/lib/ronin/formatting/text.rb +5 -5
- data/lib/ronin/fuzzing.rb +5 -5
- data/lib/ronin/fuzzing/extensions.rb +5 -5
- data/lib/ronin/fuzzing/extensions/string.rb +42 -213
- data/lib/ronin/fuzzing/fuzzer.rb +110 -0
- data/lib/ronin/fuzzing/fuzzing.rb +33 -26
- data/lib/ronin/fuzzing/mutator.rb +161 -0
- data/lib/ronin/fuzzing/repeater.rb +81 -0
- data/lib/ronin/fuzzing/template.rb +133 -0
- data/lib/ronin/mixin.rb +2 -2
- data/lib/ronin/network.rb +7 -5
- data/lib/ronin/network/dns.rb +64 -24
- data/lib/ronin/network/esmtp.rb +5 -5
- data/lib/ronin/network/extensions.rb +5 -5
- data/lib/ronin/network/extensions/dns.rb +5 -5
- data/lib/ronin/network/extensions/dns/net.rb +5 -5
- data/lib/ronin/network/extensions/esmtp.rb +5 -5
- data/lib/ronin/network/extensions/esmtp/net.rb +5 -5
- data/lib/ronin/network/extensions/http.rb +5 -5
- data/lib/ronin/network/extensions/http/net.rb +5 -5
- data/lib/ronin/network/extensions/http/uri/http.rb +5 -5
- data/lib/ronin/network/extensions/imap.rb +5 -5
- data/lib/ronin/network/extensions/imap/net.rb +5 -5
- data/lib/ronin/network/extensions/pop3.rb +5 -5
- data/lib/ronin/network/extensions/pop3/net.rb +5 -5
- data/lib/ronin/network/extensions/smtp.rb +5 -5
- data/lib/ronin/network/extensions/smtp/net.rb +5 -5
- data/lib/ronin/network/extensions/ssl.rb +5 -5
- data/lib/ronin/network/extensions/ssl/net.rb +5 -5
- data/lib/ronin/network/extensions/tcp.rb +5 -5
- data/lib/ronin/network/extensions/tcp/net.rb +5 -5
- data/lib/ronin/network/extensions/telnet.rb +5 -5
- data/lib/ronin/network/extensions/telnet/net.rb +5 -5
- data/lib/ronin/network/extensions/udp.rb +5 -5
- data/lib/ronin/network/extensions/udp/net.rb +5 -5
- data/lib/ronin/network/ftp.rb +149 -0
- data/lib/ronin/network/http.rb +5 -5
- data/lib/ronin/network/http/exceptions.rb +5 -5
- data/lib/ronin/network/http/exceptions/unknown_request.rb +5 -5
- data/lib/ronin/network/http/http.rb +65 -70
- data/lib/ronin/network/http/proxy.rb +5 -5
- data/lib/ronin/network/imap.rb +16 -15
- data/lib/ronin/network/mixins.rb +6 -5
- data/lib/ronin/network/mixins/dns.rb +5 -5
- data/lib/ronin/network/mixins/esmtp.rb +5 -5
- data/lib/ronin/network/mixins/ftp.rb +155 -0
- data/lib/ronin/network/mixins/http.rb +58 -587
- data/lib/ronin/network/mixins/imap.rb +5 -5
- data/lib/ronin/network/mixins/mixin.rb +5 -5
- data/lib/ronin/network/mixins/pop3.rb +5 -5
- data/lib/ronin/network/mixins/smtp.rb +5 -5
- data/lib/ronin/network/mixins/ssl.rb +5 -5
- data/lib/ronin/network/mixins/tcp.rb +43 -10
- data/lib/ronin/network/mixins/telnet.rb +5 -5
- data/lib/ronin/network/mixins/udp.rb +126 -6
- data/lib/ronin/network/mixins/unix.rb +279 -0
- data/lib/ronin/network/network.rb +5 -5
- data/lib/ronin/network/pop3.rb +10 -10
- data/lib/ronin/network/proxy.rb +578 -0
- data/lib/ronin/network/smtp.rb +5 -5
- data/lib/ronin/network/smtp/email.rb +6 -6
- data/lib/ronin/network/smtp/smtp.rb +12 -13
- data/lib/ronin/network/ssl.rb +16 -17
- data/lib/ronin/network/tcp.rb +7 -310
- data/lib/ronin/network/tcp/proxy.rb +417 -0
- data/lib/ronin/network/tcp/tcp.rb +452 -0
- data/lib/ronin/network/telnet.rb +34 -28
- data/lib/ronin/network/udp.rb +7 -271
- data/lib/ronin/network/udp/proxy.rb +191 -0
- data/lib/ronin/network/udp/udp.rb +452 -0
- data/lib/ronin/network/unix.rb +286 -0
- data/lib/ronin/path.rb +35 -39
- data/lib/ronin/spec/ui/output.rb +6 -12
- data/lib/ronin/support.rb +6 -5
- data/lib/ronin/support/inflector.rb +8 -12
- data/lib/ronin/support/support.rb +7 -5
- data/lib/ronin/support/version.rb +6 -6
- data/lib/ronin/templates.rb +5 -5
- data/lib/ronin/templates/erb.rb +5 -5
- data/lib/ronin/templates/template.rb +5 -5
- data/lib/ronin/ui/output.rb +5 -5
- data/lib/ronin/ui/output/helpers.rb +42 -28
- data/lib/ronin/ui/output/output.rb +17 -21
- data/lib/ronin/ui/output/terminal.rb +5 -5
- data/lib/ronin/ui/output/terminal/color.rb +15 -9
- data/lib/ronin/ui/output/terminal/raw.rb +5 -5
- data/lib/ronin/ui/shell.rb +8 -11
- data/lib/ronin/wordlist.rb +110 -30
- data/ronin-support.gemspec +39 -109
- data/spec/binary/hexdump/helpers/hexdumps.rb +13 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/ascii.bin +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_decimal_shorts.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_shorts.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_shorts.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_repeated.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_ints.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_quads.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_shorts.txt +0 -0
- data/spec/binary/hexdump/helpers/hexdumps/od_doubles.txt +17 -0
- data/spec/binary/hexdump/helpers/hexdumps/od_floats.txt +17 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_ints.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_quads.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_shorts.txt +0 -0
- data/spec/binary/hexdump/helpers/hexdumps/od_named_chars.txt +17 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_ints.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_quads.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_shorts.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_repeated.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/repeated.bin +0 -0
- data/spec/binary/hexdump/parser_spec.rb +302 -0
- data/spec/binary/struct_spec.rb +496 -0
- data/spec/binary/template_spec.rb +416 -0
- data/spec/extensions/enumerable_spec.rb +4 -4
- data/spec/extensions/file_spec.rb +12 -14
- data/spec/extensions/ip_addr_spec.rb +76 -50
- data/spec/extensions/kernel_spec.rb +7 -7
- data/spec/extensions/regexp_spec.rb +119 -59
- data/spec/extensions/resolv_spec.rb +2 -2
- data/spec/extensions/string_spec.rb +31 -30
- data/spec/formatting/binary/array_spec.rb +26 -0
- data/spec/formatting/binary/base64_spec.rb +50 -0
- data/spec/formatting/binary/float_spec.rb +36 -0
- data/spec/formatting/binary/integer_spec.rb +76 -50
- data/spec/formatting/binary/string_spec.rb +91 -198
- data/spec/formatting/digest/string_spec.rb +5 -5
- data/spec/formatting/html/integer_spec.rb +6 -6
- data/spec/formatting/html/string_spec.rb +10 -10
- data/spec/formatting/http/integer_spec.rb +3 -3
- data/spec/formatting/http/string_spec.rb +5 -5
- data/spec/formatting/sql/string_spec.rb +21 -19
- data/spec/formatting/text/array_spec.rb +15 -15
- data/spec/formatting/text/string_spec.rb +58 -28
- data/spec/fuzzing/extensions/string_spec.rb +87 -0
- data/spec/fuzzing/fuzzer_spec.rb +109 -0
- data/spec/fuzzing/fuzzing_spec.rb +24 -0
- data/spec/fuzzing/mutator_spec.rb +112 -0
- data/spec/fuzzing/repeater_spec.rb +57 -0
- data/spec/fuzzing/template_spec.rb +54 -0
- data/spec/mixin_spec.rb +10 -12
- data/spec/network/dns_spec.rb +89 -23
- data/spec/network/ftp_spec.rb +81 -0
- data/spec/network/http/http_spec.rb +237 -144
- data/spec/network/http/proxy_spec.rb +37 -37
- data/spec/network/network_spec.rb +2 -2
- data/spec/network/proxy_spec.rb +121 -0
- data/spec/network/shared/unix_server.rb +31 -0
- data/spec/network/smtp/email_spec.rb +14 -14
- data/spec/network/ssl_spec.rb +53 -3
- data/spec/network/tcp/proxy_spec.rb +118 -0
- data/spec/network/tcp/tcp_spec.rb +316 -0
- data/spec/network/telnet_spec.rb +67 -0
- data/spec/network/udp/udp_spec.rb +298 -0
- data/spec/network/unix_spec.rb +182 -0
- data/spec/path_spec.rb +43 -18
- data/spec/spec_helper.rb +2 -3
- data/spec/support/inflector_spec.rb +4 -4
- data/spec/support_spec.rb +1 -1
- data/spec/templates/erb_spec.rb +3 -3
- data/spec/templates/template_spec.rb +10 -10
- data/spec/ui/shell_spec.rb +15 -15
- data/spec/wordlist_spec.rb +80 -19
- metadata +176 -121
- data/.gemtest +0 -0
- data/spec/formatting/binary/helpers/hexdumps.rb +0 -16
- data/spec/fuzzing/string_spec.rb +0 -158
- data/spec/network/tcp_spec.rb +0 -247
- data/spec/network/udp_spec.rb +0 -248
@@ -0,0 +1,454 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (c) 2006-2021 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 <https://www.gnu.org/licenses/>.
|
18
|
+
#
|
19
|
+
|
20
|
+
require 'set'
|
21
|
+
|
22
|
+
module Ronin
|
23
|
+
module Binary
|
24
|
+
#
|
25
|
+
# Provides a translation layer between C-types and Ruby `Array#pack`
|
26
|
+
# codes.
|
27
|
+
#
|
28
|
+
# ## Types
|
29
|
+
#
|
30
|
+
# * `:uint8` (`C`) - unsigned 8-bit integer.
|
31
|
+
# * `:uint16` (`S`) - unsigned 16-bit integer.
|
32
|
+
# * `:uint32` (`L`) - unsigned 32-bit integer.
|
33
|
+
# * `:uint64` (`Q`) - unsigned 64-bit integer.
|
34
|
+
# * `:int8` (`c`) - signed 8-bit integer.
|
35
|
+
# * `:int16` (`s`) - signed 16-bit integer.
|
36
|
+
# * `:int32` (`l`) - signed 32-bit integer.
|
37
|
+
# * `:int64` (`q`) - signed 64-bit integer.
|
38
|
+
# * `:uint16_le` (`v`) - unsigned 16-bit integer, little endian.
|
39
|
+
# * `:uint32_le` (`V`) - unsigned 32-bit integer, little endian.
|
40
|
+
# * `:uint16_be` (`n`) - unsigned 16-bit integer, big endian.
|
41
|
+
# * `:uint32_be` (`N`) - unsigned 32-bit integer, big endian.
|
42
|
+
# * `:uchar` (`Z`) - unsigned character.
|
43
|
+
# * `:ushort` (`S!`) - unsigned short integer, native endian.
|
44
|
+
# * `:uint` (`I!`) - unsigned integer, native endian.
|
45
|
+
# * `:ulong` (`L!`) - unsigned long integer, native endian.
|
46
|
+
# * `:ulong_long` (`Q`) - unsigned quad integer, native endian.
|
47
|
+
# * `:char` (`Z`) - signed character.
|
48
|
+
# * `:short` (`s!`) - signed short integer, native endian.
|
49
|
+
# * `:int` (`i!`) - signed integer, native endian.
|
50
|
+
# * `:long` (`l!`) - signed long integer, native endian.
|
51
|
+
# * `:long_long` (`q`) - signed quad integer, native endian.
|
52
|
+
# * `:utf8` (`U`) - UTF8 character.
|
53
|
+
# * `:float` (`F`) - single-precision float, native format.
|
54
|
+
# * `:double` (`D`) - double-precision float, native format.
|
55
|
+
# * `:float_le` (`e`) - single-precision float, little endian.
|
56
|
+
# * `:double_le` (`E`) - double-precision float, little endian.
|
57
|
+
# * `:float_be` (`g`) - single-precision float, big endian.
|
58
|
+
# * `:double_be` (`G`) - double-precision float, big endian.
|
59
|
+
# * `:ubyte` (`C`) - unsigned byte.
|
60
|
+
# * `:byte` (`c`) - signed byte.
|
61
|
+
# * `:string` (`Z*`) - binary String, `\0` terminated.
|
62
|
+
#
|
63
|
+
# ### Ruby 1.9 specific C-types
|
64
|
+
#
|
65
|
+
# * `:uint16_le` (`S<`) - unsigned 16-bit integer, little endian.
|
66
|
+
# * `:uint32_le` (`L<`) - unsigned 32-bit integer, little endian.
|
67
|
+
# * `:uint64_le` (`Q<`) - unsigned 64-bit integer, little endian.
|
68
|
+
# * `:int16_le` (`s<`) - signed 16-bit integer, little endian.
|
69
|
+
# * `:int32_le` (`l<`) - signed 32-bit integer, little endian.
|
70
|
+
# * `:int64_le` (`q<`) - signed 64-bit integer, little endian.
|
71
|
+
# * `:uint16_be` (`S>`) - unsigned 16-bit integer, big endian.
|
72
|
+
# * `:uint32_be` (`L>`) - unsigned 32-bit integer, big endian.
|
73
|
+
# * `:uint64_be` (`Q>`) - unsigned 64-bit integer, big endian.
|
74
|
+
# * `:int16_be` (`s>`) - signed 16-bit integer, big endian.
|
75
|
+
# * `:int32_be` (`l>`) - signed 32-bit integer, big endian.
|
76
|
+
# * `:int64_be` (`q>`) - signed 64-bit integer, big endian.
|
77
|
+
# * `:ushort_le` (`S<`) - unsigned short integer, little endian.
|
78
|
+
# * `:uint_le` (`I<`) - unsigned integer, little endian.
|
79
|
+
# * `:ulong_le` (`L<`) - unsigned long integer, little endian.
|
80
|
+
# * `:ulong_long_le` (`Q<`) - unsigned quad integer, little endian.
|
81
|
+
# * `:short_le` (`s<`) - signed short integer, little endian.
|
82
|
+
# * `:int_le` (`i<`) - signed integer, little endian.
|
83
|
+
# * `:long_le` (`l<`) - signed long integer, little endian.
|
84
|
+
# * `:long_long_le` (`q<`) - signed quad integer, little endian.
|
85
|
+
# * `:ushort_be` (`S>`) - unsigned short integer, little endian.
|
86
|
+
# * `:uint_be` (`I>`) - unsigned integer, little endian.
|
87
|
+
# * `:ulong_be` (`L>`) - unsigned long integer, little endian.
|
88
|
+
# * `:ulong_long_be` (`Q>`) - unsigned quad integer, little endian.
|
89
|
+
# * `:short_be` (`s>`) - signed short integer, little endian.
|
90
|
+
# * `:int_be` (`i>`) - signed integer, little endian.
|
91
|
+
# * `:long_be` (`l>`) - signed long integer, little endian.
|
92
|
+
# * `:long_long_be` (`q>`) - signed quad integer, little endian.
|
93
|
+
#
|
94
|
+
# @see https://rubydoc.info/stdlib/core/Array:pack
|
95
|
+
#
|
96
|
+
# @api semipbulic
|
97
|
+
#
|
98
|
+
# @since 0.5.0
|
99
|
+
#
|
100
|
+
class Template
|
101
|
+
|
102
|
+
# Supported C-types and corresponding `Array#pack` codes.
|
103
|
+
TYPES = {
|
104
|
+
:uint8 => 'C',
|
105
|
+
:uint16 => 'S',
|
106
|
+
:uint32 => 'L',
|
107
|
+
:uint64 => 'Q',
|
108
|
+
|
109
|
+
:int8 => 'c',
|
110
|
+
:int16 => 's',
|
111
|
+
:int32 => 'l',
|
112
|
+
:int64 => 'q',
|
113
|
+
|
114
|
+
:uint16_le => 'v',
|
115
|
+
:uint32_le => 'V',
|
116
|
+
:uint16_be => 'n',
|
117
|
+
:uint32_be => 'N',
|
118
|
+
|
119
|
+
:uchar => 'Z',
|
120
|
+
:ushort => 'S!',
|
121
|
+
:uint => 'I!',
|
122
|
+
:ulong => 'L!',
|
123
|
+
:ulong_long => 'Q',
|
124
|
+
|
125
|
+
:char => 'Z',
|
126
|
+
:short => 's!',
|
127
|
+
:int => 'i!',
|
128
|
+
:long => 'l!',
|
129
|
+
:long_long => 'q',
|
130
|
+
|
131
|
+
:utf8 => 'U',
|
132
|
+
|
133
|
+
:float => 'F',
|
134
|
+
:double => 'D',
|
135
|
+
|
136
|
+
:float_le => 'e',
|
137
|
+
:double_le => 'E',
|
138
|
+
|
139
|
+
:float_be => 'g',
|
140
|
+
:double_be => 'G',
|
141
|
+
|
142
|
+
:ubyte => 'C',
|
143
|
+
:byte => 'c',
|
144
|
+
:string => 'Z*'
|
145
|
+
}
|
146
|
+
|
147
|
+
# Additional C-types, not available on Ruby 1.8:
|
148
|
+
if RUBY_VERSION > '1.9.'
|
149
|
+
TYPES.merge!(
|
150
|
+
:uint16_le => 'S<',
|
151
|
+
:uint32_le => 'L<',
|
152
|
+
:uint64_le => 'Q<',
|
153
|
+
|
154
|
+
:int16_le => 's<',
|
155
|
+
:int32_le => 'l<',
|
156
|
+
:int64_le => 'q<',
|
157
|
+
|
158
|
+
:uint16_be => 'S>',
|
159
|
+
:uint32_be => 'L>',
|
160
|
+
:uint64_be => 'Q>',
|
161
|
+
|
162
|
+
:int16_be => 's>',
|
163
|
+
:int32_be => 'l>',
|
164
|
+
:int64_be => 'q>',
|
165
|
+
|
166
|
+
:ushort_le => 'S!<',
|
167
|
+
:uint_le => 'I!<',
|
168
|
+
:ulong_le => 'L!<',
|
169
|
+
:ulong_long_le => 'Q<',
|
170
|
+
|
171
|
+
:short_le => 's!<',
|
172
|
+
:int_le => 'i!<',
|
173
|
+
:long_le => 'l!<',
|
174
|
+
:long_long_le => 'q<',
|
175
|
+
|
176
|
+
:ushort_be => 'S!>',
|
177
|
+
:uint_be => 'I!>',
|
178
|
+
:ulong_be => 'L!>',
|
179
|
+
:ulong_long_be => 'Q>',
|
180
|
+
|
181
|
+
:short_be => 's!>',
|
182
|
+
:int_be => 'i!>',
|
183
|
+
:long_be => 'l!>',
|
184
|
+
:long_long_be => 'q>'
|
185
|
+
)
|
186
|
+
end
|
187
|
+
|
188
|
+
# Integer C-types
|
189
|
+
INT_TYPES = Set[
|
190
|
+
:uint8,
|
191
|
+
:uint16,
|
192
|
+
:uint32,
|
193
|
+
:uint64,
|
194
|
+
|
195
|
+
:int8,
|
196
|
+
:int16,
|
197
|
+
:int32,
|
198
|
+
:int64,
|
199
|
+
|
200
|
+
:ubyte,
|
201
|
+
:ushort,
|
202
|
+
:uint,
|
203
|
+
:ulong,
|
204
|
+
:ulong_long,
|
205
|
+
|
206
|
+
:byte,
|
207
|
+
:short,
|
208
|
+
:int,
|
209
|
+
:long,
|
210
|
+
:long_long,
|
211
|
+
|
212
|
+
:uint16_le,
|
213
|
+
:uint32_le,
|
214
|
+
:uint64_le,
|
215
|
+
|
216
|
+
:int16_le,
|
217
|
+
:int32_le,
|
218
|
+
:int64_le,
|
219
|
+
|
220
|
+
:ushort_le,
|
221
|
+
:uint_le,
|
222
|
+
:ulong_le,
|
223
|
+
:ulong_long_le,
|
224
|
+
|
225
|
+
:short_le,
|
226
|
+
:int_le,
|
227
|
+
:long_le,
|
228
|
+
:long_long_le,
|
229
|
+
|
230
|
+
:uint16_be,
|
231
|
+
:uint32_be,
|
232
|
+
:uint64_be,
|
233
|
+
|
234
|
+
:int16_be,
|
235
|
+
:int32_be,
|
236
|
+
:int64_be,
|
237
|
+
|
238
|
+
:ushort_be,
|
239
|
+
:uint_be,
|
240
|
+
:ulong_be,
|
241
|
+
:ulong_long_be,
|
242
|
+
|
243
|
+
:short_be,
|
244
|
+
:int_be,
|
245
|
+
:long_be,
|
246
|
+
:long_long_be
|
247
|
+
]
|
248
|
+
|
249
|
+
# Float C-types
|
250
|
+
FLOAT_TYPES = Set[
|
251
|
+
:float, :double,
|
252
|
+
:float_le, :double_le,
|
253
|
+
:float_be, :double_be
|
254
|
+
]
|
255
|
+
|
256
|
+
# Character C-types
|
257
|
+
CHAR_TYPES = Set[:uchar, :char]
|
258
|
+
|
259
|
+
# String C-types
|
260
|
+
STRING_TYPES = CHAR_TYPES + Set[:string]
|
261
|
+
|
262
|
+
# Types which have little and big endian forms
|
263
|
+
ENDIAN_TYPES = Set[
|
264
|
+
:uint16, :uint32, :uint64,
|
265
|
+
:int16, :int32, :int64,
|
266
|
+
:ushort, :uint, :ulong, :ulong_long,
|
267
|
+
:short, :int, :long, :long_long,
|
268
|
+
:float, :double
|
269
|
+
]
|
270
|
+
|
271
|
+
# The fields of the template
|
272
|
+
attr_reader :fields
|
273
|
+
|
274
|
+
#
|
275
|
+
# Creates a new Binary Template.
|
276
|
+
#
|
277
|
+
# @param [Array<type, (type, length)>] fields
|
278
|
+
# The C-types which the packer will use.
|
279
|
+
#
|
280
|
+
# @param [Hash] options
|
281
|
+
# Template options.
|
282
|
+
#
|
283
|
+
# @option options [:little, :big, :network] :endian
|
284
|
+
# The endianness to apply to the C-types.
|
285
|
+
#
|
286
|
+
# @raise [ArgumentError]
|
287
|
+
# A given type is not known.
|
288
|
+
#
|
289
|
+
# @note
|
290
|
+
# The following C-types are **not supported** on Ruby 1.8:
|
291
|
+
#
|
292
|
+
# * `:uint16_le`
|
293
|
+
# * `:uint32_le`
|
294
|
+
# * `:uint64_le`
|
295
|
+
# * `:int16_le`
|
296
|
+
# * `:int32_le`
|
297
|
+
# * `:int64_le`
|
298
|
+
# * `:uint16_be`
|
299
|
+
# * `:uint32_be`
|
300
|
+
# * `:uint64_be`
|
301
|
+
# * `:int16_be`
|
302
|
+
# * `:int32_be`
|
303
|
+
# * `:int64_be`
|
304
|
+
# * `:ushort_le`
|
305
|
+
# * `:uint_le`
|
306
|
+
# * `:ulong_le`
|
307
|
+
# * `:ulong_long_le`
|
308
|
+
# * `:short_le`
|
309
|
+
# * `:int_le`
|
310
|
+
# * `:long_le`
|
311
|
+
# * `:long_long_le`
|
312
|
+
# * `:ushort_be`
|
313
|
+
# * `:uint_be`
|
314
|
+
# * `:ulong_be`
|
315
|
+
# * `:ulong_long_be`
|
316
|
+
# * `:short_be`
|
317
|
+
# * `:int_be`
|
318
|
+
# * `:long_be`
|
319
|
+
# * `:long_long_be`
|
320
|
+
#
|
321
|
+
# @example
|
322
|
+
# Template.new(:uint32, [:char, 100])
|
323
|
+
#
|
324
|
+
def initialize(fields,options={})
|
325
|
+
@fields = fields
|
326
|
+
@template = self.class.compile(@fields,options)
|
327
|
+
end
|
328
|
+
|
329
|
+
#
|
330
|
+
# @see #initialize
|
331
|
+
#
|
332
|
+
def self.[](*fields)
|
333
|
+
new(fields)
|
334
|
+
end
|
335
|
+
|
336
|
+
#
|
337
|
+
# Translates the type of the field.
|
338
|
+
#
|
339
|
+
# @param [Symbol] type
|
340
|
+
# The type to translate.
|
341
|
+
#
|
342
|
+
# @param [Hash] options
|
343
|
+
# Translation options.
|
344
|
+
#
|
345
|
+
# @option options [:little, :big, :network] :endian
|
346
|
+
# The endianness to apply to the C-types.
|
347
|
+
#
|
348
|
+
# @return [Symbol]
|
349
|
+
# The translated type.
|
350
|
+
#
|
351
|
+
# @raise [ArgumentError]
|
352
|
+
# The value of `:endian` is unknown.
|
353
|
+
#
|
354
|
+
def self.translate(type,options={})
|
355
|
+
if (options[:endian] && ENDIAN_TYPES.include?(type))
|
356
|
+
type = case options[:endian]
|
357
|
+
when :little then :"#{type}_le"
|
358
|
+
when :big, :network then :"#{type}_be"
|
359
|
+
else
|
360
|
+
raise(ArgumentError,"unknown endianness: #{type}")
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
return type
|
365
|
+
end
|
366
|
+
|
367
|
+
#
|
368
|
+
# Compiles C-types into an `Array#pack` / `String#unpack`
|
369
|
+
# template.
|
370
|
+
#
|
371
|
+
# @param [Array<type, (type, length)>] types
|
372
|
+
# The C-types which the packer will use.
|
373
|
+
#
|
374
|
+
# @param [Hash] options
|
375
|
+
# Type options.
|
376
|
+
#
|
377
|
+
# @option options [:little, :big, :network] :endian
|
378
|
+
# The endianness to apply to the C-types.
|
379
|
+
#
|
380
|
+
# @return [String]
|
381
|
+
# The `Array#pack` / `String#unpack` template.
|
382
|
+
#
|
383
|
+
# @raise [ArgumentError]
|
384
|
+
# A given type is not known.
|
385
|
+
#
|
386
|
+
def self.compile(types,options={})
|
387
|
+
string = ''
|
388
|
+
|
389
|
+
types.each do |(type,length)|
|
390
|
+
type = translate(type,options)
|
391
|
+
|
392
|
+
unless (code = TYPES[type])
|
393
|
+
raise(ArgumentError,"#{type.inspect} not supported")
|
394
|
+
end
|
395
|
+
|
396
|
+
string << code << length.to_s
|
397
|
+
end
|
398
|
+
|
399
|
+
return string
|
400
|
+
end
|
401
|
+
|
402
|
+
#
|
403
|
+
# Packs the data.
|
404
|
+
#
|
405
|
+
# @param [Array] data
|
406
|
+
# The data to pack.
|
407
|
+
#
|
408
|
+
# @return [String]
|
409
|
+
# The packed data.
|
410
|
+
#
|
411
|
+
def pack(*data)
|
412
|
+
data.pack(@template)
|
413
|
+
end
|
414
|
+
|
415
|
+
#
|
416
|
+
# Unpacks the string.
|
417
|
+
#
|
418
|
+
# @param [String] string
|
419
|
+
# The raw String to unpack.
|
420
|
+
#
|
421
|
+
# @return [Array]
|
422
|
+
# The unpacked data.
|
423
|
+
#
|
424
|
+
def unpack(string)
|
425
|
+
string.unpack(@template)
|
426
|
+
end
|
427
|
+
|
428
|
+
#
|
429
|
+
# Converts the template to a `Array#pack` template String.
|
430
|
+
#
|
431
|
+
# @return [String]
|
432
|
+
# The template String.
|
433
|
+
#
|
434
|
+
# @see https://rubydoc.info/stdlib/core/Array:pack
|
435
|
+
#
|
436
|
+
def to_s
|
437
|
+
@template
|
438
|
+
end
|
439
|
+
|
440
|
+
#
|
441
|
+
# Inspects the template.
|
442
|
+
#
|
443
|
+
# @return [String]
|
444
|
+
# The inspected template.
|
445
|
+
#
|
446
|
+
# @since 1.5.1
|
447
|
+
#
|
448
|
+
def inspect
|
449
|
+
"<#{self.class}: #{@fields.inspect}>"
|
450
|
+
end
|
451
|
+
|
452
|
+
end
|
453
|
+
end
|
454
|
+
end
|