| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2007 Fridrich Strba ([email protected]) |
| 11 | * |
| 12 | * For minor contributions see the git repository. |
| 13 | * |
| 14 | * Alternatively, the contents of this file may be used under the terms |
| 15 | * of the GNU Lesser General Public License Version 2.1 or later |
| 16 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 17 | * applicable instead of those above. |
| 18 | */ |
| 19 | |
| 20 | #include <librevenge/librevenge.h> |
| 21 | |
| 22 | #include <boost/algorithm/string.hpp> |
| 23 | #include <boost/archive/iterators/binary_from_base64.hpp> |
| 24 | #include <boost/archive/iterators/base64_from_binary.hpp> |
| 25 | #include <boost/archive/iterators/remove_whitespace.hpp> |
| 26 | #include <boost/archive/iterators/transform_width.hpp> |
| 27 | |
| 28 | #include <algorithm> |
| 29 | #include <iterator> |
| 30 | #include <memory> |
| 31 | #include <vector> |
| 32 | #include <string> |
| 33 | #include <stdarg.h> |
| 34 | #include <stdio.h> |
| 35 | |
| 36 | #include "RVNGMemoryStream.h" |
| 37 | |
| 38 | |
| 39 | namespace librevenge |
| 40 | { |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | |
| 45 | struct DataImpl |
| 46 | { |
| 47 | DataImpl() : m_buf(), m_stream() {} |
| 48 | |
| 49 | std::vector<unsigned char> m_buf; |
| 50 | std::unique_ptr<RVNGMemoryInputStream> m_stream; |
| 51 | }; |
| 52 | |
| 53 | void convertFromBase64(std::vector<unsigned char> &result, const std::string &source) |
| 54 | { |
| 55 | std::string::const_iterator paddingIter = std::find(source.begin(), source.end(), '='); |
| 56 | typedef boost::archive::iterators::transform_width< |
| 57 | boost::archive::iterators::binary_from_base64< |
| 58 | boost::archive::iterators::remove_whitespace< std::string::const_iterator > >, 8, 6 > base64_decoder; |
| 59 | |
| 60 | std::copy(base64_decoder(source.begin()), base64_decoder(paddingIter), std::back_inserter(result)); |
| 61 | } |
| 62 | |
| 63 | void convertToBase64(std::string &result, const std::vector<unsigned char> &source) |
| 64 | { |
| 65 | auto numPadding = unsigned((3- (source.size()%3)) %3); |
| 66 | |
| 67 | typedef boost::archive::iterators::base64_from_binary< |
| 68 | boost::archive::iterators::transform_width<std::vector<unsigned char>::const_iterator, 6, 8 > > base64_encoder; |
| 69 | |
| 70 | // Encode the buffer and create a string |
| 71 | std::copy( |
| 72 | base64_encoder(source.begin()), |
| 73 | base64_encoder(source.end()), std::back_inserter(result)); |
| 74 | |
| 75 | result.append(numPadding, '='); // add '=' for each padded character |
| 76 | } |
| 77 | |
| 78 | } // anonymous namespace |
| 79 | |
| 80 | struct RVNGBinaryDataImpl |
| 81 | { |
| 82 | RVNGBinaryDataImpl(); |
| 83 | |
| 84 | void makeUnique(); |
| 85 | |
| 86 | std::shared_ptr<DataImpl> m_ptr; |
| 87 | }; |
| 88 | |
| 89 | RVNGBinaryDataImpl::RVNGBinaryDataImpl() |
| 90 | : m_ptr(new DataImpl()) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | void RVNGBinaryDataImpl::makeUnique() |
| 95 | { |
| 96 | if (!m_ptr.unique()) |
| 97 | { |
| 98 | std::shared_ptr<DataImpl> ptr(new DataImpl()); |
| 99 | ptr->m_buf = m_ptr->m_buf; |
| 100 | m_ptr = ptr; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | RVNGBinaryData::~RVNGBinaryData() |
| 105 | { |
| 106 | delete m_binaryDataImpl; |
| 107 | } |
| 108 | |
| 109 | RVNGBinaryData::RVNGBinaryData() : |
| 110 | m_binaryDataImpl(new RVNGBinaryDataImpl) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | RVNGBinaryData::RVNGBinaryData(const RVNGBinaryData &data) : |
| 115 | m_binaryDataImpl(new RVNGBinaryDataImpl) |
| 116 | { |
| 117 | m_binaryDataImpl->m_ptr = data.m_binaryDataImpl->m_ptr; |
| 118 | } |
| 119 | |
| 120 | RVNGBinaryData::RVNGBinaryData(const unsigned char *buffer, const unsigned long bufferSize) : |
| 121 | m_binaryDataImpl(nullptr) |
| 122 | { |
| 123 | std::unique_ptr<RVNGBinaryDataImpl> impl(new RVNGBinaryDataImpl()); |
| 124 | if (buffer) |
| 125 | impl->m_ptr->m_buf.assign(buffer, buffer + bufferSize); |
| 126 | m_binaryDataImpl = impl.release(); |
| 127 | } |
| 128 | |
| 129 | RVNGBinaryData::RVNGBinaryData(const RVNGString &base64) : |
| 130 | m_binaryDataImpl(nullptr) |
| 131 | { |
| 132 | std::unique_ptr<RVNGBinaryDataImpl> impl(new RVNGBinaryDataImpl()); |
| 133 | std::string base64String(base64.cstr(), base64.size()); |
| 134 | boost::trim(base64String); |
| 135 | convertFromBase64(impl->m_ptr->m_buf, base64String); |
| 136 | m_binaryDataImpl = impl.release(); |
| 137 | } |
| 138 | |
| 139 | RVNGBinaryData::RVNGBinaryData(const char *base64) : |
| 140 | m_binaryDataImpl(nullptr) |
| 141 | { |
| 142 | std::unique_ptr<RVNGBinaryDataImpl> impl(new RVNGBinaryDataImpl()); |
| 143 | if (base64) |
| 144 | { |
| 145 | std::string base64String(base64); |
| 146 | boost::trim(base64String); |
| 147 | convertFromBase64(impl->m_ptr->m_buf, base64String); |
| 148 | } |
| 149 | m_binaryDataImpl = impl.release(); |
| 150 | } |
| 151 | |
| 152 | void RVNGBinaryData::append(const RVNGBinaryData &data) |
| 153 | { |
| 154 | m_binaryDataImpl->makeUnique(); |
| 155 | |
| 156 | unsigned long previousSize = m_binaryDataImpl->m_ptr->m_buf.size(); |
| 157 | m_binaryDataImpl->m_ptr->m_buf.reserve(previousSize + data.m_binaryDataImpl->m_ptr->m_buf.size()); |
| 158 | const auto &src = data.m_binaryDataImpl->m_ptr->m_buf; |
| 159 | std::copy(src.begin(), src.end(), std::back_inserter(m_binaryDataImpl->m_ptr->m_buf)); |
| 160 | } |
| 161 | |
| 162 | void RVNGBinaryData::appendBase64Data(const RVNGString &base64) |
| 163 | { |
| 164 | std::string base64String(base64.cstr(), base64.size()); |
| 165 | boost::trim(base64String); |
| 166 | std::vector<unsigned char> buffer; |
| 167 | convertFromBase64(buffer, base64String); |
| 168 | if (!buffer.empty()) |
| 169 | append(buffer.data(), buffer.size()); |
| 170 | } |
| 171 | |
| 172 | void RVNGBinaryData::appendBase64Data(const char *base64) |
| 173 | { |
| 174 | if (base64) |
| 175 | { |
| 176 | std::string base64String(base64); |
| 177 | boost::trim(base64String); |
| 178 | std::vector<unsigned char> buffer; |
| 179 | convertFromBase64(buffer, base64String); |
| 180 | if (!buffer.empty()) |
| 181 | append(buffer.data(), buffer.size()); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void RVNGBinaryData::append(const unsigned char *buffer, const unsigned long bufferSize) |
| 186 | { |
| 187 | if (buffer && bufferSize > 0) |
| 188 | { |
| 189 | m_binaryDataImpl->makeUnique(); |
| 190 | |
| 191 | std::vector<unsigned char> &buf = m_binaryDataImpl->m_ptr->m_buf; |
| 192 | buf.reserve(buf.size() + bufferSize); |
| 193 | buf.insert(buf.end(), buffer, buffer + bufferSize); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void RVNGBinaryData::append(const unsigned char c) |
| 198 | { |
| 199 | m_binaryDataImpl->makeUnique(); |
| 200 | |
| 201 | m_binaryDataImpl->m_ptr->m_buf.push_back(c); |
| 202 | } |
| 203 | |
| 204 | void RVNGBinaryData::clear() |
| 205 | { |
| 206 | m_binaryDataImpl->makeUnique(); |
| 207 | |
| 208 | // clear and return allocated memory |
| 209 | std::vector<unsigned char>().swap(m_binaryDataImpl->m_ptr->m_buf); |
| 210 | } |
| 211 | |
| 212 | unsigned long RVNGBinaryData::size() const |
| 213 | { |
| 214 | return (unsigned long)m_binaryDataImpl->m_ptr->m_buf.size(); |
| 215 | } |
| 216 | bool RVNGBinaryData::empty() const |
| 217 | { |
| 218 | return (unsigned long)m_binaryDataImpl->m_ptr->m_buf.empty(); |
| 219 | } |
| 220 | |
| 221 | RVNGBinaryData &RVNGBinaryData::operator=(const RVNGBinaryData &dataBuf) |
| 222 | { |
| 223 | m_binaryDataImpl->m_ptr = dataBuf.m_binaryDataImpl->m_ptr; |
| 224 | return *this; |
| 225 | } |
| 226 | |
| 227 | const unsigned char *RVNGBinaryData::getDataBuffer() const |
| 228 | { |
| 229 | if (m_binaryDataImpl->m_ptr->m_buf.empty()) |
| 230 | return nullptr; |
| 231 | return m_binaryDataImpl->m_ptr->m_buf.data(); |
| 232 | } |
| 233 | |
| 234 | const RVNGString RVNGBinaryData::getBase64Data() const |
| 235 | { |
| 236 | std::string base64; |
| 237 | convertToBase64(base64, m_binaryDataImpl->m_ptr->m_buf); |
| 238 | return RVNGString(base64.c_str()); |
| 239 | } |
| 240 | |
| 241 | RVNGInputStream *RVNGBinaryData::getDataStream() const |
| 242 | { |
| 243 | std::shared_ptr<DataImpl> data = m_binaryDataImpl->m_ptr; |
| 244 | if (data->m_stream) |
| 245 | { |
| 246 | data->m_stream.reset(); |
| 247 | } |
| 248 | if (data->m_buf.empty()) |
| 249 | return nullptr; |
| 250 | data->m_stream.reset(new RVNGMemoryInputStream(data->m_buf.data(), data->m_buf.size())); |
| 251 | return data->m_stream.get(); |
| 252 | } |
| 253 | |
| 254 | } |
| 255 | |
| 256 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |