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