summaryrefslogtreecommitdiff
path: root/modules/sensors/dynamic_sensor/HidUtils/HidUtils.cpp
blob: 0cce2a39870eadd91fc65afa2a6a793862bccce8 (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
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "HidUtils.h"
#include <stdint.h>
#include <algorithm>

namespace HidUtil {

void copyBits(const void *src, void *dst, size_t dst_size,
              unsigned int src_bit_offset, unsigned int dst_bit_offset,
              unsigned int bit_count) {
    const uint8_t *p_src;
    uint8_t       *p_dst;
    uint8_t        dst_mask;
    unsigned int   bits_rem;
    unsigned int   bit_block_count;

    // Do nothing if copying past the end of the destination buffer.
    if ((static_cast<size_t>(dst_bit_offset) > (8 * dst_size)) ||
        (static_cast<size_t>(bit_count) > (8 * dst_size)) ||
        (static_cast<size_t>(dst_bit_offset + bit_count) > (8 * dst_size))) {
        return;
    }

    // Copy bits from source to destination buffer.
    p_src = static_cast<const uint8_t*>(src) + (src_bit_offset / 8);
    src_bit_offset = src_bit_offset % 8;
    p_dst = static_cast<uint8_t*>(dst) + (dst_bit_offset / 8);
    dst_bit_offset = dst_bit_offset % 8;
    bits_rem = bit_count;
    while (bits_rem > 0) {
        // Determine the size of the next block of bits to copy. The block must
        // not cross a source or desintation byte boundary.
        bit_block_count = std::min(bits_rem, 8 - src_bit_offset);
        bit_block_count = std::min(bit_block_count, 8 - dst_bit_offset);

        // Determine the destination bit block mask.
        dst_mask = ((1 << bit_block_count) - 1) << dst_bit_offset;

        // Copy the block of bits.
        *p_dst = (*p_dst & ~dst_mask) |
                 (((*p_src >> src_bit_offset) << dst_bit_offset) & dst_mask);

        // Advance past the block of copied bits in the source.
        src_bit_offset += bit_block_count;
        p_src += src_bit_offset / 8;
        src_bit_offset = src_bit_offset % 8;

        // Advance past the block of copied bits in the destination.
        dst_bit_offset += bit_block_count;
        p_dst += dst_bit_offset / 8;
        dst_bit_offset = dst_bit_offset % 8;

        // Decrement the number of bits remaining.
        bits_rem -= bit_block_count;
    }
}

} // namespace HidUtil