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 void *pb_const_cast(const void *p);
31 static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
32 static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
33 static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
34 static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src);
35 static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src);
36 static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src);
37 static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src);
38 static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src);
39 static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src);
41 /* --- Function pointers to field encoders ---
42 * Order in the array must match pb_action_t LTYPE numbering.
44 static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = {
54 NULL, /* extensions */
55 &pb_enc_fixed_length_bytes
58 /*******************************
59 * pb_ostream_t implementation *
60 *******************************/
62 static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
65 pb_byte_t *dest = (pb_byte_t*)stream->state;
66 stream->state = dest + count;
68 for (i = 0; i < count; i++)
74 pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
78 stream.callback = (void*)1; /* Just a marker value */
80 stream.callback = &buf_write;
83 stream.max_size = bufsize;
84 stream.bytes_written = 0;
91 bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
93 if (stream->callback != NULL)
95 if (stream->bytes_written + count > stream->max_size)
96 PB_RETURN_ERROR(stream, "stream full");
99 if (!buf_write(stream, buf, count))
100 PB_RETURN_ERROR(stream, "io error");
102 if (!stream->callback(stream, buf, count))
103 PB_RETURN_ERROR(stream, "io error");
107 stream->bytes_written += count;
111 /*************************
112 * Encode a single field *
113 *************************/
115 /* Encode a static array. Handles the size calculations and possible packing. */
116 static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
117 const void *pData, size_t count, pb_encoder_t func)
126 if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
127 PB_RETURN_ERROR(stream, "array max size exceeded");
129 /* We always pack arrays if the datatype allows it. */
130 if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
132 if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
135 /* Determine the total size of packed array. */
136 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
140 else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
146 pb_ostream_t sizestream = PB_OSTREAM_SIZING;
148 for (i = 0; i < count; i++)
150 if (!func(&sizestream, field, p))
152 p = (const char*)p + field->data_size;
154 size = sizestream.bytes_written;
157 if (!pb_encode_varint(stream, (uint64_t)size))
160 if (stream->callback == NULL)
161 return pb_write(stream, NULL, size); /* Just sizing.. */
165 for (i = 0; i < count; i++)
167 if (!func(stream, field, p))
169 p = (const char*)p + field->data_size;
175 for (i = 0; i < count; i++)
177 if (!pb_encode_tag_for_field(stream, field))
180 /* Normally the data is stored directly in the array entries, but
181 * for pointer-type string and bytes fields, the array entries are
182 * actually pointers themselves also. So we have to dereference once
183 * more to get to the actual data. */
184 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
185 (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
186 PB_LTYPE(field->type) == PB_LTYPE_BYTES))
188 if (!func(stream, field, *(const void* const*)p))
193 if (!func(stream, field, p))
196 p = (const char*)p + field->data_size;
203 /* In proto3, all fields are optional and are only encoded if their value is "non-zero".
204 * This function implements the check for the zero value. */
205 static bool pb_check_proto3_default_value(const pb_field_t *field, const void *pData)
207 if (PB_ATYPE(field->type) == PB_ATYPE_STATIC)
209 if (PB_LTYPE(field->type) == PB_LTYPE_BYTES)
211 const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)pData;
212 return bytes->size == 0;
214 else if (PB_LTYPE(field->type) == PB_LTYPE_STRING)
216 return *(const char*)pData == '\0';
218 else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES)
220 /* Fixed length bytes is only empty if its length is fixed
221 * as 0. Which would be pretty strange, but we can check
223 return field->data_size == 0;
225 else if (PB_LTYPE(field->type) == PB_LTYPE_SUBMESSAGE)
227 /* Check all fields in the submessage to find if any of them
228 * are non-zero. The comparison cannot be done byte-per-byte
229 * because the C struct may contain padding bytes that must
232 pb_field_iter_t iter;
233 if (pb_field_iter_begin(&iter, (const pb_field_t*)field->ptr, pb_const_cast(pData)))
237 if (!pb_check_proto3_default_value(iter.pos, iter.pData))
241 } while (pb_field_iter_next(&iter));
248 /* Catch-all branch that does byte-per-byte comparison for zero value.
250 * This is for all pointer fields, and for static PB_LTYPE_VARINT,
251 * UVARINT, SVARINT, FIXED32, FIXED64, EXTENSION fields, and also
252 * callback fields. These all have integer or pointer value which
253 * can be compared with 0.
256 const char *p = (const char*)pData;
257 for (i = 0; i < field->data_size; i++)
269 /* Encode a field with static or pointer allocation, i.e. one whose data
270 * is available to the encoder directly. */
271 static bool checkreturn encode_basic_field(pb_ostream_t *stream,
272 const pb_field_t *field, const void *pData)
276 const void *pSize = &implicit_has;
278 func = PB_ENCODERS[PB_LTYPE(field->type)];
280 if (field->size_offset)
282 /* Static optional, repeated or oneof field */
283 pSize = (const char*)pData + field->size_offset;
285 else if (PB_HTYPE(field->type) == PB_HTYPE_OPTIONAL)
287 /* Proto3 style field, optional but without explicit has_ field. */
288 implicit_has = !pb_check_proto3_default_value(field, pData);
292 /* Required field, always present */
296 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
298 /* pData is a pointer to the field, which contains pointer to
299 * the data. If the 2nd pointer is NULL, it is interpreted as if
300 * the has_field was false.
302 pData = *(const void* const*)pData;
303 implicit_has = (pData != NULL);
306 switch (PB_HTYPE(field->type))
308 case PB_HTYPE_REQUIRED:
310 PB_RETURN_ERROR(stream, "missing required field");
311 if (!pb_encode_tag_for_field(stream, field))
313 if (!func(stream, field, pData))
317 case PB_HTYPE_OPTIONAL:
318 if (*(const bool*)pSize)
320 if (!pb_encode_tag_for_field(stream, field))
323 if (!func(stream, field, pData))
328 case PB_HTYPE_REPEATED:
329 if (!encode_array(stream, field, pData, *(const pb_size_t*)pSize, func))
334 if (*(const pb_size_t*)pSize == field->tag)
336 if (!pb_encode_tag_for_field(stream, field))
339 if (!func(stream, field, pData))
345 PB_RETURN_ERROR(stream, "invalid field type");
351 /* Encode a field with callback semantics. This means that a user function is
352 * called to provide and encode the actual data. */
353 static bool checkreturn encode_callback_field(pb_ostream_t *stream,
354 const pb_field_t *field, const void *pData)
356 const pb_callback_t *callback = (const pb_callback_t*)pData;
358 #ifdef PB_OLD_CALLBACK_STYLE
359 const void *arg = callback->arg;
361 void * const *arg = &(callback->arg);
364 if (callback->funcs.encode != NULL)
366 if (!callback->funcs.encode(stream, field, arg))
367 PB_RETURN_ERROR(stream, "callback error");
372 /* Encode a single field of any callback or static type. */
373 static bool checkreturn encode_field(pb_ostream_t *stream,
374 const pb_field_t *field, const void *pData)
376 switch (PB_ATYPE(field->type))
378 case PB_ATYPE_STATIC:
379 case PB_ATYPE_POINTER:
380 return encode_basic_field(stream, field, pData);
382 case PB_ATYPE_CALLBACK:
383 return encode_callback_field(stream, field, pData);
386 PB_RETURN_ERROR(stream, "invalid field type");
390 /* Default handler for extension fields. Expects to have a pb_field_t
391 * pointer in the extension->type->arg field. */
392 static bool checkreturn default_extension_encoder(pb_ostream_t *stream,
393 const pb_extension_t *extension)
395 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
397 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
399 /* For pointer extensions, the pointer is stored directly
400 * in the extension structure. This avoids having an extra
402 return encode_field(stream, field, &extension->dest);
406 return encode_field(stream, field, extension->dest);
410 /* Walk through all the registered extensions and give them a chance
411 * to encode themselves. */
412 static bool checkreturn encode_extension_field(pb_ostream_t *stream,
413 const pb_field_t *field, const void *pData)
415 const pb_extension_t *extension = *(const pb_extension_t* const *)pData;
421 if (extension->type->encode)
422 status = extension->type->encode(stream, extension);
424 status = default_extension_encoder(stream, extension);
429 extension = extension->next;
435 /*********************
436 * Encode all fields *
437 *********************/
439 static void *pb_const_cast(const void *p)
441 /* Note: this casts away const, in order to use the common field iterator
442 * logic for both encoding and decoding. */
451 bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
453 pb_field_iter_t iter;
454 if (!pb_field_iter_begin(&iter, fields, pb_const_cast(src_struct)))
455 return true; /* Empty message type */
458 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_EXTENSION)
460 /* Special case for the extension field placeholder */
461 if (!encode_extension_field(stream, iter.pos, iter.pData))
467 if (!encode_field(stream, iter.pos, iter.pData))
470 } while (pb_field_iter_next(&iter));
475 bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
477 return pb_encode_submessage(stream, fields, src_struct);
480 bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct)
482 pb_ostream_t stream = PB_OSTREAM_SIZING;
484 if (!pb_encode(&stream, fields, src_struct))
487 *size = stream.bytes_written;
491 /********************
493 ********************/
494 bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
496 pb_byte_t buffer[10];
501 pb_byte_t v = (pb_byte_t)value;
502 return pb_write(stream, &v, 1);
507 buffer[i] = (pb_byte_t)((value & 0x7F) | 0x80);
511 buffer[i-1] &= 0x7F; /* Unset top bit on last byte */
513 return pb_write(stream, buffer, i);
516 bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
520 zigzagged = ~((uint64_t)value << 1);
522 zigzagged = (uint64_t)value << 1;
524 return pb_encode_varint(stream, zigzagged);
527 bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
529 uint32_t val = *(const uint32_t*)value;
531 bytes[0] = (pb_byte_t)(val & 0xFF);
532 bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
533 bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
534 bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
535 return pb_write(stream, bytes, 4);
538 bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
540 uint64_t val = *(const uint64_t*)value;
542 bytes[0] = (pb_byte_t)(val & 0xFF);
543 bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
544 bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
545 bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
546 bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
547 bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
548 bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
549 bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
550 return pb_write(stream, bytes, 8);
553 bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
555 uint64_t tag = ((uint64_t)field_number << 3) | wiretype;
556 return pb_encode_varint(stream, tag);
559 bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
561 pb_wire_type_t wiretype;
562 switch (PB_LTYPE(field->type))
564 case PB_LTYPE_VARINT:
565 case PB_LTYPE_UVARINT:
566 case PB_LTYPE_SVARINT:
567 wiretype = PB_WT_VARINT;
570 case PB_LTYPE_FIXED32:
571 wiretype = PB_WT_32BIT;
574 case PB_LTYPE_FIXED64:
575 wiretype = PB_WT_64BIT;
579 case PB_LTYPE_STRING:
580 case PB_LTYPE_SUBMESSAGE:
581 case PB_LTYPE_FIXED_LENGTH_BYTES:
582 wiretype = PB_WT_STRING;
586 PB_RETURN_ERROR(stream, "invalid field type");
589 return pb_encode_tag(stream, wiretype, field->tag);
592 bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
594 if (!pb_encode_varint(stream, (uint64_t)size))
597 return pb_write(stream, buffer, size);
600 bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
602 /* First calculate the message size using a non-writing substream. */
603 pb_ostream_t substream = PB_OSTREAM_SIZING;
607 if (!pb_encode(&substream, fields, src_struct))
610 stream->errmsg = substream.errmsg;
615 size = substream.bytes_written;
617 if (!pb_encode_varint(stream, (uint64_t)size))
620 if (stream->callback == NULL)
621 return pb_write(stream, NULL, size); /* Just sizing */
623 if (stream->bytes_written + size > stream->max_size)
624 PB_RETURN_ERROR(stream, "stream full");
626 /* Use a substream to verify that a callback doesn't write more than
627 * what it did the first time. */
628 substream.callback = stream->callback;
629 substream.state = stream->state;
630 substream.max_size = size;
631 substream.bytes_written = 0;
633 substream.errmsg = NULL;
636 status = pb_encode(&substream, fields, src_struct);
638 stream->bytes_written += substream.bytes_written;
639 stream->state = substream.state;
641 stream->errmsg = substream.errmsg;
644 if (substream.bytes_written != size)
645 PB_RETURN_ERROR(stream, "submsg size changed");
652 static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
656 if (field->data_size == sizeof(int_least8_t))
657 value = *(const int_least8_t*)src;
658 else if (field->data_size == sizeof(int_least16_t))
659 value = *(const int_least16_t*)src;
660 else if (field->data_size == sizeof(int32_t))
661 value = *(const int32_t*)src;
662 else if (field->data_size == sizeof(int64_t))
663 value = *(const int64_t*)src;
665 PB_RETURN_ERROR(stream, "invalid data_size");
667 return pb_encode_varint(stream, (uint64_t)value);
670 static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
674 if (field->data_size == sizeof(uint_least8_t))
675 value = *(const uint_least8_t*)src;
676 else if (field->data_size == sizeof(uint_least16_t))
677 value = *(const uint_least16_t*)src;
678 else if (field->data_size == sizeof(uint32_t))
679 value = *(const uint32_t*)src;
680 else if (field->data_size == sizeof(uint64_t))
681 value = *(const uint64_t*)src;
683 PB_RETURN_ERROR(stream, "invalid data_size");
685 return pb_encode_varint(stream, value);
688 static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
692 if (field->data_size == sizeof(int_least8_t))
693 value = *(const int_least8_t*)src;
694 else if (field->data_size == sizeof(int_least16_t))
695 value = *(const int_least16_t*)src;
696 else if (field->data_size == sizeof(int32_t))
697 value = *(const int32_t*)src;
698 else if (field->data_size == sizeof(int64_t))
699 value = *(const int64_t*)src;
701 PB_RETURN_ERROR(stream, "invalid data_size");
703 return pb_encode_svarint(stream, value);
706 static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
709 return pb_encode_fixed64(stream, src);
712 static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
715 return pb_encode_fixed32(stream, src);
718 static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
720 const pb_bytes_array_t *bytes = NULL;
722 bytes = (const pb_bytes_array_t*)src;
726 /* Treat null pointer as an empty bytes field */
727 return pb_encode_string(stream, NULL, 0);
730 if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
731 PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size)
733 PB_RETURN_ERROR(stream, "bytes size exceeded");
736 return pb_encode_string(stream, bytes->bytes, bytes->size);
739 static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
742 size_t max_size = field->data_size;
743 const char *p = (const char*)src;
745 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
746 max_size = (size_t)-1;
750 size = 0; /* Treat null pointer as an empty string */
754 /* strnlen() is not always available, so just use a loop */
755 while (size < max_size && *p != '\0')
762 return pb_encode_string(stream, (const pb_byte_t*)src, size);
765 static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
767 if (field->ptr == NULL)
768 PB_RETURN_ERROR(stream, "invalid field descriptor");
770 return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
773 static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
775 return pb_encode_string(stream, (const pb_byte_t*)src, field->data_size);