etsi_its_messages v3.0.0
 
Loading...
Searching...
No Matches
asn1_primitives_getters.h
1/*
2=============================================================================
3MIT License
4
5Copyright (c) 2023-2024 Institute for Automotive Engineering (ika), RWTH Aachen University
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24=============================================================================
25*/
26
27#ifndef ETSI_ITS_MSGS_UTILS_IMPL_ASN1_PRIMITIVES_ASN1_PRIMITIVES_GETTERS_H
28#define ETSI_ITS_MSGS_UTILS_IMPL_ASN1_PRIMITIVES_ASN1_PRIMITIVES_GETTERS_H
29
34
42inline std::vector<bool> getBitString(const std::vector<uint8_t>& buffer, const int bits_unused) {
43 // bit string size
44 const int bits_per_byte = 8;
45 const int n_bytes = buffer.size();
46 const int n_bits = n_bytes * bits_per_byte;
47 std::vector<bool> bits;
48 bits.resize(n_bits - bits_unused, 0);
49
50 // loop over bytes in reverse order
51 for (int byte_idx = n_bytes - 1; byte_idx >= 0; byte_idx--) {
52 // loop over bits in a byte
53 for (int bit_idx_in_byte = bits_per_byte - 1; bit_idx_in_byte >= 0; bit_idx_in_byte--) {
54
55 // map bit index in byte to bit index in total bitstring
56 int bit_idx = (n_bytes - byte_idx - 1) * bits_per_byte + bit_idx_in_byte;
57 if (byte_idx == 0 && bit_idx < bits_unused) break;
58
59 // extract bit from bitstring and set output array entry appropriately
60 bool byte_has_true_bit = buffer[byte_idx] & (1 << bit_idx_in_byte);
61 if (byte_has_true_bit) bits[bits_per_byte-bit_idx-1] = true;
62 }
63 }
64 return bits;
65}
66
67#endif // ETSI_ITS_MSGS_UTILS_IMPL_ASN1_PRIMITIVES_GETTERS_H