Update doc revision and pdf cover.
[apps/low-level-can-service.git] / libs / bitfield-c / src / bitfield / bitfield.c
1 #include <bitfield/bitfield.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <stddef.h>
5 #include <sys/param.h>
6
7 uint64_t bitmask(const uint8_t bit_count) {
8     return (((uint64_t)0x1) << bit_count) - 1;
9 }
10
11 uint8_t get_nibble(const uint8_t source[], const uint8_t source_length,
12                 const uint8_t nibble_index) {
13     uint8_t byte_index = nibble_index / 2;
14     uint8_t result = get_byte(source, source_length, byte_index);
15     if(nibble_index % 2 == 0) {
16         result >>= NIBBLE_SIZE;
17     }
18     result &= bitmask(NIBBLE_SIZE);
19     return result;
20 }
21
22 uint8_t get_byte(const uint8_t source[], const uint8_t source_length,
23         const uint8_t byte_index) {
24     if(byte_index < source_length) {
25         return source[byte_index];
26     }
27     return 0;
28 }
29
30 uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length,
31                 const uint16_t offset, const uint16_t bit_count) {
32     if(bit_count > 64 || bit_count < 1) {
33         // TODO error reporting?
34         return 0;
35     }
36
37     ArrayOrBytes combined;
38     memset(combined.bytes, 0, sizeof(combined.bytes));
39     if(copy_bits_right_aligned(source, source_length, offset, bit_count,
40             combined.bytes, sizeof(combined.bytes))) {
41         if(BYTE_ORDER == LITTLE_ENDIAN) {
42             combined.whole = __builtin_bswap64(combined.whole);
43         }
44     } else {
45         // debug("couldn't copy enough bits from source")
46     }
47     return combined.whole;
48 }
49
50 bool set_nibble(const uint16_t nibble_index, const uint8_t value,
51         uint8_t* destination, const uint16_t destination_length) {
52     return copy_bits(&value, CHAR_BIT, NIBBLE_SIZE, NIBBLE_SIZE, destination,
53             destination_length, nibble_index * NIBBLE_SIZE);
54 }
55
56 bool set_bitfield(const uint64_t value, const uint16_t offset,
57         const uint16_t bit_count, uint8_t destination[],
58         uint16_t destination_length) {
59     if(value > bitmask(bit_count)) {
60         return false;
61     }
62
63     ArrayOrBytes combined = {
64         whole: value
65     };
66
67     if(BYTE_ORDER == LITTLE_ENDIAN) {
68         combined.whole = __builtin_bswap64(combined.whole);
69     }
70
71     return copy_bits(combined.bytes, sizeof(combined.bytes),
72             sizeof(combined.bytes) * CHAR_BIT - bit_count, bit_count,
73             destination, destination_length, offset);
74 }