2 # $Id: eet.rb 23 2005-04-09 17:16:58Z tilman $
4 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 # object.to_eet -> string
31 # Serializes the receiver to EET format.
33 props = to_eet_properties
35 unless props.is_a?(Hash) && !props.empty?
36 raise(Eet::PropertyError, "invalid EET properties")
39 eet_name = to_eet_name
41 if eet_name.to_str.length < 1 || eet_name.to_str.include?(0)
42 raise(Eet::NameError, "invalid EET name")
45 stream = Eet::Stream.new
47 props.each_pair do |tag, arg|
48 unless arg.is_a?(Array)
49 raise(Eet::PropertyError, "hash value not an array")
55 stream.push(*value.to_eet_chunks(tag, type))
58 chunk = Eet::Chunk.new(eet_name, stream.serialize)
59 Eet::Stream.new(chunk).serialize
62 def to_eet_chunks(tag, type = nil) # :nodoc:
63 [Eet::Chunk.new(tag, to_eet)]
69 # object.to_eet_name -> string
71 # Returns the tag that's stored with the data for _object_.
72 # If your class doesn't override this method, the class name will be
79 # object.to_eet_properties -> hash
81 # Returns a hash that contains the properties that are stored for
83 # If your class doesn't override this method, all instance variables
84 # of _object_ will be stored.
86 instance_variables.inject({}) do |h, var|
87 h[var[1..-1]] = [instance_variable_get(var)]
93 class Integer # :nodoc:
94 def to_eet_chunks(tag, type = nil)
102 data = [self].pack(fmt)
103 [Eet::Chunk.new(tag, data)]
107 class Float # :nodoc:
108 def to_eet_chunks(tag, type = nil)
110 when :double: "%32.32f"
115 [Eet::Chunk.new(tag, data + "\0")]
119 class String # :nodoc:
120 def to_eet_chunks(tag, type = nil)
121 [Eet::Chunk.new(tag, self + "\0")]
125 class TrueClass # :nodoc:
126 def to_eet_chunks(tag, type = nil)
127 [Eet::Chunk.new(tag, [1].pack("c"))]
131 class FalseClass # :nodoc:
132 def to_eet_chunks(tag, type = nil)
133 [Eet::Chunk.new(tag, [0].pack("c"))]
137 class Array # :nodoc:
138 def to_eet_chunks(tag, type = nil)
141 [Eet::Chunk.new(tag, self.to_eet)]
143 # lists always hold subtypes
144 map { |item| Eet::Chunk.new(tag, item.to_eet) }
150 def to_eet_chunks(tag, type = nil)
151 # lists always hold subtypes
152 map { |(key, value)| Eet::Chunk.new(tag, value.to_eet) }
159 class EetError < StandardError; end
160 class NameError < EetError; end
161 class PropertyError < EetError; end
162 class ChunkError < EetError; end
164 class Stream < Array # :nodoc:
165 def initialize(chunk = nil)
166 super(chunk.nil? ? 0 : 1, chunk)
170 inject("") { |a, c| a << c.serialize }
173 def Stream.deserialize(data)
174 data = data.to_str.dup
177 while data.length > 0
178 s << Chunk.deserialize(data)
185 class Chunk # :nodoc:
186 attr_reader :tag, :data
188 def initialize(tag, data)
189 if tag.to_str.include?(0)
191 "tag must not contain binary zeroes")
194 @tag = tag.to_str.dup.freeze
195 @data = data.to_str.dup.freeze
197 @size = @tag.length + 1 + @data.length
199 # libeet uses a signed 32bit integer to store the
200 # chunk size, so make sure we don't overflow it
201 if @size >= (1 << 31)
202 raise(ArgumentError, "tag or data too long")
208 buf << [@size].pack("V")
209 buf << @tag << "\0" << @data
212 def Chunk.deserialize(data)
213 if data.length < 8 || data[0, 4] != "CHnK"
214 raise(ChunkError, "invalid data")
217 size = data[4, 4].unpack("V").first
218 if size >= (1 << 31) || size > data.length - 8
219 raise(ChunkError, "invalid chunk size")
222 unless data[8, size].include?(0)
223 raise(ChunkError, "invalid chunk data")
226 c = Chunk.new(*data[8, size].split("\0", 2))
228 data.replace(data[8 + size..-1] || "")