File: amf_string_buffer.rb

package info (click to toggle)
flvtool2 1.0.6-4
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 56,240 kB
  • ctags: 388
  • sloc: ruby: 2,675; makefile: 45; xml: 30
file content (280 lines) | stat: -rw-r--r-- 6,532 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# Copyright (c) 2005 Norman Timmler (inlet media e.K., Hamburg, Germany)
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class AMFStringBuffer
  def initialize(str = nil)
    @buffer = str || ''
    @pos = 0
  end
  
  def seek(pos)
    @pos = pos
  end
  
  def read(length)
    raise EOFError if @pos + length > @buffer.length
    rt = @buffer[@pos, length]
    @pos += length
    rt
  end
  
  def readchar
    read(1).unpack('C').first
  end
  
  def write(str)
    if @pos + str.length > @buffer.length
      @buffer << ' ' * (@pos + str.length - @buffer.length)
    end
    @buffer[@pos, str.length] = str
    @pos += str.length
  end

  def close
  end
  
  def to_s
    @buffer
  end

  def length
    @buffer.length
  end

  def eof?
    @pos == length
  end

  def pos
    @pos
  end
  
  def read__AMF_string
    read(read__UI16)
  end
  
  def read__AMF_double
    num = read(8).unpack('G').first.to_f
  end

  def read__AMF_boolean
    read__UI8 == 1
  end
  
  def read__AMF_mixed_array
    size = read__UI32 # is not used
    hash = {}
    while !eof?
      key = read__AMF_string
      break if key.empty? && (type = read__UI8) == 9
      hash[key] = read__AMF_data(type)
    end
    hash
  end

  def read__AMF_object
    object = Object.new
    while !eof?
      key = read__AMF_string
      break if key.empty? && (type = read__UI8) == 9
      object.instance_variable_set( eval(":@#{key}"), read__AMF_data(type) )
    end
    object
  end

  def read__AMF_array
    size = read__UI32
    array = []
    (1..size).step do |pos|  
      break if eof?
      array << read__AMF_data
    end
    array
  end
  
  def read__AMF_date
    utc_time = Time.at((read__AMF_double / 1000).to_i)
    utc_time + (read__SI16 * 60) - Time.now.gmtoff
  end
  
  def read__AMF_data(type = nil)
    type ||= read__UI8
    value = case type.to_i
    when  0
      read__AMF_double
    when  1
      read__AMF_boolean
    when  2
      read__AMF_string
    when  3
      read__AMF_object
    when  8
      read__AMF_mixed_array
    when 10
      read__AMF_array
    when 11
      read__AMF_date
    else
    end
    return value
  end
  
  def write__AMF_string(str)
    write__UI8 2
    write__UI16 str.length
    write str
  end
  
  def write__AMF_double(value)
    write__UI8 0
    write [value].pack('G')
  end

  def write__AMF_boolean(value)
    write__UI8 1
    value = value ? 1 : 0
    write [value].pack('C')
  end
  
  def write__AMF_date(time)
    write__UI8 11
    write [(time.to_f * 1000.0)].pack('G')
    write__SI16( (Time.now.gmtoff / 60).to_i ) 
  end
  
  def write__AMF_data(object)
    if object === true || object === false
      write__AMF_boolean object
    elsif object.kind_of? Numeric
      write__AMF_double object
    elsif object.kind_of? Time
      write__AMF_date object
    elsif object.kind_of? Hash
      write__AMF_mixed_array object
    elsif object.kind_of? String
      write__AMF_string object
    elsif object.kind_of? Array
      write__AMF_array object
    else
      write__AMF_object object
    end
  end
  
  def write__AMF_key(key)
    write__UI16 key.length
    write key
  end
  
  def write__AMF_mixed_array(hash)
    write__UI8 8
    write__UI32 hash.length # length will never be read
    
    hash.each_pair do |key, value|
      write__AMF_key key
      write__AMF_data value
    end
    
    write__UI16 0
    write__UI8 9
  end

  def write__AMF_array(array)
    write__UI8 10
    write__UI32 array.length
    
    array.each do |value|
      write__AMF_data value
    end
  end

  def write__AMF_object(object)
    write__UI8 3
    
    object.instance_variables.each do |variable|
      write__AMF_key variable.gsub('@', '')
      write__AMF_data object.instance_variable_get( variable.intern )
    end
    
    write__UI16 0
    write__UI8 9
  end
  
  # FIXME: This methods are copied from flv_stream.rb. Should get in here per
  # include? or something like this.
  def read__UI8
    readchar
  end
  
  def read__UI16
    (readchar << 8) + readchar
  end
  
  def read__UI24
    (readchar << 16) + (readchar << 8) + readchar
  end
  
  def read__UI32
    (readchar << 24) + (readchar << 16) + (readchar << 8) + readchar
  end
  
  def read__STRING(length)
    read length
  end
    
  def read__SI16
    read(2).reverse.unpack('s').first.to_i
  end
  
  def write__UI8(value)
    write [value].pack('C')
  end
  
  def write__UI16(value)
    write [(value >> 8) & 0xff].pack('c')
    write [value & 0xff].pack('c')
  end

  def write__UI24(value)
    write [value >> 16].pack('c')
    write [(value >> 8) & 0xff].pack('c')
    write [value & 0xff].pack('c')
  end
  
  def write__UI32(value)
    write [value].pack('N')
  end
  
  def write__SI16(value)
    write [(value >> 8) & 0xff].pack('c')
    write [value & 0xff].pack('c')
  end

  def write__STRING(string)
    write string
  end
  alias_method :<<, :write__STRING
  
end