Class: RCS::BCDataStream

Inherits:
Object
  • Object
show all
Defined in:
lib/rcs-common/evidence/money.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ BCDataStream

Returns a new instance of BCDataStream.



234
235
236
237
# File 'lib/rcs-common/evidence/money.rb', line 234

def initialize(string)
  @buffer = string
  @read_cursor = 0
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



232
233
234
# File 'lib/rcs-common/evidence/money.rb', line 232

def buffer
  @buffer
end

#read_cursorObject (readonly)

Returns the value of attribute read_cursor.



231
232
233
# File 'lib/rcs-common/evidence/money.rb', line 231

def read_cursor
  @read_cursor
end

Instance Method Details

#_read_num(format, size) ⇒ Object



287
288
289
290
291
# File 'lib/rcs-common/evidence/money.rb', line 287

def _read_num(format, size)
  val = @buffer[@read_cursor..@read_cursor+size].unpack(format).first
  @read_cursor += size
  return val
end

#read_booleanObject



263
# File 'lib/rcs-common/evidence/money.rb', line 263

def read_boolean; return _read_num('c', 1) == 1;  end

#read_bytes(length) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/rcs-common/evidence/money.rb', line 265

def read_bytes(length)
  result = @buffer[@read_cursor..@read_cursor+length-1]
  @read_cursor += length
  return result
rescue Exception => e
  raise "attempt to read past end of buffer: #{e.message}"
end

#read_compact_sizeObject



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/rcs-common/evidence/money.rb', line 273

def read_compact_size
  size = @buffer[@read_cursor].ord
  @read_cursor += 1
  if size == 253
    size = _read_num('S', 2)
  elsif size == 254
    size = _read_num('I', 4)
  elsif size == 255
    size = _read_num('Q', 8)
  end

  return size
end

#read_int32Object



260
# File 'lib/rcs-common/evidence/money.rb', line 260

def read_int32; return _read_num('l', 4);  end

#read_int64Object



262
# File 'lib/rcs-common/evidence/money.rb', line 262

def read_int64; return _read_num('q', 8);  end

#read_stringObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rcs-common/evidence/money.rb', line 239

def read_string
  # Strings are encoded depending on length:
  # 0 to 252 :  1-byte-length followed by bytes (if any)
  # 253 to 65,535 : byte'253' 2-byte-length followed by bytes
  # 65,536 to 4,294,967,295 : byte '254' 4-byte-length followed by bytes
  # greater than 4,294,967,295 : byte '255' 8-byte-length followed by bytes of string

  if @buffer.eql? nil
    raise "not initialized"
  end

  begin
    length = self.read_compact_size
  rescue Exception => e
    raise "attempt to read past end of buffer: #{e.message}"
  end

  return self.read_bytes(length)
end

#read_uint32Object



259
# File 'lib/rcs-common/evidence/money.rb', line 259

def read_uint32; return _read_num('L', 4);  end

#read_uint64Object



261
# File 'lib/rcs-common/evidence/money.rb', line 261

def read_uint64; return _read_num('Q', 8);  end