projects
/
ruby-eet.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Chunk.deserialize fails if the given buffer is empty.
[ruby-eet.git]
/
lib
/
eet.rb
diff --git
a/lib/eet.rb
b/lib/eet.rb
index e0c7b72554bb9099713ea9256ec9ee848acf73eb..8a2893ef3b6b631cd44119684879bc0a7fa99dbb 100644
(file)
--- a/
lib/eet.rb
+++ b/
lib/eet.rb
@@
-1,5
+1,5
@@
#--
#--
-# $Id: eet.rb
23 2005-04-09 17:16:58
Z tilman $
+# $Id: eet.rb
46 2005-05-25 20:10:37
Z tilman $
#
# Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
#
#
# Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
#
@@
-46,7
+46,7
@@
class Object
props.each_pair do |tag, arg|
unless arg.is_a?(Array)
props.each_pair do |tag, arg|
unless arg.is_a?(Array)
- raise(Eet::PropertyError, "hash value not an array")
+ raise(Eet::PropertyError, "hash value
is
not an array")
end
value, type = arg
end
value, type = arg
@@
-124,13
+124,13
@@
end
class TrueClass # :nodoc:
def to_eet_chunks(tag, type = nil)
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)
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
end
@@
-161,21
+161,24
@@
module Eet
class PropertyError < EetError; end
class ChunkError < EetError; end
class PropertyError < EetError; end
class ChunkError < EetError; end
- class Stream
< Array
# :nodoc:
+ class Stream # :nodoc:
def initialize(chunk = nil)
super(chunk.nil? ? 0 : 1, chunk)
end
def initialize(chunk = nil)
super(chunk.nil? ? 0 : 1, chunk)
end
- def serialize
- inject("") { |a, c| a << c.serialize }
- end
-
def Stream.deserialize(data)
def Stream.deserialize(data)
- data = data.to_str.dup
+ if data.to_str.empty?
+ raise(ArgumentError, "buffer is empty")
+ end
+
s = Stream.new
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
s
@@
-194,22
+197,18
@@
module Eet
@tag = tag.to_str.dup.freeze
@data = data.to_str.dup.freeze
@tag = tag.to_str.dup.freeze
@data = data.to_str.dup.freeze
- @size = @tag.length + 1 + @data.length
-
# libeet uses a signed 32bit integer to store the
# chunk size, so make sure we don't overflow it
# libeet uses a signed 32bit integer to store the
# chunk size, so make sure we don't overflow it
- if
@size
>= (1 << 31)
+ if
(@tag.length + 1 + @data.length)
>= (1 << 31)
raise(ArgumentError, "tag or data too long")
end
end
raise(ArgumentError, "tag or data too long")
end
end
- def serialize
- buf = "CHnK"
- buf << [@size].pack("V")
- buf << @tag << "\0" << @data
- end
-
def Chunk.deserialize(data)
def Chunk.deserialize(data)
+ if data.to_str.empty?
+ raise(ArgumentError, "buffer is empty")
+ end
+
if data.length < 8 || data[0, 4] != "CHnK"
raise(ChunkError, "invalid data")
end
if data.length < 8 || data[0, 4] != "CHnK"
raise(ChunkError, "invalid data")
end
@@
-225,9
+224,7
@@
module Eet
c = Chunk.new(*data[8, size].split("\0", 2))
c = Chunk.new(*data[8, size].split("\0", 2))
- data.replace(data[8 + size..-1] || "")
-
- c
+ [c, 8 + size]
end
end
end
end
end
end