diff options
author | Nuno Goncalves <[email protected]> | 2017-02-26 13:17:40 +0100 |
---|---|---|
committer | Nuno Goncalves <[email protected]> | 2017-03-07 18:29:27 +0100 |
commit | 65e13832de822097a140087bee47b5abeff3c78c (patch) | |
tree | 6f2d4bae8c3f2208c377fa6e317fb1904ac5e2b1 /src/lib/asn1/ber_dec.h | |
parent | 9961e475f10a671a8e25080958d26c8c356057ef (diff) |
Add generic memory type value BER decoder
Signed-off-by: Nuno Goncalves <[email protected]>
Diffstat (limited to 'src/lib/asn1/ber_dec.h')
-rw-r--r-- | src/lib/asn1/ber_dec.h | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/lib/asn1/ber_dec.h b/src/lib/asn1/ber_dec.h index 810896880..4a5aa88fc 100644 --- a/src/lib/asn1/ber_dec.h +++ b/src/lib/asn1/ber_dec.h @@ -34,6 +34,38 @@ class BOTAN_DLL BER_Decoder BER_Decoder& get_next(BER_Object& ber); + /** + * Get next object and copy value to POD type + * Asserts value length is equal to POD type sizeof. + * Asserts Type tag and optional Class tag according to parameters. + * Copy value to POD type (struct, union, C-style array, std::array, etc.). + * @param out POD type reference where to copy object value + * @param type_tag ASN1_Tag enum to assert type on object read + * @param class_tag ASN1_Tag enum to assert class on object read (default: CONTEXT_SPECIFIC) + * @return this reference + */ + template <typename T> + BER_Decoder& get_next_value(T &out, + ASN1_Tag type_tag, + ASN1_Tag class_tag = CONTEXT_SPECIFIC) + { + static_assert(std::is_pod<T>::value, "Type must be POD"); + + BER_Object obj = get_next_object(); + obj.assert_is_a(type_tag, class_tag); + + if (obj.value.size() != sizeof(T)) + throw BER_Decoding_Error( + "Size mismatch. Object value size is " + + std::to_string(obj.value.size()) + + "; Output type size is " + + std::to_string(sizeof(T))); + + copy_mem((uint8_t *)&out, obj.value.data(), obj.value.size()); + + return (*this); + } + BER_Decoder& raw_bytes(secure_vector<uint8_t>& v); BER_Decoder& raw_bytes(std::vector<uint8_t>& v); |