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