Fixed a bunch of bugs related to callback fields.
Most importantly, callback fields in submessages were being overwritten with garbage, causing segfaults. Additionally, converted PB_LTYPE_FIXED to PB_LTYPE_FIXED32 and PB_LTYPE_FIXED64. This makes the interface a bit easier to use, and in addition runs faster. git-svn-id: https://svn.kapsi.fi/jpa/nanopb@975 e3a754e5-d11d-0410-8d38-ebb782a927b9
This commit is contained in:
@@ -15,6 +15,32 @@ bool encode_string(pb_ostream_t *stream, const pb_field_t *field, const void *ar
|
||||
return pb_encode_string(stream, (uint8_t*)str, strlen(str));
|
||||
}
|
||||
|
||||
bool encode_int32(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
|
||||
{
|
||||
if (!pb_encode_tag_for_field(stream, field))
|
||||
return false;
|
||||
|
||||
return pb_encode_varint(stream, 42);
|
||||
}
|
||||
|
||||
bool encode_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
|
||||
{
|
||||
if (!pb_encode_tag_for_field(stream, field))
|
||||
return false;
|
||||
|
||||
uint32_t value = 42;
|
||||
return pb_enc_fixed32(stream, field, &value);
|
||||
}
|
||||
|
||||
bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
|
||||
{
|
||||
if (!pb_encode_tag_for_field(stream, field))
|
||||
return false;
|
||||
|
||||
uint64_t value = 42;
|
||||
return pb_enc_fixed64(stream, field, &value);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
uint8_t buffer[1024];
|
||||
@@ -22,6 +48,15 @@ int main()
|
||||
TestMessage testmessage = {};
|
||||
|
||||
testmessage.stringvalue.funcs.encode = &encode_string;
|
||||
testmessage.int32value.funcs.encode = &encode_int32;
|
||||
testmessage.fixed32value.funcs.encode = &encode_fixed32;
|
||||
testmessage.fixed64value.funcs.encode = &encode_fixed64;
|
||||
|
||||
testmessage.has_submsg = true;
|
||||
testmessage.submsg.stringvalue.funcs.encode = &encode_string;
|
||||
testmessage.submsg.int32value.funcs.encode = &encode_int32;
|
||||
testmessage.submsg.fixed32value.funcs.encode = &encode_fixed32;
|
||||
testmessage.submsg.fixed64value.funcs.encode = &encode_fixed64;
|
||||
|
||||
if (!pb_encode(&stream, TestMessage_fields, &testmessage))
|
||||
return 1;
|
||||
|
||||
Reference in New Issue
Block a user