MOS Source Code
Loading...
Searching...
No Matches
pb_decode.c
Go to the documentation of this file.
1/* pb_decode.c -- decode a protobuf using minimal resources
2 *
3 * 2011 Petteri Aimonen <jpa@kapsi.fi>
4 */
5
6/* Use the GCC warn_unused_result attribute to check that all return values
7 * are propagated correctly. On other compilers, gcc before 3.4.0 and iar
8 * before 9.40.1 just ignore the annotation.
9 */
10#if (defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))) || \
11 (defined(__IAR_SYSTEMS_ICC__) && (__VER__ >= 9040001))
12 #define checkreturn __attribute__((warn_unused_result))
13#else
14 #define checkreturn
15#endif
16
17#include "pb.h"
18#include "pb_decode.h"
19#include "pb_common.h"
20
21/**************************************
22 * Declarations internal to this file *
23 **************************************/
24
25static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
26static bool checkreturn pb_decode_varint32_eof(pb_istream_t *stream, uint32_t *dest, bool *eof);
27static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size);
28static bool checkreturn decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
29static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
30static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
31static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
32static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
33static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
34static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension);
35static bool pb_field_set_to_default(pb_field_iter_t *field);
36static bool pb_message_set_to_defaults(pb_field_iter_t *iter);
37static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field);
38static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field);
39static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field);
40static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field);
41static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field);
42static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field);
43static bool checkreturn pb_skip_varint(pb_istream_t *stream);
44static bool checkreturn pb_skip_string(pb_istream_t *stream);
45
46#ifdef PB_ENABLE_MALLOC
47static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
48static void initialize_pointer_field(void *pItem, pb_field_iter_t *field);
49static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field);
50static void pb_release_single_field(pb_field_iter_t *field);
51#endif
52
53#ifdef PB_WITHOUT_64BIT
54#define pb_int64_t int32_t
55#define pb_uint64_t uint32_t
56#else
57#define pb_int64_t int64_t
58#define pb_uint64_t uint64_t
59#endif
60
61typedef struct {
62 uint32_t bitfield[(PB_MAX_REQUIRED_FIELDS + 31) / 32];
64
65/*******************************
66 * pb_istream_t implementation *
67 *******************************/
68
69static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
70{
71 const pb_byte_t *source = (const pb_byte_t*)stream->state;
72 stream->state = (pb_byte_t*)stream->state + count;
73
74 if (buf != NULL)
75 {
76 memcpy(buf, source, count * sizeof(pb_byte_t));
77 }
78
79 return true;
80}
81
82bool checkreturn pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
83{
84 if (count == 0)
85 return true;
86
87#ifndef PB_BUFFER_ONLY
88 if (buf == NULL && stream->callback != buf_read)
89 {
90 /* Skip input bytes */
91 pb_byte_t tmp[16];
92 while (count > 16)
93 {
94 if (!pb_read(stream, tmp, 16))
95 return false;
96
97 count -= 16;
98 }
99
100 return pb_read(stream, tmp, count);
101 }
102#endif
103
104 if (stream->bytes_left < count)
105 PB_RETURN_ERROR(stream, "end-of-stream");
106
107#ifndef PB_BUFFER_ONLY
108 if (!stream->callback(stream, buf, count))
109 PB_RETURN_ERROR(stream, "io error");
110#else
111 if (!buf_read(stream, buf, count))
112 return false;
113#endif
114
115 if (stream->bytes_left < count)
116 stream->bytes_left = 0;
117 else
118 stream->bytes_left -= count;
119
120 return true;
121}
122
123/* Read a single byte from input stream. buf may not be NULL.
124 * This is an optimization for the varint decoding. */
125static bool checkreturn pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
126{
127 if (stream->bytes_left == 0)
128 PB_RETURN_ERROR(stream, "end-of-stream");
129
130#ifndef PB_BUFFER_ONLY
131 if (!stream->callback(stream, buf, 1))
132 PB_RETURN_ERROR(stream, "io error");
133#else
134 *buf = *(const pb_byte_t*)stream->state;
135 stream->state = (pb_byte_t*)stream->state + 1;
136#endif
137
138 stream->bytes_left--;
139
140 return true;
141}
142
143pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
144{
145 pb_istream_t stream;
146 /* Cast away the const from buf without a compiler error. We are
147 * careful to use it only in a const manner in the callbacks.
148 */
149 union {
150 void *state;
151 const void *c_state;
152 } state;
153#ifdef PB_BUFFER_ONLY
154 stream.callback = NULL;
155#else
156 stream.callback = &buf_read;
157#endif
158 state.c_state = buf;
159 stream.state = state.state;
160 stream.bytes_left = msglen;
161#ifndef PB_NO_ERRMSG
162 stream.errmsg = NULL;
163#endif
164 return stream;
165}
166
167/********************
168 * Helper functions *
169 ********************/
170
171static bool checkreturn pb_decode_varint32_eof(pb_istream_t *stream, uint32_t *dest, bool *eof)
172{
173 pb_byte_t byte;
174 uint32_t result;
175
176 if (!pb_readbyte(stream, &byte))
177 {
178 if (stream->bytes_left == 0)
179 {
180 if (eof)
181 {
182 *eof = true;
183 }
184 }
185
186 return false;
187 }
188
189 if ((byte & 0x80) == 0)
190 {
191 /* Quick case, 1 byte value */
192 result = byte;
193 }
194 else
195 {
196 /* Multibyte case */
197 uint_fast8_t bitpos = 7;
198 result = byte & 0x7F;
199
200 do
201 {
202 if (!pb_readbyte(stream, &byte))
203 return false;
204
205 if (bitpos >= 32)
206 {
207 /* Note: The varint could have trailing 0x80 bytes, or 0xFF for negative. */
208 pb_byte_t sign_extension = (bitpos < 63) ? 0xFF : 0x01;
209 bool valid_extension = ((byte & 0x7F) == 0x00 ||
210 ((result >> 31) != 0 && byte == sign_extension));
211
212 if (bitpos >= 64 || !valid_extension)
213 {
214 PB_RETURN_ERROR(stream, "varint overflow");
215 }
216 }
217 else if (bitpos == 28)
218 {
219 if ((byte & 0x70) != 0 && (byte & 0x78) != 0x78)
220 {
221 PB_RETURN_ERROR(stream, "varint overflow");
222 }
223 result |= (uint32_t)(byte & 0x0F) << bitpos;
224 }
225 else
226 {
227 result |= (uint32_t)(byte & 0x7F) << bitpos;
228 }
229 bitpos = (uint_fast8_t)(bitpos + 7);
230 } while (byte & 0x80);
231 }
232
233 *dest = result;
234 return true;
235}
236
237bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
238{
239 return pb_decode_varint32_eof(stream, dest, NULL);
240}
241
242#ifndef PB_WITHOUT_64BIT
243bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
244{
245 pb_byte_t byte;
246 uint_fast8_t bitpos = 0;
247 uint64_t result = 0;
248
249 do
250 {
251 if (!pb_readbyte(stream, &byte))
252 return false;
253
254 if (bitpos >= 63 && (byte & 0xFE) != 0)
255 PB_RETURN_ERROR(stream, "varint overflow");
256
257 result |= (uint64_t)(byte & 0x7F) << bitpos;
258 bitpos = (uint_fast8_t)(bitpos + 7);
259 } while (byte & 0x80);
260
261 *dest = result;
262 return true;
263}
264#endif
265
266bool checkreturn pb_skip_varint(pb_istream_t *stream)
267{
268 pb_byte_t byte;
269 do
270 {
271 if (!pb_read(stream, &byte, 1))
272 return false;
273 } while (byte & 0x80);
274 return true;
275}
276
277bool checkreturn pb_skip_string(pb_istream_t *stream)
278{
279 uint32_t length;
280 if (!pb_decode_varint32(stream, &length))
281 return false;
282
283 if ((size_t)length != length)
284 {
285 PB_RETURN_ERROR(stream, "size too large");
286 }
287
288 return pb_read(stream, NULL, (size_t)length);
289}
290
291bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
292{
293 uint32_t temp;
294 *eof = false;
295 *wire_type = (pb_wire_type_t) 0;
296 *tag = 0;
297
298 if (!pb_decode_varint32_eof(stream, &temp, eof))
299 {
300 return false;
301 }
302
303 *tag = temp >> 3;
304 *wire_type = (pb_wire_type_t)(temp & 7);
305 return true;
306}
307
308bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
309{
310 switch (wire_type)
311 {
312 case PB_WT_VARINT: return pb_skip_varint(stream);
313 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
314 case PB_WT_STRING: return pb_skip_string(stream);
315 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
316 default: PB_RETURN_ERROR(stream, "invalid wire_type");
317 }
318}
319
320/* Read a raw value to buffer, for the purpose of passing it to callback as
321 * a substream. Size is maximum size on call, and actual size on return.
322 */
323static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
324{
325 size_t max_size = *size;
326 switch (wire_type)
327 {
328 case PB_WT_VARINT:
329 *size = 0;
330 do
331 {
332 (*size)++;
333 if (*size > max_size)
334 PB_RETURN_ERROR(stream, "varint overflow");
335
336 if (!pb_read(stream, buf, 1))
337 return false;
338 } while (*buf++ & 0x80);
339 return true;
340
341 case PB_WT_64BIT:
342 *size = 8;
343 return pb_read(stream, buf, 8);
344
345 case PB_WT_32BIT:
346 *size = 4;
347 return pb_read(stream, buf, 4);
348
349 case PB_WT_STRING:
350 /* Calling read_raw_value with a PB_WT_STRING is an error.
351 * Explicitly handle this case and fallthrough to default to avoid
352 * compiler warnings.
353 */
354
355 default: PB_RETURN_ERROR(stream, "invalid wire_type");
356 }
357}
358
359/* Decode string length from stream and return a substream with limited length.
360 * Remember to close the substream using pb_close_string_substream().
361 */
362bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
363{
365 if (!pb_decode_varint32(stream, &size))
366 return false;
367
368 *substream = *stream;
369 if (substream->bytes_left < size)
370 PB_RETURN_ERROR(stream, "parent stream too short");
371
372 substream->bytes_left = (size_t)size;
373 stream->bytes_left -= (size_t)size;
374 return true;
375}
376
377bool checkreturn pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
378{
379 if (substream->bytes_left) {
380 if (!pb_read(substream, NULL, substream->bytes_left))
381 return false;
382 }
383
384 stream->state = substream->state;
385
386#ifndef PB_NO_ERRMSG
387 stream->errmsg = substream->errmsg;
388#endif
389 return true;
390}
391
392/*************************
393 * Decode a single field *
394 *************************/
395
396static bool checkreturn decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
397{
398 switch (PB_LTYPE(field->type))
399 {
400 case PB_LTYPE_BOOL:
401 if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED)
402 PB_RETURN_ERROR(stream, "wrong wire type");
403
404 return pb_dec_bool(stream, field);
405
406 case PB_LTYPE_VARINT:
407 case PB_LTYPE_UVARINT:
408 case PB_LTYPE_SVARINT:
409 if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED)
410 PB_RETURN_ERROR(stream, "wrong wire type");
411
412 return pb_dec_varint(stream, field);
413
414 case PB_LTYPE_FIXED32:
415 if (wire_type != PB_WT_32BIT && wire_type != PB_WT_PACKED)
416 PB_RETURN_ERROR(stream, "wrong wire type");
417
418 return pb_decode_fixed32(stream, field->pData);
419
420 case PB_LTYPE_FIXED64:
421 if (wire_type != PB_WT_64BIT && wire_type != PB_WT_PACKED)
422 PB_RETURN_ERROR(stream, "wrong wire type");
423
424#ifdef PB_CONVERT_DOUBLE_FLOAT
425 if (field->data_size == sizeof(float))
426 {
427 return pb_decode_double_as_float(stream, (float*)field->pData);
428 }
429#endif
430
431#ifdef PB_WITHOUT_64BIT
432 PB_RETURN_ERROR(stream, "invalid data_size");
433#else
434 return pb_decode_fixed64(stream, field->pData);
435#endif
436
437 case PB_LTYPE_BYTES:
438 if (wire_type != PB_WT_STRING)
439 PB_RETURN_ERROR(stream, "wrong wire type");
440
441 return pb_dec_bytes(stream, field);
442
443 case PB_LTYPE_STRING:
444 if (wire_type != PB_WT_STRING)
445 PB_RETURN_ERROR(stream, "wrong wire type");
446
447 return pb_dec_string(stream, field);
448
451 if (wire_type != PB_WT_STRING)
452 PB_RETURN_ERROR(stream, "wrong wire type");
453
454 return pb_dec_submessage(stream, field);
455
457 if (wire_type != PB_WT_STRING)
458 PB_RETURN_ERROR(stream, "wrong wire type");
459
460 return pb_dec_fixed_length_bytes(stream, field);
461
462 default:
463 PB_RETURN_ERROR(stream, "invalid field type");
464 }
465}
466
467static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
468{
469 switch (PB_HTYPE(field->type))
470 {
472 return decode_basic_field(stream, wire_type, field);
473
475 if (field->pSize != NULL)
476 *(bool*)field->pSize = true;
477 return decode_basic_field(stream, wire_type, field);
478
480 if (wire_type == PB_WT_STRING
481 && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
482 {
483 /* Packed array */
484 bool status = true;
485 pb_istream_t substream;
486 pb_size_t *size = (pb_size_t*)field->pSize;
487 field->pData = (char*)field->pField + field->data_size * (*size);
488
489 if (!pb_make_string_substream(stream, &substream))
490 return false;
491
492 while (substream.bytes_left > 0 && *size < field->array_size)
493 {
494 if (!decode_basic_field(&substream, PB_WT_PACKED, field))
495 {
496 status = false;
497 break;
498 }
499 (*size)++;
500 field->pData = (char*)field->pData + field->data_size;
501 }
502
503 if (substream.bytes_left != 0)
504 PB_RETURN_ERROR(stream, "array overflow");
505 if (!pb_close_string_substream(stream, &substream))
506 return false;
507
508 return status;
509 }
510 else
511 {
512 /* Repeated field */
513 pb_size_t *size = (pb_size_t*)field->pSize;
514 field->pData = (char*)field->pField + field->data_size * (*size);
515
516 if ((*size)++ >= field->array_size)
517 PB_RETURN_ERROR(stream, "array overflow");
518
519 return decode_basic_field(stream, wire_type, field);
520 }
521
522 case PB_HTYPE_ONEOF:
523 if (PB_LTYPE_IS_SUBMSG(field->type) &&
524 *(pb_size_t*)field->pSize != field->tag)
525 {
526 /* We memset to zero so that any callbacks are set to NULL.
527 * This is because the callbacks might otherwise have values
528 * from some other union field.
529 * If callbacks are needed inside oneof field, use .proto
530 * option submsg_callback to have a separate callback function
531 * that can set the fields before submessage is decoded.
532 * pb_dec_submessage() will set any default values. */
533 memset(field->pData, 0, (size_t)field->data_size);
534
535 /* Set default values for the submessage fields. */
536 if (field->submsg_desc->default_value != NULL ||
537 field->submsg_desc->field_callback != NULL ||
538 field->submsg_desc->submsg_info[0] != NULL)
539 {
540 pb_field_iter_t submsg_iter;
541 if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData))
542 {
543 if (!pb_message_set_to_defaults(&submsg_iter))
544 PB_RETURN_ERROR(stream, "failed to set defaults");
545 }
546 }
547 }
548 *(pb_size_t*)field->pSize = field->tag;
549
550 return decode_basic_field(stream, wire_type, field);
551
552 default:
553 PB_RETURN_ERROR(stream, "invalid field type");
554 }
555}
556
557#ifdef PB_ENABLE_MALLOC
558/* Allocate storage for the field and store the pointer at iter->pData.
559 * array_size is the number of entries to reserve in an array.
560 * Zero size is not allowed, use pb_free() for releasing.
561 */
562static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
563{
564 void *ptr = *(void**)pData;
565
566 if (data_size == 0 || array_size == 0)
567 PB_RETURN_ERROR(stream, "invalid size");
568
569#ifdef __AVR__
570 /* Workaround for AVR libc bug 53284: http://savannah.nongnu.org/bugs/?53284
571 * Realloc to size of 1 byte can cause corruption of the malloc structures.
572 */
573 if (data_size == 1 && array_size == 1)
574 {
575 data_size = 2;
576 }
577#endif
578
579 /* Check for multiplication overflows.
580 * This code avoids the costly division if the sizes are small enough.
581 * Multiplication is safe as long as only half of bits are set
582 * in either multiplicand.
583 */
584 {
585 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
586 if (data_size >= check_limit || array_size >= check_limit)
587 {
588 const size_t size_max = (size_t)-1;
589 if (size_max / array_size < data_size)
590 {
591 PB_RETURN_ERROR(stream, "size too large");
592 }
593 }
594 }
595
596 /* Allocate new or expand previous allocation */
597 /* Note: on failure the old pointer will remain in the structure,
598 * the message must be freed by caller also on error return. */
599 ptr = pb_realloc(ptr, array_size * data_size);
600 if (ptr == NULL)
601 PB_RETURN_ERROR(stream, "realloc failed");
602
603 *(void**)pData = ptr;
604 return true;
605}
606
607/* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
608static void initialize_pointer_field(void *pItem, pb_field_iter_t *field)
609{
610 if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
611 PB_LTYPE(field->type) == PB_LTYPE_BYTES)
612 {
613 *(void**)pItem = NULL;
614 }
615 else if (PB_LTYPE_IS_SUBMSG(field->type))
616 {
617 /* We memset to zero so that any callbacks are set to NULL.
618 * Default values will be set by pb_dec_submessage(). */
619 memset(pItem, 0, field->data_size);
620 }
621}
622#endif
623
624static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
625{
626#ifndef PB_ENABLE_MALLOC
627 PB_UNUSED(wire_type);
628 PB_UNUSED(field);
629 PB_RETURN_ERROR(stream, "no malloc support");
630#else
631 switch (PB_HTYPE(field->type))
632 {
635 case PB_HTYPE_ONEOF:
636 if (PB_LTYPE_IS_SUBMSG(field->type) && *(void**)field->pField != NULL)
637 {
638 /* Duplicate field, have to release the old allocation first. */
639 /* FIXME: Does this work correctly for oneofs? */
640 pb_release_single_field(field);
641 }
642
643 if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
644 {
645 *(pb_size_t*)field->pSize = field->tag;
646 }
647
648 if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
649 PB_LTYPE(field->type) == PB_LTYPE_BYTES)
650 {
651 /* pb_dec_string and pb_dec_bytes handle allocation themselves */
652 field->pData = field->pField;
653 return decode_basic_field(stream, wire_type, field);
654 }
655 else
656 {
657 if (!allocate_field(stream, field->pField, field->data_size, 1))
658 return false;
659
660 field->pData = *(void**)field->pField;
661 initialize_pointer_field(field->pData, field);
662 return decode_basic_field(stream, wire_type, field);
663 }
664
666 if (wire_type == PB_WT_STRING
667 && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
668 {
669 /* Packed array, multiple items come in at once. */
670 bool status = true;
671 pb_size_t *size = (pb_size_t*)field->pSize;
672 size_t allocated_size = *size;
673 pb_istream_t substream;
674
675 if (!pb_make_string_substream(stream, &substream))
676 return false;
677
678 while (substream.bytes_left)
679 {
680 if (*size == PB_SIZE_MAX)
681 {
682#ifndef PB_NO_ERRMSG
683 stream->errmsg = "too many array entries";
684#endif
685 status = false;
686 break;
687 }
688
689 if ((size_t)*size + 1 > allocated_size)
690 {
691 /* Allocate more storage. This tries to guess the
692 * number of remaining entries. Round the division
693 * upwards. */
694 size_t remain = (substream.bytes_left - 1) / field->data_size + 1;
695 if (remain < PB_SIZE_MAX - allocated_size)
696 allocated_size += remain;
697 else
698 allocated_size += 1;
699
700 if (!allocate_field(&substream, field->pField, field->data_size, allocated_size))
701 {
702 status = false;
703 break;
704 }
705 }
706
707 /* Decode the array entry */
708 field->pData = *(char**)field->pField + field->data_size * (*size);
709 if (field->pData == NULL)
710 {
711 /* Shouldn't happen, but satisfies static analyzers */
712 status = false;
713 break;
714 }
715 initialize_pointer_field(field->pData, field);
716 if (!decode_basic_field(&substream, PB_WT_PACKED, field))
717 {
718 status = false;
719 break;
720 }
721
722 (*size)++;
723 }
724 if (!pb_close_string_substream(stream, &substream))
725 return false;
726
727 return status;
728 }
729 else
730 {
731 /* Normal repeated field, i.e. only one item at a time. */
732 pb_size_t *size = (pb_size_t*)field->pSize;
733
734 if (*size == PB_SIZE_MAX)
735 PB_RETURN_ERROR(stream, "too many array entries");
736
737 if (!allocate_field(stream, field->pField, field->data_size, (size_t)(*size + 1)))
738 return false;
739
740 field->pData = *(char**)field->pField + field->data_size * (*size);
741 (*size)++;
742 initialize_pointer_field(field->pData, field);
743 return decode_basic_field(stream, wire_type, field);
744 }
745
746 default:
747 PB_RETURN_ERROR(stream, "invalid field type");
748 }
749#endif
750}
751
752static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
753{
754 if (!field->descriptor->field_callback)
755 return pb_skip_field(stream, wire_type);
756
757 if (wire_type == PB_WT_STRING)
758 {
759 pb_istream_t substream;
760 size_t prev_bytes_left;
761
762 if (!pb_make_string_substream(stream, &substream))
763 return false;
764
765 do
766 {
767 prev_bytes_left = substream.bytes_left;
768 if (!field->descriptor->field_callback(&substream, NULL, field))
769 {
770 PB_SET_ERROR(stream, substream.errmsg ? substream.errmsg : "callback failed");
771 return false;
772 }
773 } while (substream.bytes_left > 0 && substream.bytes_left < prev_bytes_left);
774
775 if (!pb_close_string_substream(stream, &substream))
776 return false;
777
778 return true;
779 }
780 else
781 {
782 /* Copy the single scalar value to stack.
783 * This is required so that we can limit the stream length,
784 * which in turn allows to use same callback for packed and
785 * not-packed fields. */
786 pb_istream_t substream;
787 pb_byte_t buffer[10];
788 size_t size = sizeof(buffer);
789
790 if (!read_raw_value(stream, wire_type, buffer, &size))
791 return false;
792 substream = pb_istream_from_buffer(buffer, size);
793
794 return field->descriptor->field_callback(&substream, NULL, field);
795 }
796}
797
798static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
799{
800#ifdef PB_ENABLE_MALLOC
801 /* When decoding an oneof field, check if there is old data that must be
802 * released first. */
803 if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
804 {
805 if (!pb_release_union_field(stream, field))
806 return false;
807 }
808#endif
809
810 switch (PB_ATYPE(field->type))
811 {
812 case PB_ATYPE_STATIC:
813 return decode_static_field(stream, wire_type, field);
814
815 case PB_ATYPE_POINTER:
816 return decode_pointer_field(stream, wire_type, field);
817
819 return decode_callback_field(stream, wire_type, field);
820
821 default:
822 PB_RETURN_ERROR(stream, "invalid field type");
823 }
824}
825
826/* Default handler for extension fields. Expects to have a pb_msgdesc_t
827 * pointer in the extension->type->arg field, pointing to a message with
828 * only one field in it. */
829static bool checkreturn default_extension_decoder(pb_istream_t *stream,
830 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
831{
832 pb_field_iter_t iter;
833
834 if (!pb_field_iter_begin_extension(&iter, extension))
835 PB_RETURN_ERROR(stream, "invalid extension");
836
837 if (iter.tag != tag || !iter.message)
838 return true;
839
840 extension->found = true;
841 return decode_field(stream, wire_type, &iter);
842}
843
844/* Try to decode an unknown field as an extension field. Tries each extension
845 * decoder in turn, until one of them handles the field or loop ends. */
846static bool checkreturn decode_extension(pb_istream_t *stream,
847 uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension)
848{
849 size_t pos = stream->bytes_left;
850
851 while (extension != NULL && pos == stream->bytes_left)
852 {
853 bool status;
854 if (extension->type->decode)
855 status = extension->type->decode(stream, extension, tag, wire_type);
856 else
857 status = default_extension_decoder(stream, extension, tag, wire_type);
858
859 if (!status)
860 return false;
861
862 extension = extension->next;
863 }
864
865 return true;
866}
867
868/* Initialize message fields to default values, recursively */
869static bool pb_field_set_to_default(pb_field_iter_t *field)
870{
871 pb_type_t type;
872 type = field->type;
873
874 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
875 {
876 pb_extension_t *ext = *(pb_extension_t* const *)field->pData;
877 while (ext != NULL)
878 {
879 pb_field_iter_t ext_iter;
880 if (pb_field_iter_begin_extension(&ext_iter, ext))
881 {
882 ext->found = false;
883 if (!pb_message_set_to_defaults(&ext_iter))
884 return false;
885 }
886 ext = ext->next;
887 }
888 }
889 else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
890 {
891 bool init_data = true;
892 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL)
893 {
894 /* Set has_field to false. Still initialize the optional field
895 * itself also. */
896 *(bool*)field->pSize = false;
897 }
898 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
899 PB_HTYPE(type) == PB_HTYPE_ONEOF)
900 {
901 /* REPEATED: Set array count to 0, no need to initialize contents.
902 ONEOF: Set which_field to 0. */
903 *(pb_size_t*)field->pSize = 0;
904 init_data = false;
905 }
906
907 if (init_data)
908 {
909 if (PB_LTYPE_IS_SUBMSG(field->type) &&
910 (field->submsg_desc->default_value != NULL ||
911 field->submsg_desc->field_callback != NULL ||
912 field->submsg_desc->submsg_info[0] != NULL))
913 {
914 /* Initialize submessage to defaults.
915 * Only needed if it has default values
916 * or callback/submessage fields. */
917 pb_field_iter_t submsg_iter;
918 if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData))
919 {
920 if (!pb_message_set_to_defaults(&submsg_iter))
921 return false;
922 }
923 }
924 else
925 {
926 /* Initialize to zeros */
927 memset(field->pData, 0, (size_t)field->data_size);
928 }
929 }
930 }
931 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
932 {
933 /* Initialize the pointer to NULL. */
934 *(void**)field->pField = NULL;
935
936 /* Initialize array count to 0. */
937 if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
938 PB_HTYPE(type) == PB_HTYPE_ONEOF)
939 {
940 *(pb_size_t*)field->pSize = 0;
941 }
942 }
943 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
944 {
945 /* Don't overwrite callback */
946 }
947
948 return true;
949}
950
951static bool pb_message_set_to_defaults(pb_field_iter_t *iter)
952{
953 pb_istream_t defstream = PB_ISTREAM_EMPTY;
954 uint32_t tag = 0;
955 pb_wire_type_t wire_type = PB_WT_VARINT;
956 bool eof;
957
958 if (iter->descriptor->default_value)
959 {
960 defstream = pb_istream_from_buffer(iter->descriptor->default_value, (size_t)-1);
961 if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof))
962 return false;
963 }
964
965 do
966 {
967 if (!pb_field_set_to_default(iter))
968 return false;
969
970 if (tag != 0 && iter->tag == tag)
971 {
972 /* We have a default value for this field in the defstream */
973 if (!decode_field(&defstream, wire_type, iter))
974 return false;
975 if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof))
976 return false;
977
978 if (iter->pSize)
979 *(bool*)iter->pSize = false;
980 }
981 } while (pb_field_iter_next(iter));
982
983 return true;
984}
985
986/*********************
987 * Decode all fields *
988 *********************/
989
990static bool checkreturn pb_decode_inner(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
991{
992 uint32_t extension_range_start = 0;
993 pb_extension_t *extensions = NULL;
994
995 /* 'fixed_count_field' and 'fixed_count_size' track position of a repeated fixed
996 * count field. This can only handle _one_ repeated fixed count field that
997 * is unpacked and unordered among other (non repeated fixed count) fields.
998 */
999 pb_size_t fixed_count_field = PB_SIZE_MAX;
1000 pb_size_t fixed_count_size = 0;
1001 pb_size_t fixed_count_total_size = 0;
1002
1003 pb_fields_seen_t fields_seen = {{0, 0}};
1004 const uint32_t allbits = ~(uint32_t)0;
1005 pb_field_iter_t iter;
1006
1007 if (pb_field_iter_begin(&iter, fields, dest_struct))
1008 {
1009 if ((flags & PB_DECODE_NOINIT) == 0)
1010 {
1011 if (!pb_message_set_to_defaults(&iter))
1012 PB_RETURN_ERROR(stream, "failed to set defaults");
1013 }
1014 }
1015
1016 while (stream->bytes_left)
1017 {
1018 uint32_t tag;
1019 pb_wire_type_t wire_type;
1020 bool eof;
1021
1022 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
1023 {
1024 if (eof)
1025 break;
1026 else
1027 return false;
1028 }
1029
1030 if (tag == 0)
1031 {
1032 if (flags & PB_DECODE_NULLTERMINATED)
1033 {
1034 break;
1035 }
1036 else
1037 {
1038 PB_RETURN_ERROR(stream, "zero tag");
1039 }
1040 }
1041
1042 if (!pb_field_iter_find(&iter, tag) || PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION)
1043 {
1044 /* No match found, check if it matches an extension. */
1045 if (extension_range_start == 0)
1046 {
1048 {
1049 extensions = *(pb_extension_t* const *)iter.pData;
1050 extension_range_start = iter.tag;
1051 }
1052
1053 if (!extensions)
1054 {
1055 extension_range_start = (uint32_t)-1;
1056 }
1057 }
1058
1059 if (tag >= extension_range_start)
1060 {
1061 size_t pos = stream->bytes_left;
1062
1063 if (!decode_extension(stream, tag, wire_type, extensions))
1064 return false;
1065
1066 if (pos != stream->bytes_left)
1067 {
1068 /* The field was handled */
1069 continue;
1070 }
1071 }
1072
1073 /* No match found, skip data */
1074 if (!pb_skip_field(stream, wire_type))
1075 return false;
1076 continue;
1077 }
1078
1079 /* If a repeated fixed count field was found, get size from
1080 * 'fixed_count_field' as there is no counter contained in the struct.
1081 */
1082 if (PB_HTYPE(iter.type) == PB_HTYPE_REPEATED && iter.pSize == &iter.array_size)
1083 {
1084 if (fixed_count_field != iter.index) {
1085 /* If the new fixed count field does not match the previous one,
1086 * check that the previous one is NULL or that it finished
1087 * receiving all the expected data.
1088 */
1089 if (fixed_count_field != PB_SIZE_MAX &&
1090 fixed_count_size != fixed_count_total_size)
1091 {
1092 PB_RETURN_ERROR(stream, "wrong size for fixed count field");
1093 }
1094
1095 fixed_count_field = iter.index;
1096 fixed_count_size = 0;
1097 fixed_count_total_size = iter.array_size;
1098 }
1099
1100 iter.pSize = &fixed_count_size;
1101 }
1102
1103 if (PB_HTYPE(iter.type) == PB_HTYPE_REQUIRED
1104 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
1105 {
1106 uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
1107 fields_seen.bitfield[iter.required_field_index >> 5] |= tmp;
1108 }
1109
1110 if (!decode_field(stream, wire_type, &iter))
1111 return false;
1112 }
1113
1114 /* Check that all elements of the last decoded fixed count field were present. */
1115 if (fixed_count_field != PB_SIZE_MAX &&
1116 fixed_count_size != fixed_count_total_size)
1117 {
1118 PB_RETURN_ERROR(stream, "wrong size for fixed count field");
1119 }
1120
1121 /* Check that all required fields were present. */
1122 {
1123 pb_size_t req_field_count = iter.descriptor->required_field_count;
1124
1125 if (req_field_count > 0)
1126 {
1127 pb_size_t i;
1128
1129 if (req_field_count > PB_MAX_REQUIRED_FIELDS)
1130 req_field_count = PB_MAX_REQUIRED_FIELDS;
1131
1132 /* Check the whole words */
1133 for (i = 0; i < (req_field_count >> 5); i++)
1134 {
1135 if (fields_seen.bitfield[i] != allbits)
1136 PB_RETURN_ERROR(stream, "missing required field");
1137 }
1138
1139 /* Check the remaining bits (if any) */
1140 if ((req_field_count & 31) != 0)
1141 {
1142 if (fields_seen.bitfield[req_field_count >> 5] !=
1143 (allbits >> (uint_least8_t)(32 - (req_field_count & 31))))
1144 {
1145 PB_RETURN_ERROR(stream, "missing required field");
1146 }
1147 }
1148 }
1149 }
1150
1151 return true;
1152}
1153
1154bool checkreturn pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
1155{
1156 bool status;
1157
1158 if ((flags & PB_DECODE_DELIMITED) == 0)
1159 {
1160 status = pb_decode_inner(stream, fields, dest_struct, flags);
1161 }
1162 else
1163 {
1164 pb_istream_t substream;
1165 if (!pb_make_string_substream(stream, &substream))
1166 return false;
1167
1168 status = pb_decode_inner(&substream, fields, dest_struct, flags);
1169
1170 if (!pb_close_string_substream(stream, &substream))
1171 return false;
1172 }
1173
1174#ifdef PB_ENABLE_MALLOC
1175 if (!status)
1176 pb_release(fields, dest_struct);
1177#endif
1178
1179 return status;
1180}
1181
1182bool checkreturn pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
1183{
1184 bool status;
1185
1186 status = pb_decode_inner(stream, fields, dest_struct, 0);
1187
1188#ifdef PB_ENABLE_MALLOC
1189 if (!status)
1190 pb_release(fields, dest_struct);
1191#endif
1192
1193 return status;
1194}
1195
1196#ifdef PB_ENABLE_MALLOC
1197/* Given an oneof field, if there has already been a field inside this oneof,
1198 * release it before overwriting with a different one. */
1199static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field)
1200{
1201 pb_field_iter_t old_field = *field;
1202 pb_size_t old_tag = *(pb_size_t*)field->pSize; /* Previous which_ value */
1203 pb_size_t new_tag = field->tag; /* New which_ value */
1204
1205 if (old_tag == 0)
1206 return true; /* Ok, no old data in union */
1207
1208 if (old_tag == new_tag)
1209 return true; /* Ok, old data is of same type => merge */
1210
1211 /* Release old data. The find can fail if the message struct contains
1212 * invalid data. */
1213 if (!pb_field_iter_find(&old_field, old_tag))
1214 PB_RETURN_ERROR(stream, "invalid union tag");
1215
1216 pb_release_single_field(&old_field);
1217
1218 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1219 {
1220 /* Initialize the pointer to NULL to make sure it is valid
1221 * even in case of error return. */
1222 *(void**)field->pField = NULL;
1223 field->pData = NULL;
1224 }
1225
1226 return true;
1227}
1228
1229static void pb_release_single_field(pb_field_iter_t *field)
1230{
1231 pb_type_t type;
1232 type = field->type;
1233
1234 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
1235 {
1236 if (*(pb_size_t*)field->pSize != field->tag)
1237 return; /* This is not the current field in the union */
1238 }
1239
1240 /* Release anything contained inside an extension or submsg.
1241 * This has to be done even if the submsg itself is statically
1242 * allocated. */
1243 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
1244 {
1245 /* Release fields from all extensions in the linked list */
1246 pb_extension_t *ext = *(pb_extension_t**)field->pData;
1247 while (ext != NULL)
1248 {
1249 pb_field_iter_t ext_iter;
1250 if (pb_field_iter_begin_extension(&ext_iter, ext))
1251 {
1252 pb_release_single_field(&ext_iter);
1253 }
1254 ext = ext->next;
1255 }
1256 }
1257 else if (PB_LTYPE_IS_SUBMSG(type) && PB_ATYPE(type) != PB_ATYPE_CALLBACK)
1258 {
1259 /* Release fields in submessage or submsg array */
1260 pb_size_t count = 1;
1261
1262 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1263 {
1264 field->pData = *(void**)field->pField;
1265 }
1266 else
1267 {
1268 field->pData = field->pField;
1269 }
1270
1271 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1272 {
1273 count = *(pb_size_t*)field->pSize;
1274
1275 if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > field->array_size)
1276 {
1277 /* Protect against corrupted _count fields */
1278 count = field->array_size;
1279 }
1280 }
1281
1282 if (field->pData)
1283 {
1284 for (; count > 0; count--)
1285 {
1286 pb_release(field->submsg_desc, field->pData);
1287 field->pData = (char*)field->pData + field->data_size;
1288 }
1289 }
1290 }
1291
1292 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1293 {
1294 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1295 (PB_LTYPE(type) == PB_LTYPE_STRING ||
1296 PB_LTYPE(type) == PB_LTYPE_BYTES))
1297 {
1298 /* Release entries in repeated string or bytes array */
1299 void **pItem = *(void***)field->pField;
1300 pb_size_t count = *(pb_size_t*)field->pSize;
1301 for (; count > 0; count--)
1302 {
1303 pb_free(*pItem);
1304 *pItem++ = NULL;
1305 }
1306 }
1307
1308 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1309 {
1310 /* We are going to release the array, so set the size to 0 */
1311 *(pb_size_t*)field->pSize = 0;
1312 }
1313
1314 /* Release main pointer */
1315 pb_free(*(void**)field->pField);
1316 *(void**)field->pField = NULL;
1317 }
1318}
1319
1320void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
1321{
1322 pb_field_iter_t iter;
1323
1324 if (!dest_struct)
1325 return; /* Ignore NULL pointers, similar to free() */
1326
1327 if (!pb_field_iter_begin(&iter, fields, dest_struct))
1328 return; /* Empty message type */
1329
1330 do
1331 {
1332 pb_release_single_field(&iter);
1333 } while (pb_field_iter_next(&iter));
1334}
1335#else
1336void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
1337{
1338 /* Nothing to release without PB_ENABLE_MALLOC. */
1339 PB_UNUSED(fields);
1340 PB_UNUSED(dest_struct);
1341}
1342#endif
1343
1344/* Field decoders */
1345
1346bool pb_decode_bool(pb_istream_t *stream, bool *dest)
1347{
1348 uint32_t value;
1349 if (!pb_decode_varint32(stream, &value))
1350 return false;
1351
1352 *(bool*)dest = (value != 0);
1353 return true;
1354}
1355
1356bool pb_decode_svarint(pb_istream_t *stream, pb_int64_t *dest)
1357{
1358 pb_uint64_t value;
1359 if (!pb_decode_varint(stream, &value))
1360 return false;
1361
1362 if (value & 1)
1363 *dest = (pb_int64_t)(~(value >> 1));
1364 else
1365 *dest = (pb_int64_t)(value >> 1);
1366
1367 return true;
1368}
1369
1370bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1371{
1372 union {
1373 uint32_t fixed32;
1374 pb_byte_t bytes[4];
1375 } u;
1376
1377 if (!pb_read(stream, u.bytes, 4))
1378 return false;
1379
1380#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1381 /* fast path - if we know that we're on little endian, assign directly */
1382 *(uint32_t*)dest = u.fixed32;
1383#else
1384 *(uint32_t*)dest = ((uint32_t)u.bytes[0] << 0) |
1385 ((uint32_t)u.bytes[1] << 8) |
1386 ((uint32_t)u.bytes[2] << 16) |
1387 ((uint32_t)u.bytes[3] << 24);
1388#endif
1389 return true;
1390}
1391
1392#ifndef PB_WITHOUT_64BIT
1393bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1394{
1395 union {
1396 uint64_t fixed64;
1397 pb_byte_t bytes[8];
1398 } u;
1399
1400 if (!pb_read(stream, u.bytes, 8))
1401 return false;
1402
1403#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1404 /* fast path - if we know that we're on little endian, assign directly */
1405 *(uint64_t*)dest = u.fixed64;
1406#else
1407 *(uint64_t*)dest = ((uint64_t)u.bytes[0] << 0) |
1408 ((uint64_t)u.bytes[1] << 8) |
1409 ((uint64_t)u.bytes[2] << 16) |
1410 ((uint64_t)u.bytes[3] << 24) |
1411 ((uint64_t)u.bytes[4] << 32) |
1412 ((uint64_t)u.bytes[5] << 40) |
1413 ((uint64_t)u.bytes[6] << 48) |
1414 ((uint64_t)u.bytes[7] << 56);
1415#endif
1416 return true;
1417}
1418#endif
1419
1420static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field)
1421{
1422 return pb_decode_bool(stream, (bool*)field->pData);
1423}
1424
1425static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field)
1426{
1427 if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT)
1428 {
1429 pb_uint64_t value, clamped;
1430 if (!pb_decode_varint(stream, &value))
1431 return false;
1432
1433 /* Cast to the proper field size, while checking for overflows */
1434 if (field->data_size == sizeof(pb_uint64_t))
1435 clamped = *(pb_uint64_t*)field->pData = value;
1436 else if (field->data_size == sizeof(uint32_t))
1437 clamped = *(uint32_t*)field->pData = (uint32_t)value;
1438 else if (field->data_size == sizeof(uint_least16_t))
1439 clamped = *(uint_least16_t*)field->pData = (uint_least16_t)value;
1440 else if (field->data_size == sizeof(uint_least8_t))
1441 clamped = *(uint_least8_t*)field->pData = (uint_least8_t)value;
1442 else
1443 PB_RETURN_ERROR(stream, "invalid data_size");
1444
1445 if (clamped != value)
1446 PB_RETURN_ERROR(stream, "integer too large");
1447
1448 return true;
1449 }
1450 else
1451 {
1452 pb_uint64_t value;
1453 pb_int64_t svalue;
1454 pb_int64_t clamped;
1455
1456 if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT)
1457 {
1458 if (!pb_decode_svarint(stream, &svalue))
1459 return false;
1460 }
1461 else
1462 {
1463 if (!pb_decode_varint(stream, &value))
1464 return false;
1465
1466 /* See issue 97: Google's C++ protobuf allows negative varint values to
1467 * be cast as int32_t, instead of the int64_t that should be used when
1468 * encoding. Nanopb versions before 0.2.5 had a bug in encoding. In order to
1469 * not break decoding of such messages, we cast <=32 bit fields to
1470 * int32_t first to get the sign correct.
1471 */
1472 if (field->data_size == sizeof(pb_int64_t))
1473 svalue = (pb_int64_t)value;
1474 else
1475 svalue = (int32_t)value;
1476 }
1477
1478 /* Cast to the proper field size, while checking for overflows */
1479 if (field->data_size == sizeof(pb_int64_t))
1480 clamped = *(pb_int64_t*)field->pData = svalue;
1481 else if (field->data_size == sizeof(int32_t))
1482 clamped = *(int32_t*)field->pData = (int32_t)svalue;
1483 else if (field->data_size == sizeof(int_least16_t))
1484 clamped = *(int_least16_t*)field->pData = (int_least16_t)svalue;
1485 else if (field->data_size == sizeof(int_least8_t))
1486 clamped = *(int_least8_t*)field->pData = (int_least8_t)svalue;
1487 else
1488 PB_RETURN_ERROR(stream, "invalid data_size");
1489
1490 if (clamped != svalue)
1491 PB_RETURN_ERROR(stream, "integer too large");
1492
1493 return true;
1494 }
1495}
1496
1497static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
1498{
1499 uint32_t size;
1500 size_t alloc_size;
1501 pb_bytes_array_t *dest;
1502
1503 if (!pb_decode_varint32(stream, &size))
1504 return false;
1505
1506 if (size > PB_SIZE_MAX)
1507 PB_RETURN_ERROR(stream, "bytes overflow");
1508
1509 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1510 if (size > alloc_size)
1511 PB_RETURN_ERROR(stream, "size too large");
1512
1513 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1514 {
1515#ifndef PB_ENABLE_MALLOC
1516 PB_RETURN_ERROR(stream, "no malloc support");
1517#else
1518 if (stream->bytes_left < size)
1519 PB_RETURN_ERROR(stream, "end-of-stream");
1520
1521 if (!allocate_field(stream, field->pData, alloc_size, 1))
1522 return false;
1523 dest = *(pb_bytes_array_t**)field->pData;
1524#endif
1525 }
1526 else
1527 {
1528 if (alloc_size > field->data_size)
1529 PB_RETURN_ERROR(stream, "bytes overflow");
1530 dest = (pb_bytes_array_t*)field->pData;
1531 }
1532
1533 dest->size = (pb_size_t)size;
1534 return pb_read(stream, dest->bytes, (size_t)size);
1535}
1536
1537static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field)
1538{
1539 uint32_t size;
1540 size_t alloc_size;
1541 pb_byte_t *dest = (pb_byte_t*)field->pData;
1542
1543 if (!pb_decode_varint32(stream, &size))
1544 return false;
1545
1546 if (size == (uint32_t)-1)
1547 PB_RETURN_ERROR(stream, "size too large");
1548
1549 /* Space for null terminator */
1550 alloc_size = (size_t)(size + 1);
1551
1552 if (alloc_size < size)
1553 PB_RETURN_ERROR(stream, "size too large");
1554
1555 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1556 {
1557#ifndef PB_ENABLE_MALLOC
1558 PB_RETURN_ERROR(stream, "no malloc support");
1559#else
1560 if (stream->bytes_left < size)
1561 PB_RETURN_ERROR(stream, "end-of-stream");
1562
1563 if (!allocate_field(stream, field->pData, alloc_size, 1))
1564 return false;
1565 dest = *(pb_byte_t**)field->pData;
1566#endif
1567 }
1568 else
1569 {
1570 if (alloc_size > field->data_size)
1571 PB_RETURN_ERROR(stream, "string overflow");
1572 }
1573
1574 dest[size] = 0;
1575
1576 if (!pb_read(stream, dest, (size_t)size))
1577 return false;
1578
1579#ifdef PB_VALIDATE_UTF8
1580 if (!pb_validate_utf8((const char*)dest))
1581 PB_RETURN_ERROR(stream, "invalid utf8");
1582#endif
1583
1584 return true;
1585}
1586
1587static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field)
1588{
1589 bool status = true;
1590 bool submsg_consumed = false;
1591 pb_istream_t substream;
1592
1593 if (!pb_make_string_substream(stream, &substream))
1594 return false;
1595
1596 if (field->submsg_desc == NULL)
1597 PB_RETURN_ERROR(stream, "invalid field descriptor");
1598
1599 /* Submessages can have a separate message-level callback that is called
1600 * before decoding the message. Typically it is used to set callback fields
1601 * inside oneofs. */
1602 if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL)
1603 {
1604 /* Message callback is stored right before pSize. */
1605 pb_callback_t *callback = (pb_callback_t*)field->pSize - 1;
1606 if (callback->funcs.decode)
1607 {
1608 status = callback->funcs.decode(&substream, field, &callback->arg);
1609
1610 if (substream.bytes_left == 0)
1611 {
1612 submsg_consumed = true;
1613 }
1614 }
1615 }
1616
1617 /* Now decode the submessage contents */
1618 if (status && !submsg_consumed)
1619 {
1620 unsigned int flags = 0;
1621
1622 /* Static required/optional fields are already initialized by top-level
1623 * pb_decode(), no need to initialize them again. */
1624 if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
1625 PB_HTYPE(field->type) != PB_HTYPE_REPEATED)
1626 {
1627 flags = PB_DECODE_NOINIT;
1628 }
1629
1630 status = pb_decode_inner(&substream, field->submsg_desc, field->pData, flags);
1631 }
1632
1633 if (!pb_close_string_substream(stream, &substream))
1634 return false;
1635
1636 return status;
1637}
1638
1639static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
1640{
1641 uint32_t size;
1642
1643 if (!pb_decode_varint32(stream, &size))
1644 return false;
1645
1646 if (size > PB_SIZE_MAX)
1647 PB_RETURN_ERROR(stream, "bytes overflow");
1648
1649 if (size == 0)
1650 {
1651 /* As a special case, treat empty bytes string as all zeros for fixed_length_bytes. */
1652 memset(field->pData, 0, (size_t)field->data_size);
1653 return true;
1654 }
1655
1656 if (size != field->data_size)
1657 PB_RETURN_ERROR(stream, "incorrect fixed length bytes size");
1658
1659 return pb_read(stream, (pb_byte_t*)field->pData, (size_t)field->data_size);
1660}
1661
1662#ifdef PB_CONVERT_DOUBLE_FLOAT
1663bool pb_decode_double_as_float(pb_istream_t *stream, float *dest)
1664{
1665 uint_least8_t sign;
1666 int exponent;
1667 uint32_t mantissa;
1668 uint64_t value;
1669 union { float f; uint32_t i; } out;
1670
1671 if (!pb_decode_fixed64(stream, &value))
1672 return false;
1673
1674 /* Decompose input value */
1675 sign = (uint_least8_t)((value >> 63) & 1);
1676 exponent = (int)((value >> 52) & 0x7FF) - 1023;
1677 mantissa = (value >> 28) & 0xFFFFFF; /* Highest 24 bits */
1678
1679 /* Figure if value is in range representable by floats. */
1680 if (exponent == 1024)
1681 {
1682 /* Special value */
1683 exponent = 128;
1684 mantissa >>= 1;
1685 }
1686 else
1687 {
1688 if (exponent > 127)
1689 {
1690 /* Too large, convert to infinity */
1691 exponent = 128;
1692 mantissa = 0;
1693 }
1694 else if (exponent < -150)
1695 {
1696 /* Too small, convert to zero */
1697 exponent = -127;
1698 mantissa = 0;
1699 }
1700 else if (exponent < -126)
1701 {
1702 /* Denormalized */
1703 mantissa |= 0x1000000;
1704 mantissa >>= (-126 - exponent);
1705 exponent = -127;
1706 }
1707
1708 /* Round off mantissa */
1709 mantissa = (mantissa + 1) >> 1;
1710
1711 /* Check if mantissa went over 2.0 */
1712 if (mantissa & 0x800000)
1713 {
1714 exponent += 1;
1715 mantissa &= 0x7FFFFF;
1716 mantissa >>= 1;
1717 }
1718 }
1719
1720 /* Combine fields */
1721 out.i = mantissa;
1722 out.i |= (uint32_t)(exponent + 127) << 23;
1723 out.i |= (uint32_t)sign << 31;
1724
1725 *dest = out.f;
1726 return true;
1727}
1728#endif
bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
MOSAPI int(2, 3) sprintf(char *__restrict str
#define pb_realloc(ptr, size)
#define pb_free(ptr)
#define uint64_t
#define PB_HTYPE(x)
Definition pb.h:298
#define PB_LTYPE_SUBMSG_W_CB
Definition pb.h:264
uint_least8_t pb_byte_t
Definition pb.h:227
#define PB_MAX_REQUIRED_FIELDS
Definition pb.h:204
#define PB_LTYPE_EXTENSION
Definition pb.h:268
#define PB_UNUSED(x)
Definition pb.h:143
#define PB_ATYPE_CALLBACK
Definition pb.h:294
#define PB_ATYPE_STATIC
Definition pb.h:292
uint_least16_t pb_size_t
Definition pb.h:310
#define PB_SET_ERROR(stream, msg)
Definition pb.h:890
#define PB_LTYPE_STRING
Definition pb.h:255
#define PB_ATYPE_POINTER
Definition pb.h:293
#define PB_LTYPE_BOOL
Definition pb.h:239
#define PB_BYTES_ARRAY_T_ALLOCSIZE(n)
Definition pb.h:377
#define PB_SIZE_MAX
Definition pb.h:313
#define PB_LTYPE_FIXED64
Definition pb.h:244
#define PB_HTYPE_REQUIRED
Definition pb.h:282
#define PB_LTYPE_LAST_PACKABLE
Definition pb.h:247
#define PB_LTYPE_SVARINT
Definition pb.h:242
#define PB_HTYPE_ONEOF
Definition pb.h:287
#define PB_ATYPE(x)
Definition pb.h:297
#define PB_HTYPE_OPTIONAL
Definition pb.h:283
#define PB_LTYPE_UVARINT
Definition pb.h:241
#define PB_LTYPE(x)
Definition pb.h:299
#define PB_HTYPE_REPEATED
Definition pb.h:285
#define PB_LTYPE_FIXED_LENGTH_BYTES
Definition pb.h:274
#define PB_LTYPE_VARINT
Definition pb.h:240
#define PB_LTYPE_BYTES
Definition pb.h:251
pb_wire_type_t
Definition pb.h:420
@ PB_WT_VARINT
Definition pb.h:421
@ PB_WT_32BIT
Definition pb.h:424
@ PB_WT_PACKED
Definition pb.h:425
@ PB_WT_STRING
Definition pb.h:423
@ PB_WT_64BIT
Definition pb.h:422
#define PB_RETURN_ERROR(stream, msg)
Definition pb.h:894
#define PB_LTYPE_IS_SUBMSG(x)
Definition pb.h:300
#define PB_LTYPE_FIXED32
Definition pb.h:243
#define PB_LTYPE_SUBMESSAGE
Definition pb.h:259
pb_byte_t pb_type_t
Definition pb.h:234
bool pb_field_iter_next(pb_field_iter_t *iter)
Definition pb_common.c:188
bool pb_field_iter_find_extension(pb_field_iter_t *iter)
Definition pb_common.c:246
bool pb_field_iter_begin_extension(pb_field_iter_t *iter, pb_extension_t *extension)
Definition pb_common.c:166
bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_msgdesc_t *desc, void *message)
Definition pb_common.c:156
bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag)
Definition pb_common.c:195
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
Definition pb_decode.c:82
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
Definition pb_decode.c:243
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
static bool decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
Definition pb_decode.c:467
static bool pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field)
Definition pb_decode.c:1425
static bool buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
Definition pb_decode.c:69
#define pb_uint64_t
Definition pb_decode.c:58
void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1336
static bool pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
Definition pb_decode.c:1497
static bool decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
Definition pb_decode.c:752
bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
Definition pb_decode.c:1370
#define pb_int64_t
Definition pb_decode.c:57
bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
Definition pb_decode.c:237
static bool pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field)
Definition pb_decode.c:1420
bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
Definition pb_decode.c:362
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
Definition pb_decode.c:308
static bool pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
Definition pb_decode.c:1639
static bool default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
Definition pb_decode.c:829
static bool pb_field_set_to_default(pb_field_iter_t *field)
Definition pb_decode.c:869
static bool decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
Definition pb_decode.c:798
bool pb_decode_bool(pb_istream_t *stream, bool *dest)
Definition pb_decode.c:1346
bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
Definition pb_decode.c:1154
static bool decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
Definition pb_decode.c:396
static bool pb_decode_varint32_eof(pb_istream_t *stream, uint32_t *dest, bool *eof)
Definition pb_decode.c:171
static bool decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
Definition pb_decode.c:624
static bool pb_message_set_to_defaults(pb_field_iter_t *iter)
Definition pb_decode.c:951
static bool pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
Definition pb_decode.c:125
bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
Definition pb_decode.c:377
#define checkreturn
Definition pb_decode.c:14
bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
Definition pb_decode.c:1393
static bool pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field)
Definition pb_decode.c:1587
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
Definition pb_decode.c:1356
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
Definition pb_decode.c:291
static bool pb_skip_varint(pb_istream_t *stream)
Definition pb_decode.c:266
static bool pb_skip_string(pb_istream_t *stream)
Definition pb_decode.c:277
static bool pb_decode_inner(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
Definition pb_decode.c:990
static bool pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field)
Definition pb_decode.c:1537
static bool decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension)
Definition pb_decode.c:846
static bool read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
Definition pb_decode.c:323
#define PB_ISTREAM_EMPTY
Definition pb_decode.h:49
#define PB_DECODE_DELIMITED
Definition pb_decode.h:100
#define PB_DECODE_NULLTERMINATED
Definition pb_decode.h:101
#define PB_DECODE_NOINIT
Definition pb_decode.h:99
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
int16_t int_least16_t
Definition pb_syshdr.h:33
uint32_t size_t
Definition pb_syshdr.h:42
unsigned int uint32_t
Definition pb_syshdr.h:24
uint16_t uint_least16_t
Definition pb_syshdr.h:34
static void * memset(void *s, int c, size_t n)
Definition pb_syshdr.h:101
unsigned long long uint64_t
Definition pb_syshdr.h:26
int8_t int_least8_t
Definition pb_syshdr.h:30
signed int int32_t
Definition pb_syshdr.h:23
uint8_t uint_least8_t
Definition pb_syshdr.h:31
uint8_t uint_fast8_t
Definition pb_syshdr.h:32
size_t size
Definition slab.c:30
uint32_t bitfield[(64+31)/32]
Definition pb_decode.c:62
static char buffer[2048]
Definition test_printf.c:7