Debian Codegraph @

c/src/core/codec.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include <proton/object.h>
23 #include <proton/codec.h>
24 #include <proton/error.h>
25 #include <assert.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <ctype.h>
31 #include "encodings.h"
32 #define DEFINE_FIELDS
33 #include "protocol.h"
34 #include "platform/platform_fmt.h"
35 #include "util.h"
36 #include "decoder.h"
37 #include "encoder.h"
38 #include "data.h"
39 #include "logger_private.h"
40 #include "memory.h"
41
42 const char *pn_type_name(pn_type_t type)
43 {
44 switch (type)
45 {
46 case PN_NULL: return "PN_NULL";
47 case PN_BOOL: return "PN_BOOL";
48 case PN_UBYTE: return "PN_UBYTE";
49 case PN_BYTE: return "PN_BYTE";
50 case PN_USHORT: return "PN_USHORT";
51 case PN_SHORT: return "PN_SHORT";
52 case PN_UINT: return "PN_UINT";
53 case PN_INT: return "PN_INT";
54 case PN_CHAR: return "PN_CHAR";
55 case PN_ULONG: return "PN_ULONG";
56 case PN_LONG: return "PN_LONG";
57 case PN_TIMESTAMP: return "PN_TIMESTAMP";
58 case PN_FLOAT: return "PN_FLOAT";
59 case PN_DOUBLE: return "PN_DOUBLE";
60 case PN_DECIMAL32: return "PN_DECIMAL32";
61 case PN_DECIMAL64: return "PN_DECIMAL64";
62 case PN_DECIMAL128: return "PN_DECIMAL128";
63 case PN_UUID: return "PN_UUID";
64 case PN_BINARY: return "PN_BINARY";
65 case PN_STRING: return "PN_STRING";
66 case PN_SYMBOL: return "PN_SYMBOL";
67 case PN_DESCRIBED: return "PN_DESCRIBED";
68 case PN_ARRAY: return "PN_ARRAY";
69 case PN_LIST: return "PN_LIST";
70 case PN_MAP: return "PN_MAP";
71 default: break;
72 }
73
74 return "<UNKNOWN>";
75 }
76
77 static inline void pni_atom_init(pn_atom_t *atom, pn_type_t type)
78 {
79 memset(atom, 0, sizeof(pn_atom_t));
80 atom->type = type;
81 }
82
83 // data
84
85 static void pn_data_finalize(void *object)
86 {
87 pn_data_t *data = (pn_data_t *) object;
88 pni_mem_subdeallocate(pn_class(data), data, data->nodes);
89 pn_buffer_free(data->buf);
90 pn_error_free(data->error);
91 }
92
93 static const pn_fields_t *pni_node_fields(pn_data_t *data, pni_node_t *node)
94 {
95 if (!node) return NULL;
96 if (node->atom.type != PN_DESCRIBED) return NULL;
97
98 pni_node_t *descriptor = pn_data_node(data, node->down);
99
100 if (!descriptor || descriptor->atom.type != PN_ULONG) {
101 return NULL;
102 }
103
104 if (descriptor->atom.u.as_ulong >= FIELD_MIN && descriptor->atom.u.as_ulong <= FIELD_MAX) {
105 const pn_fields_t *f = &FIELDS[descriptor->atom.u.as_ulong-FIELD_MIN];
106 return (f->name_index!=0) ? f : NULL;
107 } else {
108 return NULL;
109 }
110 }
111
112 int pni_inspect_atom(pn_atom_t *atom, pn_string_t *str)
113 {
114 switch (atom->type) {
115 case PN_NULL:
116 return pn_string_addf(str, "null");
117 case PN_BOOL:
118 return pn_string_addf(str, atom->u.as_bool ? "true" : "false");
119 case PN_UBYTE:
120 return pn_string_addf(str, "%" PRIu8, atom->u.as_ubyte);
121 case PN_BYTE:
122 return pn_string_addf(str, "%" PRIi8, atom->u.as_byte);
123 case PN_USHORT:
124 return pn_string_addf(str, "%" PRIu16, atom->u.as_ushort);
125 case PN_SHORT:
126 return pn_string_addf(str, "%" PRIi16, atom->u.as_short);
127 case PN_UINT:
128 return pn_string_addf(str, "%" PRIu32, atom->u.as_uint);
129 case PN_INT:
130 return pn_string_addf(str, "%" PRIi32, atom->u.as_int);
131 case PN_CHAR:
132 return pn_string_addf(str, "%c", atom->u.as_char);
133 case PN_ULONG:
134 return pn_string_addf(str, "%" PRIu64, atom->u.as_ulong);
135 case PN_LONG:
136 return pn_string_addf(str, "%" PRIi64, atom->u.as_long);
137 case PN_TIMESTAMP:
138 return pn_string_addf(str, "%" PRIi64, atom->u.as_timestamp);
139 case PN_FLOAT:
140 return pn_string_addf(str, "%g", atom->u.as_float);
141 case PN_DOUBLE:
142 return pn_string_addf(str, "%g", atom->u.as_double);
143 case PN_DECIMAL32:
144 return pn_string_addf(str, "D32(%" PRIu32 ")", atom->u.as_decimal32);
145 case PN_DECIMAL64:
146 return pn_string_addf(str, "D64(%" PRIu64 ")", atom->u.as_decimal64);
147 case PN_DECIMAL128:
148 return pn_string_addf(str, "D128(%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
149 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
150 "%02hhx%02hhx)",
151 atom->u.as_decimal128.bytes[0],
152 atom->u.as_decimal128.bytes[1],
153 atom->u.as_decimal128.bytes[2],
154 atom->u.as_decimal128.bytes[3],
155 atom->u.as_decimal128.bytes[4],
156 atom->u.as_decimal128.bytes[5],
157 atom->u.as_decimal128.bytes[6],
158 atom->u.as_decimal128.bytes[7],
159 atom->u.as_decimal128.bytes[8],
160 atom->u.as_decimal128.bytes[9],
161 atom->u.as_decimal128.bytes[10],
162 atom->u.as_decimal128.bytes[11],
163 atom->u.as_decimal128.bytes[12],
164 atom->u.as_decimal128.bytes[13],
165 atom->u.as_decimal128.bytes[14],
166 atom->u.as_decimal128.bytes[15]);
167 case PN_UUID:
168 return pn_string_addf(str, "UUID(%02hhx%02hhx%02hhx%02hhx-"
169 "%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-"
170 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx)",
171 atom->u.as_uuid.bytes[0],
172 atom->u.as_uuid.bytes[1],
173 atom->u.as_uuid.bytes[2],
174 atom->u.as_uuid.bytes[3],
175 atom->u.as_uuid.bytes[4],
176 atom->u.as_uuid.bytes[5],
177 atom->u.as_uuid.bytes[6],
178 atom->u.as_uuid.bytes[7],
179 atom->u.as_uuid.bytes[8],
180 atom->u.as_uuid.bytes[9],
181 atom->u.as_uuid.bytes[10],
182 atom->u.as_uuid.bytes[11],
183 atom->u.as_uuid.bytes[12],
184 atom->u.as_uuid.bytes[13],
185 atom->u.as_uuid.bytes[14],
186 atom->u.as_uuid.bytes[15]);
187 case PN_BINARY:
188 case PN_STRING:
189 case PN_SYMBOL:
190 {
191 int err;
192 const char *pfx;
193 pn_bytes_t bin = atom->u.as_bytes;
194 bool quote;
195 switch (atom->type) {
196 case PN_BINARY:
197 pfx = "b";
198 quote = true;
199 break;
200 case PN_STRING:
201 pfx = "";
202 quote = true;
203 break;
204 case PN_SYMBOL:
205 pfx = ":";
206 quote = false;
207 for (unsigned i = 0; i < bin.size; i++) {
208 if (!isalpha(bin.start[i])) {
209 quote = true;
210 break;
211 }
212 }
213 break;
214 default:
215 assert(false);
216 return PN_ERR;
217 }
218
219 if ((err = pn_string_addf(str, "%s", pfx))) return err;
220 if (quote) if ((err = pn_string_addf(str, "\""))) return err;
221 if ((err = pn_quote(str, bin.start, bin.size))) return err;
222 if (quote) if ((err = pn_string_addf(str, "\""))) return err;
223 return 0;
224 }
225 case PN_LIST:
226 return pn_string_addf(str, "<list>");
227 case PN_MAP:
228 return pn_string_addf(str, "<map>");
229 case PN_ARRAY:
230 return pn_string_addf(str, "<array>");
231 case PN_DESCRIBED:
232 return pn_string_addf(str, "<described>");
233 default:
234 return pn_string_addf(str, "<undefined: %i>", atom->type);
235 }
236 }
237
238 /* Return index in current list, array etc.*/
239 static int pni_node_lindex(pn_data_t *data, pni_node_t *node)
240 {
241 int count = 0;
242 while (node) {
243 node = pn_data_node(data, node->prev);
244 count++;
245 }
246 return count - 1;
247 }
248
249 int pni_inspect_enter(void *ctx, pn_data_t *data, pni_node_t *node)
250 {
251 pn_string_t *str = (pn_string_t *) ctx;
252 pn_atom_t *atom = (pn_atom_t *) &node->atom;
253
254 pni_node_t *parent = pn_data_node(data, node->parent);
255 const pn_fields_t *fields = pni_node_fields(data, parent);
256 pni_node_t *grandparent = parent ? pn_data_node(data, parent->parent) : NULL;
257 const pn_fields_t *grandfields = pni_node_fields(data, grandparent);
258
259 int err;
260
261 if (grandfields) {
262 if (atom->type == PN_NULL) {
263 return 0;
264 }
265 pni_nid_t lindex = pni_node_lindex(data, node);
266 const char *name = (lindex < grandfields->field_count)
267 ? (const char*)FIELD_STRINGPOOL.STRING0+FIELD_FIELDS[grandfields->first_field_index+lindex]
268 : NULL;
269 if (name) {
270 err = pn_string_addf(str, "%s=", name);
271 if (err) return err;
272 }
273 }
274
275 switch (atom->type) {
276 case PN_DESCRIBED:
277 return pn_string_addf(str, "@");
278 case PN_ARRAY:
279 // XXX: need to fix for described arrays
280 return pn_string_addf(str, "@%s[", pn_type_name(node->type));
281 case PN_LIST:
282 return pn_string_addf(str, "[");
283 case PN_MAP:
284 return pn_string_addf(str, "{");
285 default:
286 if (fields && node->prev == 0) {
287 err = pn_string_addf(str, "%s", (const char *)FIELD_STRINGPOOL.STRING0+FIELD_NAME[fields->name_index]);
288 if (err) return err;
289 err = pn_string_addf(str, "(");
290 if (err) return err;
291 err = pni_inspect_atom(atom, str);
292 if (err) return err;
293 return pn_string_addf(str, ")");
294 } else {
295 return pni_inspect_atom(atom, str);
296 }
297 }
298 }
299
300 pni_node_t *pni_next_nonnull(pn_data_t *data, pni_node_t *node)
301 {
302 while (node) {
303 node = pn_data_node(data, node->next);
304 if (node && node->atom.type != PN_NULL) {
305 return node;
306 }
307 }
308
309 return NULL;
310 }
311
312 int pni_inspect_exit(void *ctx, pn_data_t *data, pni_node_t *node)
313 {
314 pn_string_t *str = (pn_string_t *) ctx;
315 int err;
316
317 switch (node->atom.type) {
318 case PN_ARRAY:
319 case PN_LIST:
320 err = pn_string_addf(str, "]");
321 if (err) return err;
322 break;
323 case PN_MAP:
324 err = pn_string_addf(str, "}");
325 if (err) return err;
326 break;
327 default:
328 break;
329 }
330
331 pni_node_t *parent = pn_data_node(data, node->parent);
332 pni_node_t *grandparent = parent ? pn_data_node(data, parent->parent) : NULL;
333 const pn_fields_t *grandfields = pni_node_fields(data, grandparent);
334 if (!grandfields || node->atom.type != PN_NULL) {
335 if (node->next) {
336 if (parent && parent->atom.type == PN_MAP && (pni_node_lindex(data, node) % 2) == 0) {
337 err = pn_string_addf(str, "=");
338 if (err) return err;
339 } else if (parent && parent->atom.type == PN_DESCRIBED && node->prev == 0) {
340 err = pn_string_addf(str, " ");
341 if (err) return err;
342 } else {
343 if (!grandfields || pni_next_nonnull(data, node)) {
344 err = pn_string_addf(str, ", ");
345 if (err) return err;
346 }
347 }
348 }
349 }
350
351 return 0;
352 }
353
354 static int pn_data_inspect(void *obj, pn_string_t *dst)
355 {
356 pn_data_t *data = (pn_data_t *) obj;
357
358 return pni_data_traverse(data, pni_inspect_enter, pni_inspect_exit, dst);
359 }
360
361 #define pn_data_initialize NULL
362 #define pn_data_hashcode NULL
363 #define pn_data_compare NULL
364
365 static inline pn_error_t *pni_data_error(pn_data_t *data)
366 {
367 if (data->error == NULL) {
368 data->error = pn_error();
369 }
370 return data->error;
371 }
372
373 pn_data_t *pn_data(size_t capacity)
374 {
375 static const pn_class_t clazz = PN_CLASS(pn_data);
376 pn_data_t *data = (pn_data_t *) pn_class_new(&clazz, sizeof(pn_data_t));
377 data->capacity = capacity;
378 data->size = 0;
379 data->nodes = capacity ? (pni_node_t *) pni_mem_suballocate(&clazz, data, capacity * sizeof(pni_node_t)) : NULL;
380 data->buf = NULL;
381 data->parent = 0;
382 data->current = 0;
383 data->base_parent = 0;
384 data->base_current = 0;
385 data->error = NULL;
386 return data;
387 }
388
389 void pn_data_free(pn_data_t *data)
390 {
391 pn_free(data);
392 }
393
394 int pn_data_errno(pn_data_t *data)
395 {
396 return pn_error_code(pni_data_error(data));
397 }
398
399 pn_error_t *pn_data_error(pn_data_t *data)
400 {
401 return pni_data_error(data);
402 }
403
404 size_t pn_data_size(pn_data_t *data)
405 {
406 return data ? data->size : 0;
407 }
408
409 void pn_data_clear(pn_data_t *data)
410 {
411 if (data) {
412 data->size = 0;
413 data->parent = 0;
414 data->current = 0;
415 data->base_parent = 0;
416 data->base_current = 0;
417 if (data->buf) pn_buffer_clear(data->buf);
418 }
419 }
420
421 static int pni_data_grow(pn_data_t *data)
422 {
423 size_t capacity = data->capacity ? data->capacity : 2;
424 if (capacity >= PNI_NID_MAX) return PN_OUT_OF_MEMORY;
425 else if (capacity < PNI_NID_MAX/2) capacity *= 2;
426 else capacity = PNI_NID_MAX;
427
428 pni_node_t *new_nodes = (pni_node_t *) pni_mem_subreallocate(pn_class(data), data, data->nodes, capacity * sizeof(pni_node_t));
429 if (new_nodes == NULL) return PN_OUT_OF_MEMORY;
430 data->capacity = capacity;
431 data->nodes = new_nodes;
432 return 0;
433 }
434
435 static ssize_t pni_data_intern(pn_data_t *data, const char *start, size_t size)
436 {
437 size_t offset = pn_buffer_size(data->buf);
438 int err = pn_buffer_append(data->buf, start, size);
439 if (err) return err;
440 err = pn_buffer_append(data->buf, "\0", 1);
441 if (err) return err;
442 return offset;
443 }
444
445 static pn_bytes_t *pni_data_bytes(pn_data_t *data, pni_node_t *node)
446 {
447 switch (node->atom.type) {
448 case PN_BINARY:
449 case PN_STRING:
450 case PN_SYMBOL:
451 return &node->atom.u.as_bytes;
452 default: return NULL;
453 }
454 }
455
456 static void pni_data_rebase(pn_data_t *data, char *base)
457 {
458 for (unsigned i = 0; i < data->size; i++) {
459 pni_node_t *node = &data->nodes[i];
460 if (node->data) {
461 pn_bytes_t *bytes = pni_data_bytes(data, node);
462 bytes->start = base + node->data_offset;
463 }
464 }
465 }
466
467 static int pni_data_intern_node(pn_data_t *data, pni_node_t *node)
468 {
469 pn_bytes_t *bytes = pni_data_bytes(data, node);
470 if (!bytes) return 0;
471 if (data->buf == NULL) {
472 // Heuristic to avoid growing small buffers too much
473 // size + 1 to allow for zero termination
474 size_t size = pn_max(bytes->size+1, PNI_INTERN_MINSIZE);
475 data->buf = pn_buffer(size);
476 }
477 size_t oldcap = pn_buffer_capacity(data->buf);
478 ssize_t offset = pni_data_intern(data, bytes->start, bytes->size);
479 if (offset < 0) return offset;
480 node->data = true;
481 node->data_offset = offset;
482 node->data_size = bytes->size;
483 pn_rwbytes_t buf = pn_buffer_memory(data->buf);
484 bytes->start = buf.start + offset;
485
486 if (pn_buffer_capacity(data->buf) != oldcap) {
487 pni_data_rebase(data, buf.start);
488 }
489
490 return 0;
491 }
492
493
494 /*
495 Append src to data after normalizing for "multiple" field encoding.
496
497 AMQP composite field definitions can be declared "multiple", see:
498
499 - http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-types-v1.0-os.html#doc-idp115568
500 - http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-types-v1.0-os.html#section-composite-type-representation
501
502 Multiple fields allow redundant encoding of two cases:
503
504 1. empty: null or an empty array.
505 2. single-value: direct encoding of value, or array with one element
506
507 For encoding compactness and inter-operability, normalize multiple
508 field values to always use null for empty, and direct encoding for
509 single value.
510 */
511 static int pni_normalize_multiple(pn_data_t *data, pn_data_t *src) {
512 int err = 0;
513 pn_handle_t point = pn_data_point(src);
514 pn_data_rewind(src);
515 pn_data_next(src);
516 if (pn_data_type(src) == PN_ARRAY) {
517 switch (pn_data_get_array(src)) {
518 case 0: /* Empty array => null */
519 err = pn_data_put_null(data);
520 break;
521 case 1: /* Single-element array => encode the element */
522 pn_data_enter(src);
523 pn_data_narrow(src);
524 err = pn_data_appendn(data, src, 1);
525 pn_data_widen(src);
526 break;
527 default: /* Multi-element array, encode unchanged */
528 err = pn_data_appendn(data, src, 1);
529 break;
530 }
531 } else {
532 err = pn_data_appendn(data, src, 1); /* Non-array, append the value */
533 }
534 pn_data_restore(src, point);
535 return err;
536 }
537
538
539 /* Format codes:
540 code: AMQP-type (arguments)
541 n: null ()
542 o: bool (int)
543 B: ubyte (unsigned int)
544 b: byte (int)
545 H: ushort (unsigned int)
546 h: short (int)
547 I: uint (uint32_t)
548 i: int (int32_t)
549 L: ulong (ulong32_t)
550 l: long (long32_t)
551 t: timestamp (pn_timestamp_t)
552 f: float (float)
553 d: double (double)
554 Z: binary (size_t, char*) - must not be NULL
555 z: binary (size_t, char*) - encode as AMQP null if NULL
556 S: string (char*)
557 s: symbol (char*)
558 D: described - next two codes are [descriptor, body]
559 @: enter array. If followed by D, a described array. Following codes to matching ']' are elements.
560 T: type (pn_type_t) - set array type while in array
561 [: enter list. Following codes up to matching ']' are elements
562 {: enter map. Following codes up to matching '}' are key, value pairs
563 ]: exit list or array
564 }: exit map
565 ?: TODO document
566 *: TODO document
567 a: single value (pn_atom_t*) - append the pn_atom_t unmodified
568 C: single value (pn_data_t*) - append the pn_data_t unmodified
569 M: multiple value (pn_data_t*) - normalize and append multiple field value,
570 see pni_normalize_multiple()
571 */
572 int pn_data_vfill(pn_data_t *data, const char *fmt, va_list ap)
573 {
574 int err = 0;
575 const char *begin = fmt;
576 while (*fmt) {
577 char code = *(fmt++);
578 if (!code) return 0;
579
580 switch (code) {
581 case 'n':
582 err = pn_data_put_null(data);
583 break;
584 case 'o':
585 err = pn_data_put_bool(data, va_arg(ap, int));
586 break;
587 case 'B':
588 err = pn_data_put_ubyte(data, va_arg(ap, unsigned int));
589 break;
590 case 'b':
591 err = pn_data_put_byte(data, va_arg(ap, int));
592 break;
593 case 'H':
594 err = pn_data_put_ushort(data, va_arg(ap, unsigned int));
595 break;
596 case 'h':
597 err = pn_data_put_short(data, va_arg(ap, int));
598 break;
599 case 'I':
600 err = pn_data_put_uint(data, va_arg(ap, uint32_t));
601 break;
602 case 'i':
603 err = pn_data_put_int(data, va_arg(ap, uint32_t));
604 break;
605 case 'L':
606 err = pn_data_put_ulong(data, va_arg(ap, uint64_t));
607 break;
608 case 'l':
609 err = pn_data_put_long(data, va_arg(ap, int64_t));
610 break;
611 case 't':
612 err = pn_data_put_timestamp(data, va_arg(ap, pn_timestamp_t));
613 break;
614 case 'f':
615 err = pn_data_put_float(data, va_arg(ap, double));
616 break;
617 case 'd':
618 err = pn_data_put_double(data, va_arg(ap, double));
619 break;
620 case 'Z': /* encode binary, must not be NULL */
621 {
622 // For maximum portability, caller must pass these as two separate args, not a single struct
623 size_t size = va_arg(ap, size_t);
624 char *start = va_arg(ap, char *);
625 err = pn_data_put_binary(data, pn_bytes(size, start));
626 }
627 break;
628 case 'z': /* encode binary or null if pointer is NULL */
629 {
630 // For maximum portability, caller must pass these as two separate args, not a single struct
631 size_t size = va_arg(ap, size_t);
632 char *start = va_arg(ap, char *);
633 if (start) {
634 err = pn_data_put_binary(data, pn_bytes(size, start));
635 } else {
636 err = pn_data_put_null(data);
637 }
638 }
639 break;
640 case 'S': /* encode string or null if NULL */
641 case 's': /* encode symbol or null if NULL */
642 {
643 char *start = va_arg(ap, char *);
644 size_t size;
645 if (start) {
646 size = strlen(start);
647 if (code == 'S') {
648 err = pn_data_put_string(data, pn_bytes(size, start));
649 } else {
650 err = pn_data_put_symbol(data, pn_bytes(size, start));
651 }
652 } else {
653 err = pn_data_put_null(data);
654 }
655 }
656 break;
657 case 'D':
658 /* The next 2 args are the descriptor, value for a described value. */
659 err = pn_data_put_described(data);
660 pn_data_enter(data);
661 break;
662 case 'T': /* Set type of open array */
663 {
664 pni_node_t *parent = pn_data_node(data, data->parent);
665 if (parent->atom.type == PN_ARRAY) {
666 parent->type = (pn_type_t) va_arg(ap, int);
667 } else {
668 return pn_error_format(pni_data_error(data), PN_ERR, "naked type");
669 }
670 }
671 break;
672 case '@': /* begin array */
673 {
674 bool described;
675 if (*(fmt + 1) == 'D') {
676 fmt++;
677 described = true;
678 } else {
679 described = false;
680 }
681 err = pn_data_put_array(data, described, (pn_type_t) 0);
682 pn_data_enter(data);
683 }
684 break;
685 case '[': /* begin list */
686 if (fmt < (begin + 2) || *(fmt - 2) != 'T') {
687 err = pn_data_put_list(data);
688 if (err) return err;
689 pn_data_enter(data);
690 }
691 break;
692 case '{': /* begin map */
693 err = pn_data_put_map(data);
694 if (err) return err;
695 pn_data_enter(data);
696 break;
697 case '}':
698 case ']':
699 if (!pn_data_exit(data))
700 return pn_error_format(pni_data_error(data), PN_ERR, "exit failed");
701 break;
702 case '?':
703 if (!va_arg(ap, int)) {
704 err = pn_data_put_null(data);
705 if (err) return err;
706 pn_data_enter(data);
707 }
708 break;
709 case '*':
710 {
711 int count = va_arg(ap, int);
712 void *ptr = va_arg(ap, void *);
713
714 char c = *(fmt++);
715
716 switch (c)
717 {
718 case 's':
719 {
720 char **sptr = (char **) ptr;
721 for (int i = 0; i < count; i++)
722 {
723 char *sym = *(sptr++);
724 err = pn_data_fill(data, "s", sym);
725 if (err) return err;
726 }
727 }
728 break;
729 default:
730 PN_LOG_DEFAULT(PN_SUBSYSTEM_AMQP, PN_LEVEL_CRITICAL, "unrecognized * code: 0x%.2X '%c'", code, code);
731 return PN_ARG_ERR;
732 }
733 }
734 break;
735 case 'C': /* Append an existing pn_data_t * */
736 {
737 pn_data_t *src = va_arg(ap, pn_data_t *);
738 if (src && pn_data_size(src) > 0) {
739 err = pn_data_appendn(data, src, 1);
740 if (err) return err;
741 } else {
742 err = pn_data_put_null(data);
743 if (err) return err;
744 }
745 }
746 break;
747 case 'a': /* Append an existing pn_atom_t * */
748 {
749 pn_atom_t *src = va_arg(ap, pn_atom_t *);
750 if (src) {
751 err = pn_data_put_atom(data, *src);
752 if (err) return err;
753 } else {
754 err = pn_data_put_null(data);
755 if (err) return err;
756 }
757 }
758 break;
759 case 'M':
760 {
761 pn_data_t *src = va_arg(ap, pn_data_t *);
762 err = (src && pn_data_size(src) > 0) ?
763 pni_normalize_multiple(data, src) : pn_data_put_null(data);
764 break;
765 }
766 default:
767 PN_LOG_DEFAULT(PN_SUBSYSTEM_AMQP, PN_LEVEL_CRITICAL, "unrecognized fill code: 0x%.2X '%c'", code, code);
768 return PN_ARG_ERR;
769 }
770
771 if (err) return err;
772
773 pni_node_t *parent = pn_data_node(data, data->parent);
774 pni_node_t *current = pn_data_node(data, data->current);
775 while (parent) {
776 if (parent->atom.type == PN_DESCRIBED && parent->children == 2) {
777 current->described = true;
778 pn_data_exit(data);
779 current = pn_data_node(data, data->current);
780 parent = pn_data_node(data, data->parent);
781 } else if (parent->atom.type == PN_NULL && parent->children == 1) {
782 pn_data_exit(data);
783 current = pn_data_node(data, data->current);
784 current->down = 0;
785 current->children = 0;
786 parent = pn_data_node(data, data->parent);
787 } else {
788 break;
789 }
790 }
791 }
792
793 return 0;
794 }
795
796
797 int pn_data_fill(pn_data_t *data, const char *fmt, ...)
798 {
799 va_list ap;
800 va_start(ap, fmt);
801 int err = pn_data_vfill(data, fmt, ap);
802 va_end(ap);
803 return err;
804 }
805
806 static bool pn_scan_next(pn_data_t *data, pn_type_t *type, bool suspend)
807 {
808 if (suspend) return false;
809 bool found = pn_data_next(data);
810 if (found) {
811 *type = pn_data_type(data);
812 return true;
813 } else {
814 pni_node_t *parent = pn_data_node(data, data->parent);
815 if (parent && parent->atom.type == PN_DESCRIBED) {
816 pn_data_exit(data);
817 return pn_scan_next(data, type, suspend);
818 } else {
819 *type = PN_INVALID;
820 return false;
821 }
822 }
823 }
824
825 static pni_node_t *pni_data_peek(pn_data_t *data);
826
827 int pn_data_vscan(pn_data_t *data, const char *fmt, va_list ap)
828 {
829 pn_data_rewind(data);
830 bool *scanarg = NULL;
831 bool at = false;
832 int level = 0;
833 int count_level = -1;
834 int resume_count = 0;
835
836 while (*fmt) {
837 char code = *(fmt++);
838
839 bool found = false;
840 pn_type_t type;
841
842 bool scanned = false;
843 bool suspend = resume_count > 0;
844
845 switch (code) {
846 case 'n':
847 found = pn_scan_next(data, &type, suspend);
848 if (found && type == PN_NULL) {
849 scanned = true;
850 } else {
851 scanned = false;
852 }
853 if (resume_count && level == count_level) resume_count--;
854 break;
855 case 'o':
856 {
857 bool *value = va_arg(ap, bool *);
858 found = pn_scan_next(data, &type, suspend);
859 if (found && type == PN_BOOL) {
860 *value = pn_data_get_bool(data);
861 scanned = true;
862 } else {
863 *value = 0;
864 scanned = false;
865 }
866 }
867 if (resume_count && level == count_level) resume_count--;
868 break;
869 case 'B':
870 {
871 uint8_t *value = va_arg(ap, uint8_t *);
872 found = pn_scan_next(data, &type, suspend);
873 if (found && type == PN_UBYTE) {
874 *value = pn_data_get_ubyte(data);
875 scanned = true;
876 } else {
877 *value = 0;
878 scanned = false;
879 }
880 }
881 if (resume_count && level == count_level) resume_count--;
882 break;
883 case 'b':
884 {
885 int8_t *value = va_arg(ap, int8_t *);
886 found = pn_scan_next(data, &type, suspend);
887 if (found && type == PN_BYTE) {
888 *value = pn_data_get_byte(data);
889 scanned = true;
890 } else {
891 *value = 0;
892 scanned = false;
893 }
894 }
895 if (resume_count && level == count_level) resume_count--;
896 break;
897 case 'H':
898 {
899 uint16_t *value = va_arg(ap, uint16_t *);
900 found = pn_scan_next(data, &type, suspend);
901 if (found && type == PN_USHORT) {
902 *value = pn_data_get_ushort(data);
903 scanned = true;
904 } else {
905 *value = 0;
906 scanned = false;
907 }
908 }
909 if (resume_count && level == count_level) resume_count--;
910 break;
911 case 'h':
912 {
913 int16_t *value = va_arg(ap, int16_t *);
914 found = pn_scan_next(data, &type, suspend);
915 if (found && type == PN_SHORT) {
916 *value = pn_data_get_short(data);
917 scanned = true;
918 } else {
919 *value = 0;
920 scanned = false;
921 }
922 }
923 if (resume_count && level == count_level) resume_count--;
924 break;
925 case 'I':
926 {
927 uint32_t *value = va_arg(ap, uint32_t *);
928 found = pn_scan_next(data, &type, suspend);
929 if (found && type == PN_UINT) {
930 *value = pn_data_get_uint(data);
931 scanned = true;
932 } else {
933 *value = 0;
934 scanned = false;
935 }
936 }
937 if (resume_count && level == count_level) resume_count--;
938 break;
939 case 'i':
940 {
941 int32_t *value = va_arg(ap, int32_t *);
942 found = pn_scan_next(data, &type, suspend);
943 if (found && type == PN_INT) {
944 *value = pn_data_get_int(data);
945 scanned = true;
946 } else {
947 *value = 0;
948 scanned = false;
949 }
950 }
951 if (resume_count && level == count_level) resume_count--;
952 break;
953 case 'c':
954 {
955 pn_char_t *value = va_arg(ap, pn_char_t *);
956 found = pn_scan_next(data, &type, suspend);
957 if (found && type == PN_CHAR) {
958 *value = pn_data_get_char(data);
959 scanned = true;
960 } else {
961 *value = 0;
962 scanned = false;
963 }
964 }
965 if (resume_count && level == count_level) resume_count--;
966 break;
967 case 'L':
968 {
969 uint64_t *value = va_arg(ap, uint64_t *);
970 found = pn_scan_next(data, &type, suspend);
971 if (found && type == PN_ULONG) {
972 *value = pn_data_get_ulong(data);
973 scanned = true;
974 } else {
975 *value = 0;
976 scanned = false;
977 }
978 }
979 if (resume_count && level == count_level) resume_count--;
980 break;
981 case 'l':
982 {
983 int64_t *value = va_arg(ap, int64_t *);
984 found = pn_scan_next(data, &type, suspend);
985 if (found && type == PN_LONG) {
986 *value = pn_data_get_long(data);
987 scanned = true;
988 } else {
989 *value = 0;
990 scanned = false;
991 }
992 }
993 if (resume_count && level == count_level) resume_count--;
994 break;
995 case 't':
996 {
997 pn_timestamp_t *value = va_arg(ap, pn_timestamp_t *);
998 found = pn_scan_next(data, &type, suspend);
999 if (found && type == PN_TIMESTAMP) {
1000 *value = pn_data_get_timestamp(data);
1001 scanned = true;
1002 } else {
1003 *value = 0;
1004 scanned = false;
1005 }
1006 }
1007 if (resume_count && level == count_level) resume_count--;
1008 break;
1009 case 'f':
1010 {
1011 float *value = va_arg(ap, float *);
1012 found = pn_scan_next(data, &type, suspend);
1013 if (found && type == PN_FLOAT) {
1014 *value = pn_data_get_float(data);
1015 scanned = true;
1016 } else {
1017 *value = 0;
1018 scanned = false;
1019 }
1020 }
1021 if (resume_count && level == count_level) resume_count--;
1022 break;
1023 case 'd':
1024 {
1025 double *value = va_arg(ap, double *);
1026 found = pn_scan_next(data, &type, suspend);
1027 if (found && type == PN_DOUBLE) {
1028 *value = pn_data_get_double(data);
1029 scanned = true;
1030 } else {
1031 *value = 0;
1032 scanned = false;
1033 }
1034 }
1035 if (resume_count && level == count_level) resume_count--;
1036 break;
1037 case 'z':
1038 {
1039 pn_bytes_t *bytes = va_arg(ap, pn_bytes_t *);
1040 found = pn_scan_next(data, &type, suspend);
1041 if (found && type == PN_BINARY) {
1042 *bytes = pn_data_get_binary(data);
1043 scanned = true;
1044 } else {
1045 bytes->start = 0;
1046 bytes->size = 0;
1047 scanned = false;
1048 }
1049 }
1050 if (resume_count && level == count_level) resume_count--;
1051 break;
1052 case 'S':
1053 {
1054 pn_bytes_t *bytes = va_arg(ap, pn_bytes_t *);
1055 found = pn_scan_next(data, &type, suspend);
1056 if (found && type == PN_STRING) {
1057 *bytes = pn_data_get_string(data);
1058 scanned = true;
1059 } else {
1060 bytes->start = 0;
1061 bytes->size = 0;
1062 scanned = false;
1063 }
1064 }
1065 if (resume_count && level == count_level) resume_count--;
1066 break;
1067 case 's':
1068 {
1069 pn_bytes_t *bytes = va_arg(ap, pn_bytes_t *);
1070 found = pn_scan_next(data, &type, suspend);
1071 if (found && type == PN_SYMBOL) {
1072 *bytes = pn_data_get_symbol(data);
1073 scanned = true;
1074 } else {
1075 bytes->start = 0;
1076 bytes->size = 0;
1077 scanned = false;
1078 }
1079 }
1080 if (resume_count && level == count_level) resume_count--;
1081 break;
1082 case 'D':
1083 found = pn_scan_next(data, &type, suspend);
1084 if (found && type == PN_DESCRIBED) {
1085 pn_data_enter(data);
1086 scanned = true;
1087 } else {
1088 if (!suspend) {
1089 resume_count = 3;
1090 count_level = level;
1091 }
1092 scanned = false;
1093 }
1094 if (resume_count && level == count_level) resume_count--;
1095 break;
1096 case '@':
1097 found = pn_scan_next(data, &type, suspend);
1098 if (found && type == PN_ARRAY) {
1099 pn_data_enter(data);
1100 scanned = true;
1101 at = true;
1102 } else {
1103 if (!suspend) {
1104 resume_count = 3;
1105 count_level = level;
1106 }
1107 scanned = false;
1108 }
1109 if (resume_count && level == count_level) resume_count--;
1110 break;
1111 case '[':
1112 if (at) {
1113 scanned = true;
1114 at = false;
1115 } else {
1116 found = pn_scan_next(data, &type, suspend);
1117 if (found && type == PN_LIST) {
1118 pn_data_enter(data);
1119 scanned = true;
1120 } else {
1121 if (!suspend) {
1122 resume_count = 1;
1123 count_level = level;
1124 }
1125 scanned = false;
1126 }
1127 }
1128 level++;
1129 break;
1130 case '{':
1131 found = pn_scan_next(data, &type, suspend);
1132 if (found && type == PN_MAP) {
1133 pn_data_enter(data);
1134 scanned = true;
1135 } else {
1136 if (resume_count) {
1137 resume_count = 1;
1138 count_level = level;
1139 }
1140 scanned = false;
1141 }
1142 level++;
1143 break;
1144 case ']':
1145 case '}':
1146 level--;
1147 if (!suspend && !pn_data_exit(data))
1148 return pn_error_format(pni_data_error(data), PN_ERR, "exit failed");
1149 if (resume_count && level == count_level) resume_count--;
1150 break;
1151 case '.':
1152 found = pn_scan_next(data, &type, suspend);
1153 scanned = found;
1154 if (resume_count && level == count_level) resume_count--;
1155 break;
1156 case '?':
1157 if (!*fmt || *fmt == '?')
1158 return pn_error_format(pni_data_error(data), PN_ARG_ERR, "codes must follow a ?");
1159 scanarg = va_arg(ap, bool *);
1160 break;
1161 case 'C':
1162 {
1163 pn_data_t *dst = va_arg(ap, pn_data_t *);
1164 if (!suspend) {
1165 size_t old = pn_data_size(dst);
1166 pni_node_t *next = pni_data_peek(data);
1167 if (next && next->atom.type != PN_NULL) {
1168 pn_data_narrow(data);
1169 int err = pn_data_appendn(dst, data, 1);
1170 pn_data_widen(data);
1171 if (err) return err;
1172 scanned = pn_data_size(dst) > old;
1173 } else {
1174 scanned = false;
1175 }
1176 pn_data_next(data);
1177 } else {
1178 scanned = false;
1179 }
1180 }
1181 if (resume_count && level == count_level) resume_count--;
1182 break;
1183 case 'a':
1184 {
1185 pn_atom_t *dst = va_arg(ap, pn_atom_t *);
1186 if (!suspend) {
1187 pni_node_t *next = pni_data_peek(data);
1188 if (next && next->atom.type != PN_NULL) {
1189 *dst = next->atom;
1190 } else {
1191 scanned = false;
1192 }
1193 pn_data_next(data);
1194 } else {
1195 scanned = false;
1196 }
1197 }
1198 if (resume_count && level == count_level) resume_count--;
1199 break;
1200 default:
1201 return pn_error_format(pni_data_error(data), PN_ARG_ERR, "unrecognized scan code: 0x%.2X '%c'", code, code);
1202 }
1203
1204 if (scanarg && code != '?') {
1205 *scanarg = scanned;
1206 scanarg = NULL;
1207 }
1208 }
1209
1210 return 0;
1211 }
1212
1213 int pn_data_scan(pn_data_t *data, const char *fmt, ...)
1214 {
1215 va_list ap;
1216 va_start(ap, fmt);
1217 int err = pn_data_vscan(data, fmt, ap);
1218 va_end(ap);
1219 return err;
1220 }
1221
1222 int pn_data_print(pn_data_t *data)
1223 {
1224 pn_string_t *str = pn_string("");
1225 int err = pn_data_inspect(data, str);
1226 if (err) {
1227 pn_free(str);
1228 return err;
1229 }
1230 printf("%s", pn_string_get(str));
1231 pn_free(str);
1232 return 0;
1233 }
1234
1235 int pn_data_format(pn_data_t *data, char *bytes, size_t *size)
1236 {
1237 pn_string_t *str = pn_string("");
1238 int err = pn_data_inspect(data, str);
1239 if (err) return err;
1240 if (pn_string_size(str) >= *size) {
1241 pn_free(str);
1242 return PN_OVERFLOW;
1243 } else {
1244 pn_string_put(str, bytes);
1245 *size = pn_string_size(str);
1246 pn_free(str);
1247 return 0;
1248 }
1249 }
1250
1251 static size_t pni_data_id(pn_data_t *data, pni_node_t *node)
1252 {
1253 return node - data->nodes + 1;
1254 }
1255
1256 static pni_node_t *pni_data_new(pn_data_t *data)
1257 {
1258 if ((data->capacity <= data->size) && (pni_data_grow(data) != 0)) return NULL;
1259 pni_node_t *node = pn_data_node(data, ++(data->size));
1260 node->next = 0;
1261 node->down = 0;
1262 node->children = 0;
1263 return node;
1264 }
1265
1266 void pn_data_rewind(pn_data_t *data)
1267 {
1268 data->parent = data->base_parent;
1269 data->current = data->base_current;
1270 }
1271
1272 static pni_node_t *pni_data_current(pn_data_t *data)
1273 {
1274 return pn_data_node(data, data->current);
1275 }
1276
1277 void pn_data_narrow(pn_data_t *data)
1278 {
1279 data->base_parent = data->parent;
1280 data->base_current = data->current;
1281 }
1282
1283 void pn_data_widen(pn_data_t *data)
1284 {
1285 data->base_parent = 0;
1286 data->base_current = 0;
1287 }
1288
1289 pn_handle_t pn_data_point(pn_data_t *data)
1290 {
1291 if (data->current) {
1292 return (pn_handle_t)(uintptr_t)data->current;
1293 } else {
1294 return (pn_handle_t)(uintptr_t)-data->parent;
1295 }
1296 }
1297
1298 bool pn_data_restore(pn_data_t *data, pn_handle_t point)
1299 {
1300 pn_shandle_t spoint = (pn_shandle_t) point;
1301 if (spoint <= 0 && ((size_t) (-spoint)) <= data->size) {
1302 data->parent = -((pn_shandle_t) point);
1303 data->current = 0;
1304 return true;
1305 } else if (spoint && spoint <= data->size) {
1306 data->current = spoint;
1307 pni_node_t *current = pni_data_current(data);
1308 data->parent = current->parent;
1309 return true;
1310 } else {
1311 return false;
1312 }
1313 }
1314
1315 static pni_node_t *pni_data_peek(pn_data_t *data)
1316 {
1317 pni_node_t *current = pni_data_current(data);
1318 if (current) {
1319 return pn_data_node(data, current->next);
1320 }
1321
1322 pni_node_t *parent = pn_data_node(data, data->parent);
1323 if (parent) {
1324 return pn_data_node(data, parent->down);
1325 }
1326
1327 return NULL;
1328 }
1329
1330 bool pn_data_next(pn_data_t *data)
1331 {
1332 pni_node_t *current = pni_data_current(data);
1333 pni_node_t *parent = pn_data_node(data, data->parent);
1334 size_t next;
1335
1336 if (current) {
1337 next = current->next;
1338 } else if (parent && parent->down) {
1339 next = parent->down;
1340 } else if (!parent && data->size) {
1341 next = 1;
1342 } else {
1343 return false;
1344 }
1345
1346 if (next) {
1347 data->current = next;
1348 return true;
1349 } else {
1350 return false;
1351 }
1352 }
1353
1354 bool pn_data_prev(pn_data_t *data)
1355 {
1356 pni_node_t *node = pni_data_current(data);
1357 if (node && node->prev) {
1358 data->current = node->prev;
1359 return true;
1360 } else {
1361 return false;
1362 }
1363 }
1364
1365 int pni_data_traverse(pn_data_t *data,
1366 int (*enter)(void *ctx, pn_data_t *data, pni_node_t *node),
1367 int (*exit)(void *ctx, pn_data_t *data, pni_node_t *node),
1368 void *ctx)
1369 {
1370 pni_node_t *node = data->size ? pn_data_node(data, 1) : NULL;
1371 while (node) {
1372 pni_node_t *parent = pn_data_node(data, node->parent);
1373
1374 int err = enter(ctx, data, node);
1375 if (err) return err;
1376
1377 size_t next = 0;
1378 if (node->down) {
1379 next = node->down;
1380 } else if (node->next) {
1381 err = exit(ctx, data, node);
1382 if (err) return err;
1383 next = node->next;
1384 } else {
1385 err = exit(ctx, data, node);
1386 if (err) return err;
1387 while (parent) {
1388 err = exit(ctx, data, parent);
1389 if (err) return err;
1390 if (parent->next) {
1391 next = parent->next;
1392 break;
1393 } else {
1394 parent = pn_data_node(data, parent->parent);
1395 }
1396 }
1397 }
1398
1399 node = pn_data_node(data, next);
1400 }
1401
1402 return 0;
1403 }
1404
1405 pn_type_t pn_data_type(pn_data_t *data)
1406 {
1407 pni_node_t *node = pni_data_current(data);
1408 if (node) {
1409 return node->atom.type;
1410 } else {
1411 return PN_INVALID;
1412 }
1413 }
1414
1415 pn_type_t pni_data_parent_type(pn_data_t *data)
1416 {
1417 pni_node_t *node = pn_data_node(data, data->parent);
1418 if (node) {
1419 return node->atom.type;
1420 } else {
1421 return PN_INVALID;
1422 }
1423 }
1424
1425 size_t pn_data_siblings(pn_data_t *data)
1426 {
1427 pni_node_t *node = pn_data_node(data, data->parent);
1428 if (node) {
1429 return node->children;
1430 } else {
1431 return 0;
1432 }
1433 }
1434
1435 bool pn_data_enter(pn_data_t *data)
1436 {
1437 if (data->current) {
1438 data->parent = data->current;
1439 data->current = 0;
1440 return true;
1441 } else {
1442 return false;
1443 }
1444 }
1445
1446 bool pn_data_exit(pn_data_t *data)
1447 {
1448 if (data->parent) {
1449 pni_node_t *parent = pn_data_node(data, data->parent);
1450 data->current = data->parent;
1451 data->parent = parent->parent;
1452 return true;
1453 } else {
1454 return false;
1455 }
1456 }
1457
1458 bool pn_data_lookup(pn_data_t *data, const char *name)
1459 {
1460 while (pn_data_next(data)) {
1461 pn_type_t type = pn_data_type(data);
1462
1463 switch (type) {
1464 case PN_STRING:
1465 case PN_SYMBOL:
1466 {
1467 pn_bytes_t bytes = pn_data_get_bytes(data);
1468 if (pn_bytes_equal(bytes, pn_bytes(strlen(name), name))) {
1469 return pn_data_next(data);
1470 }
1471 }
1472 break;
1473 default:
1474 break;
1475 }
1476
1477 // skip the value
1478 pn_data_next(data);
1479 }
1480
1481 return false;
1482 }
1483
1484 void pn_data_dump(pn_data_t *data)
1485 {
1486 pn_string_t *str = pn_string(0);
1487 printf("{current=%" PN_ZI ", parent=%" PN_ZI "}\n", (size_t) data->current, (size_t) data->parent);
1488 for (unsigned i = 0; i < data->size; i++)
1489 {
1490 pni_node_t *node = &data->nodes[i];
1491 pn_string_setn(str, "", 0);
1492 pni_inspect_atom((pn_atom_t *) &node->atom, str);
1493 printf("Node %i: prev=%" PN_ZI ", next=%" PN_ZI ", parent=%" PN_ZI ", down=%" PN_ZI
1494 ", children=%" PN_ZI ", type=%s (%s)\n",
1495 i + 1, (size_t) node->prev,
1496 (size_t) node->next,
1497 (size_t) node->parent,
1498 (size_t) node->down,
1499 (size_t) node->children,
1500 pn_type_name(node->atom.type), pn_string_get(str));
1501 }
1502 pn_free(str);
1503 }
1504
1505 static pni_node_t *pni_data_add(pn_data_t *data)
1506 {
1507 pni_node_t *current = pni_data_current(data);
1508 pni_node_t *parent = pn_data_node(data, data->parent);
1509 pni_node_t *node;
1510
1511 if (current) {
1512 if (current->next) {
1513 node = pn_data_node(data, current->next);
1514 } else {
1515 node = pni_data_new(data);
1516 if (!node) return NULL;
1517
1518 // refresh the pointers in case we grew
1519 current = pni_data_current(data);
1520 parent = pn_data_node(data, data->parent);
1521 node->prev = data->current;
1522 current->next = pni_data_id(data, node);
1523 node->parent = data->parent;
1524 if (parent) {
1525 if (!parent->down) {
1526 parent->down = pni_data_id(data, node);
1527 }
1528 parent->children++;
1529 }
1530 }
1531 } else if (parent) {
1532 if (parent->down) {
1533 node = pn_data_node(data, parent->down);
1534 } else {
1535 node = pni_data_new(data);
1536 if (!node) return NULL;
1537
1538 // refresh the pointers in case we grew
1539 parent = pn_data_node(data, data->parent);
1540 node->prev = 0;
1541 node->parent = data->parent;
1542 parent->down = pni_data_id(data, node);
1543 parent->children++;
1544 }
1545 } else if (data->size) {
1546 node = pn_data_node(data, 1);
1547 } else {
1548 node = pni_data_new(data);
1549 if (!node) return NULL;
1550
1551 node->prev = 0;
1552 node->parent = 0;
1553 }
1554
1555 node->down = 0;
1556 node->children = 0;
1557 node->data = false;
1558 node->described = false;
1559 node->data_offset = 0;
1560 node->data_size = 0;
1561 data->current = pni_data_id(data, node);
1562 return node;
1563 }
1564
1565 ssize_t pn_data_encode(pn_data_t *data, char *bytes, size_t size)
1566 {
1567 pn_encoder_t encoder;
1568 pn_encoder_initialize(&encoder);
1569 ssize_t r = pn_encoder_encode(&encoder, data, bytes, size);
1570 pn_encoder_finalize(&encoder);
1571 return r;
1572 }
1573
1574 ssize_t pn_data_encoded_size(pn_data_t *data)
1575 {
1576 pn_encoder_t encoder;
1577 pn_encoder_initialize(&encoder);
1578 ssize_t r = pn_encoder_size(&encoder, data);
1579 pn_encoder_finalize(&encoder);
1580 return r;
1581 }
1582
1583 ssize_t pn_data_decode(pn_data_t *data, const char *bytes, size_t size)
1584 {
1585 pn_decoder_t decoder;
1586 pn_decoder_initialize(&decoder);
1587 ssize_t r = pn_decoder_decode(&decoder, bytes, size, data);
1588 pn_decoder_finalize(&decoder);
1589 return r;
1590 }
1591
1592 int pn_data_put_list(pn_data_t *data)
1593 {
1594 pni_node_t *node = pni_data_add(data);
1595 if (node == NULL) return PN_OUT_OF_MEMORY;
1596 node->atom.type = PN_LIST;
1597 return 0;
1598 }
1599
1600 int pn_data_put_map(pn_data_t *data)
1601 {
1602 pni_node_t *node = pni_data_add(data);
1603 if (node == NULL) return PN_OUT_OF_MEMORY;
1604 node->atom.type = PN_MAP;
1605 return 0;
1606 }
1607
1608 int pn_data_put_array(pn_data_t *data, bool described, pn_type_t type)
1609 {
1610 pni_node_t *node = pni_data_add(data);
1611 if (node == NULL) return PN_OUT_OF_MEMORY;
1612 node->atom.type = PN_ARRAY;
1613 node->described = described;
1614 node->type = type;
1615 return 0;
1616 }
1617
1618 void pni_data_set_array_type(pn_data_t *data, pn_type_t type)
1619 {
1620 pni_node_t *array = pni_data_current(data);
1621 if (array) array->type = type;
1622 }
1623
1624 int pn_data_put_described(pn_data_t *data)
1625 {
1626 pni_node_t *node = pni_data_add(data);
1627 if (node == NULL) return PN_OUT_OF_MEMORY;
1628 node->atom.type = PN_DESCRIBED;
1629 return 0;
1630 }
1631
1632 int pn_data_put_null(pn_data_t *data)
1633 {
1634 pni_node_t *node = pni_data_add(data);
1635 if (node == NULL) return PN_OUT_OF_MEMORY;
1636 pni_atom_init(&node->atom, PN_NULL);
1637 return 0;
1638 }
1639
1640 int pn_data_put_bool(pn_data_t *data, bool b)
1641 {
1642 pni_node_t *node = pni_data_add(data);
1643 if (node == NULL) return PN_OUT_OF_MEMORY;
1644 node->atom.type = PN_BOOL;
1645 node->atom.u.as_bool = b;
1646 return 0;
1647 }
1648
1649 int pn_data_put_ubyte(pn_data_t *data, uint8_t ub)
1650 {
1651 pni_node_t *node = pni_data_add(data);
1652 if (node == NULL) return PN_OUT_OF_MEMORY;
1653 node->atom.type = PN_UBYTE;
1654 node->atom.u.as_ubyte = ub;
1655 return 0;
1656 }
1657
1658 int pn_data_put_byte(pn_data_t *data, int8_t b)
1659 {
1660 pni_node_t *node = pni_data_add(data);
1661 if (node == NULL) return PN_OUT_OF_MEMORY;
1662 node->atom.type = PN_BYTE;
1663 node->atom.u.as_byte = b;
1664 return 0;
1665 }
1666
1667 int pn_data_put_ushort(pn_data_t *data, uint16_t us)
1668 {
1669 pni_node_t *node = pni_data_add(data);
1670 if (node == NULL) return PN_OUT_OF_MEMORY;
1671 node->atom.type = PN_USHORT;
1672 node->atom.u.as_ushort = us;
1673 return 0;
1674 }
1675
1676 int pn_data_put_short(pn_data_t *data, int16_t s)
1677 {
1678 pni_node_t *node = pni_data_add(data);
1679 if (node == NULL) return PN_OUT_OF_MEMORY;
1680 node->atom.type = PN_SHORT;
1681 node->atom.u.as_short = s;
1682 return 0;
1683 }
1684
1685 int pn_data_put_uint(pn_data_t *data, uint32_t ui)
1686 {
1687 pni_node_t *node = pni_data_add(data);
1688 if (node == NULL) return PN_OUT_OF_MEMORY;
1689 node->atom.type = PN_UINT;
1690 node->atom.u.as_uint = ui;
1691 return 0;
1692 }
1693
1694 int pn_data_put_int(pn_data_t *data, int32_t i)
1695 {
1696 pni_node_t *node = pni_data_add(data);
1697 if (node == NULL) return PN_OUT_OF_MEMORY;
1698 node->atom.type = PN_INT;
1699 node->atom.u.as_int = i;
1700 return 0;
1701 }
1702
1703 int pn_data_put_char(pn_data_t *data, pn_char_t c)
1704 {
1705 pni_node_t *node = pni_data_add(data);
1706 if (node == NULL) return PN_OUT_OF_MEMORY;
1707 node->atom.type = PN_CHAR;
1708 node->atom.u.as_char = c;
1709 return 0;
1710 }
1711
1712 int pn_data_put_ulong(pn_data_t *data, uint64_t ul)
1713 {
1714 pni_node_t *node = pni_data_add(data);
1715 if (node == NULL) return PN_OUT_OF_MEMORY;
1716 node->atom.type = PN_ULONG;
1717 node->atom.u.as_ulong = ul;
1718 return 0;
1719 }
1720
1721 int pn_data_put_long(pn_data_t *data, int64_t l)
1722 {
1723 pni_node_t *node = pni_data_add(data);
1724 if (node == NULL) return PN_OUT_OF_MEMORY;
1725 node->atom.type = PN_LONG;
1726 node->atom.u.as_long = l;
1727 return 0;
1728 }
1729
1730 int pn_data_put_timestamp(pn_data_t *data, pn_timestamp_t t)
1731 {
1732 pni_node_t *node = pni_data_add(data);
1733 if (node == NULL) return PN_OUT_OF_MEMORY;
1734 node->atom.type = PN_TIMESTAMP;
1735 node->atom.u.as_timestamp = t;
1736 return 0;
1737 }
1738
1739 int pn_data_put_float(pn_data_t *data, float f)
1740 {
1741 pni_node_t *node = pni_data_add(data);
1742 if (node == NULL) return PN_OUT_OF_MEMORY;
1743 node->atom.type = PN_FLOAT;
1744 node->atom.u.as_float = f;
1745 return 0;
1746 }
1747
1748 int pn_data_put_double(pn_data_t *data, double d)
1749 {
1750 pni_node_t *node = pni_data_add(data);
1751 if (node == NULL) return PN_OUT_OF_MEMORY;
1752 node->atom.type = PN_DOUBLE;
1753 node->atom.u.as_double = d;
1754 return 0;
1755 }
1756
1757 int pn_data_put_decimal32(pn_data_t *data, pn_decimal32_t d)
1758 {
1759 pni_node_t *node = pni_data_add(data);
1760 if (node == NULL) return PN_OUT_OF_MEMORY;
1761 node->atom.type = PN_DECIMAL32;
1762 node->atom.u.as_decimal32 = d;
1763 return 0;
1764 }
1765
1766 int pn_data_put_decimal64(pn_data_t *data, pn_decimal64_t d)
1767 {
1768 pni_node_t *node = pni_data_add(data);
1769 if (node == NULL) return PN_OUT_OF_MEMORY;
1770 node->atom.type = PN_DECIMAL64;
1771 node->atom.u.as_decimal64 = d;
1772 return 0;
1773 }
1774
1775 int pn_data_put_decimal128(pn_data_t *data, pn_decimal128_t d)
1776 {
1777 pni_node_t *node = pni_data_add(data);
1778 if (node == NULL) return PN_OUT_OF_MEMORY;
1779 node->atom.type = PN_DECIMAL128;
1780 memmove(node->atom.u.as_decimal128.bytes, d.bytes, 16);
1781 return 0;
1782 }
1783
1784 int pn_data_put_uuid(pn_data_t *data, pn_uuid_t u)
1785 {
1786 pni_node_t *node = pni_data_add(data);
1787 if (node == NULL) return PN_OUT_OF_MEMORY;
1788 node->atom.type = PN_UUID;
1789 memmove(node->atom.u.as_uuid.bytes, u.bytes, 16);
1790 return 0;
1791 }
1792
1793 int pn_data_put_binary(pn_data_t *data, pn_bytes_t bytes)
1794 {
1795 pni_node_t *node = pni_data_add(data);
1796 if (node == NULL) return PN_OUT_OF_MEMORY;
1797 node->atom.type = PN_BINARY;
1798 node->atom.u.as_bytes = bytes;
1799 return pni_data_intern_node(data, node);
1800 }
1801
1802 int pn_data_put_string(pn_data_t *data, pn_bytes_t string)
1803 {
1804 pni_node_t *node = pni_data_add(data);
1805 if (node == NULL) return PN_OUT_OF_MEMORY;
1806 node->atom.type = PN_STRING;
1807 node->atom.u.as_bytes = string;
1808 return pni_data_intern_node(data, node);
1809 }
1810
1811 int pn_data_put_symbol(pn_data_t *data, pn_bytes_t symbol)
1812 {
1813 pni_node_t *node = pni_data_add(data);
1814 if (node == NULL) return PN_OUT_OF_MEMORY;
1815 node->atom.type = PN_SYMBOL;
1816 node->atom.u.as_bytes = symbol;
1817 return pni_data_intern_node(data, node);
1818 }
1819
1820 int pn_data_put_atom(pn_data_t *data, pn_atom_t atom)
1821 {
1822 pni_node_t *node = pni_data_add(data);
1823 if (node == NULL) return PN_OUT_OF_MEMORY;
1824 node->atom = atom;
1825 return pni_data_intern_node(data, node);
1826 }
1827
1828 size_t pn_data_get_list(pn_data_t *data)
1829 {
1830 pni_node_t *node = pni_data_current(data);
1831 if (node && node->atom.type == PN_LIST) {
1832 return node->children;
1833 } else {
1834 return 0;
1835 }
1836 }
1837
1838 size_t pn_data_get_map(pn_data_t *data)
1839 {
1840 pni_node_t *node = pni_data_current(data);
1841 if (node && node->atom.type == PN_MAP) {
1842 return node->children;
1843 } else {
1844 return 0;
1845 }
1846 }
1847
1848 size_t pn_data_get_array(pn_data_t *data)
1849 {
1850 pni_node_t *node = pni_data_current(data);
1851 if (node && node->atom.type == PN_ARRAY) {
1852 if (node->described) {
1853 return node->children - 1;
1854 } else {
1855 return node->children;
1856 }
1857 } else {
1858 return 0;
1859 }
1860 }
1861
1862 bool pn_data_is_array_described(pn_data_t *data)
1863 {
1864 pni_node_t *node = pni_data_current(data);
1865 if (node && node->atom.type == PN_ARRAY) {
1866 return node->described;
1867 } else {
1868 return false;
1869 }
1870 }
1871
1872 pn_type_t pn_data_get_array_type(pn_data_t *data)
1873 {
1874 pni_node_t *node = pni_data_current(data);
1875 if (node && node->atom.type == PN_ARRAY) {
1876 return node->type;
1877 } else {
1878 return PN_INVALID;
1879 }
1880 }
1881
1882 bool pn_data_is_described(pn_data_t *data)
1883 {
1884 pni_node_t *node = pni_data_current(data);
1885 return node && node->atom.type == PN_DESCRIBED;
1886 }
1887
1888 bool pn_data_is_null(pn_data_t *data)
1889 {
1890 pni_node_t *node = pni_data_current(data);
1891 return node && node->atom.type == PN_NULL;
1892 }
1893
1894 bool pn_data_get_bool(pn_data_t *data)
1895 {
1896 pni_node_t *node = pni_data_current(data);
1897 if (node && node->atom.type == PN_BOOL) {
1898 return node->atom.u.as_bool;
1899 } else {
1900 return false;
1901 }
1902 }
1903
1904 uint8_t pn_data_get_ubyte(pn_data_t *data)
1905 {
1906 pni_node_t *node = pni_data_current(data);
1907 if (node && node->atom.type == PN_UBYTE) {
1908 return node->atom.u.as_ubyte;
1909 } else {
1910 return 0;
1911 }
1912 }
1913
1914 int8_t pn_data_get_byte(pn_data_t *data)
1915 {
1916 pni_node_t *node = pni_data_current(data);
1917 if (node && node->atom.type == PN_BYTE) {
1918 return node->atom.u.as_byte;
1919 } else {
1920 return 0;
1921 }
1922 }
1923
1924 uint16_t pn_data_get_ushort(pn_data_t *data)
1925 {
1926 pni_node_t *node = pni_data_current(data);
1927 if (node && node->atom.type == PN_USHORT) {
1928 return node->atom.u.as_ushort;
1929 } else {
1930 return 0;
1931 }
1932 }
1933
1934 int16_t pn_data_get_short(pn_data_t *data)
1935 {
1936 pni_node_t *node = pni_data_current(data);
1937 if (node && node->atom.type == PN_SHORT) {
1938 return node->atom.u.as_short;
1939 } else {
1940 return 0;
1941 }
1942 }
1943
1944 uint32_t pn_data_get_uint(pn_data_t *data)
1945 {
1946 pni_node_t *node = pni_data_current(data);
1947 if (node && node->atom.type == PN_UINT) {
1948 return node->atom.u.as_uint;
1949 } else {
1950 return 0;
1951 }
1952 }
1953
1954 int32_t pn_data_get_int(pn_data_t *data)
1955 {
1956 pni_node_t *node = pni_data_current(data);
1957 if (node && node->atom.type == PN_INT) {
1958 return node->atom.u.as_int;
1959 } else {
1960 return 0;
1961 }
1962 }
1963
1964 pn_char_t pn_data_get_char(pn_data_t *data)
1965 {
1966 pni_node_t *node = pni_data_current(data);
1967 if (node && node->atom.type == PN_CHAR) {
1968 return node->atom.u.as_char;
1969 } else {
1970 return 0;
1971 }
1972 }
1973
1974 uint64_t pn_data_get_ulong(pn_data_t *data)
1975 {
1976 pni_node_t *node = pni_data_current(data);
1977 if (node && node->atom.type == PN_ULONG) {
1978 return node->atom.u.as_ulong;
1979 } else {
1980 return 0;
1981 }
1982 }
1983
1984 int64_t pn_data_get_long(pn_data_t *data)
1985 {
1986 pni_node_t *node = pni_data_current(data);
1987 if (node && node->atom.type == PN_LONG) {
1988 return node->atom.u.as_long;
1989 } else {
1990 return 0;
1991 }
1992 }
1993
1994 pn_timestamp_t pn_data_get_timestamp(pn_data_t *data)
1995 {
1996 pni_node_t *node = pni_data_current(data);
1997 if (node && node->atom.type == PN_TIMESTAMP) {
1998 return node->atom.u.as_timestamp;
1999 } else {
2000 return 0;
2001 }
2002 }
2003
2004 float pn_data_get_float(pn_data_t *data)
2005 {
2006 pni_node_t *node = pni_data_current(data);
2007 if (node && node->atom.type == PN_FLOAT) {
2008 return node->atom.u.as_float;
2009 } else {
2010 return 0;
2011 }
2012 }
2013
2014 double pn_data_get_double(pn_data_t *data)
2015 {
2016 pni_node_t *node = pni_data_current(data);
2017 if (node && node->atom.type == PN_DOUBLE) {
2018 return node->atom.u.as_double;
2019 } else {
2020 return 0;
2021 }
2022 }
2023
2024 pn_decimal32_t pn_data_get_decimal32(pn_data_t *data)
2025 {
2026 pni_node_t *node = pni_data_current(data);
2027 if (node && node->atom.type == PN_DECIMAL32) {
2028 return node->atom.u.as_decimal32;
2029 } else {
2030 return 0;
2031 }
2032 }
2033
2034 pn_decimal64_t pn_data_get_decimal64(pn_data_t *data)
2035 {
2036 pni_node_t *node = pni_data_current(data);
2037 if (node && node->atom.type == PN_DECIMAL64) {
2038 return node->atom.u.as_decimal64;
2039 } else {
2040 return 0;
2041 }
2042 }
2043
2044 pn_decimal128_t pn_data_get_decimal128(pn_data_t *data)
2045 {
2046 pni_node_t *node = pni_data_current(data);
2047 if (node && node->atom.type == PN_DECIMAL128) {
2048 return node->atom.u.as_decimal128;
2049 } else {
2050 pn_decimal128_t t = {{0}};
2051 return t;
2052 }
2053 }
2054
2055 pn_uuid_t pn_data_get_uuid(pn_data_t *data)
2056 {
2057 pni_node_t *node = pni_data_current(data);
2058 if (node && node->atom.type == PN_UUID) {
2059 return node->atom.u.as_uuid;
2060 } else {
2061 pn_uuid_t t = {{0}};
2062 return t;
2063 }
2064 }
2065
2066 pn_bytes_t pn_data_get_binary(pn_data_t *data)
2067 {
2068 pni_node_t *node = pni_data_current(data);
2069 if (node && node->atom.type == PN_BINARY) {
2070 return node->atom.u.as_bytes;
2071 } else {
2072 pn_bytes_t t = {0};
2073 return t;
2074 }
2075 }
2076
2077 pn_bytes_t pn_data_get_string(pn_data_t *data)
2078 {
2079 pni_node_t *node = pni_data_current(data);
2080 if (node && node->atom.type == PN_STRING) {
2081 return node->atom.u.as_bytes;
2082 } else {
2083 pn_bytes_t t = {0};
2084 return t;
2085 }
2086 }
2087
2088 pn_bytes_t pn_data_get_symbol(pn_data_t *data)
2089 {
2090 pni_node_t *node = pni_data_current(data);
2091 if (node && node->atom.type == PN_SYMBOL) {
2092 return node->atom.u.as_bytes;
2093 } else {
2094 pn_bytes_t t = {0};
2095 return t;
2096 }
2097 }
2098
2099 pn_bytes_t pn_data_get_bytes(pn_data_t *data)
2100 {
2101 pni_node_t *node = pni_data_current(data);
2102 if (node && (node->atom.type == PN_BINARY ||
2103 node->atom.type == PN_STRING ||
2104 node->atom.type == PN_SYMBOL)) {
2105 return node->atom.u.as_bytes;
2106 } else {
2107 pn_bytes_t t = {0};
2108 return t;
2109 }
2110 }
2111
2112 pn_atom_t pn_data_get_atom(pn_data_t *data)
2113 {
2114 pni_node_t *node = pni_data_current(data);
2115 if (node) {
2116 return *((pn_atom_t *) &node->atom);
2117 } else {
2118 pn_atom_t t = {PN_NULL, {0,}};
2119 return t;
2120 }
2121 }
2122
2123 int pn_data_copy(pn_data_t *data, pn_data_t *src)
2124 {
2125 pn_data_clear(data);
2126 int err = pn_data_append(data, src);
2127 pn_data_rewind(data);
2128 return err;
2129 }
2130
2131 int pn_data_append(pn_data_t *data, pn_data_t *src)
2132 {
2133 return pn_data_appendn(data, src, -1);
2134 }
2135
2136 int pn_data_appendn(pn_data_t *data, pn_data_t *src, int limit)
2137 {
2138 int err = 0;
2139 int level = 0, count = 0;
2140 bool stop = false;
2141 pn_handle_t point = pn_data_point(src);
2142 pn_data_rewind(src);
2143
2144 while (true) {
2145 while (!pn_data_next(src)) {
2146 if (level > 0) {
2147 pn_data_exit(data);
2148 pn_data_exit(src);
2149 level--;
2150 continue;
2151 }
2152
2153 if (!pn_data_next(src)) {
2154 stop = true;
2155 }
2156 break;
2157 }
2158
2159 if (stop) break;
2160
2161 if (level == 0 && count == limit)
2162 break;
2163
2164 pn_type_t type = pn_data_type(src);
2165 switch (type) {
2166 case PN_NULL:
2167 err = pn_data_put_null(data);
2168 if (level == 0) count++;
2169 break;
2170 case PN_BOOL:
2171 err = pn_data_put_bool(data, pn_data_get_bool(src));
2172 if (level == 0) count++;
2173 break;
2174 case PN_UBYTE:
2175 err = pn_data_put_ubyte(data, pn_data_get_ubyte(src));
2176 if (level == 0) count++;
2177 break;
2178 case PN_BYTE:
2179 err = pn_data_put_byte(data, pn_data_get_byte(src));
2180 if (level == 0) count++;
2181 break;
2182 case PN_USHORT:
2183 err = pn_data_put_ushort(data, pn_data_get_ushort(src));
2184 if (level == 0) count++;
2185 break;
2186 case PN_SHORT:
2187 err = pn_data_put_short(data, pn_data_get_short(src));
2188 if (level == 0) count++;
2189 break;
2190 case PN_UINT:
2191 err = pn_data_put_uint(data, pn_data_get_uint(src));
2192 if (level == 0) count++;
2193 break;
2194 case PN_INT:
2195 err = pn_data_put_int(data, pn_data_get_int(src));
2196 if (level == 0) count++;
2197 break;
2198 case PN_CHAR:
2199 err = pn_data_put_char(data, pn_data_get_char(src));
2200 if (level == 0) count++;
2201 break;
2202 case PN_ULONG:
2203 err = pn_data_put_ulong(data, pn_data_get_ulong(src));
2204 if (level == 0) count++;
2205 break;
2206 case PN_LONG:
2207 err = pn_data_put_long(data, pn_data_get_long(src));
2208 if (level == 0) count++;
2209 break;
2210 case PN_TIMESTAMP:
2211 err = pn_data_put_timestamp(data, pn_data_get_timestamp(src));
2212 if (level == 0) count++;
2213 break;
2214 case PN_FLOAT:
2215 err = pn_data_put_float(data, pn_data_get_float(src));
2216 if (level == 0) count++;
2217 break;
2218 case PN_DOUBLE:
2219 err = pn_data_put_double(data, pn_data_get_double(src));
2220 if (level == 0) count++;
2221 break;
2222 case PN_DECIMAL32:
2223 err = pn_data_put_decimal32(data, pn_data_get_decimal32(src));
2224 if (level == 0) count++;
2225 break;
2226 case PN_DECIMAL64:
2227 err = pn_data_put_decimal64(data, pn_data_get_decimal64(src));
2228 if (level == 0) count++;
2229 break;
2230 case PN_DECIMAL128:
2231 err = pn_data_put_decimal128(data, pn_data_get_decimal128(src));
2232 if (level == 0) count++;
2233 break;
2234 case PN_UUID:
2235 err = pn_data_put_uuid(data, pn_data_get_uuid(src));
2236 if (level == 0) count++;
2237 break;
2238 case PN_BINARY:
2239 err = pn_data_put_binary(data, pn_data_get_binary(src));
2240 if (level == 0) count++;
2241 break;
2242 case PN_STRING:
2243 err = pn_data_put_string(data, pn_data_get_string(src));
2244 if (level == 0) count++;
2245 break;
2246 case PN_SYMBOL:
2247 err = pn_data_put_symbol(data, pn_data_get_symbol(src));
2248 if (level == 0) count++;
2249 break;
2250 case PN_DESCRIBED:
2251 err = pn_data_put_described(data);
2252 if (level == 0) count++;
2253 if (err) { pn_data_restore(src, point); return err; }
2254 pn_data_enter(data);
2255 pn_data_enter(src);
2256 level++;
2257 break;
2258 case PN_ARRAY:
2259 err = pn_data_put_array(data, pn_data_is_array_described(src),
2260 pn_data_get_array_type(src));
2261 if (level == 0) count++;
2262 if (err) { pn_data_restore(src, point); return err; }
2263 pn_data_enter(data);
2264 pn_data_enter(src);
2265 level++;
2266 break;
2267 case PN_LIST:
2268 err = pn_data_put_list(data);
2269 if (level == 0) count++;
2270 if (err) { pn_data_restore(src, point); return err; }
2271 pn_data_enter(data);
2272 pn_data_enter(src);
2273 level++;
2274 break;
2275 case PN_MAP:
2276 err = pn_data_put_map(data);
2277 if (level == 0) count++;
2278 if (err) { pn_data_restore(src, point); return err; }
2279 pn_data_enter(data);
2280 pn_data_enter(src);
2281 level++;
2282 break;
2283 default:
2284 break;
2285 }
2286
2287 if (err) { pn_data_restore(src, point); return err; }
2288 }
2289
2290 pn_data_restore(src, point);
2291
2292 return 0;
2293 }