00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BOTAN_BER_DECODER_H__
00009 #define BOTAN_BER_DECODER_H__
00010
00011 #include <botan/asn1_oid.h>
00012 #include <botan/data_src.h>
00013
00014 namespace Botan {
00015
00016
00017
00018
00019 class BOTAN_DLL BER_Decoder
00020 {
00021 public:
00022 BER_Object get_next_object();
00023 void push_back(const BER_Object&);
00024
00025 bool more_items() const;
00026 BER_Decoder& verify_end();
00027 BER_Decoder& discard_remaining();
00028
00029 BER_Decoder start_cons(ASN1_Tag, ASN1_Tag = UNIVERSAL);
00030 BER_Decoder& end_cons();
00031
00032 BER_Decoder& raw_bytes(MemoryRegion<byte>&);
00033
00034 BER_Decoder& decode_null();
00035 BER_Decoder& decode(bool&);
00036 BER_Decoder& decode(u32bit&);
00037 BER_Decoder& decode(class BigInt&);
00038 BER_Decoder& decode(MemoryRegion<byte>&, ASN1_Tag);
00039
00040 BER_Decoder& decode(bool&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
00041 BER_Decoder& decode(u32bit&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
00042 BER_Decoder& decode(class BigInt&,
00043 ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
00044 BER_Decoder& decode(MemoryRegion<byte>&, ASN1_Tag,
00045 ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
00046
00047 BER_Decoder& decode(class ASN1_Object&);
00048
00049 BER_Decoder& decode_octet_string_bigint(class BigInt&);
00050
00051 template<typename T>
00052 BER_Decoder& decode_optional(T& out,
00053 ASN1_Tag type_tag,
00054 ASN1_Tag class_tag,
00055 const T& default_value = T());
00056
00057 template<typename T>
00058 BER_Decoder& decode_list(std::vector<T>& out,
00059 bool clear_out = true);
00060
00061 template<typename T>
00062 BER_Decoder& decode_and_check(const T& expected,
00063 const std::string& error_msg)
00064 {
00065 T actual;
00066 decode(actual);
00067
00068 if(actual != expected)
00069 throw Decoding_Error(error_msg);
00070
00071 return (*this);
00072 }
00073
00074 BER_Decoder& decode_optional_string(MemoryRegion<byte>&,
00075 ASN1_Tag, u16bit);
00076
00077 BER_Decoder(DataSource&);
00078 BER_Decoder(const byte[], u32bit);
00079 BER_Decoder(const MemoryRegion<byte>&);
00080 BER_Decoder(const BER_Decoder&);
00081 ~BER_Decoder();
00082 private:
00083 BER_Decoder& operator=(const BER_Decoder&) { return (*this); }
00084
00085 BER_Decoder* parent;
00086 DataSource* source;
00087 BER_Object pushed;
00088 mutable bool owns;
00089 };
00090
00091
00092
00093
00094 template<typename T>
00095 BER_Decoder& BER_Decoder::decode_optional(T& out,
00096 ASN1_Tag type_tag,
00097 ASN1_Tag class_tag,
00098 const T& default_value)
00099 {
00100 BER_Object obj = get_next_object();
00101
00102 if(obj.type_tag == type_tag && obj.class_tag == class_tag)
00103 {
00104 if(class_tag & CONSTRUCTED)
00105 BER_Decoder(obj.value).decode(out).verify_end();
00106 else
00107 {
00108 push_back(obj);
00109 decode(out, type_tag, class_tag);
00110 }
00111 }
00112 else
00113 {
00114 out = default_value;
00115 push_back(obj);
00116 }
00117
00118 return (*this);
00119 }
00120
00121
00122
00123
00124 template<typename T>
00125 BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec, bool clear_it)
00126 {
00127 if(clear_it)
00128 vec.clear();
00129
00130 while(more_items())
00131 {
00132 T value;
00133 decode(value);
00134 vec.push_back(value);
00135 }
00136 return (*this);
00137 }
00138
00139 }
00140
00141 #endif