Coverage Report

Created: 2026-09-03 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nanopb/pb_decode.c
Line
Count
Source
1
/* pb_decode.c -- decode a protobuf using minimal resources
2
 *
3
 * 2011 Petteri Aimonen <jpa@kapsi.fi>
4
 */
5
6
/* Use the GCC warn_unused_result attribute to check that all return values
7
 * are propagated correctly. On other compilers, gcc before 3.4.0 and iar
8
 * before 9.40.1 just ignore the annotation.
9
 */
10
#if (defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))) || \
11
    (defined(__IAR_SYSTEMS_ICC__) && (__VER__ >= 9040001))
12
    #define checkreturn __attribute__((warn_unused_result))
13
#else
14
    #define checkreturn
15
#endif
16
17
#include "pb.h"
18
#include "pb_decode.h"
19
#include "pb_common.h"
20
21
/**************************************
22
 * Declarations internal to this file *
23
 **************************************/
24
25
static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
26
static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size);
27
static bool checkreturn decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
28
static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
29
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
30
static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
31
static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
32
static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
33
static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension);
34
static bool pb_field_set_to_default(pb_field_iter_t *field);
35
static bool pb_message_set_to_defaults(pb_field_iter_t *iter);
36
static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field);
37
static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field);
38
static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field);
39
static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field);
40
static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field);
41
static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field);
42
static bool checkreturn pb_skip_varint(pb_istream_t *stream);
43
static bool checkreturn pb_skip_string(pb_istream_t *stream);
44
45
#ifdef PB_ENABLE_MALLOC
46
static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
47
static void initialize_pointer_field(void *pItem, pb_field_iter_t *field);
48
static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field);
49
static void pb_release_single_field(pb_field_iter_t *field);
50
#endif
51
52
#ifdef PB_WITHOUT_64BIT
53
#define pb_int64_t int32_t
54
#define pb_uint64_t uint32_t
55
#else
56
10.5M
#define pb_int64_t int64_t
57
5.83M
#define pb_uint64_t uint64_t
58
#endif
59
60
typedef struct {
61
    uint32_t bitfield[(PB_MAX_REQUIRED_FIELDS + 31) / 32];
62
} pb_fields_seen_t;
63
64
/*******************************
65
 * pb_istream_t implementation *
66
 *******************************/
67
68
static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
69
16.0M
{
70
16.0M
    const pb_byte_t *source = (const pb_byte_t*)stream->state;
71
16.0M
    stream->state = (pb_byte_t*)stream->state + count;
72
    
73
16.0M
    if (buf != NULL)
74
15.8M
    {
75
15.8M
        memcpy(buf, source, count * sizeof(pb_byte_t));
76
15.8M
    }
77
    
78
16.0M
    return true;
79
16.0M
}
80
81
bool checkreturn pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
82
2.91M
{
83
2.91M
    if (count == 0)
84
200k
        return true;
85
86
2.71M
#ifndef PB_BUFFER_ONLY
87
2.71M
  if (buf == NULL && stream->callback != buf_read)
88
78.7k
  {
89
    /* Skip input bytes */
90
78.7k
    pb_byte_t tmp[16];
91
571k
    while (count > 16)
92
492k
    {
93
492k
      if (!pb_read(stream, tmp, 16))
94
0
        return false;
95
      
96
492k
      count -= 16;
97
492k
    }
98
    
99
78.7k
    return pb_read(stream, tmp, count);
100
78.7k
  }
101
2.63M
#endif
102
103
2.63M
    if (stream->bytes_left < count)
104
264
        PB_RETURN_ERROR(stream, "end-of-stream");
105
    
106
2.63M
#ifndef PB_BUFFER_ONLY
107
2.63M
    if (!stream->callback(stream, buf, count))
108
0
        PB_RETURN_ERROR(stream, "io error");
109
#else
110
    if (!buf_read(stream, buf, count))
111
        return false;
112
#endif
113
    
114
2.63M
    if (stream->bytes_left < count)
115
0
        stream->bytes_left = 0;
116
2.63M
    else
117
2.63M
        stream->bytes_left -= count;
118
119
2.63M
    return true;
120
2.63M
}
121
122
/* Read a single byte from input stream. buf may not be NULL.
123
 * This is an optimization for the varint decoding. */
124
static bool checkreturn pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
125
18.4M
{
126
18.4M
    if (stream->bytes_left == 0)
127
247
        PB_RETURN_ERROR(stream, "end-of-stream");
128
129
18.4M
#ifndef PB_BUFFER_ONLY
130
18.4M
    if (!stream->callback(stream, buf, 1))
131
1.32k
        PB_RETURN_ERROR(stream, "io error");
132
#else
133
    *buf = *(const pb_byte_t*)stream->state;
134
    stream->state = (pb_byte_t*)stream->state + 1;
135
#endif
136
137
18.4M
    stream->bytes_left--;
138
    
139
18.4M
    return true;    
140
18.4M
}
141
142
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
143
132k
{
144
132k
    pb_istream_t stream;
145
    /* Cast away the const from buf without a compiler error.  We are
146
     * careful to use it only in a const manner in the callbacks.
147
     */
148
132k
    union {
149
132k
        void *state;
150
132k
        const void *c_state;
151
132k
    } state;
152
#ifdef PB_BUFFER_ONLY
153
    stream.callback = NULL;
154
#else
155
132k
    stream.callback = &buf_read;
156
132k
#endif
157
132k
    state.c_state = buf;
158
132k
    stream.state = state.state;
159
132k
    stream.bytes_left = msglen;
160
132k
#ifndef PB_NO_ERRMSG
161
132k
    stream.errmsg = NULL;
162
132k
#endif
163
#ifdef PB_MESSAGE_NESTING_MAX
164
    stream.depth = 0;
165
#endif
166
132k
    return stream;
167
132k
}
168
169
170
/********************
171
 * Helper functions *
172
 ********************/
