Module: Hessian2::Parser

Includes:
Constants
Included in:
Hessian2
Defined in:
lib/hessian2/parser.rb

Constant Summary

Constants included from Constants

Constants::BC_BINARY, Constants::BC_BINARY_CHUNK, Constants::BC_BINARY_DIRECT, Constants::BC_BINARY_SHORT, Constants::BC_CLASS_DEF, Constants::BC_DATE, Constants::BC_DATE_MINUTE, Constants::BC_DOUBLE, Constants::BC_DOUBLE_BYTE, Constants::BC_DOUBLE_MILL, Constants::BC_DOUBLE_ONE, Constants::BC_DOUBLE_SHORT, Constants::BC_DOUBLE_ZERO, Constants::BC_END, Constants::BC_FALSE, Constants::BC_INT, Constants::BC_INT_BYTE_ZERO, Constants::BC_INT_SHORT_ZERO, Constants::BC_INT_ZERO, Constants::BC_LIST_DIRECT, Constants::BC_LIST_DIRECT_UNTYPED, Constants::BC_LIST_FIXED, Constants::BC_LIST_FIXED_UNTYPED, Constants::BC_LIST_VARIABLE, Constants::BC_LIST_VARIABLE_UNTYPED, Constants::BC_LONG, Constants::BC_LONG_BYTE_ZERO, Constants::BC_LONG_INT, Constants::BC_LONG_SHORT_ZERO, Constants::BC_LONG_ZERO, Constants::BC_MAP, Constants::BC_MAP_UNTYPED, Constants::BC_NULL, Constants::BC_OBJECT, Constants::BC_OBJECT_DEF, Constants::BC_OBJECT_DIRECT, Constants::BC_REF, Constants::BC_STRING, Constants::BC_STRING_CHUNK, Constants::BC_STRING_DIRECT, Constants::BC_STRING_SHORT, Constants::BC_TRUE, Constants::BINARY_DIRECT_MAX, Constants::BINARY_SHORT_MAX, Constants::INT_BYTE_MAX, Constants::INT_BYTE_MIN, Constants::INT_DIRECT_MAX, Constants::INT_DIRECT_MIN, Constants::INT_SHORT_MAX, Constants::INT_SHORT_MIN, Constants::LIST_DIRECT_MAX, Constants::LONG_BYTE_MAX, Constants::LONG_BYTE_MIN, Constants::LONG_DIRECT_MAX, Constants::LONG_DIRECT_MIN, Constants::LONG_SHORT_MAX, Constants::LONG_SHORT_MIN, Constants::OBJECT_DIRECT_MAX, Constants::PACKET_DIRECT_MAX, Constants::PACKET_SHORT_MAX, Constants::P_PACKET, Constants::P_PACKET_CHUNK, Constants::P_PACKET_DIRECT, Constants::P_PACKET_SHORT, Constants::STRING_DIRECT_MAX, Constants::STRING_SHORT_MAX

Instance Method Summary collapse

Instance Method Details

#parse(data, klass = nil) ⇒ Object



35
36
37
# File 'lib/hessian2/parser.rb', line 35

def parse(data, klass = nil)
  parse_bytes(data.each_byte, klass)
end

#parse_binary(bytes) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/hessian2/parser.rb', line 313

def parse_binary(bytes)
  bc = bytes.next
  case bc
  when 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
       0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
    read_binary_direct(bytes, bc)
  when 0x34, 0x35, 0x36, 0x37
    read_binary_short(bytes, bc)
  when 0x41
    read_binary_chunk(bytes)
  when 0x42
    read_binary(bytes)
  else
    raise sprintf("%#x is not a binary", bc)
  end
end

#parse_bytes(bytes, klass = nil, refs = [], cdefs = []) ⇒ Object



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
# File 'lib/hessian2/parser.rb', line 39

