monotone

monotone Mtn Source Tree

Root/botan/ber_dec.h

1/*************************************************
2* BER Decoder Header File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#ifndef BOTAN_BER_DECODER_H__
7#define BOTAN_BER_DECODER_H__
8
9#include <botan/asn1_oid.h>
10#include <botan/bigint.h>
11#include <botan/data_src.h>
12
13namespace Botan {
14
15/*************************************************
16* BER Encoded Object *
17*************************************************/
18struct BER_Object
19 {
20 ASN1_Tag type_tag, class_tag;
21 SecureVector<byte> value;
22 };
23
24/*************************************************
25* BER Decoding Object *
26*************************************************/
27class BER_Decoder
28 {
29 public:
30 bool more_items() const;
31 void verify_end() const;
32 SecureVector<byte> get_remaining();
33 void discard_remaining();
34 BER_Object get_next_object();
35 void push_back(const BER_Object&);
36
37 BER_Decoder(DataSource&);
38 BER_Decoder(const byte[], u32bit);
39 BER_Decoder(const MemoryRegion<byte>&);
40 BER_Decoder(const BER_Decoder&);
41 ~BER_Decoder();
42 private:
43 BER_Decoder& operator=(const BER_Decoder&) { return (*this); }
44 DataSource* source;
45 BER_Object pushed;
46 mutable bool owns;
47 };
48
49/*************************************************
50* BER Decoding Functions *
51*************************************************/
52namespace BER {
53
54void decode_null(BER_Decoder&);
55void decode(BER_Decoder&, OID&);
56
57void decode(BER_Decoder&, bool&);
58void decode(BER_Decoder&, u32bit&);
59void decode(BER_Decoder&, BigInt&);
60void decode(BER_Decoder&, MemoryRegion<byte>&, ASN1_Tag);
61
62void decode(BER_Decoder&, bool&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
63void decode(BER_Decoder&, u32bit&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
64void decode(BER_Decoder&, BigInt&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
65void decode(BER_Decoder&, MemoryRegion<byte>&, ASN1_Tag,
66 ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
67
68BER_Decoder get_subsequence(BER_Decoder&);
69BER_Decoder get_subset(BER_Decoder&);
70
71BER_Decoder get_subsequence(BER_Decoder&, ASN1_Tag,
72 ASN1_Tag = CONTEXT_SPECIFIC);
73BER_Decoder get_subset(BER_Decoder&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC);
74
75std::string to_string(const BER_Object&);
76bool decode_optional_string(BER_Decoder&, MemoryRegion<byte>&,
77 ASN1_Tag, ASN1_Tag, ASN1_Tag);
78
79bool maybe_BER(DataSource&);
80
81/*************************************************
82* Decode an OPTIONAL or DEFAULT element *
83*************************************************/
84template<class T>
85bool decode_optional(BER_Decoder& in, T& out,
86 ASN1_Tag type_tag, ASN1_Tag class_tag,
87 const T& default_value = T())
88 {
89 BER_Object obj = in.get_next_object();
90
91 if(obj.type_tag == type_tag && obj.class_tag == class_tag)
92 {
93 if(class_tag & CONSTRUCTED)
94 {
95 BER_Decoder stored_value(obj.value);
96 BER::decode(stored_value, out);
97 stored_value.verify_end();
98 }
99 else
100 {
101 in.push_back(obj);
102 BER::decode(in, out, type_tag, class_tag);
103 }
104 return true;
105 }
106 else
107 {
108 out = default_value;
109 in.push_back(obj);
110 return false;
111 }
112 }
113
114}
115
116}
117
118#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status