diff options
author | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2013-12-29 14:40:15 -0500 |
---|---|---|
committer | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2013-12-29 14:40:15 -0500 |
commit | 0ba19fae04ee48392872a9647a3b711b9115f147 (patch) | |
tree | 4ba55a5925abe773531bf65256e8cbca5f03d28d /src | |
parent | 3a6af99be9a10f795b84a5783939b86d7102fb63 (diff) |
Add get_byte and get_nibble to 8byte function set.
Diffstat (limited to 'src')
-rw-r--r-- | src/bitfield/8byte.c | 20 | ||||
-rw-r--r-- | src/bitfield/8byte.h | 27 | ||||
-rw-r--r-- | src/bitfield/bitfield.c | 2 | ||||
-rw-r--r-- | src/bitfield/bitfield.h | 2 |
4 files changed, 42 insertions, 9 deletions
diff --git a/src/bitfield/8byte.c b/src/bitfield/8byte.c index 845be8c0..f08f227d 100644 --- a/src/bitfield/8byte.c +++ b/src/bitfield/8byte.c @@ -18,8 +18,21 @@ static uint16_t bits_to_bytes(uint32_t bits) { return byte_count; } +uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index, + const bool big_endian) { + return get_bit_field(source, NIBBLE_SIZE * nibble_index, NIBBLE_SIZE, + big_endian); +} + +uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index, + const bool big_endian) { + // TODO we're not handling swapped endianness - we could use get_bit_field + // but this might be more efficient + return (source >> (EIGHTBYTE_BIT - ((byte_index + 1) * CHAR_BIT))) & 0xFF; +} + uint64_t get_bit_field(uint64_t source, const uint16_t offset, - const uint16_t bit_count, bool big_endian) { + const uint16_t bit_count, const bool big_endian) { int startByte = offset / CHAR_BIT; int endByte = (offset + bit_count - 1) / CHAR_BIT; @@ -54,8 +67,3 @@ bool set_bit_field(uint64_t* destination, uint64_t value, const uint16_t offset, *destination |= value; return true; } - -uint8_t nth_byte(const uint64_t source, const uint16_t byte_index) { - return (source >> (EIGHTBYTE_BIT - ((byte_index + 1) * CHAR_BIT))) & 0xFF; -} - diff --git a/src/bitfield/8byte.h b/src/bitfield/8byte.h index 1ee9c0ec..818372d5 100644 --- a/src/bitfield/8byte.h +++ b/src/bitfield/8byte.h @@ -47,7 +47,32 @@ extern "C" { * Returns the value of the requested bit field, right aligned in a uint64_t. */ uint64_t get_bit_field(uint64_t source, const uint16_t offset, - const uint16_t bit_count, bool big_endian); + const uint16_t bit_count, const bool big_endian); + +/* Public: Return a single nibble from the payload, with range checking. + * + * source - the source payload. + * nibble_index - the index of the nibble to retreive. The leftmost nibble is + * index 0. + * big_endian - if the data passed in is little endian, set this to false and it + * will be flipped before grabbing the bit field. + * + * Returns the retreived nibble, right aligned in a uint8_t. + */ +uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index, + const bool big_endian); + +/* Public: Return a single byte from the payload, with range checking. + * + * source - the source byte array. + * byte_index - the index of the byte to retreive. The leftmost byte is index 0. + * big_endian - if the data passed in is little endian, set this to false and it + * will be flipped before grabbing the bit field. + * + * Returns the retreived byte. + */ +uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index, + const bool big_endian); /* Public: Set the bit field in the given data array to the new value. * diff --git a/src/bitfield/bitfield.c b/src/bitfield/bitfield.c index 7dfa7cd4..af17d485 100644 --- a/src/bitfield/bitfield.c +++ b/src/bitfield/bitfield.c @@ -3,8 +3,6 @@ #include <string.h> #include <stddef.h> -#define NIBBLE_SIZE (CHAR_BIT / 2) - uint8_t get_nibble(const uint8_t source[], const uint8_t source_length, const uint8_t nibble_index) { uint8_t byte_index = nibble_index / 2; diff --git a/src/bitfield/bitfield.h b/src/bitfield/bitfield.h index 52ed143d..b58e4e59 100644 --- a/src/bitfield/bitfield.h +++ b/src/bitfield/bitfield.h @@ -4,6 +4,8 @@ #include <stdint.h> #include <stdbool.h> +#define NIBBLE_SIZE (CHAR_BIT / 2) + #ifdef __cplusplus extern "C" { #endif |