blob: a774b50ce2daace630d759d066cf504464f8b9eb [file] [log] [blame]
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento8663ed12022-07-23 03:04:27 -04003 * Copyright (c) 2012-2022 University of California, Los Angeles
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -05004 *
5 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
7 *
8 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Davide Pesavento780e6462021-02-08 20:58:12 -050020#include "bzip2-helper.hpp"
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050021
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050022#include <boost/iostreams/copy.hpp>
Davide Pesavento780e6462021-02-08 20:58:12 -050023#include <boost/iostreams/device/array.hpp>
24#include <boost/iostreams/filtering_stream.hpp>
25#include <boost/iostreams/filter/bzip2.hpp>
26#include <boost/iostreams/stream.hpp>
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050027
28#include <ndn-cxx/encoding/buffer-stream.hpp>
29
Davide Pesavento8663ed12022-07-23 03:04:27 -040030namespace chronosync::bzip2 {
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050031
32namespace bio = boost::iostreams;
33
34std::shared_ptr<ndn::Buffer>
35compress(const char* buffer, size_t bufferSize)
36{
37 ndn::OBufferStream os;
38 bio::filtering_stream<bio::output> out;
39 out.push(bio::bzip2_compressor());
40 out.push(os);
Davide Pesavento780e6462021-02-08 20:58:12 -050041 bio::stream<bio::array_source> in(buffer, bufferSize);
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050042 bio::copy(in, out);
43 return os.buf();
44}
45
46std::shared_ptr<ndn::Buffer>
47decompress(const char* buffer, size_t bufferSize)
48{
49 ndn::OBufferStream os;
50 bio::filtering_stream<bio::output> out;
51 out.push(bio::bzip2_decompressor());
52 out.push(os);
Davide Pesavento780e6462021-02-08 20:58:12 -050053 bio::stream<bio::array_source> in(buffer, bufferSize);
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050054 bio::copy(in, out);
55 return os.buf();
56}
57
Davide Pesavento8663ed12022-07-23 03:04:27 -040058} // namespace chronosync::bzip2