2 # $Id: eet.rb 59 2005-06-08 16:13:15Z 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.
28 def to_eet_chunks(tag, type = nil) # :nodoc:
29 [Eet::Chunk.new(tag, to_eet)]
35 # object.to_eet_name -> string
37 # Returns the tag that's stored with the data for _object_.
38 # If your class doesn't override this method, the class name will be
45 # object.to_eet_properties -> hash
47 # Returns a hash that contains the properties that are stored for
49 # If your class doesn't override this method, all instance variables
50 # of _object_ will be stored.
52 instance_variables.inject({}) do |h, var|
53 h[var[1..-1]] = [instance_variable_get(var)]
59 class Integer # :nodoc:
60 def to_eet_chunks(tag, type = nil)
68 data = [self].pack(fmt)
69 [Eet::Chunk.new(tag, data)]
73 class String # :nodoc:
74 def to_eet_chunks(tag, type = nil)
75 [Eet::Chunk.new(tag, self + "\0")]
79 class TrueClass # :nodoc:
80 def to_eet_chunks(tag, type = nil)
81 [Eet::Chunk.new(tag, "\1")]
85 class FalseClass # :nodoc:
86 def to_eet_chunks(tag, type = nil)
87 [Eet::Chunk.new(tag, "\0")]
92 def to_eet_chunks(tag, type = nil)
95 [Eet::Chunk.new(tag, self.to_eet)]
97 # lists always hold subtypes
98 map { |item| Eet::Chunk.new(tag, item.to_eet) }
104 def to_eet_chunks(tag, type = nil)
105 # lists always hold subtypes
106 map { |(key, value)| Eet::Chunk.new(tag, value.to_eet) }
113 class ChunkError < EetError; end
115 class Stream # :nodoc:
116 def initialize(chunk = nil)
117 super(chunk.nil? ? 0 : 1, chunk)
120 def Stream.deserialize(data)
121 if data.to_str.empty?
122 raise(ArgumentError, "buffer is empty")
128 while offset < data.length
129 c, bytes = Chunk.deserialize(data[offset..-1])
139 class Chunk # :nodoc:
140 def Chunk.deserialize(data)
141 if data.to_str.empty?
142 raise(ArgumentError, "buffer is empty")
145 if data.length < 8 || data[0, 4] != "CHnK"
146 raise(ChunkError, "invalid data")
149 size = data[4, 4].unpack("V").first
150 if size >= (1 << 31) || size > data.length - 8
151 raise(ChunkError, "invalid chunk size")
154 unless data[8, size].include?(0)
155 raise(ChunkError, "invalid chunk data")
158 c = Chunk.new(*data[8, size].split("\0", 2))