Fix internal tests
[platform/core/security/cert-svc.git] / vcore / 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 <vcore/exception.h>
21
22 struct bio_st;
23 typedef bio_st BIO;
24
25 namespace ValidationCore {
26 class Base64Encoder {
27 public:
28     class Exception {
29     public:
30         VCORE_DECLARE_EXCEPTION_TYPE(ValidationCore::Exception, Base)
31         VCORE_DECLARE_EXCEPTION_TYPE(Base, InternalError)
32         VCORE_DECLARE_EXCEPTION_TYPE(Base, NotFinalized)
33         VCORE_DECLARE_EXCEPTION_TYPE(Base, AlreadyFinalized)
34     };
35     Base64Encoder();
36     void append(const std::string &data);
37     void finalize();
38     std::string get();
39     void reset();
40     ~Base64Encoder();
41
42 private:
43     Base64Encoder(const Base64Encoder &);
44     const Base64Encoder &operator=(const Base64Encoder &);
45
46     BIO *m_b64;
47     BIO *m_bmem;
48     bool m_finalized;
49 };
50
51 class Base64Decoder {
52 public:
53     class Exception {
54     public:
55         VCORE_DECLARE_EXCEPTION_TYPE(ValidationCore::Exception, Base)
56         VCORE_DECLARE_EXCEPTION_TYPE(Base, InternalError)
57         VCORE_DECLARE_EXCEPTION_TYPE(Base, NotFinalized)
58         VCORE_DECLARE_EXCEPTION_TYPE(Base, AlreadyFinalized)
59     };
60     Base64Decoder();
61     void append(const std::string &data);
62
63     /*
64      *  Function will return false when BIO_read fails
65      *  (for example: when string was not in base64 format).
66      */
67     bool finalize();
68     std::string get() const;
69     void reset();
70     ~Base64Decoder() {}
71
72 private:
73     Base64Decoder(const Base64Decoder &);
74     const Base64Decoder &operator=(const Base64Decoder &);
75
76     std::string m_input;
77     std::string m_output;
78     bool m_finalized;
79 };
80 } // namespace ValidationCore
81
82 #endif