Introducing short to data_t and handlers for converting endian
This commit is contained in:
21
lib/base.c
21
lib/base.c
@@ -14,6 +14,12 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
short_t short_byteswap(const short_t w)
|
||||
{
|
||||
// TODO: is there a faster way of doing this?
|
||||
return WORD_NTH_BYTE(w, 1) | (WORD_NTH_BYTE(w, 0) << 8);
|
||||
}
|
||||
|
||||
hword_t hword_byteswap(const hword_t w)
|
||||
{
|
||||
// TODO: is there a faster way of doing this?
|
||||
@@ -30,6 +36,15 @@ word_t word_byteswap(const word_t w)
|
||||
WORD_NTH_BYTE(w, 1) << 48 | WORD_NTH_BYTE(w, 0) << 56;
|
||||
}
|
||||
|
||||
short_t convert_bytes_to_short(const byte_t *bytes)
|
||||
{
|
||||
short_t s = 0;
|
||||
memcpy(&s, bytes, SHORT_SIZE);
|
||||
if (!LITTLE_ENDIAN)
|
||||
s = short_byteswap(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
hword_t convert_bytes_to_hword(const byte_t *bytes)
|
||||
{
|
||||
hword_t h = 0;
|
||||
@@ -48,6 +63,12 @@ word_t convert_bytes_to_word(const byte_t *bytes)
|
||||
return h;
|
||||
}
|
||||
|
||||
void convert_short_to_bytes(short_t w, byte_t *bytes)
|
||||
{
|
||||
short_t h = LITTLE_ENDIAN ? w : short_byteswap(w);
|
||||
memcpy(bytes, &h, SHORT_SIZE);
|
||||
}
|
||||
|
||||
void convert_hword_to_bytes(hword_t w, byte_t *bytes)
|
||||
{
|
||||
hword_t h = LITTLE_ENDIAN ? w : hword_byteswap(w);
|
||||
|
||||
Reference in New Issue
Block a user