Class: BinData::Section

Inherits:
Base
  • Object
show all
Extended by:
DSLMixin
Defined in:
lib/bindata/section.rb

Overview

A Section is a layer on top of a stream that transforms the underlying data. This allows BinData to process a stream that has multiple encodings. e.g. Some data data is compressed or encrypted.

require 'bindata'

class XorTransform < BinData::IO::Transform
   def initialize(xor)
     super()
     @xor = xor
   end

   def read(n)
     chain_read(n).bytes.map { |byte| (byte ^ @xor).chr }.join
   end

   def write(data)
     chain_write(data.bytes.map { |byte| (byte ^ @xor).chr }.join)
   end
end

obj = BinData::Section.new(transform: -> { XorTransform.new(0xff) },
                           type: [:string, read_length: 5])

obj.read("\x97\x9A\x93\x93\x90") #=> "hello"

Parameters

Parameters may be provided at initialisation to control the behaviour of an object. These params are:

:transform

A callable that returns a new BinData::IO::Transform.

:type

The single type inside the buffer. Use a struct if multiple fields are required.

Instance Attribute Summary

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods included from DSLMixin

dsl_parser, to_ary, to_str

Methods inherited from Base

#==, #=~, #abs_offset, arg_processor, auto_call_delayed_io, bindata_name, #clear, #debug_name, #eval_parameter, #get_parameter, #has_parameter?, #initialize_with_warning, #inspect, #lazy_evaluator, #new, #num_bytes, #pretty_print, #read, read, register_subclasses, #rel_offset, #safe_respond_to?, #to_binary_s, #to_hex, #to_s, unregister_self, #write

Methods included from AcceptedParametersPlugin

#accepted_parameters, #default_parameters, #mandatory_parameters, #mutually_exclusive_parameters, #optional_parameters

Methods included from RegisterNamePlugin

included, #initialize_shared_instance

Methods included from Framework

#bit_aligned?, #debug_name_of, #offset_of

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

:nodoc:



68
69
70
# File 'lib/bindata/section.rb', line 68

def method_missing(symbol, *args, &block) # :nodoc:
  @type.__send__(symbol, *args, &block)
end

Instance Method Details

#assign(val) ⇒ Object



56
57
58
# File 'lib/bindata/section.rb', line 56

def assign(val)
  @type.assign(val)
end

#clear?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/bindata/section.rb', line 52

def clear?
  @type.clear?
end

#do_num_bytesObject

:nodoc:



84
85
86
# File 'lib/bindata/section.rb', line 84

def do_num_bytes # :nodoc:
  to_binary_s.size
end

#do_read(io) ⇒ Object

:nodoc:



72
73
74
75
76
# File 'lib/bindata/section.rb', line 72

def do_read(io) # :nodoc:
  io.transform(eval_parameter(:transform)) do |transformed_io, _raw_io|
    @type.do_read(transformed_io)
  end
end

#do_write(io) ⇒ Object

:nodoc:



78
79
80
81
82
# File 'lib/bindata/section.rb', line 78

def do_write(io) # :nodoc:
  io.transform(eval_parameter(:transform)) do |transformed_io, _raw_io|
    @type.do_write(transformed_io)
  end
end

#initialize_instanceObject



48
49
50
# File 'lib/bindata/section.rb', line 48

def initialize_instance
  @type = get_parameter(:type).instantiate(nil, self)
end

#respond_to_missing?(symbol, include_all = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


64
65
66
# File 'lib/bindata/section.rb', line 64

def respond_to_missing?(symbol, include_all = false) # :nodoc:
  @type.respond_to?(symbol, include_all) || super
end

#snapshotObject



60
61
62
# File 'lib/bindata/section.rb', line 60

def snapshot
  @type.snapshot
end