OpenVPN 3 Core Library
Loading...
Searching...
No Matches
bufhex.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_BUFFER_BUFHEX_H
13#define OPENVPN_BUFFER_BUFHEX_H
14
18
19namespace openvpn::BufHex {
20
22
23template <typename T>
24inline std::string render(const T obj)
25{
26 const ConstBuffer buf((const unsigned char *)&obj, sizeof(obj), true);
27 return render_hex_generic(buf);
28}
29
30template <typename T>
31inline T parse(const std::string &hex, const std::string &title)
32{
33 T obj;
34 Buffer buf((unsigned char *)&obj, sizeof(obj), false);
35 try
36 {
37 parse_hex(buf, hex);
38 }
39 catch (const BufferException &e)
40 {
41 OPENVPN_THROW(buf_hex, title << ": buffer issue: " << e.what());
42 }
43 if (buf.size() != sizeof(obj))
44 OPENVPN_THROW(buf_hex, title << ": unexpected size");
45 return obj;
46}
47
48} // namespace openvpn::BufHex
49
50#endif
report various types of exceptions or errors that may occur when working with buffers
Definition buffer.hpp:115
const char * what() const noexcept override
Definition buffer.hpp:144
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1242
#define OPENVPN_EXCEPTION(C)
#define OPENVPN_THROW(exc, stuff)
std::string render(const T obj)
Definition bufhex.hpp:24
T parse(const std::string &hex, const std::string &title)
Definition bufhex.hpp:31
std::string render_hex_generic(const V &data, const bool caps=false)
Definition hexstr.hpp:230
void parse_hex(V &dest, const std::string &str)
Definition hexstr.hpp:352