Botan: src/asn1/ber_dec.h Source File

Botan 1.10.16

ber_dec.h
Go to the documentation of this file.
1 /*
2 * BER Decoder
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #ifndef BOTAN_BER_DECODER_H__
9 #define BOTAN_BER_DECODER_H__
10
11 #include <botan/asn1_oid.h>
12 #include <botan/data_src.h>
13
14 namespace Botan {
15
16 /**
17 * BER Decoding Object
18 */
19 class BOTAN_DLL BER_Decoder
20 {
21 public:
22 BER_Object get_next_object();
23 void push_back(const BER_Object&);
24
25 bool more_items() const;
26 BER_Decoder& verify_end();
27 BER_Decoder& discard_remaining();
28
29 BER_Decoder start_cons(ASN1_Tag, ASN1_Tag = UNIVERSAL);
30 BER_Decoder& end_cons();
31
32 BER_Decoder& raw_bytes(MemoryRegion<byte>&);
33
34 BER_Decoder& decode_null();
35 BER_Decoder& decode(bool&);
36 BER_Decoder& decode(size_t&);
37 BER_Decoder& decode(class BigInt&);
38 BER_Decoder& decode(MemoryRegion<byte>&, ASN1_Tag);
39
40 BER_Decoder& decode(bool&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
41 BER_Decoder& decode(size_t&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
42 BER_Decoder& decode(class BigInt&,
43 ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
44 BER_Decoder& decode(MemoryRegion<byte>&, ASN1_Tag,
45 ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
46
47 BER_Decoder& decode(class ASN1_Object&);
48
49 BER_Decoder& decode_octet_string_bigint(class BigInt&);
50
51 template<typename T>
52 BER_Decoder& decode_optional(T& out,
53 ASN1_Tag type_tag,
54 ASN1_Tag class_tag,
55 const T& default_value = T());
56
57 template<typename T>
58 BER_Decoder& decode_list(std::vector<T>& out,
59 bool clear_out = true);
60
61 template<typename T>
62 BER_Decoder& decode_and_check(const T& expected,
63 const std::string& error_msg)
64 {
65 T actual;
66 decode(actual);
67
68 if(actual != expected)
69 throw Decoding_Error(error_msg);
70
71 return (*this);
72 }
73
74 BER_Decoder& decode_optional_string(MemoryRegion<byte>&,
75 ASN1_Tag, u16bit);
76
77 BER_Decoder(DataSource&);
78 BER_Decoder(const byte[], size_t);
79 BER_Decoder(const MemoryRegion<byte>&);
80 BER_Decoder(const BER_Decoder&);
81 ~BER_Decoder();
82 private:
83 BER_Decoder& operator=(const BER_Decoder&) { return (*this); }
84
85 BER_Decoder* parent;
86 DataSource* source;
87 BER_Object pushed;
88 mutable bool owns;
89 };
90
91 /*
92 * Decode an OPTIONAL or DEFAULT element
93 */
94 template<typename T>
95 BER_Decoder& BER_Decoder::decode_optional(T& out,
96 ASN1_Tag type_tag,
97 ASN1_Tag class_tag,
98 const T& default_value)
99 {
100 BER_Object obj = get_next_object();
101
102 if(obj.type_tag == type_tag && obj.class_tag == class_tag)
103 {
104 if(class_tag & CONSTRUCTED)
105 BER_Decoder(obj.value).decode(out).verify_end();
106 else
107 {
108 push_back(obj);
109 decode(out, type_tag, class_tag);
110 }
111 }
112 else
113 {
114 out = default_value;
115 push_back(obj);
116 }
117
118 return (*this);
119 }
120
121 /*
122 * Decode a list of homogenously typed values
123 */
124 template<typename T>
125 BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec, bool clear_it)
126 {
127 if(clear_it)
128 vec.clear();
129
130 while(more_items())
131 {
132 T value;
133 decode(value);
134 vec.push_back(value);
135 }
136 return (*this);
137 }
138
139 }
140
141 #endif
Botan::CONTEXT_SPECIFIC
Definition: asn1_int.h:22
Botan::ASN1_Object
Definition: asn1_int.h:56
Botan::BigInt
Definition: bigint.h:22
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
Botan::BER_Decoder::decode_and_check
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition: ber_dec.h:62
Botan::DataSource
Definition: data_src.h:20
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::PEM_Code::decode
SecureVector< byte > decode(DataSource &source, std::string &label)
Definition: pem.cpp:56
Botan::BER_Decoder::decode_list
BER_Decoder & decode_list(std::vector< T > &out, bool clear_out=true)
Definition: ber_dec.h:125
Botan::BER_Decoder::decode_optional
BER_Decoder & decode_optional(T &out, ASN1_Tag type_tag, ASN1_Tag class_tag, const T &default_value=T())
Definition: ber_dec.h:95
Botan::CONSTRUCTED
Definition: asn1_int.h:25
Botan::ASN1_Tag
ASN1_Tag
Definition: asn1_int.h:19
Botan::u16bit
unsigned short u16bit
Definition: types.h:27
Botan::Decoding_Error
Definition: exceptn.h:138
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::BER_Object::value
SecureVector< byte > value
Definition: asn1_int.h:83
Botan::BER_Object::class_tag
ASN1_Tag class_tag
Definition: asn1_int.h:82
Botan::BER_Object::type_tag
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::verify_end
BER_Decoder & verify_end()
Definition: ber_dec.cpp:163
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::BER_Object
Definition: asn1_int.h:77