173
174
bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
175
9.78M
{
176
9.78M
    pb_byte_t byte;
177
9.78M
    uint32_t result;
178
    
179
9.78M
    if (!pb_readbyte(stream, &byte))
180
1.47k
    {
181
1.47k
        return false;
182
1.47k
    }
183
    
184
9.78M
    if ((byte & 0x80) == 0)
185
8.43M
    {
186
        /* Quick case, 1 byte value */
187
8.43M
        result = byte;
188
8.43M
    }
189
1.34M
    else
190
1.34M
    {
191
        /* Multibyte case */
192
1.34M
        uint_fast8_t bitpos = 7;
193
1.34M
        result = byte & 0x7F;
194
        
195
1.34M
        do
196
1.69M
        {
197
1.69M
            if (!pb_readbyte(stream, &byte))
198
47
                return false;
199
            
200
1.69M
            if (bitpos >= 32)
201
18.6k
            {
202
                /* Note: The varint could have trailing 0x80 bytes, or 0xFF for negative. */
203
18.6k
                pb_byte_t sign_extension = (bitpos < 63) ? 0xFF : 0x01;
204
18.6k
                bool valid_extension = ((byte & 0x7F) == 0x00 ||
205
13.9k
                         ((result >> 31) != 0 && byte == sign_extension));
206
207
18.6k
                if (bitpos >= 64 || !valid_extension)
208
71
                {
209
71
                    PB_RETURN_ERROR(stream, "varint overflow");
210
71
                }
211
18.6k
            }
212
1.67M
            else if (bitpos == 28)
213
11.9k
            {
214
11.9k
                if ((byte & 0x70) != 0 && (byte & 0x78) != 0x78)
215
15
                {
216
15
                    PB_RETURN_ERROR(stream, "varint overflow");
217
15
                }
218
11.9k
                result |= (uint32_t)(byte & 0x0F) << bitpos;
219
11.9k
            }
220
1.66M
            else
221
1.66M
            {
222
1.66M
                result |= (uint32_t)(byte & 0x7F) << bitpos;
223
1.66M
            }
224
1.69M
            bitpos = (uint_fast8_t)(bitpos + 7);
225
1.69M
        } while (byte & 0x80);
226
1.34M
   }
227
   
228
9.78M
   *dest = result;
229
9.78M
   return true;
230
9.78M
}
231
232
#ifndef PB_WITHOUT_64BIT
233
bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
234
5.65M
{
235
5.65M
    pb_byte_t byte;
236
5.65M
    uint_fast8_t bitpos = 0;
237
5.65M
    uint64_t result = 0;
238
    
239
5.65M
    do
240
6.94M
    {
241
6.94M
        if (!pb_readbyte(stream, &byte))
242
56
            return false;
243
244
6.94M
        if (bitpos >= 63 && (byte & 0xFE) != 0)
245
31
            PB_RETURN_ERROR(stream, "varint overflow");
246
247
6.94M
        result |= (uint64_t)(byte & 0x7F) << bitpos;
248
6.94M
        bitpos = (uint_fast8_t)(bitpos + 7);
249
6.94M
    } while (byte & 0x80);
250
    
251
5.65M
    *dest = result;
252
5.65M
    return true;
253
5.65M
}
254
#endif
255
256
bool checkreturn pb_skip_varint(pb_istream_t *stream)
257
797k
{
258
797k
    pb_byte_t byte;
259
797k
    do
260
1.00M
    {
261
1.00M
        if (!pb_read(stream, &byte, 1))
262
68
            return false;
263
1.00M
    } while (byte & 0x80);
264
797k
    return true;
265
797k
}
266
267
bool checkreturn pb_skip_string(pb_istream_t *stream)
268
185k
{
269
185k
    uint32_t length;
270
185k
    if (!pb_decode_varint32(stream, &length))
271
54
        return false;
272
    
273
185k
    if ((size_t)length != length)
274
0
    {
275
0
        PB_RETURN_ERROR(stream, "size too large");
276
0
    }
277
278
185k
    return pb_read(stream, NULL, (size_t)length);
279
185k
}
280
281
bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
282
8.86M
{
283
8.86M
    uint32_t temp;
284
8.86M
    *eof = false;
285
8.86M
    *wire_type = (pb_wire_type_t) 0;
286
8.86M
    *tag = 0;
287
288
8.86M
    if (stream->bytes_left == 0)
289
298k
    {
290
298k
        *eof = true;
291
298k
        return false;
292
298k
    }
293
294
8.56M
    if (!pb_decode_varint32(stream, &temp))
295
1.44k
    {
296
1.44k
#ifndef PB_BUFFER_ONLY
297
        /* Workaround for issue #1017
298
         *
299
         * Callback streams don't set bytes_left to 0 on eof until after being called by pb_decode_varint32,
300
         * which results in "io error" being raised. This contrasts the behavior of buffer streams who raise
301
         * no error on eof as bytes_left is already 0 on entry. This causes legitimate errors (e.g. missing
302
         * required fields) to be incorrectly reported by callback streams.
303
         */
304
1.44k
        if (stream->callback != buf_read && stream->bytes_left == 0)
305
1.32k
        {
306
1.32k
#ifndef PB_NO_ERRMSG
307
1.32k
            if (strcmp(stream->errmsg, "io error") == 0)
308
1.32k
                stream->errmsg = NULL;
309
1.32k
#endif
310
1.32k
            *eof = true;
311
1.32k
        }
312
1.44k
#endif
313
1.44k
        return false;
314
1.44k
    }
315
    
316
8.56M
    *tag = temp >> 3;
317
8.56M
    *wire_type = (pb_wire_type_t)(temp & 7);
318
8.56M
    return true;
319
8.56M
}
320
321
bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
322
1.20M
{
323
1.20M
    switch (wire_type)
324
1.20M
    {
325
797k
        case PB_WT_VARINT: return pb_skip_varint(stream);
326
83.3k
        case PB_WT_64BIT: return pb_read(stream, NULL, 8);
327
185k
        case PB_WT_STRING: return pb_skip_string(stream);
328
136k
        case PB_WT_32BIT: return pb_read(stream, NULL, 4);
329
0
  case PB_WT_PACKED: 
330
            /* Calling pb_skip_field with a PB_WT_PACKED is an error.
331
             * Explicitly handle this case and fallthrough to default to avoid
332
             * compiler warnings.
333
             */
334
95
        default: PB_RETURN_ERROR(stream, "invalid wire_type");
335
1.20M
    }
336
1.20M
}
337
338
/* Read a raw value to buffer, for the purpose of passing it to callback as
339
 * a substream. Size is maximum size on call, and actual size on return.
340
 */
341
static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
342
18.3k
{
343
18.3k
    size_t max_size = *size;
344
18.3k
    switch (wire_type)
345
18.3k
    {
346
12.5k
        case PB_WT_VARINT:
347
12.5k
            *size = 0;
348
12.5k
            do
349
29.8k
            {
350
29.8k
                (*size)++;
351
29.8k
                if (*size > max_size)
352
3
                    PB_RETURN_ERROR(stream, "varint overflow");
353
354
29.8k
                if (!pb_read(stream, buf, 1))
355
24
                    return false;
356
29.8k
            } while (*buf++ & 0x80);
357
12.5k
            return true;
358
            
359
4.79k
        case PB_WT_64BIT:
360
4.79k
            *size = 8;
361
4.79k
            return pb_read(stream, buf, 8);
362
        
363
942
        case PB_WT_32BIT:
364
942
            *size = 4;
365
942
            return pb_read(stream, buf, 4);
366
        
367
0
        case PB_WT_STRING:
368
            /* Calling read_raw_value with a PB_WT_STRING is an error.
369
             * Explicitly handle this case and fallthrough to default to avoid
370
             * compiler warnings.
371
             */
372
373
0
  case PB_WT_PACKED: 
374
            /* Calling read_raw_value with a PB_WT_PACKED is an error.
375
             * Explicitly handle this case and fallthrough to default to avoid
376
             * compiler warnings.
377
             */
378
379
3
        default: PB_RETURN_ERROR(stream, "invalid wire_type");
380
18.3k
    }
381
18.3k
}
382
383
/* Decode string length from stream and return a substream with limited length.
384
 * Remember to close the substream using pb_close_string_substream().
385
 */
386
bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
387
395k
{
388
395k
    uint32_t size;
389
395k
    if (!pb_decode_varint32(stream, &size))
390
76
        return false;
391
    
392
395k
    *substream = *stream;
393
395k
    if (substream->bytes_left < size)
394
128
        PB_RETURN_ERROR(stream, "parent stream too short");
395
    
396
#ifdef PB_MESSAGE_NESTING_MAX
397
    substream->depth++;
398
    if (substream->depth > PB_MESSAGE_NESTING_MAX)
399
        PB_RETURN_ERROR(stream, "max depth");
400
#endif
401
402
395k
    substream->bytes_left = (size_t)size;
403
395k
    stream->bytes_left -= (size_t)size;
404
395k
    return true;
405
395k
}
406
407
bool checkreturn pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
408
395k
{
409
395k
    if (substream->bytes_left) {
410
24.8k
        if (!pb_read(substream, NULL, substream->bytes_left))
411
0
            return false;
412
24.8k
    }
413
414
395k
    stream->state = substream->state;
415
416
395k
#ifndef PB_NO_ERRMSG
417
395k
    stream->errmsg = substream->errmsg;
418
395k
#endif
419
395k
    return true;
420
395k
}
421
422
/*************************
423
 * Decode a single field *
424
 *************************/
425
426
static bool checkreturn decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
427
7.19M
{
428
7.19M
    switch (PB_LTYPE(field->type))
429
7.19M
    {
430
342k
        case PB_LTYPE_BOOL:
431
342k
            if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED)
432
7
                PB_RETURN_ERROR(stream, "wrong wire type");
433
434
342k
            return pb_dec_bool(stream, field);
435
436
5.08M
        case PB_LTYPE_VARINT:
437
5.47M
        case PB_LTYPE_UVARINT:
438
5.65M
        case PB_LTYPE_SVARINT:
439
5.65M
            if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED)
440
53
                PB_RETURN_ERROR(stream, "wrong wire type");
441
442
5.65M
            return pb_dec_varint(stream, field);
443
444
317k
        case PB_LTYPE_FIXED32:
445
317k
            if (wire_type != PB_WT_32BIT && wire_type != PB_WT_PACKED)
446
15
                PB_RETURN_ERROR(stream, "wrong wire type");
447
448
317k
            return pb_decode_fixed32(stream, field->pData);
449
450
262k
        case PB_LTYPE_FIXED64:
451
262k
            if (wire_type != PB_WT_64BIT && wire_type != PB_WT_PACKED)
452
12
                PB_RETURN_ERROR(stream, "wrong wire type");
453
454
#ifdef PB_CONVERT_DOUBLE_FLOAT
455
            if (field->data_size == sizeof(float))
