| 1 |
#ifndef PROTON_CONSUMERS_H |
| 2 |
#define PROTON_CONSUMERS_H |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
/* Definitions of AMQP type codes */ |
| 26 |
#include "encodings.h" |
| 27 |
|
| 28 |
#include "proton/codec.h" |
| 29 |
#include "proton/types.h" |
| 30 |
|
| 31 |
#include <stdbool.h> |
| 32 |
#include <stddef.h> |
| 33 |
#include <stdint.h> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
typedef struct pni_consumer_t { |
| 37 |
const uint8_t* output_start; |
| 38 |
size_t size; |
| 39 |
size_t position; |
| 40 |
} pni_consumer_t; |
| 41 |
|
| 42 |
static inline pni_consumer_t make_consumer_from_bytes(pn_bytes_t output_bytes) { |
| 43 |
return (pni_consumer_t){ |
| 44 |
.output_start = (const uint8_t*) output_bytes.start, |
| 45 |
.size = output_bytes.size, |
| 46 |
.position = 0 |
| 47 |
}; |
| 48 |
} |
| 49 |
|
| 50 |
static inline bool pni_consumer_readf8(pni_consumer_t *consumer, uint8_t* result) |
| 51 |
{ |
| 52 |
if (consumer->position+1 > consumer->size) { |
| 53 |
consumer->position = consumer->size; |
| 54 |
return false; |
| 55 |
} |
| 56 |
uint8_t r = consumer->output_start[consumer->position+0]; |
| 57 |
consumer->position++; |
| 58 |
*result = r; |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
static inline bool pni_consumer_readf16(pni_consumer_t *consumer, uint16_t* result) |
| 63 |
{ |
| 64 |
if (consumer->position+2 > consumer->size) { |
| 65 |
consumer->position = consumer->size; |
| 66 |
return false; |
| 67 |
} |
| 68 |
uint16_t a = consumer->output_start[consumer->position+0]; |
| 69 |
uint16_t b = consumer->output_start[consumer->position+1]; |
| 70 |
uint16_t r = a << 8 |
| 71 |
| b; |
| 72 |
consumer->position += 2; |
| 73 |
*result = r; |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
static inline bool pni_consumer_readf32(pni_consumer_t *consumer, uint32_t* result) |
| 78 |
{ |
| 79 |
if (consumer->position+4 > consumer->size) { |
| 80 |
consumer->position = consumer->size; |
| 81 |
return false; |
| 82 |
} |
| 83 |
uint32_t a = consumer->output_start[consumer->position+0]; |
| 84 |
uint32_t b = consumer->output_start[consumer->position+1]; |
| 85 |
uint32_t c = consumer->output_start[consumer->position+2]; |
| 86 |
uint32_t d = consumer->output_start[consumer->position+3]; |
| 87 |
uint32_t r = a << 24 |
| 88 |
| b << 16 |
| 89 |
| c << 8 |
| 90 |
| d; |
| 91 |
consumer->position += 4; |
| 92 |
*result = r; |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
static inline bool pni_consumer_readf64(pni_consumer_t *consumer, uint64_t* result) |
| 97 |
{ |
| 98 |
uint32_t a; |
| 99 |
if (!pni_consumer_readf32(consumer, &a)) return false; |
| 100 |
uint32_t b; |
| 101 |
if (!pni_consumer_readf32(consumer, &b)) return false; |
| 102 |
*result = (uint64_t)a << 32 | (uint64_t)b; |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
static inline bool pni_consumer_readf128(pni_consumer_t *consumer, void *dst) |
| 107 |
{ |
| 108 |
if (consumer->position+16 > consumer->size) { |
| 109 |
consumer->position = consumer->size; |
| 110 |
return false; |
| 111 |
} |
| 112 |
memcpy(dst, &consumer->output_start[consumer->position], 16); |
| 113 |
consumer->position += 16; |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
static inline bool pni_consumer_readv8(pni_consumer_t *consumer, pn_bytes_t* bytes){ |
| 118 |
uint8_t size; |
| 119 |
if (!pni_consumer_readf8(consumer, &size)) return false; |
| 120 |
if (consumer->position+size > consumer->size) { |
| 121 |
consumer->position = consumer->size; |
| 122 |
return false; |
| 123 |
} |
| 124 |
*bytes = (pn_bytes_t){.size=size,.start=(const char *)consumer->output_start+consumer->position}; |
| 125 |
consumer->position += size; |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
static inline bool pni_consumer_readv32(pni_consumer_t *consumer, pn_bytes_t* bytes){ |
| 130 |
uint32_t size; |
| 131 |
if (!pni_consumer_readf32(consumer, &size)) return false; |
| 132 |
if (consumer->position+size > consumer->size) { |
| 133 |
consumer->position = consumer->size; |
| 134 |
return false; |
| 135 |
} |
| 136 |
*bytes = (pn_bytes_t){.size=size,.start=(const char *)consumer->output_start+consumer->position}; |
| 137 |
consumer->position += size; |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
static inline bool pni_consumer_read_value_not_described(pni_consumer_t* consumer, uint8_t type, pn_bytes_t *value) { |
| 142 |
uint8_t subcategory = type >> 4; |
| 143 |
switch (subcategory) { |
| 144 |
// Fixed width types: |
| 145 |
// No data |
| 146 |
case 0x4: |
| 147 |
*value = (pn_bytes_t){.size=0,.start=NULL}; |
| 148 |
return true; |
| 149 |
// 1 Octet |
| 150 |
case 0x5: |
| 151 |
if (consumer->position+1 > consumer->size) break; |
| 152 |
*value = (pn_bytes_t){.size=1,.start=(const char *)consumer->output_start+consumer->position}; |
| 153 |
consumer->position += 1; |
| 154 |
return true; |
| 155 |
// 2 Octets |
| 156 |
case 0x6: |
| 157 |
if (consumer->position+2 > consumer->size) break; |
| 158 |
*value = (pn_bytes_t){.size=2,.start=(const char *)consumer->output_start+consumer->position}; |
| 159 |
consumer->position += 2; |
| 160 |
return true; |
| 161 |
// 4 Octets |
| 162 |
case 0x7: |
| 163 |
if (consumer->position+4 > consumer->size) break; |
| 164 |
*value = (pn_bytes_t){.size=4,.start=(const char *)consumer->output_start+consumer->position}; |
| 165 |
consumer->position += 4; |
| 166 |
return true; |
| 167 |
// 8 Octets |
| 168 |
case 0x8: |
| 169 |
if (consumer->position+8 > consumer->size) break; |
| 170 |
*value = (pn_bytes_t){.size=8,.start=(const char *)consumer->output_start+consumer->position}; |
| 171 |
consumer->position += 8; |
| 172 |
return true; |
| 173 |
// 16 Octets |
| 174 |
case 0x9: |
| 175 |
if (consumer->position+16 > consumer->size) break; |
| 176 |
*value = (pn_bytes_t){.size=16,.start=(const char *)consumer->output_start+consumer->position}; |
| 177 |
consumer->position += 16; |
| 178 |
return true; |
| 179 |
// Variable width types: |
| 180 |
// One Octet of size |
| 181 |
case 0xA: |
| 182 |
case 0xC: |
| 183 |
case 0xE: { |
| 184 |
uint8_t size; |
| 185 |
if (!pni_consumer_readf8(consumer, &size)) return false; |
| 186 |
if (consumer->position+size > consumer->size) break; |
| 187 |
*value = (pn_bytes_t){.size=size,.start=(const char *)consumer->output_start+consumer->position}; |
| 188 |
consumer->position += size; |
| 189 |
return true; |
| 190 |
} |
| 191 |
// 4 Octets of size |
| 192 |
case 0xB: |
| 193 |
case 0xD: |
| 194 |
case 0xF: { |
| 195 |
uint32_t size; |
| 196 |
if (!pni_consumer_readf32(consumer, &size)) return false; |
| 197 |
if (consumer->position+size > consumer->size) break; |
| 198 |
*value = (pn_bytes_t){.size=size,.start=(const char *)consumer->output_start+consumer->position}; |
| 199 |
consumer->position += size; |
| 200 |
return true; |
| 201 |
} |
| 202 |
default: |
| 203 |
break; |
| 204 |
} |
| 205 |
consumer->position = consumer->size; |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
static inline bool pni_consumer_skip_value_not_described(pni_consumer_t* consumer, uint8_t type) { |
| 210 |
pn_bytes_t value; |
| 211 |
return pni_consumer_read_value_not_described(consumer, type, &value); |
| 212 |
} |
| 213 |
|
| 214 |
static inline bool pni_consumer_skip_value(pni_consumer_t* consumer, uint8_t type) { |
| 215 |
// Check for described type |
| 216 |
if (type==PNE_DESCRIPTOR) { |
| 217 |
// Skip descriptor |
| 218 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 219 |
if (!pni_consumer_skip_value_not_described(consumer, type)) return false; |
| 220 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 221 |
return pni_consumer_skip_value_not_described(consumer, type); |
| 222 |
} |
| 223 |
return pni_consumer_skip_value_not_described(consumer, type); |
| 224 |
} |
| 225 |
|
| 226 |
/////////////////////////////////////////////////////////////////////////////// |
| 227 |
|
| 228 |
static inline bool consume_single_value_not_described(pni_consumer_t* consumer, uint8_t* type) { |
| 229 |
uint8_t t; |
| 230 |
if (!pni_consumer_readf8(consumer, &t)) return false; |
| 231 |
if (!pni_consumer_skip_value_not_described(consumer, t)) return false; |
| 232 |
if (t==0) return false; |
| 233 |
*type = t; |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
static inline bool consume_single_value(pni_consumer_t* consumer, uint8_t* type) { |
| 238 |
uint8_t t; |
| 239 |
if (!pni_consumer_readf8(consumer, &t)) return false; |
| 240 |
*type = t; |
| 241 |
if (t==PNE_DESCRIPTOR) { |
| 242 |
uint8_t dummy; |
| 243 |
// Descriptor |
| 244 |
bool dq = consume_single_value_not_described(consumer, &dummy); |
| 245 |
// Value |
| 246 |
bool vq = consume_single_value_not_described(consumer, &dummy); |
| 247 |
return dq && vq; |
| 248 |
} else { |
| 249 |
return pni_consumer_skip_value_not_described(consumer, t); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
static inline bool consume_raw(pni_consumer_t* consumer, pn_bytes_t* raw) { |
| 254 |
size_t start = consumer->position; |
| 255 |
uint8_t dummy; |
| 256 |
bool succeed = consume_single_value(consumer, &dummy); |
| 257 |
if (succeed) { |
| 258 |
*raw = (pn_bytes_t){.size=consumer->position-start, .start=(const char*)consumer->output_start+start}; |
| 259 |
} else { |
| 260 |
*raw = (pn_bytes_t) {0, NULL}; |
| 261 |
} |
| 262 |
return succeed; |
| 263 |
} |
| 264 |
|
| 265 |
static inline bool consume_anything(pni_consumer_t* consumer) { |
| 266 |
uint8_t dummy; |
| 267 |
return consume_single_value(consumer, &dummy); |
| 268 |
} |
| 269 |
|
| 270 |
static inline bool consume_ulong(pni_consumer_t* consumer, uint64_t *ulong) { |
| 271 |
*ulong = 0; |
| 272 |
uint8_t type; |
| 273 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 274 |
switch (type) { |
| 275 |
case PNE_SMALLULONG: { |
| 276 |
uint8_t ul; |
| 277 |
if (!pni_consumer_readf8(consumer, &ul)) return false; |
| 278 |
*ulong = ul; |
| 279 |
return true; |
| 280 |
} |
| 281 |
case PNE_ULONG: { |
| 282 |
uint64_t ul; |
| 283 |
if (!pni_consumer_readf64(consumer, &ul)) return false; |
| 284 |
*ulong = ul; |
| 285 |
return true; |
| 286 |
} |
| 287 |
case PNE_ULONG0: { |
| 288 |
*ulong = 0; |
| 289 |
return true; |
| 290 |
} |
| 291 |
default: |
| 292 |
pni_consumer_skip_value(consumer, type); |
| 293 |
return false; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
static inline bool consume_uint(pni_consumer_t* consumer, uint32_t *uint) { |
| 298 |
*uint = 0; |
| 299 |
uint8_t type; |
| 300 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 301 |
switch (type) { |
| 302 |
case PNE_SMALLUINT: { |
| 303 |
uint8_t ui; |
| 304 |
if (!pni_consumer_readf8(consumer, &ui)) return false; |
| 305 |
*uint = ui; |
| 306 |
return true; |
| 307 |
} |
| 308 |
case PNE_UINT: { |
| 309 |
uint32_t ui; |
| 310 |
if (!pni_consumer_readf32(consumer, &ui)) return false; |
| 311 |
*uint = ui; |
| 312 |
return true; |
| 313 |
} |
| 314 |
case PNE_UINT0: { |
| 315 |
*uint = 0; |
| 316 |
return true; |
| 317 |
} |
| 318 |
default: |
| 319 |
pni_consumer_skip_value(consumer, type); |
| 320 |
return false; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
static inline bool consume_ushort(pni_consumer_t* consumer, uint16_t *ushort) { |
| 325 |
*ushort = 0; |
| 326 |
uint8_t type; |
| 327 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 328 |
switch (type) { |
| 329 |
case PNE_USHORT: { |
| 330 |
uint16_t us; |
| 331 |
if (!pni_consumer_readf16(consumer, &us)) return false; |
| 332 |
*ushort = us; |
| 333 |
return true; |
| 334 |
} |
| 335 |
default: |
| 336 |
pni_consumer_skip_value(consumer, type); |
| 337 |
return false; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
static inline bool consume_ubyte(pni_consumer_t* consumer, uint8_t *ubyte) { |
| 342 |
*ubyte = 0; |
| 343 |
uint8_t type; |
| 344 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 345 |
switch (type) { |
| 346 |
case PNE_UBYTE: { |
| 347 |
uint8_t ub; |
| 348 |
if (!pni_consumer_readf8(consumer, &ub)) return false; |
| 349 |
*ubyte = ub; |
| 350 |
return true; |
| 351 |
} |
| 352 |
default: |
| 353 |
pni_consumer_skip_value(consumer, type); |
| 354 |
return false; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
static inline bool consume_bool(pni_consumer_t* consumer, bool *b) { |
| 359 |
*b = false; |
| 360 |
uint8_t type; |
| 361 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 362 |
switch (type) { |
| 363 |
case PNE_BOOLEAN: { |
| 364 |
uint8_t ub; |
| 365 |
if (!pni_consumer_readf8(consumer, &ub)) return false; |
| 366 |
*b = ub; |
| 367 |
return true; |
| 368 |
} |
| 369 |
case PNE_FALSE: |
| 370 |
*b = false; |
| 371 |
return true; |
| 372 |
case PNE_TRUE: |
| 373 |
*b = true; |
| 374 |
return true; |
| 375 |
default: |
| 376 |
pni_consumer_skip_value(consumer, type); |
| 377 |
return false; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
static inline bool consume_timestamp(pni_consumer_t* consumer, pn_timestamp_t *timestamp) { |
| 382 |
*timestamp = 0; |
| 383 |
uint8_t type; |
| 384 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 385 |
switch (type) { |
| 386 |
case PNE_MS64: { |
| 387 |
return pni_consumer_readf64(consumer, (uint64_t*)timestamp); |
| 388 |
} |
| 389 |
default: |
| 390 |
return false; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
static inline bool consume_atom(pni_consumer_t* consumer, pn_atom_t *atom) { |
| 395 |
uint8_t type; |
| 396 |
if (pni_consumer_readf8(consumer, &type)) { |
| 397 |
switch (type) { |
| 398 |
case PNE_SMALLULONG: { |
| 399 |
uint8_t ul; |
| 400 |
if (!pni_consumer_readf8(consumer, &ul)) break; |
| 401 |
atom->type = PN_ULONG; |
| 402 |
atom->u.as_ulong = ul; |
| 403 |
return true; |
| 404 |
} |
| 405 |
case PNE_ULONG: { |
| 406 |
uint64_t ul; |
| 407 |
if (!pni_consumer_readf64(consumer, &ul)) break; |
| 408 |
atom->type = PN_ULONG; |
| 409 |
atom->u.as_ulong = ul; |
| 410 |
return true; |
| 411 |
} |
| 412 |
case PNE_ULONG0: { |
| 413 |
atom->type = PN_ULONG; |
| 414 |
atom->u.as_ulong = 0; |
| 415 |
return true; |
| 416 |
} |
| 417 |
case PNE_SMALLUINT: { |
| 418 |
uint8_t ui; |
| 419 |
if (!pni_consumer_readf8(consumer, &ui)) break; |
| 420 |
atom->type = PN_UINT; |
| 421 |
atom->u.as_uint = ui; |
| 422 |
return true; |
| 423 |
} |
| 424 |
case PNE_UINT: { |
| 425 |
uint32_t ui; |
| 426 |
if (!pni_consumer_readf32(consumer, &ui)) break; |
| 427 |
atom->type = PN_UINT; |
| 428 |
atom->u.as_uint = ui; |
| 429 |
return true; |
| 430 |
} |
| 431 |
case PNE_UINT0: { |
| 432 |
atom->type = PN_UINT; |
| 433 |
atom->u.as_uint = 0; |
| 434 |
return true; |
| 435 |
} |
| 436 |
case PNE_USHORT: { |
| 437 |
uint16_t us; |
| 438 |
if (!pni_consumer_readf16(consumer, &us)) break; |
| 439 |
atom->type = PN_USHORT; |
| 440 |
atom->u.as_ushort = us; |
| 441 |
return true; |
| 442 |
} |
| 443 |
case PNE_UBYTE: { |
| 444 |
uint8_t ub; |
| 445 |
if (!pni_consumer_readf8(consumer, &ub)) break; |
| 446 |
atom->type = PN_UBYTE; |
| 447 |
atom->u.as_ubyte = ub; |
| 448 |
return true; |
| 449 |
} |
| 450 |
case PNE_BOOLEAN: { |
| 451 |
uint8_t ub; |
| 452 |
if (!pni_consumer_readf8(consumer, &ub)) break; |
| 453 |
atom->type = PN_BOOL; |
| 454 |
atom->u.as_bool = ub; |
| 455 |
return true; |
| 456 |
} |
| 457 |
case PNE_FALSE: |
| 458 |
atom->type = PN_BOOL; |
| 459 |
atom->u.as_bool = false; |
| 460 |
return true; |
| 461 |
case PNE_TRUE: |
| 462 |
atom->type = PN_BOOL; |
| 463 |
atom->u.as_bool = true; |
| 464 |
return true; |
| 465 |
case PNE_MS64: { |
| 466 |
uint64_t timestamp; |
| 467 |
if (!pni_consumer_readf64(consumer, ×tamp)) break; |
| 468 |
atom->type = PN_TIMESTAMP; |
| 469 |
atom->u.as_timestamp = timestamp; |
| 470 |
return true; |
| 471 |
} |
| 472 |
case PNE_VBIN32:{ |
| 473 |
pn_bytes_t binary; |
| 474 |
if (!pni_consumer_readv32(consumer, &binary)) break; |
| 475 |
atom->type = PN_BINARY; |
| 476 |
atom->u.as_bytes = binary; |
| 477 |
return true; |
| 478 |
} |
| 479 |
case PNE_VBIN8:{ |
| 480 |
pn_bytes_t binary; |
| 481 |
if (!pni_consumer_readv8(consumer, &binary)) break; |
| 482 |
atom->type = PN_BINARY; |
| 483 |
atom->u.as_bytes = binary; |
| 484 |
return true; |
| 485 |
} |
| 486 |
case PNE_STR32_UTF8: { |
| 487 |
pn_bytes_t string; |
| 488 |
if (!pni_consumer_readv32(consumer, &string)) break; |
| 489 |
atom->type = PN_STRING; |
| 490 |
atom->u.as_bytes = string; |
| 491 |
return true; |
| 492 |
} |
| 493 |
case PNE_STR8_UTF8: { |
| 494 |
pn_bytes_t string; |
| 495 |
if (!pni_consumer_readv8(consumer, &string)) break; |
| 496 |
atom->type = PN_STRING; |
| 497 |
atom->u.as_bytes = string; |
| 498 |
return true; |
| 499 |
} |
| 500 |
case PNE_SYM32:{ |
| 501 |
pn_bytes_t symbol; |
| 502 |
if (!pni_consumer_readv32(consumer, &symbol)) break; |
| 503 |
atom->type = PN_SYMBOL; |
| 504 |
atom->u.as_bytes = symbol; |
| 505 |
return true; |
| 506 |
} |
| 507 |
case PNE_SYM8:{ |
| 508 |
pn_bytes_t symbol; |
| 509 |
if (!pni_consumer_readv8(consumer, &symbol)) break; |
| 510 |
atom->type = PN_SYMBOL; |
| 511 |
atom->u.as_bytes = symbol; |
| 512 |
return true; |
| 513 |
} |
| 514 |
case PNE_UUID: { |
| 515 |
pn_uuid_t uuid; |
| 516 |
if (!pni_consumer_readf128(consumer, &uuid)) break; |
| 517 |
atom->type = PN_UUID; |
| 518 |
atom->u.as_uuid = uuid; |
| 519 |
return true; |
| 520 |
} |
| 521 |
case PNE_NULL: |
| 522 |
atom->type = PN_NULL; |
| 523 |
return true; |
| 524 |
default: |
| 525 |
pni_consumer_skip_value(consumer, type); |
| 526 |
break; |
| 527 |
} |
| 528 |
} |
| 529 |
atom->type = PN_NULL; |
| 530 |
return false; |
| 531 |
} |
| 532 |
|
| 533 |
// XXX: assuming numeric - |
| 534 |
// if we get a symbol we should map it to the numeric value and dispatch on that |
| 535 |
static inline bool consume_described_ulong_descriptor(pni_consumer_t* consumer, pni_consumer_t *subconsumer, uint64_t *descriptor) { |
| 536 |
*descriptor = 0; |
| 537 |
*subconsumer = (pni_consumer_t){.output_start=NULL, .position=0, .size=0}; |
| 538 |
uint8_t type; |
| 539 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 540 |
switch (type) { |
| 541 |
case PNE_DESCRIPTOR: { |
| 542 |
bool dq = consume_ulong(consumer, descriptor); |
| 543 |
size_t sposition = consumer->position; |
| 544 |
uint8_t dummy; |
| 545 |
bool vq = consume_single_value(consumer, &dummy); |
| 546 |
if (dq && vq) { |
| 547 |
size_t scsize = consumer->position > sposition ? consumer->position-sposition : 0; |
| 548 |
*subconsumer = (pni_consumer_t){.output_start=consumer->output_start+sposition, .position=0, .size=scsize}; |
| 549 |
return true; |
| 550 |
} |
| 551 |
return false; |
| 552 |
} |
| 553 |
default: |
| 554 |
pni_consumer_skip_value_not_described(consumer, type); |
| 555 |
return false; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
static inline bool consume_described(pni_consumer_t* consumer, pni_consumer_t *subconsumer) { |
| 560 |
*subconsumer = (pni_consumer_t){.output_start=NULL, .position=0, .size=0}; |
| 561 |
uint8_t type; |
| 562 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 563 |
switch (type) { |
| 564 |
case PNE_DESCRIPTOR: { |
| 565 |
uint8_t dummy; |
| 566 |
bool dq = consume_single_value_not_described(consumer, &dummy); |
| 567 |
size_t sposition = consumer->position; |
| 568 |
bool vq = consume_single_value(consumer, &dummy); |
| 569 |
if (dq && vq) { |
| 570 |
size_t scsize = consumer->position > sposition ? consumer->position-sposition : 0; |
| 571 |
*subconsumer = (pni_consumer_t){.output_start=consumer->output_start+sposition, .position=0, .size=scsize}; |
| 572 |
return true; |
| 573 |
} |
| 574 |
return false; |
| 575 |
} |
| 576 |
default: |
| 577 |
pni_consumer_skip_value_not_described(consumer, type); |
| 578 |
return false; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
static inline bool consume_list(pni_consumer_t* consumer, pni_consumer_t *subconsumer, uint32_t *count) { |
| 583 |
*subconsumer = (pni_consumer_t){.output_start=NULL, .position=0, .size=0}; |
| 584 |
*count = 0; |
| 585 |
uint8_t type; |
| 586 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 587 |
switch (type) { |
| 588 |
case PNE_LIST32: { |
| 589 |
uint32_t s; |
| 590 |
if (!pni_consumer_readf32(consumer, &s)) return false; |
| 591 |
size_t scsize = s < consumer->size-consumer->position ? s : consumer->size-consumer->position; |
| 592 |
*subconsumer = (pni_consumer_t){.output_start=consumer->output_start+consumer->position, .position=0, .size=scsize}; |
| 593 |
consumer->position += scsize; |
| 594 |
return pni_consumer_readf32(subconsumer, count); |
| 595 |
} |
| 596 |
case PNE_LIST8: { |
| 597 |
uint8_t s; |
| 598 |
if (!pni_consumer_readf8(consumer, &s)) return false; |
| 599 |
size_t scsize = s < consumer->size-consumer->position ? s : consumer->size-consumer->position; |
| 600 |
*subconsumer = (pni_consumer_t){.output_start=consumer->output_start+consumer->position, .position=0, .size=scsize}; |
| 601 |
consumer->position += scsize; |
| 602 |
uint8_t c; |
| 603 |
if (!pni_consumer_readf8(subconsumer, &c)) return false; |
| 604 |
*count = c; |
| 605 |
return true; |
| 606 |
} |
| 607 |
case PNE_LIST0: |
| 608 |
return true; |
| 609 |
default: |
| 610 |
pni_consumer_skip_value(consumer, type); |
| 611 |
return false; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
// TODO: This is currently a placeholder - maybe not actually needed |
| 616 |
static inline bool consume_end_list(pni_consumer_t *consumer) { |
| 617 |
return true; |
| 618 |
} |
| 619 |
|
| 620 |
static inline bool consume_array(pni_consumer_t* consumer, pni_consumer_t *subconsumer, uint32_t *count, uint8_t *element_type) { |
| 621 |
*subconsumer = (pni_consumer_t){.output_start=NULL, .position=0, .size=0}; |
| 622 |
*count = 0; |
| 623 |
*element_type = 0; |
| 624 |
uint8_t type; |
| 625 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 626 |
switch (type) { |
| 627 |
case PNE_ARRAY32: { |
| 628 |
uint32_t s; |
| 629 |
if (!pni_consumer_readf32(consumer, &s)) return false; |
| 630 |
size_t scsize = s < consumer->size-consumer->position ? s : consumer->size-consumer->position; |
| 631 |
*subconsumer = (pni_consumer_t){.output_start=consumer->output_start+consumer->position, .position=0, .size=scsize}; |
| 632 |
consumer->position += scsize; |
| 633 |
if (!pni_consumer_readf32(subconsumer, count)) return false; |
| 634 |
return pni_consumer_readf8(subconsumer, element_type); |
| 635 |
} |
| 636 |
case PNE_ARRAY8: { |
| 637 |
uint8_t s; |
| 638 |
if (!pni_consumer_readf8(consumer, &s)) return false; |
| 639 |
size_t scsize = s < consumer->size-consumer->position ? s : consumer->size-consumer->position; |
| 640 |
*subconsumer = (pni_consumer_t){.output_start=consumer->output_start+consumer->position, .position=0, .size=scsize}; |
| 641 |
consumer->position += scsize; |
| 642 |
uint8_t c; |
| 643 |
if (!pni_consumer_readf8(subconsumer, &c)) return false; |
| 644 |
*count = c; |
| 645 |
return pni_consumer_readf8(subconsumer, element_type); |
| 646 |
} |
| 647 |
default: |
| 648 |
pni_consumer_skip_value(consumer, type); |
| 649 |
return false; |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
static inline bool consume_described_anything(pni_consumer_t* consumer) { |
| 654 |
uint8_t type; |
| 655 |
bool tq = consume_single_value(consumer, &type); |
| 656 |
return tq && type==PNE_DESCRIPTOR; |
| 657 |
} |
| 658 |
|
| 659 |
static inline bool consume_described_type_anything(pni_consumer_t* consumer, uint64_t *type) { |
| 660 |
pni_consumer_t subconsumer; |
| 661 |
return consume_described_ulong_descriptor(consumer, &subconsumer, type); |
| 662 |
} |
| 663 |
|
| 664 |
static inline bool consume_described_maybe_type_anything(pni_consumer_t* consumer, bool *qtype, uint64_t *type) { |
| 665 |
pni_consumer_t subconsumer; |
| 666 |
*qtype = consume_described_ulong_descriptor(consumer, &subconsumer, type); |
| 667 |
return *qtype; |
| 668 |
} |
| 669 |
|
| 670 |
static inline bool consume_copy(pni_consumer_t *consumer, pn_data_t *data) { |
| 671 |
size_t iposition = consumer->position; |
| 672 |
uint8_t type; |
| 673 |
bool tq = consume_single_value(consumer, &type); |
| 674 |
if (!tq || type==PNE_NULL) return false; |
| 675 |
|
| 676 |
pn_bytes_t value = {.size = consumer->position-iposition, .start = (const char*)consumer->output_start+iposition}; |
| 677 |
ssize_t err = pn_data_decode(data, value.start, value.size); |
| 678 |
return err>=0 && err==(ssize_t)value.size; |
| 679 |
} |
| 680 |
|
| 681 |
static inline bool consume_described_maybe_type_raw(pni_consumer_t *consumer, bool *qtype, uint64_t *type, pn_bytes_t *raw) { |
| 682 |
pni_consumer_t subconsumer; |
| 683 |
*qtype = consume_described_ulong_descriptor(consumer, &subconsumer, type); |
| 684 |
return *qtype && consume_raw(&subconsumer, raw); |
| 685 |
} |
| 686 |
|
| 687 |
static inline bool consume_described_maybe_type_maybe_anything(pni_consumer_t *consumer, bool *qtype, uint64_t *type, bool *qanything) { |
| 688 |
pni_consumer_t subconsumer; |
| 689 |
*qtype = consume_described_ulong_descriptor(consumer, &subconsumer, type); |
| 690 |
*qanything = consume_anything(&subconsumer); |
| 691 |
return *qtype && *qanything; |
| 692 |
} |
| 693 |
|
| 694 |
static inline bool consume_described_copy(pni_consumer_t *consumer, pn_data_t *data) { |
| 695 |
pni_consumer_t subconsumer; |
| 696 |
return consume_described(consumer, &subconsumer) && consume_copy(&subconsumer, data); |
| 697 |
} |
| 698 |
|
| 699 |
static inline bool consume_string(pni_consumer_t *consumer, pn_bytes_t *string) { |
| 700 |
uint8_t type; |
| 701 |
*string = (pn_bytes_t){.size=0, .start=0}; |
| 702 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 703 |
switch (type) { |
| 704 |
case PNE_STR32_UTF8: { |
| 705 |
return pni_consumer_readv32(consumer, string); |
| 706 |
} |
| 707 |
case PNE_STR8_UTF8: { |
| 708 |
return pni_consumer_readv8(consumer, string); |
| 709 |
} |
| 710 |
default: |
| 711 |
pni_consumer_skip_value(consumer, type); |
| 712 |
return false; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
static inline bool consume_symbol(pni_consumer_t *consumer, pn_bytes_t *symbol) { |
| 717 |
uint8_t type; |
| 718 |
*symbol = (pn_bytes_t){.size=0, .start=0}; |
| 719 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 720 |
switch (type) { |
| 721 |
case PNE_SYM32:{ |
| 722 |
return pni_consumer_readv32(consumer, symbol); |
| 723 |
} |
| 724 |
case PNE_SYM8:{ |
| 725 |
return pni_consumer_readv8(consumer, symbol); |
| 726 |
} |
| 727 |
default: |
| 728 |
pni_consumer_skip_value(consumer, type); |
| 729 |
return false; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
static inline bool consume_binaryornull(pni_consumer_t *consumer, pn_bytes_t *binary) { |
| 734 |
uint8_t type; |
| 735 |
*binary = (pn_bytes_t){.size=0, .start=0}; |
| 736 |
if (!pni_consumer_readf8(consumer, &type)) return false; |
| 737 |
switch (type) { |
| 738 |
case PNE_NULL:{ |
| 739 |
return true; |
| 740 |
} |
| 741 |
case PNE_VBIN32:{ |
| 742 |
return pni_consumer_readv32(consumer, binary); |
| 743 |
} |
| 744 |
case PNE_VBIN8:{ |
| 745 |
return pni_consumer_readv8(consumer, binary); |
| 746 |
} |
| 747 |
default: |
| 748 |
pni_consumer_skip_value(consumer, type); |
| 749 |
return false; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
#endif // PROTON_CONSUMERS_H |