MongoDB  2.7.0
base64.h
1 // util/base64.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 namespace mongo {
21  namespace base64 {
22 
23  class Alphabet {
24  public:
25  Alphabet()
26  : encode((unsigned char*)
27  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
28  "abcdefghijklmnopqrstuvwxyz"
29  "0123456789"
30  "+/")
31  , decode(new unsigned char[257]) {
32  memset( decode.get() , 0 , 256 );
33  for ( int i=0; i<64; i++ ) {
34  decode[ encode[i] ] = i;
35  }
36 
37  test();
38  }
39  void test() {
40  verify( strlen( (char*)encode ) == 64 );
41  for ( int i=0; i<26; i++ )
42  verify( encode[i] == toupper( encode[i+26] ) );
43  }
44 
45  char e( int x ) {
46  return encode[x&0x3f];
47  }
48 
49  private:
50  const unsigned char * encode;
51  public:
52  boost::scoped_array<unsigned char> decode;
53  };
54 
55  extern Alphabet alphabet;
56 
57 
58  void encode( stringstream& ss , const char * data , int size );
59  string encode( const char * data , int size );
60  string encode( const string& s );
61 
62  void decode( stringstream& ss , const string& s );
63  string decode( const string& s );
64 
65  extern const char* chars;
66 
67  void testAlphabet();
68  }
69 }
Definition: base64.h:23