summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/amqp/Decoder.cpp
blob: 9c577e6c92a81671595a986ac43ef95463dcc763 (plain)
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
 *
 * 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.
 *
 */
#include "qpid/amqp/Decoder.h"
#include "qpid/amqp/CharSequence.h"
#include "qpid/amqp/Constructor.h"
#include "qpid/amqp/Descriptor.h"
#include "qpid/amqp/Reader.h"
#include "qpid/amqp/typecodes.h"
#include "qpid/types/Uuid.h"
#include "qpid/types/Variant.h"
#include "qpid/log/Statement.h"
#include "qpid/Exception.h"

namespace qpid {
namespace amqp {

using namespace qpid::amqp::typecodes;

Decoder::Decoder(const char* d, size_t s) : start(d), size(s), position(0) {}

namespace {
class MapBuilder : public Reader
{
  public:
    void onNull(const Descriptor*)
    {
        qpid::types::Variant v;
        handle(v, NULL_NAME);
    }
    void onBoolean(bool v, const Descriptor*)
    {
        handle(v, BOOLEAN_NAME);
    }
    void onUByte(uint8_t v, const Descriptor*)
    {
        handle(v, UBYTE_NAME);
    }
    void onUShort(uint16_t v, const Descriptor*)
    {
        handle(v, USHORT_NAME);
    }
    void onUInt(uint32_t v, const Descriptor*)
    {
        handle(v, UINT_NAME);
    }
    void onULong(uint64_t v, const Descriptor*)
    {
        handle(v, ULONG_NAME);
    }
    void onByte(int8_t v, const Descriptor*)
    {
        handle(v, BYTE_NAME);
    }
    void onShort(int16_t v, const Descriptor*)
    {
        handle(v, SHORT_NAME);
    }
    void onInt(int32_t v, const Descriptor*)
    {
        handle(v, INT_NAME);
    }
    void onLong(int64_t v, const Descriptor*)
    {
        handle(v, LONG_NAME);
    }
    void onFloat(float v, const Descriptor*)
    {
        handle(v, FLOAT_NAME);
    }
    void onDouble(double v, const Descriptor*)
    {
        handle(v, DOUBLE_NAME);
    }
    void onUuid(const CharSequence& v, const Descriptor*)
    {
        handle(v, UUID_NAME);
    }
    void onTimestamp(int64_t v, const Descriptor*)
    {
        handle(v, TIMESTAMP_NAME);
    }
    void onBinary(const CharSequence& v, const Descriptor*)
    {
        handle(v);
    }
    void onString(const CharSequence& v, const Descriptor*)
    {
        handle(v);
    }
    void onSymbol(const CharSequence& v, const Descriptor*)
    {
        handle(v);
    }
    MapBuilder(qpid::types::Variant::Map& m) : map(m), state(KEY) {}
  private:
    qpid::types::Variant::Map& map;
    enum {KEY, SKIP, VALUE} state;
    std::string key;