456
            {
457
                return pb_decode_double_as_float(stream, (float*)field->pData);
458
            }
459
#endif
460
461
#ifdef PB_WITHOUT_64BIT
462
            PB_RETURN_ERROR(stream, "invalid data_size");
463
#else
464
262k
            return pb_decode_fixed64(stream, field->pData);
465
0
#endif
466
467
50.0k
        case PB_LTYPE_BYTES:
468
50.0k
            if (wire_type != PB_WT_STRING)
469
14
                PB_RETURN_ERROR(stream, "wrong wire type");
470
471
49.9k
            return pb_dec_bytes(stream, field);
472
473
218k
        case PB_LTYPE_STRING:
474
218k
            if (wire_type != PB_WT_STRING)
475
8
                PB_RETURN_ERROR(stream, "wrong wire type");
476
477
218k
            return pb_dec_string(stream, field);
478
479
273k
        case PB_LTYPE_SUBMESSAGE:
480
320k
        case PB_LTYPE_SUBMSG_W_CB:
481
320k
            if (wire_type != PB_WT_STRING)
482
15
                PB_RETURN_ERROR(stream, "wrong wire type");
483
484
319k
            return pb_dec_submessage(stream, field);
485
486
28.9k
        case PB_LTYPE_FIXED_LENGTH_BYTES:
487
28.9k
            if (wire_type != PB_WT_STRING)
488
3
                PB_RETURN_ERROR(stream, "wrong wire type");
489
490
28.9k
            return pb_dec_fixed_length_bytes(stream, field);
491
492
0
        default:
493
0
            PB_RETURN_ERROR(stream, "invalid field type");
494
7.19M
    }
495
7.19M
}
496
497
static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
498
7.16M
{
499
7.16M
    switch (PB_HTYPE(field->type))
500
7.16M
    {
501
6.41M
        case PB_HTYPE_REQUIRED:
502
6.41M
            return decode_basic_field(stream, wire_type, field);
503
            
504
520k
        case PB_HTYPE_OPTIONAL:
505
520k
            if (field->pSize != NULL)
506
520k
                *(bool*)field->pSize = true;
507
520k
            return decode_basic_field(stream, wire_type, field);
508
    
509
32.1k
        case PB_HTYPE_REPEATED:
510
32.1k
            if (wire_type == PB_WT_STRING
511
22.8k
                && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
512
10.6k
            {
513
                /* Packed array */
514
10.6k
                bool status = true;
515
10.6k
                pb_istream_t substream;
516
10.6k
                pb_size_t *size = (pb_size_t*)field->pSize;
517
10.6k
                field->pData = (char*)field->pField + field->data_size * (*size);
518
519
10.6k
                if (!pb_make_string_substream(stream, &substream))
520
68
                    return false;
521
522
46.1k
                while (substream.bytes_left > 0 && *size < field->array_size)
523
35.6k
                {
524
35.6k
                    if (!decode_basic_field(&substream, PB_WT_PACKED, field))
525
30
                    {
526
30
                        status = false;
527
30
                        break;
528
30
                    }
529
35.6k
                    (*size)++;
530
35.6k
                    field->pData = (char*)field->pData + field->data_size;
531
35.6k
                }
532
533
10.5k
                if (substream.bytes_left != 0)
534
46
                    PB_RETURN_ERROR(stream, "array overflow");
535
10.5k
                if (!pb_close_string_substream(stream, &substream))
536
0
                    return false;
537
538
10.5k
                return status;
539
10.5k
            }
540
21.5k
            else
541
21.5k
            {
542
                /* Repeated field */
543
21.5k
                pb_size_t *size = (pb_size_t*)field->pSize;
544
21.5k
                field->pData = (char*)field->pField + field->data_size * (*size);
545
546
21.5k
                if ((*size)++ >= field->array_size)
547
2
                    PB_RETURN_ERROR(stream, "array overflow");
548
549
21.5k
                return decode_basic_field(stream, wire_type, field);
550
21.5k
            }
551
552
205k
        case PB_HTYPE_ONEOF:
553
205k
            if (PB_LTYPE_IS_SUBMSG(field->type) &&
554
205k
                *(pb_size_t*)field->pSize != field->tag)
555
118k
            {
556
                /* We memset to zero so that any callbacks are set to NULL.
557
                 * This is because the callbacks might otherwise have values
558
                 * from some other union field.
559
                 * If callbacks are needed inside oneof field, use .proto
560
                 * option submsg_callback to have a separate callback function
561
                 * that can set the fields before submessage is decoded.
562
                 * pb_dec_submessage() will set any default values. */
563
118k
                memset(field->pData, 0, (size_t)field->data_size);
564
565
                /* Set default values for the submessage fields. */
566
118k
                if (field->submsg_desc->default_value != NULL ||
567
49.5k
                    field->submsg_desc->field_callback != NULL ||
568
49.5k
                    field->submsg_desc->submsg_info[0] != NULL)
569
68.7k
                {
570
68.7k
                    pb_field_iter_t submsg_iter;
571
68.7k
                    if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData))
572
68.7k
                    {
573
68.7k
                        if (!pb_message_set_to_defaults(&submsg_iter))
574
0
                            PB_RETURN_ERROR(stream, "failed to set defaults");
575
68.7k
                    }
576
68.7k
                }
577
118k
            }
578
205k
            *(pb_size_t*)field->pSize = field->tag;
579
580
205k
            return decode_basic_field(stream, wire_type, field);
581
582
0
        default:
583
0
            PB_RETURN_ERROR(stream, "invalid field type");
584
7.16M
    }
585
7.16M
}
586
587
#ifdef PB_ENABLE_MALLOC
588
/* Allocate storage for the field and store the pointer at iter->pData.
589
 * array_size is the number of entries to reserve in an array.
590
 * Zero size is not allowed, use pb_free() for releasing.
591
 */
592
static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
593
{    
594
    void *ptr = *(void**)pData;
595
    
596
    if (data_size == 0 || array_size == 0)
597
        PB_RETURN_ERROR(stream, "invalid size");
598
    
599
#ifdef __AVR__
600
    /* Workaround for AVR libc bug 53284: http://savannah.nongnu.org/bugs/?53284
601
     * Realloc to size of 1 byte can cause corruption of the malloc structures.
602
     */
603
    if (data_size == 1 && array_size == 1)
604
    {
605
        data_size = 2;
606
    }
607
#endif
608
609
    /* Check for multiplication overflows.
610
     * This code avoids the costly division if the sizes are small enough.
611
     * Multiplication is safe as long as only half of bits are set
612
     * in either multiplicand.
613
     */
614
    {
615
        const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
616
        if (data_size >= check_limit || array_size >= check_limit)
617
        {
618
            const size_t size_max = (size_t)-1;
619
            if (size_max / array_size < data_size)
620
            {
621
                PB_RETURN_ERROR(stream, "size too large");
622
            }
623
        }
624
    }
625
    
626
    /* Allocate new or expand previous allocation */
627
    /* Note: on failure the old pointer will remain in the structure,
628
     * the message must be freed by caller also on error return. */
629
    ptr = pb_realloc(ptr, array_size * data_size);
630
    if (ptr == NULL)
631
        PB_RETURN_ERROR(stream, "realloc failed");
632
    
633
    *(void**)pData = ptr;
634
    return true;
635
}
636
637
/* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
638
static void initialize_pointer_field(void *pItem, pb_field_iter_t *field)
639
{
640
    if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
641
        PB_LTYPE(field->type) == PB_LTYPE_BYTES)
642
    {
643
        *(void**)pItem = NULL;
644
    }
645
    else if (PB_LTYPE_IS_SUBMSG(field->type))
646
    {
647
        /* We memset to zero so that any callbacks are set to NULL.
648
         * Default values will be set by pb_dec_submessage(). */
649
        memset(pItem, 0, field->data_size);
650
    }
