Module: BinaryCodec

Defined in:
lib/binary-codec/utilities.rb,
lib/binary-codec/types/blob.rb,
lib/binary-codec/types/hash.rb,
lib/binary-codec/types/uint.rb,
lib/binary-codec/binary_codec.rb,
lib/binary-codec/enums/fields.rb,
lib/binary-codec/types/amount.rb,
lib/binary-codec/types/currency.rb,
lib/binary-codec/enums/constants.rb,
lib/binary-codec/types/st_object.rb,
lib/binary-codec/types/account_id.rb,
lib/binary-codec/enums/definitions.rb,
lib/binary-codec/serdes/bytes_list.rb,
lib/binary-codec/serdes/binary_parser.rb,
lib/binary-codec/types/serialized_type.rb,
lib/binary-codec/serdes/binary_serializer.rb

Defined Under Namespace

Classes: AccountId, Amount, BinaryCodec, BinaryParser, BinarySerializer, Blob, BytesList, ComparableSerializedType, Currency, Definitions, FieldHeader, FieldInfo, FieldInstance, FieldLookup, Hash, Hash128, Hash160, Hash192, Hash256, STObject, SerializedType, Uint, Uint16, Uint32, Uint64, Uint8

Constant Summary collapse

TYPE_WIDTH =
2
LEDGER_ENTRY_WIDTH =
2
TRANSACTION_TYPE_WIDTH =
2
TRANSACTION_RESULT_WIDTH =
1

Class Method Summary collapse

Class Method Details

.aligned16?(array) ⇒ Boolean

Determine if an array is 16-bit aligned

Returns:

  • (Boolean)


71
72
73
# File 'lib/binary-codec/utilities.rb', line 71

def self.aligned16?(array)
  (array.length % 2).zero?
end

.aligned32?(array) ⇒ Boolean

Determine if an array is 32-bit aligned

Returns:

  • (Boolean)


76
77
78
# File 'lib/binary-codec/utilities.rb', line 76

def self.aligned32?(array)
  (array.length % 4).zero?
end

.compare(array1, array2) ⇒ Object

Compare two arrays of any type



40
41
42
43
44
45
46
47
48
49
# File 'lib/binary-codec/utilities.rb', line 40

def self.compare(array1, array2)
  raise 'Cannot compare arrays of different length' if array1.length != array2.length

  array1.each_with_index do |value, i|
    return 1 if value > array2[i]
    return -1 if value < array2[i]
  end

  0
end

.compare16(array1, array2) ⇒ Object

Compares two 16-bit aligned arrays



57
58
59
60
61
# File 'lib/binary-codec/utilities.rb', line 57

def self.compare16(array1, array2)
  raise 'Array lengths must be even for 16-bit alignment' unless (array1.length % 2).zero? && (array2.length % 2).zero?

  array1.pack('C*').unpack('n*') <=> array2.pack('C*').unpack('n*')
end

.compare32(array1, array2) ⇒ Object

Compares two 32-bit aligned arrays



64
65
66
67
68
# File 'lib/binary-codec/utilities.rb', line 64

def self.compare32(array1, array2)
  raise 'Array lengths must be divisible by 4 for 32-bit alignment' unless (array1.length % 4).zero? && (array2.length % 4).zero?

  array1.pack('C*').unpack('N*') <=> array2.pack('C*').unpack('N*')
end

.compare8(array1, array2) ⇒ Object

Compares two 8-bit aligned arrays



52
53
54
# File 'lib/binary-codec/utilities.rb', line 52

def self.compare8(array1, array2)
  compare(array1, array2)
end

.equal(array1, array2) ⇒ Object

Compare two byte arrays



34
35
36
37
# File 'lib/binary-codec/utilities.rb', line 34

def self.equal(array1, array2)
  return false unless array1.length == array2.length
  array1 == array2
end

.read_uint16be(array, offset = 0) ⇒ Object

Read an unsigned 16-bit integer in big-endian format



9
10
11
# File 'lib/binary-codec/utilities.rb', line 9

def self.read_uint16be(array, offset = 0)
  (array[offset] << 8) + array[offset + 1]
end

.read_uint32be(array, offset = 0) ⇒ Object

Read an unsigned 32-bit integer in big-endian format



20
21
22
23
# File 'lib/binary-codec/utilities.rb', line 20

def self.read_uint32be(array, offset = 0)
  (array[offset] << 24) + (array[offset + 1] << 16) +
    (array[offset + 2] << 8) + array[offset + 3]
end

.write_uint16be(array, value, offset = 0) ⇒ Object

Write a 16-bit unsigned integer in big-endian format



14
15
16
17
# File 'lib/binary-codec/utilities.rb', line 14

def self.write_uint16be(array, value, offset = 0)
  array[offset] = (value >> 8) & 0xFF
  array[offset + 1] = value & 0xFF
end

.write_uint32be(buffer, value, offset = 0) ⇒ Object

Write an unsigned 32-bit integer to a buffer in big-endian format



26
27
28
29
30
31
# File 'lib/binary-codec/utilities.rb', line 26

def self.write_uint32be(buffer, value, offset = 0)
  buffer[offset] = (value >> 24) & 0xFF
  buffer[offset + 1] = (value >> 16) & 0xFF
  buffer[offset + 2] = (value >> 8) & 0xFF
  buffer[offset + 3] = value & 0xFF
end

.write_uint8(array, value, offset = 0) ⇒ Object

Write an 8-bit unsigned integer



4
5
6
# File 'lib/binary-codec/utilities.rb', line 4

def self.write_uint8(array, value, offset = 0)
  array[offset] = value & 0xFF
end