Class: TSparser::Binary
- Inherits:
-
String
- Object
- String
- TSparser::Binary
- Defined in:
- lib/binary.rb
Defined Under Namespace
Classes: BinaryException
Class Method Summary collapse
Instance Method Summary collapse
- #&(byte_integer) ⇒ Object
-
#<=>(integer) ⇒ Object
Comparator to Integer.
-
#b(byte_index, bit_range = nil) ⇒ Object
Return Integer that is converted from bits specified by “bit_range” (arg2) in byte specified by “byte_index” (arg1).
- #bit_pointer ⇒ Object
- #dump ⇒ Object
-
#from(start_position) ⇒ Object
Generate new Binary instance that is subsequence of self (from specified position to end).
-
#initialize(byte_string = nil) ⇒ Binary
constructor
“byte_string” is string encoded as ASCII_8BIT (BINARY).
-
#join(*binaries) ⇒ Object
Generate new Binary instance that is joined from “self”, “arg1”, “arg2”, …
- #last_read_byte ⇒ Object
-
#read_bit_as_binary(bitlen) ⇒ Object
Read specified length of bits and return as Binary instance.
-
#read_bit_as_integer(bitlen) ⇒ Object
Read specified length of bits and return as Integer instance.
- #read_byte_as_binary(bytelen) ⇒ Object
-
#read_byte_as_integer(bytelen) ⇒ Object
Read specified length of bytes and return as Integer instance.
-
#read_one_bit ⇒ Object
Read one bit and return as 0 or 1.
-
#readable? ⇒ Boolean
Return whether bit pointer reached end or not (true/false).
-
#rest_readable_bit_length ⇒ Object
Return length of rest readable bit.
-
#sub_integer(integer, bit_range) ⇒ Object
Get sub-bit of specified integer.
-
#to_i(byte_position) ⇒ Object
Return Integer that is converted from Byte at specified position.
Constructor Details
#initialize(byte_string = nil) ⇒ Binary
“byte_string” is string encoded as ASCII_8BIT (BINARY).
12 13 14 15 16 17 18 19 |
# File 'lib/binary.rb', line 12 def initialize(byte_string=nil) byte_string ||= "".encode(Encoding::ASCII_8BIT) if byte_string.encoding != Encoding::ASCII_8BIT raise BinaryException.new("byte_string's encoding should be ASCII_8BIT(BINARY) " + "(this is #{byte_string.encoding})") end super(byte_string) end |
Class Method Details
.from_int(*integers) ⇒ Object
7 8 9 |
# File 'lib/binary.rb', line 7 def self.from_int(*integers) return new(integers.pack("C*")) end |
Instance Method Details
#&(byte_integer) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/binary.rb', line 75 def &(byte_integer) if byte_integer < 0x00 || byte_integer > 0xFF raise BinaryException.new("Can't apply operator& with integer #{byte_integer}.") end if self.length != 1 raise BinaryException.new("Can't apply operator& on bytes #{self}.") end return Binary.from_int(self.to_i(0) & byte_integer) end |
#<=>(integer) ⇒ Object
Comparator to Integer
52 53 54 55 56 57 58 |
# File 'lib/binary.rb', line 52 def <=>(integer) return super unless integer.is_a?(Integer) unless self.length == 1 raise BinaryException.new("Can't compare non-single byte with integer.") end return self.to_i(0) <=> integer end |
#b(byte_index, bit_range = nil) ⇒ Object
Return Integer that is converted from bits specified by “bit_range” (arg2) in byte specified by “byte_index” (arg1).
Warning: Bit index is from right to left. So, LSB’s position is 0, MSB’s is 7
25 26 27 28 29 |
# File 'lib/binary.rb', line 25 def b(byte_index, bit_range=nil) byte_num = self[byte_index].unpack("C")[0] return byte_num unless bit_range return sub_integer(byte_num, bit_range) end |
#bit_pointer ⇒ Object
190 191 192 193 |
# File 'lib/binary.rb', line 190 def bit_pointer @bit_pointer ||= 0 return @bit_pointer end |
#dump ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/binary.rb', line 85 def dump bytes = [] each_byte do |byte| bytes << sprintf("%02X", byte) end return bytes.join(" ") end |
#from(start_position) ⇒ Object
Generate new Binary instance that is subsequence of self (from specified position to end).
61 62 63 64 65 66 |
# File 'lib/binary.rb', line 61 def from(start_position) unless start_position < self.length raise BinaryException.new("starting point must should be less than length") end return self[start_position, self.length - start_position] end |
#join(*binaries) ⇒ Object
Generate new Binary instance that is joined from “self”, “arg1”, “arg2”, … (in order).
69 70 71 72 73 |
# File 'lib/binary.rb', line 69 def join(*binaries) return binaries.inject(self) do |combined, binary| Binary.new(combined + binary) end end |
#last_read_byte ⇒ Object
176 177 178 |
# File 'lib/binary.rb', line 176 def last_read_byte return Binary.new(self[bit_pointer-1]) end |
#read_bit_as_binary(bitlen) ⇒ Object
Read specified length of bits and return as Binary instance. Bit pointer proceed for that length.
Warning: “bitlen” must be integer of multiple of 8, and bit pointer must be pointing start of byte.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/binary.rb', line 154 def read_bit_as_binary(bitlen) unless bit_pointer % 8 == 0 raise BinaryException.new("Bit pointer must be pointing start of byte. " + "But now pointing #{bit_pointer}.") end unless bitlen % 8 == 0 raise BinaryException.new("Arg must be integer of multiple of 8. " + "But you specified #{bitlen}.") end if self.length - bit_pointer/8 < bitlen/8 raise BinaryException.new("Rest of self length(#{self.length - bit_pointer/8}byte)" + " is shorter than specified byte length(#{bitlen/8}byte).") end response = self[bit_pointer/8, bitlen/8] bit_pointer_inc(bitlen) return response end |
#read_bit_as_integer(bitlen) ⇒ Object
Read specified length of bits and return as Integer instance. Bit pointer proceed for that length.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/binary.rb', line 100 def read_bit_as_integer(bitlen) if self.length * 8 - bit_pointer < bitlen raise BinaryException.new("Rest of self length(#{self.length * 8 - bit_pointer}bit) "+ "is shorter than specified bit length(#{bitlen}bit).") end if bit_pointer % 8 == 0 && bitlen % 8 == 0 return read_byte_as_integer(bitlen/8) else response = 0 bitlen.times do response = response << 1 response += read_one_bit end return response end end |
#read_byte_as_binary(bytelen) ⇒ Object
172 173 174 |
# File 'lib/binary.rb', line 172 def read_byte_as_binary(bytelen) return read_bit_as_binary(bytelen*8) end |
#read_byte_as_integer(bytelen) ⇒ Object
Read specified length of bytes and return as Integer instance. Bit pointer proceed for that length.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/binary.rb', line 119 def read_byte_as_integer(bytelen) unless bit_pointer % 8 == 0 raise BinaryException.new("Bit pointer must be pointing start of byte. " + "But now pointing #{bit_pointer}.") end if self.length - bit_pointer/8 < bytelen raise BinaryException.new("Rest of self length(#{self.length - bit_pointer/8}byte) " + "is shorter than specified byte length(#{bytelen}byte).") end response = 0 bytelen.times do |i| response = response << 8 response += to_i(bit_pointer/8 + i) end bit_pointer_inc(bytelen * 8) return response end |
#read_one_bit ⇒ Object
Read one bit and return as 0 or 1. Bit pointer proceed for one.
139 140 141 142 143 144 145 146 147 |
# File 'lib/binary.rb', line 139 def read_one_bit unless self.length * 8 - bit_pointer > 0 raise BinaryException.new("Readable buffer doesn't exist" + "(#{self.length * 8 - bit_pointer}bit exists).") end response = to_i(bit_pointer/8)[7 - bit_pointer%8] bit_pointer_inc(1) return response end |
#readable? ⇒ Boolean
Return whether bit pointer reached end or not (true/false).
181 182 183 |
# File 'lib/binary.rb', line 181 def readable? return bit_pointer < self.length * 8 end |
#rest_readable_bit_length ⇒ Object
Return length of rest readable bit
186 187 188 |
# File 'lib/binary.rb', line 186 def rest_readable_bit_length return self.length * 8 - bit_pointer end |
#sub_integer(integer, bit_range) ⇒ Object
Get sub-bit of specified integer.
Example:
binary = Binary.new(something_byte_string)
binary.sub_integer(0b11111100, 1..2) # => 2 (0b10)
36 37 38 39 40 41 42 43 44 |
# File 'lib/binary.rb', line 36 def sub_integer(integer, bit_range) bit_range = bit_range..bit_range if bit_range.kind_of?(Integer) num = 0 bit_range.reverse_each do |i| num = num << 1 num += integer[i] end return num end |
#to_i(byte_position) ⇒ Object
Return Integer that is converted from Byte at specified position.
47 48 49 |
# File 'lib/binary.rb', line 47 def to_i(byte_position) return self[byte_position].unpack("C")[0] end |