1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14/* This file is derived from the libb64 project which itself was released to public domain.
15 * For details, see http://sourceforge.net/projects/libb64. Original code by Chris Venter
16 * c++ wrapper for a base64 encoding and decoding algorithm
17*/
18
19#ifndef OSGDB_CONVERTBASE64_H
20#define OSGDB_CONVERTBASE64_H 1
21
22#include <iostream>
23#include <string>
24#include <vector>
25
26#include <osgDB/Export>
27
28namespace osgDB
29{
30    const int BUFFERSIZE = 8192;
31
32    typedef enum
33    {
34        step_a, step_b, step_c, step_d
35    } base64_decodestep;
36
37    typedef enum
38    {
39        step_A, step_B, step_C
40    } base64_encodestep;
41
42    typedef struct
43    {
44        base64_encodestep step;
45        char result;
46        int stepcount;
47    } base64_encodestate;
48
49    inline void base64_init_encodestate(base64_encodestate* state_in)
50    {
51        state_in->step = step_A;
52        state_in->result = 0;
53        state_in->stepcount = 0;
54    }
55
56
57    typedef struct
58    {
59        base64_decodestep step;
60        char plainchar;
61    } base64_decodestate;
62
63    inline void base64_init_decodestate(base64_decodestate* state_in)
64    {
65        state_in->step = step_a;
66        state_in->plainchar = 0;
67    }
68
69    class OSGDB_EXPORT Base64encoder
70    {
71    public:
72        Base64encoder(int buffersize_in = BUFFERSIZE):
73            _buffersize(buffersize_in)
74        {
75            base64_init_encodestate(&_state);
76        }
77
78        int encode(char value_in);
79
80        int encode(const char* code_in, const int length_in, char* plaintext_out);
81
82        int encode_end(char* plaintext_out);
83
84        void encode(std::istream& istream_in, std::ostream& ostream_in);
85
86        void encode(const char* chars_in, int length_in, std::string& code_out);
87
88    private:
89        base64_encodestate _state;
90        int _buffersize;
91    };
92
93    class OSGDB_EXPORT Base64decoder
94    {
95    public:
96        Base64decoder(int buffersize_in = BUFFERSIZE):
97            _buffersize(buffersize_in)
98        {
99            base64_init_decodestate(&_state);
100        }
101
102        int decode(char value_in);
103
104        int decode(const char* code_in, const int length_in, char* plaintext_out);
105
106        void decode(std::istream& istream_in, std::ostream& ostream_in);
107
108        // Decode strings, returns one char* of appropriate size
109        // Note that deallocation of char* is up to the caller of this method
110        char* decode(const std::vector<std::string>& str_in, std::vector<unsigned int>& pos_out);
111
112    private:
113        base64_decodestate _state;
114        int _buffersize;
115    };
116
117} // namespace osgDB
118
119#endif // BASE64_DECODE_H
120
121