class BinParserElement
Attributes
endianness[R]
name[R]
offset[R]
size[R]
Public Class Methods
calculate_pack_template_string(size)
click to toggle source
# File lib/binp.rb, line 78 def self.calculate_pack_template_string(size) 'C' * size end
calculate_unpack_template_string(type, endianness)
click to toggle source
see: docs.ruby-lang.org/ja/latest/doc/pack_template.html
# File lib/binp.rb, line 46 def self.calculate_unpack_template_string(type, endianness) # 特殊な条件 case type when BinParserElement::Type::UINT16 if endianness == BinParserElement::Endianness::BIG_ENDIAN # ビッグエンディアン、符号なし 16 bit 整数 return 'n' else # リトルエンディアン、符号なし 16 bit 整数 return 'v' end when BinParserElement::Type::UINT32 if endianness == BinParserElement::Endianness::BIG_ENDIAN # ビッグエンディアン、符号あり 32 bit 整数 return 'N' else # リトルエンディアン、符号あり 32 bit 整数 return 'V' end when BinParserElement::Type::UINT8 # 符号なし 8 bit 整数 return 'C' when BinParserElement::Type::INT8 # 符号あり 8 bit 整数 return 'c' end # その他 type[:value] + endianness[:value] end
get_flags(uint8_array, config)
click to toggle source
# File lib/binp.rb, line 93 def self.get_flags(uint8_array, config) offset = config['offset'] layout = config['layout'] target = uint8_array.slice(offset, 1) layout.map { |e| c = config.dup c['name'] = e['name'] # ビットが立っているかの確認 c['value'] = target[0] & (1 << e['position']) # true_label, false_label が存在すればそれに設定。 # 存在しなければデフォルト値('ON', 'OFF')に設定。 if c['value'] != 0 c['value'] = e.fetch('true_label', 'ON') else c['value'] = e.fetch('false_label', 'OFF') end c } end
get_value(uint8_array, offset, size, type, endianness, value_labels={})
click to toggle source
# File lib/binp.rb, line 82 def self.get_value(uint8_array, offset, size, type, endianness, value_labels={}) target = uint8_array.slice(offset, size) pack_template_string = calculate_pack_template_string(size) unpack_template_string = calculate_unpack_template_string(type, endianness) value = target.pack(pack_template_string).unpack(unpack_template_string)[0] # value_label が設定されている数値であれば、それに置き換える value_labels ||= {} value_labels.fetch(value, value) end
new(name, offset, size, endianness)
click to toggle source
# File lib/binp.rb, line 21 def initialize(name, offset, size, endianness) @name = name @offset = offset @size = size @endianness = endianness end