Class: BinaryParser::TemplateBase

Inherits:
Object
  • Object
show all
Includes:
BuiltInTemplate
Defined in:
lib/binary_parser.rb,
lib/template_base.rb

Overview

load built-in template file

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binary, parent_scope = nil) ⇒ TemplateBase

Returns a new instance of TemplateBase.



35
36
37
# File 'lib/template_base.rb', line 35

def initialize(binary, parent_scope=nil)
  @scope = Scope.new(self.class.structure, convert_into_abstract_binary(binary), parent_scope)
end

Class Method Details

.Def(parent_structure = nil, &definition_proc) ⇒ Object



27
28
29
# File 'lib/template_base.rb', line 27

def self.Def(parent_structure=nil, &definition_proc)
  def_structure(parent_structure, &definition_proc)
end

.def_structure(parent_structure = nil, &definition_proc) ⇒ Object



5
6
7
8
9
10
# File 'lib/template_base.rb', line 5

def self.def_structure(parent_structure=nil, &definition_proc)
  @structure_def = StructureDefinition.new(instance_methods, parent_structure, &definition_proc)
  @structure_def.names.each do |name|
    def_var_method(name)
  end
end

.def_var_method(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/template_base.rb', line 12

def self.def_var_method(name)
  define_method(name){|&block|
    if block
      case block.arity
      when 0
        @scope.load_var(name).instance_eval(&block)
      when 1
        block.call(@scope.load_var(name))
      end
    else
      @scope.load_var(name)
    end
  }
end

.structureObject



31
32
33
# File 'lib/template_base.rb', line 31

def self.structure
  return @structure_def ||= StructureDefinition.new
end

Instance Method Details

#[](name) ⇒ Object



47
48
49
# File 'lib/template_base.rb', line 47

def [](name)
  @scope.load_var(name)
end

#binary_bit_lengthObject

Real length(bit) of held binary



80
81
82
# File 'lib/template_base.rb', line 80

def binary_bit_length
  @scope.abstract_binary.bit_length
end

#content_descriptionObject

String that describes this object.

* If you want to print some of this content-description in 'show' method,
  override this method.


111
112
113
# File 'lib/template_base.rb', line 111

def content_description
  ""
end

#convert_into_abstract_binary(object) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/template_base.rb', line 39

def convert_into_abstract_binary(object)
  return object if object.is_a?(AbstractBinary)
  if object.is_a?(String) && object.encoding == Encoding::BINARY
    return AbstractBinary.new(object)
  end
  raise BadManipulationError, "Argument should be AbstractBinary or BINAY String."
end

#hold_enough_binary?Boolean

Whether real length of held binary is NOT smaller than structure-specified length of binary. Special case:

If held binary's length is too short to calculate structure-specified length,
this method throws ParsingError.

Returns:

  • (Boolean)


96
97
98
# File 'lib/template_base.rb', line 96

def hold_enough_binary?
  structure_bit_length <= binary_bit_length
end

#hold_just_binary?Boolean

Whether real length of held binary is equal to structure-specified length of binary. Special case:

If held binary's length is too short to calculate structure-specified length,
this method throws ParsingError.

Returns:

  • (Boolean)


104
105
106
# File 'lib/template_base.rb', line 104

def hold_just_binary?
  structure_bit_length == binary_bit_length
end

#namesObject



51
52
53
# File 'lib/template_base.rb', line 51

def names
  @scope.names
end

#show(recursively = false, out = STDOUT, depth = 0) ⇒ Object

Print all elements’ information. Args:

recursively => Whether print recursively or not. Default is false.
out         => Print target. Default is STDOUT.


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/template_base.rb', line 119

def show(recursively=false, out=STDOUT, depth=0)
  max_name_length = names.inject(5){|max_len, name| [max_len, name.length].max}
  if names.size > 0
    out.puts "#{" " * (depth*2)}*#{"-" * 80}"
  end
  names.each do |name|
    out.puts sprintf("#{" " * (depth*2)}%-#{max_name_length}s  Pos: %6s  Len: %6s  Type: %10s  Cont: %s",
                     name.to_s,
                     @scope.eval_bit_position(name),
                     @scope.eval_bit_length(name),
                     self[name].class.name.split("::").last,
                     self[name] ? self[name].content_description : "Nil")
    self[name].show(true, out, depth + 1) if recursively && self[name]
  end
end

#structure_bit_lengthObject

Structure-specified length(bit) of binary. Special case:

If held binary's length is too short to calculate structure-specified length,
this method throws ParsingError.


88
89
90
# File 'lib/template_base.rb', line 88

def structure_bit_length
  @scope.eval_entire_bit_length
end

#to_charsObject

Convert held binary into character-numbers. Example: If held binary is “ABC” in ascii, this returns [0x41, 0x42, 0x43]. Special case:

If held binary's length or start position isn't a multiple of 8,
this method throws BadBinaryManipulationError.


75
76
77
# File 'lib/template_base.rb', line 75

def to_chars
  @scope.abstract_binary.to_chars
end

#to_iObject

Convert held binary into unsigned integer. Special case:

If held binary's length is 0, this method throws BadBinaryManipulationError.


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

def to_i
  @scope.abstract_binary.to_i
end

#to_sObject

Convert held binary into string encoded in Encoding::BINARY. Special case:

If held binary's length or start position isn't a multiple of 8,
this method throws BadBinaryManipulationError.


66
67
68
# File 'lib/template_base.rb', line 66

def to_s
  @scope.abstract_binary.to_s
end