lanplus: Make byteswapping generic

Get rid of lanplus-specific yet very generic in nature
lanplus_swap() function that unconditionally swaps bytes
in an arbitrary byte array. Move it to helper module and
add two conditionally working interfaces to it:

 - array_ntoh() for network (BE) to host conversion, and
 - array_letoh() for ipmi (LE) to host conversion.

The added functions will only perform byte swapping if
the target architecture differs in endianness from the
data source. array_ntoh() will only do swap on LE machines,
while array_letoh() will only do it on BE ones.

These functions are introduced for future use in other
places of ipmitool.

Partially resolves ipmitool/ipmitool#26

Signed-off-by: Alexander Amelkin <alexander@amelkin.msk.ru>
This commit is contained in:
Alexander Amelkin
2018-08-01 11:33:44 +03:00
parent 5491b12596
commit 7747d86cc4
3 changed files with 63 additions and 77 deletions

View File

@@ -228,6 +228,48 @@ void printbuf(const uint8_t * buf, int len, const char * desc)
fprintf(stderr, "\n");
}
/*
* Unconditionally reverse the order of arbitrarily long strings of bytes
*/
static uint8_t *_ipmi_byteswap(uint8_t *buffer, size_t length)
{
size_t i;
uint8_t temp;
size_t max = length - 1;
for (i = 0; i < length / 2; ++i) {
temp = buffer[i];
buffer[i] = buffer[max - i];
buffer[max - i] = temp;
}
return buffer;
}
/* Convert data array from network (big-endian) to host byte order */
uint8_t *array_ntoh(uint8_t *buffer, size_t length)
{
#if WORDS_BIGENDIAN
/* Big-endian host doesn't need conversion from big-endian network */
return buffer;
#else
/* Little-endian host needs conversion from big-endian network */
return _ipmi_byteswap(buffer, length);
#endif
}
/* Convert data array from little-endian to host byte order */
uint8_t *array_letoh(uint8_t *buffer, size_t length)
{
#if WORDS_BIGENDIAN
/* Big-endian host needs conversion from little-endian IPMI */
return _ipmi_byteswap(buffer, length);
#else
/* Little-endian host doesn't need conversion from little-endian IPMI */
return buffer;
#endif
}
/* str2mac - parse-out MAC address from given string and store it
* into buffer.
*