tlx
base64.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/base64.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2017 Timo Bingmann <tb@panthema.net>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_STRING_BASE64_HEADER
12 #define TLX_STRING_BASE64_HEADER
13 
14 #include <string>
15 
16 namespace tlx {
17 
18 //! \addtogroup tlx_string
19 //! \{
20 //! \name Base64 Encoding and Decoding
21 //! \{
22 
23 /******************************************************************************/
24 // Base64 Encoding and Decoding
25 
26 /*!
27  * Encode the given binary data into base64 representation as described in RFC
28  * 2045 or RFC 3548. The output string contains only characters [A-Za-z0-9+/]
29  * and is roughly 33% longer than the input. The output string can be broken
30  * into lines after n characters, where n must be a multiple of 4.
31  *
32  * \param data input data to encode
33  * \param size size of input data to encode
34  * \param line_break break the output string every n characters
35  * \return base64 encoded string
36  */
37 std::string base64_encode(const void* data, size_t size, size_t line_break = 0);
38 
39 /*!
40  * Encode the given binary string into base64 representation as described in RFC
41  * 2045 or RFC 3548. The output string contains only characters [A-Za-z0-9+/]
42  * and is roughly 33% longer than the input. The output string can be broken
43  * into lines after n characters, where n must be a multiple of 4.
44  *
45  * \param str input string to encode
46  * \param line_break break the output string every n characters
47  * \return base64 encoded string
48  */
49 std::string base64_encode(const std::string& str, size_t line_break = 0);
50 
51 /*!
52  * Decode a string in base64 representation as described in RFC 2045 or RFC 3548
53  * and return the original data. If a non-whitespace invalid base64 character is
54  * encountered _and_ the parameter "strict" is true, then this function will
55  * throw a std::runtime_error. If "strict" is false, the character is silently
56  * ignored.
57  *
58  * \param data input data to decode
59  * \param size size of input data to decode
60  * \param strict throw exception on invalid character
61  * \return decoded binary data
62  */
63 std::string base64_decode(const void* data, size_t size, bool strict = true);
64 
65 /*!
66  * Decode a string in base64 representation as described in RFC 2045 or RFC 3548
67  * and return the original data. If a non-whitespace invalid base64 character is
68  * encountered _and_ the parameter "strict" is true, then this function will
69  * throw a std::runtime_error. If "strict" is false, the character is silently
70  * ignored.
71  *
72  * \param str input string to encode
73  * \param strict throw exception on invalid character
74  * \return decoded binary data
75  */
76 std::string base64_decode(const std::string& str, bool strict = true);
77 
78 //! \}
79 //! \}
80 
81 } // namespace tlx
82 
83 #endif // !TLX_STRING_BASE64_HEADER
84 
85 /******************************************************************************/
std::string base64_decode(const void *data, size_t size, bool strict)
Decode a string in base64 representation as described in RFC 2045 or RFC 3548 and return the original...
Definition: base64.cpp:108
std::string base64_encode(const void *data, size_t size, size_t line_break)
Encode the given binary data into base64 representation as described in RFC 2045 or RFC 3548...
Definition: base64.cpp:25