Class: Cabriolet::Binary::ENCINTReader
- Inherits:
-
Object
- Object
- Cabriolet::Binary::ENCINTReader
- Defined in:
- lib/cabriolet/binary/chm_structures.rb
Overview
Helper class for reading ENCINT (variable-length integers)
Class Method Summary collapse
-
.read(io) ⇒ Object
Read an ENCINT from an IO stream Returns the integer value.
-
.read_from_string(str, pos) ⇒ Object
Read an ENCINT from a string at a given position Returns [value, new_position].
Class Method Details
.read(io) ⇒ Object
Read an ENCINT from an IO stream Returns the integer value
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/cabriolet/binary/chm_structures.rb', line 116 def self.read(io) result = 0 byte = 0x80 bytes_read = 0 max_bytes = 9 # 63 bits max while byte.anybits?(0x80) && bytes_read < max_bytes byte_data = io.read(1) if byte_data.nil? raise Cabriolet::FormatError, "Unexpected end of ENCINT" end byte = byte_data.unpack1("C") result = (result << 7) | (byte & 0x7F) bytes_read += 1 end if bytes_read == max_bytes && byte.anybits?(0x80) raise Cabriolet::FormatError, "ENCINT too large" end result end |
.read_from_string(str, pos) ⇒ Object
Read an ENCINT from a string at a given position Returns [value, new_position]
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/cabriolet/binary/chm_structures.rb', line 144 def self.read_from_string(str, pos) result = 0 byte = 0x80 bytes_read = 0 max_bytes = 9 while byte.anybits?(0x80) && bytes_read < max_bytes if pos >= str.length raise Cabriolet::FormatError, "ENCINT beyond string" end byte = str.getbyte(pos) pos += 1 result = (result << 7) | (byte & 0x7F) bytes_read += 1 end if bytes_read == max_bytes && byte.anybits?(0x80) raise Cabriolet::FormatError, "ENCINT too large" end [result, pos] end |