Botan 3.9.0
Crypto and TLS for C&
mem_utils.h
Go to the documentation of this file.
1/*
2* (C) 2025 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_MEM_UTILS_H_
8#define BOTAN_MEM_UTILS_H_
9
10#include <cstdint>
11#include <cstring>
12#include <span>
13#include <string>
14#include <string_view>
15#include <type_traits>
16
17namespace Botan {
18
19/**
20* Return true if any of the provided arguments are null
21*/
22template <typename... Ptrs>
23bool any_null_pointers(Ptrs... ptr) {
24 static_assert((... && std::is_pointer_v<Ptrs>), "All arguments must be pointers");
25 return (... || (ptr == nullptr));
26}
27
28inline std::span<const uint8_t> as_span_of_bytes(const char* s, size_t len) {
29 const uint8_t* b = reinterpret_cast<const uint8_t*>(s);
30 return std::span{b, len};
31}
32
33inline std::span<const uint8_t> as_span_of_bytes(const std::string& s) {
34 return as_span_of_bytes(s.data(), s.size());
35}
36
37inline std::span<const uint8_t> as_span_of_bytes(std::string_view s) {
38 return as_span_of_bytes(s.data(), s.size());
39}
40
41inline std::span<const uint8_t> cstr_as_span_of_bytes(const char* s) {
42 return as_span_of_bytes(s, std::strlen(s));
43}
44
45inline std::string bytes_to_string(std::span<const uint8_t> bytes) {
46 return std::string(reinterpret_cast<const char*>(bytes.data()), bytes.size());
47}
48
49} // namespace Botan
50
51#endif
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:28
std::string bytes_to_string(std::span< const uint8_t > bytes)
Definition mem_utils.h:45
std::span< const uint8_t > cstr_as_span_of_bytes(const char *s)
Definition mem_utils.h:41
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:23