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>
24 #include <dpl/scoped_free.h>
28 namespace ValidationCore {
29 Base64Encoder::Base64Encoder() :
36 void Base64Encoder::append(const std::string &data)
39 LogWarning("Already finalized.");
40 ThrowMsg(Exception::AlreadyFinalized, "Already finalized");
46 BIO_write(m_b64, data.c_str(), data.size());
49 void Base64Encoder::finalize()
52 LogWarning("Already finalized.");
53 ThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
59 std::string Base64Encoder::get()
62 LogWarning("Not finalized");
63 ThrowMsg(Exception::NotFinalized, "Not finalized");
66 BIO_get_mem_ptr(m_b64, &bptr);
68 LogError("Bio internal error");
69 ThrowMsg(Exception::InternalError, "Bio internal error");
72 if (bptr->length > 0) {
73 return std::string(bptr->data, bptr->length - 1);
78 void Base64Encoder::reset()
82 m_b64 = BIO_new(BIO_f_base64());
83 m_bmem = BIO_new(BIO_s_mem());
84 if (!m_b64 || !m_bmem) {
85 LogError("Error during allocation memory in BIO");
86 ThrowMsg(Exception::InternalError,
87 "Error during allocation memory in BIO");
89 m_b64 = BIO_push(m_b64, m_bmem);
92 Base64Encoder::~Base64Encoder()
97 Base64Decoder::Base64Decoder() :
102 void Base64Decoder::append(const std::string &data)
105 LogWarning("Already finalized.");
106 ThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
108 m_input.append(data);
111 static bool whiteCharacter(char a)
113 if (a == '\n') { return true; }
117 bool Base64Decoder::finalize()
120 LogWarning("Already finalized.");
121 ThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
126 m_input.erase(std::remove_if(m_input.begin(),
131 for (size_t i = 0; i<m_input.size(); ++i) {
132 if (isalnum(m_input[i])
135 || m_input[i] == '=')
139 LogError("Base64 input contains illegal chars: " << m_input[i]);
144 size_t len = m_input.size();
146 DPL::ScopedFree<char> buffer(static_cast<char*>(malloc(len)));
149 LogError("Error in malloc.");
150 ThrowMsg(Exception::InternalError, "Error in malloc.");
153 memset(buffer.Get(), 0, len);
154 b64 = BIO_new(BIO_f_base64());
156 LogError("Couldn't create BIO object.");
157 ThrowMsg(Exception::InternalError, "Couldn't create BIO object.");
159 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
160 DPL::ScopedFree<char> tmp(strdup(m_input.c_str()));
163 bmem = BIO_new_mem_buf(tmp.Get(), len);
167 LogError("Internal error in BIO");
168 ThrowMsg(Exception::InternalError, "Internal error in BIO");
171 bmem = BIO_push(b64, bmem);
175 LogError("Internal error in BIO");
176 ThrowMsg(Exception::InternalError, "Internal error in BIO");
179 int readlen = BIO_read(bmem, buffer.Get(), len);
185 m_output.append(buffer.Get(), readlen);
194 std::string Base64Decoder::get() const
197 LogWarning("Not finalized.");
198 ThrowMsg(Exception::NotFinalized, "Not finalized");
203 void Base64Decoder::reset()
209 } // namespace ValidationCore