Class: Stribog::ByteVector

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/stribog/byte_vector.rb

Overview

byte_array

Author:

  • WildDima

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vector) ⇒ ByteVector

Returns a new instance of ByteVector.



22
23
24
# File 'lib/stribog/byte_vector.rb', line 22

def initialize(vector)
  @vector = vector
end

Instance Attribute Details

#vectorObject (readonly)

Returns the value of attribute vector.



8
9
10
# File 'lib/stribog/byte_vector.rb', line 8

def vector
  @vector
end

Class Method Details

.convert(vector) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/stribog/byte_vector.rb', line 10

def self.convert(vector)
  case vector
  when String
    new(vector.unpack('C*'))
  when Numeric
    # TODO: REFACTOR
    bin = vector.to_s(2)
    size = 2**Math.log2(vector.size * 8).ceil
    new(['0' * (size - bin.size) + bin].pack('B*').unpack('C*'))
  end
end

Instance Method Details

#+(other) ⇒ Object



32
33
34
# File 'lib/stribog/byte_vector.rb', line 32

def +(other)
  self.class.new vector + other.to_a
end

#[](index) ⇒ Object



73
74
75
# File 'lib/stribog/byte_vector.rb', line 73

def [](index)
  vector[index]
end

#^(other) ⇒ Object



26
27
28
29
30
# File 'lib/stribog/byte_vector.rb', line 26

def ^(other)
  vec = [other, self].sort_by(&:size).reverse.map(&:reverse)
  self.class.new vec[0].map
                       .with_index { |bit, index| bit ^ (vec[1][index] || 0) }.reverse
end

#addition(size: 64) ⇒ Object



36
37
38
39
# File 'lib/stribog/byte_vector.rb', line 36

def addition(size: 64)
  return self if vector.size >= size
  self.class.new(Array.new(size - vector.size, 0) + vector)
end

#bit64Object



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

def bit64
  @bit64 ||= byte8.map { |b| [b].pack('Q*').unpack('B*') }.flatten
end

#byte8Object



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

def byte8
  @byte8 ||= vector.pack('C*').unpack('Q*')
end

#eachObject



77
78
79
80
81
82
# File 'lib/stribog/byte_vector.rb', line 77

def each
  return vector.each unless block_given?
  vector.each do |v|
    yield(v)
  end
end

#padding(size: 64) ⇒ Object



41
42
43
44
45
# File 'lib/stribog/byte_vector.rb', line 41

def padding(size: 64)
  return self if vector.size >= size
  # (self.class.new([1]) + vector).addition_by_zeros(size: 512)
  self.class.new([1] + vector).addition(size: size)
end

#reverseObject



92
93
94
# File 'lib/stribog/byte_vector.rb', line 92

def reverse
  self.class.new vector.reverse
end

#sizeObject



69
70
71
# File 'lib/stribog/byte_vector.rb', line 69

def size
  @size ||= vector.size
end

#to_aObject



84
85
86
# File 'lib/stribog/byte_vector.rb', line 84

def to_a
  vector
end

#to_decObject



55
56
57
58
# File 'lib/stribog/byte_vector.rb', line 55

def to_dec
  # to_s.to_i(2)
  vector.pack('C*').unpack('B*').first.to_i(2)
end

#to_hexObject



60
61
62
# File 'lib/stribog/byte_vector.rb', line 60

def to_hex
  to_s.to_i(2).to_s(16)
end

#to_sObject



64
65
66
67
# File 'lib/stribog/byte_vector.rb', line 64

def to_s
  # vector.pack('C*')
  vector
end

#zero?Boolean

Returns:

  • (Boolean)


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

def zero?
  vector.any?(&:zero?)
end