651
}
652
#endif
653
654
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
655
0
{
656
0
#ifndef PB_ENABLE_MALLOC
657
0
    PB_UNUSED(wire_type);
658
0
    PB_UNUSED(field);
659
0
    PB_RETURN_ERROR(stream, "no malloc support");
660
#else
661
    switch (PB_HTYPE(field->type))
662
    {
663
        case PB_HTYPE_REQUIRED:
664
        case PB_HTYPE_OPTIONAL:
665
        case PB_HTYPE_ONEOF:
666
            if (PB_LTYPE_IS_SUBMSG(field->type) && *(void**)field->pField != NULL)
667
            {
668
                /* Duplicate field, have to release the old allocation first. */
669
                /* FIXME: Does this work correctly for oneofs? */
670
                pb_release_single_field(field);
671
            }
672
        
673
            if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
674
            {
675
                *(pb_size_t*)field->pSize = field->tag;
676
            }
677
678
            if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
679
                PB_LTYPE(field->type) == PB_LTYPE_BYTES)
680
            {
681
                /* pb_dec_string and pb_dec_bytes handle allocation themselves */
682
                field->pData = field->pField;
683
                return decode_basic_field(stream, wire_type, field);
684
            }
685
            else
686
            {
687
                if (!allocate_field(stream, field->pField, field->data_size, 1))
688
                    return false;
689
                
690
                field->pData = *(void**)field->pField;
691
                initialize_pointer_field(field->pData, field);
692
                return decode_basic_field(stream, wire_type, field);
693
            }
694
    
695
        case PB_HTYPE_REPEATED:
696
            if (wire_type == PB_WT_STRING
697
                && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
698
            {
699
                /* Packed array, multiple items come in at once. */
700
                bool status = true;
701
                pb_size_t *size = (pb_size_t*)field->pSize;
702
                size_t allocated_size = *size;
703
                pb_istream_t substream;
704
                
705
                if (!pb_make_string_substream(stream, &substream))
706
                    return false;
707
                
708
                while (substream.bytes_left)
709
                {
710
                    if (*size == PB_SIZE_MAX)
711
                    {
712
#ifndef PB_NO_ERRMSG
713
                        stream->errmsg = "too many array entries";
714
#endif
715
                        status = false;
716
                        break;
717
                    }
718
719
                    if ((size_t)*size + 1 > allocated_size)
720
                    {
721
                        /* Allocate more storage. This tries to guess the
722
                         * number of remaining entries. Round the division
723
                         * upwards. */
724
                        size_t remain = (substream.bytes_left - 1) / field->data_size + 1;
725
                        if (remain < PB_SIZE_MAX - allocated_size)
726
                            allocated_size += remain;
727
                        else
728
                            allocated_size += 1;
729
                        
730
                        if (!allocate_field(&substream, field->pField, field->data_size, allocated_size))
731
                        {
732
                            status = false;
733
                            break;
734
                        }
735
                    }
736
737
                    /* Decode the array entry */
738
                    field->pData = *(char**)field->pField + field->data_size * (*size);
739
                    if (field->pData == NULL)
740
                    {
741
                        /* Shouldn't happen, but satisfies static analyzers */
742
                        status = false;
743
                        break;
744
                    }
745
                    initialize_pointer_field(field->pData, field);
746
                    if (!decode_basic_field(&substream, PB_WT_PACKED, field))
747
                    {
748
                        status = false;
749
                        break;
750
                    }
751
                    
752
                    (*size)++;
753
                }
754
                if (!pb_close_string_substream(stream, &substream))
755
                    return false;
756
                
757
                return status;
758
            }
759
            else
760
            {
761
                /* Normal repeated field, i.e. only one item at a time. */
762
                pb_size_t *size = (pb_size_t*)field->pSize;
763
764
                if (*size == PB_SIZE_MAX)
765
                    PB_RETURN_ERROR(stream, "too many array entries");
766
                
767
                if (!allocate_field(stream, field->pField, field->data_size, (size_t)(*size + 1)))
768
                    return false;
769
            
770
                field->pData = *(char**)field->pField + field->data_size * (*size);
771
                (*size)++;
772
                initialize_pointer_field(field->pData, field);
773
                return decode_basic_field(stream, wire_type, field);
774
            }
775
776
        default:
777
            PB_RETURN_ERROR(stream, "invalid field type");
778
    }
779
#endif
780
0
}
781
782
static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
783
83.0k
{
784
    /* Clear any data that may have been decoded for another oneof field
785
     * that has come before this callback field.
786
     */
787
83.0k
    if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
788
11.9k
    {
789
11.9k
        if (*(pb_size_t*)field->pSize != 0 && *(pb_size_t*)field->pSize != field->tag)
790
1.58k
        {
791
1.58k
            memset(field->pData, 0, (size_t)field->data_size);
792
1.58k
        }
793
11.9k
        *(pb_size_t*)field->pSize = field->tag;
794
11.9k
    }
795
796
83.0k
    if (!field->descriptor->field_callback)
797
0
        return pb_skip_field(stream, wire_type);
798
799
83.0k
    if (wire_type == PB_WT_STRING)
800
64.7k
    {
801
64.7k
        pb_istream_t substream;
802
64.7k
        size_t prev_bytes_left;
803
        
804
64.7k
        if (!pb_make_string_substream(stream, &substream))
805
39
            return false;
806
807
        /* If the callback field is inside a submsg, first call the submsg_callback which
808
         * should set the decoder for the callback field. */
809
64.6k
        if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL) {
810
0
            pb_callback_t* callback;
811
0
            *(pb_size_t*)field->pSize = field->tag;
812
0
            callback = (pb_callback_t*)field->pSize - 1;
813
814
0
            if (callback->funcs.decode)
815
0
            {
816
0
                if (!callback->funcs.decode(&substream, field, &callback->arg)) {
817
0
                    PB_SET_ERROR(stream, substream.errmsg ? substream.errmsg : "submsg callback failed");
818
0
                    return false;
819
0
                }
820
0
            }
821
0
        }
822
        
823
64.6k
        do
824
64.6k
        {
825
64.6k
            prev_bytes_left = substream.bytes_left;
826
64.6k
            if (!field->descriptor->field_callback(&substream, NULL, field))
827
0
            {
828
0
                PB_SET_ERROR(stream, substream.errmsg ? substream.errmsg : "callback failed");
829
0
                return false;
830
0
            }
831
64.6k
        } while (substream.bytes_left > 0 && substream.bytes_left < prev_bytes_left);
832
        
833
64.6k
        if (!pb_close_string_substream(stream, &substream))
834
0
            return false;
835
836
64.6k
        return true;
837
64.6k
    }
838
18.3k
    else
839
18.3k
    {
840
        /* Copy the single scalar value to stack.
841
         * This is required so that we can limit the stream length,
842
         * which in turn allows to use same callback for packed and
843
         * not-packed fields. */
844
18.3k
        pb_istream_t substream;
845
18.3k
        pb_byte_t buffer[10];
846
18.3k
        size_t size = sizeof(buffer);
847
        
848
18.3k
        if (!read_raw_value(stream, wire_type, buffer, &size))
849
45
            return false;
850
18.2k
        substream = pb_istream_from_buffer(buffer, size);
851
        
852
18.2k
        return field->descriptor->field_callback(&substream, NULL, field);
853
18.3k
    }
854
83.0k
}
855
856
static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
857
7.25M
{
858
#ifdef PB_ENABLE_MALLOC
859
    /* When decoding an oneof field, check if there is old data that must be
860
     * released first. */
861
    if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
862
    {
863
        if (!pb_release_union_field(stream, field))
864
            return false;
865
    }
866
#endif
867
868
7.25M
    switch (PB_ATYPE(field->type))
869
7.25M
    {
870
7.16M
        case PB_ATYPE_STATIC:
871
7.16M
            return decode_static_field(stream, wire_type, field);
872
        
873
0
        case PB_ATYPE_POINTER:
874
0
            return decode_pointer_field(stream, wire_type, field);
875
        
876
83.0k
        case PB_ATYPE_CALLBACK:
877
83.0k
            return decode_callback_field(stream, wire_type, field);
878
        
879
0
        default:
880
0
            PB_RETURN_ERROR(stream, "invalid field type");
881
7.25M
    }
882
7.25M
}
883
884
/* Default handler for extension fields. Expects to have a pb_msgdesc_t
885
 * pointer in the extension->type->arg field, pointing to a message with
886
 * only one field in it.  */
