lib/dbus/marshall.rb in ruby-dbus-0.6.0 vs lib/dbus/marshall.rb in ruby-dbus-0.7.0
- old
+ new
@@ -40,22 +40,20 @@
elsif @endianness == LIL_END
@uint32 = "V"
@uint16 = "v"
@double = "E"
else
- # FIXME: shouldn't a more special exception be raised here?
- # yes, idea for a good name ? :)
- raise Exception, "Incorrect endianness"
+ raise InvalidPacketException, "Incorrect endianness #{@endianness}"
end
@idx = 0
end
# Unmarshall the buffer for a given _signature_ and length _len_.
# Return an array of unmarshalled objects
def unmarshall(signature, len = nil)
if len != nil
- if @buffy.size < @idx + len
+ if @buffy.bytesize < @idx + len
raise IncompleteBufferException
end
end
sigtree = Type::Parser.new(signature).parse
ret = Array.new
@@ -71,11 +69,11 @@
case a
when 1
when 2, 4, 8
bits = a - 1
@idx = @idx + bits & ~bits
- raise IncompleteBufferException if @idx > @buffy.size
+ raise IncompleteBufferException if @idx > @buffy.bytesize
else
raise "Unsupported alignment #{a}"
end
end
@@ -84,32 +82,32 @@
# Yes : Message marshalling code needs to align "body" to 8 byte boundary
private
# Retrieve the next _nbytes_ number of bytes from the buffer.
def get(nbytes)
- raise IncompleteBufferException if @idx + nbytes > @buffy.size
+ raise IncompleteBufferException if @idx + nbytes > @buffy.bytesize
ret = @buffy.slice(@idx, nbytes)
@idx += nbytes
ret
end
# Retrieve the series of bytes until the next NULL (\0) byte.
def get_nul_terminated
raise IncompleteBufferException if not @buffy[@idx..-1] =~ /^([^\0]*)\0/
str = $1
- raise IncompleteBufferException if @idx + str.size + 1 > @buffy.size
- @idx += str.size + 1
+ raise IncompleteBufferException if @idx + str.bytesize + 1 > @buffy.bytesize
+ @idx += str.bytesize + 1
str
end
# Get the string length and string itself from the buffer.
# Return the string.
def get_string
align(4)
str_sz = get(4).unpack(@uint32)[0]
ret = @buffy.slice(@idx, str_sz)
- raise IncompleteBufferException if @idx + str_sz + 1 > @buffy.size
+ raise IncompleteBufferException if @idx + str_sz + 1 > @buffy.bytesize
@idx += str_sz
if @buffy[@idx].ord != 0
raise InvalidPacketException, "String is not nul-terminated"
end
@idx += 1
@@ -120,11 +118,11 @@
# Get the signature length and signature itself from the buffer.
# Return the signature.
def get_signature
str_sz = get(1).unpack('C')[0]
ret = @buffy.slice(@idx, str_sz)
- raise IncompleteBufferException if @idx + str_sz + 1 >= @buffy.size
+ raise IncompleteBufferException if @idx + str_sz + 1 >= @buffy.bytesize
@idx += str_sz
if @buffy[@idx].ord != 0
raise InvalidPacketException, "Type is not nul-terminated"
end
@idx += 1
@@ -191,11 +189,11 @@
# checks please
array_sz = get(4).unpack(@uint32)[0]
raise InvalidPacketException if array_sz > 67108864
align(signature.child.alignment)
- raise IncompleteBufferException if @idx + array_sz > @buffy.size
+ raise IncompleteBufferException if @idx + array_sz > @buffy.bytesize
packet = Array.new
start_idx = @idx
while @idx - start_idx < array_sz
packet << do_parse(signature.child)
@@ -221,10 +219,11 @@
packet = do_parse(sig)
when Type::OBJECT_PATH
packet = get_string
when Type::STRING
packet = get_string
+ packet.force_encoding('UTF-8') if RUBY_VERSION >= '1.9'
when Type::SIGNATURE
packet = get_signature
when Type::DICT_ENTRY
align(8)
key = do_parse(signature.members[0])
@@ -238,11 +237,11 @@
end # def do_parse
end # class PacketUnmarshaller
# D-Bus packet marshaller class
#
- # Class that handles the conversion (unmarshalling) of Ruby objects to
+ # Class that handles the conversion (marshalling) of Ruby objects to
# (binary) payload data.
class PacketMarshaller
# The current or result packet.
# FIXME: allow access only when marshalling is finished
attr_reader :packet
@@ -265,35 +264,35 @@
end
end
# Align the buffer with NULL (\0) bytes on a byte length of _a_.
def align(a)
- @packet = @packet.ljust(num_align(@offset + @packet.length, a) - @offset, 0.chr)
+ @packet = @packet.ljust(num_align(@offset + @packet.bytesize, a) - @offset, 0.chr)
end
# Append the the string _str_ itself to the packet.
def append_string(str)
align(4)
- @packet += [str.length].pack("L") + str + "\0"
+ @packet += [str.bytesize].pack("L") + [str].pack("Z*")
end
# Append the the signature _signature_ itself to the packet.
def append_signature(str)
- @packet += str.length.chr + str + "\0"
+ @packet += str.bytesize.chr + str + "\0"
end
# Append the array type _type_ to the packet and allow for appending
# the child elements.
def array(type)
# Thanks to Peter Rullmann for this line
align(4)
- sizeidx = @packet.size
+ sizeidx = @packet.bytesize
@packet += "ABCD"
align(type.alignment)
- contentidx = @packet.size
+ contentidx = @packet.bytesize
yield
- sz = @packet.size - contentidx
+ sz = @packet.bytesize - contentidx
raise InvalidPacketException if sz > 67108864
@packet[sizeidx...sizeidx + 4] = [sz].pack("L")
end
# Align and allow for appending struct fields.
@@ -306,10 +305,12 @@
def append_simple_string(s)
@packet += s + "\0"
end
# Append a value _val_ to the packet based on its _type_.
+ #
+ # Host native endianness is used, declared in Message#marshall
def append(type, val)
raise TypeException, "Cannot send nil" if val.nil?
type = type.chr if type.kind_of?(Fixnum)
type = Type::Parser.new(type).parse[0] if type.kind_of?(String)
@@ -370,10 +371,10 @@
vartype = Type::Parser.new(vartype).parse[0]
end
append_signature(vartype.to_s)
align(vartype.alignment)
- sub = PacketMarshaller.new(@offset + @packet.length)
+ sub = PacketMarshaller.new(@offset + @packet.bytesize)
sub.append(vartype, vardata)
@packet += sub.packet
when Type::ARRAY
if val.kind_of?(Hash)
raise TypeException, "Expected an Array but got a Hash" if type.child.sigtype != Type::DICT_ENTRY