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.
19 #include <openssl/bio.h>
20 #include <openssl/evp.h>
21 #include <openssl/buffer.h>
23 #include <dpl/log/log.h>
25 #include <dpl/scoped_free.h>
27 #include <vcore/Base64.h>
29 namespace ValidationCore {
30 Base64Encoder::Base64Encoder() :
37 void Base64Encoder::append(const std::string &data)
40 LogWarning("Already finalized.");
41 VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized");
47 BIO_write(m_b64, data.c_str(), data.size());
50 void Base64Encoder::finalize()
53 LogWarning("Already finalized.");
54 VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
58 if (BIO_flush(m_b64) != 1)
59 VcoreThrowMsg(Exception::InternalError, "Bio internal error");
62 std::string Base64Encoder::get()
65 LogWarning("Not finalized");
66 VcoreThrowMsg(Exception::NotFinalized, "Not finalized");
69 BIO_get_mem_ptr(m_b64, &bptr);
71 LogError("Bio internal error");
72 VcoreThrowMsg(Exception::InternalError, "Bio internal error");
75 if (bptr->length > 0) {
76 return std::string(bptr->data, bptr->length);
81 void Base64Encoder::reset()
85 m_b64 = BIO_new(BIO_f_base64());
86 m_bmem = BIO_new(BIO_s_mem());
87 if (!m_b64 || !m_bmem) {
88 LogError("Error during allocation memory in BIO");
89 VcoreThrowMsg(Exception::InternalError,
90 "Error during allocation memory in BIO");
92 BIO_set_flags(m_b64, BIO_FLAGS_BASE64_NO_NL);
93 m_b64 = BIO_push(m_b64, m_bmem);
96 Base64Encoder::~Base64Encoder()
101 Base64Decoder::Base64Decoder() :
106 void Base64Decoder::append(const std::string &data)
109 LogWarning("Already finalized.");
110 VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
112 m_input.append(data);
115 static bool whiteCharacter(char a)
117 if (a == '\n') { return true; }
121 bool Base64Decoder::finalize()
124 LogWarning("Already finalized.");
125 VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
130 m_input.erase(std::remove_if(m_input.begin(),
135 for (size_t i = 0; i<m_input.size(); ++i) {
136 if (isalnum(m_input[i])
139 || m_input[i] == '=')
143 LogError("Base64 input contains illegal chars: " << m_input[i]);
148 size_t len = m_input.size();
150 VcoreDPL::ScopedFree<char> buffer(static_cast<char*>(malloc(len)));
153 LogError("Error in malloc.");
154 VcoreThrowMsg(Exception::InternalError, "Error in malloc.");
157 memset(buffer.Get(), 0, len);
158 b64 = BIO_new(BIO_f_base64());
160 LogError("Couldn't create BIO object.");
161 VcoreThrowMsg(Exception::InternalError, "Couldn't create BIO object.");
163 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
164 VcoreDPL::ScopedFree<char> tmp(strdup(m_input.c_str()));
167 bmem = BIO_new_mem_buf(tmp.Get(), len);
171 LogError("Internal error in BIO");
172 VcoreThrowMsg(Exception::InternalError, "Internal error in BIO");
175 bmem = BIO_push(b64, bmem);
179 LogError("Internal error in BIO");
180 VcoreThrowMsg(Exception::InternalError, "Internal error in BIO");
183 int readlen = BIO_read(bmem, buffer.Get(), len);
189 m_output.append(buffer.Get(), readlen);
198 std::string Base64Decoder::get() const
201 LogWarning("Not finalized.");
202 VcoreThrowMsg(Exception::NotFinalized, "Not finalized");
207 void Base64Decoder::reset()
213 } // namespace ValidationCore