def parse_bytes(bytes, klass = nil, refs = [], cdefs = [])
  bc = bytes.next
  case bc
  when 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
       0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 
       0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
       0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
    # 0x00 - 0x1f utf-8 string length 0-31
    read_string_direct(bytes, bc)
  when 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
       0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
    # 0x20 - 0x2f binary data length 0-15
    read_binary_direct(bytes, bc)
  when 0x30, 0x31, 0x32, 0x33
    # 0x30 - 0x33 utf-8 string length 0-1023
    read_string_short(bytes, bc)
  when 0x34, 0x35, 0x36, 0x37
    # 0x34 - 0x37 binary data length 0-1023
    read_binary_short(bytes, bc)
  when 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
    # 0x38 - 0x3f three-octet compact long (-x40000 to x3ffff)
    read_long_short_zero(bytes, bc)
  when 0x41 # 8-bit binary data non-final chunk ('A')
    read_binary_chunk(bytes)
  when 0x42 # 8-bit binary data final chunk ('B')
    read_binary(bytes)
  when 0x43 # object type definition ('C')
    name = parse_string(bytes)
    fields = []

    parse_int(bytes).times do
      fields << parse_string(bytes)
    end
    cdefs << Struct.new(*fields.map{|f| f.to_sym})

    parse_bytes(bytes, klass, refs, cdefs)
  when 0x44 # 64-bit IEEE encoded double ('D')
    read_double(bytes)
  when 0x46 # boolean false ('F')
    false
  when 0x48 # untyped map ('H')
    val = {}
    refs << val # store a value reference first
    while bytes.peek != BC_END
      val[parse_bytes(bytes, klass, refs, cdefs)] = parse_bytes(bytes, klass, refs, cdefs)
    end

    bytes.next
    val
  when 0x49 # 32-bit signed integer ('I')
    read_int(bytes)
  when 0x4a # 64-bit UTC millisecond date
    read_date(bytes)
  when 0x4b # 32-bit UTC minute date
    read_date_minute(bytes)
  when 0x4c # 64-bit signed long integer ('L')
    read_long(bytes)
  when 0x4d # map with type ('M')
    parse_type(bytes) # skip type
    val = {}
    refs << val
    while bytes.peek != BC_END
      val[parse_bytes(bytes, klass, refs, cdefs)] = parse_bytes(bytes, klass, refs, cdefs)
    end

    bytes.next
    val
  when 0x4e # null ('N')
    nil
  when 0x4f # object instance ('O')
    cdef = cdefs[parse_int(bytes)]
    val = cdef.new
    refs << val # store a value reference first
    val.members.each do |sym|
      val[sym] = parse_bytes(bytes, klass, refs, cdefs)
    end

    val
  when 0x51 # reference to map/list/object - integer ('Q')
    refs[parse_int(bytes)]
  when 0x52 # utf-8 string non-final chunk ('R')
    read_string_chunk(bytes)
  when 0x53 # utf-8 string final chunk ('S')
    read_string(bytes)
  when 0x54 # boolean true ('T')
    true
  when 0x55 # variable-length list/vector ('U')
    parse_type(bytes)
    if klass && !klass.is_a?(Array) # parse to struct
      arr = []
      while bytes.peek != BC_END
        arr << parse_bytes(bytes, nil, refs, cdefs)
      end

      val = klass.new(*arr)
      refs << val
    else
      klass = klass ? klass.first : nil
      val = []
      refs << val # store a value reference first
      while bytes.peek != BC_END
        val << parse_bytes(bytes, klass, refs, cdefs)
      end
    end

    bytes.next
    val
  when 0x56 # fixed-length list/vector ('V')
    parse_type(bytes)
    if klass && !klass.is_a?(Array) # parse to struct
      arr = []
      parse_int(bytes).times do
        arr << parse_bytes(bytes, nil, refs, cdefs)
      end

      val = klass.new(*arr)
      refs << val
    else
      klass = klass ? klass.first : nil
      val = []
      refs << val # store a value reference
      parse_int(bytes).times do
        val << parse_bytes(bytes, klass, refs, cdefs)
      end
    end

    val
  when 0x57 # variable-length untyped list/vector ('W')
    if klass && !klass.is_a?(Array) # parse to struct
      arr = []
      while bytes.peek != BC_END
        arr << parse_bytes(bytes, nil, refs, cdefs)
      end

      val = klass.new(*arr)
      refs << val
    else
      klass = klass ? klass.first : nil
      val = []
      refs << val # store a value reference first
      while bytes.peek != BC_END
        val << parse_bytes(bytes, klass, refs, cdefs)
      end
    end

    bytes.next
    val
  when 0x58 # fixed-length untyped list/vector ('X')
    if klass && !klass.is_a?(Array) # parse to struct
      arr = []
      parse_int(bytes).times do
        arr << parse_bytes(bytes, nil, refs, cdefs)
      end

      val = klass.new(*arr)
      refs << val
    else
      klass = klass ? klass.first : nil
      val = []
      refs << val # store a value reference first
      parse_int(bytes).times do
        val << parse_bytes(bytes, klass, refs, cdefs)
      end
    end

    val
  when 0x59 # long encoded as 32-bit int ('Y')
    read_int(bytes)
  when 0x5b # double 0.0
    0
  when 0x5c # double 1.0
    1
  when 0x5d # double represented as byte (-128.0 to 127.0)
    read_double_direct(bytes)
  when 0x5e # double represented as short (-32768.0 to 32767.0)
    read_double_short(bytes)
  when 0x5f # double represented as float
    read_double_mill(bytes)
  when 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
       0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f
    # 0x60 - 0x6f object with direct type
    cdef = cdefs[bc - BC_OBJECT_DIRECT]
    val = cdef.new
    refs << val # store a value reference first
    val.members.each do |sym|
      val[sym] = parse_bytes(bytes, klass, refs, cdefs)
    end

    val
  when 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77
    # 0x70 - 0x77 fixed list with direct length
    parse_type(bytes)
    if klass && !klass.is_a?(Array) # parse to struct
      arr = []
      (bc - BC_LIST_DIRECT).times do
        arr << parse_bytes(bytes, nil, refs, cdefs)
      end

      val = klass.new(*arr)
      refs << val
    else
      klass = klass ? klass.first : nil
      val = []
      refs << val # store a value reference first
      (bc - BC_LIST_DIRECT).times do
        val << parse_bytes(bytes, klass, refs, cdefs)
      end
    end

    val
  when 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
    # 0x78 - 0x7f fixed untyped list with direct length
    if klass && !klass.is_a?(Array) # parse to struct
      arr = []
      (bc - BC_LIST_DIRECT_UNTYPED).times do
        arr << parse_bytes(bytes, nil, refs, cdefs)
      end

      val = klass.new(*arr)
      refs << val
    else
      klass = klass ? klass.first : nil
      val = []
      refs << val # store a value reference first
      (bc - BC_LIST_DIRECT_UNTYPED).times do
        val << parse_bytes(bytes, klass, refs, cdefs)
      end
    end

    val
  when 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
       0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
       0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
       0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
       0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
       0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
       0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
       0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf
    # 0x80 - 0xbf one-octet compact int (-x10 to x2f, x90 is 0)
    read_int_zero(bc)
  when 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
       0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
    # 0xc0 - 0xcf two-octet compact int (-x800 to x7ff)
    read_int_byte_zero(bytes, bc)
  when 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7
    # 0xd0 - 0xd7 three-octet compact int (-x40000 to x3ffff)
    read_int_short_zero(bytes, bc)
  when 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
       0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
       0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef
    # 0xd8 - 0xef one-octet compact long (-x8 to xf, xe0 is 0)
    read_long_zero(bc)
  when 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
       0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
    # 0xf0 - 0xff two-octet compact long (-x800 to x7ff, xf8 is 0)
    read_long_byte_zero(bytes, bc)
  else
    raise sprintf("Invalid type: %#x", bc)
  end
