2008-12-17 16:49:06 +00:00
|
|
|
# --
|
2009-01-06 15:51:01 +00:00
|
|
|
# Copyright (C) 2008-2009 10gen Inc.
|
2008-12-17 16:43:08 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License, version 3, as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
|
|
|
# for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-17 16:49:06 +00:00
|
|
|
# ++
|
2008-12-17 16:43:08 +00:00
|
|
|
|
|
|
|
# A byte buffer.
|
2008-11-22 01:00:51 +00:00
|
|
|
class ByteBuffer
|
|
|
|
|
|
|
|
attr_reader :order
|
|
|
|
|
|
|
|
def initialize(initial_data=[])
|
|
|
|
@buf = initial_data
|
|
|
|
@cursor = 0
|
|
|
|
self.order = :little_endian
|
|
|
|
end
|
|
|
|
|
|
|
|
# +endianness+ should be :little_endian or :big_endian. Default is :little_endian
|
|
|
|
def order=(endianness)
|
|
|
|
@order = endianness
|
|
|
|
@int_pack_order = endianness == :little_endian ? 'V' : 'N'
|
|
|
|
@double_pack_order = endianness == :little_endian ? 'E' : 'G'
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewind
|
|
|
|
@cursor = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def position
|
|
|
|
@cursor
|
|
|
|
end
|
|
|
|
|
|
|
|
def position=(val)
|
|
|
|
@cursor = val
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
|
|
|
@buf = []
|
|
|
|
rewind
|
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
@buf.size
|
|
|
|
end
|
|
|
|
alias_method :length, :size
|
|
|
|
|
|
|
|
def put(byte, offset=nil)
|
|
|
|
@cursor = offset if offset
|
|
|
|
@buf[@cursor] = byte
|
|
|
|
@cursor += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_array(array, offset=nil)
|
|
|
|
@cursor = offset if offset
|
|
|
|
@buf[@cursor, array.length] = array
|
|
|
|
@cursor += array.length
|
|
|
|
end
|
|
|
|
|
2009-01-21 17:09:18 +00:00
|
|
|
def put_int(i, offset=nil)
|
|
|
|
a = []
|
|
|
|
[i].pack(@int_pack_order).each_byte { |b| a << b }
|
|
|
|
put_array(a, offset)
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def put_long(i, offset=nil)
|
|
|
|
offset = @cursor unless offset
|
|
|
|
if @int_pack_order == 'N'
|
|
|
|
put_int(i >> 32, offset)
|
|
|
|
put_int(i & 0xffffffff, offset + 4)
|
|
|
|
else
|
|
|
|
put_int(i & 0xffffffff, offset)
|
|
|
|
put_int(i >> 32, offset + 4)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-21 17:09:18 +00:00
|
|
|
def put_double(d, offset=nil)
|
|
|
|
a = []
|
|
|
|
[d].pack(@double_pack_order).each_byte { |b| a << b }
|
|
|
|
put_array(a, offset)
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
|
2008-12-18 21:14:56 +00:00
|
|
|
# If +size+ == nil, returns one byte. Else returns array of bytes of length
|
|
|
|
# # +size+.
|
|
|
|
def get(len=nil)
|
|
|
|
one_byte = len.nil?
|
|
|
|
len ||= 1
|
2008-11-22 01:00:51 +00:00
|
|
|
check_read_length(len)
|
|
|
|
start = @cursor
|
|
|
|
@cursor += len
|
2008-12-18 21:14:56 +00:00
|
|
|
if one_byte
|
2008-11-22 01:00:51 +00:00
|
|
|
@buf[start]
|
|
|
|
else
|
|
|
|
@buf[start, len]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_int
|
|
|
|
check_read_length(4)
|
|
|
|
vals = ""
|
|
|
|
(@cursor..@cursor+3).each { |i| vals << @buf[i].chr }
|
|
|
|
@cursor += 4
|
|
|
|
vals.unpack(@int_pack_order)[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_long
|
|
|
|
i1 = get_int
|
|
|
|
i2 = get_int
|
|
|
|
if @int_pack_order == 'N'
|
|
|
|
(i1 << 32) + i2
|
|
|
|
else
|
|
|
|
(i2 << 32) + i1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_double
|
|
|
|
check_read_length(8)
|
|
|
|
vals = ""
|
|
|
|
(@cursor..@cursor+7).each { |i| vals << @buf[i].chr }
|
|
|
|
@cursor += 8
|
|
|
|
vals.unpack(@double_pack_order)[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def more?
|
|
|
|
@cursor < @buf.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_a
|
|
|
|
@buf
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
@buf.pack("C*")
|
|
|
|
end
|
|
|
|
|
|
|
|
def dump
|
|
|
|
@buf.each_with_index { |c, i| $stderr.puts "#{'%04d' % i}: #{'%02x' % c} #{'%03o' % c} #{'%s' % c.chr} #{'%3d' % c}" }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_read_length(len)
|
|
|
|
raise "attempt to read past end of buffer" if @cursor + len > @buf.length
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|