887
static bool checkreturn default_extension_decoder(pb_istream_t *stream,
888
    pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
889
137k
{
890
137k
    pb_field_iter_t iter;
891
892
137k
    if (!pb_field_iter_begin_extension(&iter, extension))
893
0
        PB_RETURN_ERROR(stream, "invalid extension");
894
895
137k
    if (iter.tag != tag || !iter.message)
896
131k
        return true;
897
898
6.15k
    extension->found = true;
899
6.15k
    return decode_field(stream, wire_type, &iter);
900
137k
}
901
902
/* Try to decode an unknown field as an extension field. Tries each extension
903
 * decoder in turn, until one of them handles the field or loop ends. */
904
static bool checkreturn decode_extension(pb_istream_t *stream,
905
    uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension)
906
137k
{
907
137k
    size_t pos = stream->bytes_left;
908
    
909
274k
    while (extension != NULL && pos == stream->bytes_left)
910
137k
    {
911
137k
        bool status;
912
137k
        if (extension->type->decode)
913
0
            status = extension->type->decode(stream, extension, tag, wire_type);
914
137k
        else
915
137k
            status = default_extension_decoder(stream, extension, tag, wire_type);
916
917
137k
        if (!status)
918
33
            return false;
919
        
920
137k
        extension = extension->next;
921
137k
    }
922
    
923
137k
    return true;
924
137k
}
925
926
/* Initialize message fields to default values, recursively */
927
static bool pb_field_set_to_default(pb_field_iter_t *field)
928
985k
{
929
985k
    pb_type_t type;
930
985k
    type = field->type;
931
932
985k
    if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
933
8.54k
    {
934
8.54k
        pb_extension_t *ext = *(pb_extension_t* const *)field->pData;
935
14.4k
        while (ext != NULL)
936
5.89k
        {
937
5.89k
            pb_field_iter_t ext_iter;
938
5.89k
            if (pb_field_iter_begin_extension(&ext_iter, ext))
939
5.89k
            {
940
5.89k
                ext->found = false;
941
5.89k
                if (!pb_message_set_to_defaults(&ext_iter))
942
0
                    return false;
943
5.89k
            }
944
5.89k
            ext = ext->next;
945
5.89k
        }
946
8.54k
    }
947
976k
    else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
948
903k
    {
949
903k
        bool init_data = true;
950
903k
        if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL)
951
256k
        {
952
            /* Set has_field to false. Still initialize the optional field
953
             * itself also. */
954
256k
            *(bool*)field->pSize = false;
955
256k
        }
956
646k
        else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
957
494k
                 PB_HTYPE(type) == PB_HTYPE_ONEOF)
958
194k
        {
959
            /* REPEATED: Set array count to 0, no need to initialize contents.
960
               ONEOF: Set which_field to 0. */
961
194k
            *(pb_size_t*)field->pSize = 0;
962
194k
            init_data = false;
963
194k
        }
964
965
903k
        if (init_data)
966
709k
        {
967
709k
            if (PB_LTYPE_IS_SUBMSG(field->type) &&
968
65.7k
                (field->submsg_desc->default_value != NULL ||
969
40.0k
                 field->submsg_desc->field_callback != NULL ||
970
40.0k
                 field->submsg_desc->submsg_info[0] != NULL))
971
25.6k
            {
972
                /* Initialize submessage to defaults.
973
                 * Only needed if it has default values
974
                 * or callback/submessage fields. */
975
25.6k
                pb_field_iter_t submsg_iter;
976
25.6k
                if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData))
977
25.6k
                {
978
25.6k
                    if (!pb_message_set_to_defaults(&submsg_iter))
979
0
                        return false;
980
25.6k
                }
981
25.6k
            }
982
683k
            else
983
683k
            {
984
                /* Initialize to zeros */
985
683k
                memset(field->pData, 0, (size_t)field->data_size);
986
683k
            }
987
709k
        }
988
903k
    }
989
73.1k
    else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
990
0
    {
991
        /* Initialize the pointer to NULL. */
992
0
        *(void**)field->pField = NULL;
993
994
        /* Initialize array count to 0. */
995
0
        if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
996
0
            PB_HTYPE(type) == PB_HTYPE_ONEOF)
997
0
        {
998
0
            *(pb_size_t*)field->pSize = 0;
999
0
        }
1000
0
    }
1001
73.1k
    else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
1002
73.1k
    {
1003
        /* Don't overwrite callback */
1004
73.1k
    }
1005
1006
985k
    return true;
1007
985k
}
1008
1009
static bool pb_message_set_to_defaults(pb_field_iter_t *iter)
1010
112k
{
1011
112k
    pb_istream_t defstream = PB_ISTREAM_EMPTY;
1012
112k
    uint32_t tag = 0;
1013
112k
    pb_wire_type_t wire_type = PB_WT_VARINT;
1014
112k
    bool eof;
1015
1016
112k
    if (iter->descriptor->default_value)
1017
106k
    {
1018
106k
        defstream = pb_istream_from_buffer(iter->descriptor->default_value, (size_t)-1);
1019
106k
        if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof))
1020
0
            return false;
1021
106k
    }
1022
1023
112k
    do
1024
985k
    {
1025
985k
        if (!pb_field_set_to_default(iter))
1026
0
            return false;
1027
1028
985k
        if (tag != 0 && iter->tag == tag)
1029
493k
        {
1030
            /* We have a default value for this field in the defstream */
1031
493k
            if (!decode_field(&defstream, wire_type, iter))
1032
0
                return false;
1033
493k
            if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof))
1034
0
                return false;
1035
1036
493k
            if (iter->pSize)
1037
239k
                *(bool*)iter->pSize = false;
1038
493k
        }
1039
985k
    } while (pb_field_iter_next(iter));
1040
1041
112k
    return true;
1042
112k
}
1043
1044
/*********************
1045
 * Decode all fields *
1046
 *********************/
