Botan  1.10.9
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&);
39 
42  BER_Decoder& decode(class BigInt&,
46 
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 
78  BER_Decoder(const byte[], size_t);
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>
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
BER_Decoder(DataSource &)
Definition: ber_dec.cpp:258
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition: ber_dec.h:62
unsigned char byte
Definition: types.h:22
SecureVector< byte > decode(DataSource &source, std::string &label)
Definition: pem.cpp:56
BER_Decoder & decode_list(std::vector< T > &out, bool clear_out=true)
Definition: ber_dec.h:125
BER_Decoder & decode_optional(T &out, ASN1_Tag type_tag, ASN1_Tag class_tag, const T &default_value=T())
Definition: ber_dec.h:95
bool more_items() const
Definition: ber_dec.cpp:150
void push_back(const BER_Object &)
Definition: ber_dec.cpp:222
ASN1_Tag
Definition: asn1_int.h:19
unsigned short u16bit
Definition: types.h:27
SecureVector< byte > value
Definition: asn1_int.h:83
BER_Object get_next_object()
Definition: ber_dec.cpp:193
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82