grpc 0.13.0 → 0.13.1.pre1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of grpc might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.yardopts +1 -0
- data/Makefile +1114 -937
- data/include/grpc/census.h +71 -89
- data/include/grpc/compression.h +7 -7
- data/include/grpc/grpc.h +65 -68
- data/include/grpc/grpc_security.h +38 -38
- data/include/grpc/impl/codegen/alloc.h +7 -7
- data/include/grpc/impl/codegen/byte_buffer.h +13 -13
- data/include/grpc/impl/codegen/grpc_types.h +7 -2
- data/include/grpc/impl/codegen/log.h +5 -5
- data/include/grpc/impl/codegen/port_platform.h +14 -6
- data/include/grpc/impl/codegen/slice.h +15 -15
- data/include/grpc/impl/codegen/slice_buffer.h +17 -17
- data/include/grpc/impl/codegen/sync.h +26 -22
- data/include/grpc/impl/codegen/time.h +22 -24
- data/include/grpc/support/avl.h +9 -8
- data/include/grpc/support/cmdline.h +12 -12
- data/include/grpc/support/cpu.h +2 -2
- data/include/grpc/support/histogram.h +22 -22
- data/include/grpc/support/host_port.h +2 -2
- data/include/grpc/support/log_win32.h +1 -1
- data/include/grpc/support/string_util.h +2 -2
- data/include/grpc/support/subprocess.h +5 -5
- data/include/grpc/support/thd.h +9 -9
- data/include/grpc/support/useful.h +3 -1
- data/src/core/census/context.c +64 -85
- data/src/core/census/grpc_filter.c +2 -2
- data/src/core/census/mlog.c +600 -0
- data/src/core/census/mlog.h +95 -0
- data/src/core/channel/channel_args.c +67 -6
- data/src/core/channel/channel_args.h +7 -1
- data/src/core/channel/client_channel.c +26 -36
- data/src/core/channel/client_uchannel.c +1 -1
- data/src/core/channel/http_client_filter.c +2 -2
- data/src/core/channel/http_server_filter.c +2 -2
- data/src/core/channel/subchannel_call_holder.c +5 -7
- data/src/core/client_config/connector.c +3 -2
- data/src/core/client_config/connector.h +2 -2
- data/src/core/client_config/lb_policies/load_balancer_api.c +163 -0
- data/src/core/client_config/lb_policies/load_balancer_api.h +85 -0
- data/src/core/client_config/lb_policies/pick_first.c +10 -11
- data/src/core/client_config/lb_policies/round_robin.c +7 -8
- data/src/core/client_config/lb_policy.c +3 -3
- data/src/core/client_config/lb_policy.h +3 -2
- data/src/core/client_config/subchannel.c +51 -21
- data/src/core/client_config/subchannel.h +15 -6
- data/src/core/client_config/subchannel_index.c +261 -0
- data/src/core/client_config/subchannel_index.h +77 -0
- data/src/core/compression/{algorithm.c → compression_algorithm.c} +0 -0
- data/src/core/httpcli/httpcli.c +13 -11
- data/src/core/httpcli/httpcli.h +3 -2
- data/src/core/httpcli/httpcli_security_connector.c +7 -7
- data/src/core/iomgr/fd_posix.c +4 -2
- data/src/core/iomgr/iocp_windows.c +10 -6
- data/src/core/iomgr/iocp_windows.h +9 -2
- data/src/core/iomgr/iomgr.c +18 -2
- data/src/core/iomgr/iomgr_internal.h +5 -1
- data/src/core/iomgr/pollset.h +9 -10
- data/src/core/iomgr/pollset_multipoller_with_epoll.c +1 -0
- data/src/core/iomgr/pollset_multipoller_with_poll_posix.c +10 -5
- data/src/core/iomgr/pollset_posix.c +30 -35
- data/src/core/iomgr/pollset_posix.h +10 -6
- data/src/core/iomgr/pollset_set.h +3 -9
- data/src/core/iomgr/pollset_set_posix.c +23 -3
- data/src/core/iomgr/pollset_set_posix.h +2 -18
- data/src/core/iomgr/pollset_set_windows.c +3 -3
- data/src/core/iomgr/pollset_set_windows.h +2 -2
- data/src/core/iomgr/pollset_windows.c +24 -21
- data/src/core/iomgr/pollset_windows.h +1 -5
- data/src/core/iomgr/tcp_client_posix.c +7 -5
- data/src/core/iomgr/tcp_posix.c +4 -2
- data/src/core/iomgr/tcp_server_windows.c +1 -2
- data/src/core/iomgr/timer.c +2 -3
- data/src/core/iomgr/timer.h +21 -1
- data/src/core/iomgr/timer_heap.c +10 -12
- data/src/core/iomgr/udp_server.c +5 -4
- data/src/core/iomgr/udp_server.h +1 -0
- data/src/core/iomgr/workqueue_posix.c +1 -0
- data/src/core/iomgr/workqueue_posix.h +3 -1
- data/src/core/proto/grpc/lb/v0/load_balancer.pb.c +119 -0
- data/src/core/proto/grpc/lb/v0/load_balancer.pb.h +182 -0
- data/src/core/security/{base64.c → b64.c} +1 -1
- data/src/core/security/{base64.h → b64.h} +1 -1
- data/src/core/security/client_auth_filter.c +0 -1
- data/src/core/security/credentials.c +12 -5
- data/src/core/security/credentials.h +3 -3
- data/src/core/security/google_default_credentials.c +24 -19
- data/src/core/security/handshake.c +15 -7
- data/src/core/security/handshake.h +2 -1
- data/src/core/security/json_token.c +1 -1
- data/src/core/security/jwt_verifier.c +1 -1
- data/src/core/security/security_connector.c +84 -64
- data/src/core/security/security_connector.h +42 -22
- data/src/core/security/security_context.c +8 -3
- data/src/core/security/server_auth_filter.c +2 -2
- data/src/core/security/server_secure_chttp2.c +7 -7
- data/src/core/support/avl.c +2 -2
- data/src/core/support/env_linux.c +17 -0
- data/src/core/support/{file.c → load_file.c} +2 -2
- data/src/core/support/{file.h → load_file.h} +4 -12
- data/src/core/support/sync.c +6 -1
- data/src/core/support/time_posix.c +1 -1
- data/src/core/{iomgr/timer_internal.h → support/tmpfile.h} +17 -23
- data/src/core/support/{file_posix.c → tmpfile_posix.c} +2 -2
- data/src/core/support/{file_win32.c → tmpfile_win32.c} +2 -2
- data/src/core/surface/alarm.c +3 -2
- data/src/core/surface/call.c +102 -52
- data/src/core/surface/channel_create.c +1 -1
- data/src/core/surface/completion_queue.c +73 -41
- data/src/core/surface/init.c +4 -0
- data/src/core/surface/lame_client.c +1 -2
- data/src/core/surface/secure_channel_create.c +6 -7
- data/src/core/surface/server.c +13 -5
- data/src/core/surface/validate_metadata.c +1 -1
- data/src/core/surface/version.c +1 -1
- data/src/core/transport/chttp2/internal.h +22 -10
- data/src/core/transport/chttp2/parsing.c +3 -3
- data/src/core/transport/chttp2/stream_lists.c +39 -21
- data/src/core/transport/chttp2/writing.c +19 -28
- data/src/core/transport/chttp2_transport.c +80 -37
- data/src/core/transport/metadata.c +8 -0
- data/src/core/transport/static_metadata.c +17 -17
- data/src/core/transport/static_metadata.h +3 -3
- data/src/core/transport/transport.c +2 -1
- data/src/core/transport/transport.h +12 -5
- data/src/ruby/ext/grpc/extconf.rb +1 -0
- data/src/ruby/ext/grpc/rb_call.c +6 -0
- data/src/ruby/ext/grpc/rb_call_credentials.c +12 -14
- data/src/ruby/ext/grpc/rb_channel.c +8 -14
- data/src/ruby/ext/grpc/rb_channel_credentials.c +11 -12
- data/src/ruby/ext/grpc/rb_grpc.c +19 -18
- data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +4 -0
- data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +8 -2
- data/src/ruby/lib/grpc/core/time_consts.rb +2 -2
- data/src/ruby/lib/grpc/errors.rb +2 -2
- data/src/ruby/lib/grpc/generic/rpc_server.rb +58 -39
- data/src/ruby/lib/grpc/version.rb +1 -1
- data/src/ruby/pb/README.md +2 -2
- data/src/ruby/pb/generate_proto_ruby.sh +2 -2
- data/src/ruby/pb/grpc/health/checker.rb +11 -11
- data/src/ruby/pb/grpc/health/v1/health.rb +28 -0
- data/src/ruby/pb/grpc/health/{v1alpha → v1}/health_services.rb +4 -4
- data/src/ruby/spec/client_server_spec.rb +2 -1
- data/src/ruby/spec/generic/rpc_server_spec.rb +3 -22
- data/src/ruby/spec/pb/health/checker_spec.rb +22 -36
- data/third_party/nanopb/pb.h +547 -0
- data/third_party/nanopb/pb_common.c +97 -0
- data/third_party/nanopb/pb_common.h +42 -0
- data/third_party/nanopb/pb_decode.c +1319 -0
- data/third_party/nanopb/pb_decode.h +149 -0
- data/third_party/nanopb/pb_encode.c +690 -0
- data/third_party/nanopb/pb_encode.h +154 -0
- metadata +32 -16
- data/src/ruby/pb/grpc/health/v1alpha/health.rb +0 -29
@@ -0,0 +1,97 @@
|
|
1
|
+
/* pb_common.c: Common support functions for pb_encode.c and pb_decode.c.
|
2
|
+
*
|
3
|
+
* 2014 Petteri Aimonen <jpa@kapsi.fi>
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include "pb_common.h"
|
7
|
+
|
8
|
+
bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_field_t *fields, void *dest_struct)
|
9
|
+
{
|
10
|
+
iter->start = fields;
|
11
|
+
iter->pos = fields;
|
12
|
+
iter->required_field_index = 0;
|
13
|
+
iter->dest_struct = dest_struct;
|
14
|
+
iter->pData = (char*)dest_struct + iter->pos->data_offset;
|
15
|
+
iter->pSize = (char*)iter->pData + iter->pos->size_offset;
|
16
|
+
|
17
|
+
return (iter->pos->tag != 0);
|
18
|
+
}
|
19
|
+
|
20
|
+
bool pb_field_iter_next(pb_field_iter_t *iter)
|
21
|
+
{
|
22
|
+
const pb_field_t *prev_field = iter->pos;
|
23
|
+
|
24
|
+
if (prev_field->tag == 0)
|
25
|
+
{
|
26
|
+
/* Handle empty message types, where the first field is already the terminator.
|
27
|
+
* In other cases, the iter->pos never points to the terminator. */
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
iter->pos++;
|
32
|
+
|
33
|
+
if (iter->pos->tag == 0)
|
34
|
+
{
|
35
|
+
/* Wrapped back to beginning, reinitialize */
|
36
|
+
(void)pb_field_iter_begin(iter, iter->start, iter->dest_struct);
|
37
|
+
return false;
|
38
|
+
}
|
39
|
+
else
|
40
|
+
{
|
41
|
+
/* Increment the pointers based on previous field size */
|
42
|
+
size_t prev_size = prev_field->data_size;
|
43
|
+
|
44
|
+
if (PB_HTYPE(prev_field->type) == PB_HTYPE_ONEOF &&
|
45
|
+
PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
|
46
|
+
{
|
47
|
+
/* Don't advance pointers inside unions */
|
48
|
+
prev_size = 0;
|
49
|
+
iter->pData = (char*)iter->pData - prev_field->data_offset;
|
50
|
+
}
|
51
|
+
else if (PB_ATYPE(prev_field->type) == PB_ATYPE_STATIC &&
|
52
|
+
PB_HTYPE(prev_field->type) == PB_HTYPE_REPEATED)
|
53
|
+
{
|
54
|
+
/* In static arrays, the data_size tells the size of a single entry and
|
55
|
+
* array_size is the number of entries */
|
56
|
+
prev_size *= prev_field->array_size;
|
57
|
+
}
|
58
|
+
else if (PB_ATYPE(prev_field->type) == PB_ATYPE_POINTER)
|
59
|
+
{
|
60
|
+
/* Pointer fields always have a constant size in the main structure.
|
61
|
+
* The data_size only applies to the dynamically allocated area. */
|
62
|
+
prev_size = sizeof(void*);
|
63
|
+
}
|
64
|
+
|
65
|
+
if (PB_HTYPE(prev_field->type) == PB_HTYPE_REQUIRED)
|
66
|
+
{
|
67
|
+
/* Count the required fields, in order to check their presence in the
|
68
|
+
* decoder. */
|
69
|
+
iter->required_field_index++;
|
70
|
+
}
|
71
|
+
|
72
|
+
iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset;
|
73
|
+
iter->pSize = (char*)iter->pData + iter->pos->size_offset;
|
74
|
+
return true;
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag)
|
79
|
+
{
|
80
|
+
const pb_field_t *start = iter->pos;
|
81
|
+
|
82
|
+
do {
|
83
|
+
if (iter->pos->tag == tag &&
|
84
|
+
PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION)
|
85
|
+
{
|
86
|
+
/* Found the wanted field */
|
87
|
+
return true;
|
88
|
+
}
|
89
|
+
|
90
|
+
(void)pb_field_iter_next(iter);
|
91
|
+
} while (iter->pos != start);
|
92
|
+
|
93
|
+
/* Searched all the way back to start, and found nothing. */
|
94
|
+
return false;
|
95
|
+
}
|
96
|
+
|
97
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
/* pb_common.h: Common support functions for pb_encode.c and pb_decode.c.
|
2
|
+
* These functions are rarely needed by applications directly.
|
3
|
+
*/
|
4
|
+
|
5
|
+
#ifndef PB_COMMON_H_INCLUDED
|
6
|
+
#define PB_COMMON_H_INCLUDED
|
7
|
+
|
8
|
+
#include "pb.h"
|
9
|
+
|
10
|
+
#ifdef __cplusplus
|
11
|
+
extern "C" {
|
12
|
+
#endif
|
13
|
+
|
14
|
+
/* Iterator for pb_field_t list */
|
15
|
+
struct pb_field_iter_s {
|
16
|
+
const pb_field_t *start; /* Start of the pb_field_t array */
|
17
|
+
const pb_field_t *pos; /* Current position of the iterator */
|
18
|
+
unsigned required_field_index; /* Zero-based index that counts only the required fields */
|
19
|
+
void *dest_struct; /* Pointer to start of the structure */
|
20
|
+
void *pData; /* Pointer to current field value */
|
21
|
+
void *pSize; /* Pointer to count/has field */
|
22
|
+
};
|
23
|
+
typedef struct pb_field_iter_s pb_field_iter_t;
|
24
|
+
|
25
|
+
/* Initialize the field iterator structure to beginning.
|
26
|
+
* Returns false if the message type is empty. */
|
27
|
+
bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_field_t *fields, void *dest_struct);
|
28
|
+
|
29
|
+
/* Advance the iterator to the next field.
|
30
|
+
* Returns false when the iterator wraps back to the first field. */
|
31
|
+
bool pb_field_iter_next(pb_field_iter_t *iter);
|
32
|
+
|
33
|
+
/* Advance the iterator until it points at a field with the given tag.
|
34
|
+
* Returns false if no such field exists. */
|
35
|
+
bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag);
|
36
|
+
|
37
|
+
#ifdef __cplusplus
|
38
|
+
} /* extern "C" */
|
39
|
+
#endif
|
40
|
+
|
41
|
+
#endif
|
42
|
+
|
@@ -0,0 +1,1319 @@
|
|
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 and gcc before 3.4.0 just
|
8
|
+
* ignore the annotation.
|
9
|
+
*/
|
10
|
+
#if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
|
11
|
+
#define checkreturn
|
12
|
+
#else
|
13
|
+
#define checkreturn __attribute__((warn_unused_result))
|
14
|
+
#endif
|
15
|
+
|
16
|
+
#include "pb.h"
|
17
|
+
#include "pb_decode.h"
|
18
|
+
#include "pb_common.h"
|
19
|
+
|
20
|
+
/**************************************
|
21
|
+
* Declarations internal to this file *
|
22
|
+
**************************************/
|
23
|
+
|
24
|
+
typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
|
25
|
+
|
26
|
+
static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count);
|
27
|
+
static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
|
28
|
+
static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size);
|
29
|
+
static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
|
30
|
+
static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
|
31
|
+
static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
|
32
|
+
static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension);
|
33
|
+
static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
|
34
|
+
static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter);
|
35
|
+
static bool checkreturn find_extension_field(pb_field_iter_t *iter);
|
36
|
+
static void pb_field_set_to_default(pb_field_iter_t *iter);
|
37
|
+
static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
|
38
|
+
static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
|
39
|
+
static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
|
40
|
+
static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
|
41
|
+
static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
|
42
|
+
static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
|
43
|
+
static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
|
44
|
+
static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
|
45
|
+
static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
|
46
|
+
static bool checkreturn pb_skip_varint(pb_istream_t *stream);
|
47
|
+
static bool checkreturn pb_skip_string(pb_istream_t *stream);
|
48
|
+
|
49
|
+
#ifdef PB_ENABLE_MALLOC
|
50
|
+
static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
|
51
|
+
static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter);
|
52
|
+
static void pb_release_single_field(const pb_field_iter_t *iter);
|
53
|
+
#endif
|
54
|
+
|
55
|
+
/* --- Function pointers to field decoders ---
|
56
|
+
* Order in the array must match pb_action_t LTYPE numbering.
|
57
|
+
*/
|
58
|
+
static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
|
59
|
+
&pb_dec_varint,
|
60
|
+
&pb_dec_uvarint,
|
61
|
+
&pb_dec_svarint,
|
62
|
+
&pb_dec_fixed32,
|
63
|
+
&pb_dec_fixed64,
|
64
|
+
|
65
|
+
&pb_dec_bytes,
|
66
|
+
&pb_dec_string,
|
67
|
+
&pb_dec_submessage,
|
68
|
+
NULL /* extensions */
|
69
|
+
};
|
70
|
+
|
71
|
+
/*******************************
|
72
|
+
* pb_istream_t implementation *
|
73
|
+
*******************************/
|
74
|
+
|
75
|
+
static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
|
76
|
+
{
|
77
|
+
uint8_t *source = (uint8_t*)stream->state;
|
78
|
+
stream->state = source + count;
|
79
|
+
|
80
|
+
if (buf != NULL)
|
81
|
+
{
|
82
|
+
while (count--)
|
83
|
+
*buf++ = *source++;
|
84
|
+
}
|
85
|
+
|
86
|
+
return true;
|
87
|
+
}
|
88
|
+
|
89
|
+
bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
|
90
|
+
{
|
91
|
+
#ifndef PB_BUFFER_ONLY
|
92
|
+
if (buf == NULL && stream->callback != buf_read)
|
93
|
+
{
|
94
|
+
/* Skip input bytes */
|
95
|
+
uint8_t tmp[16];
|
96
|
+
while (count > 16)
|
97
|
+
{
|
98
|
+
if (!pb_read(stream, tmp, 16))
|
99
|
+
return false;
|
100
|
+
|
101
|
+
count -= 16;
|
102
|
+
}
|
103
|
+
|
104
|
+
return pb_read(stream, tmp, count);
|
105
|
+
}
|
106
|
+
#endif
|
107
|
+
|
108
|
+
if (stream->bytes_left < count)
|
109
|
+
PB_RETURN_ERROR(stream, "end-of-stream");
|
110
|
+
|
111
|
+
#ifndef PB_BUFFER_ONLY
|
112
|
+
if (!stream->callback(stream, buf, count))
|
113
|
+
PB_RETURN_ERROR(stream, "io error");
|
114
|
+
#else
|
115
|
+
if (!buf_read(stream, buf, count))
|
116
|
+
return false;
|
117
|
+
#endif
|
118
|
+
|
119
|
+
stream->bytes_left -= count;
|
120
|
+
return true;
|
121
|
+
}
|
122
|
+
|
123
|
+
/* Read a single byte from input stream. buf may not be NULL.
|
124
|
+
* This is an optimization for the varint decoding. */
|
125
|
+
static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
|
126
|
+
{
|
127
|
+
if (stream->bytes_left == 0)
|
128
|
+
PB_RETURN_ERROR(stream, "end-of-stream");
|
129
|
+
|
130
|
+
#ifndef PB_BUFFER_ONLY
|
131
|
+
if (!stream->callback(stream, buf, 1))
|
132
|
+
PB_RETURN_ERROR(stream, "io error");
|
133
|
+
#else
|
134
|
+
*buf = *(uint8_t*)stream->state;
|
135
|
+
stream->state = (uint8_t*)stream->state + 1;
|
136
|
+
#endif
|
137
|
+
|
138
|
+
stream->bytes_left--;
|
139
|
+
|
140
|
+
return true;
|
141
|
+
}
|
142
|
+
|
143
|
+
pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
|
144
|
+
{
|
145
|
+
pb_istream_t stream;
|
146
|
+
#ifdef PB_BUFFER_ONLY
|
147
|
+
stream.callback = NULL;
|
148
|
+
#else
|
149
|
+
stream.callback = &buf_read;
|
150
|
+
#endif
|
151
|
+
stream.state = buf;
|
152
|
+
stream.bytes_left = bufsize;
|
153
|
+
#ifndef PB_NO_ERRMSG
|
154
|
+
stream.errmsg = NULL;
|
155
|
+
#endif
|
156
|
+
return stream;
|
157
|
+
}
|
158
|
+
|
159
|
+
/********************
|
160
|
+
* Helper functions *
|
161
|
+
********************/
|
162
|
+
|
163
|
+
static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
|
164
|
+
{
|
165
|
+
uint8_t byte;
|
166
|
+
uint32_t result;
|
167
|
+
|
168
|
+
if (!pb_readbyte(stream, &byte))
|
169
|
+
return false;
|
170
|
+
|
171
|
+
if ((byte & 0x80) == 0)
|
172
|
+
{
|
173
|
+
/* Quick case, 1 byte value */
|
174
|
+
result = byte;
|
175
|
+
}
|
176
|
+
else
|
177
|
+
{
|
178
|
+
/* Multibyte case */
|
179
|
+
uint8_t bitpos = 7;
|
180
|
+
result = byte & 0x7F;
|
181
|
+
|
182
|
+
do
|
183
|
+
{
|
184
|
+
if (bitpos >= 32)
|
185
|
+
PB_RETURN_ERROR(stream, "varint overflow");
|
186
|
+
|
187
|
+
if (!pb_readbyte(stream, &byte))
|
188
|
+
return false;
|
189
|
+
|
190
|
+
result |= (uint32_t)(byte & 0x7F) << bitpos;
|
191
|
+
bitpos = (uint8_t)(bitpos + 7);
|
192
|
+
} while (byte & 0x80);
|
193
|
+
}
|
194
|
+
|
195
|
+
*dest = result;
|
196
|
+
return true;
|
197
|
+
}
|
198
|
+
|
199
|
+
bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
|
200
|
+
{
|
201
|
+
uint8_t byte;
|
202
|
+
uint8_t bitpos = 0;
|
203
|
+
uint64_t result = 0;
|
204
|
+
|
205
|
+
do
|
206
|
+
{
|
207
|
+
if (bitpos >= 64)
|
208
|
+
PB_RETURN_ERROR(stream, "varint overflow");
|
209
|
+
|
210
|
+
if (!pb_readbyte(stream, &byte))
|
211
|
+
return false;
|
212
|
+
|
213
|
+
result |= (uint64_t)(byte & 0x7F) << bitpos;
|
214
|
+
bitpos = (uint8_t)(bitpos + 7);
|
215
|
+
} while (byte & 0x80);
|
216
|
+
|
217
|
+
*dest = result;
|
218
|
+
return true;
|
219
|
+
}
|
220
|
+
|
221
|
+
bool checkreturn pb_skip_varint(pb_istream_t *stream)
|
222
|
+
{
|
223
|
+
uint8_t byte;
|
224
|
+
do
|
225
|
+
{
|
226
|
+
if (!pb_read(stream, &byte, 1))
|
227
|
+
return false;
|
228
|
+
} while (byte & 0x80);
|
229
|
+
return true;
|
230
|
+
}
|
231
|
+
|
232
|
+
bool checkreturn pb_skip_string(pb_istream_t *stream)
|
233
|
+
{
|
234
|
+
uint32_t length;
|
235
|
+
if (!pb_decode_varint32(stream, &length))
|
236
|
+
return false;
|
237
|
+
|
238
|
+
return pb_read(stream, NULL, length);
|
239
|
+
}
|
240
|
+
|
241
|
+
bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
|
242
|
+
{
|
243
|
+
uint32_t temp;
|
244
|
+
*eof = false;
|
245
|
+
*wire_type = (pb_wire_type_t) 0;
|
246
|
+
*tag = 0;
|
247
|
+
|
248
|
+
if (!pb_decode_varint32(stream, &temp))
|
249
|
+
{
|
250
|
+
if (stream->bytes_left == 0)
|
251
|
+
*eof = true;
|
252
|
+
|
253
|
+
return false;
|
254
|
+
}
|
255
|
+
|
256
|
+
if (temp == 0)
|
257
|
+
{
|
258
|
+
*eof = true; /* Special feature: allow 0-terminated messages. */
|
259
|
+
return false;
|
260
|
+
}
|
261
|
+
|
262
|
+
*tag = temp >> 3;
|
263
|
+
*wire_type = (pb_wire_type_t)(temp & 7);
|
264
|
+
return true;
|
265
|
+
}
|
266
|
+
|
267
|
+
bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
|
268
|
+
{
|
269
|
+
switch (wire_type)
|
270
|
+
{
|
271
|
+
case PB_WT_VARINT: return pb_skip_varint(stream);
|
272
|
+
case PB_WT_64BIT: return pb_read(stream, NULL, 8);
|
273
|
+
case PB_WT_STRING: return pb_skip_string(stream);
|
274
|
+
case PB_WT_32BIT: return pb_read(stream, NULL, 4);
|
275
|
+
default: PB_RETURN_ERROR(stream, "invalid wire_type");
|
276
|
+
}
|
277
|
+
}
|
278
|
+
|
279
|
+
/* Read a raw value to buffer, for the purpose of passing it to callback as
|
280
|
+
* a substream. Size is maximum size on call, and actual size on return.
|
281
|
+
*/
|
282
|
+
static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
|
283
|
+
{
|
284
|
+
size_t max_size = *size;
|
285
|
+
switch (wire_type)
|
286
|
+
{
|
287
|
+
case PB_WT_VARINT:
|
288
|
+
*size = 0;
|
289
|
+
do
|
290
|
+
{
|
291
|
+
(*size)++;
|
292
|
+
if (*size > max_size) return false;
|
293
|
+
if (!pb_read(stream, buf, 1)) return false;
|
294
|
+
} while (*buf++ & 0x80);
|
295
|
+
return true;
|
296
|
+
|
297
|
+
case PB_WT_64BIT:
|
298
|
+
*size = 8;
|
299
|
+
return pb_read(stream, buf, 8);
|
300
|
+
|
301
|
+
case PB_WT_32BIT:
|
302
|
+
*size = 4;
|
303
|
+
return pb_read(stream, buf, 4);
|
304
|
+
|
305
|
+
default: PB_RETURN_ERROR(stream, "invalid wire_type");
|
306
|
+
}
|
307
|
+
}
|
308
|
+
|
309
|
+
/* Decode string length from stream and return a substream with limited length.
|
310
|
+
* Remember to close the substream using pb_close_string_substream().
|
311
|
+
*/
|
312
|
+
bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
|
313
|
+
{
|
314
|
+
uint32_t size;
|
315
|
+
if (!pb_decode_varint32(stream, &size))
|
316
|
+
return false;
|
317
|
+
|
318
|
+
*substream = *stream;
|
319
|
+
if (substream->bytes_left < size)
|
320
|
+
PB_RETURN_ERROR(stream, "parent stream too short");
|
321
|
+
|
322
|
+
substream->bytes_left = size;
|
323
|
+
stream->bytes_left -= size;
|
324
|
+
return true;
|
325
|
+
}
|
326
|
+
|
327
|
+
void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
|
328
|
+
{
|
329
|
+
stream->state = substream->state;
|
330
|
+
|
331
|
+
#ifndef PB_NO_ERRMSG
|
332
|
+
stream->errmsg = substream->errmsg;
|
333
|
+
#endif
|
334
|
+
}
|
335
|
+
|
336
|
+
/*************************
|
337
|
+
* Decode a single field *
|
338
|
+
*************************/
|
339
|
+
|
340
|
+
static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
|
341
|
+
{
|
342
|
+
pb_type_t type;
|
343
|
+
pb_decoder_t func;
|
344
|
+
|
345
|
+
type = iter->pos->type;
|
346
|
+
func = PB_DECODERS[PB_LTYPE(type)];
|
347
|
+
|
348
|
+
switch (PB_HTYPE(type))
|
349
|
+
{
|
350
|
+
case PB_HTYPE_REQUIRED:
|
351
|
+
return func(stream, iter->pos, iter->pData);
|
352
|
+
|
353
|
+
case PB_HTYPE_OPTIONAL:
|
354
|
+
*(bool*)iter->pSize = true;
|
355
|
+
return func(stream, iter->pos, iter->pData);
|
356
|
+
|
357
|
+
case PB_HTYPE_REPEATED:
|
358
|
+
if (wire_type == PB_WT_STRING
|
359
|
+
&& PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
|
360
|
+
{
|
361
|
+
/* Packed array */
|
362
|
+
bool status = true;
|
363
|
+
pb_size_t *size = (pb_size_t*)iter->pSize;
|
364
|
+
pb_istream_t substream;
|
365
|
+
if (!pb_make_string_substream(stream, &substream))
|
366
|
+
return false;
|
367
|
+
|
368
|
+
while (substream.bytes_left > 0 && *size < iter->pos->array_size)
|
369
|
+
{
|
370
|
+
void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
|
371
|
+
if (!func(&substream, iter->pos, pItem))
|
372
|
+
{
|
373
|
+
status = false;
|
374
|
+
break;
|
375
|
+
}
|
376
|
+
(*size)++;
|
377
|
+
}
|
378
|
+
pb_close_string_substream(stream, &substream);
|
379
|
+
|
380
|
+
if (substream.bytes_left != 0)
|
381
|
+
PB_RETURN_ERROR(stream, "array overflow");
|
382
|
+
|
383
|
+
return status;
|
384
|
+
}
|
385
|
+
else
|
386
|
+
{
|
387
|
+
/* Repeated field */
|
388
|
+
pb_size_t *size = (pb_size_t*)iter->pSize;
|
389
|
+
void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
|
390
|
+
if (*size >= iter->pos->array_size)
|
391
|
+
PB_RETURN_ERROR(stream, "array overflow");
|
392
|
+
|
393
|
+
(*size)++;
|
394
|
+
return func(stream, iter->pos, pItem);
|
395
|
+
}
|
396
|
+
|
397
|
+
case PB_HTYPE_ONEOF:
|
398
|
+
*(pb_size_t*)iter->pSize = iter->pos->tag;
|
399
|
+
if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
|
400
|
+
{
|
401
|
+
/* We memset to zero so that any callbacks are set to NULL.
|
402
|
+
* Then set any default values. */
|
403
|
+
memset(iter->pData, 0, iter->pos->data_size);
|
404
|
+
pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData);
|
405
|
+
}
|
406
|
+
return func(stream, iter->pos, iter->pData);
|
407
|
+
|
408
|
+
default:
|
409
|
+
PB_RETURN_ERROR(stream, "invalid field type");
|
410
|
+
}
|
411
|
+
}
|
412
|
+
|
413
|
+
#ifdef PB_ENABLE_MALLOC
|
414
|
+
/* Allocate storage for the field and store the pointer at iter->pData.
|
415
|
+
* array_size is the number of entries to reserve in an array.
|
416
|
+
* Zero size is not allowed, use pb_free() for releasing.
|
417
|
+
*/
|
418
|
+
static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
|
419
|
+
{
|
420
|
+
void *ptr = *(void**)pData;
|
421
|
+
|
422
|
+
if (data_size == 0 || array_size == 0)
|
423
|
+
PB_RETURN_ERROR(stream, "invalid size");
|
424
|
+
|
425
|
+
/* Check for multiplication overflows.
|
426
|
+
* This code avoids the costly division if the sizes are small enough.
|
427
|
+
* Multiplication is safe as long as only half of bits are set
|
428
|
+
* in either multiplicand.
|
429
|
+
*/
|
430
|
+
{
|
431
|
+
const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
|
432
|
+
if (data_size >= check_limit || array_size >= check_limit)
|
433
|
+
{
|
434
|
+
const size_t size_max = (size_t)-1;
|
435
|
+
if (size_max / array_size < data_size)
|
436
|
+
{
|
437
|
+
PB_RETURN_ERROR(stream, "size too large");
|
438
|
+
}
|
439
|
+
}
|
440
|
+
}
|
441
|
+
|
442
|
+
/* Allocate new or expand previous allocation */
|
443
|
+
/* Note: on failure the old pointer will remain in the structure,
|
444
|
+
* the message must be freed by caller also on error return. */
|
445
|
+
ptr = pb_realloc(ptr, array_size * data_size);
|
446
|
+
if (ptr == NULL)
|
447
|
+
PB_RETURN_ERROR(stream, "realloc failed");
|
448
|
+
|
449
|
+
*(void**)pData = ptr;
|
450
|
+
return true;
|
451
|
+
}
|
452
|
+
|
453
|
+
/* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
|
454
|
+
static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
|
455
|
+
{
|
456
|
+
if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
|
457
|
+
PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
|
458
|
+
{
|
459
|
+
*(void**)pItem = NULL;
|
460
|
+
}
|
461
|
+
else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
|
462
|
+
{
|
463
|
+
pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
|
464
|
+
}
|
465
|
+
}
|
466
|
+
#endif
|
467
|
+
|
468
|
+
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
|
469
|
+
{
|
470
|
+
#ifndef PB_ENABLE_MALLOC
|
471
|
+
PB_UNUSED(wire_type);
|
472
|
+
PB_UNUSED(iter);
|
473
|
+
PB_RETURN_ERROR(stream, "no malloc support");
|
474
|
+
#else
|
475
|
+
pb_type_t type;
|
476
|
+
pb_decoder_t func;
|
477
|
+
|
478
|
+
type = iter->pos->type;
|
479
|
+
func = PB_DECODERS[PB_LTYPE(type)];
|
480
|
+
|
481
|
+
switch (PB_HTYPE(type))
|
482
|
+
{
|
483
|
+
case PB_HTYPE_REQUIRED:
|
484
|
+
case PB_HTYPE_OPTIONAL:
|
485
|
+
case PB_HTYPE_ONEOF:
|
486
|
+
if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
|
487
|
+
*(void**)iter->pData != NULL)
|
488
|
+
{
|
489
|
+
/* Duplicate field, have to release the old allocation first. */
|
490
|
+
pb_release_single_field(iter);
|
491
|
+
}
|
492
|
+
|
493
|
+
if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
|
494
|
+
{
|
495
|
+
*(pb_size_t*)iter->pSize = iter->pos->tag;
|
496
|
+
}
|
497
|
+
|
498
|
+
if (PB_LTYPE(type) == PB_LTYPE_STRING ||
|
499
|
+
PB_LTYPE(type) == PB_LTYPE_BYTES)
|
500
|
+
{
|
501
|
+
return func(stream, iter->pos, iter->pData);
|
502
|
+
}
|
503
|
+
else
|
504
|
+
{
|
505
|
+
if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
|
506
|
+
return false;
|
507
|
+
|
508
|
+
initialize_pointer_field(*(void**)iter->pData, iter);
|
509
|
+
return func(stream, iter->pos, *(void**)iter->pData);
|
510
|
+
}
|
511
|
+
|
512
|
+
case PB_HTYPE_REPEATED:
|
513
|
+
if (wire_type == PB_WT_STRING
|
514
|
+
&& PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
|
515
|
+
{
|
516
|
+
/* Packed array, multiple items come in at once. */
|
517
|
+
bool status = true;
|
518
|
+
pb_size_t *size = (pb_size_t*)iter->pSize;
|
519
|
+
size_t allocated_size = *size;
|
520
|
+
void *pItem;
|
521
|
+
pb_istream_t substream;
|
522
|
+
|
523
|
+
if (!pb_make_string_substream(stream, &substream))
|
524
|
+
return false;
|
525
|
+
|
526
|
+
while (substream.bytes_left)
|
527
|
+
{
|
528
|
+
if ((size_t)*size + 1 > allocated_size)
|
529
|
+
{
|
530
|
+
/* Allocate more storage. This tries to guess the
|
531
|
+
* number of remaining entries. Round the division
|
532
|
+
* upwards. */
|
533
|
+
allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
|
534
|
+
|
535
|
+
if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
|
536
|
+
{
|
537
|
+
status = false;
|
538
|
+
break;
|
539
|
+
}
|
540
|
+
}
|
541
|
+
|
542
|
+
/* Decode the array entry */
|
543
|
+
pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
|
544
|
+
initialize_pointer_field(pItem, iter);
|
545
|
+
if (!func(&substream, iter->pos, pItem))
|
546
|
+
{
|
547
|
+
status = false;
|
548
|
+
break;
|
549
|
+
}
|
550
|
+
|
551
|
+
if (*size == PB_SIZE_MAX)
|
552
|
+
{
|
553
|
+
#ifndef PB_NO_ERRMSG
|
554
|
+
stream->errmsg = "too many array entries";
|
555
|
+
#endif
|
556
|
+
status = false;
|
557
|
+
break;
|
558
|
+
}
|
559
|
+
|
560
|
+
(*size)++;
|
561
|
+
}
|
562
|
+
pb_close_string_substream(stream, &substream);
|
563
|
+
|
564
|
+
return status;
|
565
|
+
}
|
566
|
+
else
|
567
|
+
{
|
568
|
+
/* Normal repeated field, i.e. only one item at a time. */
|
569
|
+
pb_size_t *size = (pb_size_t*)iter->pSize;
|
570
|
+
void *pItem;
|
571
|
+
|
572
|
+
if (*size == PB_SIZE_MAX)
|
573
|
+
PB_RETURN_ERROR(stream, "too many array entries");
|
574
|
+
|
575
|
+
(*size)++;
|
576
|
+
if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
|
577
|
+
return false;
|
578
|
+
|
579
|
+
pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
|
580
|
+
initialize_pointer_field(pItem, iter);
|
581
|
+
return func(stream, iter->pos, pItem);
|
582
|
+
}
|
583
|
+
|
584
|
+
default:
|
585
|
+
PB_RETURN_ERROR(stream, "invalid field type");
|
586
|
+
}
|
587
|
+
#endif
|
588
|
+
}
|
589
|
+
|
590
|
+
static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
|
591
|
+
{
|
592
|
+
pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
|
593
|
+
|
594
|
+
#ifdef PB_OLD_CALLBACK_STYLE
|
595
|
+
void *arg = pCallback->arg;
|
596
|
+
#else
|
597
|
+
void **arg = &(pCallback->arg);
|
598
|
+
#endif
|
599
|
+
|
600
|
+
if (pCallback->funcs.decode == NULL)
|
601
|
+
return pb_skip_field(stream, wire_type);
|
602
|
+
|
603
|
+
if (wire_type == PB_WT_STRING)
|
604
|
+
{
|
605
|
+
pb_istream_t substream;
|
606
|
+
|
607
|
+
if (!pb_make_string_substream(stream, &substream))
|
608
|
+
return false;
|
609
|
+
|
610
|
+
do
|
611
|
+
{
|
612
|
+
if (!pCallback->funcs.decode(&substream, iter->pos, arg))
|
613
|
+
PB_RETURN_ERROR(stream, "callback failed");
|
614
|
+
} while (substream.bytes_left);
|
615
|
+
|
616
|
+
pb_close_string_substream(stream, &substream);
|
617
|
+
return true;
|
618
|
+
}
|
619
|
+
else
|
620
|
+
{
|
621
|
+
/* Copy the single scalar value to stack.
|
622
|
+
* This is required so that we can limit the stream length,
|
623
|
+
* which in turn allows to use same callback for packed and
|
624
|
+
* not-packed fields. */
|
625
|
+
pb_istream_t substream;
|
626
|
+
uint8_t buffer[10];
|
627
|
+
size_t size = sizeof(buffer);
|
628
|
+
|
629
|
+
if (!read_raw_value(stream, wire_type, buffer, &size))
|
630
|
+
return false;
|
631
|
+
substream = pb_istream_from_buffer(buffer, size);
|
632
|
+
|
633
|
+
return pCallback->funcs.decode(&substream, iter->pos, arg);
|
634
|
+
}
|
635
|
+
}
|
636
|
+
|
637
|
+
static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
|
638
|
+
{
|
639
|
+
#ifdef PB_ENABLE_MALLOC
|
640
|
+
/* When decoding an oneof field, check if there is old data that must be
|
641
|
+
* released first. */
|
642
|
+
if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
|
643
|
+
{
|
644
|
+
if (!pb_release_union_field(stream, iter))
|
645
|
+
return false;
|
646
|
+
}
|
647
|
+
#endif
|
648
|
+
|
649
|
+
switch (PB_ATYPE(iter->pos->type))
|
650
|
+
{
|
651
|
+
case PB_ATYPE_STATIC:
|
652
|
+
return decode_static_field(stream, wire_type, iter);
|
653
|
+
|
654
|
+
case PB_ATYPE_POINTER:
|
655
|
+
return decode_pointer_field(stream, wire_type, iter);
|
656
|
+
|
657
|
+
case PB_ATYPE_CALLBACK:
|
658
|
+
return decode_callback_field(stream, wire_type, iter);
|
659
|
+
|
660
|
+
default:
|
661
|
+
PB_RETURN_ERROR(stream, "invalid field type");
|
662
|
+
}
|
663
|
+
}
|
664
|
+
|
665
|
+
static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
|
666
|
+
{
|
667
|
+
/* Fake a field iterator for the extension field.
|
668
|
+
* It is not actually safe to advance this iterator, but decode_field
|
669
|
+
* will not even try to. */
|
670
|
+
const pb_field_t *field = (const pb_field_t*)extension->type->arg;
|
671
|
+
(void)pb_field_iter_begin(iter, field, extension->dest);
|
672
|
+
iter->pData = extension->dest;
|
673
|
+
iter->pSize = &extension->found;
|
674
|
+
|
675
|
+
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
|
676
|
+
{
|
677
|
+
/* For pointer extensions, the pointer is stored directly
|
678
|
+
* in the extension structure. This avoids having an extra
|
679
|
+
* indirection. */
|
680
|
+
iter->pData = &extension->dest;
|
681
|
+
}
|
682
|
+
}
|
683
|
+
|
684
|
+
/* Default handler for extension fields. Expects a pb_field_t structure
|
685
|
+
* in extension->type->arg. */
|
686
|
+
static bool checkreturn default_extension_decoder(pb_istream_t *stream,
|
687
|
+
pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
|
688
|
+
{
|
689
|
+
const pb_field_t *field = (const pb_field_t*)extension->type->arg;
|
690
|
+
pb_field_iter_t iter;
|
691
|
+
|
692
|
+
if (field->tag != tag)
|
693
|
+
return true;
|
694
|
+
|
695
|
+
iter_from_extension(&iter, extension);
|
696
|
+
extension->found = true;
|
697
|
+
return decode_field(stream, wire_type, &iter);
|
698
|
+
}
|
699
|
+
|
700
|
+
/* Try to decode an unknown field as an extension field. Tries each extension
|
701
|
+
* decoder in turn, until one of them handles the field or loop ends. */
|
702
|
+
static bool checkreturn decode_extension(pb_istream_t *stream,
|
703
|
+
uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
|
704
|
+
{
|
705
|
+
pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
|
706
|
+
size_t pos = stream->bytes_left;
|
707
|
+
|
708
|
+
while (extension != NULL && pos == stream->bytes_left)
|
709
|
+
{
|
710
|
+
bool status;
|
711
|
+
if (extension->type->decode)
|
712
|
+
status = extension->type->decode(stream, extension, tag, wire_type);
|
713
|
+
else
|
714
|
+
status = default_extension_decoder(stream, extension, tag, wire_type);
|
715
|
+
|
716
|
+
if (!status)
|
717
|
+
return false;
|
718
|
+
|
719
|
+
extension = extension->next;
|
720
|
+
}
|
721
|
+
|
722
|
+
return true;
|
723
|
+
}
|
724
|
+
|
725
|
+
/* Step through the iterator until an extension field is found or until all
|
726
|
+
* entries have been checked. There can be only one extension field per
|
727
|
+
* message. Returns false if no extension field is found. */
|
728
|
+
static bool checkreturn find_extension_field(pb_field_iter_t *iter)
|
729
|
+
{
|
730
|
+
const pb_field_t *start = iter->pos;
|
731
|
+
|
732
|
+
do {
|
733
|
+
if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
|
734
|
+
return true;
|
735
|
+
(void)pb_field_iter_next(iter);
|
736
|
+
} while (iter->pos != start);
|
737
|
+
|
738
|
+
return false;
|
739
|
+
}
|
740
|
+
|
741
|
+
/* Initialize message fields to default values, recursively */
|
742
|
+
static void pb_field_set_to_default(pb_field_iter_t *iter)
|
743
|
+
{
|
744
|
+
pb_type_t type;
|
745
|
+
type = iter->pos->type;
|
746
|
+
|
747
|
+
if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
|
748
|
+
{
|
749
|
+
pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
|
750
|
+
while (ext != NULL)
|
751
|
+
{
|
752
|
+
pb_field_iter_t ext_iter;
|
753
|
+
ext->found = false;
|
754
|
+
iter_from_extension(&ext_iter, ext);
|
755
|
+
pb_field_set_to_default(&ext_iter);
|
756
|
+
ext = ext->next;
|
757
|
+
}
|
758
|
+
}
|
759
|
+
else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
|
760
|
+
{
|
761
|
+
bool init_data = true;
|
762
|
+
if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
|
763
|
+
{
|
764
|
+
/* Set has_field to false. Still initialize the optional field
|
765
|
+
* itself also. */
|
766
|
+
*(bool*)iter->pSize = false;
|
767
|
+
}
|
768
|
+
else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
|
769
|
+
PB_HTYPE(type) == PB_HTYPE_ONEOF)
|
770
|
+
{
|
771
|
+
/* REPEATED: Set array count to 0, no need to initialize contents.
|
772
|
+
ONEOF: Set which_field to 0. */
|
773
|
+
*(pb_size_t*)iter->pSize = 0;
|
774
|
+
init_data = false;
|
775
|
+
}
|
776
|
+
|
777
|
+
if (init_data)
|
778
|
+
{
|
779
|
+
if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
|
780
|
+
{
|
781
|
+
/* Initialize submessage to defaults */
|
782
|
+
pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
|
783
|
+
}
|
784
|
+
else if (iter->pos->ptr != NULL)
|
785
|
+
{
|
786
|
+
/* Initialize to default value */
|
787
|
+
memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
|
788
|
+
}
|
789
|
+
else
|
790
|
+
{
|
791
|
+
/* Initialize to zeros */
|
792
|
+
memset(iter->pData, 0, iter->pos->data_size);
|
793
|
+
}
|
794
|
+
}
|
795
|
+
}
|
796
|
+
else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
|
797
|
+
{
|
798
|
+
/* Initialize the pointer to NULL. */
|
799
|
+
*(void**)iter->pData = NULL;
|
800
|
+
|
801
|
+
/* Initialize array count to 0. */
|
802
|
+
if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
|
803
|
+
PB_HTYPE(type) == PB_HTYPE_ONEOF)
|
804
|
+
{
|
805
|
+
*(pb_size_t*)iter->pSize = 0;
|
806
|
+
}
|
807
|
+
}
|
808
|
+
else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
|
809
|
+
{
|
810
|
+
/* Don't overwrite callback */
|
811
|
+
}
|
812
|
+
}
|
813
|
+
|
814
|
+
static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
|
815
|
+
{
|
816
|
+
pb_field_iter_t iter;
|
817
|
+
|
818
|
+
if (!pb_field_iter_begin(&iter, fields, dest_struct))
|
819
|
+
return; /* Empty message type */
|
820
|
+
|
821
|
+
do
|
822
|
+
{
|
823
|
+
pb_field_set_to_default(&iter);
|
824
|
+
} while (pb_field_iter_next(&iter));
|
825
|
+
}
|
826
|
+
|
827
|
+
/*********************
|
828
|
+
* Decode all fields *
|
829
|
+
*********************/
|
830
|
+
|
831
|
+
bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
|
832
|
+
{
|
833
|
+
uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
834
|
+
uint32_t extension_range_start = 0;
|
835
|
+
pb_field_iter_t iter;
|
836
|
+
|
837
|
+
/* Return value ignored, as empty message types will be correctly handled by
|
838
|
+
* pb_field_iter_find() anyway. */
|
839
|
+
(void)pb_field_iter_begin(&iter, fields, dest_struct);
|
840
|
+
|
841
|
+
while (stream->bytes_left)
|
842
|
+
{
|
843
|
+
uint32_t tag;
|
844
|
+
pb_wire_type_t wire_type;
|
845
|
+
bool eof;
|
846
|
+
|
847
|
+
if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
|
848
|
+
{
|
849
|
+
if (eof)
|
850
|
+
break;
|
851
|
+
else
|
852
|
+
return false;
|
853
|
+
}
|
854
|
+
|
855
|
+
if (!pb_field_iter_find(&iter, tag))
|
856
|
+
{
|
857
|
+
/* No match found, check if it matches an extension. */
|
858
|
+
if (tag >= extension_range_start)
|
859
|
+
{
|
860
|
+
if (!find_extension_field(&iter))
|
861
|
+
extension_range_start = (uint32_t)-1;
|
862
|
+
else
|
863
|
+
extension_range_start = iter.pos->tag;
|
864
|
+
|
865
|
+
if (tag >= extension_range_start)
|
866
|
+
{
|
867
|
+
size_t pos = stream->bytes_left;
|
868
|
+
|
869
|
+
if (!decode_extension(stream, tag, wire_type, &iter))
|
870
|
+
return false;
|
871
|
+
|
872
|
+
if (pos != stream->bytes_left)
|
873
|
+
{
|
874
|
+
/* The field was handled */
|
875
|
+
continue;
|
876
|
+
}
|
877
|
+
}
|
878
|
+
}
|
879
|
+
|
880
|
+
/* No match found, skip data */
|
881
|
+
if (!pb_skip_field(stream, wire_type))
|
882
|
+
return false;
|
883
|
+
continue;
|
884
|
+
}
|
885
|
+
|
886
|
+
if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
|
887
|
+
&& iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
|
888
|
+
{
|
889
|
+
uint8_t tmp = (uint8_t)(1 << (iter.required_field_index & 7));
|
890
|
+
fields_seen[iter.required_field_index >> 3] |= tmp;
|
891
|
+
}
|
892
|
+
|
893
|
+
if (!decode_field(stream, wire_type, &iter))
|
894
|
+
return false;
|
895
|
+
}
|
896
|
+
|
897
|
+
/* Check that all required fields were present. */
|
898
|
+
{
|
899
|
+
/* First figure out the number of required fields by
|
900
|
+
* seeking to the end of the field array. Usually we
|
901
|
+
* are already close to end after decoding.
|
902
|
+
*/
|
903
|
+
unsigned req_field_count;
|
904
|
+
pb_type_t last_type;
|
905
|
+
unsigned i;
|
906
|
+
do {
|
907
|
+
req_field_count = iter.required_field_index;
|
908
|
+
last_type = iter.pos->type;
|
909
|
+
} while (pb_field_iter_next(&iter));
|
910
|
+
|
911
|
+
/* Fixup if last field was also required. */
|
912
|
+
if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
|
913
|
+
req_field_count++;
|
914
|
+
|
915
|
+
/* Check the whole bytes */
|
916
|
+
for (i = 0; i < (req_field_count >> 3); i++)
|
917
|
+
{
|
918
|
+
if (fields_seen[i] != 0xFF)
|
919
|
+
PB_RETURN_ERROR(stream, "missing required field");
|
920
|
+
}
|
921
|
+
|
922
|
+
/* Check the remaining bits */
|
923
|
+
if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
|
924
|
+
PB_RETURN_ERROR(stream, "missing required field");
|
925
|
+
}
|
926
|
+
|
927
|
+
return true;
|
928
|
+
}
|
929
|
+
|
930
|
+
bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
|
931
|
+
{
|
932
|
+
bool status;
|
933
|
+
pb_message_set_to_defaults(fields, dest_struct);
|
934
|
+
status = pb_decode_noinit(stream, fields, dest_struct);
|
935
|
+
|
936
|
+
#ifdef PB_ENABLE_MALLOC
|
937
|
+
if (!status)
|
938
|
+
pb_release(fields, dest_struct);
|
939
|
+
#endif
|
940
|
+
|
941
|
+
return status;
|
942
|
+
}
|
943
|
+
|
944
|
+
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
|
945
|
+
{
|
946
|
+
pb_istream_t substream;
|
947
|
+
bool status;
|
948
|
+
|
949
|
+
if (!pb_make_string_substream(stream, &substream))
|
950
|
+
return false;
|
951
|
+
|
952
|
+
status = pb_decode(&substream, fields, dest_struct);
|
953
|
+
pb_close_string_substream(stream, &substream);
|
954
|
+
return status;
|
955
|
+
}
|
956
|
+
|
957
|
+
#ifdef PB_ENABLE_MALLOC
|
958
|
+
/* Given an oneof field, if there has already been a field inside this oneof,
|
959
|
+
* release it before overwriting with a different one. */
|
960
|
+
static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
|
961
|
+
{
|
962
|
+
pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
|
963
|
+
pb_size_t new_tag = iter->pos->tag; /* New which_ value */
|
964
|
+
|
965
|
+
if (old_tag == 0)
|
966
|
+
return true; /* Ok, no old data in union */
|
967
|
+
|
968
|
+
if (old_tag == new_tag)
|
969
|
+
return true; /* Ok, old data is of same type => merge */
|
970
|
+
|
971
|
+
/* Release old data. The find can fail if the message struct contains
|
972
|
+
* invalid data. */
|
973
|
+
if (!pb_field_iter_find(iter, old_tag))
|
974
|
+
PB_RETURN_ERROR(stream, "invalid union tag");
|
975
|
+
|
976
|
+
pb_release_single_field(iter);
|
977
|
+
|
978
|
+
/* Restore iterator to where it should be.
|
979
|
+
* This shouldn't fail unless the pb_field_t structure is corrupted. */
|
980
|
+
if (!pb_field_iter_find(iter, new_tag))
|
981
|
+
PB_RETURN_ERROR(stream, "iterator error");
|
982
|
+
|
983
|
+
return true;
|
984
|
+
}
|
985
|
+
|
986
|
+
static void pb_release_single_field(const pb_field_iter_t *iter)
|
987
|
+
{
|
988
|
+
pb_type_t type;
|
989
|
+
type = iter->pos->type;
|
990
|
+
|
991
|
+
if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
|
992
|
+
{
|
993
|
+
if (*(pb_size_t*)iter->pSize != iter->pos->tag)
|
994
|
+
return; /* This is not the current field in the union */
|
995
|
+
}
|
996
|
+
|
997
|
+
/* Release anything contained inside an extension or submsg.
|
998
|
+
* This has to be done even if the submsg itself is statically
|
999
|
+
* allocated. */
|
1000
|
+
if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
|
1001
|
+
{
|
1002
|
+
/* Release fields from all extensions in the linked list */
|
1003
|
+
pb_extension_t *ext = *(pb_extension_t**)iter->pData;
|
1004
|
+
while (ext != NULL)
|
1005
|
+
{
|
1006
|
+
pb_field_iter_t ext_iter;
|
1007
|
+
iter_from_extension(&ext_iter, ext);
|
1008
|
+
pb_release_single_field(&ext_iter);
|
1009
|
+
ext = ext->next;
|
1010
|
+
}
|
1011
|
+
}
|
1012
|
+
else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
|
1013
|
+
{
|
1014
|
+
/* Release fields in submessage or submsg array */
|
1015
|
+
void *pItem = iter->pData;
|
1016
|
+
pb_size_t count = 1;
|
1017
|
+
|
1018
|
+
if (PB_ATYPE(type) == PB_ATYPE_POINTER)
|
1019
|
+
{
|
1020
|
+
pItem = *(void**)iter->pData;
|
1021
|
+
}
|
1022
|
+
|
1023
|
+
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
|
1024
|
+
{
|
1025
|
+
count = *(pb_size_t*)iter->pSize;
|
1026
|
+
}
|
1027
|
+
|
1028
|
+
if (pItem)
|
1029
|
+
{
|
1030
|
+
while (count--)
|
1031
|
+
{
|
1032
|
+
pb_release((const pb_field_t*)iter->pos->ptr, pItem);
|
1033
|
+
pItem = (uint8_t*)pItem + iter->pos->data_size;
|
1034
|
+
}
|
1035
|
+
}
|
1036
|
+
}
|
1037
|
+
|
1038
|
+
if (PB_ATYPE(type) == PB_ATYPE_POINTER)
|
1039
|
+
{
|
1040
|
+
if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
|
1041
|
+
(PB_LTYPE(type) == PB_LTYPE_STRING ||
|
1042
|
+
PB_LTYPE(type) == PB_LTYPE_BYTES))
|
1043
|
+
{
|
1044
|
+
/* Release entries in repeated string or bytes array */
|
1045
|
+
void **pItem = *(void***)iter->pData;
|
1046
|
+
pb_size_t count = *(pb_size_t*)iter->pSize;
|
1047
|
+
while (count--)
|
1048
|
+
{
|
1049
|
+
pb_free(*pItem);
|
1050
|
+
*pItem++ = NULL;
|
1051
|
+
}
|
1052
|
+
}
|
1053
|
+
|
1054
|
+
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
|
1055
|
+
{
|
1056
|
+
/* We are going to release the array, so set the size to 0 */
|
1057
|
+
*(pb_size_t*)iter->pSize = 0;
|
1058
|
+
}
|
1059
|
+
|
1060
|
+
/* Release main item */
|
1061
|
+
pb_free(*(void**)iter->pData);
|
1062
|
+
*(void**)iter->pData = NULL;
|
1063
|
+
}
|
1064
|
+
}
|
1065
|
+
|
1066
|
+
void pb_release(const pb_field_t fields[], void *dest_struct)
|
1067
|
+
{
|
1068
|
+
pb_field_iter_t iter;
|
1069
|
+
|
1070
|
+
if (!pb_field_iter_begin(&iter, fields, dest_struct))
|
1071
|
+
return; /* Empty message type */
|
1072
|
+
|
1073
|
+
do
|
1074
|
+
{
|
1075
|
+
pb_release_single_field(&iter);
|
1076
|
+
} while (pb_field_iter_next(&iter));
|
1077
|
+
}
|
1078
|
+
#endif
|
1079
|
+
|
1080
|
+
/* Field decoders */
|
1081
|
+
|
1082
|
+
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
|
1083
|
+
{
|
1084
|
+
uint64_t value;
|
1085
|
+
if (!pb_decode_varint(stream, &value))
|
1086
|
+
return false;
|
1087
|
+
|
1088
|
+
if (value & 1)
|
1089
|
+
*dest = (int64_t)(~(value >> 1));
|
1090
|
+
else
|
1091
|
+
*dest = (int64_t)(value >> 1);
|
1092
|
+
|
1093
|
+
return true;
|
1094
|
+
}
|
1095
|
+
|
1096
|
+
bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
|
1097
|
+
{
|
1098
|
+
#ifdef __BIG_ENDIAN__
|
1099
|
+
uint8_t *bytes = (uint8_t*)dest;
|
1100
|
+
uint8_t lebytes[4];
|
1101
|
+
|
1102
|
+
if (!pb_read(stream, lebytes, 4))
|
1103
|
+
return false;
|
1104
|
+
|
1105
|
+
bytes[0] = lebytes[3];
|
1106
|
+
bytes[1] = lebytes[2];
|
1107
|
+
bytes[2] = lebytes[1];
|
1108
|
+
bytes[3] = lebytes[0];
|
1109
|
+
return true;
|
1110
|
+
#else
|
1111
|
+
return pb_read(stream, (uint8_t*)dest, 4);
|
1112
|
+
#endif
|
1113
|
+
}
|
1114
|
+
|
1115
|
+
bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
|
1116
|
+
{
|
1117
|
+
#ifdef __BIG_ENDIAN__
|
1118
|
+
uint8_t *bytes = (uint8_t*)dest;
|
1119
|
+
uint8_t lebytes[8];
|
1120
|
+
|
1121
|
+
if (!pb_read(stream, lebytes, 8))
|
1122
|
+
return false;
|
1123
|
+
|
1124
|
+
bytes[0] = lebytes[7];
|
1125
|
+
bytes[1] = lebytes[6];
|
1126
|
+
bytes[2] = lebytes[5];
|
1127
|
+
bytes[3] = lebytes[4];
|
1128
|
+
bytes[4] = lebytes[3];
|
1129
|
+
bytes[5] = lebytes[2];
|
1130
|
+
bytes[6] = lebytes[1];
|
1131
|
+
bytes[7] = lebytes[0];
|
1132
|
+
return true;
|
1133
|
+
#else
|
1134
|
+
return pb_read(stream, (uint8_t*)dest, 8);
|
1135
|
+
#endif
|
1136
|
+
}
|
1137
|
+
|
1138
|
+
static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
1139
|
+
{
|
1140
|
+
uint64_t value;
|
1141
|
+
int64_t svalue;
|
1142
|
+
int64_t clamped;
|
1143
|
+
if (!pb_decode_varint(stream, &value))
|
1144
|
+
return false;
|
1145
|
+
|
1146
|
+
/* See issue 97: Google's C++ protobuf allows negative varint values to
|
1147
|
+
* be cast as int32_t, instead of the int64_t that should be used when
|
1148
|
+
* encoding. Previous nanopb versions had a bug in encoding. In order to
|
1149
|
+
* not break decoding of such messages, we cast <=32 bit fields to
|
1150
|
+
* int32_t first to get the sign correct.
|
1151
|
+
*/
|
1152
|
+
if (field->data_size == 8)
|
1153
|
+
svalue = (int64_t)value;
|
1154
|
+
else
|
1155
|
+
svalue = (int32_t)value;
|
1156
|
+
|
1157
|
+
switch (field->data_size)
|
1158
|
+
{
|
1159
|
+
case 1: clamped = *(int8_t*)dest = (int8_t)svalue; break;
|
1160
|
+
case 2: clamped = *(int16_t*)dest = (int16_t)svalue; break;
|
1161
|
+
case 4: clamped = *(int32_t*)dest = (int32_t)svalue; break;
|
1162
|
+
case 8: clamped = *(int64_t*)dest = svalue; break;
|
1163
|
+
default: PB_RETURN_ERROR(stream, "invalid data_size");
|
1164
|
+
}
|
1165
|
+
|
1166
|
+
if (clamped != svalue)
|
1167
|
+
PB_RETURN_ERROR(stream, "integer too large");
|
1168
|
+
|
1169
|
+
return true;
|
1170
|
+
}
|
1171
|
+
|
1172
|
+
static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
1173
|
+
{
|
1174
|
+
uint64_t value, clamped;
|
1175
|
+
if (!pb_decode_varint(stream, &value))
|
1176
|
+
return false;
|
1177
|
+
|
1178
|
+
switch (field->data_size)
|
1179
|
+
{
|
1180
|
+
case 1: clamped = *(uint8_t*)dest = (uint8_t)value; break;
|
1181
|
+
case 2: clamped = *(uint16_t*)dest = (uint16_t)value; break;
|
1182
|
+
case 4: clamped = *(uint32_t*)dest = (uint32_t)value; break;
|
1183
|
+
case 8: clamped = *(uint64_t*)dest = value; break;
|
1184
|
+
default: PB_RETURN_ERROR(stream, "invalid data_size");
|
1185
|
+
}
|
1186
|
+
|
1187
|
+
if (clamped != value)
|
1188
|
+
PB_RETURN_ERROR(stream, "integer too large");
|
1189
|
+
|
1190
|
+
return true;
|
1191
|
+
}
|
1192
|
+
|
1193
|
+
static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
1194
|
+
{
|
1195
|
+
int64_t value, clamped;
|
1196
|
+
if (!pb_decode_svarint(stream, &value))
|
1197
|
+
return false;
|
1198
|
+
|
1199
|
+
switch (field->data_size)
|
1200
|
+
{
|
1201
|
+
case 1: clamped = *(int8_t*)dest = (int8_t)value; break;
|
1202
|
+
case 2: clamped = *(int16_t*)dest = (int16_t)value; break;
|
1203
|
+
case 4: clamped = *(int32_t*)dest = (int32_t)value; break;
|
1204
|
+
case 8: clamped = *(int64_t*)dest = value; break;
|
1205
|
+
default: PB_RETURN_ERROR(stream, "invalid data_size");
|
1206
|
+
}
|
1207
|
+
|
1208
|
+
if (clamped != value)
|
1209
|
+
PB_RETURN_ERROR(stream, "integer too large");
|
1210
|
+
|
1211
|
+
return true;
|
1212
|
+
}
|
1213
|
+
|
1214
|
+
static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
1215
|
+
{
|
1216
|
+
PB_UNUSED(field);
|
1217
|
+
return pb_decode_fixed32(stream, dest);
|
1218
|
+
}
|
1219
|
+
|
1220
|
+
static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
1221
|
+
{
|
1222
|
+
PB_UNUSED(field);
|
1223
|
+
return pb_decode_fixed64(stream, dest);
|
1224
|
+
}
|
1225
|
+
|
1226
|
+
static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
1227
|
+
{
|
1228
|
+
uint32_t size;
|
1229
|
+
size_t alloc_size;
|
1230
|
+
pb_bytes_array_t *bdest;
|
1231
|
+
|
1232
|
+
if (!pb_decode_varint32(stream, &size))
|
1233
|
+
return false;
|
1234
|
+
|
1235
|
+
if (size > PB_SIZE_MAX)
|
1236
|
+
PB_RETURN_ERROR(stream, "bytes overflow");
|
1237
|
+
|
1238
|
+
alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
|
1239
|
+
if (size > alloc_size)
|
1240
|
+
PB_RETURN_ERROR(stream, "size too large");
|
1241
|
+
|
1242
|
+
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
|
1243
|
+
{
|
1244
|
+
#ifndef PB_ENABLE_MALLOC
|
1245
|
+
PB_RETURN_ERROR(stream, "no malloc support");
|
1246
|
+
#else
|
1247
|
+
if (!allocate_field(stream, dest, alloc_size, 1))
|
1248
|
+
return false;
|
1249
|
+
bdest = *(pb_bytes_array_t**)dest;
|
1250
|
+
#endif
|
1251
|
+
}
|
1252
|
+
else
|
1253
|
+
{
|
1254
|
+
if (alloc_size > field->data_size)
|
1255
|
+
PB_RETURN_ERROR(stream, "bytes overflow");
|
1256
|
+
bdest = (pb_bytes_array_t*)dest;
|
1257
|
+
}
|
1258
|
+
|
1259
|
+
bdest->size = (pb_size_t)size;
|
1260
|
+
return pb_read(stream, bdest->bytes, size);
|
1261
|
+
}
|
1262
|
+
|
1263
|
+
static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
1264
|
+
{
|
1265
|
+
uint32_t size;
|
1266
|
+
size_t alloc_size;
|
1267
|
+
bool status;
|
1268
|
+
if (!pb_decode_varint32(stream, &size))
|
1269
|
+
return false;
|
1270
|
+
|
1271
|
+
/* Space for null terminator */
|
1272
|
+
alloc_size = size + 1;
|
1273
|
+
|
1274
|
+
if (alloc_size < size)
|
1275
|
+
PB_RETURN_ERROR(stream, "size too large");
|
1276
|
+
|
1277
|
+
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
|
1278
|
+
{
|
1279
|
+
#ifndef PB_ENABLE_MALLOC
|
1280
|
+
PB_RETURN_ERROR(stream, "no malloc support");
|
1281
|
+
#else
|
1282
|
+
if (!allocate_field(stream, dest, alloc_size, 1))
|
1283
|
+
return false;
|
1284
|
+
dest = *(void**)dest;
|
1285
|
+
#endif
|
1286
|
+
}
|
1287
|
+
else
|
1288
|
+
{
|
1289
|
+
if (alloc_size > field->data_size)
|
1290
|
+
PB_RETURN_ERROR(stream, "string overflow");
|
1291
|
+
}
|
1292
|
+
|
1293
|
+
status = pb_read(stream, (uint8_t*)dest, size);
|
1294
|
+
*((uint8_t*)dest + size) = 0;
|
1295
|
+
return status;
|
1296
|
+
}
|
1297
|
+
|
1298
|
+
static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
1299
|
+
{
|
1300
|
+
bool status;
|
1301
|
+
pb_istream_t substream;
|
1302
|
+
const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
|
1303
|
+
|
1304
|
+
if (!pb_make_string_substream(stream, &substream))
|
1305
|
+
return false;
|
1306
|
+
|
1307
|
+
if (field->ptr == NULL)
|
1308
|
+
PB_RETURN_ERROR(stream, "invalid field descriptor");
|
1309
|
+
|
1310
|
+
/* New array entries need to be initialized, while required and optional
|
1311
|
+
* submessages have already been initialized in the top-level pb_decode. */
|
1312
|
+
if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
|
1313
|
+
status = pb_decode(&substream, submsg_fields, dest);
|
1314
|
+
else
|
1315
|
+
status = pb_decode_noinit(&substream, submsg_fields, dest);
|
1316
|
+
|
1317
|
+
pb_close_string_substream(stream, &substream);
|
1318
|
+
return status;
|
1319
|
+
}
|