Class: Arpie::BytesBinaryType
Constant Summary
Constants included
from Arpie
MTU
Instance Method Summary
collapse
Methods inherited from BinaryType
#check_limit, #required_opts
Methods included from Arpie
#bogon!, #incomplete!
Constructor Details
#initialize(pack_string, force_opts = {}) ⇒ BytesBinaryType
Returns a new instance of BytesBinaryType.
3
4
5
6
|
# File 'lib/arpie/binary/bytes_type.rb', line 3
def initialize pack_string, force_opts = {}
@pack_string = pack_string
@force_opts = force_opts
end
|
Instance Method Details
#binary_size(opts) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/arpie/binary/bytes_type.rb', line 8
def binary_size opts
opts = @force_opts.merge(opts || {})
if opts[:sizeof]
len_handler = Binary.get_type_handler(opts[:sizeof])
len_handler.binary_size(opts[:sizeof_opts])
elsif opts[:length]
opts[:length]
else
nil
end
end
|
#from(binary, opts) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/arpie/binary/bytes_type.rb', line 20
def from binary, opts
opts = (opts || {}).merge(@force_opts)
if opts[:sizeof]
len_handler = Binary.get_type_handler(opts[:sizeof])
len, len_size = len_handler.from(binary, opts[:sizeof_opts])
binary.size >= len_size + len or incomplete!
[binary.unpack("x#{len_size} #{@pack_string}#{len}")[0], len_size + len]
elsif opts[:length]
len = case opts[:length]
when :all
binary.size
when Symbol
opts[:object].send(opts[:length])
else
opts[:length]
end
binary.size >= len or incomplete!
[binary.unpack("#{@pack_string}#{len}")[0], len]
else
raise ArgumentError, "need one of [:sizeof, :length]"
end
end
|
#to(object, opts) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/arpie/binary/bytes_type.rb', line 47
def to object, opts
opts = (opts || {}).merge(@force_opts)
if opts[:sizeof]
len_handler = Binary.get_type_handler(opts[:sizeof])
len_handler.respond_to?(:pack_string) or raise ArgumentError,
"#{self.class}#to: needs a PackStringType parameter for length"
[object.size, object].pack("#{len_handler.pack_string} #{@pack_string}*")
elsif opts[:length]
len = case opts[:length]
when :all
"*"
when Symbol
"*"
else
opts[:length]
end
[object].pack("#{@pack_string}#{len}")
else
raise ArgumentError, "need one of [:sizeof, :length]"
end
end
|