end

#parse_int(bytes) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/hessian2/parser.rb', line 330

def parse_int(bytes)
  bc = bytes.next
  case bc
  when 0x49
    read_int(bytes)
  when 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
       0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
       0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
       0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
       0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
       0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
       0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
       0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf
    read_int_zero(bc)
  when 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
       0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
    read_int_byte_zero(bytes, bc)
  when 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7
    read_int_short_zero(bytes, bc)
  else
    raise sprintf("%#x is not a int", bc)
  end
end

#parse_rpc(data) ⇒ Object



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
# File 'lib/hessian2/parser.rb', line 8

def parse_rpc(data)
  bytes = data.each_byte
  bc = bytes.next
  if bc == 0x48 # skip hessian version
    2.times{ bytes.next }
    bc = bytes.next
  end

  case bc
  when 0x43 # rpc call ('C')
    method = parse_string(bytes)
    refs, cdefs = [], []
    args = [].tap do |arr|
      parse_int(bytes).times{ arr << parse_bytes(bytes, nil, refs, cdefs) }
    end
    [ method, *args ]
  when 0x46 # fault ('F')
    fault = parse_bytes(bytes)
    code, message = fault['code'], fault['message']
    raise Fault.new, code == 'RuntimeError' ? message : "#{code} - #{message}"
  when 0x52 # rpc result ('R')
    parse_bytes(bytes)
  else
    raise data
  end
end

#parse_string(bytes) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/hessian2/parser.rb', line 354

def parse_string(bytes)
  bc = bytes.next
  case bc
  when 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
       0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 
       0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
       0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
    read_string_direct(bytes, bc)
  when 0x30, 0x31, 0x32, 0x33
    read_string_short(bytes, bc)
  when 0x52
    read_string_chunk(bytes)
  when 0x53
    read_string(bytes)
  else
    raise sprintf("%#x is not a string", bc)
  end
end

#parse_type(bytes) ⇒ Object



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
# File 'lib/hessian2/parser.rb', line 373

def parse_type(bytes)
  bc = bytes.next
  case bc
  when 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
       0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 
       0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
       0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
    read_string_direct(bytes, bc)
  when 0x30, 0x31, 0x32, 0x33
    read_string_short(bytes, bc)
  when 0x49
    read_int(bytes)
  when 0x52
    read_string_chunk(bytes)
  when 0x53
    read_string(bytes)
  when 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
       0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
       0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
       0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
       0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
       0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
       0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
       0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf
    read_int_zero(bc)
  when 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
       0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
    read_int_byte_zero(bytes, bc)
  when 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7
    read_int_short_zero(bytes, bc)
  else
    raise sprintf("%#x is not a type", bc)
  end
end

#parse_utf8_char(bytes) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/hessian2/parser.rb', line 300

def parse_utf8_char(bytes)
  bc = bytes.next
  if bc < 0x80 # 0xxxxxxx
    bc
  elsif bc & 0xe0 == 0xc0 # 110xxxxx 10xxxxxx
    ((bc & 0x1f) << 6) + (bytes.next & 0x3f)
  elsif bc & 0xf0 == 0xe0 # 1110xxxx 10xxxxxx 10xxxxxx
    ((bc & 0x0f) << 12) + ((bytes.next & 0x3f) << 6) + (bytes.next & 0x3f)
  else
    raise sprintf("bad utf-8 encoding at %#x", bc)
  end
end