11 /* Public: Reads a subset of bits into a uint64_t.
13 * source - the bytes in question.
14 * offset - the starting index of the bit field (beginning from 0).
15 * bit_count - the width of the bit field to extract.
16 * data_is_big_endian - if the data passed in is little endian, set this to false and it
17 * will be flipped before grabbing the bit field.
19 * Bit fields are positioned according to big-endian bit layout.
21 * For example, the bit layout of the value "42" (i.e. 00101010 set at position
22 * 14 with length 6 is:
24 * 000000000000001010100000000000000000000000000000000000000000000
26 * and the same value and position but with length 8 is:
28 * 000000000000000010101000000000000000000000000000000000000000000
30 * If the architecture where is code is running is little-endian, the input data
31 * will be swapped before grabbing the bit field.
35 * uint64_t value = get_bitfield(data, 2, 4);
37 * Returns the value of the requested bit field, right aligned in a uint64_t.
39 uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset,
40 const uint16_t bit_count, const bool data_is_big_endian);
42 /* Public: Return a single nibble from the payload, with range checking.
44 * source - the source payload.
45 * nibble_index - the index of the nibble to retreive. The leftmost nibble is
47 * data_is_big_endian - if the data passed in is little endian, set this to false and it
48 * will be flipped before grabbing the bit field.
50 * Returns the retreived nibble, right aligned in a uint8_t.
52 uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index,
53 const bool data_is_big_endian);
55 /* Public: Return a single byte from the payload, with range checking.
57 * source - the source byte array.
58 * byte_index - the index of the byte to retreive. The leftmost byte is index 0.
59 * data_is_big_endian - if the data passed in is little endian, set this to false and it
60 * will be flipped before grabbing the bit field.
62 * Returns the retreived byte.
64 uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index,
65 const bool data_is_big_endian);
67 /* Public: Set the bit field in the given data array to the new value.
69 * destination - a byte array with size at least offset + bit_count.
70 * value - the value to set in the bit field.
71 * offset - the starting index of the bit field (beginning from 0).
72 * bit_count - the number of bits to set in the data.
74 * Returns true if the bit_count is enough to fully represent the value, and
75 * false if it will not fit.
77 bool eightbyte_set_bitfield(uint64_t value,
78 const uint16_t offset, const uint16_t bit_count, uint64_t* destination);
80 /* Private: Determine the index of the last bit used.
82 uint8_t find_end_bit(const uint16_t num_bits);