Coverage Report

Created: 2025-07-11 06:15

/src/Botan-3.4.0/build/include/public/botan/asn1_print.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2014,2015,2017 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#ifndef BOTAN_ASN1_PRINT_H_
8
#define BOTAN_ASN1_PRINT_H_
9
10
#include <botan/asn1_obj.h>
11
#include <iosfwd>
12
#include <string>
13
#include <vector>
14
15
namespace Botan {
16
17
class BigInt;
18
class BER_Decoder;
19
20
/**
21
* Format ASN.1 data and call a virtual to format
22
*/
23
class BOTAN_PUBLIC_API(2, 4) ASN1_Formatter {
24
   public:
25
0
      virtual ~ASN1_Formatter() = default;
26
27
      /**
28
      * @param print_context_specific if true, try to parse nested context specific data.
29
      * @param max_depth do not recurse more than this many times. If zero, recursion
30
      *        is unbounded.
31
      */
32
      ASN1_Formatter(bool print_context_specific, size_t max_depth) :
33
0
            m_print_context_specific(print_context_specific), m_max_depth(max_depth) {}
34
35
      void print_to_stream(std::ostream& out, const uint8_t in[], size_t len) const;
36
37
      std::string print(const uint8_t in[], size_t len) const;
38
39
      template <typename Alloc>
40
      std::string print(const std::vector<uint8_t, Alloc>& vec) const {
41
         return print(vec.data(), vec.size());
42
      }
43
44
   protected:
45
      /**
46
      * This is called for each element
47
      */
48
      virtual std::string format(
49
         ASN1_Type type_tag, ASN1_Class class_tag, size_t level, size_t length, std::string_view value) const = 0;
50
51
      /**
52
      * This is called to format binary elements that we don't know how to
53
      * convert to a string. The result will be passed as value to format; the
54
      * tags are included as a hint to aid decoding.
55
      */
56
      virtual std::string format_bin(ASN1_Type type_tag,
57
                                     ASN1_Class class_tag,
58
                                     const std::vector<uint8_t>& vec) const = 0;
59
60
      /**
61
      * This is called to format integers
62
      */
63
      virtual std::string format_bn(const BigInt& bn) const = 0;
64
65
   private:
66
      void decode(std::ostream& output, BER_Decoder& decoder, size_t level) const;
67
68
      const bool m_print_context_specific;
69
      const size_t m_max_depth;
70
};
71
72
/**
73
* Format ASN.1 data into human readable output. The exact form of the output for
74
* any particular input is not guaranteed and may change from release to release.
75
*/
76
class BOTAN_PUBLIC_API(2, 4) ASN1_Pretty_Printer final : public ASN1_Formatter {
77
   public:
78
      /**
79
      * @param print_limit strings larger than this are not printed
80
      * @param print_binary_limit binary strings larger than this are not printed
81
      * @param print_context_specific if true, try to parse nested context specific data.
82
      * @param initial_level the initial depth (0 or 1 are the only reasonable values)
83
      * @param value_column ASN.1 values are lined up at this column in output
84
      * @param max_depth do not recurse more than this many times. If zero, recursion
85
      *        is unbounded.
86
      */
87
      ASN1_Pretty_Printer(size_t print_limit = 4096,
88
                          size_t print_binary_limit = 2048,
89
                          bool print_context_specific = true,
90
                          size_t initial_level = 0,
91
                          size_t value_column = 60,
92
                          size_t max_depth = 64) :
93
            ASN1_Formatter(print_context_specific, max_depth),
94
            m_print_limit(print_limit),
95
            m_print_binary_limit(print_binary_limit),
96
            m_initial_level(initial_level),
97
0
            m_value_column(value_column) {}
98
99
   private:
100
      std::string format(
101
         ASN1_Type type_tag, ASN1_Class class_tag, size_t level, size_t length, std::string_view value) const override;
102
103
      std::string format_bin(ASN1_Type type_tag, ASN1_Class class_tag, const std::vector<uint8_t>& vec) const override;
104
105
      std::string format_bn(const BigInt& bn) const override;
106
107
      const size_t m_print_limit;
108
      const size_t m_print_binary_limit;
109
      const size_t m_initial_level;
110
      const size_t m_value_column;
111
};
112
113
}  // namespace Botan
114
115
#endif