Botan 3.13.0
Crypto and TLS for C&
Botan::SCAN_Name Class Referencefinal

#include <scan_name.h>

Public Member Functions

const std::string & algo_name () const
std::string arg (size_t i) const
std::string arg (size_t i, std::string_view def_value) const
size_t arg_as_integer (size_t i) const
size_t arg_as_integer (size_t i, size_t def_value) const
size_t arg_count () const
bool arg_count_between (size_t lower, size_t upper) const
std::string cipher_mode () const
std::string cipher_mode_pad () const
 SCAN_Name (std::string_view algo_spec)
const std::string & to_string () const

Detailed Description

A class encapsulating a SCAN name (similar to JCE conventions) http://www.users.zetnet.co.uk/hopwood/crypto/scan/

Definition at line 22 of file scan_name.h.

Constructor & Destructor Documentation

◆ SCAN_Name()

Botan::SCAN_Name::SCAN_Name ( std::string_view algo_spec)
explicit

Create a SCAN_Name

Parameters
algo_specA SCAN-format name

Definition at line 61 of file scan_name.cpp.

61 : m_orig_algo_spec(algo_spec) {
62 if(algo_spec.empty()) {
63 throw Invalid_Argument("Expected algorithm name, got empty string");
64 }
65
66 // Fast path for a bare name with no arguments or modes (eg "SHA-256"),
67 // which is the common case. Equivalent to the general parse below, which
68 // for such input produces a single token and no args/modes.
69 if(algo_spec.find_first_of("(),/") == std::string_view::npos) {
70 m_alg_name = std::string(algo_spec);
71 return;
72 }
73
74 std::vector<std::pair<size_t, std::string>> name;
75 size_t level = 0;
76 std::pair<size_t, std::string> accum = std::make_pair(level, "");
77
78 bool expect_token = true;
79
80 for(const char c : algo_spec) {
81 if(c == '/' || c == ',' || c == '(' || c == ')') {
82 if(c == '(') {
83 ++level;
84 } else if(c == ')') {
85 if(level == 0) {
86 throw Invalid_Algorithm_Name(m_orig_algo_spec);
87 }
88 --level;
89 }
90
91 if(c == '/' && level > 0) {
92 accum.second.push_back(c);
93 expect_token = false;
94 } else {
95 if(expect_token) {
96 throw Invalid_Algorithm_Name(m_orig_algo_spec);
97 }
98 if(!accum.second.empty()) {
99 name.push_back(accum);
100 }
101 accum = std::make_pair(level, "");
102 expect_token = (c != ')');
103 }
104 } else {
105 accum.second.push_back(c);
106 expect_token = false;
107 }
108 }
109
110 if(!accum.second.empty()) {
111 name.push_back(accum);
112 }
113
114 if(level != 0) {
115 throw Invalid_Algorithm_Name(m_orig_algo_spec);
116 }
117
118 if(expect_token) {
119 // A trailing separator with no following token, eg "Foo/" or "Foo,"
120 throw Invalid_Algorithm_Name(m_orig_algo_spec);
121 }
122
123 if(name.empty()) {
124 throw Invalid_Algorithm_Name(m_orig_algo_spec);
125 }
126
127 m_alg_name = name[0].second;
128
129 bool in_modes = false;
130
131 for(size_t i = 1; i != name.size(); ++i) {
132 if(name[i].first == 0) {
133 m_mode_info.push_back(make_arg(name, i));
134 in_modes = true;
135 } else if(name[i].first == 1 && !in_modes) {
136 m_args.push_back(make_arg(name, i));
137 }
138 }
139}

Member Function Documentation

◆ algo_name()

◆ arg() [1/2]

◆ arg() [2/2]

std::string Botan::SCAN_Name::arg ( size_t i,
std::string_view def_value ) const
Parameters
iwhich argument
def_valuethe default value
Returns
ith argument or the default value

Definition at line 148 of file scan_name.cpp.

148 {
149 if(i >= arg_count()) {
150 return std::string(def_value);
151 }
152 return m_args[i];
153}

References arg(), and arg_count().

◆ arg_as_integer() [1/2]

size_t Botan::SCAN_Name::arg_as_integer ( size_t i) const
Parameters
iwhich argument
Returns
ith argument as an integer

Definition at line 162 of file scan_name.cpp.

162 {
163 return to_u32bit(arg(i));
164}
std::string arg(size_t i) const
uint32_t to_u32bit(std::string_view input)
Definition parsing.cpp:76

References arg(), arg_as_integer(), and Botan::to_u32bit().

◆ arg_as_integer() [2/2]

size_t Botan::SCAN_Name::arg_as_integer ( size_t i,
size_t def_value ) const

◆ arg_count()

◆ arg_count_between()

bool Botan::SCAN_Name::arg_count_between ( size_t lower,
size_t upper ) const
inline
Parameters
loweris the lower bound
upperis the upper bound
Returns
if the number of arguments is between lower and upper

Definition at line 50 of file scan_name.h.

50 {
51 return ((arg_count() >= lower) && (arg_count() <= upper));
52 }

References arg_count().

Referenced by Botan::BlockCipher::create(), Botan::KDF::create(), Botan::SignaturePaddingScheme::create(), and Botan::StreamCipher::create().

◆ cipher_mode()

std::string Botan::SCAN_Name::cipher_mode ( ) const
inline
Returns
cipher mode (if any)

Definition at line 83 of file scan_name.h.

83{ return (!m_mode_info.empty()) ? m_mode_info[0] : ""; }

Referenced by Botan::commoncrypto_opts_from_algo().

◆ cipher_mode_pad()

std::string Botan::SCAN_Name::cipher_mode_pad ( ) const
inline
Returns
cipher mode padding (if any)

Definition at line 88 of file scan_name.h.

88{ return (m_mode_info.size() >= 2) ? m_mode_info[1] : ""; }

Referenced by Botan::commoncrypto_opts_from_algo().

◆ to_string()

const std::string & Botan::SCAN_Name::to_string ( ) const
inline
Returns
original input string

Definition at line 33 of file scan_name.h.

33{ return m_orig_algo_spec; }

Referenced by arg().


The documentation for this class was generated from the following files: