• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13724401033

07 Mar 2025 12:55PM UTC coverage: 91.669%. Remained the same
13724401033

push

github

web-flow
Merge pull request #4754 from randombit/jack/compare-perf-buf-size

Extend compare_perf script to properly handle byte oriented operations

95874 of 104587 relevant lines covered (91.67%)

11251001.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.5
/src/lib/asn1/asn1_obj.cpp
1
/*
2
* ASN.1 Internals
3
* (C) 1999-2007,2018 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/asn1_obj.h>
9

10
#include <botan/data_src.h>
11
#include <botan/der_enc.h>
12
#include <botan/mem_ops.h>
13
#include <botan/internal/fmt.h>
14
#include <botan/internal/stl_util.h>
15
#include <sstream>
16

17
namespace Botan {
18

19
std::vector<uint8_t> ASN1_Object::BER_encode() const {
26,036✔
20
   std::vector<uint8_t> output;
26,036✔
21
   DER_Encoder der(output);
26,036✔
22
   this->encode_into(der);
26,036✔
23
   return output;
26,036✔
24
}
26,036✔
25

26
/*
27
* Check a type invariant on BER data
28
*/
29
void BER_Object::assert_is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag, std::string_view descr) const {
1,097,943✔
30
   if(this->is_a(expected_type_tag, expected_class_tag) == false) {
1,097,943✔
31
      std::stringstream msg;
3,386✔
32

33
      msg << "Tag mismatch when decoding " << descr << " got ";
3,386✔
34

35
      if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject) {
3,386✔
36
         msg << "EOF";
1,084✔
37
      } else {
38
         if(m_class_tag == ASN1_Class::Universal || m_class_tag == ASN1_Class::Constructed) {
2,302✔
39
            msg << asn1_tag_to_string(m_type_tag);
3,616✔
40
         } else {
41
            msg << std::to_string(static_cast<uint32_t>(m_type_tag));
494✔
42
         }
43

44
         msg << "/" << asn1_class_to_string(m_class_tag);
4,604✔
45
      }
46

47
      msg << " expected ";
3,386✔
48

49
      if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed) {
3,386✔
50
         msg << asn1_tag_to_string(expected_type_tag);
6,684✔
51
      } else {
52
         msg << std::to_string(static_cast<uint32_t>(expected_type_tag));
44✔
53
      }
54

55
      msg << "/" << asn1_class_to_string(expected_class_tag);
6,772✔
56

57
      throw BER_Decoding_Error(msg.str());
6,772✔
58
   }
3,386✔
59
}
1,094,557✔
60

61
bool BER_Object::is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag) const {
1,372,826✔
62
   return (m_type_tag == expected_type_tag && m_class_tag == expected_class_tag);
1,372,826✔
63
}
64

65
bool BER_Object::is_a(int expected_type_tag, ASN1_Class expected_class_tag) const {
89,200✔
66
   return is_a(ASN1_Type(expected_type_tag), expected_class_tag);
89,200✔
67
}
68

69
void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag) {
6,863,215✔
70
   m_type_tag = type_tag;
6,863,215✔
71
   m_class_tag = class_tag;
6,863,215✔
72
}
6,863,215✔
73

74
std::string asn1_class_to_string(ASN1_Class type) {
5,688✔
75
   switch(type) {
5,688✔
76
      case ASN1_Class::Universal:
2,536✔
77
         return "UNIVERSAL";
2,536✔
78
      case ASN1_Class::Constructed:
2,614✔
79
         return "CONSTRUCTED";
2,614✔
80
      case ASN1_Class::ContextSpecific:
122✔
81
         return "CONTEXT_SPECIFIC";
122✔
82
      case ASN1_Class::Application:
44✔
83
         return "APPLICATION";
44✔
84
      case ASN1_Class::Private:
40✔
85
         return "PRIVATE";
40✔
86
      case ASN1_Class::NoObject:
×
87
         return "NO_OBJECT";
×
88
      default:
332✔
89
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
996✔
90
   }
91
}
92

