summaryrefslogtreecommitdiffstats
path: root/libs/bitfield-c/src/bitfield/bitfield.c
blob: e9077e1cfc4b4bc631182ddb1a3c2d2e6eaf701c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bitfield/bitfield.h>
#include <limits.h>
#include <string.h>
#include <stddef.h>
#include <sys/param.h>

uint64_t bitmask(const unsigned int bit_count) {
    return (((uint64_t)0x1) << bit_count) - 1;
}

unsigned int get_nibble(const uint8_t source[], const unsigned int source_length,
                const unsigned int nibble_index) {
    unsigned int byte_index = nibble_index / 2;
    unsigned int result = get_byte(source, source_length, byte_index);
    if(nibble_index % 2 == 0) {
        result >>= NIBBLE_SIZE;
    }
    result &= bitmask(NIBBLE_SIZE);
    return result;
}

unsigned int get_byte(const uint8_t source[], const unsigned int source_length,
        const unsigned int byte_index) {
    if(byte_index < source_length) {
        return source[byte_index];
    }
    return 0;
}

uint64_t get_bitfield(const uint8_t source[], const unsigned int source_length,
                const unsigned int offset, const unsigned int bit_count) {
    if(bit_count > 64 || bit_count < 1) {
        // TODO error reporting?
        return 0;
    }

    ArrayOrBytes combined;
    memset(combined.bytes, 0, sizeof(combined.bytes));
    if(copy_bits_right_aligned(source, source_length, offset, bit_count,
            combined.bytes, sizeof(combined.bytes))) {
        if(BYTE_ORDER == LITTLE_ENDIAN) {
            combined.whole = __builtin_bswap64(combined.whole);
        }
    } else {
        // debug("couldn't copy enough bits from source")
    }
    return combined.whole;
}

bool set_nibble(const unsigned int nibble_index, const unsigned int value,
        uint8_t* destination, const unsigned int destination_length) {
    return copy_bits(&value, CHAR_BIT, NIBBLE_SIZE, NIBBLE_SIZE, destination,
            destination_length, nibble_index * NIBBLE_SIZE);
}

bool set_bitfield(const uint64_t value, const unsigned int offset,
        const unsigned int bit_count, uint8_t destination[],
        unsigned int destination_length) {
    if(value > bitmask(bit_count)) {
        return false;
    }

    ArrayOrBytes combined = {
        whole: value
    };

    if(BYTE_ORDER == LITTLE_ENDIAN) {
        combined.whole = __builtin_bswap64(combined.whole);
    }

    return copy_bits(combined.bytes, sizeof(combined.bytes),
            sizeof(combined.bytes) * CHAR_BIT - bit_count, bit_count,
            destination, destination_length, offset);
}