Botan 1.10.17
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
14namespace Botan {
15
16/**
17* BER Decoding Object
18*/
19class BOTAN_DLL BER_Decoder
20 {
21 public:
23 void push_back(const BER_Object&);
24
25 bool more_items() const;
28
31
33
35 BER_Decoder& decode(bool&);
36 BER_Decoder& decode(size_t&);
37 BER_Decoder& decode(class BigInt&);
39
46
48
50
51 template<typename T>
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>&,
76
78 BER_Decoder(const byte[], size_t);
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*/
94template<typename T>
96 ASN1_Tag type_tag,
97 ASN1_Tag class_tag,
98 const T& default_value)
99 {
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*/
124template<typename T>
125BER_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_Object get_next_object()
Definition ber_dec.cpp:196
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition ber_dec.cpp:238
bool more_items() const
Definition ber_dec.cpp:153
BER_Decoder & raw_bytes(MemoryRegion< byte > &)
Definition ber_dec.cpp:173
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition ber_dec.h:62
BER_Decoder & decode(bool &)
Definition ber_dec.cpp:344
BER_Decoder & decode_optional(T &out, ASN1_Tag type_tag, ASN1_Tag class_tag, const T &default_value=T())
Definition ber_dec.h:95
BER_Decoder & verify_end()
Definition ber_dec.cpp:163
BER_Decoder & end_cons()
Definition ber_dec.cpp:252
BER_Decoder & decode_list(std::vector< T > &out, bool clear_out=true)
Definition ber_dec.h:125
BER_Decoder & decode_octet_string_bigint(class BigInt &)
Definition ber_dec.cpp:365
BER_Decoder & discard_remaining()
Definition ber_dec.cpp:185
BER_Decoder & decode_null()
Definition ber_dec.cpp:332
void push_back(const BER_Object &)
Definition ber_dec.cpp:228
BER_Decoder(DataSource &)
Definition ber_dec.cpp:264
SecureVector< byte > value
Definition asn1_int.h:83
ASN1_Tag class_tag
Definition asn1_int.h:82
ASN1_Tag type_tag
Definition asn1_int.h:82
ASN1_Tag
Definition asn1_int.h:19
@ CONSTRUCTED
Definition asn1_int.h:25
@ CONTEXT_SPECIFIC
Definition asn1_int.h:22
@ UNIVERSAL
Definition asn1_int.h:20
unsigned short u16bit
Definition types.h:27