2 # Copyright (c) 2005-2007 Tilman Sauerbeck (tilman at code-monkey de)
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 def to_eet_chunks(tag, type = nil) # :nodoc:
27 [Eet::Chunk.new(tag, to_eet)]
33 # object.to_eet_name -> string
35 # Returns the tag that's stored with the data for _object_.
36 # If your class doesn't override this method, the class name will be
43 # object.to_eet_properties -> hash
45 # Returns a hash that contains the properties that are stored for
47 # If your class doesn't override this method, all instance variables
48 # of _object_ will be stored.
50 instance_variables.inject({}) do |h, var|
51 h[var[1..-1]] = [instance_variable_get(var)]
57 class String # :nodoc:
58 def to_eet_chunks(tag, type = nil)
59 [Eet::Chunk.new(tag, self + "\0")]
63 class TrueClass # :nodoc:
64 def to_eet_chunks(tag, type = nil)
65 [Eet::Chunk.new(tag, "\1")]
69 class FalseClass # :nodoc:
70 def to_eet_chunks(tag, type = nil)
71 [Eet::Chunk.new(tag, "\0")]
76 def to_eet_chunks(tag, type = nil)
79 [Eet::Chunk.new(tag, self.to_eet)]
81 # lists always hold subtypes
82 map { |item| Eet::Chunk.new(tag, item.to_eet) }
88 def to_eet_chunks(tag, type = nil)
89 # lists always hold subtypes
90 map { |(key, value)| Eet::Chunk.new(tag, value.to_eet) }
97 class ChunkError < EetError; end
99 class Stream # :nodoc:
100 def initialize(chunk = nil)
101 super(chunk.nil? ? 0 : 1, chunk)
104 def Stream.deserialize(data)
105 if data.to_str.empty?
106 raise(ArgumentError, "buffer is empty")
112 while offset < data.length
113 c, bytes = Chunk.deserialize(data[offset..-1])
123 class Chunk # :nodoc:
124 def Chunk.deserialize(data)
125 if data.to_str.empty?
126 raise(ArgumentError, "buffer is empty")
129 if data.length < 8 || data[0, 4] != "CHnK"
130 raise(ChunkError, "invalid data")
133 size = data[4, 4].unpack("V").first
134 if size >= (1 << 31) || size > data.length - 8
135 raise(ChunkError, "invalid chunk size")
138 unless data[8, size].include?(0)
139 raise(ChunkError, "invalid chunk data")
142 c = Chunk.new(*data[8, size].split("\0", 2))