Fix warnings with -Wcast-qual. Add test for C++ compile.
[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     memcpy(dest, buf, count);
41     stream->state = dest + count;
42     return true;
43 }
44
45 pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize)
46 {
47     pb_ostream_t stream;
48     stream.callback = &buf_write;
49     stream.state = buf;
50     stream.max_size = bufsize;
51     stream.bytes_written = 0;
52     return stream;
53 }
54
55 bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
56 {
57     if (stream->callback != NULL)
58     {
59         if (stream->bytes_written + count > stream->max_size)
60             return false;
61         
62         if (!stream->callback(stream, buf, count))
63             return false;
64     }
65     
66     stream->bytes_written += count;
67     return true;
68 }
69
70 /* Main encoding stuff */
71
72 /* Callbacks don't need this function because they usually know the data type
73  * without examining the field structure.
74  * Therefore it is static for now.
75  */
76 static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
77                          const void *pData, size_t count, pb_encoder_t func)
78 {
79     size_t i;
80     const void *p;
81     size_t size;
82     
83     if (count == 0)
84         return true;
85     
86     if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
87     {
88         if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
89             return false;
90         
91         /* Determine the total size of packed array. */
92         if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
93         {
94             size = 4 * count;
95         }
96         else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
97         {
98             size = 8 * count;
99         }
100         else
101         {
102             pb_ostream_t sizestream = {0,0,0,0};
103             p = pData;
104             for (i = 0; i < count; i++)
105             {
106                 if (!func(&sizestream, field, p))
107                     return false;
108                 p = (const char*)p + field->data_size;
109             }
110             size = sizestream.bytes_written;
111         }
112         
113         if (!pb_encode_varint(stream, (uint64_t)size))
114             return false;
115         
116         if (stream->callback == NULL)
117             return pb_write(stream, NULL, size); /* Just sizing.. */
118         
119         /* Write the data */
120         p = pData;
121         for (i = 0; i < count; i++)
122         {
123             if (!func(stream, field, p))
124                 return false;
125             p = (const char*)p + field->data_size;
126         }
127     }
128     else
129     {
130         p = pData;
131         for (i = 0; i < count; i++)
132         {
133             if (!pb_encode_tag_for_field(stream, field))
134                 return false;
135             if (!func(stream, field, p))
136                 return false;
137             p = (const char*)p + field->data_size;
138         }
139     }
140     
141     return true;
142 }
143
144 bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
145 {
146     const pb_field_t *field = fields;
147     const void *pData = src_struct;
148     const void *pSize;
149     size_t prev_size = 0;
150     
151     while (field->tag != 0)
152     {
153         pb_encoder_t func = PB_ENCODERS[PB_LTYPE(field->type)];
154         pData = (const char*)pData + prev_size + field->data_offset;
155         pSize = (const char*)pData + field->size_offset;
156         
157         prev_size = field->data_size;
158         if (PB_HTYPE(field->type) == PB_HTYPE_ARRAY)
159             prev_size *= field->array_size;
160                 
161         switch (PB_HTYPE(field->type))
162         {
163             case PB_HTYPE_REQUIRED:
164                 if (!pb_encode_tag_for_field(stream, field))
165                     return false;
166                 if (!func(stream, field, pData))
167                     return false;
168                 break;
169             
170             case PB_HTYPE_OPTIONAL:
171                 if (*(const bool*)pSize)
172                 {
173                     if (!pb_encode_tag_for_field(stream, field))
174                         return false;
175                 
176                     if (!func(stream, field, pData))
177                         return false;
178                 }
179                 break;
180             
181             case PB_HTYPE_ARRAY:
182                 if (!encode_array(stream, field, pData, *(const size_t*)pSize, func))
183                     return false;
184                 break;
185             
186             case PB_HTYPE_CALLBACK:
187             {
188                 const pb_callback_t *callback = (const pb_callback_t*)pData;
189                 if (callback->funcs.encode != NULL)
190                 {
191                     if (!callback->funcs.encode(stream, field, callback->arg))
192                         return false;
193                 }
194                 break;
195             }
196         }
197     
198         field++;
199     }
200     
201     return true;
202 }
203
204 /* Helper functions */
205 bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
206 {
207     uint8_t buffer[10];
208     int i = 0;
209     
210     if (value == 0)
211         return pb_write(stream, (uint8_t*)&value, 1);
212     
213     while (value)
214     {
215         buffer[i] = (uint8_t)((value & 0x7F) | 0x80);
216         value >>= 7;
217         i++;
218     }
219     buffer[i-1] &= 0x7F; /* Unset top bit on last byte */
220     
221     return pb_write(stream, buffer, i);
222 }
223
224 bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
225 {
226     uint64_t zigzagged;
227     if (value < 0)
228         zigzagged = ~(value << 1);
229     else
230         zigzagged = value << 1;
231     
232     return pb_encode_varint(stream, zigzagged);
233 }
234
235 bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
236 {
237     #ifdef __BIG_ENDIAN__
238     const uint8_t *bytes = value;
239     uint8_t lebytes[4];
240     lebytes[0] = bytes[3];
241     lebytes[1] = bytes[2];
242     lebytes[2] = bytes[1];
243     lebytes[3] = bytes[0];
244     return pb_write(stream, lebytes, 4);
245     #else
246     return pb_write(stream, (const uint8_t*)value, 4);
247     #endif
248 }
249
250 bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
251 {
252     #ifdef __BIG_ENDIAN__
253     const uint8_t *bytes = value;
254     uint8_t lebytes[8];
255     lebytes[0] = bytes[7];
256     lebytes[1] = bytes[6];
257     lebytes[2] = bytes[5];
258     lebytes[3] = bytes[4];
259     lebytes[4] = bytes[3];
260     lebytes[5] = bytes[2];
261     lebytes[6] = bytes[1];
262     lebytes[7] = bytes[0];
263     return pb_write(stream, lebytes, 8);
264     #else
265     return pb_write(stream, (const uint8_t*)value, 8);
266     #endif
267 }
268
269 bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, int field_number)
270 {
271     int tag = wiretype | (field_number << 3);
272     return pb_encode_varint(stream, (uint64_t)tag);
273 }
274
275 bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
276 {
277     pb_wire_type_t wiretype;
278     switch (PB_LTYPE(field->type))
279     {
280         case PB_LTYPE_VARINT:
281         case PB_LTYPE_SVARINT:
282             wiretype = PB_WT_VARINT;
283             break;
284         
285         case PB_LTYPE_FIXED32:
286             wiretype = PB_WT_32BIT;
287             break;
288         
289         case PB_LTYPE_FIXED64:
290             wiretype = PB_WT_64BIT;
291             break;
292         
293         case PB_LTYPE_BYTES:
294         case PB_LTYPE_STRING:
295         case PB_LTYPE_SUBMESSAGE:
296             wiretype = PB_WT_STRING;
297             break;
298         
299         default:
300             return false;
301     }
302     
303     return pb_encode_tag(stream, wiretype, field->tag);
304 }
305
306 bool checkreturn pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, size_t size)
307 {
308     if (!pb_encode_varint(stream, (uint64_t)size))
309         return false;
310     
311     return pb_write(stream, buffer, size);
312 }
313
314 bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
315 {
316     /* First calculate the message size using a non-writing substream. */
317     pb_ostream_t substream = {0,0,0,0};
318     size_t size;
319     bool status;
320     
321     if (!pb_encode(&substream, fields, src_struct))
322         return false;
323     
324     size = substream.bytes_written;
325     
326     if (!pb_encode_varint(stream, (uint64_t)size))
327         return false;
328     
329     if (stream->callback == NULL)
330         return pb_write(stream, NULL, size); /* Just sizing */
331     
332     if (stream->bytes_written + size > stream->max_size)
333         return false;
334         
335     /* Use a substream to verify that a callback doesn't write more than
336      * what it did the first time. */
337     substream.callback = stream->callback;
338     substream.state = stream->state;
339     substream.max_size = size;
340     substream.bytes_written = 0;
341     
342     status = pb_encode(&substream, fields, src_struct);
343     
344     stream->bytes_written += substream.bytes_written;
345     stream->state = substream.state;
346     
347     if (substream.bytes_written != size)
348         return false;
349     
350     return status;
351 }
352
353 /* Field encoders */
354
355 bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
356 {
357     uint64_t value = 0;
358     
359     switch (field->data_size)
360     {
361         case 1: value = *(const uint8_t*)src; break;
362         case 2: value = *(const uint16_t*)src; break;
363         case 4: value = *(const uint32_t*)src; break;
364         case 8: value = *(const uint64_t*)src; break;
365         default: return false;
366     }
367     
368     return pb_encode_varint(stream, value);
369 }
370
371 bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
372 {
373     uint64_t value = 0;
374     
375     switch (field->data_size)
376     {
377         case 4: value = *(const int32_t*)src; break;
378         case 8: value = *(const int64_t*)src; break;
379         default: return false;
380     }
381     
382     return pb_encode_svarint(stream, value);
383 }
384
385 bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
386 {
387     UNUSED(field);
388     return pb_encode_fixed64(stream, src);
389 }
390
391 bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
392 {
393     UNUSED(field);
394     return pb_encode_fixed32(stream, src);
395 }
396
397 bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
398 {
399     const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
400     UNUSED(field);
401     return pb_encode_string(stream, bytes->bytes, bytes->size);
402 }
403
404 bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
405 {
406     UNUSED(field);
407     return pb_encode_string(stream, (const uint8_t*)src, strlen((const char*)src));
408 }
409
410 bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
411 {
412     if (field->ptr == NULL)
413         return false;
414     
415     return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
416 }
417