Add compile-time option PB_BUFFER_ONLY.
[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 pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
157 {
158     const pb_field_t *field = fields;
159     const void *pData = src_struct;
160     const void *pSize;
161     size_t prev_size = 0;
162     
163     while (field->tag != 0)
164     {
165         pb_encoder_t func = PB_ENCODERS[PB_LTYPE(field->type)];
166         pData = (const char*)pData + prev_size + field->data_offset;
167         pSize = (const char*)pData + field->size_offset;
168         
169         prev_size = field->data_size;
170         if (PB_HTYPE(field->type) == PB_HTYPE_ARRAY)
171             prev_size *= field->array_size;
172                 
173         switch (PB_HTYPE(field->type))
174         {
175             case PB_HTYPE_REQUIRED:
176                 if (!pb_encode_tag_for_field(stream, field))
177                     return false;
178                 if (!func(stream, field, pData))
179                     return false;
180                 break;
181             
182             case PB_HTYPE_OPTIONAL:
183                 if (*(const bool*)pSize)
184                 {
185                     if (!pb_encode_tag_for_field(stream, field))
186                         return false;
187                 
188                     if (!func(stream, field, pData))
189                         return false;
190                 }
191                 break;
192             
193             case PB_HTYPE_ARRAY:
194                 if (!encode_array(stream, field, pData, *(const size_t*)pSize, func))
195                     return false;
196                 break;
197             
198             case PB_HTYPE_CALLBACK:
199             {
200                 const pb_callback_t *callback = (const pb_callback_t*)pData;
201                 if (callback->funcs.encode != NULL)
202                 {
203                     if (!callback->funcs.encode(stream, field, callback->arg))
204                         return false;
205                 }
206                 break;
207             }
208         }
209     
210         field++;
211     }
212     
213     return true;
214 }
215
216 /* Helper functions */
217 bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
218 {
219     uint8_t buffer[10];
220     size_t i = 0;
221     
222     if (value == 0)
223         return pb_write(stream, (uint8_t*)&value, 1);
224     
225     while (value)
226     {
227         buffer[i] = (uint8_t)((value & 0x7F) | 0x80);
228         value >>= 7;
229         i++;
230     }
231     buffer[i-1] &= 0x7F; /* Unset top bit on last byte */
232     
233     return pb_write(stream, buffer, i);
234 }
235
236 bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
237 {
238     uint64_t zigzagged;
239     if (value < 0)
240         zigzagged = (uint64_t)(~(value << 1));
241     else
242         zigzagged = (uint64_t)(value << 1);
243     
244     return pb_encode_varint(stream, zigzagged);
245 }
246
247 bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
248 {
249     #ifdef __BIG_ENDIAN__
250     const uint8_t *bytes = value;
251     uint8_t lebytes[4];
252     lebytes[0] = bytes[3];
253     lebytes[1] = bytes[2];
254     lebytes[2] = bytes[1];
255     lebytes[3] = bytes[0];
256     return pb_write(stream, lebytes, 4);
257     #else
258     return pb_write(stream, (const uint8_t*)value, 4);
259     #endif
260 }
261
262 bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
263 {
264     #ifdef __BIG_ENDIAN__
265     const uint8_t *bytes = value;
266     uint8_t lebytes[8];
267     lebytes[0] = bytes[7];
268     lebytes[1] = bytes[6];
269     lebytes[2] = bytes[5];
270     lebytes[3] = bytes[4];
271     lebytes[4] = bytes[3];
272     lebytes[5] = bytes[2];
273     lebytes[6] = bytes[1];
274     lebytes[7] = bytes[0];
275     return pb_write(stream, lebytes, 8);
276     #else
277     return pb_write(stream, (const uint8_t*)value, 8);
278     #endif
279 }
280
281 bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
282 {
283     uint64_t tag = wiretype | (field_number << 3);
284     return pb_encode_varint(stream, tag);
285 }
286
287 bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
288 {
289     pb_wire_type_t wiretype;
290     switch (PB_LTYPE(field->type))
291     {
292         case PB_LTYPE_VARINT:
293         case PB_LTYPE_SVARINT:
294             wiretype = PB_WT_VARINT;
295             break;
296         
297         case PB_LTYPE_FIXED32:
298             wiretype = PB_WT_32BIT;
299             break;
300         
301         case PB_LTYPE_FIXED64:
302             wiretype = PB_WT_64BIT;
303             break;
304         
305         case PB_LTYPE_BYTES:
306         case PB_LTYPE_STRING:
307         case PB_LTYPE_SUBMESSAGE:
308             wiretype = PB_WT_STRING;
309             break;
310         
311         default:
312             return false;
313     }
314     
315     return pb_encode_tag(stream, wiretype, field->tag);
316 }
317
318 bool checkreturn pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, size_t size)
319 {
320     if (!pb_encode_varint(stream, (uint64_t)size))
321         return false;
322     
323     return pb_write(stream, buffer, size);
324 }
325
326 bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
327 {
328     /* First calculate the message size using a non-writing substream. */
329     pb_ostream_t substream = {0,0,0,0};
330     size_t size;
331     bool status;
332     
333     if (!pb_encode(&substream, fields, src_struct))
334         return false;
335     
336     size = substream.bytes_written;
337     
338     if (!pb_encode_varint(stream, (uint64_t)size))
339         return false;
340     
341     if (stream->callback == NULL)
342         return pb_write(stream, NULL, size); /* Just sizing */
343     
344     if (stream->bytes_written + size > stream->max_size)
345         return false;
346         
347     /* Use a substream to verify that a callback doesn't write more than
348      * what it did the first time. */
349     substream.callback = stream->callback;
350     substream.state = stream->state;
351     substream.max_size = size;
352     substream.bytes_written = 0;
353     
354     status = pb_encode(&substream, fields, src_struct);
355     
356     stream->bytes_written += substream.bytes_written;
357     stream->state = substream.state;
358     
359     if (substream.bytes_written != size)
360         return false;
361     
362     return status;
363 }
364
365 /* Field encoders */
366
367 bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
368 {
369     uint64_t value = 0;
370     
371     switch (field->data_size)
372     {
373         case 1: value = *(const uint8_t*)src; break;
374         case 2: value = *(const uint16_t*)src; break;
375         case 4: value = *(const uint32_t*)src; break;
376         case 8: value = *(const uint64_t*)src; break;
377         default: return false;
378     }
379     
380     return pb_encode_varint(stream, value);
381 }
382
383 bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
384 {
385     int64_t value = 0;
386     
387     switch (field->data_size)
388     {
389         case 4: value = *(const int32_t*)src; break;
390         case 8: value = *(const int64_t*)src; break;
391         default: return false;
392     }
393     
394     return pb_encode_svarint(stream, value);
395 }
396
397 bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
398 {
399     UNUSED(field);
400     return pb_encode_fixed64(stream, src);
401 }
402
403 bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
404 {
405     UNUSED(field);
406     return pb_encode_fixed32(stream, src);
407 }
408
409 bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
410 {
411     const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
412     UNUSED(field);
413     return pb_encode_string(stream, bytes->bytes, bytes->size);
414 }
415
416 bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
417 {
418     UNUSED(field);
419     return pb_encode_string(stream, (const uint8_t*)src, strlen((const char*)src));
420 }
421
422 bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
423 {
424     if (field->ptr == NULL)
425         return false;
426     
427     return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
428 }
429