93
std::string asn1_tag_to_string(ASN1_Type type) {
5,977✔
94
   switch(type) {
5,977✔
95
      case ASN1_Type::Sequence:
2,166✔
96
         return "SEQUENCE";
2,166✔
97

98
      case ASN1_Type::Set:
442✔
99
         return "SET";
442✔
100

101
      case ASN1_Type::PrintableString:
61✔
102
         return "PRINTABLE STRING";
61✔
103

104
      case ASN1_Type::NumericString:
20✔
105
         return "NUMERIC STRING";
20✔
106

107
      case ASN1_Type::Ia5String:
14✔
108
         return "IA5 STRING";
14✔
109

110
      case ASN1_Type::TeletexString:
8✔
111
         return "T61 STRING";
8✔
112

113
      case ASN1_Type::Utf8String:
62✔
114
         return "UTF8 STRING";
62✔
115

116
      case ASN1_Type::VisibleString:
8✔
117
         return "VISIBLE STRING";
8✔
118

119
      case ASN1_Type::BmpString:
1✔
120
         return "BMP STRING";
1✔
121

122
      case ASN1_Type::UniversalString:
×
123
         return "UNIVERSAL STRING";
×
124

125
      case ASN1_Type::UtcTime:
141✔
126
         return "UTC TIME";
141✔
127

128
      case ASN1_Type::GeneralizedTime:
15✔
129
         return "GENERALIZED TIME";
15✔
130

131
      case ASN1_Type::OctetString:
321✔
132
         return "OCTET STRING";
321✔
133

134
      case ASN1_Type::BitString:
184✔
135
         return "BIT STRING";
184✔
136

137
      case ASN1_Type::Enumerated:
51✔
138
         return "ENUMERATED";
51✔
139

140
      case ASN1_Type::Integer:
1,637✔
141
         return "INTEGER";
1,637✔
142

143
      case ASN1_Type::Null:
128✔
144
         return "NULL";
128✔
145

146
      case ASN1_Type::ObjectId:
185✔
147
         return "OBJECT";
185✔
148

149
      case ASN1_Type::Boolean:
86✔
150
         return "BOOLEAN";
86✔
151

152
      case ASN1_Type::NoObject:
1✔
153
         return "NO_OBJECT";
1✔
154

155
      default:
446✔
156
         return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
1,338✔
157
   }
158
}
159

160
/*
161
* BER Decoding Exceptions
162
*/
163
BER_Decoding_Error::BER_Decoding_Error(std::string_view str) : Decoding_Error(fmt("BER: {}", str)) {}
22,870✔
164

165
BER_Bad_Tag::BER_Bad_Tag(std::string_view str, uint32_t tagging) : BER_Decoding_Error(fmt("{}: {}", str, tagging)) {}
3,952✔
166

167
namespace ASN1 {
168

169
/*
170
* Put some arbitrary bytes into a SEQUENCE
171
*/
172
std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents) {
73,901✔
173
   return ASN1::put_in_sequence(contents.data(), contents.size());
73,901✔
174
}
175

176
std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len) {
74,121✔
177
   std::vector<uint8_t> output;
74,121✔
178
   DER_Encoder(output).start_sequence().raw_bytes(bits, len).end_cons();
148,242✔
179
   return output;
74,121✔
180
}
×
181

182
/*
183
* Convert a BER object into a string object
184
*/
185
std::string to_string(const BER_Object& obj) {
203,613✔
186
   return std::string(cast_uint8_ptr_to_char(obj.bits()), obj.length());
203,613✔
187
}
188

189
/*
190
* Do heuristic tests for BER data
191
*/
192
bool maybe_BER(DataSource& source) {
51,980✔
193
   uint8_t first_u8;
51,980✔
194
   if(!source.peek_byte(first_u8)) {
51,980✔
195
      BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
204✔
196
      throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
204✔
197
   }
198

199
   const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
51,776✔
200
   if(first_u8 == cons_seq) {
51,776✔
201
      return true;
43,244✔
202
   }
203
   return false;
204
}
205

206
}  // namespace ASN1
207

208
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc