## Bitfield Manipulation
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
- uint8_t result = getByte(data, sizeof(data), 0);
- uint8_t result = getNibble(data, sizeof(data), 0);
- fail_unless(copyBitsRightAligned(data, 4, 4, 12, result, 4));
+ uint8_t result = get_byte(data, sizeof(data), 0);
+ uint8_t result = get_nibble(data, sizeof(data), 0);
+ fail_unless(copy_bits_right_aligned(data, 4, 4, 12, result, 4));
## 8 Byte Bitfield Decoding
static const uint8_t reverse_mask_xor[] =
{ 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 };
-bool copyBits(const uint8_t* source_origin, const uint16_t source_length,
+bool copy_bits(const uint8_t* source_origin, const uint16_t source_length,
const uint16_t source_offset, uint16_t bit_count,
uint8_t* destination_origin, const uint16_t destination_length,
const uint16_t destination_offset) {
return endBit == 0 ? CHAR_BIT : endBit;
}
-bool copyBitsRightAligned(const uint8_t source[], const uint16_t source_length,
+bool copy_bits_right_aligned(const uint8_t source[], const uint16_t source_length,
const uint16_t offset, const uint16_t bit_count,
uint8_t* destination, const uint16_t destination_length) {
- return copyBits(source, source_length, offset, bit_count, destination,
+ return copy_bits(source, source_length, offset, bit_count, destination,
destination_length,
// provide a proper destination offset so the result is right
// aligned
#define NIBBLE_SIZE (CHAR_BIT / 2)
-uint8_t getNibble(const uint8_t source[], const uint8_t source_length,
+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;
- uint8_t result = getByte(source, source_length, byte_index);
+ uint8_t result = get_byte(source, source_length, byte_index);
if(nibble_index % 2 == 0) {
result >>= NIBBLE_SIZE;
}
return result;
}
-uint8_t getByte(const uint8_t source[], const uint8_t source_length,
+uint8_t get_byte(const uint8_t source[], const uint8_t source_length,
const uint8_t byte_index) {
if(byte_index < source_length) {
return source[byte_index];
*
* Returns the retreived nibble, right aligned in a uint8_t.
*/
-uint8_t getNibble(const uint8_t source[], const uint8_t source_length,
+uint8_t get_nibble(const uint8_t source[], const uint8_t source_length,
const uint8_t nibble_index);
/* Public: Return a single byte from the byte array, with range checking.
*
* Returns the retreived byte.
*/
-uint8_t getByte(const uint8_t source[], const uint8_t source_length,
+uint8_t get_byte(const uint8_t source[], const uint8_t source_length,
const uint8_t byte_index);
/* Public: Copy a range of bits from one bit array to another.
*
* uint8_t source[4] = {0x11, 0x22, 0x33, 0x44};
* uint8_t destination[4] = {0};
- * copyBits(source, sizeof(source), 8, 8, destination,
+ * copy_bits(source, sizeof(source), 8, 8, destination,
* sizeof(destination), 0);
* // destination[0] == 0x22
* // destination[1] == 0x0
* Returns true if the copy was successful and false if the range exceeded the
* size of the source or destination, or if the range size negative or 0.
*/
-bool copyBits(const uint8_t* source_origin, const uint16_t source_length,
+bool copy_bits(const uint8_t* source_origin, const uint16_t source_length,
const uint16_t source_offset, uint16_t bit_count,
uint8_t* destination_origin, const uint16_t destination_length,
const uint16_t destination_offset);
*
* uint8_t source[4] = {0x11, 0x22, 0x33, 0x44};
* uint8_t destination[4] = {0};
- * copyBitsRightAligned(source, sizeof(source), 8, 8, destination,
+ * copy_bits_right_aligned(source, sizeof(source), 8, 8, destination,
* sizeof(destination));
* // destination[0] == 0x0
* // destination[1] == 0x0
* int value = (int)destination;
* // value == 0x22 == 32
*
- * The arguments are the same as copyBits, but without the destination_offset
+ * The arguments are the same as copy_bits, but without the destination_offset
* option - that's set automatically to right align the result.
*
* Returns true if the copy was successful and false if the range exceeded the
* size of the source or destination, or if the range size negative or 0.
*/
-bool copyBitsRightAligned(const uint8_t source[], const uint16_t source_length,
+bool copy_bits_right_aligned(const uint8_t source[], const uint16_t source_length,
const uint16_t offset, const uint16_t bit_count,
uint8_t* destination, const uint16_t destination_length);
START_TEST (test_get_byte)
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
- uint8_t result = getByte(data, sizeof(data), 0);
+ uint8_t result = get_byte(data, sizeof(data), 0);
ck_assert_int_eq(result, 0x12);
- result = getByte(data, sizeof(data), 3);
+ result = get_byte(data, sizeof(data), 3);
ck_assert_int_eq(result, 0x78);
}
END_TEST
START_TEST (test_get_nibble)
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
- uint8_t result = getNibble(data, sizeof(data), 0);
+ uint8_t result = get_nibble(data, sizeof(data), 0);
ck_assert_int_eq(result, 0x1);
- result = getNibble(data, sizeof(data), 1);
+ result = get_nibble(data, sizeof(data), 1);
ck_assert_int_eq(result, 0x2);
- result = getNibble(data, sizeof(data), 2);
+ result = get_nibble(data, sizeof(data), 2);
ck_assert_int_eq(result, 0x3);
}
END_TEST
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t result[4];
- fail_if(copyBitsRightAligned(data, 4, 25, 16, result, 4));
+ fail_if(copy_bits_right_aligned(data, 4, 25, 16, result, 4));
}
END_TEST
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t result[4] = {0};
- fail_unless(copyBitsRightAligned(data, 4, 0, 16, result, 4));
+ fail_unless(copy_bits_right_aligned(data, 4, 0, 16, result, 4));
ck_assert_int_eq(result[0], 0x12);
ck_assert_int_eq(result[1], 0x34);
}
{
uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
uint8_t result[4] = {0};
- fail_unless(copyBitsRightAligned(data, 4, 4, 12, result, 4));
+ fail_unless(copy_bits_right_aligned(data, 4, 4, 12, result, 4));
ck_assert_int_eq(result[0], 0x2);
ck_assert_int_eq(result[1], 0x34);
}
ck_assert_int_eq(value, 0x0000000000000000LLU);
}
END_TEST
-// TODO test encode bool
Suite* canwriteSuite(void) {
Suite* s = suite_create("write");