summaryrefslogtreecommitdiffstats
path: root/pb_encode.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2016-01-27 18:59:54 +0200
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2016-01-27 20:34:46 +0200
commit3b6099faa2d829c74c9576f4aa1ce4b237c997db (patch)
treedc0822f496f2bd5bf4f03ffc80a22385bb086cd8 /pb_encode.c
parent8d35488bcde1cc6d912d4c875bcf6d2745899d7d (diff)
Fix a few remaining bugs related to CHAR_BIT!=8 platforms.
Diffstat (limited to 'pb_encode.c')
-rw-r--r--pb_encode.c56
1 files changed, 30 insertions, 26 deletions
diff --git a/pb_encode.c b/pb_encode.c
index 95683751..9f91c9d5 100644
--- a/pb_encode.c
+++ b/pb_encode.c
@@ -572,16 +572,16 @@ static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *fi
{
int64_t value = 0;
- /* Cases 1 and 2 are for compilers that have smaller types for bool
- * or enums, and for int_size option. */
- switch (field->data_size)
- {
- case 1: value = *(const int_least8_t*)src; break;
- case 2: value = *(const int_least16_t*)src; break;
- case 4: value = *(const int32_t*)src; break;
- case 8: value = *(const int64_t*)src; break;
- default: PB_RETURN_ERROR(stream, "invalid data_size");
- }
+ if (field->data_size == sizeof(int_least8_t))
+ value = *(const int_least8_t*)src;
+ else if (field->data_size == sizeof(int_least16_t))
+ value = *(const int_least16_t*)src;
+ else if (field->data_size == sizeof(int32_t))
+ value = *(const int32_t*)src;
+ else if (field->data_size == sizeof(int64_t))
+ value = *(const int64_t*)src;
+ else
+ PB_RETURN_ERROR(stream, "invalid data_size");
return pb_encode_varint(stream, (uint64_t)value);
}
@@ -590,14 +590,16 @@ static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *f
{
uint64_t value = 0;
- switch (field->data_size)
- {
- case 1: value = *(const uint_least8_t*)src; break;
- case 2: value = *(const uint_least16_t*)src; break;
- case 4: value = *(const uint32_t*)src; break;
- case 8: value = *(const uint64_t*)src; break;
- default: PB_RETURN_ERROR(stream, "invalid data_size");
- }
+ if (field->data_size == sizeof(uint_least8_t))
+ value = *(const uint_least8_t*)src;
+ else if (field->data_size == sizeof(uint_least16_t))
+ value = *(const uint_least16_t*)src;
+ else if (field->data_size == sizeof(uint32_t))
+ value = *(const uint32_t*)src;
+ else if (field->data_size == sizeof(uint64_t))
+ value = *(const uint64_t*)src;
+ else
+ PB_RETURN_ERROR(stream, "invalid data_size");
return pb_encode_varint(stream, value);
}
@@ -606,14 +608,16 @@ static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *f
{
int64_t value = 0;
- switch (field->data_size)
- {
- case 1: value = *(const int_least8_t*)src; break;
- case 2: value = *(const int_least16_t*)src; break;
- case 4: value = *(const int32_t*)src; break;
- case 8: value = *(const int64_t*)src; break;
- default: PB_RETURN_ERROR(stream, "invalid data_size");
- }
+ if (field->data_size == sizeof(int_least8_t))
+ value = *(const int_least8_t*)src;
+ else if (field->data_size == sizeof(int_least16_t))
+ value = *(const int_least16_t*)src;
+ else if (field->data_size == sizeof(int32_t))
+ value = *(const int32_t*)src;
+ else if (field->data_size == sizeof(int64_t))
+ value = *(const int64_t*)src;
+ else
+ PB_RETURN_ERROR(stream, "invalid data_size");
return pb_encode_svarint(stream, value);
}