tizen beta release
[framework/web/wrt-commons.git] / modules / vcore / src / vcore / Base64.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #ifndef _BASE64_H_
17 #define _BASE64_H_
18
19 #include <string>
20 #include <dpl/noncopyable.h>
21 #include <dpl/exception.h>
22
23 struct bio_st;
24 typedef bio_st BIO;
25
26 namespace ValidationCore {
27 class Base64Encoder : public DPL::Noncopyable
28 {
29   public:
30     class Exception
31     {
32       public:
33         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
34         DECLARE_EXCEPTION_TYPE(Base, InternalError)
35         DECLARE_EXCEPTION_TYPE(Base, NotFinalized)
36         DECLARE_EXCEPTION_TYPE(Base, AlreadyFinalized)
37     };
38     Base64Encoder();
39     void append(const std::string &data);
40     void finalize();
41     std::string get();
42     void reset();
43     ~Base64Encoder();
44
45   private:
46     BIO *m_b64;
47     BIO *m_bmem;
48     bool m_finalized;
49 };
50
51 class Base64Decoder : public DPL::Noncopyable
52 {
53   public:
54     class Exception
55     {
56       public:
57         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
58         DECLARE_EXCEPTION_TYPE(Base, InternalError)
59         DECLARE_EXCEPTION_TYPE(Base, NotFinalized)
60         DECLARE_EXCEPTION_TYPE(Base, AlreadyFinalized)
61     };
62     Base64Decoder();
63     void append(const std::string &data);
64
65     /*
66      *  Function will return false when BIO_read fails
67      *  (for example: when string was not in base64 format).
68      */
69     bool finalize();
70     std::string get() const;
71     void reset();
72     ~Base64Decoder()
73     {
74     }
75
76   private:
77     std::string m_input;
78     std::string m_output;
79     bool m_finalized;
80 };
81 } // namespace ValidationCore
82
83 #endif