#--
-# $Id: eet.rb 39 2005-05-18 18:54:31Z tilman $
+# $Id: eet.rb 47 2005-05-30 19:19:07Z tilman $
#
# Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
#
class TrueClass # :nodoc:
def to_eet_chunks(tag, type = nil)
- [Eet::Chunk.new(tag, [1].pack("c"))]
+ [Eet::Chunk.new(tag, "\1")]
end
end
class FalseClass # :nodoc:
def to_eet_chunks(tag, type = nil)
- [Eet::Chunk.new(tag, [0].pack("c"))]
+ [Eet::Chunk.new(tag, "\0")]
end
end
end
def Stream.deserialize(data)
- data = data.to_str.dup
+ if data.to_str.empty?
+ raise(ArgumentError, "buffer is empty")
+ end
+
s = Stream.new
+ offset = 0
+
+ while offset < data.length
+ c, bytes = Chunk.deserialize(data[offset..-1])
- while data.length > 0
- s << Chunk.deserialize(data)
+ s << c
+ offset += bytes
end
s
end
class Chunk # :nodoc:
- attr_reader :tag, :data
-
- def initialize(tag, data)
- if tag.to_str.include?(0)
- raise(ArgumentError,
- "tag must not contain binary zeroes")
- end
-
- @tag = tag.to_str.dup.freeze
- @data = data.to_str.dup.freeze
-
- # libeet uses a signed 32bit integer to store the
- # chunk size, so make sure we don't overflow it
- if (@tag.length + 1 + @data.length) >= (1 << 31)
- raise(ArgumentError, "tag or data too long")
+ def Chunk.deserialize(data)
+ if data.to_str.empty?
+ raise(ArgumentError, "buffer is empty")
end
- end
- def Chunk.deserialize(data)
if data.length < 8 || data[0, 4] != "CHnK"
raise(ChunkError, "invalid data")
end
c = Chunk.new(*data[8, size].split("\0", 2))
- data.replace(data[8 + size..-1] || "")
-
- c
+ [c, 8 + size]
end
end
end