Various bugfixes and improvements

- Fixed wrong return type from swap16() and swap32().
- Padding must be calculated according to alignment rules (given by
_Alignof), not type size. While that would work on 32-bit CPUs, it
might fail on 8-bit or 16-bit CPUs.
- Added extra parentheses on assignment in while loop to avoid compiler warning.
This commit is contained in:
Xavier Del Campo Romero 2020-05-04 16:45:54 +02:00
parent dfa47a3a6b
commit 71a3db14a5
1 changed files with 13 additions and 5 deletions

View File

@ -158,7 +158,7 @@ static void read8(void *const dst, const void *const src)
*(uint8_t *)dst = *(const uint8_t *)src;
}
static size_t swap32(void *dst, const void *const src)
static void swap32(void *dst, const void *const src)
{
*(uint8_t *)dst++ = *((const uint8_t *)src + 3);
*(uint8_t *)dst++ = *((const uint8_t *)src + 2);
@ -200,7 +200,7 @@ static void readle32(void *const dst, const void *const src)
}
}
static size_t swap16(void *dst, const void *const src)
static void swap16(void *dst, const void *const src)
{
*(uint8_t *)dst++ = *((const uint8_t *)src + 1);
*(uint8_t *)dst = *(const uint8_t *)src;
@ -257,7 +257,7 @@ enum serializer_err deserialize(const char *format,
char c;
size_t in_sz = 0, out_sz = 0;
while (c = *format)
while ((c = *format))
{
const enum token token = get_token(c, &state, &endianness);
@ -274,7 +274,14 @@ enum serializer_err deserialize(const char *format,
[TOKEN_BE32BIT] = sizeof (uint32_t)
};
const size_t st = sizes[token];
static const size_t aligns[] =
{
[TOKEN_8BIT] = _Alignof (uint8_t),
[TOKEN_LE16BIT] = _Alignof (uint16_t),
[TOKEN_BE16BIT] = _Alignof (uint16_t),
[TOKEN_LE32BIT] = _Alignof (uint32_t),
[TOKEN_BE32BIT] = _Alignof (uint32_t)
};
static void (*const read[])(void *dst, const void *src) =
{
@ -285,7 +292,8 @@ enum serializer_err deserialize(const char *format,
[TOKEN_BE32BIT] = readbe32
};
const size_t padding = out_sz % st;
const size_t st = sizes[token];
const size_t padding = (uintptr_t)((const uint8_t *)dst + out_sz) % aligns[token];
if (padding)
{