Coverage Report

Created: 2026-01-13 06:07

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