1047
1048
static bool checkreturn pb_decode_inner(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
1049
301k
{
1050
    /* If the message contains extension fields, the extension handlers
1051
     * are called when tag number is >= extension_range_start. This precheck
1052
     * is just for speed, and the handlers will check for precise match.
1053
     */
1054
301k
    uint32_t extension_range_start = 0;
1055
301k
    pb_extension_t *extensions = NULL;
1056
1057
    /* 'fixed_count_field' and 'fixed_count_size' track position of a repeated fixed
1058
     * count field. This can only handle _one_ repeated fixed count field that
1059
     * is unpacked and unordered among other (non repeated fixed count) fields.
1060
     */
1061
301k
    pb_size_t fixed_count_field = PB_SIZE_MAX;
1062
301k
    pb_size_t fixed_count_size = 0;
1063
301k
    pb_size_t fixed_count_total_size = 0;
1064
1065
    /* Tag and wire type of next field from the input stream */
1066
301k
    uint32_t tag;
1067
301k
    pb_wire_type_t wire_type;
1068
301k
    bool eof;
1069
1070
    /* Track presence of required fields */
1071
301k
    pb_fields_seen_t fields_seen = {{0, 0}};
1072
301k
    const uint32_t allbits = ~(uint32_t)0;
1073
1074
    /* Descriptor for the structure field matching the tag decoded from stream */
1075
301k
    pb_field_iter_t iter;
1076
1077
301k
    if (pb_field_iter_begin(&iter, fields, dest_struct))
1078
164k
    {
1079
164k
        if ((flags & PB_DECODE_NOINIT) == 0)
1080
12.6k
        {
1081
12.6k
            if (!pb_message_set_to_defaults(&iter))
1082
0
                PB_RETURN_ERROR(stream, "failed to set defaults");
1083
12.6k
        }
1084
164k
    }
1085
1086
8.26M
    while (pb_decode_tag(stream, &wire_type, &tag, &eof))
1087
7.96M
    {
1088
7.96M
        if (tag == 0)
1089
69
        {
1090
69
          if (flags & PB_DECODE_NULLTERMINATED)
1091
0
          {
1092
0
            eof = true;
1093
0
            break;
1094
0
          }
1095
69
          else
1096
69
          {
1097
69
            PB_RETURN_ERROR(stream, "zero tag");
1098
69
          }
1099
69
        }
1100
1101
7.96M
        if (!pb_field_iter_find(&iter, tag) || PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION)
1102
1.20M
        {
1103
            /* No match found, check if it matches an extension. */
1104
1.20M
            if (extension_range_start == 0)
1105
27.5k
            {
1106
27.5k
                if (pb_field_iter_find_extension(&iter))
1107
3.66k
                {
1108
3.66k
                    extensions = *(pb_extension_t* const *)iter.pData;
1109
3.66k
                    extension_range_start = iter.tag;
1110
3.66k
                }
1111
1112
27.5k
                if (!extensions)
1113
24.7k
                {
1114
24.7k
                    extension_range_start = (uint32_t)-1;
1115
24.7k
                }
1116
27.5k
            }
1117
1118
1.20M
            if (tag >= extension_range_start)
1119
137k
            {
1120
137k
                size_t pos = stream->bytes_left;
1121
1122
137k
                if (!decode_extension(stream, tag, wire_type, extensions))
1123
33
                    return false;
1124
1125
137k
                if (pos != stream->bytes_left)
1126
6.12k
                {
1127
                    /* The field was handled */
1128
6.12k
                    continue;
1129
6.12k
                }
1130
137k
            }
1131
1132
            /* No match found, skip data */
1133
1.20M
            if (!pb_skip_field(stream, wire_type))
1134
326
                return false;
1135
1.20M
            continue;
1136
1.20M
        }
1137
1138
        /* If a repeated fixed count field was found, get size from
1139
         * 'fixed_count_field' as there is no counter contained in the struct.
1140
         */
1141
6.75M
        if (PB_HTYPE(iter.type) == PB_HTYPE_REPEATED && iter.pSize == &iter.array_size)
1142
3.59k
        {
1143
3.59k
            if (fixed_count_field != iter.index) {
1144
                /* If the new fixed count field does not match the previous one,
1145
                 * check that the previous one is NULL or that it finished
1146
                 * receiving all the expected data.
1147
                 */
1148
3.30k
                if (fixed_count_field != PB_SIZE_MAX &&
1149
1.67k
                    fixed_count_size != fixed_count_total_size)
1150
3
                {
1151
3
                    PB_RETURN_ERROR(stream, "wrong size for fixed count field");
1152
3
                }
1153
1154
3.30k
                fixed_count_field = iter.index;
1155
3.30k
                fixed_count_size = 0;
1156
3.30k
                fixed_count_total_size = iter.array_size;
1157
3.30k
            }
1158
1159
3.59k
            iter.pSize = &fixed_count_size;
1160
3.59k
        }
1161
1162
6.75M
        if (PB_HTYPE(iter.type) == PB_HTYPE_REQUIRED
1163
6.20M
            && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
1164
6.20M
        {
1165
6.20M
            uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
1166
6.20M
            fields_seen.bitfield[iter.required_field_index >> 5] |= tmp;
1167
6.20M
        }
1168
1169
6.75M
        if (!decode_field(stream, wire_type, &iter))
1170
1.26k
            return false;
1171
6.75M
    }
1172
1173
299k
    if (!eof)
1174
114
    {
1175
        /* pb_decode_tag() returned error before end of stream */
1176
114
        return false;
1177
114
    }
1178
1179
    /* Check that all elements of the last decoded fixed count field were present. */
1180
299k
    if (fixed_count_field != PB_SIZE_MAX &&
1181
1.57k
        fixed_count_size != fixed_count_total_size)
1182
9
    {
1183
9
        PB_RETURN_ERROR(stream, "wrong size for fixed count field");
1184
9
    }
1185
1186
    /* Check that all required fields were present. */
1187
299k
    {
1188
299k
        pb_size_t req_field_count = iter.descriptor->required_field_count;
1189
1190
299k
        if (req_field_count > 0)
1191
156k
        {
1192
156k
            pb_size_t i;
1193
1194
156k
            if (req_field_count > PB_MAX_REQUIRED_FIELDS)
1195
0
                req_field_count = PB_MAX_REQUIRED_FIELDS;
1196
1197
            /* Check the whole words */
1198
156k
            for (i = 0; i < (req_field_count >> 5); i++)
1199
0
            {
1200
0
                if (fields_seen.bitfield[i] != allbits)
1201
0
                    PB_RETURN_ERROR(stream, "missing required field");
1202
0
            }
1203
1204
            /* Check the remaining bits (if any) */
1205
156k
            if ((req_field_count & 31) != 0)
1206
156k
            {
1207
156k
                if (fields_seen.bitfield[req_field_count >> 5] !=
1208
156k
                    (allbits >> (uint_least8_t)(32 - (req_field_count & 31))))
1209
350
                {
1210
350
                    PB_RETURN_ERROR(stream, "missing required field");
1211
350
                }
1212
156k
            }
1213
156k
        }
1214
299k
    }
1215
1216
299k
    return true;
1217
299k
}
1218
1219
bool checkreturn pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
1220
8.54k
{
1221
8.54k
    bool status;
1222
1223
8.54k
    if ((flags & PB_DECODE_DELIMITED) == 0)
1224
8.54k
    {
1225
8.54k
      status = pb_decode_inner(stream, fields, dest_struct, flags);
1226
8.54k
    }
1227
0
    else
1228
0
    {
1229
0
      pb_istream_t substream;
1230
0
      if (!pb_make_string_substream(stream, &substream))
1231
0
        return false;
1232
1233
0
      status = pb_decode_inner(&substream, fields, dest_struct, flags);
1234
1235
0
      if (!pb_close_string_substream(stream, &substream))
1236
0
        status = false;
1237
0
    }
1238
    
1239
#ifdef PB_ENABLE_MALLOC
1240
    if (!status)
1241
        pb_release(fields, dest_struct);
1242
#endif
1243
    
1244
8.54k
    return status;
1245
8.54k
}
1246
1247
bool checkreturn pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
1248
3.98k
{
1249
3.98k
    return pb_decode_ex(stream, fields, dest_struct, 0);
1250
3.98k
}
1251
1252
#ifdef PB_ENABLE_MALLOC
1253
/* Given an oneof field, if there has already been a field inside this oneof,
1254
 * release it before overwriting with a different one. */
1255
static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field)
1256
{
1257
    pb_field_iter_t old_field = *field;
1258
    pb_size_t old_tag = *(pb_size_t*)field->pSize; /* Previous which_ value */
1259
    pb_size_t new_tag = field->tag; /* New which_ value */
1260
1261
    if (old_tag == 0)
1262
        return true; /* Ok, no old data in union */
1263
1264
    if (old_tag == new_tag)
1265
        return true; /* Ok, old data is of same type => merge */
1266
1267
    /* Release old data. The find can fail if the message struct contains
1268
     * invalid data. */
1269
    if (!pb_field_iter_find(&old_field, old_tag))
1270
        PB_RETURN_ERROR(stream, "invalid union tag");
1271
1272
    pb_release_single_field(&old_field);
1273
1274
    if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1275
    {
1276
        /* Initialize the pointer to NULL to make sure it is valid
1277
         * even in case of error return. */
1278
        *(void**)field->pField = NULL;
1279
        field->pData = NULL;
1280
    }
1281
1282
    return true;
1283
}
1284
1285
static void pb_release_single_field(pb_field_iter_t *field)
1286
{
1287
    pb_type_t type;
1288
    type = field->type;
1289
1290
    if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
1291
    {
1292
        if (*(pb_size_t*)field->pSize != field->tag)
1293
            return; /* This is not the current field in the union */
1294
    }
1295
1296
    /* Release anything contained inside an extension or submsg.
1297
     * This has to be done even if the submsg itself is statically
1298
     * allocated. */
1299
    if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
1300
    {
1301
        /* Release fields from all extensions in the linked list */
1302
        pb_extension_t *ext = *(pb_extension_t**)field->pData;
1303
        while (ext != NULL)
1304
        {
1305
            pb_field_iter_t ext_iter;
1306
            if (pb_field_iter_begin_extension(&ext_iter, ext))
1307
            {
1308
                pb_release_single_field(&ext_iter);
1309
            }
1310
            ext = ext->next;
1311
        }
1312
    }
1313
    else if (PB_LTYPE_IS_SUBMSG(type) && PB_ATYPE(type) != PB_ATYPE_CALLBACK)
1314
    {
1315
        /* Release fields in submessage or submsg array */
1316
        pb_size_t count = 1;
1317
        
1318
        if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1319
        {
1320
            field->pData = *(void**)field->pField;
1321
        }
1322
        else
1323
        {
1324
            field->pData = field->pField;
1325
        }
1326
        
1327
        if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1328
        {
1329
            count = *(pb_size_t*)field->pSize;
1330
1331
            if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > field->array_size)
1332
            {
1333
                /* Protect against corrupted _count fields */
1334
                count = field->array_size;
1335
            }
1336
        }
1337
        
1338
        if (field->pData)
1339
        {
1340
            for (; count > 0; count--)
1341
            {
1342
                pb_release(field->submsg_desc, field->pData);
1343
                field->pData = (char*)field->pData + field->data_size;
1344
            }
1345
        }
1346
    }
1347
    
1348
    if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1349
    {
1350
        if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1351
            (PB_LTYPE(type) == PB_LTYPE_STRING ||
1352
             PB_LTYPE(type) == PB_LTYPE_BYTES))
1353
        {
1354
            /* Release entries in repeated string or bytes array */
1355
            void **pItem = *(void***)field->pField;
1356
            pb_size_t count = *(pb_size_t*)field->pSize;
1357
            for (; count > 0; count--)
1358
            {
1359
                pb_free(*pItem);
1360
                *pItem++ = NULL;
1361
            }
1362
        }
1363
        
1364
        if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1365
        {
1366
            /* We are going to release the array, so set the size to 0 */
1367
            *(pb_size_t*)field->pSize = 0;
1368
        }
1369
        
1370
        /* Release main pointer */
1371
        pb_free(*(void**)field->pField);
1372
        *(void**)field->pField = NULL;
1373
    }
