E-MailRelay

gbase64.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gbase64.h
19///
20
21#ifndef G_BASE64_H
22#define G_BASE64_H
23
24#include "gdef.h"
25#include "gexception.h"
26#include "gstringview.h"
27#include <string>
28
29 namespace G
30{
31 class Base64 ;
32}
33
34//| \class G::Base64
35/// A base64 codec class.
36/// \see RFC-1341 section 5.2
37///
38 class G::Base64
39{
40 public:
41 G_EXCEPTION( Error , tx("base64 encoding error") )
42
43 static std::string encode( std::string_view , std::string_view line_break = {} ) ;
44 ///< Encodes the given string, optionally inserting line-breaks
45 ///< to limit the line length.
46
47 static std::string decode( std::string_view , bool throw_on_invalid = false , bool strict = true ) ;
48 ///< Decodes the given string. Either throws an exception if
49 ///< not a valid() encoding, or returns the empty string.
50
51 static bool valid( std::string_view , bool strict = true ) ;
52 ///< Returns true if the string is a valid base64 encoding,
53 ///< possibly allowing for embedded newlines, carriage returns,
54 ///< and space characters. Strict checking permits one or
55 ///< two pad characters at the end to make a multiple of
56 ///< four characters in total, but no newlines, carriage
57 ///< returns or other odd characters. Empty strings
58 ///< are valid; single character strings are not.
59
60 public:
61 Base64() = delete ;
62} ;
63
64#endif
G::Base64
A base64 codec class.
Definition: gbase64.h:39
G::Base64::encode
static std::string encode(std::string_view, std::string_view line_break={})
Encodes the given string, optionally inserting line-breaks to limit the line length.
Definition: gbase64.cpp:79
G::Base64::decode
static std::string decode(std::string_view, bool throw_on_invalid=false, bool strict=true)
Decodes the given string.
Definition: gbase64.cpp:84
G::Base64::valid
static bool valid(std::string_view, bool strict=true)
Returns true if the string is a valid base64 encoding, possibly allowing for embedded newlines,...
Definition: gbase64.cpp:89
gexception.h
gstringview.h
G
Low-level classes.
Definition: garg.h:36
G::tx
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84