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>
30 Base64Encoder::Base64Encoder() :
37 void Base64Encoder::append(const RawBuffer &data)
40 LogWarning("Already finalized.");
41 ThrowMsg(Exception::AlreadyFinalized, "Already finalized");
47 BIO_write(m_b64, data.data(), data.size());
50 void Base64Encoder::finalize()
53 LogWarning("Already finalized.");
54 ThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
57 (void)BIO_flush(m_b64);
60 RawBuffer Base64Encoder::get()
63 LogWarning("Not finalized");
64 ThrowMsg(Exception::NotFinalized, "Not finalized");
66 BUF_MEM *bptr = nullptr;
67 BIO_get_mem_ptr(m_b64, &bptr);
69 LogError("Bio internal error");
70 ThrowMsg(Exception::InternalError, "Bio internal error");
73 if (bptr->length > 0) {
74 return RawBuffer(bptr->data, bptr->data + bptr->length);
79 void Base64Encoder::reset()
83 m_b64 = BIO_new(BIO_f_base64());
84 m_bmem = BIO_new(BIO_s_mem());
85 if (!m_b64 || !m_bmem) {
86 LogError("Error during allocation memory in BIO");
87 ThrowMsg(Exception::InternalError,
88 "Error during allocation memory in BIO");
90 BIO_set_flags(m_b64, BIO_FLAGS_BASE64_NO_NL);
91 m_b64 = BIO_push(m_b64, m_bmem);
94 Base64Encoder::~Base64Encoder()
99 Base64Decoder::Base64Decoder() :
104 void Base64Decoder::append(const RawBuffer &data)
107 LogWarning("Already finalized.");
108 ThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
110 std::copy(data.begin(), data.end(), std::back_inserter(m_input));
113 static bool whiteCharacter(char a)
118 bool Base64Decoder::finalize()
121 LogWarning("Already finalized.");
122 ThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
127 m_input.erase(std::remove_if(m_input.begin(),
132 for (size_t i = 0; i<m_input.size(); ++i) {
133 if (isalnum(m_input[i])
136 || m_input[i] == '=')
140 LogError("Base64 input contains illegal chars: " << m_input[i]);
145 size_t len = m_input.size();
147 RawBuffer buffer(len);
149 if (!buffer.data()) {
150 LogError("Error in malloc.");
151 ThrowMsg(Exception::InternalError, "Error in malloc.");
154 memset(buffer.data(), 0, buffer.size());
155 b64 = BIO_new(BIO_f_base64());
157 LogError("Couldn't create BIO object.");
158 ThrowMsg(Exception::InternalError, "Couldn't create BIO object.");
160 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
161 RawBuffer tmp(m_input);
164 bmem = BIO_new_mem_buf(tmp.data(), len);
168 LogError("Internal error in BIO");
169 ThrowMsg(Exception::InternalError, "Internal error in BIO");
172 bmem = BIO_push(b64, bmem);
176 LogError("Internal error in BIO");
177 ThrowMsg(Exception::InternalError, "Internal error in BIO");
180 int readlen = BIO_read(bmem, buffer.data(), buffer.size());
186 buffer.resize(readlen);
187 m_output = std::move(buffer);
196 RawBuffer Base64Decoder::get() const
199 LogWarning("Not finalized.");
200 ThrowMsg(Exception::NotFinalized, "Not finalized");
205 void Base64Decoder::reset()