SingingCat 0
application
pb_encode.c
1/* pb_encode.c -- encode a protobuf using minimal resources
2 *
3 * 2011 Petteri Aimonen <jpa@kapsi.fi>
4 */
5
6#include "pb.h"
7#include "pb_encode.h"
8#include "pb_common.h"
9
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.
13 */
14#if !defined(__GNUC__) || (__GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
15#define checkreturn
16#else
17#define checkreturn __attribute__((warn_unused_result))
18#endif
19
20/**************************************
21* Declarations internal to this file *
22**************************************/
23static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
24static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field);
25static bool checkreturn pb_check_proto3_default_value(const pb_field_iter_t *field);
26static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_iter_t *field);
27static bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_iter_t *field);
28static bool checkreturn encode_field(pb_ostream_t *stream, pb_field_iter_t *field);
29static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_iter_t *field);
30static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension);
31static bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high);
32static bool checkreturn pb_enc_bool(pb_ostream_t *stream, const pb_field_iter_t *field);
33static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_iter_t *field);
34static bool checkreturn pb_enc_fixed(pb_ostream_t *stream, const pb_field_iter_t *field);
35static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_iter_t *field);
36static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_iter_t *field);
37static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_iter_t *field);
38static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_iter_t *field);
39
40#ifdef PB_WITHOUT_64BIT
41#define pb_int64_t int32_t
42#define pb_uint64_t uint32_t
43#else
44#define pb_int64_t int64_t
45#define pb_uint64_t uint64_t
46#endif
47
48/*******************************
49* pb_ostream_t implementation *
50*******************************/
51static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count) {
52 pb_byte_t *dest = (pb_byte_t *)stream->state;
53
54 stream->state = dest + count;
55
56 memcpy(dest, buf, count * sizeof(pb_byte_t));
57
58 return true;
59}
60
61pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize) {
63
64#ifdef PB_BUFFER_ONLY
65 /* In PB_BUFFER_ONLY configuration the callback pointer is just int*.
66 * NULL pointer marks a sizing field, so put a non-NULL value to mark a buffer stream.
67 */
68 static const int marker = 0;
69 stream.callback = &marker;
70#else
71 stream.callback = &buf_write;
72#endif
73 stream.state = buf;
74 stream.max_size = bufsize;
75 stream.bytes_written = 0;
76#ifndef PB_NO_ERRMSG
77 stream.errmsg = NULL;
78#endif
79 return stream;
80}
81
82bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count) {
83 if (count > 0 && stream->callback != NULL) {
84 if (stream->bytes_written + count < stream->bytes_written ||
85 stream->bytes_written + count > stream->max_size) {
86 PB_RETURN_ERROR(stream, "stream full");
87 }
88
89#ifdef PB_BUFFER_ONLY
90 if (!buf_write(stream, buf, count)) {
91 PB_RETURN_ERROR(stream, "io error");
92 }
93#else
94 if (!stream->callback(stream, buf, count)) {
95 PB_RETURN_ERROR(stream, "io error");
96 }
97#endif
98 }
99
100 stream->bytes_written += count;
101 return true;
102}
103
104/*************************
105* Encode a single field *
106*************************/
107
108/* Read a bool value without causing undefined behavior even if the value
109 * is invalid. See issue #434 and
110 * https://stackoverflow.com/questions/27661768/weird-results-for-conditional
111 */
112static bool safe_read_bool(const void *pSize) {
113 const char *p = (const char *)pSize;
114 size_t i;
115
116 for (i = 0; i < sizeof(bool); i++) {
117 if (p[i] != 0) {
118 return true;
119 }
120 }
121 return false;
122}
123
124/* Encode a static array. Handles the size calculations and possible packing. */
125static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field) {
126 pb_size_t i;
127 pb_size_t count;
128
129#ifndef PB_ENCODE_ARRAYS_UNPACKED
130 size_t size;
131#endif
132
133 count = *(pb_size_t *)field->pSize;
134
135 if (count == 0) {
136 return true;
137 }
138
139 if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size) {
140 PB_RETURN_ERROR(stream, "array max size exceeded");
141 }
142
143#ifndef PB_ENCODE_ARRAYS_UNPACKED
144 /* We always pack arrays if the datatype allows it. */
145 if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE) {
146 if (!pb_encode_tag(stream, PB_WT_STRING, field->tag)) {
147 return false;
148 }
149
150 /* Determine the total size of packed array. */
151 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32) {
152 size = 4 * (size_t)count;
153 } else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64) {
154 size = 8 * (size_t)count;
155 } else {
156 pb_ostream_t sizestream = PB_OSTREAM_SIZING;
157 void *pData_orig = field->pData;
158 for (i = 0; i < count; i++) {
159 if (!pb_enc_varint(&sizestream, field)) {
160 PB_RETURN_ERROR(stream, PB_GET_ERROR(&sizestream));
161 }
162 field->pData = (char *)field->pData + field->data_size;
163 }
164 field->pData = pData_orig;
165 size = sizestream.bytes_written;
166 }
167
168 if (!pb_encode_varint(stream, (pb_uint64_t)size)) {
169 return false;
170 }
171
172 if (stream->callback == NULL) {
173 return pb_write(stream, NULL, size); /* Just sizing.. */
174 }
175 /* Write the data */
176 for (i = 0; i < count; i++) {
177 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32 || PB_LTYPE(field->type) == PB_LTYPE_FIXED64) {
178 if (!pb_enc_fixed(stream, field)) {
179 return false;
180 }
181 } else {
182 if (!pb_enc_varint(stream, field)) {
183 return false;
184 }
185 }
186
187 field->pData = (char *)field->pData + field->data_size;
188 }
189 } else /* Unpacked fields */
190#endif
191 {
192 for (i = 0; i < count; i++) {
193 /* Normally the data is stored directly in the array entries, but
194 * for pointer-type string and bytes fields, the array entries are
195 * actually pointers themselves also. So we have to dereference once
196 * more to get to the actual data. */
197 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
198 (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
199 PB_LTYPE(field->type) == PB_LTYPE_BYTES)) {
200 bool status;
201 void *pData_orig = field->pData;
202 field->pData = *(void *const *)field->pData;
203
204 if (!field->pData) {
205 /* Null pointer in array is treated as empty string / bytes */
206 status = pb_encode_tag_for_field(stream, field) &&
207 pb_encode_varint(stream, 0);
208 } else {
209 status = encode_basic_field(stream, field);
210 }
211
212 field->pData = pData_orig;
213
214 if (!status) {
215 return false;
216 }
217 } else {
218 if (!encode_basic_field(stream, field)) {
219 return false;
220 }
221 }
222 field->pData = (char *)field->pData + field->data_size;
223 }
224 }
225
226 return true;
227}
228
229/* In proto3, all fields are optional and are only encoded if their value is "non-zero".
230 * This function implements the check for the zero value. */
231static bool checkreturn pb_check_proto3_default_value(const pb_field_iter_t *field) {
232 pb_type_t type = field->type;
233
234 if (PB_ATYPE(type) == PB_ATYPE_STATIC) {
235 if (PB_HTYPE(type) == PB_HTYPE_REQUIRED) {
236 /* Required proto2 fields inside proto3 submessage, pretty rare case */
237 return false;
238 } else if (PB_HTYPE(type) == PB_HTYPE_REPEATED) {
239 /* Repeated fields inside proto3 submessage: present if count != 0 */
240 return *(const pb_size_t *)field->pSize == 0;
241 } else if (PB_HTYPE(type) == PB_HTYPE_ONEOF) {
242 /* Oneof fields */
243 return *(const pb_size_t *)field->pSize == 0;
244 } else if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL) {
245 /* Proto2 optional fields inside proto3 message, or proto3
246 * submessage fields. */
247 return safe_read_bool(field->pSize) == false;
248 } else if (field->descriptor->default_value) {
249 /* Proto3 messages do not have default values, but proto2 messages
250 * can contain optional fields without has_fields (generator option 'proto3').
251 * In this case they must always be encoded, to make sure that the
252 * non-zero default value is overwritten.
253 */
254 return false;
255 }
256
257 /* Rest is proto3 singular fields */
258 if (PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE) {
259 /* Simple integer / float fields */
260 pb_size_t i;
261 const char *p = (const char *)field->pData;
262 for (i = 0; i < field->data_size; i++) {
263 if (p[i] != 0) {
264 return false;
265 }
266 }
267
268 return true;
269 } else if (PB_LTYPE(type) == PB_LTYPE_BYTES) {
270 const pb_bytes_array_t *bytes = (const pb_bytes_array_t *)field->pData;
271 return bytes->size == 0;
272 } else if (PB_LTYPE(type) == PB_LTYPE_STRING) {
273 return *(const char *)field->pData == '\0';
274 } else if (PB_LTYPE(type) == PB_LTYPE_FIXED_LENGTH_BYTES) {
275 /* Fixed length bytes is only empty if its length is fixed
276 * as 0. Which would be pretty strange, but we can check
277 * it anyway. */
278 return field->data_size == 0;
279 } else if (PB_LTYPE_IS_SUBMSG(type)) {
280 /* Check all fields in the submessage to find if any of them
281 * are non-zero. The comparison cannot be done byte-per-byte
282 * because the C struct may contain padding bytes that must
283 * be skipped. Note that usually proto3 submessages have
284 * a separate has_field that is checked earlier in this if.
285 */
286 pb_field_iter_t iter;
287 if (pb_field_iter_begin(&iter, field->submsg_desc, field->pData)) {
288 do{
289 if (!pb_check_proto3_default_value(&iter)) {
290 return false;
291 }
292 } while (pb_field_iter_next(&iter));
293 }
294 return true;
295 }
296 } else if (PB_ATYPE(type) == PB_ATYPE_POINTER) {
297 return field->pData == NULL;
298 } else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK) {
299 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION) {
300 const pb_extension_t *extension = *(const pb_extension_t *const *)field->pData;
301 return extension == NULL;
302 } else if (field->descriptor->field_callback == pb_default_field_callback) {
303 pb_callback_t *pCallback = (pb_callback_t *)field->pData;
304 return pCallback->funcs.encode == NULL;
305 } else {
306 return field->descriptor->field_callback == NULL;
307 }
308 }
309
310 return false; /* Not typically reached, safe default for weird special cases. */
311}
312
313/* Encode a field with static or pointer allocation, i.e. one whose data
314 * is available to the encoder directly. */
315static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_iter_t *field) {
316 if (!field->pData) {
317 /* Missing pointer field */
318 return true;
319 }
320
321 if (!pb_encode_tag_for_field(stream, field)) {
322 return false;
323 }
324
325 switch (PB_LTYPE(field->type)) {
326 case PB_LTYPE_BOOL:
327 return pb_enc_bool(stream, field);
328
329 case PB_LTYPE_VARINT:
330 case PB_LTYPE_UVARINT:
331 case PB_LTYPE_SVARINT:
332 return pb_enc_varint(stream, field);
333
334 case PB_LTYPE_FIXED32:
335 case PB_LTYPE_FIXED64:
336 return pb_enc_fixed(stream, field);
337
338 case PB_LTYPE_BYTES:
339 return pb_enc_bytes(stream, field);
340
341 case PB_LTYPE_STRING:
342 return pb_enc_string(stream, field);
343
344 case PB_LTYPE_SUBMESSAGE:
345 case PB_LTYPE_SUBMSG_W_CB:
346 return pb_enc_submessage(stream, field);
347
348 case PB_LTYPE_FIXED_LENGTH_BYTES:
349 return pb_enc_fixed_length_bytes(stream, field);
350
351 default:
352 PB_RETURN_ERROR(stream, "invalid field type");
353 }
354}
355
356/* Encode a field with callback semantics. This means that a user function is
357 * called to provide and encode the actual data. */
358static bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_iter_t *field) {
359 if (field->descriptor->field_callback != NULL) {
360 if (!field->descriptor->field_callback(NULL, stream, field)) {
361 PB_RETURN_ERROR(stream, "callback error");
362 }
363 }
364 return true;
365}
366
367/* Encode a single field of any callback, pointer or static type. */
368static bool checkreturn encode_field(pb_ostream_t *stream, pb_field_iter_t *field) {
369 /* Check field presence */
370 if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF) {
371 if (*(const pb_size_t *)field->pSize != field->tag) {
372 /* Different type oneof field */
373 return true;
374 }
375 } else if (PB_HTYPE(field->type) == PB_HTYPE_OPTIONAL) {
376 if (field->pSize) {
377 if (safe_read_bool(field->pSize) == false) {
378 /* Missing optional field */
379 return true;
380 }
381 } else if (PB_ATYPE(field->type) == PB_ATYPE_STATIC) {
382 /* Proto3 singular field */
383 if (pb_check_proto3_default_value(field)) {
384 return true;
385 }
386 }
387 }
388
389 if (!field->pData) {
390 if (PB_HTYPE(field->type) == PB_HTYPE_REQUIRED) {
391 PB_RETURN_ERROR(stream, "missing required field");
392 }
393
394 /* Pointer field set to NULL */
395 return true;
396 }
397
398 /* Then encode field contents */
399 if (PB_ATYPE(field->type) == PB_ATYPE_CALLBACK) {
400 return encode_callback_field(stream, field);
401 } else if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED) {
402 return encode_array(stream, field);
403 } else {
404 return encode_basic_field(stream, field);
405 }
406}
407
408/* Default handler for extension fields. Expects to have a pb_msgdesc_t
409 * pointer in the extension->type->arg field, pointing to a message with
410 * only one field in it. */
411static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension) {
412 pb_field_iter_t iter;
413
414 if (!pb_field_iter_begin_extension_const(&iter, extension)) {
415 PB_RETURN_ERROR(stream, "invalid extension");
416 }
417
418 return encode_field(stream, &iter);
419}
420
421
422/* Walk through all the registered extensions and give them a chance
423 * to encode themselves. */
424static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_iter_t *field) {
425 const pb_extension_t *extension = *(const pb_extension_t *const *)field->pData;
426
427 while (extension) {
428 bool status;
429 if (extension->type->encode) {
430 status = extension->type->encode(stream, extension);
431 } else {
432 status = default_extension_encoder(stream, extension);
433 }
434
435 if (!status) {
436 return false;
437 }
438
439 extension = extension->next;
440 }
441
442 return true;
443}
444
445/*********************
446* Encode all fields *
447*********************/
448bool checkreturn pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct) {
449 pb_field_iter_t iter;
450
451 if (!pb_field_iter_begin_const(&iter, fields, src_struct)) {
452 return true; /* Empty message type */
453 }
454 do {
455 if (PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION) {
456 /* Special case for the extension field placeholder */
457 if (!encode_extension_field(stream, &iter)) {
458 return false;
459 }
460 } else {
461 /* Regular field */
462 if (!encode_field(stream, &iter)) {
463 return false;
464 }
465 }
466 } while (pb_field_iter_next(&iter));
467
468 return true;
469}
470
471bool checkreturn pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags) {
472 if ((flags & PB_ENCODE_DELIMITED) != 0) {
473 return pb_encode_submessage(stream, fields, src_struct);
474 } else if ((flags & PB_ENCODE_NULLTERMINATED) != 0) {
475 const pb_byte_t zero = 0;
476
477 if (!pb_encode(stream, fields, src_struct)) {
478 return false;
479 }
480
481 return pb_write(stream, &zero, 1);
482 } else {
483 return pb_encode(stream, fields, src_struct);
484 }
485}
486
487bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct) {
488 pb_ostream_t stream = PB_OSTREAM_SIZING;
489
490 if (!pb_encode(&stream, fields, src_struct)) {
491 return false;
492 }
493
494 *size = stream.bytes_written;
495 return true;
496}
497
498/********************
499* Helper functions *
500********************/
501
502/* This function avoids 64-bit shifts as they are quite slow on many platforms. */
503static bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high) {
504 size_t i = 0;
505 pb_byte_t buffer[10];
506 pb_byte_t byte = (pb_byte_t)(low & 0x7F);
507
508 low >>= 7;
509
510 while (i < 4 && (low != 0 || high != 0)) {
511 byte |= 0x80;
512 buffer[i++] = byte;
513 byte = (pb_byte_t)(low & 0x7F);
514 low >>= 7;
515 }
516
517 if (high) {
518 byte = (pb_byte_t)(byte | ((high & 0x07) << 4));
519 high >>= 3;
520
521 while (high) {
522 byte |= 0x80;
523 buffer[i++] = byte;
524 byte = (pb_byte_t)(high & 0x7F);
525 high >>= 7;
526 }
527 }
528
529 buffer[i++] = byte;
530
531 return pb_write(stream, buffer, i);
532}
533
534bool checkreturn pb_encode_varint(pb_ostream_t *stream, pb_uint64_t value) {
535 if (value <= 0x7F) {
536 /* Fast path: single byte */
537 pb_byte_t byte = (pb_byte_t)value;
538 return pb_write(stream, &byte, 1);
539 } else {
540#ifdef PB_WITHOUT_64BIT
541 return pb_encode_varint_32(stream, value, 0);
542#else
543 return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)(value >> 32));
544#endif
545 }
546}
547
548bool checkreturn pb_encode_svarint(pb_ostream_t *stream, pb_int64_t value) {
549 pb_uint64_t zigzagged;
550 pb_uint64_t mask = ((pb_uint64_t)-1) >> 1; /* Satisfy clang -fsanitize=integer */
551
552 if (value < 0) {
553 zigzagged = ~(((pb_uint64_t)value & mask) << 1);
554 } else {
555 zigzagged = (pb_uint64_t)value << 1;
556 }
557
558 return pb_encode_varint(stream, zigzagged);
559}
560
561bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value) {
562#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
563 /* Fast path if we know that we're on little endian */
564 return pb_write(stream, (const pb_byte_t *)value, 4);
565#else
566 uint32_t val = *(const uint32_t *)value;
567 pb_byte_t bytes[4];
568 bytes[0] = (pb_byte_t)(val & 0xFF);
569 bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
570 bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
571 bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
572 return pb_write(stream, bytes, 4);
573#endif
574}
575
576#ifndef PB_WITHOUT_64BIT
577bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value) {
578#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
579 /* Fast path if we know that we're on little endian */
580 return pb_write(stream, (const pb_byte_t *)value, 8);
581#else
582 uint64_t val = *(const uint64_t *)value;
583 pb_byte_t bytes[8];
584 bytes[0] = (pb_byte_t)(val & 0xFF);
585 bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
586 bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
587 bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
588 bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
589 bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
590 bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
591 bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
592 return pb_write(stream, bytes, 8);
593#endif
594}
595#endif
596
597bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number) {
598 pb_uint64_t tag = ((pb_uint64_t)field_number << 3) | wiretype;
599
600 return pb_encode_varint(stream, tag);
601}
602
603bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field) {
604 pb_wire_type_t wiretype;
605
606 switch (PB_LTYPE(field->type)) {
607 case PB_LTYPE_BOOL:
608 case PB_LTYPE_VARINT:
609 case PB_LTYPE_UVARINT:
610 case PB_LTYPE_SVARINT:
611 wiretype = PB_WT_VARINT;
612 break;
613
614 case PB_LTYPE_FIXED32:
615 wiretype = PB_WT_32BIT;
616 break;
617
618 case PB_LTYPE_FIXED64:
619 wiretype = PB_WT_64BIT;
620 break;
621
622 case PB_LTYPE_BYTES:
623 case PB_LTYPE_STRING:
624 case PB_LTYPE_SUBMESSAGE:
625 case PB_LTYPE_SUBMSG_W_CB:
626 case PB_LTYPE_FIXED_LENGTH_BYTES:
627 wiretype = PB_WT_STRING;
628 break;
629
630 default:
631 PB_RETURN_ERROR(stream, "invalid field type");
632 }
633
634 return pb_encode_tag(stream, wiretype, field->tag);
635}
636
637bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size) {
638 if (!pb_encode_varint(stream, (pb_uint64_t)size)) {
639 return false;
640 }
641
642 return pb_write(stream, buffer, size);
643}
644
645bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct) {
646 /* First calculate the message size using a non-writing substream. */
647 pb_ostream_t substream = PB_OSTREAM_SIZING;
648 size_t size;
649 bool status;
650
651 if (!pb_encode(&substream, fields, src_struct)) {
652#ifndef PB_NO_ERRMSG
653 stream->errmsg = substream.errmsg;
654#endif
655 return false;
656 }
657
658 size = substream.bytes_written;
659
660 if (!pb_encode_varint(stream, (pb_uint64_t)size)) {
661 return false;
662 }
663
664 if (stream->callback == NULL) {
665 return pb_write(stream, NULL, size); /* Just sizing */
666 }
667 if (stream->bytes_written + size > stream->max_size) {
668 PB_RETURN_ERROR(stream, "stream full");
669 }
670
671 /* Use a substream to verify that a callback doesn't write more than
672 * what it did the first time. */
673 substream.callback = stream->callback;
674 substream.state = stream->state;
675 substream.max_size = size;
676 substream.bytes_written = 0;
677#ifndef PB_NO_ERRMSG
678 substream.errmsg = NULL;
679#endif
680
681 status = pb_encode(&substream, fields, src_struct);
682
683 stream->bytes_written += substream.bytes_written;
684 stream->state = substream.state;
685#ifndef PB_NO_ERRMSG
686 stream->errmsg = substream.errmsg;
687#endif
688
689 if (substream.bytes_written != size) {
690 PB_RETURN_ERROR(stream, "submsg size changed");
691 }
692
693 return status;
694}
695
696/* Field encoders */
697
698static bool checkreturn pb_enc_bool(pb_ostream_t *stream, const pb_field_iter_t *field) {
699 uint32_t value = safe_read_bool(field->pData) ? 1 : 0;
700
701 PB_UNUSED(field);
702 return pb_encode_varint(stream, value);
703}
704
705static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_iter_t *field) {
706 if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT) {
707 /* Perform unsigned integer extension */
708 pb_uint64_t value = 0;
709
710 if (field->data_size == sizeof(uint_least8_t)) {
711 value = *(const uint_least8_t *)field->pData;
712 } else if (field->data_size == sizeof(uint_least16_t)) {
713 value = *(const uint_least16_t *)field->pData;
714 } else if (field->data_size == sizeof(uint32_t)) {
715 value = *(const uint32_t *)field->pData;
716 } else if (field->data_size == sizeof(pb_uint64_t)) {
717 value = *(const pb_uint64_t *)field->pData;
718 } else {
719 PB_RETURN_ERROR(stream, "invalid data_size");
720 }
721
722 return pb_encode_varint(stream, value);
723 } else {
724 /* Perform signed integer extension */
725 pb_int64_t value = 0;
726
727 if (field->data_size == sizeof(int_least8_t)) {
728 value = *(const int_least8_t *)field->pData;
729 } else if (field->data_size == sizeof(int_least16_t)) {
730 value = *(const int_least16_t *)field->pData;
731 } else if (field->data_size == sizeof(int32_t)) {
732 value = *(const int32_t *)field->pData;
733 } else if (field->data_size == sizeof(pb_int64_t)) {
734 value = *(const pb_int64_t *)field->pData;
735 } else {
736 PB_RETURN_ERROR(stream, "invalid data_size");
737 }
738
739 if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT) {
740 return pb_encode_svarint(stream, value);
741 }
742#ifdef PB_WITHOUT_64BIT
743 else if (value < 0) {
744 return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)-1);
745 }
746#endif
747 else {
748 return pb_encode_varint(stream, (pb_uint64_t)value);
749 }
750 }
751}
752
753static bool checkreturn pb_enc_fixed(pb_ostream_t *stream, const pb_field_iter_t *field) {
754#ifdef PB_CONVERT_DOUBLE_FLOAT
755 if (field->data_size == sizeof(float) && PB_LTYPE(field->type) == PB_LTYPE_FIXED64) {
756 return pb_encode_float_as_double(stream, *(float *)field->pData);
757 }
758#endif
759
760 if (field->data_size == sizeof(uint32_t)) {
761 return pb_encode_fixed32(stream, field->pData);
762 }
763#ifndef PB_WITHOUT_64BIT
764 else if (field->data_size == sizeof(uint64_t)) {
765 return pb_encode_fixed64(stream, field->pData);
766 }
767#endif
768 else {
769 PB_RETURN_ERROR(stream, "invalid data_size");
770 }
771}
772
773static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_iter_t *field) {
774 const pb_bytes_array_t *bytes = NULL;
775
776 bytes = (const pb_bytes_array_t *)field->pData;
777
778 if (bytes == NULL) {
779 /* Treat null pointer as an empty bytes field */
780 return pb_encode_string(stream, NULL, 0);
781 }
782
783 if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
784 bytes->size > field->data_size - offsetof(pb_bytes_array_t, bytes)) {
785 PB_RETURN_ERROR(stream, "bytes size exceeded");
786 }
787
788 return pb_encode_string(stream, bytes->bytes, (size_t)bytes->size);
789}
790
791static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_iter_t *field) {
792 size_t size = 0;
793 size_t max_size = (size_t)field->data_size;
794 const char *str = (const char *)field->pData;
795
796 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) {
797 max_size = (size_t)-1;
798 } else {
799 /* pb_dec_string() assumes string fields end with a null
800 * terminator when the type isn't PB_ATYPE_POINTER, so we
801 * shouldn't allow more than max-1 bytes to be written to
802 * allow space for the null terminator.
803 */
804 if (max_size == 0) {
805 PB_RETURN_ERROR(stream, "zero-length string");
806 }
807
808 max_size -= 1;
809 }
810
811
812 if (str == NULL) {
813 size = 0; /* Treat null pointer as an empty string */
814 } else {
815 const char *p = str;
816
817 /* strnlen() is not always available, so just use a loop */
818 while (size < max_size && *p != '\0') {
819 size++;
820 p++;
821 }
822
823 if (*p != '\0') {
824 PB_RETURN_ERROR(stream, "unterminated string");
825 }
826 }
827
828#ifdef PB_VALIDATE_UTF8
829 if (!pb_validate_utf8(str)) {
830 PB_RETURN_ERROR(stream, "invalid utf8");
831 }
832#endif
833
834 return pb_encode_string(stream, (const pb_byte_t *)str, size);
835}
836
837static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_iter_t *field) {
838 if (field->submsg_desc == NULL) {
839 PB_RETURN_ERROR(stream, "invalid field descriptor");
840 }
841
842 if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL) {
843 /* Message callback is stored right before pSize. */
844 pb_callback_t *callback = (pb_callback_t *)field->pSize - 1;
845 if (callback->funcs.encode) {
846 if (!callback->funcs.encode(stream, field, &callback->arg)) {
847 return false;
848 }
849 }
850 }
851
852 return pb_encode_submessage(stream, field->submsg_desc, field->pData);
853}
854
855static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_iter_t *field) {
856 return pb_encode_string(stream, (const pb_byte_t *)field->pData, (size_t)field->data_size);
857}
858
859#ifdef PB_CONVERT_DOUBLE_FLOAT
860bool pb_encode_float_as_double(pb_ostream_t *stream, float value) {
861 union { float f; uint32_t i; } in;
862 uint_least8_t sign;
863 int exponent;
864 uint64_t mantissa;
865
866 in.f = value;
867
868 /* Decompose input value */
869 sign = (uint_least8_t)((in.i >> 31) & 1);
870 exponent = (int)((in.i >> 23) & 0xFF) - 127;
871 mantissa = in.i & 0x7FFFFF;
872
873 if (exponent == 128) {
874 /* Special value (NaN etc.) */
875 exponent = 1024;
876 } else if (exponent == -127) {
877 if (!mantissa) {
878 /* Zero */
879 exponent = -1023;
880 } else {
881 /* Denormalized */
882 mantissa <<= 1;
883 while (!(mantissa & 0x800000)) {
884 mantissa <<= 1;
885 exponent--;
886 }
887 mantissa &= 0x7FFFFF;
888 }
889 }
890
891 /* Combine fields */
892 mantissa <<= 29;
893 mantissa |= (uint64_t)(exponent + 1023) << 52;
894 mantissa |= (uint64_t)sign << 63;
895
896 return pb_encode_fixed64(stream, &mantissa);
897}
898#endif
Definition: streams.h:14