Botan 3.14.1
Crypto and TLS for C&
telephone_number.cpp
Go to the documentation of this file.
1/*
2* (C) 2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/telephone_number.h>
8
9#include <botan/assert.h>
10
11namespace Botan {
12
13std::optional<TelephoneNumber> TelephoneNumber::from_string(std::string_view tn) {
14 // TelephoneNumber ::= IA5String (SIZE (1..15)) (FROM ("0123456789#*"))
15 if(tn.empty() || tn.size() > 15 || tn.find_first_not_of("0123456789#*") != std::string_view::npos) {
16 return std::nullopt;
17 }
18 return TelephoneNumber(std::string(tn));
19}
20
22 return m_number.find_first_of("*#") != std::string::npos;
23}
24
25std::optional<uint64_t> TelephoneNumber::numeric_value() const {
26 if(is_wildcard()) {
27 return std::nullopt;
28 }
29 uint64_t value = 0;
30 for(const char c : m_number) {
31 value = (value * 10) + static_cast<uint64_t>(c - '0');
32 }
33 return value;
34}
35
36/*
37* RFC 8226 Section 9:
38* "The count field is only applicable to start fields whose values do not
39* include "*" or "#" (i.e., a TelephoneNumber that does not include "*" or
40* "#"). count MUST NOT make the number increase in length (i.e., a
41* TelephoneNumberRange with TelephoneNumber=10 and count=91 is invalid);
42* formally, given the inputs count and TelephoneNumber of length D,
43* TelephoneNumber + count MUST be less than 10^D."
44*
45* The formal statement is off by one relative to its own example, so this
46* checks that the last number in the range (start + count - 1) has D digits.
47*/
48std::optional<TelephoneNumberRange> TelephoneNumberRange::from(const TelephoneNumber& start, uint64_t count) {
49 const auto pow10 = [](size_t n) -> uint64_t {
50 // Always in range since the input length is at most 15
51 uint64_t v = 1;
52 for(size_t i = 0; i != n; ++i) {
53 v *= 10;
54 }
55 return v;
56 };
57
58 const auto first = start.numeric_value();
59 if(!first.has_value() || count <= 1) {
60 return std::nullopt;
61 }
62 if(count > pow10(start.length()) - *first) {
63 return std::nullopt;
64 }
65 return TelephoneNumberRange(start, count);
66}
67
69 uint64_t value = m_start.numeric_value().value() + m_count - 1;
70 std::string digits(m_start.length(), '0');
71 for(size_t i = digits.size(); i > 0; --i) {
72 digits[i - 1] = static_cast<char>('0' + (value % 10));
73 value /= 10;
74 }
75 auto tn = TelephoneNumber::from_string(digits);
76 BOTAN_ASSERT_NOMSG(tn.has_value());
77 return *tn;
78}
79
81 const auto value = tn.numeric_value();
82 if(!value.has_value() || tn.length() != m_start.length()) {
83 return false;
84 }
85 const uint64_t first = m_start.numeric_value().value();
86 return *value >= first && (*value - first) < m_count;
87}
88
89bool TelephoneNumberRange::contains(const TelephoneNumberRange& other) const {
90 if(other.m_start.length() != m_start.length()) {
91 return false;
92 }
93 const uint64_t first = m_start.numeric_value().value();
94 const uint64_t other_first = other.m_start.numeric_value().value();
95 return other_first >= first && (other_first - first) + other.m_count <= m_count;
96}
97
98} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
static std::optional< TelephoneNumberRange > from(const TelephoneNumber &start, uint64_t count)
bool contains(const TelephoneNumber &tn) const
const TelephoneNumber & start() const
TelephoneNumber last() const
std::optional< uint64_t > numeric_value() const
static std::optional< TelephoneNumber > from_string(std::string_view tn)