Class: Arpie::ListBinaryType

Inherits:
BinaryType show all
Defined in:
lib/arpie/binary/list_type.rb

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!

Instance Method Details

#binary_size(opts) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/arpie/binary/list_type.rb', line 4

def binary_size opts
  if opts[:sizeof]
    len_handler = Binary.get_type_handler(opts[:sizeof])
    len_handler.binary_size(opts[:sizeof_opts])
  elsif opts[:length]
    case opts[:length]
      when Symbol
        opts[:object] ? opts[:object].send(opts[:length]) : nil
      else
        opts[:length]
    end
  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
46
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
72
73
74
75
76
77
78
# File 'lib/arpie/binary/list_type.rb', line 20

def from binary, opts
  type_of = Binary.get_type_handler(opts[:of])
  type_of_binary_size = nil
  if type_of.respond_to?(:binary_size)
    type_of_binary_size = type_of.binary_size(opts[:of_opts]) rescue nil
  end

  list = []
  consumed = 0
  length = nil

  if opts[:sizeof]
    len_handler = Binary.get_type_handler(opts[:sizeof])
    length, ate = len_handler.from(binary, opts[:sizeof_opts])
    consumed += ate

  elsif opts[:length]
    length = case opts[:length]
      when :all
        if type_of_binary_size
          binary.size / type_of_binary_size
        else
          nil
        end

      when Symbol
        opts[:object].send(opts[:length])

      else
        opts[:length]

      end
  else
    raise ArgumentError, "need one of [:sizeof, :length]"
  end

  cc, ate = nil, nil



  if length.nil?
    loop do
      nextdata = binary[consumed .. -1]
      break if !nextdata || nextdata == ""
      cc, ate = type_of.from(binary[consumed .. -1], opts[:of_opts])
      list << cc
      consumed += ate
    end

  else
    for i in 0...length do
      cc, ate = type_of.from(binary[consumed .. -1], opts[:of_opts])
      list << cc
      consumed += ate
    end
  end

  [list, consumed]
end

#to(object, opts) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/arpie/binary/list_type.rb', line 80

def to object, opts
  object.is_a?(Array) or bogon! object, "require Array"

  type_of = Binary.get_type_handler(opts[:of])

  if opts[:sizeof]
    len_handler = Binary.get_type_handler(opts[:sizeof])
    ([len_handler.to(object.size, opts[:sizeof_opts])] + object.map {|o|
      type_of.to(o, opts[:of_opts])
    }).join('')

  elsif opts[:length]
    length = case opts[:length]
      when :all
        object.size
      when Symbol
        object.size
      else
        opts[:length]
    end

    object.size == length or bogon! object,
      "Array#size does not match required fixed width: " +
      "have #{object.size}, require #{length.inspect}"

    object.map {|o|
      type_of.to(o, opts[:of_opts])
    }.join('')

  else
    raise ArgumentError, "need one of [:sizeof, :length]"
  end

end