Skip to content

Commit 969bec7

Browse files
committed
* Add Aes128Key::from_hex method.
* Remove commented out variables from `hdlc_decoder.cpp`
1 parent 4468df3 commit 969bec7

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/dlms_parser/decryption/aes_128_gcm_decryptor.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#pragma once
22

3+
#include <charconv>
34
#include <cstdint>
45
#include <cstring>
56
#include <optional>
67
#include <array>
78
#include <span>
9+
#include <string_view>
810

911
namespace dlms_parser {
1012

@@ -23,6 +25,16 @@ class Aes128Key final {
2325
return Aes128Key(arr);
2426
}
2527

28+
[[nodiscard]] static std::optional<Aes128Key> from_hex(std::string_view hex) {
29+
if (hex.size() != 32) return std::nullopt;
30+
std::array<uint8_t, 16> arr{};
31+
for (size_t i = 0; i < 16; ++i) {
32+
auto [ptr, ec] = std::from_chars(hex.data() + i * 2, hex.data() + i * 2 + 2, arr[i], 16);
33+
if (ec != std::errc{}) return std::nullopt;
34+
}
35+
return Aes128Key(arr);
36+
}
37+
2638
[[nodiscard]] const uint8_t* data() const { return key.data(); }
2739

2840
private:

src/dlms_parser/hdlc_decoder.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ static size_t address_length(std::span<const uint8_t> p) {
5959
return 0;
6060
}
6161

62-
static constexpr uint8_t HDLC_FLAG = 0x7E;
63-
// static constexpr uint8_t HDLC_ESCAPE = 0x7D; // byte-stuffing disabled, see decode_one_()
64-
// static constexpr uint8_t HDLC_ESCAPE_XOR = 0x20;
65-
static constexpr uint8_t HDLC_SEG_BIT = 0x08; // bit 3 of frame-type byte: "more frames follow"
62+
static constexpr uint8_t HDLC_FLAG = 0x7E;
63+
static constexpr uint8_t HDLC_SEG_BIT = 0x08; // bit 3 of frame-type byte: "more frames follow"
6664

6765
// ---------------------------------------------------------------------------
6866
// check() — stateless frame completeness check

0 commit comments

Comments
 (0)