1 /* pb_encode.c -- encode a protobuf using minimal resources
3 * 2011 Petteri Aimonen <jpa@kapsi.fi>
6 #define NANOPB_INTERNALS
12 /* Verify that we remember to check all return values for proper error propagation */
13 #define checkreturn __attribute__((warn_unused_result))
19 typedef bool (*pb_encoder_t)(pb_ostream_t *stream, const pb_field_t *field, const void *src) checkreturn;
21 /* --- Function pointers to field encoders ---
22 * Order in the array must match pb_action_t LTYPE numbering.
24 static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = {
35 /* pb_ostream_t implementation */
37 static bool checkreturn buf_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
39 uint8_t *dest = (uint8_t*)stream->state;
40 memcpy(dest, buf, count);
41 stream->state = dest + count;
45 pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize)
48 stream.callback = &buf_write;
50 stream.max_size = bufsize;
51 stream.bytes_written = 0;
55 bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
57 if (stream->callback != NULL)
59 if (stream->bytes_written + count > stream->max_size)
62 if (!stream->callback(stream, buf, count))
66 stream->bytes_written += count;
70 /* Main encoding stuff */
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.
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)
86 if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
88 if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
91 /* Determine the total size of packed array. */
92 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
96 else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
102 pb_ostream_t sizestream = {0,0,0,0};
104 for (i = 0; i < count; i++)
106 if (!func(&sizestream, field, p))
108 p = (const char*)p + field->data_size;
110 size = sizestream.bytes_written;
113 if (!pb_encode_varint(stream, (uint64_t)size))
116 if (stream->callback == NULL)
117 return pb_write(stream, NULL, size); /* Just sizing.. */
121 for (i = 0; i < count; i++)
123 if (!func(stream, field, p))
125 p = (const char*)p + field->data_size;
131 for (i = 0; i < count; i++)
133 if (!pb_encode_tag_for_field(stream, field))
135 if (!func(stream, field, p))
137 p = (const char*)p + field->data_size;
144 bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
146 const pb_field_t *field = fields;
147 const void *pData = src_struct;
149 size_t prev_size = 0;
151 while (field->tag != 0)
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;
157 prev_size = field->data_size;
158 if (PB_HTYPE(field->type) == PB_HTYPE_ARRAY)
159 prev_size *= field->array_size;
161 switch (PB_HTYPE(field->type))
163 case PB_HTYPE_REQUIRED:
164 if (!pb_encode_tag_for_field(stream, field))
166 if (!func(stream, field, pData))
170 case PB_HTYPE_OPTIONAL:
173 if (!pb_encode_tag_for_field(stream, field))
176 if (!func(stream, field, pData))
182 if (!encode_array(stream, field, pData, *(size_t*)pSize, func))
186 case PB_HTYPE_CALLBACK:
188 pb_callback_t *callback = (pb_callback_t*)pData;
189 if (callback->funcs.encode != NULL)
191 if (!callback->funcs.encode(stream, field, callback->arg))
204 /* Helper functions */
205 bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
211 return pb_write(stream, (uint8_t*)&value, 1);
215 buffer[i] = (uint8_t)((value & 0x7F) | 0x80);
219 buffer[i-1] &= 0x7F; /* Unset top bit on last byte */
221 return pb_write(stream, buffer, i);
224 bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
228 zigzagged = ~(value << 1);
230 zigzagged = value << 1;
232 return pb_encode_varint(stream, zigzagged);
235 bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
237 #ifdef __BIG_ENDIAN__
238 const uint8_t *bytes = value;
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);
246 return pb_write(stream, (uint8_t*)value, 4);
250 bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
252 #ifdef __BIG_ENDIAN__
253 const uint8_t *bytes = value;
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);
265 return pb_write(stream, (uint8_t*)value, 8);
269 bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, int field_number)
271 int tag = wiretype | (field_number << 3);
272 return pb_encode_varint(stream, (uint64_t)tag);
275 bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
277 pb_wire_type_t wiretype;
278 switch (PB_LTYPE(field->type))
280 case PB_LTYPE_VARINT:
281 case PB_LTYPE_SVARINT:
282 wiretype = PB_WT_VARINT;
285 case PB_LTYPE_FIXED32:
286 wiretype = PB_WT_32BIT;
289 case PB_LTYPE_FIXED64:
290 wiretype = PB_WT_64BIT;
294 case PB_LTYPE_STRING:
295 case PB_LTYPE_SUBMESSAGE:
296 wiretype = PB_WT_STRING;
303 return pb_encode_tag(stream, wiretype, field->tag);
306 bool checkreturn pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, size_t size)
308 if (!pb_encode_varint(stream, (uint64_t)size))
311 return pb_write(stream, buffer, size);
314 bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
316 /* First calculate the message size using a non-writing substream. */
317 pb_ostream_t substream = {0,0,0,0};
321 if (!pb_encode(&substream, fields, src_struct))
324 size = substream.bytes_written;
326 if (!pb_encode_varint(stream, (uint64_t)size))
329 if (stream->callback == NULL)
330 return pb_write(stream, NULL, size); /* Just sizing */
332 if (stream->bytes_written + size > stream->max_size)
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;
342 status = pb_encode(&substream, fields, src_struct);
344 stream->bytes_written += substream.bytes_written;
345 stream->state = substream.state;
347 if (substream.bytes_written != size)
355 bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
359 switch (field->data_size)
361 case 1: value = *(uint8_t*)src; break;
362 case 2: value = *(uint16_t*)src; break;
363 case 4: value = *(uint32_t*)src; break;
364 case 8: value = *(uint64_t*)src; break;
365 default: return false;
368 return pb_encode_varint(stream, value);
371 bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
375 switch (field->data_size)
377 case 4: value = *(int32_t*)src; break;
378 case 8: value = *(int64_t*)src; break;
379 default: return false;
382 return pb_encode_svarint(stream, value);
385 bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
388 return pb_encode_fixed64(stream, src);
391 bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
394 return pb_encode_fixed32(stream, src);
397 bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
399 pb_bytes_array_t *bytes = (pb_bytes_array_t*)src;
401 return pb_encode_string(stream, bytes->bytes, bytes->size);
404 bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
407 return pb_encode_string(stream, (uint8_t*)src, strlen((char*)src));
410 bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
412 if (field->ptr == NULL)
415 return pb_encode_submessage(stream, (pb_field_t*)field->ptr, src);