1374
}
1375
1376
void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
1377
{
1378
    pb_field_iter_t iter;
1379
    
1380
    if (!dest_struct)
1381
        return; /* Ignore NULL pointers, similar to free() */
1382
1383
    if (!pb_field_iter_begin(&iter, fields, dest_struct))
1384
        return; /* Empty message type */
1385
    
1386
    do
1387
    {
1388
        pb_release_single_field(&iter);
1389
    } while (pb_field_iter_next(&iter));
1390
}
1391
#else
1392
void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
1393
6.64k
{
1394
    /* Nothing to release without PB_ENABLE_MALLOC. */
1395
6.64k
    PB_UNUSED(fields);
1396
6.64k
    PB_UNUSED(dest_struct);
1397
6.64k
}
1398
#endif
1399
1400
/* Field decoders */
1401
1402
bool pb_decode_bool(pb_istream_t *stream, bool *dest)
1403
342k
{
1404
342k
    uint32_t value;
1405
342k
    if (!pb_decode_varint32(stream, &value))
1406
17
        return false;
1407
1408
342k
    *(bool*)dest = (value != 0);
1409
342k
    return true;
1410
342k
}
1411
1412
bool pb_decode_svarint(pb_istream_t *stream, pb_int64_t *dest)
1413
178k
{
1414
178k
    pb_uint64_t value;
1415
178k
    if (!pb_decode_varint(stream, &value))
1416
23
        return false;
1417
    
1418
178k
    if (value & 1)
1419
30.0k
        *dest = (pb_int64_t)(~(value >> 1));
1420
148k
    else
1421
148k
        *dest = (pb_int64_t)(value >> 1);
1422
    
1423
178k
    return true;
1424
178k
}
1425
1426
bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1427
317k
{
1428
317k
    union {
1429
317k
        uint32_t fixed32;
1430
317k
        pb_byte_t bytes[4];
1431
317k
    } u;
1432
1433
317k
    if (!pb_read(stream, u.bytes, 4))
1434
13
        return false;
1435
1436
317k
#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1437
    /* fast path - if we know that we're on little endian, assign directly */
1438
317k
    *(uint32_t*)dest = u.fixed32;
1439
#else
1440
    *(uint32_t*)dest = ((uint32_t)u.bytes[0] << 0) |
1441
                       ((uint32_t)u.bytes[1] << 8) |
1442
                       ((uint32_t)u.bytes[2] << 16) |
1443
                       ((uint32_t)u.bytes[3] << 24);
1444
#endif
1445
317k
    return true;
1446
317k
}
1447
1448
#ifndef PB_WITHOUT_64BIT
1449
bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1450
262k
{
1451
262k
    union {
1452
262k
        uint64_t fixed64;
1453
262k
        pb_byte_t bytes[8];
1454
262k
    } u;
1455
1456
262k
    if (!pb_read(stream, u.bytes, 8))
1457
16
        return false;
1458
1459
262k
#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1460
    /* fast path - if we know that we're on little endian, assign directly */
1461
262k
    *(uint64_t*)dest = u.fixed64;
1462
#else
1463
    *(uint64_t*)dest = ((uint64_t)u.bytes[0] << 0) |
1464
                       ((uint64_t)u.bytes[1] << 8) |
1465
                       ((uint64_t)u.bytes[2] << 16) |
1466
                       ((uint64_t)u.bytes[3] << 24) |
1467
                       ((uint64_t)u.bytes[4] << 32) |
1468
                       ((uint64_t)u.bytes[5] << 40) |
1469
                       ((uint64_t)u.bytes[6] << 48) |
1470
                       ((uint64_t)u.bytes[7] << 56);
1471
#endif
1472
262k
    return true;
1473
262k
}
1474
#endif
1475
1476
static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field)
1477
342k
{
1478
342k
    return pb_decode_bool(stream, (bool*)field->pData);
1479
342k
}
1480
1481
static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field)
1482
5.65M
{
1483
5.65M
    if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT)
1484
388k
    {
1485
388k
        pb_uint64_t value, clamped;
1486
388k
        if (!pb_decode_varint(stream, &value))
1487
18
            return false;
1488
1489
        /* Cast to the proper field size, while checking for overflows */
1490
388k
        if (field->data_size == sizeof(pb_uint64_t))
1491
174k
            clamped = *(pb_uint64_t*)field->pData = value;
1492
214k
        else if (field->data_size == sizeof(uint32_t))
1493
188k
            clamped = *(uint32_t*)field->pData = (uint32_t)value;
1494
26.1k
        else if (field->data_size == sizeof(uint_least16_t))
1495
13.0k
            clamped = *(uint_least16_t*)field->pData = (uint_least16_t)value;
1496
13.1k
        else if (field->data_size == sizeof(uint_least8_t))
1497
13.1k
            clamped = *(uint_least8_t*)field->pData = (uint_least8_t)value;
1498
0
        else
1499
0
            PB_RETURN_ERROR(stream, "invalid data_size");
1500
1501
388k
        if (clamped != value)
1502
114
            PB_RETURN_ERROR(stream, "integer too large");
1503
1504
388k
        return true;
1505
388k
    }
1506
5.26M
    else
1507
5.26M
    {
1508
5.26M
        pb_uint64_t value;
1509
5.26M
        pb_int64_t svalue;
1510
5.26M
        pb_int64_t clamped;
1511
1512
5.26M
        if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT)
1513
178k
        {
1514
178k
            if (!pb_decode_svarint(stream, &svalue))
1515
23
                return false;
1516
178k
        }
1517
5.08M
        else
1518
5.08M
        {
1519
5.08M
            if (!pb_decode_varint(stream, &value))
1520
46
                return false;
1521
1522
            /* See issue 97: Google's C++ protobuf allows negative varint values to
1523
            * be cast as int32_t, instead of the int64_t that should be used when
1524
            * encoding. Nanopb versions before 0.2.5 had a bug in encoding. In order to
1525
            * not break decoding of such messages, we cast <=32 bit fields to
1526
            * int32_t first to get the sign correct.
1527
            */
1528
5.08M
            if (field->data_size == sizeof(pb_int64_t))
1529
163k
                svalue = (pb_int64_t)value;
1530
4.92M
            else
1531
4.92M
                svalue = (int32_t)value;
1532
5.08M
        }
1533
1534
        /* Cast to the proper field size, while checking for overflows */
1535
5.26M
        if (field->data_size == sizeof(pb_int64_t))
1536
243k
            clamped = *(pb_int64_t*)field->pData = svalue;
1537
5.02M
        else if (field->data_size == sizeof(int32_t))
1538
4.96M
            clamped = *(int32_t*)field->pData = (int32_t)svalue;
1539
51.9k
        else if (field->data_size == sizeof(int_least16_t))
1540
25.9k
            clamped = *(int_least16_t*)field->pData = (int_least16_t)svalue;
1541
25.9k
        else if (field->data_size == sizeof(int_least8_t))
1542
25.9k
            clamped = *(int_least8_t*)field->pData = (int_least8_t)svalue;
1543
0
        else
1544
0
            PB_RETURN_ERROR(stream, "invalid data_size");
1545
1546
5.26M
        if (clamped != svalue)
1547
96
            PB_RETURN_ERROR(stream, "integer too large");
1548
1549
5.26M
        return true;
1550
5.26M
    }
