Class: Arpie::Binary

Inherits:
Object
  • Object
show all
Extended by:
Arpie
Defined in:
lib/arpie/binary.rb,
lib/arpie/binary/fixed_type.rb

Overview

A Binary is a helper to convert arbitary bitlevel binary data to and from ruby Struct-lookalikes.

Here’s an example:

class Test < Arpie::Binary
  describe "I am a test"

  field :a, :uint8
  field :b, :bytes, :sizeof => :nint16
end

This will allow you to parse binary blocks consisting of:

  • a byte

  • a network-order int (16 bit)

  • a string which length’ is the given int

Rinse and repeat.

For all available data types, please look into the source file of this class at the bottom.

Writing new types is easy enough, (see BinaryType).

On confusing names:

Arpie uses the term binary to refer to on-the-wire data bit/byte binary format. A Binary (uppercase) is a object describing the format. If you’re reading binary, think “raw data”; if you’re reading Binary or object, think Arpie::Binary.

Another warning:

Do not use Kernel methods as field names. It’ll confuse method_missing. Example:

field :test, :uint8
=> in `test': wrong number of arguments (ArgumentError)

In fact, this is the reason while Binary will not let you define fields with with names like existing instance methods.

Defined Under Namespace

Classes: Field, Virtual

Constant Summary

Constants included from Arpie

MTU, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Arpie

bogon!, incomplete!

Constructor Details

#initializeBinary

Returns a new instance of Binary.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/arpie/binary.rb', line 62

def initialize
  @fields = {}
  @@fields[self.class] ||= []

  # set up our own class handlers, create anon classes, set up default values
  @@fields[self.class].each {|field|
    if field.inline_handler
      @fields[field.name] = field.inline_handler.new

    elsif field.type.is_a?(Class)
      @fields[field.name] = field.type.new

    elsif field.opts[:default]
      @fields[field.name] = field.opts[:default]
    end
  }
  if block_given?
    yield self
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/arpie/binary.rb', line 99

def method_missing m, *a
  m.to_s =~ /^(.+?)(=?)$/ or super
  at = $1.to_sym
  if self.class.field?(at)
    if $2 == "="
      a.size == 1 or raise ArgumentError
      if !a[0].is_a?(Class) && inline = self.class.get_field(at)[3]
        a[0], __nil = inline.from(a[0], {})
      end
      @fields[at] = a[0]
    else
      @fields[at]
    end

  elsif self.class.virtual?(at)
    if $2 == "="
      raise ArgumentError
    else
      Binary.call_virtual(self, at)
    end

  else
    super
  end
end

Class Method Details

.__anonymousObject



55
56
57
# File 'lib/arpie/binary.rb', line 55

def self.__anonymous
  @@anonymous[self]
end

.__anonymous=(x) ⇒ Object



58
59
60
# File 'lib/arpie/binary.rb', line 58

def self.__anonymous= x
  @@anonymous[self] = x
end

.add_hook(hook, handler) ⇒ Object



324
325
326
327
328
# File 'lib/arpie/binary.rb', line 324

def self.add_hook(hook, handler)
  @@hooks[self] ||= {}
  @@hooks[self][hook] ||= []
  @@hooks[self][hook] << handler
end

.binary_size(opts = {}) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/arpie/binary.rb', line 312

def self.binary_size opts = {}
  @@fields[self] ||= []
  total = @@fields[self].inject(0) {|sum, field|
    klass = get_type_handler(field.type)
    sz = klass.binary_size(field.opts)
    sz or raise "cannot binary_size dynamic Binary definitions"
    sum + sz
  }

  total
end

.call_hooks(hook, *va) ⇒ Object



330
331
332
333
334
335
336
337
# File 'lib/arpie/binary.rb', line 330

def self.call_hooks(hook, *va)
  @@hooks[self] ||= {}
  @@hooks[self][hook] ||= []
  @@hooks[self][hook].each {|handler|
    va = *handler.call(*va)
  }
  va
end

.call_virtual(on_object, name) ⇒ Object



125
126
127
128
129
# File 'lib/arpie/binary.rb', line 125

def self.call_virtual(on_object, name)
  @@virtuals[on_object.class].select {|virtual|
    virtual.name == name
  }[0].handler.call(on_object)
end

.describe(text = nil) ⇒ Object

You can use this to provide a short description of this Binary. It will be shown when calling Binary.inspect. When called without a parameter, (non-recursively) print out a pretty description of this data type as it would appear on the wire.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/arpie/binary.rb', line 187

def self.describe text = nil
  unless text.nil?
    @@description[self] = text
  else
    ret = []
    ret << "%-10s %s" % ["Binary:", @@description[self]]
    ret << ""

    sprf    = "%-10s %-25s %-15s %-15s %-15s %s"
    sprf_of = "%68s %s"
    if @@virtuals[self] && @@virtuals[self].size > 0
      ret << sprf % ["Virtuals:", "NAME", "TYPE", "WIDTH", "", "DESCRIPTION"]
      @@virtuals[self].each {|virtual|
        width = self.get_type_handler(virtual.type).binary_size(virtual.opts)
        ret << sprf % [ "",
          virtual.name,
          virtual.type,
          width,
          "",
          virtual.opts[:description]
        ]
      }
      ret << ""
    end
    if @@fields[self] && @@fields[self].size > 0
      ret << sprf % %w{Fields:   NAME TYPE WIDTH OF DESCRIPTION}
      @@fields[self].each {|field|
        width = self.get_type_handler(field.type).binary_size(field.opts)
        ret << sprf % [ "",
          field.name,
          field.type,
          (field.opts[:length] || field.opts[:sizeof] || width),
          field.opts[:of] ? field.opts[:of].inspect : "",
          field.opts[:description]
        ]
        ret << sprf_of % [ "",
          field.opts[:of_opts].inspect
        ] if field.opts[:of_opts]
      }
    end
    ret.join("\n")
  end
end

.describe_all_typesObject



173
174
175
176
177
178
179
180
181
# File 'lib/arpie/binary.rb', line 173

def self.describe_all_types
  ret = []
  strf = "%-15s %-8s %s"
  ret << strf % %w{TYPE WIDTH HANDLER}
  @@types.sort{|a,b| a[0].to_s <=> b[0].to_s}.each {|type, handler|
    ret << strf % [type.inspect, (handler.binary_size({}) rescue nil), handler.inspect]
  }
  ret.join("\n")
end

.f(*va, &b) ⇒ Object

Alias for field.



290
291
292
# File 'lib/arpie/binary.rb', line 290

def self.f *va, &b
  field *va, &b
end

.field(name, type = nil, opts = {}, &block) ⇒ Object

Specify that this Binary has a field of type type. See the class documentation for usage.

Raises:

  • (ArgumentError)


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/arpie/binary.rb', line 244

def self.field name, type = nil, opts = {}, &block
  @@fields[self] ||= []
  handler = get_type_handler(type)

  raise ArgumentError, "#{name.inspect} already exists as a virtual" if virtual?(name)
  raise ArgumentError, "#{name.inspect} already exists as a field" if field?(name)
  raise ArgumentError, "#{name.inspect} already exists as a instance method" if instance_methods.index(name.to_s)
  raise ArgumentError, "#{name.inspect}: cannot inline classes" if block_given? and type.class === Arpie::Binary

  @@fields[self].each {|field|
    raise ArgumentError, "#{name.inspect}: :optional fields cannot be followed by required fields" if
      field[:opts].include?(:optional)
  } unless opts[:optional]


  type.nil? && !block_given? and raise ArgumentError,
    "You need to specify an inline handler if no type is given."
  inline_handler = nil

  if block_given?
    inline_handler = Class.new(Arpie::Binary)
    inline_handler.__anonymous = [name, type, opts]
    inline_handler.instance_eval(&block)
  end

  if type.nil?
    type, inline_handler = inline_handler, nil
  end

  if handler.respond_to?(:required_opts)
    missing_required = handler.required_opts.keys - opts.keys
    raise ArgumentError, "#{self}: #{name.inspect} as type #{type.inspect} " +
      "requires options: #{missing_required.inspect}" if missing_required.size > 0
    handler.required_opts.each {|k,v|
      v.nil? and next
      v.call(opts[k]) or raise ArgumentError, "#{self}: Invalid value given for opt key #{k.inspect}."
    }
  end

  opts[:description] ||= opts[:desc] if opts[:desc]
  opts.delete(:desc)

  @@fields[self] << Field.new(name.to_sym, type, opts, inline_handler)
end

.field?(field) ⇒ Boolean

Returns true if this Binary has the named field.

Returns:

  • (Boolean)


139
140
141
142
143
144
# File 'lib/arpie/binary.rb', line 139

def self.field? field
  @@fields[self] or return false
  @@fields[self].select {|_field|
    _field.name == field
  }.size > 0
end

.from(binary, opts = {}) ⇒ Object

Parse the given binary, which is a string, and return an instance of this class. Will raise Arpie::EIncomplete when not enough data is available in binary to construct a complete Binary.



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/arpie/binary.rb', line 342

def self.from binary, opts = {}
  @@fields[self] ||= []
  binary, * = * self.call_hooks(:pre_from, binary)

  consumed_bytes = 0
  obj = new
  @@fields[self].each {|field| # name, klass, kopts, inline_handler|
    field.opts[:binary] = binary
    field.opts[:object] = obj
    handler = get_type_handler(field.type)

    attrib, consumed = binary, nil

    attrib, consumed =
      handler.from(binary[consumed_bytes .. -1], field.opts) rescue case $!
      when EIncomplete
        if field.opts[:optional]
          attrib, consumed = field.opts[:default], handler.binary_size(field.opts)
        else
          raise $!,
            "#{$!.to_s}, #{self}#from needs more data for " +
            "#{field.name.inspect}. (data: #{binary[consumed_bytes .. -1].inspect})"
        end
      when StreamError
        bogon! binary[consumed_bytes .. -1], "#{self}#from: #{field.name.inspect}: #{$!.to_s}"
      else
        raise
    end
    consumed_bytes += consumed

    obj.send((field.name.to_s + "=").to_sym, attrib)
    field.opts.delete(:binary)
    field.opts.delete(:object)
  }

  binary, obj, consumed_bytes = self.call_hooks(:post_from, binary, obj, consumed_bytes)
  [obj, consumed_bytes]
end

.get_field(field) ⇒ Object

Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
# File 'lib/arpie/binary.rb', line 146

def self.get_field field
  @@fields[self] or raise ArgumentError, "No such field: #{field.inspect}"
  @@fields[self].each {|_field|
    _field.name == field and return _field
  }
  raise ArgumentError, "No such field: #{field.inspect}"
end

.get_type_handler(type) ⇒ Object

Returns the BinaryType handler class for type, which can be a symbol (:uint8), or a Arpie::Binary.



164
165
166
167
168
169
170
171
# File 'lib/arpie/binary.rb', line 164

def self.get_type_handler type
  if type.class === Arpie::Binary
    type
  else
    @@types[type] or raise ArgumentError,
      "#{self}: No such field type: #{type.inspect}"
  end
end

.method_missing(meth, *va, &block) ⇒ Object

You can use the field type as a class method to create new fields, as if you would call Binary.field with the appropriate parameters.

Examples:

uint8 :nameof, :default => 1
fixed :fixedValue, :value => [1,2,3].pack("CCC")
list :ls, :of => :uint8, :sizeof => :nameof


238
239
240
# File 'lib/arpie/binary.rb', line 238

def self.method_missing meth, *va, &block
  self.field(va.shift, meth, *va, &block)
end

.post_from(&handler) ⇒ Object

Add a hook that gets called after converting a Binary to wire format. Arguments to the handler: binary, object Note that all handlers need to return their arguemts as they were passed, as they will replace the original values.



445
446
447
# File 'lib/arpie/binary.rb', line 445

def self.post_from &handler
  self.add_hook(:post_from, handler)
end

.post_to(&handler) ⇒ Object

Add a hook that gets called after converting a binary to Binary representation. Arguments to the handler: object, binary, consumed_bytes. Note that all handlers need to return their arguemts as they were passed, as they will replace the original values.



429
430
431
# File 'lib/arpie/binary.rb', line 429

def self.post_to &handler
  self.add_hook(:post_to, handler)
end

.pre_from(&handler) ⇒ Object

Add a hook that gets called before converting a Binary to wire format. Arguments to the handler: object Note that all handlers need to return their arguemts as they were passed, as they will replace the original values.



437
438
439
# File 'lib/arpie/binary.rb', line 437

def self.pre_from &handler
  self.add_hook(:pre_from, handler)
end

.pre_to(&handler) ⇒ Object

Add a hook that gets called before converting a binary to Binary representation. Arguments to the handler: binary Note that all handlers need to return their arguemts as they were passed, as they will replace the original values.



421
422
423
# File 'lib/arpie/binary.rb', line 421

def self.pre_to &handler
  self.add_hook(:pre_to, handler)
end

.register_type(handler, *type_aliases) ⇒ Object

This registers a new type with this binary.



132
133
134
135
136
# File 'lib/arpie/binary.rb', line 132

def self.register_type handler, *type_aliases
  type_aliases.each do |type|
    @@types[type] = handler
  end
end

.s(*va, &b) ⇒ Object

An alias for static.



43
44
45
# File 'lib/arpie/binary/fixed_type.rb', line 43

def self.s *va, &b
  static *va, &b
end

.static(name_or_content, content = nil) ⇒ Object

Create a static field. This is an alias for:

field :name, :fixed, :value => content, :default => content

If no name is specified, it is assumed that the user will never want to access it and a suitable one is autogenerated.



31
32
33
34
35
36
37
38
39
40
# File 'lib/arpie/binary/fixed_type.rb', line 31

def self.static name_or_content, content = nil
  if content.nil?
    name = "static-%x" % [name_or_content.hash]
    content = name_or_content
  else
    name = name_or_content
  end

  field name, :fixed, :value => content, :default => content
end

.to(object, opts = {}) ⇒ Object

Recursively convert the given Binary object to wire format.



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/arpie/binary.rb', line 382

def self.to object, opts = {}
  object.nil? and raise ArgumentError, "cannot #to nil"
  @@fields[self] ||= []
  r = []
  object, * = * self.call_hooks(:pre_to, object)

  @@fields[self].each {|field| # name, klass, kopts, inline_handler|
    field.opts[:object] = object
    handler = get_type_handler(field.type)
    val = object.send(field.name)

    if field.inline_handler
      val = val.to
    end

    # r << (val.respond_to?(:to) ? val.to(opts) : handler.to(val, kopts)) rescue case $!
    r << handler.to(val, field.opts) rescue case $!
      when StreamError
        raise $!, "#{self}#from: #{field.name.inspect}: #{$!.to_s}"
      else
        raise
    end
    field.opts.delete(:object)
  }

  r = r.join('')
  _obj, r = self.call_hooks(:post_to, object, r)
  r
end

.v(*va, &b) ⇒ Object

Alias for virtual.



308
309
310
# File 'lib/arpie/binary.rb', line 308

def self.v *va, &b
  virtual *va, &b
end

.virtual(name, type, opts = {}, &handler) ⇒ Object

Set up a new virtual field

Raises:

  • (ArgumentError)


295
296
297
298
299
300
301
302
303
304
305
# File 'lib/arpie/binary.rb', line 295

def self.virtual name, type, opts = {}, &handler
  raise ArgumentError, "You need to pass a block with virtuals" unless block_given?
  raise ArgumentError, "#{name.inspect} already exists as a virtual" if virtual?(name)
  raise ArgumentError, "#{name.inspect} already exists as a field" if field?(name)
  raise ArgumentError, "#{name.inspect} already exists as a instance method" if instance_methods.index(name.to_s)

  @@virtuals[self] ||= []
  opts[:description] ||= opts[:desc]
  opts.delete(:desc)
  @@virtuals[self] << Virtual.new(name.to_sym, type, opts, handler)
end

.virtual?(virtual) ⇒ Boolean

Returns true if this Binary has the named virtual.

Returns:

  • (Boolean)


155
156
157
158
159
160
# File 'lib/arpie/binary.rb', line 155

def self.virtual? virtual
  @@virtuals[self] or return false
  @@virtuals[self].select {|_virtual|
    _virtual.name == virtual
  }.size > 0
end

Instance Method Details

#inspectObject

:nodoc:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/arpie/binary.rb', line 83

def inspect #:nodoc:
  desc = " " + @@description[self.class].inspect if @@description[self.class]
  # Anonymous is special
  klass = self.class.respond_to?(:__anonymous) && self.class.__anonymous ?
    "Anon#{self.class.__anonymous.inspect}" :
    self.class.to_s

  fields = []
  @@fields[self.class].each {|field|
    fields << "%s=>%s" % [field.name.inspect, @fields[field.name].inspect]
  }
  fields = '{' + fields.join(", ") + '}'

  "#<#{klass}#{desc} #{fields}>"
end

#to(opts = {}) ⇒ Object



412
413
414
# File 'lib/arpie/binary.rb', line 412

def to opts = {}
  self.class.to(self, opts)
end