Move STATIC_ASSERTs to .pb.c file.
[apps/agl-service-can-low-level.git] / pb_encode.c
1 /* pb_encode.c -- encode a protobuf using minimal resources
2  *
3  * 2011 Petteri Aimonen <jpa@kapsi.fi>
4  */
5
6 #define NANOPB_INTERNALS
7 #include "pb.h"
8 #include "pb_encode.h"
9 #include <string.h>
10
11 /* The warn_unused_result attribute appeared first in gcc-3.4.0 */
12 #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
13     #define checkreturn
14 #else
15     /* Verify that we remember to check all return values for proper error propagation */
16     #define checkreturn __attribute__((warn_unused_result))
17 #endif
18
19 typedef bool (*pb_encoder_t)(pb_ostream_t *stream, const pb_field_t *field, const void *src) checkreturn;
20
21 /* --- Function pointers to field encoders ---
22  * Order in the array must match pb_action_t LTYPE numbering.
23  */
24 static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = {
25     &pb_enc_varint,
26     &pb_enc_svarint,
27     &pb_enc_fixed32,
28     &pb_enc_fixed64,
29     
30     &pb_enc_bytes,
31     &pb_enc_string,
32     &pb_enc_submessage
33 };
34
35 /* pb_ostream_t implementation */
36
37 static bool checkreturn buf_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
38 {
39     uint8_t *dest = (uint8_t*)stream->state;
40     stream->state = dest + count;
41     
42     while (count--)
43         *dest++ = *buf++;
44     
45     return true;
46 }
47
48 pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize)
49 {
50     pb_ostream_t stream;
51 #ifdef PB_BUFFER_ONLY
52     stream.callback = (void*)1; /* Just some marker value */
53 #else
54     stream.callback = &buf_write;
55 #endif
56     stream.state = buf;
57     stream.max_size = bufsize;
58     stream.bytes_written = 0;
59     return stream;
60 }
61
62 bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
63 {
64     if (stream->callback != NULL)
65     {
66         if (stream->bytes_written + count > stream->max_size)
67             return false;
68
69 #ifdef PB_BUFFER_ONLY
70         if (!buf_write(stream, buf, count))
71             return false;
72 #else        
73         if (!stream->callback(stream, buf, count))
74             return false;
75 #endif
76     }
77     
78     stream->bytes_written += count;
79     return true;
80 }
81
82 /* Main encoding stuff */
83
84 /* Callbacks don't need this function because they usually know the data type
85  * without examining the field structure.
86  * Therefore it is static for now.
87  */
88 static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
89                          const void *pData, size_t count, pb_encoder_t func)
90 {
91     size_t i;
92     const void *p;
93     size_t size;
94     
95     if (count == 0)
96         return true;
97     
98     if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
99     {
100         if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
101             return false;
102         
103         /* Determine the total size of packed array. */
104         if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
105         {
106             size = 4 * count;
107         }
108         else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
109         {
110             size = 8 * count;
111         }
112         else
113         {
114             pb_ostream_t sizestream = {0,0,0,0};
115             p = pData;
116             for (i = 0; i < count; i++)
117             {
118                 if (!func(&sizestream, field, p))
119                     return false;
120                 p = (const char*)p + field->data_size;
121             }
122             size = sizestream.bytes_written;
123         }
124         
125         if (!pb_encode_varint(stream, (uint64_t)size))
126             return false;
127         
128         if (stream->callback == NULL)
129             return pb_write(stream, NULL, size); /* Just sizing.. */
130         
131         /* Write the data */
132         p = pData;
133         for (i = 0; i < count; i++)
134         {
135             if (!func(stream, field, p))
136                 return false;
137             p = (const char*)p + field->data_size;
138         }
139     }
140     else
141     {
142         p = pData;
143         for (i = 0; i < count; i++)
144         {
145             if (!pb_encode_tag_for_field(stream, field))
146                 return false;
147             if (!func(stream, field, p))
148                 return false;
149             p = (const char*)p + field->data_size;
150         }
151     }
152     
153     return true;
154 }
155
156 bool checkreturn encode_static_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData)
157 {
158     pb_encoder_t func;
159     const void *pSize;
160     
161     func = PB_ENCODERS[PB_LTYPE(field->type)];
162     pSize = (const char*)pData + field->size_offset;
163     
164     switch (PB_HTYPE(field->type))
165     {
166         case PB_HTYPE_REQUIRED:
167             if (!pb_encode_tag_for_field(stream, field))
168                 return false;
169             if (!func(stream, field, pData))
170                 return false;
171             break;
172         
173         case PB_HTYPE_OPTIONAL:
174             if (*(const bool*)pSize)
175             {
176                 if (!pb_encode_tag_for_field(stream, field))
177                     return false;
178             
179                 if (!func(stream, field, pData))
180                     return false;
181             }
182             break;
183         
184         case PB_HTYPE_REPEATED:
185             if (!encode_array(stream, field, pData, *(const size_t*)pSize, func))
186                 return false;
187             break;
188         
189         default:
190             return false;
191     }
192     
193     return true;
194 }
195
196 bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData)
197 {
198     const pb_callback_t *callback = (const pb_callback_t*)pData;
199     if (callback->funcs.encode != NULL)
200     {
201         if (!callback->funcs.encode(stream, field, callback->arg))
202             return false;
203     }
204     return true;
205 }
206
207 bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
208 {
209     const pb_field_t *field = fields;
210     const void *pData = src_struct;
211     size_t prev_size = 0;
212     
213     while (field->tag != 0)
214     {
215         pData = (const char*)pData + prev_size + field->data_offset;
216         prev_size = field->data_size;
217         
218         /* Special case for static arrays */
219         if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
220             PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
221         {
222             prev_size *= field->array_size;
223         }
224                 
225         switch (PB_ATYPE(field->type))
226         {
227             case PB_ATYPE_STATIC:
228                 if (!encode_static_field(stream, field, pData))
229                     return false;
230                 break;
231             
232             case PB_ATYPE_CALLBACK:
233                 if (!encode_callback_field(stream, field, pData))
234                     return false;
235                 break;
236             
237             default:
238                 return false;
239         }
240     
241         field++;
242     }
243     
244     return true;
245 }
246
247 /* Helper functions */
248 bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
249 {
250     uint8_t buffer[10];
251     size_t i = 0;
252     
253     if (value == 0)
254         return pb_write(stream, (uint8_t*)&value, 1);
255     
256     while (value)
257     {
258         buffer[i] = (uint8_t)((value & 0x7F) | 0x80);
259         value >>= 7;
260         i++;
261     }
262     buffer[i-1] &= 0x7F; /* Unset top bit on last byte */
263     
264     return pb_write(stream, buffer, i);
265 }
266
267 bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
268 {
269     uint64_t zigzagged;
270     if (value < 0)
271         zigzagged = (uint64_t)(~(value << 1));
272     else
273         zigzagged = (uint64_t)(value << 1);
274     
275     return pb_encode_varint(stream, zigzagged);
276 }
277
278 bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
279 {
280     #ifdef __BIG_ENDIAN__
281     const uint8_t *bytes = value;
282     uint8_t lebytes[4];
283     lebytes[0] = bytes[3];
284     lebytes[1] = bytes[2];
285     lebytes[2] = bytes[1];
286     lebytes[3] = bytes[0];
287     return pb_write(stream, lebytes, 4);
288     #else
289     return pb_write(stream, (const uint8_t*)value, 4);
290     #endif
291 }
292
293 bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
294 {
295     #ifdef __BIG_ENDIAN__
296     const uint8_t *bytes = value;
297     uint8_t lebytes[8];
298     lebytes[0] = bytes[7];
299     lebytes[1] = bytes[6];
300     lebytes[2] = bytes[5];
301     lebytes[3] = bytes[4];
302     lebytes[4] = bytes[3];
303     lebytes[5] = bytes[2];
304     lebytes[6] = bytes[1];
305     lebytes[7] = bytes[0];
306     return pb_write(stream, lebytes, 8);
307     #else
308     return pb_write(stream, (const uint8_t*)value, 8);
309     #endif
310 }
311
312 bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
313 {
314     uint64_t tag = wiretype | (field_number << 3);
315     return pb_encode_varint(stream, tag);
316 }
317
318 bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
319 {
320     pb_wire_type_t wiretype;
321     switch (PB_LTYPE(field->type))
322     {
323         case PB_LTYPE_VARINT:
324         case PB_LTYPE_SVARINT:
325             wiretype = PB_WT_VARINT;
326             break;
327         
328         case PB_LTYPE_FIXED32:
329             wiretype = PB_WT_32BIT;
330             break;
331         
332         case PB_LTYPE_FIXED64:
333             wiretype = PB_WT_64BIT;
334             break;
335         
336         case PB_LTYPE_BYTES:
337         case PB_LTYPE_STRING:
338         case PB_LTYPE_SUBMESSAGE:
339             wiretype = PB_WT_STRING;
340             break;
341         
342         default:
343             return false;
344     }
345     
346     return pb_encode_tag(stream, wiretype, field->tag);
347 }
348
349 bool checkreturn pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, size_t size)
350 {
351     if (!pb_encode_varint(stream, (uint64_t)size))
352         return false;
353     
354     return pb_write(stream, buffer, size);
355 }
356
357 bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
358 {
359     /* First calculate the message size using a non-writing substream. */
360     pb_ostream_t substream = {0,0,0,0};
361     size_t size;
362     bool status;
363     
364     if (!pb_encode(&substream, fields, src_struct))
365         return false;
366     
367     size = substream.bytes_written;
368     
369     if (!pb_encode_varint(stream, (uint64_t)size))
370         return false;
371     
372     if (stream->callback == NULL)
373         return pb_write(stream, NULL, size); /* Just sizing */
374     
375     if (stream->bytes_written + size > stream->max_size)
376         return false;
377         
378     /* Use a substream to verify that a callback doesn't write more than
379      * what it did the first time. */
380     substream.callback = stream->callback;
381     substream.state = stream->state;
382     substream.max_size = size;
383     substream.bytes_written = 0;
384     
385     status = pb_encode(&substream, fields, src_struct);
386     
387     stream->bytes_written += substream.bytes_written;
388     stream->state = substream.state;
389     
390     if (substream.bytes_written != size)
391         return false;
392     
393     return status;
394 }
395
396 /* Field encoders */
397
398 bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
399 {
400     uint64_t value = 0;
401     
402     switch (field->data_size)
403     {
404         case 1: value = *(const uint8_t*)src; break;
405         case 2: value = *(const uint16_t*)src; break;
406         case 4: value = *(const uint32_t*)src; break;
407         case 8: value = *(const uint64_t*)src; break;
408         default: return false;
409     }
410     
411     return pb_encode_varint(stream, value);
412 }
413
414 bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
415 {
416     int64_t value = 0;
417     
418     switch (field->data_size)
419     {
420         case 4: value = *(const int32_t*)src; break;
421         case 8: value = *(const int64_t*)src; break;
422         default: return false;
423     }
424     
425     return pb_encode_svarint(stream, value);
426 }
427
428 bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
429 {
430     UNUSED(field);
431     return pb_encode_fixed64(stream, src);
432 }
433
434 bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
435 {
436     UNUSED(field);
437     return pb_encode_fixed32(stream, src);
438 }
439
440 bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
441 {
442     const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
443     UNUSED(field);
444     return pb_encode_string(stream, bytes->bytes, bytes->size);
445 }
446
447 bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
448 {
449     UNUSED(field);
450     return pb_encode_string(stream, (const uint8_t*)src, strlen((const char*)src));
451 }
452
453 bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
454 {
455     if (field->ptr == NULL)
456         return false;
457     
458     return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
459 }
460