Implement error message support for the encoder side.
Update issue 7 Status: FixedInGit
This commit is contained in:
37
pb_encode.c
37
pb_encode.c
@@ -56,6 +56,9 @@ pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize)
|
|||||||
stream.state = buf;
|
stream.state = buf;
|
||||||
stream.max_size = bufsize;
|
stream.max_size = bufsize;
|
||||||
stream.bytes_written = 0;
|
stream.bytes_written = 0;
|
||||||
|
#ifndef PB_NO_ERRMSG
|
||||||
|
stream.errmsg = NULL;
|
||||||
|
#endif
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,14 +67,14 @@ bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count
|
|||||||
if (stream->callback != NULL)
|
if (stream->callback != NULL)
|
||||||
{
|
{
|
||||||
if (stream->bytes_written + count > stream->max_size)
|
if (stream->bytes_written + count > stream->max_size)
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "stream full");
|
||||||
|
|
||||||
#ifdef PB_BUFFER_ONLY
|
#ifdef PB_BUFFER_ONLY
|
||||||
if (!buf_write(stream, buf, count))
|
if (!buf_write(stream, buf, count))
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "io error");
|
||||||
#else
|
#else
|
||||||
if (!stream->callback(stream, buf, count))
|
if (!stream->callback(stream, buf, count))
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "io error");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +114,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pb_ostream_t sizestream = {0,0,0,0};
|
pb_ostream_t sizestream = PB_OSTREAM_SIZING;
|
||||||
p = pData;
|
p = pData;
|
||||||
for (i = 0; i < count; i++)
|
for (i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
@@ -187,7 +190,7 @@ bool checkreturn encode_static_field(pb_ostream_t *stream, const pb_field_t *fie
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "invalid field type");
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -199,7 +202,7 @@ bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_t *f
|
|||||||
if (callback->funcs.encode != NULL)
|
if (callback->funcs.encode != NULL)
|
||||||
{
|
{
|
||||||
if (!callback->funcs.encode(stream, field, callback->arg))
|
if (!callback->funcs.encode(stream, field, callback->arg))
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "callback error");
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -235,7 +238,7 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "invalid field type");
|
||||||
}
|
}
|
||||||
|
|
||||||
field++;
|
field++;
|
||||||
@@ -340,7 +343,7 @@ bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "invalid field type");
|
||||||
}
|
}
|
||||||
|
|
||||||
return pb_encode_tag(stream, wiretype, field->tag);
|
return pb_encode_tag(stream, wiretype, field->tag);
|
||||||
@@ -357,7 +360,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)
|
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. */
|
/* First calculate the message size using a non-writing substream. */
|
||||||
pb_ostream_t substream = {0,0,0,0};
|
pb_ostream_t substream = PB_OSTREAM_SIZING;
|
||||||
size_t size;
|
size_t size;
|
||||||
bool status;
|
bool status;
|
||||||
|
|
||||||
@@ -373,7 +376,7 @@ bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fie
|
|||||||
return pb_write(stream, NULL, size); /* Just sizing */
|
return pb_write(stream, NULL, size); /* Just sizing */
|
||||||
|
|
||||||
if (stream->bytes_written + size > stream->max_size)
|
if (stream->bytes_written + size > stream->max_size)
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "stream full");
|
||||||
|
|
||||||
/* Use a substream to verify that a callback doesn't write more than
|
/* Use a substream to verify that a callback doesn't write more than
|
||||||
* what it did the first time. */
|
* what it did the first time. */
|
||||||
@@ -381,14 +384,20 @@ bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fie
|
|||||||
substream.state = stream->state;
|
substream.state = stream->state;
|
||||||
substream.max_size = size;
|
substream.max_size = size;
|
||||||
substream.bytes_written = 0;
|
substream.bytes_written = 0;
|
||||||
|
#ifndef PB_NO_ERRMSG
|
||||||
|
substream.errmsg = NULL;
|
||||||
|
#endif
|
||||||
|
|
||||||
status = pb_encode(&substream, fields, src_struct);
|
status = pb_encode(&substream, fields, src_struct);
|
||||||
|
|
||||||
stream->bytes_written += substream.bytes_written;
|
stream->bytes_written += substream.bytes_written;
|
||||||
stream->state = substream.state;
|
stream->state = substream.state;
|
||||||
|
#ifndef PB_NO_ERRMSG
|
||||||
|
stream->errmsg = substream.errmsg;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (substream.bytes_written != size)
|
if (substream.bytes_written != size)
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "submsg size changed");
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
@@ -405,7 +414,7 @@ bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, co
|
|||||||
case 2: value = *(const uint16_t*)src; break;
|
case 2: value = *(const uint16_t*)src; break;
|
||||||
case 4: value = *(const uint32_t*)src; break;
|
case 4: value = *(const uint32_t*)src; break;
|
||||||
case 8: value = *(const uint64_t*)src; break;
|
case 8: value = *(const uint64_t*)src; break;
|
||||||
default: return false;
|
default: PB_RETURN_ERROR(stream, "invalid data_size");
|
||||||
}
|
}
|
||||||
|
|
||||||
return pb_encode_varint(stream, value);
|
return pb_encode_varint(stream, value);
|
||||||
@@ -419,7 +428,7 @@ bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, c
|
|||||||
{
|
{
|
||||||
case 4: value = *(const int32_t*)src; break;
|
case 4: value = *(const int32_t*)src; break;
|
||||||
case 8: value = *(const int64_t*)src; break;
|
case 8: value = *(const int64_t*)src; break;
|
||||||
default: return false;
|
default: PB_RETURN_ERROR(stream, "invalid data_size");
|
||||||
}
|
}
|
||||||
|
|
||||||
return pb_encode_svarint(stream, value);
|
return pb_encode_svarint(stream, value);
|
||||||
@@ -453,7 +462,7 @@ bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, co
|
|||||||
bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
|
bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
|
||||||
{
|
{
|
||||||
if (field->ptr == NULL)
|
if (field->ptr == NULL)
|
||||||
return false;
|
PB_RETURN_ERROR(stream, "invalid field descriptor");
|
||||||
|
|
||||||
return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
|
return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
|
||||||
}
|
}
|
||||||
|
|||||||
11
pb_encode.h
11
pb_encode.h
@@ -46,11 +46,22 @@ struct _pb_ostream_t
|
|||||||
void *state; /* Free field for use by callback implementation */
|
void *state; /* Free field for use by callback implementation */
|
||||||
size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */
|
size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */
|
||||||
size_t bytes_written;
|
size_t bytes_written;
|
||||||
|
|
||||||
|
#ifndef PB_NO_ERRMSG
|
||||||
|
const char *errmsg;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize);
|
pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize);
|
||||||
bool pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count);
|
bool pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count);
|
||||||
|
|
||||||
|
/* Stream type for use in computing message sizes */
|
||||||
|
#ifndef PB_NO_ERRMSG
|
||||||
|
#define PB_OSTREAM_SIZING {0,0,0,0,0}
|
||||||
|
#else
|
||||||
|
#define PB_OSTREAM_SIZING {0,0,0,0}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Encode struct to given output stream.
|
/* Encode struct to given output stream.
|
||||||
* Returns true on success, false on any failure.
|
* Returns true on success, false on any failure.
|
||||||
* The actual struct pointed to by src_struct must match the description in fields.
|
* The actual struct pointed to by src_struct must match the description in fields.
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ int main()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||||
return 1; /* Failure */
|
return 1; /* Failure */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,12 @@ int main()
|
|||||||
|
|
||||||
/* Now encode it and check if we succeeded. */
|
/* Now encode it and check if we succeeded. */
|
||||||
if (pb_encode(&stream, Person_fields, &person))
|
if (pb_encode(&stream, Person_fields, &person))
|
||||||
|
{
|
||||||
return 0; /* Success */
|
return 0; /* Success */
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||||
return 1; /* Failure */
|
return 1; /* Failure */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Encoding failed!\n");
|
fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||||
return 1; /* Failure */
|
return 1; /* Failure */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user