#include "pb_encode.h"
#include <string.h>
-#ifdef __GNUC__
-/* Verify that we remember to check all return values for proper error propagation */
-#define checkreturn __attribute__((warn_unused_result))
+/* The warn_unused_result attribute appeared first in gcc-3.4.0 */
+#if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
+ #define checkreturn
#else
-#define checkreturn
+ /* Verify that we remember to check all return values for proper error propagation */
+ #define checkreturn __attribute__((warn_unused_result))
#endif
-
typedef bool (*pb_encoder_t)(pb_ostream_t *stream, const pb_field_t *field, const void *src) checkreturn;
/* --- Function pointers to field encoders ---
static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
const void *pData, size_t count, pb_encoder_t func)
{
- int i;
+ size_t i;
const void *p;
size_t size;
}
else
{
- pb_ostream_t sizestream = {0};
+ pb_ostream_t sizestream = {0,0,0,0};
p = pData;
for (i = 0; i < count; i++)
{
size = sizestream.bytes_written;
}
- if (!pb_encode_varint(stream, size))
+ if (!pb_encode_varint(stream, (uint64_t)size))
return false;
if (stream->callback == NULL)
while (value)
{
- buffer[i] = (value & 0x7F) | 0x80;
+ buffer[i] = (uint8_t)((value & 0x7F) | 0x80);
value >>= 7;
i++;
}
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);
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);
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)
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);
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;
size = substream.bytes_written;
- if (!pb_encode_varint(stream, size))
+ if (!pb_encode_varint(stream, (uint64_t)size))
return false;
if (stream->callback == NULL)
bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
+ UNUSED(field);
return pb_encode_fixed64(stream, src);
}
bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
+ UNUSED(field);
return pb_encode_fixed32(stream, src);
}
bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
pb_bytes_array_t *bytes = (pb_bytes_array_t*)src;
+ UNUSED(field);
return pb_encode_string(stream, bytes->bytes, bytes->size);
}
bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
+ UNUSED(field);
return pb_encode_string(stream, (uint8_t*)src, strlen((char*)src));
}