gloox  1.0.1
base64.cpp
1 /*
2  Copyright (c) 2005-2012 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "base64.h"
15 
16 namespace gloox
17 {
18 
19  namespace Base64
20  {
21 
22  static const std::string alphabet64( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" );
23  static const char pad = '=';
24  static const char np = (char)std::string::npos;
25  static char table64vals[] =
26  {
27  62, np, np, np, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, np, np, np, np, np,
28  np, np, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
29  18, 19, 20, 21, 22, 23, 24, 25, np, np, np, np, np, np, 26, 27, 28, 29, 30, 31,
30  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
31  };
32 
33  inline char table64( unsigned char c )
34  {
35  return ( c < 43 || c > 122 ) ? np : table64vals[c-43];
36  }
37 
38  const std::string encode64( const std::string& input )
39  {
40  std::string encoded;
41  char c;
42  const std::string::size_type length = input.length();
43 
44  encoded.reserve( length * 2 );
45 
46  for( std::string::size_type i = 0; i < length; ++i )
47  {
48  c = static_cast<char>( ( input[i] >> 2 ) & 0x3f );
49  encoded += alphabet64[c];
50 
51  c = static_cast<char>( ( input[i] << 4 ) & 0x3f );
52  if( ++i < length )
53  c = static_cast<char>( c | static_cast<char>( ( input[i] >> 4 ) & 0x0f ) );
54  encoded += alphabet64[c];
55 
56  if( i < length )
57  {
58  c = static_cast<char>( ( input[i] << 2 ) & 0x3c );
59  if( ++i < length )
60  c = static_cast<char>( c | static_cast<char>( ( input[i] >> 6 ) & 0x03 ) );
61  encoded += alphabet64[c];
62  }
63  else
64  {
65  ++i;
66  encoded += pad;
67  }
68 
69  if( i < length )
70  {
71  c = static_cast<char>( input[i] & 0x3f );
72  encoded += alphabet64[c];
73  }
74  else
75  {
76  encoded += pad;
77  }
78  }
79 
80  return encoded;
81  }
82 
83  const std::string decode64( const std::string& input )
84  {
85  char c, d;
86  const std::string::size_type length = input.length();
87  std::string decoded;
88 
89  decoded.reserve( length );
90 
91  for( std::string::size_type i = 0; i < length; ++i )
92  {
93  c = table64(input[i]);
94  ++i;
95  d = table64(input[i]);
96  c = static_cast<char>( ( c << 2 ) | ( ( d >> 4 ) & 0x3 ) );
97  decoded += c;
98  if( ++i < length )
99  {
100  c = input[i];
101  if( pad == c )
102  break;
103 
104  c = table64(input[i]);
105  d = static_cast<char>( ( ( d << 4 ) & 0xf0 ) | ( ( c >> 2 ) & 0xf ) );
106  decoded += d;
107  }
108 
109  if( ++i < length )
110  {
111  d = input[i];
112  if( pad == d )
113  break;
114 
115  d = table64(input[i]);
116  c = static_cast<char>( ( ( c << 6 ) & 0xc0 ) | d );
117  decoded += c;
118  }
119  }
120 
121  return decoded;
122  }
123 
124  }
125 
126 }