Coverage Report

Created: 2025-12-31 06:39

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