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