diff options
author | Jack Lloyd <[email protected]> | 2017-03-22 11:11:55 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-03-22 11:11:55 -0400 |
commit | 8f359f8bdc10eb8642883606eb4d166b87de51ca (patch) | |
tree | 30f3b62af1ab0b9636c4f267db26a8cb2244c94f | |
parent | 131e32146811fc553220681c132b045e95e02877 (diff) | |
parent | 65e13832de822097a140087bee47b5abeff3c78c (diff) |
Merge GH #897 Add generic memory type BER decoder
-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); |