Add check for sizeof(double) == 8.
[apps/agl-service-can-low-level.git] / pb_encode.c
index 1817373..bba5dc1 100644 (file)
@@ -8,14 +8,14 @@
 #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 ---
@@ -37,15 +37,22 @@ static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = {
 static bool checkreturn buf_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
 {
     uint8_t *dest = (uint8_t*)stream->state;
-    memcpy(dest, buf, count);
     stream->state = dest + count;
+    
+    while (count--)
+        *dest++ = *buf++;
+    
     return true;
 }
 
 pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize)
 {
     pb_ostream_t stream;
+#ifdef PB_BUFFER_ONLY
+    stream.callback = (void*)1; /* Just some marker value */
+#else
     stream.callback = &buf_write;
+#endif
     stream.state = buf;
     stream.max_size = bufsize;
     stream.bytes_written = 0;
@@ -58,9 +65,14 @@ bool checkreturn pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count
     {
         if (stream->bytes_written + count > stream->max_size)
             return false;
-        
+
+#ifdef PB_BUFFER_ONLY
+        if (!buf_write(stream, buf, count))
+            return false;
+#else        
         if (!stream->callback(stream, buf, count))
             return false;
+#endif
     }
     
     stream->bytes_written += count;
@@ -99,7 +111,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
         }
         else
         {
-            pb_ostream_t sizestream = {0};
+            pb_ostream_t sizestream = {0,0,0,0};
             p = pData;
             for (i = 0; i < count; i++)
             {
@@ -110,7 +122,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
             size = sizestream.bytes_written;
         }
         
-        if (!pb_encode_varint(stream, size))
+        if (!pb_encode_varint(stream, (uint64_t)size))
             return false;
         
         if (stream->callback == NULL)
@@ -168,7 +180,7 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons
                 break;
             
             case PB_HTYPE_OPTIONAL:
-                if (*(bool*)pSize)
+                if (*(const bool*)pSize)
                 {
                     if (!pb_encode_tag_for_field(stream, field))
                         return false;
@@ -179,13 +191,13 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons
                 break;
             
             case PB_HTYPE_ARRAY:
-                if (!encode_array(stream, field, pData, *(size_t*)pSize, func))
+                if (!encode_array(stream, field, pData, *(const size_t*)pSize, func))
                     return false;
                 break;
             
             case PB_HTYPE_CALLBACK:
             {
-                pb_callback_t *callback = (pb_callback_t*)pData;
+                const pb_callback_t *callback = (const pb_callback_t*)pData;
                 if (callback->funcs.encode != NULL)
                 {
                     if (!callback->funcs.encode(stream, field, callback->arg))
@@ -205,7 +217,7 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons
 bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
 {
     uint8_t buffer[10];
-    int i = 0;
+    size_t i = 0;
     
     if (value == 0)
         return pb_write(stream, (uint8_t*)&value, 1);
@@ -225,9 +237,9 @@ bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
 {
     uint64_t zigzagged;
     if (value < 0)
-        zigzagged = ~(value << 1);
+        zigzagged = (uint64_t)(~(value << 1));
     else
-        zigzagged = value << 1;
+        zigzagged = (uint64_t)(value << 1);
     
     return pb_encode_varint(stream, zigzagged);
 }
@@ -235,29 +247,40 @@ bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
 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);
+    return pb_write(stream, (const uint8_t*)value, 4);
     #endif
 }
 
 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);
+    return pb_write(stream, (const uint8_t*)value, 8);
     #endif
 }
 
-bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, int field_number)
+bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
 {
-    int tag = wiretype | (field_number << 3);
+    uint64_t tag = wiretype | (field_number << 3);
     return pb_encode_varint(stream, tag);
 }
 
@@ -294,7 +317,7 @@ bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t
 
 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);
@@ -303,7 +326,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)
 {
     /* 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;
     
@@ -312,7 +335,7 @@ bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fie
     
     size = substream.bytes_written;
     
-    if (!pb_encode_varint(stream, size))
+    if (!pb_encode_varint(stream, (uint64_t)size))
         return false;
     
     if (stream->callback == NULL)
@@ -347,10 +370,10 @@ bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, co
     
     switch (field->data_size)
     {
-        case 1: value = *(uint8_t*)src; break;
-        case 2: value = *(uint16_t*)src; break;
-        case 4: value = *(uint32_t*)src; break;
-        case 8: value = *(uint64_t*)src; break;
+        case 1: value = *(const uint8_t*)src; break;
+        case 2: value = *(const uint16_t*)src; break;
+        case 4: value = *(const uint32_t*)src; break;
+        case 8: value = *(const uint64_t*)src; break;
         default: return false;
     }
     
@@ -359,12 +382,12 @@ bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, co
 
 bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
 {
-    uint64_t value = 0;
+    int64_t value = 0;
     
     switch (field->data_size)
     {
-        case 4: value = *(int32_t*)src; break;
-        case 8: value = *(int64_t*)src; break;
+        case 4: value = *(const int32_t*)src; break;
+        case 8: value = *(const int64_t*)src; break;
         default: return false;
     }
     
@@ -385,7 +408,7 @@ bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, c
 
 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;
+    const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
     UNUSED(field);
     return pb_encode_string(stream, bytes->bytes, bytes->size);
 }
@@ -393,7 +416,7 @@ bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, con
 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));
+    return pb_encode_string(stream, (const uint8_t*)src, strlen((const char*)src));
 }
 
 bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
@@ -401,6 +424,6 @@ bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field
     if (field->ptr == NULL)
         return false;
     
-    return pb_encode_submessage(stream, (pb_field_t*)field->ptr, src);
+    return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
 }