Compare commits
22 Commits
nanopb-0.1
...
nanopb-0.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1aa61f108a | ||
|
|
b582bc9bf6 | ||
|
|
5703ad0c55 | ||
|
|
0a5b6852ec | ||
|
|
8524de39ce | ||
|
|
10b5da12dc | ||
|
|
d8bddabb83 | ||
|
|
01a1556898 | ||
|
|
efef38cf78 | ||
|
|
72cca8d783 | ||
|
|
7c5bb6541a | ||
|
|
9b1e1b440a | ||
|
|
78086cc27d | ||
|
|
67add3259a | ||
|
|
95eb4a5499 | ||
|
|
7bcf7ef579 | ||
|
|
99375a132f | ||
|
|
c07e576de8 | ||
|
|
3a919ddc5e | ||
|
|
0f1d5cca59 | ||
|
|
e18352d506 | ||
|
|
7e1059628c |
@@ -6,6 +6,26 @@ Nanopb: API reference
|
||||
|
||||
.. contents ::
|
||||
|
||||
Compilation options
|
||||
===================
|
||||
The following options can be specified using -D switch given to the C compiler:
|
||||
|
||||
============================ ================================================================================================
|
||||
__BIG_ENDIAN__ Set this if your platform stores integers and floats in big-endian format.
|
||||
Mixed-endian systems (different layout for ints and floats) are currently not supported.
|
||||
NANOPB_INTERNALS Set this to expose the field encoder functions that are hidden since nanopb-0.1.3.
|
||||
PB_MAX_REQUIRED_FIELDS Maximum number of required fields to check for presence. Default value is 64. Increases stack
|
||||
usage 1 byte per every 8 fields. Compiler warning will tell if you need this.
|
||||
PB_FIELD_16BIT Add support for tag numbers > 255 and fields larger than 255 bytes or 255 array entries.
|
||||
Increases code size 3 bytes per each field. Compiler error will tell if you need this.
|
||||
PB_FIELD_32BIT Add support for tag numbers > 65535 and fields larger than 65535 bytes or 65535 array entries.
|
||||
Increases code size 9 bytes per each field. Compiler error will tell if you need this.
|
||||
============================ ================================================================================================
|
||||
|
||||
The PB_MAX_REQUIRED_FIELDS, PB_FIELD_16BIT and PB_FIELD_32BIT settings allow raising some datatype limits to suit larger messages.
|
||||
Their need is recognized automatically by C-preprocessor #if-directives in the generated .pb.h files. The default setting is to use
|
||||
the smallest datatypes (least resources used).
|
||||
|
||||
pb.h
|
||||
====
|
||||
|
||||
@@ -66,7 +86,7 @@ Describes a single structure field with memory position in relation to others. T
|
||||
:array_size: Maximum number of entries in an array, if it is an array type.
|
||||
:ptr: Pointer to default value for optional fields, or to submessage description for PB_LTYPE_SUBMESSAGE.
|
||||
|
||||
The *uint8_t* datatypes limit the maximum size of a single item to 255 bytes and arrays to 255 items. Compiler will warn "Initializer too large for type" if the limits are exceeded. The types can be changed to larger ones if necessary.
|
||||
The *uint8_t* datatypes limit the maximum size of a single item to 255 bytes and arrays to 255 items. Compiler will give error if the values are too large. The types can be changed to larger ones by defining *PB_FIELD_16BIT*.
|
||||
|
||||
pb_bytes_array_t
|
||||
----------------
|
||||
@@ -283,6 +303,23 @@ Read data from input stream. Always use this function, don't try to call the str
|
||||
|
||||
End of file is signalled by *stream->bytes_left* being zero after pb_read returns false.
|
||||
|
||||
pb_decode
|
||||
---------
|
||||
Read and decode all fields of a structure. Reads until EOF on input stream. ::
|
||||
|
||||
bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
|
||||
|
||||
:stream: Input stream to read from.
|
||||
:fields: A field description array. Usually autogenerated.
|
||||
:dest_struct: Pointer to structure where data will be stored.
|
||||
:returns: True on success, false on IO error, on detectable errors in field description, if a field encoder returns false or if a required field is missing.
|
||||
|
||||
In Protocol Buffers binary format, EOF is only allowed between fields. If it happens anywhere else, pb_decode will return *false*. If pb_decode returns false, you cannot trust any of the data in the structure.
|
||||
|
||||
In addition to EOF, the pb_decode implementation supports terminating a message with a 0 byte. This is compatible with the official Protocol Buffers because 0 is never a valid field tag.
|
||||
|
||||
For optional fields, this function applies the default value and sets *has_<field>* to false if the field is not present.
|
||||
|
||||
pb_decode_varint
|
||||
----------------
|
||||
Read and decode a varint_ encoded integer. ::
|
||||
@@ -311,24 +348,30 @@ Skip a varint-length-prefixed string. This means skipping a value with wire type
|
||||
:stream: Input stream to read from.
|
||||
:returns: True on success, false on IO error or length exceeding uint32_t.
|
||||
|
||||
pb_decode
|
||||
---------
|
||||
Read and decode all fields of a structure. Reads until EOF on input stream. ::
|
||||
pb_decode_tag
|
||||
-------------
|
||||
Decode the tag that comes before field in the protobuf encoding::
|
||||
|
||||
bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
|
||||
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, int *tag, bool *eof);
|
||||
|
||||
:stream: Input stream to read from.
|
||||
:fields: A field description array. Usually autogenerated.
|
||||
:dest_struct: Pointer to structure where data will be stored.
|
||||
:returns: True on success, false on IO error, on detectable errors in field description, if a field encoder returns false or if a required field is missing.
|
||||
:wire_type: Pointer to variable where to store the wire type of the field.
|
||||
:tag: Pointer to variable where to store the tag of the field.
|
||||
:eof: Pointer to variable where to store end-of-file status.
|
||||
:returns: True on success, false on error or EOF.
|
||||
|
||||
In Protocol Buffers binary format, EOF is only allowed between fields. If it happens anywhere else, pb_decode will return *false*. If pb_decode returns false, you cannot trust any of the data in the structure.
|
||||
When the message (stream) ends, this function will return false and set *eof* to true. On other
|
||||
errors, *eof* will be set to false.
|
||||
|
||||
In addition to EOF, the pb_decode implementation supports terminating a message with a 0 byte. This is compatible with the official Protocol Buffers because 0 is never a valid field tag.
|
||||
pb_skip_field
|
||||
-------------
|
||||
Remove the data for a field from the stream, without actually decoding it::
|
||||
|
||||
For optional fields, this function applies the default value and sets *has_<field>* to false if the field is not present.
|
||||
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
|
||||
|
||||
Because of memory concerns, the detection of missing required fields is not perfect if the structure contains more than 32 fields.
|
||||
:stream: Input stream to read from.
|
||||
:wire_type: Type of field to skip.
|
||||
:returns: True on success, false on IO error.
|
||||
|
||||
.. sidebar:: Field decoders
|
||||
|
||||
|
||||
17
example_unions/Makefile
Normal file
17
example_unions/Makefile
Normal file
@@ -0,0 +1,17 @@
|
||||
CFLAGS=-ansi -Wall -Werror -I .. -g -O0
|
||||
DEPS=../pb_decode.c ../pb_decode.h ../pb_encode.c ../pb_encode.h ../pb.h
|
||||
|
||||
all: encode decode
|
||||
./encode 1 | ./decode
|
||||
./encode 2 | ./decode
|
||||
./encode 3 | ./decode
|
||||
|
||||
clean:
|
||||
rm -f encode unionproto.pb.h unionproto.pb.c
|
||||
|
||||
%: %.c $(DEPS) unionproto.pb.h unionproto.pb.c
|
||||
$(CC) $(CFLAGS) -o $@ $< ../pb_decode.c ../pb_encode.c unionproto.pb.c
|
||||
|
||||
unionproto.pb.h unionproto.pb.c: unionproto.proto ../generator/nanopb_generator.py
|
||||
protoc -I. -I../generator -I/usr/include -ounionproto.pb $<
|
||||
python ../generator/nanopb_generator.py unionproto.pb
|
||||
92
example_unions/decode.c
Normal file
92
example_unions/decode.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/* This program reads a message from stdin, detects its type and decodes it.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <pb_decode.h>
|
||||
#include "unionproto.pb.h"
|
||||
|
||||
/* This function reads manually the first tag from the stream and finds the
|
||||
* corresponding message type. It doesn't yet decode the actual message.
|
||||
*
|
||||
* Returns a pointer to the MsgType_fields array, as an identifier for the
|
||||
* message type. Returns null if the tag is of unknown type or an error occurs.
|
||||
*/
|
||||
const pb_field_t* decode_unionmessage_type(pb_istream_t *stream)
|
||||
{
|
||||
pb_wire_type_t wire_type;
|
||||
uint32_t tag;
|
||||
bool eof;
|
||||
|
||||
while (pb_decode_tag(stream, &wire_type, &tag, &eof))
|
||||
{
|
||||
if (wire_type == PB_WT_STRING)
|
||||
{
|
||||
const pb_field_t *field;
|
||||
for (field = UnionMessage_fields; field->tag != 0; field++)
|
||||
{
|
||||
if (field->tag == tag && (field->type & PB_LTYPE_SUBMESSAGE))
|
||||
{
|
||||
/* Found our field. */
|
||||
return field->ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Wasn't our field.. */
|
||||
pb_skip_field(stream, wire_type);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool decode_unionmessage_contents(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
|
||||
{
|
||||
pb_field_t field = {}; /* NB: Could get rid of this wrapper by fixing issue #2. */
|
||||
field.ptr = fields;
|
||||
|
||||
return pb_dec_submessage(stream, &field, dest_struct);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
/* Read the data into buffer */
|
||||
uint8_t buffer[512];
|
||||
size_t count = fread(buffer, 1, sizeof(buffer), stdin);
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, count);
|
||||
|
||||
const pb_field_t *type = decode_unionmessage_type(&stream);
|
||||
bool status = false;
|
||||
|
||||
if (type == MsgType1_fields)
|
||||
{
|
||||
MsgType1 msg = {};
|
||||
status = decode_unionmessage_contents(&stream, MsgType1_fields, &msg);
|
||||
printf("Got MsgType1: %d\n", msg.value);
|
||||
}
|
||||
else if (type == MsgType2_fields)
|
||||
{
|
||||
MsgType2 msg = {};
|
||||
status = decode_unionmessage_contents(&stream, MsgType2_fields, &msg);
|
||||
printf("Got MsgType2: %s\n", msg.value ? "true" : "false");
|
||||
}
|
||||
else if (type == MsgType3_fields)
|
||||
{
|
||||
MsgType3 msg = {};
|
||||
status = decode_unionmessage_contents(&stream, MsgType3_fields, &msg);
|
||||
printf("Got MsgType3: %d %d\n", msg.value1, msg.value2);
|
||||
}
|
||||
|
||||
if (!status)
|
||||
{
|
||||
printf("Decoding failed.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
85
example_unions/encode.c
Normal file
85
example_unions/encode.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/* This program takes a command line argument and encodes a message in
|
||||
* one of MsgType1, MsgType2 or MsgType3.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <pb_encode.h>
|
||||
#include "unionproto.pb.h"
|
||||
|
||||
/* This function is the core of the union encoding process. It handles
|
||||
* the top-level pb_field_t array manually, in order to encode a correct
|
||||
* field tag before the message. The pointer to MsgType_fields array is
|
||||
* used as an unique identifier for the message type.
|
||||
*/
|
||||
bool encode_unionmessage(pb_ostream_t *stream, const pb_field_t messagetype[], const void *message)
|
||||
{
|
||||
const pb_field_t *field;
|
||||
for (field = UnionMessage_fields; field->tag != 0; field++)
|
||||
{
|
||||
if (field->ptr == messagetype)
|
||||
{
|
||||
/* This is our field, encode the message using it. */
|
||||
if (!pb_encode_tag_for_field(stream, field))
|
||||
return false;
|
||||
|
||||
return pb_encode_submessage(stream, messagetype, message);
|
||||
}
|
||||
}
|
||||
|
||||
/* Didn't find the field for messagetype */
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s (1|2|3)\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t buffer[512];
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
|
||||
bool status = false;
|
||||
int msgtype = atoi(argv[1]);
|
||||
if (msgtype == 1)
|
||||
{
|
||||
/* Send message of type 1 */
|
||||
MsgType1 msg = {42};
|
||||
status = encode_unionmessage(&stream, MsgType1_fields, &msg);
|
||||
}
|
||||
else if (msgtype == 2)
|
||||
{
|
||||
/* Send message of type 2 */
|
||||
MsgType2 msg = {true};
|
||||
status = encode_unionmessage(&stream, MsgType2_fields, &msg);
|
||||
}
|
||||
else if (msgtype == 3)
|
||||
{
|
||||
/* Send message of type 3 */
|
||||
MsgType3 msg = {3, 1415};
|
||||
status = encode_unionmessage(&stream, MsgType3_fields, &msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unknown message type: %d\n", msgtype);
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (!status)
|
||||
{
|
||||
fprintf(stderr, "Encoding failed!\n");
|
||||
return 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
fwrite(buffer, 1, stream.bytes_written, stdout);
|
||||
return 0; /* Success */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
30
example_unions/unionproto.proto
Normal file
30
example_unions/unionproto.proto
Normal file
@@ -0,0 +1,30 @@
|
||||
// This is an example of how to handle 'union' style messages
|
||||
// with nanopb, without allocating memory for all the message types.
|
||||
//
|
||||
// There is no official type in Protocol Buffers for describing unions,
|
||||
// but they are commonly implemented by filling out exactly one of
|
||||
// several optional fields.
|
||||
|
||||
message MsgType1
|
||||
{
|
||||
required int32 value = 1;
|
||||
}
|
||||
|
||||
message MsgType2
|
||||
{
|
||||
required bool value = 1;
|
||||
}
|
||||
|
||||
message MsgType3
|
||||
{
|
||||
required int32 value1 = 1;
|
||||
required int32 value2 = 2;
|
||||
}
|
||||
|
||||
message UnionMessage
|
||||
{
|
||||
optional MsgType1 msg1 = 1;
|
||||
optional MsgType2 msg2 = 2;
|
||||
optional MsgType3 msg3 = 3;
|
||||
}
|
||||
|
||||
@@ -217,10 +217,10 @@ class Field:
|
||||
prev_field_name is the name of the previous field or None.
|
||||
'''
|
||||
result = ' {%d, ' % self.tag
|
||||
result += self.htype
|
||||
result += '(pb_type_t) ((int) ' + self.htype
|
||||
if self.ltype is not None:
|
||||
result += ' | ' + self.ltype
|
||||
result += ',\n'
|
||||
result += ' | (int) ' + self.ltype
|
||||
result += '),\n'
|
||||
|
||||
if prev_field_name is None:
|
||||
result += ' offsetof(%s, %s),' % (self.struct_name, self.name)
|
||||
@@ -251,6 +251,18 @@ class Field:
|
||||
result += '\n &%s_default}' % (self.struct_name + self.name)
|
||||
|
||||
return result
|
||||
|
||||
def largest_field_value(self):
|
||||
'''Determine if this field needs 16bit or 32bit pb_field_t structure to compile properly.
|
||||
Returns numeric value or a C-expression for assert.'''
|
||||
if self.ltype == 'PB_LTYPE_SUBMESSAGE':
|
||||
if self.htype == 'PB_HTYPE_ARRAY':
|
||||
return 'pb_membersize(%s, %s[0])' % (self.struct_name, self.name)
|
||||
else:
|
||||
return 'pb_membersize(%s, %s)' % (self.struct_name, self.name)
|
||||
|
||||
return max(self.tag, self.max_size, self.max_count)
|
||||
|
||||
|
||||
class Message:
|
||||
def __init__(self, names, desc):
|
||||
@@ -343,7 +355,7 @@ def toposort2(data):
|
||||
'''
|
||||
for k, v in data.items():
|
||||
v.discard(k) # Ignore self dependencies
|
||||
extra_items_in_deps = reduce(set.union, data.values()) - set(data.keys())
|
||||
extra_items_in_deps = reduce(set.union, data.values(), set()) - set(data.keys())
|
||||
data.update(dict([(item, set()) for item in extra_items_in_deps]))
|
||||
while True:
|
||||
ordered = set(item for item,dep in data.items() if not dep)
|
||||
@@ -402,6 +414,51 @@ def generate_header(dependencies, headername, enums, messages):
|
||||
for msg in messages:
|
||||
yield msg.fields_declaration() + '\n'
|
||||
|
||||
if messages:
|
||||
count_required_fields = lambda m: len([f for f in msg.fields if f.htype == 'PB_HTYPE_REQUIRED'])
|
||||
largest_msg = max(messages, key = count_required_fields)
|
||||
largest_count = count_required_fields(largest_msg)
|
||||
if largest_count > 64:
|
||||
yield '\n/* Check that missing required fields will be properly detected */\n'
|
||||
yield '#if PB_MAX_REQUIRED_FIELDS < %d\n' % largest_count
|
||||
yield '#error Properly detecting missing required fields in %s requires \\\n' % largest_msg.name
|
||||
yield ' setting PB_MAX_REQUIRED_FIELDS to %d or more.\n' % largest_count
|
||||
yield '#endif\n'
|
||||
|
||||
worst = 0
|
||||
worst_field = ''
|
||||
checks = []
|
||||
for msg in messages:
|
||||
for field in msg.fields:
|
||||
status = field.largest_field_value()
|
||||
if isinstance(status, (str, unicode)):
|
||||
checks.append(status)
|
||||
elif status > worst:
|
||||
worst = status
|
||||
worst_field = str(field.struct_name) + '.' + str(field.name)
|
||||
|
||||
if worst > 255 or checks:
|
||||
yield '\n/* Check that field information fits in pb_field_t */\n'
|
||||
|
||||
if worst < 65536:
|
||||
yield '#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)\n'
|
||||
if worst > 255:
|
||||
yield '#error Field descriptor for %s is too large. Define PB_FIELD_16BIT to fix this.\n' % worst_field
|
||||
else:
|
||||
assertion = ' && '.join(str(c) + ' < 256' for c in checks)
|
||||
yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_16BIT)\n' % assertion
|
||||
yield '#endif\n\n'
|
||||
|
||||
if worst > 65535 or checks:
|
||||
yield '#if !defined(PB_FIELD_32BIT)\n'
|
||||
if worst > 65535:
|
||||
yield '#error Field descriptor for %s is too large. Define PB_FIELD_32BIT to fix this.\n' % worst_field
|
||||
else:
|
||||
assertion = ' && '.join(str(c) + ' < 65536' for c in checks)
|
||||
yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_32BIT)\n' % assertion
|
||||
yield '#endif\n'
|
||||
|
||||
# End of header
|
||||
yield '\n#endif\n'
|
||||
|
||||
def generate_source(headername, enums, messages):
|
||||
|
||||
39
pb.h
39
pb.h
@@ -22,6 +22,21 @@
|
||||
#define UNUSED(x) (void)(x)
|
||||
#endif
|
||||
|
||||
/* Compile-time assertion, used for checking compatible compilation options. */
|
||||
#ifndef STATIC_ASSERT
|
||||
#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1];
|
||||
#endif
|
||||
|
||||
/* Number of required fields to keep track of
|
||||
* (change here or on compiler command line). */
|
||||
#ifndef PB_MAX_REQUIRED_FIELDS
|
||||
#define PB_MAX_REQUIRED_FIELDS 64
|
||||
#endif
|
||||
|
||||
#if PB_MAX_REQUIRED_FIELDS < 64
|
||||
#error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
|
||||
#endif
|
||||
|
||||
/* List of possible field types. These are used in the autogenerated code.
|
||||
* Least-significant 4 bits tell the scalar type
|
||||
* Most-significant 4 bits specify repeated/required/packed etc.
|
||||
@@ -88,19 +103,37 @@ typedef enum {
|
||||
|
||||
/* This structure is used in auto-generated constants
|
||||
* to specify struct fields.
|
||||
* You can change field sizes here if you need structures
|
||||
* You can change field sizes if you need structures
|
||||
* larger than 256 bytes or field tags larger than 256.
|
||||
* The compiler should complain if your .proto has such
|
||||
* structures ("initializer too large for type").
|
||||
* structures. Fix that by defining PB_FIELD_16BIT or
|
||||
* PB_FIELD_32BIT.
|
||||
*/
|
||||
typedef struct _pb_field_t pb_field_t;
|
||||
struct _pb_field_t {
|
||||
|
||||
#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
|
||||
uint8_t tag;
|
||||
pb_type_t type;
|
||||
uint8_t data_offset; /* Offset of field data, relative to previous field. */
|
||||
int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
|
||||
uint8_t data_size; /* Data size in bytes for a single item */
|
||||
uint8_t array_size; /* Maximum number of entries in array */
|
||||
#elif defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
|
||||
uint16_t tag;
|
||||
pb_type_t type;
|
||||
uint8_t data_offset;
|
||||
int8_t size_offset;
|
||||
uint16_t data_size;
|
||||
uint16_t array_size;
|
||||
#else
|
||||
uint32_t tag;
|
||||
pb_type_t type;
|
||||
uint8_t data_offset;
|
||||
int8_t size_offset;
|
||||
uint32_t data_size;
|
||||
uint32_t array_size;
|
||||
#endif
|
||||
|
||||
/* Field definitions for submessage
|
||||
* OR default value for all other non-array, non-callback types
|
||||
@@ -161,7 +194,7 @@ typedef enum {
|
||||
#define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
|
||||
#define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
|
||||
#define pb_delta_end(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
|
||||
#define PB_LAST_FIELD {0,0,0,0}
|
||||
#define PB_LAST_FIELD {0,(pb_type_t) 0,0,0}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
120
pb_decode.c
120
pb_decode.c
@@ -117,12 +117,33 @@ bool checkreturn pb_skip_string(pb_istream_t *stream)
|
||||
return pb_read(stream, NULL, length);
|
||||
}
|
||||
|
||||
/* Currently the wire type related stuff is kept hidden from
|
||||
* callbacks. They shouldn't need it. It's better for performance
|
||||
* to just assume the correct type and fail safely on corrupt message.
|
||||
*/
|
||||
bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
|
||||
{
|
||||
uint32_t temp;
|
||||
*eof = false;
|
||||
*wire_type = 0;
|
||||
*tag = 0;
|
||||
|
||||
if (!pb_decode_varint32(stream, &temp))
|
||||
{
|
||||
if (stream->bytes_left == 0)
|
||||
*eof = true;
|
||||
|
||||
static bool checkreturn skip(pb_istream_t *stream, pb_wire_type_t wire_type)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (temp == 0)
|
||||
{
|
||||
*eof = true; /* Special feature: allow 0-terminated messages. */
|
||||
return false;
|
||||
}
|
||||
|
||||
*tag = temp >> 3;
|
||||
*wire_type = (pb_wire_type_t)(temp & 7);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
|
||||
{
|
||||
switch (wire_type)
|
||||
{
|
||||
@@ -185,18 +206,20 @@ static bool checkreturn make_string_substream(pb_istream_t *stream, pb_istream_t
|
||||
|
||||
/* Iterator for pb_field_t list */
|
||||
typedef struct {
|
||||
const pb_field_t *start;
|
||||
const pb_field_t *current;
|
||||
int field_index;
|
||||
void *dest_struct;
|
||||
void *pData;
|
||||
void *pSize;
|
||||
const pb_field_t *start; /* Start of the pb_field_t array */
|
||||
const pb_field_t *current; /* Current position of the iterator */
|
||||
int field_index; /* Zero-based index of the field. */
|
||||
int required_field_index; /* Zero-based index that counts only the required fields */
|
||||
void *dest_struct; /* Pointer to the destination structure to decode to */
|
||||
void *pData; /* Pointer where to store current field value */
|
||||
void *pSize; /* Pointer where to store the size of current array field */
|
||||
} pb_field_iterator_t;
|
||||
|
||||
static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct)
|
||||
{
|
||||
iter->start = iter->current = fields;
|
||||
iter->field_index = 0;
|
||||
iter->required_field_index = 0;
|
||||
iter->pData = (char*)dest_struct + iter->current->data_offset;
|
||||
iter->pSize = (char*)iter->pData + iter->current->size_offset;
|
||||
iter->dest_struct = dest_struct;
|
||||
@@ -210,12 +233,16 @@ static bool pb_field_next(pb_field_iterator_t *iter)
|
||||
if (PB_HTYPE(iter->current->type) == PB_HTYPE_ARRAY)
|
||||
prev_size *= iter->current->array_size;
|
||||
|
||||
if (PB_HTYPE(iter->current->type) == PB_HTYPE_REQUIRED)
|
||||
iter->required_field_index++;
|
||||
|
||||
iter->current++;
|
||||
iter->field_index++;
|
||||
if (iter->current->tag == 0)
|
||||
{
|
||||
iter->current = iter->start;
|
||||
iter->field_index = 0;
|
||||
iter->required_field_index = 0;
|
||||
iter->pData = iter->dest_struct;
|
||||
prev_size = 0;
|
||||
notwrapped = false;
|
||||
@@ -226,7 +253,7 @@ static bool pb_field_next(pb_field_iterator_t *iter)
|
||||
return notwrapped;
|
||||
}
|
||||
|
||||
static bool checkreturn pb_field_find(pb_field_iterator_t *iter, int tag)
|
||||
static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t tag)
|
||||
{
|
||||
int start = iter->field_index;
|
||||
|
||||
@@ -261,6 +288,7 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
|
||||
&& PB_LTYPE(iter->current->type) <= PB_LTYPE_LAST_PACKABLE)
|
||||
{
|
||||
/* Packed array */
|
||||
bool status;
|
||||
size_t *size = (size_t*)iter->pSize;
|
||||
pb_istream_t substream;
|
||||
if (!make_string_substream(stream, &substream))
|
||||
@@ -273,7 +301,9 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
|
||||
return false;
|
||||
(*size)++;
|
||||
}
|
||||
return (substream.bytes_left == 0);
|
||||
status = (substream.bytes_left == 0);
|
||||
stream->state = substream.state;
|
||||
return status;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -292,7 +322,7 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
|
||||
pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
|
||||
|
||||
if (pCallback->funcs.decode == NULL)
|
||||
return skip(stream, wire_type);
|
||||
return pb_skip_field(stream, wire_type);
|
||||
|
||||
if (wire_type == PB_WT_STRING)
|
||||
{
|
||||
@@ -382,9 +412,8 @@ static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_str
|
||||
|
||||
bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
|
||||
{
|
||||
uint32_t fields_seen = 0; /* Used to check for required fields */
|
||||
uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0}; /* Used to check for required fields */
|
||||
pb_field_iterator_t iter;
|
||||
int i;
|
||||
|
||||
pb_message_set_to_defaults(fields, dest_struct);
|
||||
|
||||
@@ -392,46 +421,46 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void
|
||||
|
||||
while (stream->bytes_left)
|
||||
{
|
||||
uint32_t temp;
|
||||
int tag;
|
||||
uint32_t tag;
|
||||
pb_wire_type_t wire_type;
|
||||
if (!pb_decode_varint32(stream, &temp))
|
||||
bool eof;
|
||||
|
||||
if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
|
||||
{
|
||||
if (stream->bytes_left == 0)
|
||||
break; /* It was EOF */
|
||||
if (eof)
|
||||
break;
|
||||
else
|
||||
return false; /* It was error */
|
||||
return false;
|
||||
}
|
||||
|
||||
if (temp == 0)
|
||||
break; /* Special feature: allow 0-terminated messages. */
|
||||
|
||||
tag = temp >> 3;
|
||||
wire_type = (pb_wire_type_t)(temp & 7);
|
||||
|
||||
if (!pb_field_find(&iter, tag))
|
||||
{
|
||||
/* No match found, skip data */
|
||||
if (!skip(stream, wire_type))
|
||||
if (!pb_skip_field(stream, wire_type))
|
||||
return false;
|
||||
continue;
|
||||
}
|
||||
|
||||
fields_seen |= 1 << (iter.field_index & 31);
|
||||
if (PB_HTYPE(iter.current->type) == PB_HTYPE_REQUIRED
|
||||
&& iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
|
||||
{
|
||||
fields_seen[iter.required_field_index >> 3] |= 1 << (iter.required_field_index & 7);
|
||||
}
|
||||
|
||||
if (!decode_field(stream, wire_type, &iter))
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Check that all required fields (mod 31) were present. */
|
||||
for (i = 0; fields[i].tag != 0; i++)
|
||||
{
|
||||
if (PB_HTYPE(fields[i].type) == PB_HTYPE_REQUIRED &&
|
||||
!(fields_seen & (1 << (i & 31))))
|
||||
/* Check that all required fields were present. */
|
||||
pb_field_init(&iter, fields, dest_struct);
|
||||
do {
|
||||
if (PB_HTYPE(iter.current->type) == PB_HTYPE_REQUIRED &&
|
||||
iter.required_field_index < PB_MAX_REQUIRED_FIELDS &&
|
||||
!(fields_seen[iter.required_field_index >> 3] & (1 << (iter.required_field_index & 7))))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} while (pb_field_next(&iter));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -476,8 +505,11 @@ bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, v
|
||||
uint8_t bytes[4] = {0};
|
||||
bool status = pb_read(stream, bytes, 4);
|
||||
if (status) {
|
||||
uint8_t bebytes[4] = {bytes[3], bytes[2], bytes[1], bytes[0]};
|
||||
memcpy(dest, bebytes, 4);
|
||||
uint8_t *d = (uint8_t*)dest;
|
||||
d[0] = bytes[3];
|
||||
d[1] = bytes[2];
|
||||
d[2] = bytes[1];
|
||||
d[3] = bytes[0];
|
||||
}
|
||||
return status;
|
||||
#else
|
||||
@@ -492,9 +524,15 @@ bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, v
|
||||
uint8_t bytes[8] = {0};
|
||||
bool status = pb_read(stream, bytes, 8);
|
||||
if (status) {
|
||||
uint8_t bebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
|
||||
bytes[3], bytes[2], bytes[1], bytes[0]};
|
||||
memcpy(dest, bebytes, 8);
|
||||
uint8_t *d = (uint8_t*)dest;
|
||||
d[0] = bytes[7];
|
||||
d[1] = bytes[6];
|
||||
d[2] = bytes[5];
|
||||
d[3] = bytes[4];
|
||||
d[4] = bytes[3];
|
||||
d[5] = bytes[2];
|
||||
d[6] = bytes[1];
|
||||
d[7] = bytes[0];
|
||||
}
|
||||
return status;
|
||||
#else
|
||||
|
||||
@@ -48,6 +48,9 @@ bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struc
|
||||
* You may want to use these from your caller or callbacks.
|
||||
*/
|
||||
|
||||
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
|
||||
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
|
||||
|
||||
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);
|
||||
|
||||
bool pb_skip_varint(pb_istream_t *stream);
|
||||
|
||||
33
pb_encode.c
33
pb_encode.c
@@ -99,7 +99,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
|
||||
}
|
||||
else
|
||||
{
|
||||
pb_ostream_t sizestream = {0};
|
||||
pb_ostream_t sizestream = {0,0,0,0};
|
||||
p = pData;
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
@@ -110,7 +110,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
|
||||
size = sizestream.bytes_written;
|
||||
}
|
||||
|
||||
if (!pb_encode_varint(stream, size))
|
||||
if (!pb_encode_varint(stream, (uint64_t)size))
|
||||
return false;
|
||||
|
||||
if (stream->callback == NULL)
|
||||
@@ -235,8 +235,12 @@ bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
|
||||
bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
|
||||
{
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uint8_t *bytes = value;
|
||||
uint8_t lebytes[4] = {bytes[3], bytes[2], bytes[1], bytes[0]};
|
||||
const uint8_t *bytes = value;
|
||||
uint8_t lebytes[4];
|
||||
lebytes[0] = bytes[3];
|
||||
lebytes[1] = bytes[2];
|
||||
lebytes[2] = bytes[1];
|
||||
lebytes[3] = bytes[0];
|
||||
return pb_write(stream, lebytes, 4);
|
||||
#else
|
||||
return pb_write(stream, (uint8_t*)value, 4);
|
||||
@@ -246,9 +250,16 @@ bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
|
||||
bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
|
||||
{
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uint8_t *bytes[8] = value;
|
||||
uint8_t lebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
|
||||
bytes[3], bytes[2], bytes[1], bytes[0]};
|
||||
const uint8_t *bytes = value;
|
||||
uint8_t lebytes[8];
|
||||
lebytes[0] = bytes[7];
|
||||
lebytes[1] = bytes[6];
|
||||
lebytes[2] = bytes[5];
|
||||
lebytes[3] = bytes[4];
|
||||
lebytes[4] = bytes[3];
|
||||
lebytes[5] = bytes[2];
|
||||
lebytes[6] = bytes[1];
|
||||
lebytes[7] = bytes[0];
|
||||
return pb_write(stream, lebytes, 8);
|
||||
#else
|
||||
return pb_write(stream, (uint8_t*)value, 8);
|
||||
@@ -258,7 +269,7 @@ bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
|
||||
bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, int field_number)
|
||||
{
|
||||
int tag = wiretype | (field_number << 3);
|
||||
return pb_encode_varint(stream, tag);
|
||||
return pb_encode_varint(stream, (uint64_t)tag);
|
||||
}
|
||||
|
||||
bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
|
||||
@@ -294,7 +305,7 @@ bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t
|
||||
|
||||
bool checkreturn pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, size_t size)
|
||||
{
|
||||
if (!pb_encode_varint(stream, size))
|
||||
if (!pb_encode_varint(stream, (uint64_t)size))
|
||||
return false;
|
||||
|
||||
return pb_write(stream, buffer, size);
|
||||
@@ -303,7 +314,7 @@ bool checkreturn pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, s
|
||||
bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
|
||||
{
|
||||
/* First calculate the message size using a non-writing substream. */
|
||||
pb_ostream_t substream = {0};
|
||||
pb_ostream_t substream = {0,0,0,0};
|
||||
size_t size;
|
||||
bool status;
|
||||
|
||||
@@ -312,7 +323,7 @@ bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fie
|
||||
|
||||
size = substream.bytes_written;
|
||||
|
||||
if (!pb_encode_varint(stream, size))
|
||||
if (!pb_encode_varint(stream, (uint64_t)size))
|
||||
return false;
|
||||
|
||||
if (stream->callback == NULL)
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
CFLAGS=-ansi -Wall -Werror -I .. -g -O0 --coverage
|
||||
LDFLAGS=--coverage
|
||||
DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks.pb.h unittests.h unittestproto.pb.h alltypes.pb.h
|
||||
TESTS=test_decode1 test_encode1 decode_unittests encode_unittests
|
||||
DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks.pb.h unittests.h unittestproto.pb.h alltypes.pb.h missing_fields.pb.h
|
||||
TESTS=test_decode1 test_encode1 decode_unittests encode_unittests test_no_messages
|
||||
|
||||
# More strict checks for the core part of nanopb
|
||||
CFLAGS_CORE=-pedantic -Wextra
|
||||
|
||||
all: breakpoints $(TESTS) run_unittests
|
||||
|
||||
@@ -13,9 +16,9 @@ clean:
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
pb_encode.o: ../pb_encode.c $(DEPS)
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
$(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
|
||||
pb_decode.o: ../pb_decode.c $(DEPS)
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
$(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
|
||||
|
||||
test_decode1: test_decode1.o pb_decode.o person.pb.o
|
||||
test_decode2: test_decode2.o pb_decode.o person.pb.o
|
||||
@@ -25,8 +28,10 @@ test_encode2: test_encode2.o pb_encode.o person.pb.o
|
||||
test_encode3: test_encode3.o pb_encode.o alltypes.pb.o
|
||||
test_decode_callbacks: test_decode_callbacks.o pb_decode.o callbacks.pb.o
|
||||
test_encode_callbacks: test_encode_callbacks.o pb_encode.o callbacks.pb.o
|
||||
test_missing_fields: test_missing_fields.o pb_encode.o pb_decode.o missing_fields.pb.o
|
||||
decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
|
||||
encode_unittests: encode_unittests.o pb_encode.o unittestproto.pb.o
|
||||
test_no_messages: no_messages.pb.h no_messages.pb.c no_messages.pb.o
|
||||
|
||||
%.pb: %.proto
|
||||
protoc -I. -I../generator -I/usr/include -o$@ $<
|
||||
@@ -41,7 +46,7 @@ coverage: run_unittests
|
||||
gcov pb_encode.gcda
|
||||
gcov pb_decode.gcda
|
||||
|
||||
run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks
|
||||
run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks test_missing_fields
|
||||
rm -f *.gcda
|
||||
|
||||
./decode_unittests > /dev/null
|
||||
@@ -61,6 +66,8 @@ run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_
|
||||
|
||||
./test_encode3 | ./test_decode3
|
||||
./test_encode3 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null
|
||||
|
||||
./test_missing_fields
|
||||
|
||||
run_fuzztest: test_decode2
|
||||
bash -c 'I=1; while true; do cat /dev/urandom | ./test_decode2 > /dev/null; I=$$(($$I+1)); echo -en "\r$$I"; done'
|
||||
|
||||
@@ -6,6 +6,7 @@ message SubMessage {
|
||||
}
|
||||
|
||||
enum MyEnum {
|
||||
Zero = 0;
|
||||
First = 1;
|
||||
Second = 2;
|
||||
Truth = 42;
|
||||
@@ -33,6 +34,29 @@ message AllTypes {
|
||||
required SubMessage req_submsg = 16;
|
||||
required MyEnum req_enum = 17;
|
||||
|
||||
|
||||
repeated int32 rep_int32 = 21 [(nanopb).max_count = 5];
|
||||
repeated int64 rep_int64 = 22 [(nanopb).max_count = 5];
|
||||
repeated uint32 rep_uint32 = 23 [(nanopb).max_count = 5];
|
||||
repeated uint64 rep_uint64 = 24 [(nanopb).max_count = 5];
|
||||
repeated sint32 rep_sint32 = 25 [(nanopb).max_count = 5];
|
||||
repeated sint64 rep_sint64 = 26 [(nanopb).max_count = 5];
|
||||
repeated bool rep_bool = 27 [(nanopb).max_count = 5];
|
||||
|
||||
repeated fixed32 rep_fixed32 = 28 [(nanopb).max_count = 5];
|
||||
repeated sfixed32 rep_sfixed32= 29 [(nanopb).max_count = 5];
|
||||
repeated float rep_float = 30 [(nanopb).max_count = 5];
|
||||
|
||||
repeated fixed64 rep_fixed64 = 31 [(nanopb).max_count = 5];
|
||||
repeated sfixed64 rep_sfixed64= 32 [(nanopb).max_count = 5];
|
||||
repeated double rep_double = 33 [(nanopb).max_count = 5];
|
||||
|
||||
repeated string rep_string = 34 [(nanopb).max_size = 16, (nanopb).max_count = 5];
|
||||
repeated bytes rep_bytes = 35 [(nanopb).max_size = 16, (nanopb).max_count = 5];
|
||||
repeated SubMessage rep_submsg = 36 [(nanopb).max_count = 5];
|
||||
repeated MyEnum rep_enum = 37 [(nanopb).max_count = 5];
|
||||
|
||||
|
||||
// Just to make sure that the size of the fields has been calculated
|
||||
// properly, i.e. otherwise a bug in last field might not be detected.
|
||||
required int32 end = 99;
|
||||
|
||||
138
tests/missing_fields.proto
Normal file
138
tests/missing_fields.proto
Normal file
@@ -0,0 +1,138 @@
|
||||
/* Test for one missing field among many */
|
||||
|
||||
message AllFields
|
||||
{
|
||||
required int32 field1 = 1;
|
||||
required int32 field2 = 2;
|
||||
required int32 field3 = 3;
|
||||
required int32 field4 = 4;
|
||||
required int32 field5 = 5;
|
||||
required int32 field6 = 6;
|
||||
required int32 field7 = 7;
|
||||
required int32 field8 = 8;
|
||||
required int32 field9 = 9;
|
||||
required int32 field10 = 10;
|
||||
required int32 field11 = 11;
|
||||
required int32 field12 = 12;
|
||||
required int32 field13 = 13;
|
||||
required int32 field14 = 14;
|
||||
required int32 field15 = 15;
|
||||
required int32 field16 = 16;
|
||||
required int32 field17 = 17;
|
||||
required int32 field18 = 18;
|
||||
required int32 field19 = 19;
|
||||
required int32 field20 = 20;
|
||||
required int32 field21 = 21;
|
||||
required int32 field22 = 22;
|
||||
required int32 field23 = 23;
|
||||
required int32 field24 = 24;
|
||||
required int32 field25 = 25;
|
||||
required int32 field26 = 26;
|
||||
required int32 field27 = 27;
|
||||
required int32 field28 = 28;
|
||||
required int32 field29 = 29;
|
||||
required int32 field30 = 30;
|
||||
required int32 field31 = 31;
|
||||
required int32 field32 = 32;
|
||||
required int32 field33 = 33;
|
||||
required int32 field34 = 34;
|
||||
required int32 field35 = 35;
|
||||
required int32 field36 = 36;
|
||||
required int32 field37 = 37;
|
||||
required int32 field38 = 38;
|
||||
required int32 field39 = 39;
|
||||
required int32 field40 = 40;
|
||||
required int32 field41 = 41;
|
||||
required int32 field42 = 42;
|
||||
required int32 field43 = 43;
|
||||
required int32 field44 = 44;
|
||||
required int32 field45 = 45;
|
||||
required int32 field46 = 46;
|
||||
required int32 field47 = 47;
|
||||
required int32 field48 = 48;
|
||||
required int32 field49 = 49;
|
||||
required int32 field50 = 50;
|
||||
required int32 field51 = 51;
|
||||
required int32 field52 = 52;
|
||||
required int32 field53 = 53;
|
||||
required int32 field54 = 54;
|
||||
required int32 field55 = 55;
|
||||
required int32 field56 = 56;
|
||||
required int32 field57 = 57;
|
||||
required int32 field58 = 58;
|
||||
required int32 field59 = 59;
|
||||
required int32 field60 = 60;
|
||||
required int32 field61 = 61;
|
||||
required int32 field62 = 62;
|
||||
required int32 field63 = 63;
|
||||
required int32 field64 = 64;
|
||||
}
|
||||
|
||||
message MissingField
|
||||
{
|
||||
required int32 field1 = 1;
|
||||
required int32 field2 = 2;
|
||||
required int32 field3 = 3;
|
||||
required int32 field4 = 4;
|
||||
required int32 field5 = 5;
|
||||
required int32 field6 = 6;
|
||||
required int32 field7 = 7;
|
||||
required int32 field8 = 8;
|
||||
required int32 field9 = 9;
|
||||
required int32 field10 = 10;
|
||||
required int32 field11 = 11;
|
||||
required int32 field12 = 12;
|
||||
required int32 field13 = 13;
|
||||
required int32 field14 = 14;
|
||||
required int32 field15 = 15;
|
||||
required int32 field16 = 16;
|
||||
required int32 field17 = 17;
|
||||
required int32 field18 = 18;
|
||||
required int32 field19 = 19;
|
||||
required int32 field20 = 20;
|
||||
required int32 field21 = 21;
|
||||
required int32 field22 = 22;
|
||||
required int32 field23 = 23;
|
||||
required int32 field24 = 24;
|
||||
required int32 field25 = 25;
|
||||
required int32 field26 = 26;
|
||||
required int32 field27 = 27;
|
||||
required int32 field28 = 28;
|
||||
required int32 field29 = 29;
|
||||
required int32 field30 = 30;
|
||||
required int32 field31 = 31;
|
||||
required int32 field32 = 32;
|
||||
required int32 field33 = 33;
|
||||
required int32 field34 = 34;
|
||||
required int32 field35 = 35;
|
||||
required int32 field36 = 36;
|
||||
required int32 field37 = 37;
|
||||
required int32 field38 = 38;
|
||||
required int32 field39 = 39;
|
||||
required int32 field40 = 40;
|
||||
required int32 field41 = 41;
|
||||
required int32 field42 = 42;
|
||||
required int32 field43 = 43;
|
||||
required int32 field44 = 44;
|
||||
required int32 field45 = 45;
|
||||
required int32 field46 = 46;
|
||||
required int32 field47 = 47;
|
||||
required int32 field48 = 48;
|
||||
required int32 field49 = 49;
|
||||
required int32 field50 = 50;
|
||||
required int32 field51 = 51;
|
||||
required int32 field52 = 52;
|
||||
required int32 field53 = 53;
|
||||
required int32 field54 = 54;
|
||||
required int32 field55 = 55;
|
||||
required int32 field56 = 56;
|
||||
required int32 field57 = 57;
|
||||
required int32 field58 = 58;
|
||||
required int32 field59 = 59;
|
||||
required int32 field60 = 60;
|
||||
required int32 field61 = 61;
|
||||
required int32 field62 = 62;
|
||||
/* required int32 field63 = 63; */
|
||||
required int32 field64 = 64;
|
||||
}
|
||||
|
||||
7
tests/no_messages.proto
Normal file
7
tests/no_messages.proto
Normal file
@@ -0,0 +1,7 @@
|
||||
/* Test that a file without any messages works. */
|
||||
|
||||
enum Test {
|
||||
First = 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,33 @@ bool check_alltypes(pb_istream_t *stream)
|
||||
TEST(alltypes.req_submsg.substuff2 == 1016);
|
||||
TEST(alltypes.req_enum == MyEnum_Truth);
|
||||
|
||||
TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == 2001 && alltypes.rep_int32[0] == 0);
|
||||
TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == 2002 && alltypes.rep_int64[0] == 0);
|
||||
TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
|
||||
TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
|
||||
TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == 2005 && alltypes.rep_sint32[0] == 0);
|
||||
TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == 2006 && alltypes.rep_sint64[0] == 0);
|
||||
TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
|
||||
|
||||
TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
|
||||
TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == 2009 && alltypes.rep_sfixed32[0] == 0);
|
||||
TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
|
||||
|
||||
TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
|
||||
TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == 2012 && alltypes.rep_sfixed64[0] == 0);
|
||||
TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
|
||||
|
||||
TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
|
||||
TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4].size == 4 && alltypes.rep_bytes[0].size == 0);
|
||||
TEST(memcmp(alltypes.rep_bytes[4].bytes, "2015", 4) == 0);
|
||||
|
||||
TEST(alltypes.rep_submsg_count == 5);
|
||||
TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
|
||||
TEST(alltypes.rep_submsg[4].substuff2 == 2016 && alltypes.rep_submsg[0].substuff2 == 0);
|
||||
|
||||
TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
|
||||
|
||||
|
||||
TEST(alltypes.end == 1099);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -3,36 +3,65 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <pb_encode.h>
|
||||
#include "alltypes.pb.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
/* Initialize the structure with constants */
|
||||
AllTypes alltypes = {
|
||||
1001,
|
||||
1002,
|
||||
1003,
|
||||
1004,
|
||||
1005,
|
||||
1006,
|
||||
true,
|
||||
|
||||
1008,
|
||||
1009,
|
||||
1010.0f,
|
||||
|
||||
1011,
|
||||
1012,
|
||||
1013.0,
|
||||
|
||||
"1014",
|
||||
{4, "1015"},
|
||||
{"1016", 1016},
|
||||
MyEnum_Truth,
|
||||
|
||||
1099
|
||||
};
|
||||
AllTypes alltypes = {0};
|
||||
|
||||
alltypes.req_int32 = 1001;
|
||||
alltypes.req_int64 = 1002;
|
||||
alltypes.req_uint32 = 1003;
|
||||
alltypes.req_uint64 = 1004;
|
||||
alltypes.req_sint32 = 1005;
|
||||
alltypes.req_sint64 = 1006;
|
||||
alltypes.req_bool = true;
|
||||
|
||||
alltypes.req_fixed32 = 1008;
|
||||
alltypes.req_sfixed32 = 1009;
|
||||
alltypes.req_float = 1010.0f;
|
||||
|
||||
alltypes.req_fixed64 = 1011;
|
||||
alltypes.req_sfixed64 = 1012;
|
||||
alltypes.req_double = 1013.0;
|
||||
|
||||
strcpy(alltypes.req_string, "1014");
|
||||
alltypes.req_bytes.size = 4;
|
||||
memcpy(alltypes.req_bytes.bytes, "1015", 4);
|
||||
strcpy(alltypes.req_submsg.substuff1, "1016");
|
||||
alltypes.req_submsg.substuff2 = 1016;
|
||||
alltypes.req_enum = MyEnum_Truth;
|
||||
|
||||
alltypes.rep_int32_count = 5; alltypes.rep_int32[4] = 2001;
|
||||
alltypes.rep_int64_count = 5; alltypes.rep_int64[4] = 2002;
|
||||
alltypes.rep_uint32_count = 5; alltypes.rep_uint32[4] = 2003;
|
||||
alltypes.rep_uint64_count = 5; alltypes.rep_uint64[4] = 2004;
|
||||
alltypes.rep_sint32_count = 5; alltypes.rep_sint32[4] = 2005;
|
||||
alltypes.rep_sint64_count = 5; alltypes.rep_sint64[4] = 2006;
|
||||
alltypes.rep_bool_count = 5; alltypes.rep_bool[4] = true;
|
||||
|
||||
alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32[4] = 2008;
|
||||
alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32[4] = 2009;
|
||||
alltypes.rep_float_count = 5; alltypes.rep_float[4] = 2010.0f;
|
||||
|
||||
alltypes.rep_fixed64_count = 5; alltypes.rep_fixed64[4] = 2011;
|
||||
alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64[4] = 2012;
|
||||
alltypes.rep_double_count = 5; alltypes.rep_double[4] = 2013.0;
|
||||
|
||||
alltypes.rep_string_count = 5; strcpy(alltypes.rep_string[4], "2014");
|
||||
alltypes.rep_bytes_count = 5; alltypes.rep_bytes[4].size = 4;
|
||||
memcpy(alltypes.rep_bytes[4].bytes, "2015", 4);
|
||||
|
||||
alltypes.rep_submsg_count = 5;
|
||||
strcpy(alltypes.rep_submsg[4].substuff1, "2016");
|
||||
alltypes.rep_submsg[4].substuff2 = 2016;
|
||||
|
||||
alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth;
|
||||
|
||||
alltypes.end = 1099;
|
||||
|
||||
uint8_t buffer[512];
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
|
||||
49
tests/test_missing_fields.c
Normal file
49
tests/test_missing_fields.c
Normal file
@@ -0,0 +1,49 @@
|
||||
/* Checks that missing required fields are detected properly */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pb_encode.h>
|
||||
#include <pb_decode.h>
|
||||
#include "missing_fields.pb.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
uint8_t buffer[512] = {};
|
||||
|
||||
/* Create a message with one missing field */
|
||||
{
|
||||
MissingField msg = {};
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||
if (!pb_encode(&stream, MissingField_fields, &msg))
|
||||
{
|
||||
printf("Encode failed.\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test that it decodes properly if we don't require that field */
|
||||
{
|
||||
MissingField msg = {};
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
|
||||
|
||||
if (!pb_decode(&stream, MissingField_fields, &msg))
|
||||
{
|
||||
printf("Decode failed.\n");
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test that it does *not* decode properly if we require the field */
|
||||
{
|
||||
AllFields msg = {};
|
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
|
||||
|
||||
if (pb_decode(&stream, AllFields_fields, &msg))
|
||||
{
|
||||
printf("Decode didn't detect missing field.\n");
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
return 0; /* All ok */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user