Debian Codegraph @

c/src/core/value_dump.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 "value_dump.h"
23
24 #include "encodings.h"
25 #include "consumers.h"
26 #include "framing.h"
27 #include "protocol.h"
28 #include "util.h"
29
30 #include "proton/object.h"
31 #include "proton/types.h"
32
33 #include <ctype.h>
34 #include <inttypes.h>
35 #include <stdint.h>
36
37 static inline pn_bytes_t pn_bytes_advance(pn_bytes_t bytes, size_t size) {
38 return (pn_bytes_t) {.size=bytes.size-size,.start=bytes.start+size};
39 }
40
41 // This is only used in places where a described value is not wanted/allowed
42 // So we interpret type to mean the 'base' type of the described value in this case
43 static inline void pni_frame_get_type_value2(pni_consumer_t* consumer, uint8_t* type, pn_bytes_t* value) {
44 if (!pni_consumer_readf8(consumer, type)) goto error;
45 if (*type==PNE_DESCRIPTOR) {
46 // Skip over descriptor potentially recursively
47 uint8_t dtype;
48 pn_bytes_t dvalue;
49 pni_frame_get_type_value2(consumer, &dtype, &dvalue);
50 if (!pni_consumer_readf8(consumer, type)) goto error;
51 }
52 if (!pni_consumer_read_value_not_described(consumer, *type, value)) goto error;
53 return;
54
55 error:
56 *value = (pn_bytes_t){0,0};
57 }
58
59 static inline size_t pni_frame_get_type_value(pn_bytes_t bytes, uint8_t* type, pn_bytes_t* value) {
60 pni_consumer_t consumer = make_consumer_from_bytes(bytes);
61 pni_frame_get_type_value2(&consumer, type, value);
62 return consumer.position;
63 }
64
65 static inline size_t pni_frame_read_value_not_described(pn_bytes_t bytes, uint8_t type, pn_bytes_t* value) {
66 pni_consumer_t consumer = make_consumer_from_bytes(bytes);
67 if (!pni_consumer_read_value_not_described(&consumer, type, value)) {
68 *value = (pn_bytes_t){0,0};
69 };
70 return consumer.position;
71 }
72
73 static inline bool type_isfixedsize(uint8_t type) {
74 return type < 0xa0;
75 }
76
77 static inline bool type_is8bitsize(uint8_t type) {
78 uint8_t subcategory = type >> 4;
79 return subcategory==0xA || subcategory==0xC || subcategory==0xE;
80 }
81
82 static inline bool type_isspecial(uint8_t type) {
83 return (type >> 4) == 0x4;
84 }
85
86 static inline bool type_issimpleint(uint8_t type) {
87 uint8_t subcategory = type >> 4;
88 return (subcategory==0x5 && type<=0x55) || ((subcategory>0x5 && subcategory<=0x8) && (type & 0xe) == 0);
89 }
90
91 static inline bool type_isunsigned_ifsimpleint(uint8_t type) {
92 uint8_t subtype = type & 0xf;
93 return (subtype==0 || subtype==2 || subtype==3);
94 }
95
96 static inline bool type_isulong(uint8_t type) {
97 return type==PNE_ULONG0 || type==PNE_SMALLULONG || type==PNE_ULONG0;
98 }
99
100 static inline bool type_iscompund(uint8_t type) {
101 return type >= 0xc0;
102 }
103
104 static inline bool type_islist_notspecial(uint8_t type) {
105 return type==PNE_LIST8 || type==PNE_LIST32;
106 }
107
108 void pn_value_dump_special(uint8_t type, pn_string_t *output) {
109 switch (type) {
110 case PNE_NULL:
111 pn_string_addf(output, "null");
112 break;
113 case PNE_TRUE:
114 pn_string_addf(output, "true");
115 break;
116 case PNE_FALSE:
117 pn_string_addf(output, "false");
118 break;
119 case PNE_UINT0:
120 pn_string_addf(output, "0x0");
121 break;
122 case PNE_ULONG0:
123 pn_string_addf(output, "0x0");
124 break;
125 case PNE_LIST0:
126 pn_string_addf(output, "[]");
127 break;
128 default:
129 pn_string_addf(output, "!!<unknown>");
130 break;
131 }
132 }
133
134 void pn_value_dump_descriptor_ulong(uint8_t type, pn_bytes_t value, pn_string_t* output, uint64_t* dcode) {
135 uint64_t ulong;
136 switch (type) {
137 case PNE_ULONG0:
138 ulong = 0;
139 break;
140 case PNE_SMALLULONG:
141 ulong = *value.start;
142 break;
143 case PNE_ULONG:
144 ulong = pni_read64(value.start);
145 break;
146 default:
147 // If we get a different descriptor type - huh
148 pn_string_addf(output, "!!<not-a-ulong>");
149 return;
150 }
151 *dcode = ulong;
152
153 // Check if we have a name for this descriptor
154 if (ulong>=FIELD_MIN && ulong<=FIELD_MAX) {
155 uint8_t name_index = FIELDS[ulong-FIELD_MIN].name_index;
156 if (name_index!=0) {
157 pn_string_addf(output, "%s(%" PRIu64 ") ",
158 (const char *)FIELD_STRINGPOOL.STRING0+FIELD_NAME[name_index],
159 ulong);
160 return;
161 }
162 }
163
164 pn_string_addf(output, "%" PRIu64 " ", ulong);
165 return;
166 }
167
168 void pn_value_dump_scalar(uint8_t type, pn_bytes_t value, pn_string_t *output){
169 if (type_isfixedsize(type)) {
170 if (type_isspecial(type)) {
171 pn_value_dump_special(type, output);
172 } else if (type_issimpleint(type)) {
173 // Read bits into unsigned sign extended
174 uint64_t uint;
175 uint64_t mask;
176 switch (value.size) {
177 case 1:
178 uint = (int8_t)*value.start;
179 mask = 0xff;
180 break;
181 case 2:
182 uint = (int16_t)pni_read16(value.start);
183 mask = 0xffff;
184 break;
185 case 4:
186 uint = (int32_t)pni_read32(value.start);
187 mask = 0xffffffff;
188 break;
189 case 8:
190 uint = pni_read64(value.start);
191 mask = 0xffffffffffffffff;
192 break;
193 case 0:
194 // We'll get here if there aren't enough bytes left for the value
195 pn_string_addf(output, "!!");
196 return;
197 default:
198 // It has to be length 1,2,4 or 8!
199 pn_string_addf(output, "!!<WeirdLengthHappened(%zu)>", value.size);
200 return;
201 }
202 if (type_isunsigned_ifsimpleint(type)) {
203 // mask high sign extended bits if unsigned
204 uint = uint & mask;
205 pn_string_addf(output, "0x%" PRIx64, uint);
206 } else {
207 int64_t i = (int64_t)uint;
208 pn_string_addf(output, "%" PRIi64, i);
209 }
210 } else {
211 // Check if we didn't have enough bytes for the value
212 if (value.size==0) {
213 pn_string_addf(output, "!!");
214 return;
215 }
216 switch (type) {
217 case PNE_BOOLEAN:
218 pn_string_addf(output, *value.start ? "true" : "false");
219 break;
220 case PNE_FLOAT: {
221 union {uint32_t i; float f;} conv;
222 conv.i = pni_read32(value.start);
223 pn_string_addf(output, "%g", conv.f);
224 break;
225 }
226 case PNE_DOUBLE: {
227 union {uint64_t i; double f;} conv;
228 conv.i = pni_read64(value.start);
229 pn_string_addf(output, "%g", conv.f);
230 break;
231 }
232 case PNE_UTF32:
233 break;
234 case PNE_MS64: {
235 int64_t timestamp = pni_read64(value.start);
236 pn_string_addf(output, "%" PRIi64, timestamp);
237 break;
238 }
239 case PNE_DECIMAL32:
240 pn_string_addf(output, "D32(%04" PRIx32 ")", pni_read32(value.start));
241 break;
242 case PNE_DECIMAL64:
243 pn_string_addf(output, "D64(%08" PRIx64 ")", pni_read64(value.start));
244 break;
245 case PNE_DECIMAL128:
246 pn_string_addf(
247 output,
248 "D128(%08" PRIx64 "%08" PRIx64 ")",
249 pni_read64(value.start), pni_read64(&value.start[8]));
250 break;
251 case PNE_UUID:
252 pn_string_addf(
253 output,
254 "UUID(%02hhx%02hhx%02hhx%02hhx-"
255 "%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-"
256 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx)",
257 value.start[0], value.start[1], value.start[2],value.start[3],
258 value.start[4], value.start[5],
259 value.start[6], value.start[7],
260 value.start[8], value.start[9],
261 value.start[10], value.start[11], value.start[12], value.start[13], value.start[14], value.start[15]);
262 break;
263 default:
264 pn_string_addf(output, "!!<UnknownType<0x%02hhx>(", type);
265 for (size_t i=0; i<value.size; i++) {
266 pn_string_addf(output, "%.2x", value.start[i]);
267 }
268 pn_string_addf(output, ")>");
269 }
270 }
271 } else {
272 // given we're variable size scalar and we've already calculated the length we can simply switch on subtype
273 // which can only be 0 (binary), 1 (utf8 string) or 3 (symbol) [I wonder what happened to subtype 2!]
274 const char* prefix = "";
275 const char* suffix = "";
276 switch (type & 0xf) {
277 case 0:
278 prefix = "b\"";
279 suffix = "\"";
280 break;
281 case 1:
282 prefix = "\"";
283 suffix = "\"";
284 break;
285 case 3: {
286 bool quote = false;
287 if (!isalpha(value.start[0])) {
288 quote = true;
289 } else {
290 for (size_t i = 1; i < value.size; i++) {
291 if ( !isalnum(value.start[i]) && value.start[i]!='-' ) {
292 quote = true;
293 break;
294 }
295 }
296 }
297 if (quote) {
298 prefix = ":\"";
299 suffix = "\"";
300 } else {
301 prefix = ":";
302 suffix = "";
303 }
304 break;
305 }
306 default:
307 prefix = "<?<";
308 suffix = ">?>";
309 }
310 pn_string_addf(output, "%s", prefix);
311 pn_quote(output, value.start, value.size);
312 pn_string_addf(output, "%s", suffix);
313 }
314 }
315
316 static inline uint32_t consume_count(uint8_t type, pn_bytes_t *value) {
317 uint32_t count;
318 if (type_is8bitsize(type)) {
319 count = value->start[0];
320 *value = pn_bytes_advance(*value, 1);
321 } else {
322 count = pni_read32(value->start);
323 *value = pn_bytes_advance(*value, 4);
324 }
325 return count;
326 }
327
328 void pn_value_dump_nondescribed_value(uint8_t type, pn_bytes_t value, pn_string_t *output);
329
330 void pn_value_dump_list(uint32_t count, pn_bytes_t value, pn_string_t *output) {
331 uint32_t elements = 0;
332 pn_string_addf(output, "[");
333 while (value.size) {
334 elements++;
335 size_t size = pn_value_dump(value, output);
336 value = pn_bytes_advance(value, size);
337 if (value.size) {
338 pn_string_addf(output, ", ");
339 }
340 }
341 pn_string_addf(output, "]");
342 if (elements!=count) {
343 pn_string_addf(output, "<%" PRIu32 "!=%" PRIu32 ">", elements, count);
344 }
345 }
346
347 void pn_value_dump_described_list(uint32_t count, pn_bytes_t value, uint64_t dcode, pn_string_t *output) {
348 uint32_t elements = 0;
349 bool output_element = false;
350 pn_string_addf(output, "[");
351 while (value.size) {
352 uint8_t type = value.start[0];
353 if (type==PNE_NULL) {
354 value = pn_bytes_advance(value, 1);
355 } else {
356 if (output_element) {
357 pn_string_addf(output, ", ");
358 }
359 const pn_fields_t *fields = &FIELDS[dcode-FIELD_MIN];
360 if (elements < fields->field_count) {
361 pn_string_addf(output, "%s=",
362 (const char*)FIELD_STRINGPOOL.STRING0+FIELD_FIELDS[fields->first_field_index+elements]);
363 }
364 size_t size = pn_value_dump(value, output);
365 value = pn_bytes_advance(value, size);
366 output_element = true;
367 }
368 elements++;
369 }
370 pn_string_addf(output, "]");
371 if (elements!=count) {
372 pn_string_addf(output, "<%" PRIu32 "!=%" PRIu32 ">", elements, count);
373 }
374 }
375
376 void pn_value_dump_map(uint32_t count, pn_bytes_t value, pn_string_t *output) {
377 uint32_t elements = 0;
378 pn_string_addf(output, "{");
379 while (value.size) {
380 elements++;
381 size_t size = pn_value_dump(value, output);
382 value = pn_bytes_advance(value, size);
383 if (value.size) {
384 if (elements & 1) {
385 pn_string_addf(output, "=");
386 } else {
387 pn_string_addf(output, ", ");
388 }
389 }
390 }
391 pn_string_addf(output, "}");
392 if (elements!=count) {
393 pn_string_addf(output, "<%" PRIu32 "!=%" PRIu32 ">", elements, count);
394 }
395 }
396
397 static inline const char* pni_type_name(uint8_t type) {
398 switch (type) {
399 case PNE_NULL: return "null";
400 case PNE_TRUE: return "true";
401 case PNE_FALSE: return "false";
402 case PNE_BOOLEAN: return "bool";
403 case PNE_BYTE: return "byte";
404 case PNE_UBYTE: return "ubyte";
405 case PNE_SHORT: return "short";
406 case PNE_USHORT: return "ushort";
407 case PNE_INT:
408 case PNE_SMALLINT: return "int";
409 case PNE_UINT0:
410 case PNE_UINT:
411 case PNE_SMALLUINT: return "uint";
412 case PNE_LONG:
413 case PNE_SMALLLONG: return "long";
414 case PNE_ULONG0:
415 case PNE_ULONG:
416 case PNE_SMALLULONG: return "ulong";
417 case PNE_FLOAT: return "float";
418 case PNE_DOUBLE: return "double";
419 case PNE_DECIMAL32: return "decimal32";
420 case PNE_DECIMAL64: return "decimal64";
421 case PNE_DECIMAL128: return "decimal128";
422 case PNE_UUID: return "uuid";
423 case PNE_MS64: return "timestamp";
424 case PNE_SYM8:
425 case PNE_SYM32: return "symbol";
426 case PNE_STR8_UTF8:
427 case PNE_STR32_UTF8: return "string";
428 case PNE_VBIN8:
429 case PNE_VBIN32: return "binary";
430 case PNE_LIST0:
431 case PNE_LIST8:
432 case PNE_LIST32: return "list";
433 case PNE_MAP8:
434 case PNE_MAP32: return "map";
435 default:
436 break;
437 }
438 return NULL;
439 }
440
441 void pn_value_dump_array(uint32_t count, pn_bytes_t value, pn_string_t *output) {
442 // Read base type only (ignoring descriptor) and first value
443 uint8_t type = 0;
444 pn_bytes_t evalue;
445 if (count==0) {
446 type = *value.start;
447 } else {
448 size_t size = pni_frame_get_type_value(value, &type, &evalue);
449 value = pn_bytes_advance(value, size);
450 }
451 if (type==0){
452 // Type isn't allowed to be 0 at present because we dump described values as the base type anyway
453 pn_string_addf(output, "@<!!>[]");
454 return;
455 }
456 const char* type_name = pni_type_name(type);
457 if (type_name) {
458 pn_string_addf(output, "@<%s>[", type_name);
459 } else {
460 pn_string_addf(output, "@<%02hhx>[", type);
461 }
462 if (count==0) {
463 pn_string_addf(output, "]");
464 return;
465 }
466
467 pn_value_dump_nondescribed_value(type, evalue, output);
468 if (type_isspecial(type)) {
469 if (count>1) {
470 pn_string_addf(output, ", ...(%d more)]", count-1);
471 } else {
472 pn_string_addf(output, "]");
473 }
474 return;
475 }
476 uint32_t elements = 1;
477 while (value.size) {
478 pn_string_addf(output, ", ");
479 elements++;
480 size_t size = pni_frame_read_value_not_described(value, type, &evalue);
481 pn_value_dump_nondescribed_value(type, evalue, output);
482 if (size <= value.size) {
483 value = pn_bytes_advance(value, size);
484 } else {
485 pn_string_addf(output, "<error: %zd > %zd>", size, value.size);
486 value.size = 0;
487 }
488 }
489 pn_string_addf(output, "]");
490 if (elements!=count) {
491 pn_string_addf(output, "<%" PRIu32 "!=%" PRIu32 ">", elements, count);
492 }
493 }
494
495 void pn_value_dump_nondescribed_value(uint8_t type, pn_bytes_t value, pn_string_t *output){
496 if (!type_iscompund(type)) {
497 // The one exception to 'scalar' is LIST0 which is encoded as a special type
498 pn_value_dump_scalar(type, value, output);
499 return;
500 }
501
502 if (value.size==0) {
503 switch (type) {
504 case PNE_LIST8:
505 case PNE_LIST32:
506 pn_string_addf(output, "[!!]");
507 break;
508 case PNE_MAP8:
509 case PNE_MAP32:
510 pn_string_addf(output, "{!!}");
511 break;
512 case PNE_ARRAY8:
513 case PNE_ARRAY32:
514 pn_string_addf(output, "@<>[!!]");
515 break;
516 }
517 return;
518 }
519
520 // Get count
521 uint32_t count = consume_count(type, &value);
522
523 switch (type) {
524 case PNE_LIST8:
525 case PNE_LIST32:
526 pn_value_dump_list(count, value, output);
527 break;
528 case PNE_MAP8:
529 case PNE_MAP32:
530 pn_value_dump_map(count, value, output);
531 break;
532 case PNE_ARRAY8:
533 case PNE_ARRAY32:
534 pn_value_dump_array(count, value, output);
535 break;
536 }
537 }
538
539 size_t pn_value_dump_described(pn_bytes_t bytes, uint64_t dcode, pn_string_t *output) {
540 uint8_t type;
541 pn_bytes_t value;
542 size_t size = pni_frame_get_type_value(bytes, &type, &value);
543 if (size==0) {
544 pn_string_addf(output, "!!");
545 return 0;
546 }
547
548 // The only described type handled differently is list
549 if (type_islist_notspecial(type) && dcode) {
550 if (value.size==0) {
551 pn_string_addf(output, "[!!]");
552 return size;
553 }
554 uint32_t count = consume_count(type, &value);
555 pn_value_dump_described_list(count, value, dcode, output);
556 } else {
557 pn_value_dump_nondescribed_value(type, value, output);
558 }
559 return size;
560 }
561
562 size_t pn_value_dump_nondescribed(pn_bytes_t bytes, pn_string_t *output) {
563 uint8_t type;
564 pn_bytes_t value;
565 size_t size = pni_frame_get_type_value(bytes, &type, &value);
566 if (size==0) {
567 pn_string_addf(output, "!!");
568 return 0;
569 }
570
571 pn_value_dump_nondescribed_value(type, value, output);
572 return size;
573 }
574
575 size_t pn_value_dump(pn_bytes_t frame, pn_string_t *output)
576 {
577 if (frame.size==0) {
578 return 0;
579 }
580 uint8_t type = frame.start[0];
581 // Check for described type first
582 if (type==PNE_DESCRIPTOR) {
583 pn_string_addf(output, "@");
584 frame = pn_bytes_advance(frame, 1);
585 size_t fsize = 1;
586 uint8_t type;
587 pn_bytes_t value;
588 size_t size = pni_frame_get_type_value(frame, &type, &value);
589 frame = pn_bytes_advance(frame, size);
590 fsize += size;
591 if (value.size==0) {
592 pn_string_addf(output, "!!");
593 return fsize;
594 }
595 if (type_isulong(type)) {
596 uint64_t dcode;
597 pn_value_dump_descriptor_ulong(type, value, output, &dcode);
598 fsize += pn_value_dump_described(frame, dcode, output);
599 } else {
600 pn_value_dump_nondescribed_value(type, value, output);
601 fsize += pn_value_dump_nondescribed(frame, output);
602 }
603 return fsize;
604 } else {
605 return pn_value_dump_nondescribed(frame, output);
606 }
607 }