Class: BinaryParser::AbstractBinary

Inherits:
Object
  • Object
show all
Defined in:
lib/general_class/abstract_binary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binary_string, bit_index = nil, bit_length = nil) ⇒ AbstractBinary

Returns a new instance of AbstractBinary.



6
7
8
9
10
11
12
13
14
# File 'lib/general_class/abstract_binary.rb', line 6

def initialize(binary_string, bit_index=nil, bit_length=nil)
  unless binary_string.encoding == Encoding::BINARY
    raise BadBinaryManipulationError, "binary_string's encoding should be" +
      "ASCII_8BIT(BINARY). This is #{binary_string.encoding}."
  end
  @bin_str = binary_string
  @bit_index = bit_index || 0
  @bit_length = bit_length || binary_string.length * 8
end

Instance Attribute Details

#bit_lengthObject (readonly)

Returns the value of attribute bit_length.



4
5
6
# File 'lib/general_class/abstract_binary.rb', line 4

def bit_length
  @bit_length
end

Instance Method Details

#sub(spec) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/general_class/abstract_binary.rb', line 16

def sub(spec)
  additional_bit_index = spec[:bit_index].to_i + spec[:byte_index].to_i * 8
  if additional_bit_index >= @bit_index + @bit_length
    raise BadBinaryManipulationError, "Impossible index specification of sub binary " +
      "(bit_index: #{additional_bit_index} on [#{@bit_index}, #{@bit_length}))"
  end

  if spec[:bit_length] || spec[:byte_length]
    new_bit_length = spec[:bit_length].to_i + spec[:byte_length].to_i * 8
  else
    new_bit_length = @bit_length - additional_bit_index
  end
  if additional_bit_index + new_bit_length > @bit_length
    raise BadBinaryManipulationError, "Impossible length specification of" +
      "sub binary (bit_index: #{additional_bit_index}, " +
      "bit_length: #{new_bit_length} on [#{@bit_index}, #{@bit_length}])"
  end

  return self.class.new(@bin_str, @bit_index + additional_bit_index, new_bit_length)
end

#to_charsObject



52
53
54
55
56
57
58
# File 'lib/general_class/abstract_binary.rb', line 52

def to_chars
  unless @bit_index % 8 == 0 && @bit_length % 8 == 0
    raise BadBinaryManipulationError, "Invalid position(from #{@bit_index} bit)" +
      "and length(#{@bit_length} bit)."
  end
  return @bin_str[@bit_index / 8, @bit_length / 8].unpack("C*")
end

#to_iObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/general_class/abstract_binary.rb', line 37

def to_i
  if @bit_length == 0
    raise BadBinaryManipulationError, "Cannot convert empty binary into integer."
  end
  res, rest_bit, char_pos = 0, @bit_length - 1, @bit_index % 8
  @bin_str[@bit_index / 8, (@bit_length + @bit_index % 8) / 8 + 1].unpack("C*").each do |char|
    (char_pos..7).each do |i|
      res += char[7 - i] * (1 << rest_bit)
      return res if (rest_bit -= 1) < 0
    end
    char_pos = 0
  end
  raise ProgramAssertionError, "Failed to convert integer value."
end

#to_sObject



60
61
62
63
64
65
66
# File 'lib/general_class/abstract_binary.rb', line 60

def to_s
  unless @bit_index % 8 == 0 && @bit_length % 8 == 0
    raise BadBinaryManipulationError, "Invalid position(from #{@bit_index} bit) " +
      "and length(#{@bit_length} bit)."
  end
  return @bin_str[@bit_index / 8, @bit_length / 8]
end