2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <openssl/bio.h>
21 #include <openssl/evp.h>
22 #include <openssl/buffer.h>
24 #include <dpl/log/log.h>
26 #include <vcore/Base64.h>
28 namespace ValidationCore {
29 Base64Encoder::Base64Encoder() :
36 void Base64Encoder::append(const std::string &data)
39 LogWarning("Already finalized.");
40 VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized");
46 BIO_write(m_b64, data.c_str(), data.size());
49 void Base64Encoder::finalize()
52 LogWarning("Already finalized.");
53 VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
57 if (BIO_flush(m_b64) != 1)
58 VcoreThrowMsg(Exception::InternalError, "Bio internal error");
61 std::string Base64Encoder::get()
64 LogWarning("Not finalized");
65 VcoreThrowMsg(Exception::NotFinalized, "Not finalized");
68 BIO_get_mem_ptr(m_b64, &bptr);
70 LogError("Bio internal error");
71 VcoreThrowMsg(Exception::InternalError, "Bio internal error");
74 if (bptr->length > 0) {
75 return std::string(bptr->data, bptr->length);
80 void Base64Encoder::reset()
84 m_b64 = BIO_new(BIO_f_base64());
85 m_bmem = BIO_new(BIO_s_mem());
86 if (!m_b64 || !m_bmem) {
87 LogError("Error during allocation memory in BIO");
88 VcoreThrowMsg(Exception::InternalError,
89 "Error during allocation memory in BIO");
91 BIO_set_flags(m_b64, BIO_FLAGS_BASE64_NO_NL);
92 m_b64 = BIO_push(m_b64, m_bmem);
95 Base64Encoder::~Base64Encoder()
100 Base64Decoder::Base64Decoder() :
105 void Base64Decoder::append(const std::string &data)
108 LogWarning("Already finalized.");
109 VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
111 m_input.append(data);
114 static bool whiteCharacter(char a)
122 bool Base64Decoder::finalize()
125 LogWarning("Already finalized.");
126 VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
131 m_input.erase(std::remove_if(m_input.begin(),
136 for (size_t i = 0; i<m_input.size(); ++i) {
137 if (isalnum(m_input[i])
140 || m_input[i] == '=')
142 LogError("Base64 input contains illegal chars: " << m_input[i]);
147 size_t len = m_input.size();
148 std::shared_ptr<char> buffer(new char[len], [](char *p){delete []p;});
150 LogError("Error in new");
151 VcoreThrowMsg(Exception::InternalError, "Error in new");
154 memset(buffer.get(), 0, len);
155 b64 = BIO_new(BIO_f_base64());
157 LogError("Couldn't create BIO object.");
158 VcoreThrowMsg(Exception::InternalError, "Couldn't create BIO object.");
160 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
162 std::shared_ptr<char> tmp(strdup(m_input.c_str()), [](char *p){free(p);});
164 LogError("Error in strdup");
165 VcoreThrowMsg(Exception::InternalError, "Error in strdup");
170 bmem = BIO_new_mem_buf(tmp.get(), len);
174 LogError("Internal error in BIO");
175 VcoreThrowMsg(Exception::InternalError, "Internal error in BIO");
178 bmem = BIO_push(b64, bmem);
182 LogError("Internal error in BIO");
183 VcoreThrowMsg(Exception::InternalError, "Internal error in BIO");
186 int readlen = BIO_read(bmem, buffer.get(), len);
192 m_output.append(buffer.get(), readlen);
201 std::string Base64Decoder::get() const
204 LogWarning("Not finalized.");
205 VcoreThrowMsg(Exception::NotFinalized, "Not finalized");
210 void Base64Decoder::reset()
216 } // namespace ValidationCore