Cleanup of comments.
[apps/low-level-can-service.git] / pb_decode.c
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 and gcc before 3.4.0 just
8  * ignore the annotation.
9  */
10 #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
11     #define checkreturn
12 #else
13     #define checkreturn __attribute__((warn_unused_result))
14 #endif
15
16 #define NANOPB_INTERNALS
17 #include "pb.h"
18 #include "pb_decode.h"
19
20 /* --- Function pointers to field decoders ---
21  * Order in the array must match pb_action_t LTYPE numbering.
22  */
23 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
24 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
25     &pb_dec_varint,
26     &pb_dec_svarint,
27     &pb_dec_fixed32,
28     &pb_dec_fixed64,
29     
30     &pb_dec_bytes,
31     &pb_dec_string,
32     &pb_dec_submessage,
33     NULL /* extensions */
34 };
35
36 /*******************************
37  * pb_istream_t implementation *
38  *******************************/
39
40 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
41 {
42     uint8_t *source = (uint8_t*)stream->state;
43     stream->state = source + count;
44     
45     if (buf != NULL)
46     {
47         while (count--)
48             *buf++ = *source++;
49     }
50     
51     return true;
52 }
53
54 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
55 {
56 #ifndef PB_BUFFER_ONLY
57         if (buf == NULL && stream->callback != buf_read)
58         {
59                 /* Skip input bytes */
60                 uint8_t tmp[16];
61                 while (count > 16)
62                 {
63                         if (!pb_read(stream, tmp, 16))
64                                 return false;
65                         
66                         count -= 16;
67                 }
68                 
69                 return pb_read(stream, tmp, count);
70         }
71 #endif
72
73     if (stream->bytes_left < count)
74         PB_RETURN_ERROR(stream, "end-of-stream");
75     
76 #ifndef PB_BUFFER_ONLY
77     if (!stream->callback(stream, buf, count))
78         PB_RETURN_ERROR(stream, "io error");
79 #else
80     if (!buf_read(stream, buf, count))
81         return false;
82 #endif
83     
84     stream->bytes_left -= count;
85     return true;
86 }
87
88 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
89 {
90     pb_istream_t stream;
91 #ifdef PB_BUFFER_ONLY
92     stream.callback = NULL;
93 #else
94     stream.callback = &buf_read;
95 #endif
96     stream.state = buf;
97     stream.bytes_left = bufsize;
98 #ifndef PB_NO_ERRMSG
99     stream.errmsg = NULL;
100 #endif
101     return stream;
102 }
103
104 /********************
105  * Helper functions *
106  ********************/
107
108 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
109 {
110     uint8_t byte;
111     uint32_t result;
112     
113     if (!pb_read(stream, &byte, 1))
114         return false;
115     
116     if (!(byte & 0x80))
117     {
118         /* Quick case, 1 byte value */
119         result = byte;
120     }
121     else
122     {
123         /* Multibyte case */
124         uint8_t bitpos = 7;
125         result = byte & 0x7F;
126         
127         do
128         {
129             if (bitpos >= 32)
130                 PB_RETURN_ERROR(stream, "varint overflow");
131             
132             if (!pb_read(stream, &byte, 1))
133                 return false;
134             
135             result |= (uint32_t)(byte & 0x7F) << bitpos;
136             bitpos = (uint8_t)(bitpos + 7);
137         } while (byte & 0x80);
138    }
139    
140    *dest = result;
141    return true;
142 }
143
144 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
145 {
146     uint8_t byte;
147     uint8_t bitpos = 0;
148     uint64_t result = 0;
149     
150     do
151     {
152         if (bitpos >= 64)
153             PB_RETURN_ERROR(stream, "varint overflow");
154         
155         if (!pb_read(stream, &byte, 1))
156             return false;
157
158         result |= (uint64_t)(byte & 0x7F) << bitpos;
159         bitpos = (uint8_t)(bitpos + 7);
160     } while (byte & 0x80);
161     
162     *dest = result;
163     return true;
164 }
165
166 bool checkreturn pb_skip_varint(pb_istream_t *stream)
167 {
168     uint8_t byte;
169     do
170     {
171         if (!pb_read(stream, &byte, 1))
172             return false;
173     } while (byte & 0x80);
174     return true;
175 }
176
177 bool checkreturn pb_skip_string(pb_istream_t *stream)
178 {
179     uint32_t length;
180     if (!pb_decode_varint32(stream, &length))
181         return false;
182     
183     return pb_read(stream, NULL, length);
184 }
185
186 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
187 {
188     uint32_t temp;
189     *eof = false;
190     *wire_type = (pb_wire_type_t) 0;
191     *tag = 0;
192     
193     if (!pb_decode_varint32(stream, &temp))
194     {
195         if (stream->bytes_left == 0)
196             *eof = true;
197
198         return false;
199     }
200     
201     if (temp == 0)
202     {
203         *eof = true; /* Special feature: allow 0-terminated messages. */
204         return false;
205     }
206     
207     *tag = temp >> 3;
208     *wire_type = (pb_wire_type_t)(temp & 7);
209     return true;
210 }
211
212 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
213 {
214     switch (wire_type)
215     {
216         case PB_WT_VARINT: return pb_skip_varint(stream);
217         case PB_WT_64BIT: return pb_read(stream, NULL, 8);
218         case PB_WT_STRING: return pb_skip_string(stream);
219         case PB_WT_32BIT: return pb_read(stream, NULL, 4);
220         default: PB_RETURN_ERROR(stream, "invalid wire_type");
221     }
222 }
223
224 /* Read a raw value to buffer, for the purpose of passing it to callback as
225  * a substream. Size is maximum size on call, and actual size on return.
226  */
227 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
228 {
229     size_t max_size = *size;
230     switch (wire_type)
231     {
232         case PB_WT_VARINT:
233             *size = 0;
234             do
235             {
236                 (*size)++;
237                 if (*size > max_size) return false;
238                 if (!pb_read(stream, buf, 1)) return false;
239             } while (*buf++ & 0x80);
240             return true;
241             
242         case PB_WT_64BIT:
243             *size = 8;
244             return pb_read(stream, buf, 8);
245         
246         case PB_WT_32BIT:
247             *size = 4;
248             return pb_read(stream, buf, 4);
249         
250         default: PB_RETURN_ERROR(stream, "invalid wire_type");
251     }
252 }
253
254 /* Decode string length from stream and return a substream with limited length.
255  * Remember to close the substream using pb_close_string_substream().
256  */
257 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
258 {
259     uint32_t size;
260     if (!pb_decode_varint32(stream, &size))
261         return false;
262     
263     *substream = *stream;
264     if (substream->bytes_left < size)
265         PB_RETURN_ERROR(stream, "parent stream too short");
266     
267     substream->bytes_left = size;
268     stream->bytes_left -= size;
269     return true;
270 }
271
272 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
273 {
274     stream->state = substream->state;
275
276 #ifndef PB_NO_ERRMSG
277     stream->errmsg = substream->errmsg;
278 #endif
279 }
280
281 /* Iterator for pb_field_t list */
282 typedef struct {
283     const pb_field_t *start; /* Start of the pb_field_t array */
284     const pb_field_t *pos; /* Current position of the iterator */
285     unsigned field_index; /* Zero-based index of the field. */
286     unsigned required_field_index; /* Zero-based index that counts only the required fields */
287     void *dest_struct; /* Pointer to the destination structure to decode to */
288     void *pData; /* Pointer where to store current field value */
289     void *pSize; /* Pointer where to store the size of current array field */
290 } pb_field_iterator_t;
291
292 static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct)
293 {
294     iter->start = iter->pos = fields;
295     iter->field_index = 0;
296     iter->required_field_index = 0;
297     iter->pData = (char*)dest_struct + iter->pos->data_offset;
298     iter->pSize = (char*)iter->pData + iter->pos->size_offset;
299     iter->dest_struct = dest_struct;
300 }
301
302 static bool pb_field_next(pb_field_iterator_t *iter)
303 {
304     bool notwrapped = true;
305     size_t prev_size = iter->pos->data_size;
306     
307     if (PB_ATYPE(iter->pos->type) == PB_ATYPE_STATIC &&
308         PB_HTYPE(iter->pos->type) == PB_HTYPE_REPEATED)
309     {
310         prev_size *= iter->pos->array_size;
311     }
312     
313     if (iter->pos->tag == 0)
314         return false; /* Only happens with empty message types */
315     
316     if (PB_HTYPE(iter->pos->type) == PB_HTYPE_REQUIRED)
317         iter->required_field_index++;
318     
319     iter->pos++;
320     iter->field_index++;
321     if (iter->pos->tag == 0)
322     {
323         iter->pos = iter->start;
324         iter->field_index = 0;
325         iter->required_field_index = 0;
326         iter->pData = iter->dest_struct;
327         prev_size = 0;
328         notwrapped = false;
329     }
330     
331     iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset;
332     iter->pSize = (char*)iter->pData + iter->pos->size_offset;
333     return notwrapped;
334 }
335
336 static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t tag)
337 {
338     unsigned start = iter->field_index;
339     
340     do {
341         if (iter->pos->tag == tag &&
342             PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION)
343         {
344             return true;
345         }
346         pb_field_next(iter);
347     } while (iter->field_index != start);
348     
349     return false;
350 }
351
352 /*************************
353  * Decode a single field *
354  *************************/
355
356 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
357 {
358     pb_type_t type;
359     pb_decoder_t func;
360     
361     type = iter->pos->type;
362     func = PB_DECODERS[PB_LTYPE(type)];
363
364     switch (PB_HTYPE(type))
365     {
366         case PB_HTYPE_REQUIRED:
367             return func(stream, iter->pos, iter->pData);
368             
369         case PB_HTYPE_OPTIONAL:
370             *(bool*)iter->pSize = true;
371             return func(stream, iter->pos, iter->pData);
372     
373         case PB_HTYPE_REPEATED:
374             if (wire_type == PB_WT_STRING
375                 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
376             {
377                 /* Packed array */
378                 bool status = true;
379                 size_t *size = (size_t*)iter->pSize;
380                 pb_istream_t substream;
381                 if (!pb_make_string_substream(stream, &substream))
382                     return false;
383                 
384                 while (substream.bytes_left && *size < iter->pos->array_size)
385                 {
386                     void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
387                     if (!func(&substream, iter->pos, pItem))
388                     {
389                         status = false;
390                         break;
391                     }
392                     (*size)++;
393                 }
394                 pb_close_string_substream(stream, &substream);
395                 
396                 if (substream.bytes_left != 0)
397                     PB_RETURN_ERROR(stream, "array overflow");
398                 
399                 return status;
400             }
401             else
402             {
403                 /* Repeated field */
404                 size_t *size = (size_t*)iter->pSize;
405                 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
406                 if (*size >= iter->pos->array_size)
407                     PB_RETURN_ERROR(stream, "array overflow");
408                 
409                 (*size)++;
410                 return func(stream, iter->pos, pItem);
411             }
412
413         default:
414             PB_RETURN_ERROR(stream, "invalid field type");
415     }
416 }
417
418 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
419 {
420     pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
421     
422 #ifdef PB_OLD_CALLBACK_STYLE
423     void *arg = pCallback->arg;
424 #else
425     void **arg = &(pCallback->arg);
426 #endif
427     
428     if (pCallback->funcs.decode == NULL)
429         return pb_skip_field(stream, wire_type);
430     
431     if (wire_type == PB_WT_STRING)
432     {
433         pb_istream_t substream;
434         
435         if (!pb_make_string_substream(stream, &substream))
436             return false;
437         
438         do
439         {
440             if (!pCallback->funcs.decode(&substream, iter->pos, arg))
441                 PB_RETURN_ERROR(stream, "callback failed");
442         } while (substream.bytes_left);
443         
444         pb_close_string_substream(stream, &substream);
445         return true;
446     }
447     else
448     {
449         /* Copy the single scalar value to stack.
450          * This is required so that we can limit the stream length,
451          * which in turn allows to use same callback for packed and
452          * not-packed fields. */
453         pb_istream_t substream;
454         uint8_t buffer[10];
455         size_t size = sizeof(buffer);
456         
457         if (!read_raw_value(stream, wire_type, buffer, &size))
458             return false;
459         substream = pb_istream_from_buffer(buffer, size);
460         
461         return pCallback->funcs.decode(&substream, iter->pos, arg);
462     }
463 }
464
465 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
466 {
467     switch (PB_ATYPE(iter->pos->type))
468     {
469         case PB_ATYPE_STATIC:
470             return decode_static_field(stream, wire_type, iter);
471         
472         case PB_ATYPE_CALLBACK:
473             return decode_callback_field(stream, wire_type, iter);
474         
475         default:
476             PB_RETURN_ERROR(stream, "invalid field type");
477     }
478 }
479
480 /* Default handler for extension fields. Expects a pb_field_t structure
481  * in extension->type->arg. */
482 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
483     pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
484 {
485     const pb_field_t *field = (const pb_field_t*)extension->type->arg;
486     pb_field_iterator_t iter;
487     bool dummy;
488     
489     if (field->tag != tag)
490         return true;
491     
492     iter.start = field;
493     iter.pos = field;
494     iter.field_index = 0;
495     iter.required_field_index = 0;
496     iter.dest_struct = extension->dest;
497     iter.pData = extension->dest;
498     iter.pSize = &dummy;
499     
500     return decode_field(stream, wire_type, &iter);
501 }
502
503 /* Try to decode an unknown field as an extension field. Tries each extension
504  * decoder in turn, until one of them handles the field or loop ends. */
505 static bool checkreturn decode_extension(pb_istream_t *stream,
506     uint32_t tag, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
507 {
508     pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
509     size_t pos = stream->bytes_left;
510     
511     while (extension && pos == stream->bytes_left)
512     {
513         bool status;
514         if (extension->type->decode)
515             status = extension->type->decode(stream, extension, tag, wire_type);
516         else
517             status = default_extension_decoder(stream, extension, tag, wire_type);
518
519         if (!status)
520             return false;
521         
522         extension = extension->next;
523     }
524     
525     return true;
526 }
527
528 /* Step through the iterator until an extension field is found or until all
529  * entries have been checked. There can be only one extension field per
530  * message. Returns false if no extension field is found. */
531 static bool checkreturn find_extension_field(pb_field_iterator_t *iter)
532 {
533     unsigned start = iter->field_index;
534     
535     do {
536         if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
537             return true;
538         pb_field_next(iter);
539     } while (iter->field_index != start);
540     
541     return false;
542 }
543
544 /* Initialize message fields to default values, recursively */
545 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
546 {
547     pb_field_iterator_t iter;
548     pb_field_init(&iter, fields, dest_struct);
549     
550     /* Initialize size/has fields and apply default values */
551     do
552     {
553         pb_type_t type;
554         type = iter.pos->type;
555     
556         if (iter.pos->tag == 0)
557             continue;
558         
559         if (PB_ATYPE(type) == PB_ATYPE_STATIC)
560         {
561             /* Initialize the size field for optional/repeated fields to 0. */
562             if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
563             {
564                 *(bool*)iter.pSize = false;
565             }
566             else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
567             {
568                 *(size_t*)iter.pSize = 0;
569                 continue; /* Array is empty, no need to initialize contents */
570             }
571             
572             /* Initialize field contents to default value */
573             if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
574             {
575                 pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
576             }
577             else if (iter.pos->ptr != NULL)
578             {
579                 memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
580             }
581             else
582             {
583                 memset(iter.pData, 0, iter.pos->data_size);
584             }
585         }
586         else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
587         {
588             continue; /* Don't overwrite callback */
589         }
590     } while (pb_field_next(&iter));
591 }
592
593 /*********************
594  * Decode all fields *
595  *********************/
596
597 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
598 {
599     uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0}; /* Used to check for required fields */
600     uint32_t extension_range_start = 0;
601     pb_field_iterator_t iter;
602     
603     pb_field_init(&iter, fields, dest_struct);
604     
605     while (stream->bytes_left)
606     {
607         uint32_t tag;
608         pb_wire_type_t wire_type;
609         bool eof;
610         
611         if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
612         {
613             if (eof)
614                 break;
615             else
616                 return false;
617         }
618         
619         if (!pb_field_find(&iter, tag))
620         {
621             /* No match found, check if it matches an extension. */
622             if (tag >= extension_range_start)
623             {
624                 if (!find_extension_field(&iter))
625                     extension_range_start = (uint32_t)-1;
626                 else
627                     extension_range_start = iter.pos->tag;
628                 
629                 if (tag >= extension_range_start)
630                 {
631                     size_t pos = stream->bytes_left;
632                 
633                     if (!decode_extension(stream, tag, wire_type, &iter))
634                         return false;
635                     
636                     if (pos != stream->bytes_left)
637                     {
638                         /* The field was handled */
639                         continue;                    
640                     }
641                 }
642             }
643         
644             /* No match found, skip data */
645             if (!pb_skip_field(stream, wire_type))
646                 return false;
647             continue;
648         }
649         
650         if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
651             && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
652         {
653             fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
654         }
655             
656         if (!decode_field(stream, wire_type, &iter))
657             return false;
658     }
659     
660     /* Check that all required fields were present. */
661     {
662         /* First figure out the number of required fields by
663          * seeking to the end of the field array. Usually we
664          * are already close to end after decoding.
665          */
666         unsigned req_field_count;
667         pb_type_t last_type;
668         unsigned i;
669         do {
670             req_field_count = iter.required_field_index;
671             last_type = iter.pos->type;
672         } while (pb_field_next(&iter));
673         
674         /* Fixup if last field was also required. */
675         if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag)
676             req_field_count++;
677         
678         /* Check the whole bytes */
679         for (i = 0; i < (req_field_count >> 3); i++)
680         {
681             if (fields_seen[i] != 0xFF)
682                 PB_RETURN_ERROR(stream, "missing required field");
683         }
684         
685         /* Check the remaining bits */
686         if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
687             PB_RETURN_ERROR(stream, "missing required field");
688     }
689     
690     return true;
691 }
692
693 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
694 {
695     pb_message_set_to_defaults(fields, dest_struct);
696     return pb_decode_noinit(stream, fields, dest_struct);
697 }
698
699 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
700 {
701     pb_istream_t substream;
702     bool status;
703     
704     if (!pb_make_string_substream(stream, &substream))
705         return false;
706     
707     status = pb_decode(&substream, fields, dest_struct);
708     pb_close_string_substream(stream, &substream);
709     return status;
710 }
711
712 /* Field decoders */
713
714 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
715 {
716     uint64_t value;
717     if (!pb_decode_varint(stream, &value))
718         return false;
719     
720     if (value & 1)
721         *dest = (int64_t)(~(value >> 1));
722     else
723         *dest = (int64_t)(value >> 1);
724     
725     return true;
726 }
727
728 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
729 {
730     #ifdef __BIG_ENDIAN__
731     uint8_t *bytes = (uint8_t*)dest;
732     uint8_t lebytes[4];
733     
734     if (!pb_read(stream, lebytes, 4))
735         return false;
736     
737     bytes[0] = lebytes[3];
738     bytes[1] = lebytes[2];
739     bytes[2] = lebytes[1];
740     bytes[3] = lebytes[0];
741     return true;
742     #else
743     return pb_read(stream, (uint8_t*)dest, 4);
744     #endif   
745 }
746
747 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
748 {
749     #ifdef __BIG_ENDIAN__
750     uint8_t *bytes = (uint8_t*)dest;
751     uint8_t lebytes[8];
752     
753     if (!pb_read(stream, lebytes, 8))
754         return false;
755     
756     bytes[0] = lebytes[7];
757     bytes[1] = lebytes[6];
758     bytes[2] = lebytes[5];
759     bytes[3] = lebytes[4];
760     bytes[4] = lebytes[3];
761     bytes[5] = lebytes[2];
762     bytes[6] = lebytes[1];
763     bytes[7] = lebytes[0];
764     return true;
765     #else
766     return pb_read(stream, (uint8_t*)dest, 8);
767     #endif   
768 }
769
770 bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
771 {
772     uint64_t value;
773     if (!pb_decode_varint(stream, &value))
774         return false;
775     
776     switch (field->data_size)
777     {
778         case 1: *(uint8_t*)dest = (uint8_t)value; break;
779         case 2: *(uint16_t*)dest = (uint16_t)value; break;
780         case 4: *(uint32_t*)dest = (uint32_t)value; break;
781         case 8: *(uint64_t*)dest = value; break;
782         default: PB_RETURN_ERROR(stream, "invalid data_size");
783     }
784     
785     return true;
786 }
787
788 bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
789 {
790     int64_t value;
791     if (!pb_decode_svarint(stream, &value))
792         return false;
793     
794     switch (field->data_size)
795     {
796         case 4: *(int32_t*)dest = (int32_t)value; break;
797         case 8: *(int64_t*)dest = value; break;
798         default: PB_RETURN_ERROR(stream, "invalid data_size");
799     }
800     
801     return true;
802 }
803
804 bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
805 {
806     UNUSED(field);
807     return pb_decode_fixed32(stream, dest);
808 }
809
810 bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
811 {
812     UNUSED(field);
813     return pb_decode_fixed64(stream, dest);
814 }
815
816 bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
817 {
818     pb_bytes_array_t *x = (pb_bytes_array_t*)dest;
819     
820     uint32_t temp;
821     if (!pb_decode_varint32(stream, &temp))
822         return false;
823     x->size = temp;
824     
825     /* Check length, noting the space taken by the size_t header. */
826     if (x->size > field->data_size - offsetof(pb_bytes_array_t, bytes))
827         PB_RETURN_ERROR(stream, "bytes overflow");
828     
829     return pb_read(stream, x->bytes, x->size);
830 }
831
832 bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
833 {
834     uint32_t size;
835     bool status;
836     if (!pb_decode_varint32(stream, &size))
837         return false;
838     
839     /* Check length, noting the null terminator */
840     if (size + 1 > field->data_size)
841         PB_RETURN_ERROR(stream, "string overflow");
842     
843     status = pb_read(stream, (uint8_t*)dest, size);
844     *((uint8_t*)dest + size) = 0;
845     return status;
846 }
847
848 bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
849 {
850     bool status;
851     pb_istream_t substream;
852     const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
853     
854     if (!pb_make_string_substream(stream, &substream))
855         return false;
856     
857     if (field->ptr == NULL)
858         PB_RETURN_ERROR(stream, "invalid field descriptor");
859     
860     /* New array entries need to be initialized, while required and optional
861      * submessages have already been initialized in the top-level pb_decode. */
862     if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
863         status = pb_decode(&substream, submsg_fields, dest);
864     else
865         status = pb_decode_noinit(&substream, submsg_fields, dest);
866     
867     pb_close_string_substream(stream, &substream);
868     return status;
869 }