bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
- UNUSED(field);
- return pb_encode_string(stream, (const uint8_t*)src, strlen((const char*)src));
+ /* strnlen() is not always available, so just use a for-loop */
+ size_t size = 0;
+ const char *p = (const char*)src;
+ while (size < field->data_size && *p != '\0')
+ {
+ size++;
+ p++;
+ }
+
+ return pb_encode_string(stream, (const uint8_t*)src, size);
}
bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
uint8_t buffer[30];
pb_ostream_t s;
- char value[] = "xyzzy";
+ char value[30] = "xyzzy";
COMMENT("Test pb_enc_string")
- TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x05xyzzy"))
+ TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x05xyzzy"))
value[0] = '\0';
- TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x00"))
+ TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x00"))
+ memset(value, 'x', 30);
+ TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x0Axxxxxxxxxx"))
}
{
repeated float data = 1 [(nanopb).max_count = 10];
}
+message StringMessage {
+ required string data = 1 [(nanopb).max_size = 10];
+}
+
message CallbackArray {
// We cheat a bit and use this message for testing other types, too.
// Nanopb does not care about the actual defined data type for callback