From f090e030be53e574fecbe7cf50edfb5fdacb53e1 Mon Sep 17 00:00:00 2001 From: lloyd Date: Fri, 19 May 2006 00:07:25 +0000 Subject: Syntax changes to the BER and DER APIs to improve readability of code that uses them. These changes are not backwards compatible, this commit updates all uses of the APIs within the library. --- include/ber_dec.h | 108 +++++++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 49 deletions(-) (limited to 'include/ber_dec.h') diff --git a/include/ber_dec.h b/include/ber_dec.h index 32feb43e1..f1147e052 100644 --- a/include/ber_dec.h +++ b/include/ber_dec.h @@ -7,33 +7,28 @@ #define BOTAN_BER_DECODER_H__ #include -#include #include namespace Botan { -/************************************************* -* BER Encoded Object * -*************************************************/ -struct BER_Object - { - ASN1_Tag type_tag, class_tag; - SecureVector value; - }; - /************************************************* * BER Decoding Object * *************************************************/ class BER_Decoder { public: - bool more_items() const; - void verify_end() const; - SecureVector get_remaining(); - void discard_remaining(); BER_Object get_next_object(); void push_back(const BER_Object&); + bool more_items() const; + BER_Decoder& verify_end(); + BER_Decoder& discard_remaining(); + + BER_Decoder start_cons(ASN1_Tag); + BER_Decoder& end_cons(); + + BER_Decoder& raw_bytes(MemoryRegion&); + BER_Decoder& decode_null(); BER_Decoder& decode(bool&); BER_Decoder& decode(u32bit&); @@ -47,6 +42,17 @@ class BER_Decoder BER_Decoder& decode(MemoryRegion&, ASN1_Tag, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + BER_Decoder& decode(class ASN1_Object&); + + template + BER_Decoder& decode_optional(T&, ASN1_Tag, ASN1_Tag, const T& = T()); + + template + BER_Decoder& decode_list(std::vector&, bool = true); + + BER_Decoder& decode_optional_string(MemoryRegion&, + ASN1_Tag, u16bit); + BER_Decoder(DataSource&); BER_Decoder(const byte[], u32bit); BER_Decoder(const MemoryRegion&); @@ -54,64 +60,68 @@ class BER_Decoder ~BER_Decoder(); private: BER_Decoder& operator=(const BER_Decoder&) { return (*this); } + + BER_Decoder* parent; DataSource* source; BER_Object pushed; mutable bool owns; }; -/************************************************* -* BER Decoding Functions * -*************************************************/ -namespace BER { - -void decode(BER_Decoder&, OID&); - -BER_Decoder get_subsequence(BER_Decoder&); -BER_Decoder get_subset(BER_Decoder&); - -BER_Decoder get_subsequence(BER_Decoder&, ASN1_Tag, - ASN1_Tag = CONTEXT_SPECIFIC); -BER_Decoder get_subset(BER_Decoder&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); - -std::string to_string(const BER_Object&); -bool decode_optional_string(BER_Decoder&, MemoryRegion&, - ASN1_Tag, ASN1_Tag, ASN1_Tag); - /************************************************* * Decode an OPTIONAL or DEFAULT element * *************************************************/ -template -bool decode_optional(BER_Decoder& in, T& out, - ASN1_Tag type_tag, ASN1_Tag class_tag, - const T& default_value = T()) +template +BER_Decoder& BER_Decoder::decode_optional(T& out, + ASN1_Tag type_tag, + ASN1_Tag class_tag, + const T& default_value) { - BER_Object obj = in.get_next_object(); + BER_Object obj = get_next_object(); if(obj.type_tag == type_tag && obj.class_tag == class_tag) { if(class_tag & CONSTRUCTED) - { - BER_Decoder stored_value(obj.value); - //BER::decode(stored_value, out); - stored_value.decode(out); - stored_value.verify_end(); - } + BER_Decoder(obj.value).decode(out).verify_end(); else { - in.push_back(obj); - //BER::decode(in, out, type_tag, class_tag); - in.decode(out, type_tag, class_tag); + push_back(obj); + decode(out, type_tag, class_tag); } - return true; } else { out = default_value; - in.push_back(obj); - return false; + push_back(obj); + } + + return (*this); + } + +/************************************************* +* Decode a list of homogenously typed values * +*************************************************/ +template +BER_Decoder& BER_Decoder::decode_list(std::vector& vec, bool clear_it) + { + if(clear_it) + vec.clear(); + + while(more_items()) + { + T value; + decode(value); + vec.push_back(value); } + return (*this); } +/************************************************* +* BER Decoding Functions * +*************************************************/ +namespace BER { + +void decode(BER_Decoder&, Key_Constraints&); + } } -- cgit v1.2.3