bin_struct 0.1.0 → 0.2.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -2
- data/README.md +1 -1
- data/lib/bin_struct/abstract_tlv.rb +75 -73
- data/lib/bin_struct/array.rb +35 -26
- data/lib/bin_struct/cstring.rb +62 -11
- data/lib/bin_struct/enum.rb +35 -25
- data/lib/bin_struct/int.rb +59 -77
- data/lib/bin_struct/int_string.rb +19 -12
- data/lib/bin_struct/length_from.rb +6 -6
- data/lib/bin_struct/oui.rb +13 -10
- data/lib/bin_struct/string.rb +24 -12
- data/lib/bin_struct/struct.rb +628 -0
- data/lib/bin_struct/{fieldable.rb → structable.rb} +15 -15
- data/lib/bin_struct/version.rb +2 -1
- data/lib/bin_struct.rb +2 -2
- metadata +4 -4
- data/lib/bin_struct/fields.rb +0 -612
|
@@ -10,16 +10,16 @@ module BinStruct
|
|
|
10
10
|
# This module is a mixin adding +length_from+ capacity to a type.
|
|
11
11
|
# +length_from+ capacity is the capacity, for a type, to gets its
|
|
12
12
|
# length from another object.
|
|
13
|
-
# @author Sylvain Daubert
|
|
14
|
-
# @
|
|
13
|
+
# @author Sylvain Daubert (2016-2024)
|
|
14
|
+
# @author LemonTree55
|
|
15
15
|
module LengthFrom
|
|
16
16
|
# Max value returned by {#sz_to_read}.
|
|
17
17
|
MAX_SZ_TO_READ = 65_535
|
|
18
18
|
|
|
19
|
-
# Initialize +
|
|
20
|
-
# Should be
|
|
19
|
+
# Initialize +length_from+ capacity.
|
|
20
|
+
# Should be called by extended object's initialize method.
|
|
21
21
|
# @param [Hash] options
|
|
22
|
-
# @option options [
|
|
22
|
+
# @option options [Int,Proc] :length_from object or proc from which
|
|
23
23
|
# takes length when reading
|
|
24
24
|
# @return [void]
|
|
25
25
|
def initialize_length_from(options)
|
|
@@ -28,7 +28,7 @@ module BinStruct
|
|
|
28
28
|
|
|
29
29
|
# Return a substring from +str+ of length given in another object.
|
|
30
30
|
# @param [#to_s] str
|
|
31
|
-
# @return [String]
|
|
31
|
+
# @return [::String]
|
|
32
32
|
def read_with_length_from(str)
|
|
33
33
|
s = BinStruct.force_binary(str.to_s)
|
|
34
34
|
s[0, sz_to_read]
|
data/lib/bin_struct/oui.rb
CHANGED
|
@@ -11,23 +11,26 @@ module BinStruct
|
|
|
11
11
|
# oui = OUI.new
|
|
12
12
|
# oui.from_human('00:01:02')
|
|
13
13
|
# oui.to_human # => "00:01:02"
|
|
14
|
-
#
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
# oui.to_s # => "\x00\x01\x03"
|
|
15
|
+
# @author Sylvain Daubert (2016-2024)
|
|
16
|
+
# @author LemonTree55
|
|
17
|
+
class OUI < Struct
|
|
18
|
+
include Structable
|
|
17
19
|
|
|
18
20
|
# @attribute b2
|
|
19
21
|
# @return [Integer] left-most byte
|
|
20
|
-
|
|
22
|
+
define_attr :b2, Int8
|
|
21
23
|
# @attribute b1
|
|
22
24
|
# @return [Integer] center byte
|
|
23
|
-
|
|
25
|
+
define_attr :b1, Int8
|
|
24
26
|
# @attribute b0
|
|
25
27
|
# @return [Integer] right-most byte
|
|
26
|
-
|
|
28
|
+
define_attr :b0, Int8
|
|
27
29
|
|
|
28
30
|
# Read a human-readable string to populate object
|
|
29
|
-
# @param [String] str
|
|
30
|
-
# @return [
|
|
31
|
+
# @param [::String] str
|
|
32
|
+
# @return [self]
|
|
33
|
+
# @raise [ArgumentError] OUI cannot be recognized from +str+
|
|
31
34
|
def from_human(str)
|
|
32
35
|
return self if str.nil?
|
|
33
36
|
|
|
@@ -41,9 +44,9 @@ module BinStruct
|
|
|
41
44
|
end
|
|
42
45
|
|
|
43
46
|
# Get OUI in human readable form (colon-separated bytes)
|
|
44
|
-
# @return [String]
|
|
47
|
+
# @return [::String]
|
|
45
48
|
def to_human
|
|
46
|
-
|
|
49
|
+
attributes.map { |m| '%02x' % self[m] }.join(':')
|
|
47
50
|
end
|
|
48
51
|
end
|
|
49
52
|
end
|
data/lib/bin_struct/string.rb
CHANGED
|
@@ -9,25 +9,27 @@
|
|
|
9
9
|
require 'forwardable'
|
|
10
10
|
|
|
11
11
|
module BinStruct
|
|
12
|
-
# This class mimics regular String, but it is {
|
|
13
|
-
# @author Sylvain Daubert
|
|
14
|
-
# @
|
|
12
|
+
# This class mimics regular String, but it is {Structable}.
|
|
13
|
+
# @author Sylvain Daubert (2016-2024)
|
|
14
|
+
# @author LemonTree55
|
|
15
15
|
class String
|
|
16
16
|
extend Forwardable
|
|
17
|
-
include
|
|
17
|
+
include Structable
|
|
18
18
|
include LengthFrom
|
|
19
19
|
|
|
20
|
-
def_delegators :@string, :[], :
|
|
20
|
+
def_delegators :@string, :[], :length, :size, :inspect, :==,
|
|
21
21
|
:unpack, :force_encoding, :encoding, :index, :empty?,
|
|
22
22
|
:encode, :slice, :slice!, :[]=
|
|
23
23
|
|
|
24
|
+
# Underlying Ruby String
|
|
24
25
|
# @return [::String]
|
|
25
26
|
attr_reader :string
|
|
27
|
+
# String static length, if set
|
|
26
28
|
# @return [Integer]
|
|
27
29
|
attr_reader :static_length
|
|
28
30
|
|
|
29
31
|
# @param [Hash] options
|
|
30
|
-
# @option options [
|
|
32
|
+
# @option options [Int,Proc] :length_from object or proc from which
|
|
31
33
|
# takes length when reading
|
|
32
34
|
# @option options [Integer] :static_length set a static length for this string
|
|
33
35
|
def initialize(options = {})
|
|
@@ -36,15 +38,19 @@ module BinStruct
|
|
|
36
38
|
@static_length = options[:static_length]
|
|
37
39
|
end
|
|
38
40
|
|
|
41
|
+
# Initialize object on copying:
|
|
42
|
+
# * duplicate underlying Ruby String
|
|
43
|
+
# @return [void]
|
|
39
44
|
def initialize_copy(_orig)
|
|
40
45
|
@string = @string.dup
|
|
41
46
|
end
|
|
42
47
|
|
|
48
|
+
# Populate String from a binary String. Limit length using {LengthFrom} or {#static_length}, if one is set.
|
|
43
49
|
# @param [::String] str
|
|
44
|
-
# @return [
|
|
50
|
+
# @return [self]
|
|
45
51
|
def read(str)
|
|
46
52
|
s = read_with_length_from(str)
|
|
47
|
-
register_internal_string
|
|
53
|
+
register_internal_string(s)
|
|
48
54
|
self
|
|
49
55
|
end
|
|
50
56
|
|
|
@@ -52,9 +58,8 @@ module BinStruct
|
|
|
52
58
|
private :old_sz_to_read
|
|
53
59
|
|
|
54
60
|
# Size to read.
|
|
55
|
-
# Computed from static_length or length_from
|
|
61
|
+
# Computed from {#static_length} or +length_from+, if one defined.
|
|
56
62
|
# @return [Integer]
|
|
57
|
-
# @since 3.1.6
|
|
58
63
|
def sz_to_read
|
|
59
64
|
return static_length if static_length?
|
|
60
65
|
|
|
@@ -63,11 +68,12 @@ module BinStruct
|
|
|
63
68
|
|
|
64
69
|
# Say if a static length is defined
|
|
65
70
|
# @return [Boolean]
|
|
66
|
-
# @since 3.1.6
|
|
67
71
|
def static_length?
|
|
68
72
|
!static_length.nil?
|
|
69
73
|
end
|
|
70
74
|
|
|
75
|
+
# Format String when inspecting from a {Struct}
|
|
76
|
+
# @return [::String]
|
|
71
77
|
def format_inspect
|
|
72
78
|
inspect
|
|
73
79
|
end
|
|
@@ -76,10 +82,16 @@ module BinStruct
|
|
|
76
82
|
# @param [#to_s] str
|
|
77
83
|
# @return [self]
|
|
78
84
|
def <<(str)
|
|
79
|
-
@string << str.to_s
|
|
85
|
+
@string << BinStruct.force_binary(str.to_s)
|
|
80
86
|
self
|
|
81
87
|
end
|
|
82
88
|
|
|
89
|
+
# Generate binary string
|
|
90
|
+
# @return [::String]
|
|
91
|
+
def to_s
|
|
92
|
+
@string
|
|
93
|
+
end
|
|
94
|
+
|
|
83
95
|
alias sz length
|
|
84
96
|
alias to_human to_s
|
|
85
97
|
alias from_human read
|