etsi_its_messages v3.4.0
Loading...
Searching...
No Matches
asn1_primitives_setters.h
1// SPDX-License-Identifier: MIT
2// Copyright Institute for Automotive Engineering (ika), RWTH Aachen University
3
4#ifndef ETSI_ITS_MSGS_UTILS_IMPL_ASN1_PRIMITIVES_ASN1_PRIMITIVES_SETTERS_H
5#define ETSI_ITS_MSGS_UTILS_IMPL_ASN1_PRIMITIVES_ASN1_PRIMITIVES_SETTERS_H
6
11
19template <typename T>
20inline void setBitString(T& bitstring, const std::vector<bool>& bits) {
21
22 // bit string size
23 const int bits_per_byte = 8;
24 const int n_bytes = static_cast<int>((bits.size() - 1) / bits_per_byte) + 1;
25 const int n_bits = n_bytes * bits_per_byte;
26
27 // init output
28 bitstring.bits_unused = n_bits - bits.size();
29 bitstring.value = std::vector<uint8_t>(n_bytes);
30
31 // loop over all bytes
32 for (int byte_idx = 0; byte_idx < n_bytes; byte_idx++) {
33
34 // loop over bits in a byte
35 for (int bit_idx_in_byte = 0; bit_idx_in_byte < bits_per_byte; bit_idx_in_byte++) {
36
37 // map bit index in byte to bit index in total bitstring
38 int bit_idx = bit_idx_in_byte + byte_idx * bits_per_byte;
39 if ((byte_idx + 1) >= n_bytes && (bit_idx_in_byte + bitstring.bits_unused) >= bits_per_byte) break;
40
41 // set bit in output bitstring appropriately
42 bitstring.value[byte_idx] |= bits[bit_idx] << (bits_per_byte - 1 - bit_idx_in_byte);
43 }
44 }
45}
46
47#endif // ETSI_ITS_MSGS_UTILS_IMPL_ASN1_PRIMITIVES_SETTERS_H