1 /* pb_encode.c -- encode a protobuf using minimal resources
3 * 2011 Petteri Aimonen <jpa@kapsi.fi>
10 /* Use the GCC warn_unused_result attribute to check that all return values
11 * are propagated correctly. On other compilers and gcc before 3.4.0 just
12 * ignore the annotation.
14 #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
17 #define checkreturn __attribute__((warn_unused_result))
20 /**************************************
21 * Declarations internal to this file *
22 **************************************/
23 typedef bool (*pb_encoder_t)(pb_ostream_t *stream, const pb_field_t *field, const void *src) checkreturn;
25 static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
26 static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field, const void *pData, size_t count, pb_encoder_t func);
27 static bool checkreturn encode_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData);
28 static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension);
29 static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData);
30 static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
31 static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
32 static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
33 static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src);
34 static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src);
35 static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src);
36 static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src);
37 static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src);
38 static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src);
40 /* --- Function pointers to field encoders ---
41 * Order in the array must match pb_action_t LTYPE numbering.
43 static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = {
53 NULL, /* extensions */
54 &pb_enc_fixed_length_bytes
57 /*******************************
58 * pb_ostream_t implementation *
59 *******************************/
61 static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
64 pb_byte_t *dest = (pb_byte_t*)stream->state;
65 stream->state = dest + count;
67 for (i = 0; i < count; i++)
73 pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
77 stream.callback = (void*)1; /* Just a marker value */
79 stream.callback = &buf_write;
82 stream.max_size = bufsize;
83 stream.bytes_written = 0;
90 bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
92 if (stream->callback != NULL)
94 if (stream->bytes_written + count > stream->max_size)
95 PB_RETURN_ERROR(stream, "stream full");
98 if (!buf_write(stream, buf, count))
99 PB_RETURN_ERROR(stream, "io error");
101 if (!stream->callback(stream, buf, count))
102 PB_RETURN_ERROR(stream, "io error");
106 stream->bytes_written += count;
110 /*************************
111 * Encode a single field *
112 *************************/
114 /* Encode a static array. Handles the size calculations and possible packing. */
115 static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
116 const void *pData, size_t count, pb_encoder_t func)
125 if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
126 PB_RETURN_ERROR(stream, "array max size exceeded");
128 /* We always pack arrays if the datatype allows it. */
129 if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
131 if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
134 /* Determine the total size of packed array. */
135 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
139 else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
145 pb_ostream_t sizestream = PB_OSTREAM_SIZING;
147 for (i = 0; i < count; i++)
149 if (!func(&sizestream, field, p))
151 p = (const char*)p + field->data_size;
153 size = sizestream.bytes_written;
156 if (!pb_encode_varint(stream, (uint64_t)size))
159 if (stream->callback == NULL)
160 return pb_write(stream, NULL, size); /* Just sizing.. */
164 for (i = 0; i < count; i++)
166 if (!func(stream, field, p))
168 p = (const char*)p + field->data_size;
174 for (i = 0; i < count; i++)
176 if (!pb_encode_tag_for_field(stream, field))
179 /* Normally the data is stored directly in the array entries, but
180 * for pointer-type string and bytes fields, the array entries are
181 * actually pointers themselves also. So we have to dereference once
182 * more to get to the actual data. */
183 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
184 (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
185 PB_LTYPE(field->type) == PB_LTYPE_BYTES))
187 if (!func(stream, field, *(const void* const*)p))
192 if (!func(stream, field, p))
195 p = (const char*)p + field->data_size;
202 /* In proto3, all fields are optional and are only encoded if their value is "non-zero".
203 * This function implements the check for the zero value. */
204 static bool pb_check_proto3_default_value(const pb_field_t *field, const void *pData)
206 if(PB_LTYPE(field->type) == PB_LTYPE_BYTES)
208 const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)pData;
209 return bytes->size == 0;
211 else if (PB_LTYPE(field->type) == PB_LTYPE_STRING)
213 return *(const char*)pData == '\0';
215 else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES)
217 /* Fixed length bytes is only empty if its length is fixed
218 * as 0. Which would be pretty strange, but we can check
220 return field->data_size == 0;
224 /* PB_LTYPE_VARINT, UVARINT, SVARINT, FIXED32, FIXED64,
225 * SUBMESSAGE, EXTENSION: These all have integer or pointer
226 * value which can be compared with 0. This does the check
227 * byte-by-byte to avoid the switch-cast logic used in
228 * pb_enc_varint(). (Casting to char* is safe with regards
229 * to C strict aliasing rules.)
232 const char *p = (const char*)pData;
233 for (i = 0; i < field->data_size; i++)
245 /* Encode a field with static or pointer allocation, i.e. one whose data
246 * is available to the encoder directly. */
247 static bool checkreturn encode_basic_field(pb_ostream_t *stream,
248 const pb_field_t *field, const void *pData)
252 const void *pSize = &implicit_has;
254 func = PB_ENCODERS[PB_LTYPE(field->type)];
256 if (field->size_offset)
258 /* Static optional, repeated or oneof field */
259 pSize = (const char*)pData + field->size_offset;
261 else if (PB_HTYPE(field->type) == PB_HTYPE_OPTIONAL)
263 /* Proto3 style field, optional but without explicit has_ field. */
264 implicit_has = !pb_check_proto3_default_value(field, pData);
268 /* Required field, always present */
272 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
274 /* pData is a pointer to the field, which contains pointer to
275 * the data. If the 2nd pointer is NULL, it is interpreted as if
276 * the has_field was false.
278 pData = *(const void* const*)pData;
279 implicit_has = (pData != NULL);
282 switch (PB_HTYPE(field->type))
284 case PB_HTYPE_REQUIRED:
286 PB_RETURN_ERROR(stream, "missing required field");
287 if (!pb_encode_tag_for_field(stream, field))
289 if (!func(stream, field, pData))
293 case PB_HTYPE_OPTIONAL:
294 if (*(const bool*)pSize)
296 if (!pb_encode_tag_for_field(stream, field))
299 if (!func(stream, field, pData))
304 case PB_HTYPE_REPEATED:
305 if (!encode_array(stream, field, pData, *(const pb_size_t*)pSize, func))
310 if (*(const pb_size_t*)pSize == field->tag)
312 if (!pb_encode_tag_for_field(stream, field))
315 if (!func(stream, field, pData))
321 PB_RETURN_ERROR(stream, "invalid field type");
327 /* Encode a field with callback semantics. This means that a user function is
328 * called to provide and encode the actual data. */
329 static bool checkreturn encode_callback_field(pb_ostream_t *stream,
330 const pb_field_t *field, const void *pData)
332 const pb_callback_t *callback = (const pb_callback_t*)pData;
334 #ifdef PB_OLD_CALLBACK_STYLE
335 const void *arg = callback->arg;
337 void * const *arg = &(callback->arg);
340 if (callback->funcs.encode != NULL)
342 if (!callback->funcs.encode(stream, field, arg))
343 PB_RETURN_ERROR(stream, "callback error");
348 /* Encode a single field of any callback or static type. */
349 static bool checkreturn encode_field(pb_ostream_t *stream,
350 const pb_field_t *field, const void *pData)
352 switch (PB_ATYPE(field->type))
354 case PB_ATYPE_STATIC:
355 case PB_ATYPE_POINTER:
356 return encode_basic_field(stream, field, pData);
358 case PB_ATYPE_CALLBACK:
359 return encode_callback_field(stream, field, pData);
362 PB_RETURN_ERROR(stream, "invalid field type");
366 /* Default handler for extension fields. Expects to have a pb_field_t
367 * pointer in the extension->type->arg field. */
368 static bool checkreturn default_extension_encoder(pb_ostream_t *stream,
369 const pb_extension_t *extension)
371 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
373 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
375 /* For pointer extensions, the pointer is stored directly
376 * in the extension structure. This avoids having an extra
378 return encode_field(stream, field, &extension->dest);
382 return encode_field(stream, field, extension->dest);
386 /* Walk through all the registered extensions and give them a chance
387 * to encode themselves. */
388 static bool checkreturn encode_extension_field(pb_ostream_t *stream,
389 const pb_field_t *field, const void *pData)
391 const pb_extension_t *extension = *(const pb_extension_t* const *)pData;
397 if (extension->type->encode)
398 status = extension->type->encode(stream, extension);
400 status = default_extension_encoder(stream, extension);
405 extension = extension->next;
411 /*********************
412 * Encode all fields *
413 *********************/
415 static void *remove_const(const void *p)
417 /* Note: this casts away const, in order to use the common field iterator
418 * logic for both encoding and decoding. */
427 bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
429 pb_field_iter_t iter;
430 if (!pb_field_iter_begin(&iter, fields, remove_const(src_struct)))
431 return true; /* Empty message type */
434 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_EXTENSION)
436 /* Special case for the extension field placeholder */
437 if (!encode_extension_field(stream, iter.pos, iter.pData))
443 if (!encode_field(stream, iter.pos, iter.pData))
446 } while (pb_field_iter_next(&iter));
451 bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
453 return pb_encode_submessage(stream, fields, src_struct);
456 bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct)
458 pb_ostream_t stream = PB_OSTREAM_SIZING;
460 if (!pb_encode(&stream, fields, src_struct))
463 *size = stream.bytes_written;
467 /********************
469 ********************/
470 bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
472 pb_byte_t buffer[10];
477 pb_byte_t v = (pb_byte_t)value;
478 return pb_write(stream, &v, 1);
483 buffer[i] = (pb_byte_t)((value & 0x7F) | 0x80);
487 buffer[i-1] &= 0x7F; /* Unset top bit on last byte */
489 return pb_write(stream, buffer, i);
492 bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
496 zigzagged = ~((uint64_t)value << 1);
498 zigzagged = (uint64_t)value << 1;
500 return pb_encode_varint(stream, zigzagged);
503 bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
505 uint32_t val = *(const uint32_t*)value;
507 bytes[0] = (pb_byte_t)(val & 0xFF);
508 bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
509 bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
510 bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
511 return pb_write(stream, bytes, 4);
514 bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
516 uint64_t val = *(const uint64_t*)value;
518 bytes[0] = (pb_byte_t)(val & 0xFF);
519 bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
520 bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
521 bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
522 bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
523 bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
524 bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
525 bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
526 return pb_write(stream, bytes, 8);
529 bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
531 uint64_t tag = ((uint64_t)field_number << 3) | wiretype;
532 return pb_encode_varint(stream, tag);
535 bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
537 pb_wire_type_t wiretype;
538 switch (PB_LTYPE(field->type))
540 case PB_LTYPE_VARINT:
541 case PB_LTYPE_UVARINT:
542 case PB_LTYPE_SVARINT:
543 wiretype = PB_WT_VARINT;
546 case PB_LTYPE_FIXED32:
547 wiretype = PB_WT_32BIT;
550 case PB_LTYPE_FIXED64:
551 wiretype = PB_WT_64BIT;
555 case PB_LTYPE_STRING:
556 case PB_LTYPE_SUBMESSAGE:
557 case PB_LTYPE_FIXED_LENGTH_BYTES:
558 wiretype = PB_WT_STRING;
562 PB_RETURN_ERROR(stream, "invalid field type");
565 return pb_encode_tag(stream, wiretype, field->tag);
568 bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
570 if (!pb_encode_varint(stream, (uint64_t)size))
573 return pb_write(stream, buffer, size);
576 bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
578 /* First calculate the message size using a non-writing substream. */
579 pb_ostream_t substream = PB_OSTREAM_SIZING;
583 if (!pb_encode(&substream, fields, src_struct))
586 stream->errmsg = substream.errmsg;
591 size = substream.bytes_written;
593 if (!pb_encode_varint(stream, (uint64_t)size))
596 if (stream->callback == NULL)
597 return pb_write(stream, NULL, size); /* Just sizing */
599 if (stream->bytes_written + size > stream->max_size)
600 PB_RETURN_ERROR(stream, "stream full");
602 /* Use a substream to verify that a callback doesn't write more than
603 * what it did the first time. */
604 substream.callback = stream->callback;
605 substream.state = stream->state;
606 substream.max_size = size;
607 substream.bytes_written = 0;
609 substream.errmsg = NULL;
612 status = pb_encode(&substream, fields, src_struct);
614 stream->bytes_written += substream.bytes_written;
615 stream->state = substream.state;
617 stream->errmsg = substream.errmsg;
620 if (substream.bytes_written != size)
621 PB_RETURN_ERROR(stream, "submsg size changed");
628 static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
632 if (field->data_size == sizeof(int_least8_t))
633 value = *(const int_least8_t*)src;
634 else if (field->data_size == sizeof(int_least16_t))
635 value = *(const int_least16_t*)src;
636 else if (field->data_size == sizeof(int32_t))
637 value = *(const int32_t*)src;
638 else if (field->data_size == sizeof(int64_t))
639 value = *(const int64_t*)src;
641 PB_RETURN_ERROR(stream, "invalid data_size");
643 return pb_encode_varint(stream, (uint64_t)value);
646 static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
650 if (field->data_size == sizeof(uint_least8_t))
651 value = *(const uint_least8_t*)src;
652 else if (field->data_size == sizeof(uint_least16_t))
653 value = *(const uint_least16_t*)src;
654 else if (field->data_size == sizeof(uint32_t))
655 value = *(const uint32_t*)src;
656 else if (field->data_size == sizeof(uint64_t))
657 value = *(const uint64_t*)src;
659 PB_RETURN_ERROR(stream, "invalid data_size");
661 return pb_encode_varint(stream, value);
664 static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
668 if (field->data_size == sizeof(int_least8_t))
669 value = *(const int_least8_t*)src;
670 else if (field->data_size == sizeof(int_least16_t))
671 value = *(const int_least16_t*)src;
672 else if (field->data_size == sizeof(int32_t))
673 value = *(const int32_t*)src;
674 else if (field->data_size == sizeof(int64_t))
675 value = *(const int64_t*)src;
677 PB_RETURN_ERROR(stream, "invalid data_size");
679 return pb_encode_svarint(stream, value);
682 static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
685 return pb_encode_fixed64(stream, src);
688 static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
691 return pb_encode_fixed32(stream, src);
694 static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
696 const pb_bytes_array_t *bytes = NULL;
698 bytes = (const pb_bytes_array_t*)src;
702 /* Treat null pointer as an empty bytes field */
703 return pb_encode_string(stream, NULL, 0);
706 if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
707 PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size)
709 PB_RETURN_ERROR(stream, "bytes size exceeded");
712 return pb_encode_string(stream, bytes->bytes, bytes->size);
715 static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
718 size_t max_size = field->data_size;
719 const char *p = (const char*)src;
721 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
722 max_size = (size_t)-1;
726 size = 0; /* Treat null pointer as an empty string */
730 /* strnlen() is not always available, so just use a loop */
731 while (size < max_size && *p != '\0')
738 return pb_encode_string(stream, (const pb_byte_t*)src, size);
741 static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
743 if (field->ptr == NULL)
744 PB_RETURN_ERROR(stream, "invalid field descriptor");
746 return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
749 static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
751 return pb_encode_string(stream, (const pb_byte_t*)src, field->data_size);