1551
5.65M
}
1552
1553
static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
1554
49.9k
{
1555
49.9k
    uint32_t size;
1556
49.9k
    size_t alloc_size;
1557
49.9k
    pb_bytes_array_t *dest;
1558
    
1559
49.9k
    if (!pb_decode_varint32(stream, &size))
1560
6
        return false;
1561
    
1562
49.9k
    if (size > PB_SIZE_MAX)
1563
32
        PB_RETURN_ERROR(stream, "bytes overflow");
1564
    
1565
49.9k
    alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1566
49.9k
    if (size > alloc_size)
1567
0
        PB_RETURN_ERROR(stream, "size too large");
1568
    
1569
49.9k
    if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1570
0
    {
1571
0
#ifndef PB_ENABLE_MALLOC
1572
0
        PB_RETURN_ERROR(stream, "no malloc support");
1573
#else
1574
        if (stream->bytes_left < size)
1575
            PB_RETURN_ERROR(stream, "end-of-stream");
1576
1577
        if (!allocate_field(stream, field->pData, alloc_size, 1))
1578
            return false;
1579
        dest = *(pb_bytes_array_t**)field->pData;
1580
#endif
1581
0
    }
1582
49.9k
    else
1583
49.9k
    {
1584
49.9k
        if (alloc_size > field->data_size)
1585
46
            PB_RETURN_ERROR(stream, "bytes overflow");
1586
49.9k
        dest = (pb_bytes_array_t*)field->pData;
1587
49.9k
    }
1588
1589
49.9k
    dest->size = (pb_size_t)size;
1590
49.9k
    return pb_read(stream, dest->bytes, (size_t)size);
1591
49.9k
}
1592
1593
static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field)
1594
218k
{
1595
218k
    uint32_t size;
1596
218k
    size_t alloc_size;
1597
218k
    pb_byte_t *dest = (pb_byte_t*)field->pData;
1598
1599
218k
    if (!pb_decode_varint32(stream, &size))
1600
4
        return false;
1601
1602
218k
    if (size == (uint32_t)-1)
1603
2
        PB_RETURN_ERROR(stream, "size too large");
1604
1605
    /* Space for null terminator */
1606
218k
    alloc_size = (size_t)(size + 1);
1607
1608
218k
    if (alloc_size < size)
1609
0
        PB_RETURN_ERROR(stream, "size too large");
1610
1611
218k
    if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1612
0
    {
1613
0
#ifndef PB_ENABLE_MALLOC
1614
0
        PB_RETURN_ERROR(stream, "no malloc support");
1615
#else
1616
        if (stream->bytes_left < size)
1617
            PB_RETURN_ERROR(stream, "end-of-stream");
1618
1619
        if (!allocate_field(stream, field->pData, alloc_size, 1))
1620
            return false;
1621
        dest = *(pb_byte_t**)field->pData;
1622
#endif
1623
0
    }
1624
218k
    else
1625
218k
    {
1626
218k
        if (alloc_size > field->data_size)
1627
96
            PB_RETURN_ERROR(stream, "string overflow");
1628
218k
    }
1629
    
1630
218k
    dest[size] = 0;
1631
1632
218k
    if (!pb_read(stream, dest, (size_t)size))
1633
8
        return false;
1634
1635
#ifdef PB_VALIDATE_UTF8
1636
    if (!pb_validate_utf8((const char*)dest))
1637
        PB_RETURN_ERROR(stream, "invalid utf8");
1638
#endif
1639
1640
218k
    return true;
1641
218k
}
1642
1643
static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field)
1644
319k
{
1645
319k
    bool status = true;
1646
319k
    bool submsg_consumed = false;
1647
319k
    pb_istream_t substream;
1648
1649
319k
    if (!pb_make_string_substream(stream, &substream))
1650
97
        return false;
1651
    
1652
319k
    if (field->submsg_desc == NULL)
1653
0
        PB_RETURN_ERROR(stream, "invalid field descriptor");
1654
    
1655
    /* Submessages can have a separate message-level callback that is called
1656
     * before decoding the message. Typically it is used to set callback fields
1657
     * inside oneofs. */
1658
319k
    if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL)
1659
46.9k
    {
1660
        /* Message callback is stored right before pSize. */
1661
46.9k
        pb_callback_t *callback = (pb_callback_t*)field->pSize - 1;
1662
46.9k
        if (callback->funcs.decode)
1663
46.9k
        {
1664
46.9k
            status = callback->funcs.decode(&substream, field, &callback->arg);
1665
1666
46.9k
            if (substream.bytes_left == 0)
1667
27.2k
            {
1668
27.2k
                submsg_consumed = true;
1669
27.2k
            }
1670
46.9k
        }
1671
46.9k
    }
1672
1673
    /* Now decode the submessage contents */
1674
319k
    if (status && !submsg_consumed)
1675
292k
    {
1676
292k
        unsigned int flags = 0;
1677
1678
        /* Static required/optional fields are already initialized by top-level
1679
         * pb_decode(), no need to initialize them again. */
1680
292k
        if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
1681
292k
            PB_HTYPE(field->type) != PB_HTYPE_REPEATED)
1682
287k
        {
1683
287k
            flags = PB_DECODE_NOINIT;
1684
287k
        }
1685
1686
292k
        status = pb_decode_inner(&substream, field->submsg_desc, field->pData, flags);
1687
292k
    }
1688
    
1689
319k
    if (!pb_close_string_substream(stream, &substream))
1690
0
        return false;
1691
1692
319k
    return status;
1693
319k
}
1694
1695
static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
1696
28.9k
{
1697
28.9k
    uint32_t size;
1698
1699
28.9k
    if (!pb_decode_varint32(stream, &size))
1700
6
        return false;
1701
1702
28.9k
    if (size > PB_SIZE_MAX)
1703
31
        PB_RETURN_ERROR(stream, "bytes overflow");
1704
1705
28.9k
    if (size == 0)
1706
16.4k
    {
1707
        /* As a special case, treat empty bytes string as all zeros for fixed_length_bytes. */
1708
16.4k
        memset(field->pData, 0, (size_t)field->data_size);
1709
16.4k
        return true;
1710
16.4k
    }
1711
1712
12.4k
    if (size != field->data_size)
1713
46
        PB_RETURN_ERROR(stream, "incorrect fixed length bytes size");
1714
1715
12.4k
    return pb_read(stream, (pb_byte_t*)field->pData, (size_t)field->data_size);
1716
12.4k
}
1717
1718
#ifdef PB_CONVERT_DOUBLE_FLOAT
1719
bool pb_decode_double_as_float(pb_istream_t *stream, float *dest)
1720
{
1721
    uint_least8_t sign;
1722
    int exponent;
1723
    uint32_t mantissa;
1724
    uint64_t value;
1725
    union { float f; uint32_t i; } out;
1726
1727
    if (!pb_decode_fixed64(stream, &value))
1728
        return false;
1729
1730
    /* Decompose input value */
1731
    sign = (uint_least8_t)((value >> 63) & 1);
1732
    exponent = (int)((value >> 52) & 0x7FF) - 1023;
1733
    mantissa = (value >> 28) & 0xFFFFFF; /* Highest 24 bits */
1734
1735
    /* Figure if value is in range representable by floats. */
1736
    if (exponent == 1024)
1737
    {
1738
        /* Special value */
1739
        exponent = 128;
1740
        mantissa >>= 1;
1741
    }
1742
    else
1743
    {
1744
        if (exponent > 127)
1745
        {
1746
            /* Too large, convert to infinity */
1747
            exponent = 128;
1748
            mantissa = 0;
1749
        }
1750
        else if (exponent < -150)
1751
        {
1752
            /* Too small, convert to zero */
1753
            exponent = -127;
1754
            mantissa = 0;
1755
        }
1756
        else if (exponent < -126)
1757
        {
1758
            /* Denormalized */
1759
            mantissa |= 0x1000000;
1760
            mantissa >>= (-126 - exponent);
1761
            exponent = -127;
1762
        }
1763
1764
        /* Round off mantissa */
1765
        mantissa = (mantissa + 1) >> 1;
1766
1767
        /* Check if mantissa went over 2.0 */
1768
        if (mantissa & 0x800000)
1769
        {
1770
            exponent += 1;
1771
            mantissa &= 0x7FFFFF;
1772
            mantissa >>= 1;
1773
        }
1774
    }
1775
1776
    /* Combine fields */
1777
    out.i = mantissa;
1778
    out.i |= (uint32_t)(exponent + 127) << 23;
1779
    out.i |= (uint32_t)sign << 31;
1780
1781
    *dest = out.f;
1782
    return true;
1783
}
1784
#endif