    template <typename T> void handle(T value, const std::string& name)
    {
        switch (state) {
          case KEY:
            QPID_LOG(warning, "Ignoring key of type " << name);
            state = SKIP;
            break;
          case VALUE:
            map[key] = value;
          case SKIP:
            state = KEY;
            break;
        }
    }
    void handle(const CharSequence& value)
    {
        switch (state) {
          case KEY:
            key = value.str();
            state = VALUE;
            break;
          case VALUE:
            map[key] = value.str();
          case SKIP:
            state = KEY;
            break;
        }
    }
};
}
void Decoder::readMap(qpid::types::Variant::Map& map)
{
    MapBuilder builder(map);
    read(builder);
}

qpid::types::Variant::Map Decoder::readMap()
{
    qpid::types::Variant::Map map;
    readMap(map);
    return map;
}

void Decoder::read(Reader& reader)
{
    while (available() && reader.proceed()) {
        readOne(reader);
    }
}

void Decoder::readOne(Reader& reader)
{
    const char* temp = start + position;
    Constructor c = readConstructor();
    if (c.isDescribed) reader.onDescriptor(c.descriptor, temp);
    readValue(reader, c.code, c.isDescribed ? &c.descriptor : 0);
}

void Decoder::readValue(Reader& reader, uint8_t code, const Descriptor* descriptor)
{
    switch(code) {
      case NULL_VALUE:
        reader.onNull(descriptor);
        break;
      case BOOLEAN:
        reader.onBoolean(readBoolean(), descriptor);
        break;
      case BOOLEAN_TRUE:
        reader.onBoolean(true, descriptor);
        break;
      case BOOLEAN_FALSE:
        reader.onBoolean(false, descriptor);
        break;
      case UBYTE:
        reader.onUByte(readUByte(), descriptor);
        break;
      case USHORT:
        reader.onUShort(readUShort(), descriptor);
        break;
      case UINT:
        reader.onUInt(readUInt(), descriptor);
        break;
      case UINT_SMALL:
        reader.onUInt(readUByte(), descriptor);
        break;
      case UINT_ZERO:
        reader.onUInt(0, descriptor);
        break;
      case ULONG:
        reader.onULong(readULong(), descriptor);
        break;
      case ULONG_SMALL:
        reader.onULong(readUByte(), descriptor);
        break;
      case ULONG_ZERO:
        reader.onULong(0, descriptor);
        break;
      case BYTE:
        reader.onByte(readByte(), descriptor);
        break;
      case SHORT:
        reader.onShort(readShort(), descriptor);
        break;
      case INT:
        reader.onInt(readInt(), descriptor);
        break;
      case INT_SMALL:
        reader.onInt(readByte(), descriptor);
        break;
      case LONG:
        reader.onLong(readLong(), descriptor);
        break;
      case LONG_SMALL:
        reader.onLong(readByte(), descriptor);
        break;
      case FLOAT:
        reader.onFloat(readFloat(), descriptor);
        break;
      case DOUBLE:
        reader.onDouble(readDouble(), descriptor);
        break;
      case UUID:
        reader.onUuid(readRawUuid(), descriptor);
        break;
      case TIMESTAMP:
        reader.onTimestamp(readLong(), descriptor);
        break;

      case BINARY8:
        reader.onBinary(readSequence8(), descriptor);
        break;
      case BINARY32:
        reader.onBinary(readSequence32(), descriptor);
        break;
      case STRING8:
        reader.onString(readSequence8(), descriptor);
        break;
      case STRING32:
        reader.onString(readSequence32(), descriptor);
        break;
      case SYMBOL8:
        reader.onSymbol(readSequence8(), descriptor);
        break;
      case SYMBOL32:
        reader.onSymbol(readSequence32(), descriptor);
        break;

      case LIST0:
        reader.onStartList(0, CharSequence::create(0, 0), descriptor);
        reader.onEndList(0, descriptor);
        break;
      case LIST8:
        readList8(reader, descriptor);
        break;
      case LIST32:
        readList32(reader, descriptor);
        break;
      case MAP8:
        readMap8(reader, descriptor);
        break;
      case MAP32:
        readMap32(reader, descriptor);
        break;
      case ARRAY8:
        readArray8(reader, descriptor);
        break;
      case ARRAY32:
        readArray32(reader, descriptor);
        break;
      default:
        break;
    }
}

void Decoder::readList8(Reader& reader, const Descriptor* descriptor)
{
    uint8_t size = readUByte();
    uint8_t count = readUByte();
    readList(reader, size-sizeof(size), count, descriptor);
}

void Decoder::readList32(Reader& reader, const Descriptor* descriptor)
{
    uint32_t size = readUInt();
    uint32_t count = readUInt();
    readList(reader, size-sizeof(size), count, descriptor);
}

void Decoder::readMap8(Reader& reader, const Descriptor* descriptor)
{
    uint8_t size = readUByte();
    uint8_t count = readUByte();
    readMap(reader, size-sizeof(size), count, descriptor);
}

void Decoder::readMap32(Reader& reader, const Descriptor* descriptor)
{
    uint32_t size = readUInt();
    uint32_t count = readUInt();
    readMap(reader, size-sizeof(size), count, descriptor);
}

void Decoder::readArray8(Reader& reader, const Descriptor* descriptor)
{
    uint8_t size = readUByte();
    uint8_t count = readUByte();
    readArray(reader, size-sizeof(size), count, descriptor);
}

void Decoder::readArray32(Reader& reader, const Descriptor* descriptor)
{
    uint32_t size = readUInt();
    uint32_t count = readUInt();
    readArray(reader, size-sizeof(size), count, descriptor);
}

void Decoder::readList(Reader& reader, uint32_t size, uint32_t count, const Descriptor* descriptor)
{
    if (reader.onStartList(count, CharSequence::create(data(), size), descriptor)) {
        for (uint32_t i = 0; i < count; ++i) {
            readOne(reader);
        }
        reader.onEndList(count, descriptor);
    } else {
        //skip
        advance(size);
    }
}
void Decoder::readMap(Reader& reader, uint32_t size, uint32_t count, const Descriptor* descriptor)
{
    if (reader.onStartMap(count, CharSequence::create(data(), size), descriptor)) {
        for (uint32_t i = 0; i < count; ++i) {
            readOne(reader);
        }
        reader.onEndMap(count, descriptor);
    } else {
        //skip
        advance(size);
    }
}

void Decoder::readArray(Reader& reader, uint32_t size, uint32_t count, const Descriptor* descriptor)
{
    size_t temp = position;
    Constructor constructor = readConstructor();
    CharSequence raw = CharSequence::create(data(), size-(position-temp));
    if (reader.onStartArray(count, raw, constructor, descriptor)) {
        for (uint32_t i = 0; i < count; ++i) {
            readValue(reader, constructor.code, constructor.isDescribed ? &constructor.descriptor : 0);
        }
        reader.onEndArray(count, descriptor);
    } else {
        //skip
        advance(raw.size);
    }
}


Constructor Decoder::readConstructor()
{
    Constructor result(readCode());
    if (result.code == DESCRIPTOR) {
        result.isDescribed = true;
        result.descriptor = readDescriptor();
        result.code = readCode();
    } else {
        result.isDescribed = false;
    }
    return result;
}

Descriptor Decoder::readDescriptor()
{
    uint8_t code = readCode();
    switch(code) {
      case SYMBOL8:
        return Descriptor(readSequence8());
      case SYMBOL32:
        return Descriptor(readSequence32());
      case ULONG:
        return Descriptor(readULong());
      case ULONG_SMALL:
        return Descriptor((uint64_t) readUByte());
      case ULONG_ZERO:
        return Descriptor((uint64_t) 0);
      default:
        throw qpid::Exception(QPID_MSG("Expected descriptor of type ulong or symbol; found " << code));
    }
}

void Decoder::advance(size_t n)
{
    if (n > available()) throw qpid::Exception(QPID_MSG("Out of Bounds"));
    position += n;
}

const char* Decoder::data()
{
    return start + position;
}

size_t Decoder::available()
{
    return size - position;
}

uint8_t Decoder::readCode()
{
    return readUByte();
}

bool Decoder::readBoolean()
{
    return readUByte();
}

uint8_t Decoder::readUByte()
{
    return static_cast<uint8_t>(start[position++]);
}

uint16_t Decoder::readUShort()
{
    uint16_t hi = (unsigned char) start[position++];
    hi = hi << 8;
    hi |= (unsigned char) start[position++];
    return hi;
}

uint32_t Decoder::readUInt()
{
    uint32_t a = (unsigned char) start[position++];
    uint32_t b = (unsigned char) start[position++];
    uint32_t c = (unsigned char) start[position++];
    uint32_t d = (unsigned char) start[position++];
    a = a << 24;
    a |= b << 16;
    a |= c << 8;
    a |= d;
    return a;
}

uint64_t Decoder::readULong()
{
    uint64_t hi =readUInt();
    uint64_t lo = readUInt();
    hi = hi << 32;
    return hi | lo;
}

int8_t Decoder::readByte()
{
    return (int8_t) readUByte();
}

int16_t Decoder::readShort()
{
    return (int16_t) readUShort();
}

int32_t Decoder::readInt()
{
    return (int32_t) readUInt();
}

int64_t Decoder::readLong()
{
    return (int64_t) readULong();
}

float Decoder::readFloat()
{
    union {
        uint32_t i;
        float    f;
    } val;
    val.i = readUInt();
    return val.f;
}

double Decoder::readDouble()
{
    union {
        uint64_t i;
        double   f;
    } val;
    val.i = readULong();
    return val.f;
}

CharSequence Decoder::readSequence8()
{
    CharSequence s;
    s.size = readUByte();
    s.data = start + position;
    advance(s.size);
    return s;
}

CharSequence Decoder::readSequence32()
{
    CharSequence s;
    s.size = readUInt();
    s.data = start + position;
    advance(s.size);
    return s;
}

qpid::types::Uuid Decoder::readUuid()
{
    qpid::types::Uuid uuid(start + position);
    advance(16);
    return uuid;
}

CharSequence Decoder::readRawUuid()
{
    CharSequence s;
    s.data = start + position;
    s.size = 16;
    advance(s.size);
    return s;
}

size_t Decoder::getPosition() const { return position; }
size_t Decoder::getSize() const { return size; }
void Decoder::resetSize(size_t s) { size = s; }
}} // namespace qpid::amqp