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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
require 'qpid/packer.rb'
require 'iconv'
module Qpid
class Codec
include Qpid::Packer
attr_reader :spec
def initialize(spec = "")
@spec = spec
end
def write_void(v)
unless v.nil?
raise Exception.new("void not nil: #{v}")
end
end
def read_void
return nil
end
def write_bit(b)
unless b
raise Exception.new("bit is nil: #{b}")
end
end
def read_bit
return true
end
def read_uint8
return unpack("C", 1)
end
def write_uint8(n)
return pack("C", n)
end
def read_int8
return unpack("c", 1)
end
def write_int8(n)
pack("c", n)
end
def read_char
return unpack("c", 1)
end
def write_char(c)
pack("c")
end
def read_boolean
return read_uint8 != 0
end
def write_boolean(b)
n = 0
n = 1 if b != 0
write_uint8(n)
end
def read_uint16
return unpack("n", 2)
end
def write_uint16(n)
pack("n", n)
end
def read_int16
# XXX: holy moly.. pack/unpack doesn't have signed network byte order. Crazy hackery.
val = unpack("n", 2)
val -= 2 ** 16 if val >= 2 ** 15
return val
end
def write_int16(n)
# XXX: Magically this one works even though it's not signed.
pack("n", n)
end
def read_uint32
return unpack("N", 4)
end
def write_uint32(n)
pack("N", n)
end
def read_int32
# Again no pack/unpack for signed int
return unpack("N", 4)
end
def write_int32(n)
# FIXME
pack("N", n)
end
def read_float
return unpack("g", 4)
end
def write_float(n)
pack("g", n)
end
def read_sequence_no
return read_uint32.to_serial
end
def write_sequence_no(n)
write_uint32(n.value)
end
def encode_64bit(num, signed = false)
b = []
if num < 0 && signed
num += 2 ** 64
end
(0..7).each do |c|
d = 7 - c
b[c] = (num & (0xff << d * 8)) >> d * 8
end
pack('C8', *b)
end
def decode_64bit(signed = false)
# Silly ruby pack/unpack does not implement 64 bit network byte order
# encode/decode.
a = unpack('C8', 8)
num = 0
(0..7).each do |c|
d = 7 - c
num |= a[c] << 8 * d
end
if signed && num >= 2 ** 63
num -= 2 ** 64
end
return num
end
def read_uint64
return decode_64bit
end
def write_uint64(n)
encode_64bit(n)
end
def read_int64
return decode_64bit(signed = true)
end
def write_int64(n)
encode_64bit(n, signed = true)
end
def read_datetime
return read_uint64
end
def write_datetime(n)
write_uint64(n)
end
def read_double
return unpack("G", 8)
end
def write_double(n)
pack("G", n)
end
def read_vbin8
# XXX
return read(read_uint8)
end
def write_vbin8(b)
# XXX
write_uint8(b.length)
write(b)
end
def read_str8
# FIXME: Check iconv.. I think this will throw if there are odd characters.
return Iconv.conv("ASCII", "UTF-8", read_vbin8)
end
def write_str8(s)
write_vbin8(Iconv.conv("UTF-8", "ASCII", s))
end
def read_str16
return Iconv.conv("ASCII", "UTF-8", read_vbin16)
end
def write_str16(s)
write_vbin16(Iconv.conv("UTF-8", "ASCII", s))
end
def read_vbin16
# XXX: Using read method?
return read(read_uint16)
end
def write_vbin16(b)
write_uint16(b.length)
write(b)
end
def read_sequence_set
# FIXME: Need datatypes
result = RangedSet.new
size = read_uint16
nranges = size / 8
nranges.times do |i|
lower = read_sequence_no
upper = read_sequence_no
result.add(lower, upper)
end
return result
end
def write_sequence_set(ss)
size = 8 * ss.ranges.length
write_uint16(size)
ss.ranges.each do |range|
write_sequence_no(range.lower)
write_sequence_no(range.upper)
end
end
def read_vbin32
return read(read_uint32)
end
def write_vbin32(b)
write_uint32(b.length)
write(b)
end
def write_map(m)
sc = StringCodec.new(@spec)
unless m.nil?
sc.write_uint32(m.size)
m.each do |k, v|
unless type = @spec.encoding(v.class)
raise Exception.new("no encoding for: #{v.class}")
end
sc.write_str8(k)
sc.write_uint8(type.code)
type.encode(sc, v)
end
end
write_vbin32(sc.encoded)
end
def read_map
sc = StringCodec.new(@spec, read_vbin32)
return nil unless sc.encoded
count = sc.read_uint32
result = nil
if count
result = {}
until sc.encoded.empty?
k = sc.read_str8
code = sc.read_uint8
type = @spec.types[code]
v = type.decode(sc)
result[k] = v
end
end
return result
end
def write_array(a)
sc = StringCodec.new(@spec)
unless a.nil?
if a.length > 0
type = @spec.encoding(a[0].class)
else
type = @spec.encoding(nil.class)
end
sc.write_uint8(type.code)
sc.write_uint32(a.size)
a.each { |o| type.encode(sc, o) }
end
write_vbin32(sc.encoded)
end
def read_array
sc = StringCodec.new(@spec, read_vbin32)
return nil if not sc.encoded
type = @spec.types[sc.read_uint8]
count = sc.read_uint32
result = nil
if count
result = []
count.times { |i| result << (type.decode(sc)) }
end
return result
end
def write_list(l)
sc = StringCodec.new(@spec)
unless l.nil?
sc.write_uint32(l.length)
l.each do |o|
type = @spec.encoding(o.class)
sc.write_uint8(type.code)
type.encode(sc, o)
end
end
write_vbin32(sc.encoded)
end
def read_list
sc = StringCodec.new(@spec, read_vbin32)
return nil if not sc.encoded
count = sc.read_uint32
result = nil
if count
result = []
count.times do |i|
type = @spec.types[sc.read_uint8]
result << type.decode(sc)
end
end
return result
end
def read_struct32
size = read_uint32
code = read_uint16
type = @spec.structs[code]
# XXX: BLEH!
fields = type.decode_fields(self)
return Qpid::struct(type, fields)
end
def write_struct32(value)
type = value.st_type
sc = StringCodec.new(@spec)
sc.write_uint16(type.code)
type.encode_fields(sc, value)
write_vbin32(sc.encoded)
end
def read_control
cntrl = @spec.controls[read_uint16]
return Qpid::struct(cntrl, cntrl.decode_fields(self))
end
def write_control(ctrl)
type = ctrl.st_type
write_uint16(type.code)
type.encode_fields(self, ctrl)
end
def read_command
type = @spec.commands[read_uint16]
hdr = @spec[:header].decode(self)
cmd = Qpid::struct(type, type.decode_fields(self))
return hdr, cmd
end
def write_command(hdr, cmd)
type = cmd.st_type
write_uint16(type.code)
hdr.st_type.encode(self, hdr)
type.encode_fields(self, cmd)
end
def read_size(width)
if width > 0
return send(:"read_uint#{width * 8}")
end
end
def write_size(width, n)
if width > 0
send(:"write_uint#{width * 8}", n)
end
end
def read_uuid
return unpack("a16", 16)
end
def write_uuid(s)
pack("a16", s)
end
def read_bin128
return unpack("a16", 16)
end
def write_bin128(b)
pack("a16", b)
end
end
class StringCodec < Codec
def initialize(spec, encoded = "")
@spec = spec
@encoded = encoded
end
attr_reader :encoded
def write(s)
@encoded += s
end
def read(n)
return "" if n.nil?
result = @encoded[0...n]
@encoded = @encoded[n...@encoded.size] || ""
return result
end
end
end
|