27BerTlvUtil::BerTlvUtil() {}
30 const std::vector<uint8_t>& tlvStructure,
const bool primitiveOnly)
33 return parseBufferSimple(tlvStructure, primitiveOnly);
41 const std::vector<uint8_t>& tlvStructure,
const bool primitiveOnly)
44 return parseBuffer(tlvStructure, primitiveOnly);
53 if (tagId < 0 || tagId > 0xFFFFFF) {
58 return (tagId & 0x20) != 0;
61 if (tagId <= 0xFFFF) {
62 return (tagId & 0x2000) != 0;
65 return (tagId & 0x200000) != 0;
68const std::map<const int, const std::vector<uint8_t>> BerTlvUtil::parseBufferSimple(
69 const std::vector<uint8_t>& tlvStructure,
const bool primitiveOnly)
72 std::map<const int, const std::vector<uint8_t>> tlvs;
76 const int tagSize = getTagSize(tlvStructure, offset);
77 const std::vector<uint8_t> tagBytes =
79 const int tag = getTag(tlvStructure, offset, tagSize);
80 const int lengthSize = getLengthSize(tlvStructure, offset + tagSize);
81 const int valueSize = getLength(tlvStructure, offset + tagSize, lengthSize);
82 const std::vector<uint8_t> value =
84 offset + tagSize + lengthSize,
85 offset + tagSize + lengthSize + valueSize);
87 offset += tagSize + lengthSize + valueSize;
89 if ((tagBytes[0] & 0x20) != 0) {
92 tlvs.insert({tag, value});
95 const std::map<const int, const std::vector<uint8_t>>
parse =
100 tlvs.insert({tag, value});
102 }
while (offset <
static_cast<int>(tlvStructure.size()));
107const std::map<const int, std::vector<std::vector<uint8_t>>> BerTlvUtil::parseBuffer(
108 const std::vector<uint8_t>& tlvStructure,
const bool primitiveOnly)
110 std::map<const int, std::vector<std::vector<uint8_t>>> tlvs;
114 const int tagSize = getTagSize(tlvStructure, offset);
115 const std::vector<uint8_t> tagBytes =
117 const int tag = getTag(tlvStructure, offset, tagSize);
118 const int lengthSize = getLengthSize(tlvStructure, offset + tagSize);
119 const int valueSize = getLength(tlvStructure, offset + tagSize, lengthSize);
120 const std::vector<uint8_t> value =
122 offset + tagSize + lengthSize,
123 offset + tagSize + lengthSize + valueSize);
125 offset += tagSize + lengthSize + valueSize;
127 if ((tagBytes[0] & 0x20) != 0) {
129 if (!primitiveOnly) {
130 std::vector<std::vector<uint8_t>>& values = getOrInitTagValues(tlvs, tag);
131 values.push_back(value);
134 std::map<const int, std::vector<std::vector<uint8_t>>> tlvs2 =
135 parse(value, primitiveOnly);
137 for (
const auto & entry : tlvs2) {
138 std::vector<std::vector<uint8_t>>& values = getOrInitTagValues(tlvs, entry.first);
143 std::vector<std::vector<uint8_t>>& values = getOrInitTagValues(tlvs, tag);
144 values.push_back(value);
147 }
while (offset <
static_cast<int>(tlvStructure.size()));
152std::vector<std::vector<uint8_t>>& BerTlvUtil::getOrInitTagValues(
153 std::map<
const int, std::vector<std::vector<uint8_t>>>& tlvs,
const int tag)
155 const auto it = tlvs.find(tag);
157 if (it == tlvs.end()) {
158 std::vector<std::vector<uint8_t>> values;
159 tlvs.insert({tag, values});
163 return tlvs.find(tag)->second;
166int BerTlvUtil::getTagSize(
const std::vector<uint8_t>& tlvStructure,
const int offset)
169 if (offset >=
static_cast<int>(tlvStructure.size())) {
173 if ((tlvStructure[offset] & 0x1F) == 0x1F) {
174 if ((tlvStructure[offset + 1] & 0x80) == 0) {
177 if ((tlvStructure[offset + 2] & 0x80) != 0) {
188int BerTlvUtil::getTag(
const std::vector<uint8_t>& tlvStructure,
const int offset,
const int size)
192 return tlvStructure[offset] & 0xFF;
194 return ((tlvStructure[offset] & 0xFF) << 8) + (tlvStructure[offset + 1] & 0xFF);
196 return ((tlvStructure[offset] & 0xFF) << 16)
197 + ((tlvStructure[offset + 1] & 0xFF) << 8)
198 + (tlvStructure[offset + 2] & 0xFF);
204int BerTlvUtil::getLengthSize(
const std::vector<uint8_t>& tlvStructure,
const int offset)
206 int firstByteLength = tlvStructure[offset] & 0xff;
208 switch (firstByteLength) {
214 if (firstByteLength >= 0x80) {
222int BerTlvUtil::getLength(
const std::vector<uint8_t>& tlvStructure,
228 return tlvStructure[offset] & 0x7F;
230 return tlvStructure[offset + 1] & 0xFF;
232 return ((tlvStructure[offset + 1] & 0xFF) << 8) + (tlvStructure[offset + 2] & 0xFF);
static const std::map< const int, const std::vector< uint8_t > > parseSimple(const std::vector< uint8_t > &tlvStructure, const bool primitiveOnly)
static bool isConstructed(const int tagId)
static const std::map< const int, std::vector< std::vector< uint8_t > > > parse(const std::vector< uint8_t > &tlvStructure, const bool primitiveOnly)
static bool addAll(std::vector< T > &a, const std::vector< T > &b)
static std::vector< char > copyOfRange(const std::vector< char > &original, const size_t from, const size_t to)