Merge branch 'master' into dynamic_alloc_dev
Conflicts: tests/Makefile tests/test_encode1.c
This commit is contained in:
@@ -6,3 +6,4 @@ all: index.html concepts.html reference.html \
|
|||||||
|
|
||||||
%.html: %.rst
|
%.html: %.rst
|
||||||
rst2html --stylesheet=lsr.css --link-stylesheet $< $@
|
rst2html --stylesheet=lsr.css --link-stylesheet $< $@
|
||||||
|
sed -i 's!</head>!<link href="favicon.ico" type="image/x-icon" rel="shortcut icon" />\n</head>!' $@
|
||||||
|
|||||||
BIN
docs/logo/logo.png
Normal file
BIN
docs/logo/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
1470
docs/logo/logo.svg
Normal file
1470
docs/logo/logo.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 102 KiB |
BIN
docs/logo/logo16px.png
Normal file
BIN
docs/logo/logo16px.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 854 B |
BIN
docs/logo/logo48px.png
Normal file
BIN
docs/logo/logo48px.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
2
generator/Makefile
Normal file
2
generator/Makefile
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
nanopb_pb2.py: nanopb.proto
|
||||||
|
protoc --python_out=. -I /usr/include -I . nanopb.proto
|
||||||
@@ -561,8 +561,10 @@ bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, v
|
|||||||
#ifdef __BIG_ENDIAN__
|
#ifdef __BIG_ENDIAN__
|
||||||
uint8_t bytes[4] = {0};
|
uint8_t bytes[4] = {0};
|
||||||
bool status = pb_read(stream, bytes, 4);
|
bool status = pb_read(stream, bytes, 4);
|
||||||
|
if (status) {
|
||||||
uint8_t bebytes[4] = {bytes[3], bytes[2], bytes[1], bytes[0]};
|
uint8_t bebytes[4] = {bytes[3], bytes[2], bytes[1], bytes[0]};
|
||||||
memcpy(dest, bebytes, 4);
|
memcpy(dest, bebytes, 4);
|
||||||
|
}
|
||||||
return status;
|
return status;
|
||||||
#else
|
#else
|
||||||
return pb_read(stream, (uint8_t*)dest, 4);
|
return pb_read(stream, (uint8_t*)dest, 4);
|
||||||
@@ -574,9 +576,11 @@ bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, v
|
|||||||
#ifdef __BIG_ENDIAN__
|
#ifdef __BIG_ENDIAN__
|
||||||
uint8_t bytes[8] = {0};
|
uint8_t bytes[8] = {0};
|
||||||
bool status = pb_read(stream, bytes, 8);
|
bool status = pb_read(stream, bytes, 8);
|
||||||
|
if (status) {
|
||||||
uint8_t bebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
|
uint8_t bebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
|
||||||
bytes[3], bytes[2], bytes[1], bytes[0]};
|
bytes[3], bytes[2], bytes[1], bytes[0]};
|
||||||
memcpy(dest, bebytes, 8);
|
memcpy(dest, bebytes, 8);
|
||||||
|
}
|
||||||
return status;
|
return status;
|
||||||
#else
|
#else
|
||||||
return pb_read(stream, (uint8_t*)dest, 8);
|
return pb_read(stream, (uint8_t*)dest, 8);
|
||||||
|
|||||||
@@ -389,6 +389,7 @@ bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field
|
|||||||
status = pb_encode(&substream, (const pb_message_t*)field->ptr, src);
|
status = pb_encode(&substream, (const pb_message_t*)field->ptr, src);
|
||||||
|
|
||||||
stream->bytes_written += substream.bytes_written;
|
stream->bytes_written += substream.bytes_written;
|
||||||
|
stream->state = substream.state;
|
||||||
|
|
||||||
if (substream.bytes_written != size)
|
if (substream.bytes_written != size)
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -24,7 +24,9 @@ decode_ptr_unittests.o: decode_unittests.c $(DEPS)
|
|||||||
$(CC) $(CFLAGS) -c -o $@ $<
|
$(CC) $(CFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
test_decode1: test_decode1.o pb_decode.o person.pb.o
|
test_decode1: test_decode1.o pb_decode.o person.pb.o
|
||||||
|
test_decode2: test_decode2.o pb_decode.o person.pb.o
|
||||||
test_encode1: test_encode1.o pb_encode.o person.pb.o
|
test_encode1: test_encode1.o pb_encode.o person.pb.o
|
||||||
|
test_encode2: test_encode2.o pb_encode.o person.pb.o
|
||||||
test_decode_callbacks: test_decode_callbacks.o pb_decode.o callbacks.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_encode_callbacks: test_encode_callbacks.o pb_encode.o callbacks.pb.o
|
||||||
decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
|
decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
|
||||||
@@ -47,7 +49,7 @@ coverage: run_unittests
|
|||||||
gcov pb_decode.gcda
|
gcov pb_decode.gcda
|
||||||
gcov pb_ptr_decode.gcda
|
gcov pb_ptr_decode.gcda
|
||||||
|
|
||||||
run_unittests: decode_unittests decode_ptr_unittests encode_unittests test_encode1 test_decode1 test_encode_callbacks test_decode_callbacks
|
run_unittests: decode_unittests decode_ptr_unittests encode_unittests test_encode1 test_encode2 test_decode1 test_decode2 test_encode_callbacks test_decode_callbacks
|
||||||
rm -f *.gcda
|
rm -f *.gcda
|
||||||
|
|
||||||
./decode_unittests > /dev/null
|
./decode_unittests > /dev/null
|
||||||
@@ -57,8 +59,14 @@ run_unittests: decode_unittests decode_ptr_unittests encode_unittests test_encod
|
|||||||
[ "`./test_encode1 | ./test_decode1`" = \
|
[ "`./test_encode1 | ./test_decode1`" = \
|
||||||
"`./test_encode1 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
|
"`./test_encode1 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
|
||||||
|
|
||||||
|
[ "`./test_encode2 | ./test_decode1`" = \
|
||||||
|
"`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
|
||||||
|
|
||||||
|
[ "`./test_encode2 | ./test_decode2`" = \
|
||||||
|
"`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
|
||||||
|
|
||||||
[ "`./test_encode_callbacks | ./test_decode_callbacks`" = \
|
[ "`./test_encode_callbacks | ./test_decode_callbacks`" = \
|
||||||
"`./test_encode_callbacks | protoc --decode=TestMessage callbacks.proto`" ]
|
"`./test_encode_callbacks | protoc --decode=TestMessage callbacks.proto`" ]
|
||||||
|
|
||||||
run_fuzztest: test_decode1
|
run_fuzztest: test_decode2
|
||||||
bash -c 'I=1; while cat /dev/urandom | ./test_decode1 > /dev/null; do I=$$(($$I+1)); echo -en "\r$$I"; done'
|
bash -c 'I=1; while true; do cat /dev/urandom | ./test_decode2 > /dev/null; I=$$(($$I+1)); echo -en "\r$$I"; done'
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ bool print_person(pb_istream_t *stream)
|
|||||||
/* Now the decoding is done, rest is just to print stuff out. */
|
/* Now the decoding is done, rest is just to print stuff out. */
|
||||||
|
|
||||||
printf("name: \"%s\"\n", person.name);
|
printf("name: \"%s\"\n", person.name);
|
||||||
printf("id: %d\n", person.id);
|
printf("id: %ld\n", (long)person.id);
|
||||||
|
|
||||||
if (Person_has(person, email))
|
if (Person_has(person, email))
|
||||||
printf("email: \"%s\"\n", person.email);
|
printf("email: \"%s\"\n", person.email);
|
||||||
@@ -34,6 +34,8 @@ bool print_person(pb_istream_t *stream)
|
|||||||
printf("phone {\n");
|
printf("phone {\n");
|
||||||
printf(" number: \"%s\"\n", phone->number);
|
printf(" number: \"%s\"\n", phone->number);
|
||||||
|
|
||||||
|
if (Person_PhoneNumber_has(*phone, type))
|
||||||
|
{
|
||||||
switch (phone->type)
|
switch (phone->type)
|
||||||
{
|
{
|
||||||
case Person_PhoneType_WORK:
|
case Person_PhoneType_WORK:
|
||||||
@@ -48,39 +50,23 @@ bool print_person(pb_istream_t *stream)
|
|||||||
printf(" type: MOBILE\n");
|
printf(" type: MOBILE\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
printf("}\n");
|
printf("}\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This binds the pb_istream_t to stdin */
|
|
||||||
bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
|
|
||||||
{
|
|
||||||
FILE *file = (FILE*)stream->state;
|
|
||||||
bool status;
|
|
||||||
|
|
||||||
if (buf == NULL)
|
|
||||||
{
|
|
||||||
/* Skipping data */
|
|
||||||
while (count-- && fgetc(file) != EOF);
|
|
||||||
return count == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
status = (fread(buf, 1, count, file) == count);
|
|
||||||
|
|
||||||
if (feof(file))
|
|
||||||
stream->bytes_left = 0;
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
/* Maximum size is specified to prevent infinite length messages from
|
/* Read the data into buffer */
|
||||||
* hanging this in the fuzz test.
|
uint8_t buffer[512];
|
||||||
*/
|
size_t count = fread(buffer, 1, sizeof(buffer), stdin);
|
||||||
pb_istream_t stream = {&callback, stdin, 10000};
|
|
||||||
|
/* Construct a pb_istream_t for reading from the buffer */
|
||||||
|
pb_istream_t stream = pb_istream_from_buffer(buffer, count);
|
||||||
|
|
||||||
|
/* Decode and print out the stuff */
|
||||||
if (!print_person(&stream))
|
if (!print_person(&stream))
|
||||||
{
|
{
|
||||||
printf("Parsing failed.\n");
|
printf("Parsing failed.\n");
|
||||||
|
|||||||
90
tests/test_decode2.c
Normal file
90
tests/test_decode2.c
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
/* Same as test_decode1 but reads from stdin directly.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <pb_decode.h>
|
||||||
|
#include "person.pb.h"
|
||||||
|
|
||||||
|
/* This function is called once from main(), it handles
|
||||||
|
the decoding and printing.
|
||||||
|
Ugly copy-paste from test_decode1.c. */
|
||||||
|
bool print_person(pb_istream_t *stream)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
Person person;
|
||||||
|
|
||||||
|
if (!pb_decode(stream, Person_msg, &person))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* Now the decoding is done, rest is just to print stuff out. */
|
||||||
|
|
||||||
|
printf("name: \"%s\"\n", person.name);
|
||||||
|
printf("id: %ld\n", (long)person.id);
|
||||||
|
|
||||||
|
if (Person_has(person, email))
|
||||||
|
printf("email: \"%s\"\n", person.email);
|
||||||
|
|
||||||
|
for (i = 0; i < person.phone_count; i++)
|
||||||
|
{
|
||||||
|
Person_PhoneNumber *phone = &person.phone[i];
|
||||||
|
printf("phone {\n");
|
||||||
|
printf(" number: \"%s\"\n", phone->number);
|
||||||
|
|
||||||
|
if (Person_PhoneNumber_has(*phone, type))
|
||||||
|
{
|
||||||
|
switch (phone->type)
|
||||||
|
{
|
||||||
|
case Person_PhoneType_WORK:
|
||||||
|
printf(" type: WORK\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Person_PhoneType_HOME:
|
||||||
|
printf(" type: HOME\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Person_PhoneType_MOBILE:
|
||||||
|
printf(" type: MOBILE\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* This binds the pb_istream_t to stdin */
|
||||||
|
bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
|
||||||
|
{
|
||||||
|
FILE *file = (FILE*)stream->state;
|
||||||
|
bool status;
|
||||||
|
|
||||||
|
if (buf == NULL)
|
||||||
|
{
|
||||||
|
/* Skipping data */
|
||||||
|
while (count-- && fgetc(file) != EOF);
|
||||||
|
return count == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = (fread(buf, 1, count, file) == count);
|
||||||
|
|
||||||
|
if (feof(file))
|
||||||
|
stream->bytes_left = 0;
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
/* Maximum size is specified to prevent infinite length messages from
|
||||||
|
* hanging this in the fuzz test.
|
||||||
|
*/
|
||||||
|
pb_istream_t stream = {&callback, stdin, 10000};
|
||||||
|
if (!print_person(&stream))
|
||||||
|
{
|
||||||
|
printf("Parsing failed.\n");
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,7 +30,7 @@ bool print_int32(pb_istream_t *stream, const pb_field_t *field, void *arg)
|
|||||||
if (!pb_decode_varint(stream, &value))
|
if (!pb_decode_varint(stream, &value))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
printf((char*)arg, (int32_t)value);
|
printf((char*)arg, (long)value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ bool print_fixed32(pb_istream_t *stream, const pb_field_t *field, void *arg)
|
|||||||
if (!pb_dec_fixed32(stream, NULL, &value))
|
if (!pb_dec_fixed32(stream, NULL, &value))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
printf((char*)arg, value);
|
printf((char*)arg, (long)value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ bool print_fixed64(pb_istream_t *stream, const pb_field_t *field, void *arg)
|
|||||||
if (!pb_dec_fixed64(stream, NULL, &value))
|
if (!pb_dec_fixed64(stream, NULL, &value))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
printf((char*)arg, value);
|
printf((char*)arg, (long long)value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,18 +69,18 @@ int main()
|
|||||||
testmessage.submsg.stringvalue.funcs.decode = &print_string;
|
testmessage.submsg.stringvalue.funcs.decode = &print_string;
|
||||||
testmessage.submsg.stringvalue.arg = "submsg {\n stringvalue: \"%s\"\n";
|
testmessage.submsg.stringvalue.arg = "submsg {\n stringvalue: \"%s\"\n";
|
||||||
testmessage.submsg.int32value.funcs.decode = &print_int32;
|
testmessage.submsg.int32value.funcs.decode = &print_int32;
|
||||||
testmessage.submsg.int32value.arg = " int32value: %d\n";
|
testmessage.submsg.int32value.arg = " int32value: %ld\n";
|
||||||
testmessage.submsg.fixed32value.funcs.decode = &print_fixed32;
|
testmessage.submsg.fixed32value.funcs.decode = &print_fixed32;
|
||||||
testmessage.submsg.fixed32value.arg = " fixed32value: %d\n";
|
testmessage.submsg.fixed32value.arg = " fixed32value: %ld\n";
|
||||||
testmessage.submsg.fixed64value.funcs.decode = &print_fixed64;
|
testmessage.submsg.fixed64value.funcs.decode = &print_fixed64;
|
||||||
testmessage.submsg.fixed64value.arg = " fixed64value: %lld\n}\n";
|
testmessage.submsg.fixed64value.arg = " fixed64value: %lld\n}\n";
|
||||||
|
|
||||||
testmessage.stringvalue.funcs.decode = &print_string;
|
testmessage.stringvalue.funcs.decode = &print_string;
|
||||||
testmessage.stringvalue.arg = "stringvalue: \"%s\"\n";
|
testmessage.stringvalue.arg = "stringvalue: \"%s\"\n";
|
||||||
testmessage.int32value.funcs.decode = &print_int32;
|
testmessage.int32value.funcs.decode = &print_int32;
|
||||||
testmessage.int32value.arg = "int32value: %d\n";
|
testmessage.int32value.arg = "int32value: %ld\n";
|
||||||
testmessage.fixed32value.funcs.decode = &print_fixed32;
|
testmessage.fixed32value.funcs.decode = &print_fixed32;
|
||||||
testmessage.fixed32value.arg = "fixed32value: %d\n";
|
testmessage.fixed32value.arg = "fixed32value: %ld\n";
|
||||||
testmessage.fixed64value.funcs.decode = &print_fixed64;
|
testmessage.fixed64value.funcs.decode = &print_fixed64;
|
||||||
testmessage.fixed64value.arg = "fixed64value: %lld\n";
|
testmessage.fixed64value.arg = "fixed64value: %lld\n";
|
||||||
|
|
||||||
|
|||||||
@@ -1,33 +1,36 @@
|
|||||||
/* A very simple encoding test case using person.proto.
|
/* A very simple encoding test case using person.proto.
|
||||||
* Just puts constant data in the fields and writes the
|
* Just puts constant data in the fields and encodes into
|
||||||
* data to stdout.
|
* buffer, which is then written to stdout.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
#include "person.pb.h"
|
#include "person.pb.h"
|
||||||
|
|
||||||
/* This binds the pb_ostream_t into the stdout stream */
|
|
||||||
bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
|
|
||||||
{
|
|
||||||
FILE *file = (FILE*) stream->state;
|
|
||||||
return fwrite(buf, 1, count, file) == count;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
/* Initialize the structure with constants */
|
/* Initialize the structure with constants */
|
||||||
Person person = {{0}, "Test Person 99", 99, "test@person.com",
|
Person person = {{1 << Person_email_index},
|
||||||
1, {{{0}, "555-12345678", Person_PhoneType_MOBILE}}};
|
"Test Person 99", 99, "test@person.com",
|
||||||
Person_set(person, email);
|
3, {{{1 << Person_PhoneNumber_type_index},
|
||||||
Person_PhoneNumber_set(person.phone[0], type);
|
"555-12345678", Person_PhoneType_MOBILE},
|
||||||
|
{{1 << Person_PhoneNumber_type_index},
|
||||||
|
"99-2342", 0},
|
||||||
|
{{1 << Person_PhoneNumber_type_index},
|
||||||
|
"1234-5678", Person_PhoneType_WORK},
|
||||||
|
}};
|
||||||
|
|
||||||
/* Prepare the stream, output goes directly to stdout */
|
uint8_t buffer[512];
|
||||||
pb_ostream_t stream = {&streamcallback, stdout, SIZE_MAX, 0};
|
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||||
|
|
||||||
/* Now encode it and check if we succeeded. */
|
/* Now encode it and check if we succeeded. */
|
||||||
if (pb_encode(&stream, Person_msg, &person))
|
if (pb_encode(&stream, Person_msg, &person))
|
||||||
|
{
|
||||||
|
fwrite(buffer, 1, stream.bytes_written, stdout);
|
||||||
return 0; /* Success */
|
return 0; /* Success */
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
return 1; /* Failure */
|
return 1; /* Failure */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
36
tests/test_encode2.c
Normal file
36
tests/test_encode2.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/* Same as test_encode1.c, except writes directly to stdout.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <pb_encode.h>
|
||||||
|
#include "person.pb.h"
|
||||||
|
|
||||||
|
/* This binds the pb_ostream_t into the stdout stream */
|
||||||
|
bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
|
||||||
|
{
|
||||||
|
FILE *file = (FILE*) stream->state;
|
||||||
|
return fwrite(buf, 1, count, file) == count;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
/* Initialize the structure with constants */
|
||||||
|
Person person = {{1 << Person_email_index},
|
||||||
|
"Test Person 99", 99, "test@person.com",
|
||||||
|
3, {{{1 << Person_PhoneNumber_type_index},
|
||||||
|
"555-12345678", Person_PhoneType_MOBILE},
|
||||||
|
{{1 << Person_PhoneNumber_type_index},
|
||||||
|
"99-2342", 0},
|
||||||
|
{{1 << Person_PhoneNumber_type_index},
|
||||||
|
"1234-5678", Person_PhoneType_WORK},
|
||||||
|
}};
|
||||||
|
|
||||||
|
/* Prepare the stream, output goes directly to stdout */
|
||||||
|
pb_ostream_t stream = {&streamcallback, stdout, SIZE_MAX, 0};
|
||||||
|
|
||||||
|
/* Now encode it and check if we succeeded. */
|
||||||
|
if (pb_encode(&stream, Person_msg, &person))
|
||||||
|
return 0; /* Success */
|
||||||
|
else
|
||||||
|
return 1; /* Failure */
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user