VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gbase64.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 <string>
27 
28 namespace G
29 {
30  class Base64 ;
31 }
32 
33 /// \class G::Base64
34 /// A base64 codec class.
35 /// \see RFC 1341 section 5.2
36 ///
37 class G::Base64
38 {
39 public:
40  G_EXCEPTION( Error , "base64 encoding error" ) ;
41 
42  static std::string encode( const std::string & s , const std::string & line_break ) ;
43  ///< Encodes the given string.
44 
45  static std::string encode( const std::string & s ) ;
46  ///< Encodes the given string. Uses carriage-return-line-feed
47  ///< as the line-break string.
48 
49  static std::string decode( const std::string & ) ;
50  ///< Decodes the given string. Throws an exception
51  ///< if not a valid encoding.
52 
53  static bool valid( const std::string & ) ;
54  ///< Returns true if the string can be decoded.
55 
56 private:
57  Base64() ;
58  static inline g_uint32_t numeric( char c ) ;
59  static inline void accumulate_8( g_uint32_t & n , std::string::const_iterator & ,
60  std::string::const_iterator , int & ) ;
61  static inline size_t hi_6( g_uint32_t n ) ;
62  static inline void generate_6( g_uint32_t & n , int & i , std::string & result ) ;
63  static inline char to_char( g_uint32_t n ) ;
64  static inline size_t index( char c , bool & error ) ;
65  static inline size_t accumulate_6( g_uint32_t & n , char c_in , int & , bool & error ) ;
66  static inline g_uint32_t hi_8( g_uint32_t n ) ;
67  static inline void generate_8( g_uint32_t & n , int & i , std::string & result ) ;
68  static std::string decode( const std::string & s , bool & error ) ;
69 } ;
70 
71 #endif
72 
static bool valid(const std::string &)
Returns true if the string can be decoded.
Definition: gbase64.cpp:166
static std::string decode(const std::string &)
Decodes the given string.
Definition: gbase64.cpp:131
A base64 codec class.
Definition: gbase64.h:37
static std::string encode(const std::string &s, const std::string &line_break)
Encodes the given string.
Definition: gbase64.cpp:68