Module: Solace::Utils::Codecs
- Defined in:
- lib/solace/utils/codecs.rb
Overview
Module for encoding and decoding data
Class Method Summary collapse
-
.base58_to_binary(string) ⇒ String
Decodes a Base58 string into a binary string.
-
.base58_to_bytes(string) ⇒ String
Decodes a Base58 string into a sequence of bytes.
-
.base64_to_bytestream(base64) ⇒ StringIO
Creates a StringIO from a base64 string.
-
.binary_to_base58(binary) ⇒ String
Encodes a sequence of bytes in Base58 format.
-
.bytes_to_base58(bytes) ⇒ String
Encodes a sequence of bytes in Base58 format.
-
.decode_compact_u16(stream) ⇒ Integer
Decodes a compact-u16 (ShortVec) value from an IO-like object.
-
.decode_le_u64(stream) ⇒ Integer
Decodes a little-endian u64 value from a sequence of bytes.
-
.encode_compact_u16(u16) ⇒ String
Encodes a compact-u16 value in a compact form (shortvec).
-
.encode_le_u64(u64) ⇒ String
Encodes a u64 value in little-endian format.
-
.valid_base58?(string) ⇒ Boolean
Checks if a string is a valid Base58 string.
Class Method Details
.base58_to_binary(string) ⇒ String
Decodes a Base58 string into a binary string
131 132 133 |
# File 'lib/solace/utils/codecs.rb', line 131 def self.base58_to_binary(string) base58_to_bytes(string).pack('C*') end |
.base58_to_bytes(string) ⇒ String
Decodes a Base58 string into a sequence of bytes
147 148 149 |
# File 'lib/solace/utils/codecs.rb', line 147 def self.base58_to_bytes(string) Base58.base58_to_binary(string, :bitcoin).bytes end |
.base64_to_bytestream(base64) ⇒ StringIO
Creates a StringIO from a base64 string.
35 36 37 |
# File 'lib/solace/utils/codecs.rb', line 35 def self.base64_to_bytestream(base64) StringIO.new(Base64.decode64(base64)) end |
.binary_to_base58(binary) ⇒ String
Encodes a sequence of bytes in Base58 format
123 124 125 |
# File 'lib/solace/utils/codecs.rb', line 123 def self.binary_to_base58(binary) Base58.binary_to_base58(binary, :bitcoin) end |
.bytes_to_base58(bytes) ⇒ String
Encodes a sequence of bytes in Base58 format
139 140 141 |
# File 'lib/solace/utils/codecs.rb', line 139 def self.bytes_to_base58(bytes) binary_to_base58(bytes.pack('C*')) end |
.decode_compact_u16(stream) ⇒ Integer
Decodes a compact-u16 (ShortVec) value from an IO-like object.
Reads bytes one at a time, accumulating the result until the MSB is 0.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/solace/utils/codecs.rb', line 83 def self.decode_compact_u16(stream) value = 0 shift = 0 bytes_read = 0 loop do byte = stream.read(1) raise EOFError, 'Unexpected end of input while decoding compact-u16' unless byte byte = byte.ord value |= (byte & 0x7F) << shift bytes_read += 1 break if byte.nobits?(0x80) shift += 7 end [value, bytes_read] end |
.decode_le_u64(stream) ⇒ Integer
Decodes a little-endian u64 value from a sequence of bytes
115 116 117 |
# File 'lib/solace/utils/codecs.rb', line 115 def self.decode_le_u64(stream) stream.read(8).unpack1('Q<') end |
.encode_compact_u16(u16) ⇒ String
Encodes a compact-u16 value in a compact form (shortvec)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/solace/utils/codecs.rb', line 43 def self.encode_compact_u16(u16) out = [] loop do # In general, n >> 7 shifts the bits of n to the right by # 7 positions, effectively dividing n by 128 and discarding # the remainder (integer division). This is commonly used in # encoding schemes to process one "byte" (7 bits) at a time. if (u16 >> 7).zero? out << u16 break end # The expression out << ((n & 0x7F) | 0x80) is used in variable-length # integer encoding, such as the compact-u16 encoding. # # n & 0x7F: # - 0x7F is 127 in decimal, or 0111 1111 in binary. # - n & 0x7F masks out all but the lowest 7 bits of n. This extracts the least significant 7 bits of n. # # (n & 0x7F) | 0x80: # - 0x80 is 128 in decimal, or 1000 0000 in binary. # - | (bitwise OR) sets the highest bit (the 8th bit) to 1. # - This is a signal that there are more bytes to come in the encoding (i.e., the value hasn't been fully # encoded yet). # # out << ...: # - This appends the resulting byte to the out array. out << ((u16 & 0x7F) | 0x80) u16 >>= 7 end out.pack('C*') end |
.encode_le_u64(u64) ⇒ String
Encodes a u64 value in little-endian format
107 108 109 |
# File 'lib/solace/utils/codecs.rb', line 107 def self.encode_le_u64(u64) [u64].pack('Q<') # 64-bit little-endian end |
.valid_base58?(string) ⇒ Boolean
Checks if a string is a valid Base58 string
155 156 157 158 159 160 161 162 |
# File 'lib/solace/utils/codecs.rb', line 155 def self.valid_base58?(string) return false if string.nil? || string.empty? Base58.decode(string) true rescue StandardError => _e false end |