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 **************************************/
23 typedef bool (*pb_encoder_t)(pb_ostream_t *stream, const pb_field_t *field, const void *src) checkreturn;
24
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
39 /* --- Function pointers to field encoders ---
40 * Order in the array must match pb_action_t LTYPE numbering.
41 */
42 static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = {
43 &pb_enc_varint,
44 &pb_enc_uvarint,
45 &pb_enc_svarint,
46 &pb_enc_fixed32,
47 &pb_enc_fixed64,
48
49 &pb_enc_bytes,
50 &pb_enc_string,
51 &pb_enc_submessage,
52 NULL, /* extensions */
53 &pb_enc_bytes /* PB_LTYPE_FIXED_LENGTH_BYTES */
54 };
55
56 /*******************************
57 * pb_ostream_t implementation *
58 *******************************/
59
buf_write(pb_ostream_t * stream,const pb_byte_t * buf,size_t count)60 static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
61 {
62 pb_byte_t *dest = (pb_byte_t*)stream->state;
63 stream->state = dest + count;
64
65 while (count--)
66 *dest++ = *buf++;
67
68 return true;
69 }
70
pb_ostream_from_buffer(pb_byte_t * buf,size_t bufsize)71 pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
72 {
73 pb_ostream_t stream;
74 #ifdef PB_BUFFER_ONLY
75 stream.callback = (void*)1; /* Just a marker value */
76 #else
77 stream.callback = &buf_write;
78 #endif
79 stream.state = buf;
80 stream.max_size = bufsize;
81 stream.bytes_written = 0;
82 #ifndef PB_NO_ERRMSG
83 stream.errmsg = NULL;
84 #endif
85 return stream;
86 }
87
pb_write(pb_ostream_t * stream,const pb_byte_t * buf,size_t count)88 bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
89 {
90 if (stream->callback != NULL)
91 {
92 if (stream->bytes_written + count > stream->max_size)
93 PB_RETURN_ERROR(stream, "stream full");
94
95 #ifdef PB_BUFFER_ONLY
96 if (!buf_write(stream, buf, count))
97 PB_RETURN_ERROR(stream, "io error");
98 #else
99 if (!stream->callback(stream, buf, count))
100 PB_RETURN_ERROR(stream, "io error");
101 #endif
102 }
103
104 stream->bytes_written += count;
105 return true;
106 }
107
108 /*************************
109 * Encode a single field *
110 *************************/
111
112 /* Encode a static array. Handles the size calculations and possible packing. */
encode_array(pb_ostream_t * stream,const pb_field_t * field,const void * pData,size_t count,pb_encoder_t func)113 static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
114 const void *pData, size_t count, pb_encoder_t func)
115 {
116 size_t i;
117 const void *p;
118 size_t size;
119
120 if (count == 0)
121 return true;
122
123 if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
124 PB_RETURN_ERROR(stream, "array max size exceeded");
125
126 /* We always pack arrays if the datatype allows it. */
127 if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
128 {
129 if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
130 return false;
131
132 /* Determine the total size of packed array. */
133 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
134 {
135 size = 4 * count;
136 }
137 else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
138 {
139 size = 8 * count;
140 }
141 else
142 {
143 pb_ostream_t sizestream = PB_OSTREAM_SIZING;
144 p = pData;
145 for (i = 0; i < count; i++)
146 {
147 if (!func(&sizestream, field, p))
148 return false;
149 p = (const char*)p + field->data_size;
150 }
151 size = sizestream.bytes_written;
152 }
153
154 if (!pb_encode_varint(stream, (uint64_t)size))
155 return false;
156
157 if (stream->callback == NULL)
158 return pb_write(stream, NULL, size); /* Just sizing.. */
159
160 /* Write the data */
161 p = pData;
162 for (i = 0; i < count; i++)
163 {
164 if (!func(stream, field, p))
165 return false;
166 p = (const char*)p + field->data_size;
167 }
168 }
169 else
170 {
171 p = pData;
172 for (i = 0; i < count; i++)
173 {
174 if (!pb_encode_tag_for_field(stream, field))
175 return false;
176
177 /* Normally the data is stored directly in the array entries, but
178 * for pointer-type string and bytes fields, the array entries are
179 * actually pointers themselves also. So we have to dereference once
180 * more to get to the actual data. */
181 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
182 (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
183 PB_LTYPE(field->type) == PB_LTYPE_BYTES))
184 {
185 if (!func(stream, field, *(const void* const*)p))
186 return false;
187 }
188 else
189 {
190 if (!func(stream, field, p))
191 return false;
192 }
193 p = (const char*)p + field->data_size;
194 }
195 }
196
197 return true;
198 }
199
200 /* Encode a field with static or pointer allocation, i.e. one whose data
201 * is available to the encoder directly. */
encode_basic_field(pb_ostream_t * stream,const pb_field_t * field,const void * pData)202 static bool checkreturn encode_basic_field(pb_ostream_t *stream,
203 const pb_field_t *field, const void *pData)
204 {
205 pb_encoder_t func;
206 const void *pSize;
207 bool implicit_has = true;
208
209 func = PB_ENCODERS[PB_LTYPE(field->type)];
210
211 if (field->size_offset)
212 pSize = (const char*)pData + field->size_offset;
213 else
214 pSize = &implicit_has;
215
216 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
217 {
218 /* pData is a pointer to the field, which contains pointer to
219 * the data. If the 2nd pointer is NULL, it is interpreted as if
220 * the has_field was false.
221 */
222
223 pData = *(const void* const*)pData;
224 implicit_has = (pData != NULL);
225 }
226
227 switch (PB_HTYPE(field->type))
228 {
229 case PB_HTYPE_REQUIRED:
230 if (!pData)
231 PB_RETURN_ERROR(stream, "missing required field");
232 if (!pb_encode_tag_for_field(stream, field))
233 return false;
234 if (!func(stream, field, pData))
235 return false;
236 break;
237
238 case PB_HTYPE_OPTIONAL:
239 if (*(const bool*)pSize)
240 {
241 if (!pb_encode_tag_for_field(stream, field))
242 return false;
243
244 if (!func(stream, field, pData))
245 return false;
246 }
247 break;
248
249 case PB_HTYPE_REPEATED:
250 if (!encode_array(stream, field, pData, *(const pb_size_t*)pSize, func))
251 return false;
252 break;
253
254 case PB_HTYPE_ONEOF:
255 if (*(const pb_size_t*)pSize == field->tag)
256 {
257 if (!pb_encode_tag_for_field(stream, field))
258 return false;
259
260 if (!func(stream, field, pData))
261 return false;
262 }
263 break;
264
265 default:
266 PB_RETURN_ERROR(stream, "invalid field type");
267 }
268
269 return true;
270 }
271
272 /* Encode a field with callback semantics. This means that a user function is
273 * called to provide and encode the actual data. */
encode_callback_field(pb_ostream_t * stream,const pb_field_t * field,const void * pData)274 static bool checkreturn encode_callback_field(pb_ostream_t *stream,
275 const pb_field_t *field, const void *pData)
276 {
277 const pb_callback_t *callback = (const pb_callback_t*)pData;
278
279 #ifdef PB_OLD_CALLBACK_STYLE
280 const void *arg = callback->arg;
281 #else
282 void * const *arg = &(callback->arg);
283 #endif
284
285 if (callback->funcs.encode != NULL)
286 {
287 if (!callback->funcs.encode(stream, field, arg))
288 PB_RETURN_ERROR(stream, "callback error");
289 }
290 return true;
291 }
292
293 /* Encode a single field of any callback or static type. */
encode_field(pb_ostream_t * stream,const pb_field_t * field,const void * pData)294 static bool checkreturn encode_field(pb_ostream_t *stream,
295 const pb_field_t *field, const void *pData)
296 {
297 switch (PB_ATYPE(field->type))
298 {
299 case PB_ATYPE_STATIC:
300 case PB_ATYPE_POINTER:
301 return encode_basic_field(stream, field, pData);
302
303 case PB_ATYPE_CALLBACK:
304 return encode_callback_field(stream, field, pData);
305
306 default:
307 PB_RETURN_ERROR(stream, "invalid field type");
308 }
309 }
310
311 /* Default handler for extension fields. Expects to have a pb_field_t
312 * pointer in the extension->type->arg field. */
default_extension_encoder(pb_ostream_t * stream,const pb_extension_t * extension)313 static bool checkreturn default_extension_encoder(pb_ostream_t *stream,
314 const pb_extension_t *extension)
315 {
316 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
317
318 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
319 {
320 /* For pointer extensions, the pointer is stored directly
321 * in the extension structure. This avoids having an extra
322 * indirection. */
323 return encode_field(stream, field, &extension->dest);
324 }
325 else
326 {
327 return encode_field(stream, field, extension->dest);
328 }
329 }
330
331 /* Walk through all the registered extensions and give them a chance
332 * to encode themselves. */
encode_extension_field(pb_ostream_t * stream,const pb_field_t * field,const void * pData)333 static bool checkreturn encode_extension_field(pb_ostream_t *stream,
334 const pb_field_t *field, const void *pData)
335 {
336 const pb_extension_t *extension = *(const pb_extension_t* const *)pData;
337 PB_UNUSED(field);
338
339 while (extension)
340 {
341 bool status;
342 if (extension->type->encode)
343 status = extension->type->encode(stream, extension);
344 else
345 status = default_extension_encoder(stream, extension);
346
347 if (!status)
348 return false;
349
350 extension = extension->next;
351 }
352
353 return true;
354 }
355
356 /*********************
357 * Encode all fields *
358 *********************/
359
remove_const(const void * p)360 static void *remove_const(const void *p)
361 {
362 /* Note: this casts away const, in order to use the common field iterator
363 * logic for both encoding and decoding. */
364 union {
365 void *p1;
366 const void *p2;
367 } t;
368 t.p2 = p;
369 return t.p1;
370 }
371
pb_encode(pb_ostream_t * stream,const pb_field_t fields[],const void * src_struct)372 bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
373 {
374 pb_field_iter_t iter;
375 if (!pb_field_iter_begin(&iter, fields, remove_const(src_struct)))
376 return true; /* Empty message type */
377
378 do {
379 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_EXTENSION)
380 {
381 /* Special case for the extension field placeholder */
382 if (!encode_extension_field(stream, iter.pos, iter.pData))
383 return false;
384 }
385 else
386 {
387 /* Regular field */
388 if (!encode_field(stream, iter.pos, iter.pData))
389 return false;
390 }
391 } while (pb_field_iter_next(&iter));
392
393 return true;
394 }
395
pb_encode_delimited(pb_ostream_t * stream,const pb_field_t fields[],const void * src_struct)396 bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
397 {
398 return pb_encode_submessage(stream, fields, src_struct);
399 }
400
pb_get_encoded_size(size_t * size,const pb_field_t fields[],const void * src_struct)401 bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct)
402 {
403 pb_ostream_t stream = PB_OSTREAM_SIZING;
404
405 if (!pb_encode(&stream, fields, src_struct))
406 return false;
407
408 *size = stream.bytes_written;
409 return true;
410 }
411
412 /********************
413 * Helper functions *
414 ********************/
pb_encode_varint(pb_ostream_t * stream,uint64_t value)415 bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
416 {
417 pb_byte_t buffer[10];
418 size_t i = 0;
419
420 if (value <= 0x7F)
421 {
422 pb_byte_t v = (pb_byte_t)value;
423 return pb_write(stream, &v, 1);
424 }
425
426 while (value)
427 {
428 buffer[i] = (pb_byte_t)((value & 0x7F) | 0x80);
429 value >>= 7;
430 i++;
431 }
432 buffer[i-1] &= 0x7F; /* Unset top bit on last byte */
433
434 return pb_write(stream, buffer, i);
435 }
436
pb_encode_svarint(pb_ostream_t * stream,int64_t value)437 bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
438 {
439 uint64_t zigzagged;
440 if (value < 0)
441 zigzagged = ~((uint64_t)value << 1);
442 else
443 zigzagged = (uint64_t)value << 1;
444
445 return pb_encode_varint(stream, zigzagged);
446 }
447
pb_encode_fixed32(pb_ostream_t * stream,const void * value)448 bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
449 {
450 uint32_t val = *(const uint32_t*)value;
451 pb_byte_t bytes[4];
452 bytes[0] = (pb_byte_t)(val & 0xFF);
453 bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
454 bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
455 bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
456 return pb_write(stream, bytes, 4);
457 }
458
pb_encode_fixed64(pb_ostream_t * stream,const void * value)459 bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
460 {
461 uint64_t val = *(const uint64_t*)value;
462 pb_byte_t bytes[8];
463 bytes[0] = (pb_byte_t)(val & 0xFF);
464 bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
465 bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
466 bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
467 bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
468 bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
469 bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
470 bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
471 return pb_write(stream, bytes, 8);
472 }
473
pb_encode_tag(pb_ostream_t * stream,pb_wire_type_t wiretype,uint32_t field_number)474 bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
475 {
476 uint64_t tag = ((uint64_t)field_number << 3) | wiretype;
477 return pb_encode_varint(stream, tag);
478 }
479
pb_encode_tag_for_field(pb_ostream_t * stream,const pb_field_t * field)480 bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
481 {
482 pb_wire_type_t wiretype;
483 switch (PB_LTYPE(field->type))
484 {
485 case PB_LTYPE_VARINT:
486 case PB_LTYPE_UVARINT:
487 case PB_LTYPE_SVARINT:
488 wiretype = PB_WT_VARINT;
489 break;
490
491 case PB_LTYPE_FIXED32:
492 wiretype = PB_WT_32BIT;
493 break;
494
495 case PB_LTYPE_FIXED64:
496 wiretype = PB_WT_64BIT;
497 break;
498
499 case PB_LTYPE_BYTES:
500 case PB_LTYPE_STRING:
501 case PB_LTYPE_SUBMESSAGE:
502 case PB_LTYPE_FIXED_LENGTH_BYTES:
503 wiretype = PB_WT_STRING;
504 break;
505
506 default:
507 PB_RETURN_ERROR(stream, "invalid field type");
508 }
509
510 return pb_encode_tag(stream, wiretype, field->tag);
511 }
512
pb_encode_string(pb_ostream_t * stream,const pb_byte_t * buffer,size_t size)513 bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
514 {
515 if (!pb_encode_varint(stream, (uint64_t)size))
516 return false;
517
518 return pb_write(stream, buffer, size);
519 }
520
pb_encode_submessage(pb_ostream_t * stream,const pb_field_t fields[],const void * src_struct)521 bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
522 {
523 /* First calculate the message size using a non-writing substream. */
524 pb_ostream_t substream = PB_OSTREAM_SIZING;
525 size_t size;
526 bool status;
527
528 if (!pb_encode(&substream, fields, src_struct))
529 {
530 #ifndef PB_NO_ERRMSG
531 stream->errmsg = substream.errmsg;
532 #endif
533 return false;
534 }
535
536 size = substream.bytes_written;
537
538 if (!pb_encode_varint(stream, (uint64_t)size))
539 return false;
540
541 if (stream->callback == NULL)
542 return pb_write(stream, NULL, size); /* Just sizing */
543
544 if (stream->bytes_written + size > stream->max_size)
545 PB_RETURN_ERROR(stream, "stream full");
546
547 /* Use a substream to verify that a callback doesn't write more than
548 * what it did the first time. */
549 substream.callback = stream->callback;
550 substream.state = stream->state;
551 substream.max_size = size;
552 substream.bytes_written = 0;
553 #ifndef PB_NO_ERRMSG
554 substream.errmsg = NULL;
555 #endif
556
557 status = pb_encode(&substream, fields, src_struct);
558
559 stream->bytes_written += substream.bytes_written;
560 stream->state = substream.state;
561 #ifndef PB_NO_ERRMSG
562 stream->errmsg = substream.errmsg;
563 #endif
564
565 if (substream.bytes_written != size)
566 PB_RETURN_ERROR(stream, "submsg size changed");
567
568 return status;
569 }
570
571 /* Field encoders */
572
pb_enc_varint(pb_ostream_t * stream,const pb_field_t * field,const void * src)573 static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
574 {
575 int64_t value = 0;
576
577 if (field->data_size == sizeof(int_least8_t))
578 value = *(const int_least8_t*)src;
579 else if (field->data_size == sizeof(int_least16_t))
580 value = *(const int_least16_t*)src;
581 else if (field->data_size == sizeof(int32_t))
582 value = *(const int32_t*)src;
583 else if (field->data_size == sizeof(int64_t))
584 value = *(const int64_t*)src;
585 else
586 PB_RETURN_ERROR(stream, "invalid data_size");
587
588 return pb_encode_varint(stream, (uint64_t)value);
589 }
590
pb_enc_uvarint(pb_ostream_t * stream,const pb_field_t * field,const void * src)591 static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
592 {
593 uint64_t value = 0;
594
595 if (field->data_size == sizeof(uint_least8_t))
596 value = *(const uint_least8_t*)src;
597 else if (field->data_size == sizeof(uint_least16_t))
598 value = *(const uint_least16_t*)src;
599 else if (field->data_size == sizeof(uint32_t))
600 value = *(const uint32_t*)src;
601 else if (field->data_size == sizeof(uint64_t))
602 value = *(const uint64_t*)src;
603 else
604 PB_RETURN_ERROR(stream, "invalid data_size");
605
606 return pb_encode_varint(stream, value);
607 }
608
pb_enc_svarint(pb_ostream_t * stream,const pb_field_t * field,const void * src)609 static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
610 {
611 int64_t value = 0;
612
613 if (field->data_size == sizeof(int_least8_t))
614 value = *(const int_least8_t*)src;
615 else if (field->data_size == sizeof(int_least16_t))
616 value = *(const int_least16_t*)src;
617 else if (field->data_size == sizeof(int32_t))
618 value = *(const int32_t*)src;
619 else if (field->data_size == sizeof(int64_t))
620 value = *(const int64_t*)src;
621 else
622 PB_RETURN_ERROR(stream, "invalid data_size");
623
624 return pb_encode_svarint(stream, value);
625 }
626
pb_enc_fixed64(pb_ostream_t * stream,const pb_field_t * field,const void * src)627 static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
628 {
629 PB_UNUSED(field);
630 return pb_encode_fixed64(stream, src);
631 }
632
pb_enc_fixed32(pb_ostream_t * stream,const pb_field_t * field,const void * src)633 static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
634 {
635 PB_UNUSED(field);
636 return pb_encode_fixed32(stream, src);
637 }
638
pb_enc_bytes(pb_ostream_t * stream,const pb_field_t * field,const void * src)639 static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
640 {
641 const pb_bytes_array_t *bytes = NULL;
642
643 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES)
644 return pb_encode_string(stream, (const pb_byte_t*)src, field->data_size);
645
646 bytes = (const pb_bytes_array_t*)src;
647
648 if (src == NULL)
649 {
650 /* Treat null pointer as an empty bytes field */
651 return pb_encode_string(stream, NULL, 0);
652 }
653
654 if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
655 PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size)
656 {
657 PB_RETURN_ERROR(stream, "bytes size exceeded");
658 }
659
660 return pb_encode_string(stream, bytes->bytes, bytes->size);
661 }
662
pb_enc_string(pb_ostream_t * stream,const pb_field_t * field,const void * src)663 static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
664 {
665 size_t size = 0;
666 size_t max_size = field->data_size;
667 const char *p = (const char*)src;
668
669 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
670 max_size = (size_t)-1;
671
672 if (src == NULL)
673 {
674 size = 0; /* Treat null pointer as an empty string */
675 }
676 else
677 {
678 /* strnlen() is not always available, so just use a loop */
679 while (size < max_size && *p != '\0')
680 {
681 size++;
682 p++;
683 }
684 }
685
686 return pb_encode_string(stream, (const pb_byte_t*)src, size);
687 }
688
pb_enc_submessage(pb_ostream_t * stream,const pb_field_t * field,const void * src)689 static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
690 {
691 if (field->ptr == NULL)
692 PB_RETURN_ERROR(stream, "invalid field descriptor");
693
694 return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
695 }
696
697