compression.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #include <blob/compression.h>
30 
31 #ifndef HAVE_BZIP2
32  #define HAVE_BZIP2
33 #endif
34 
35 #ifdef HAVE_BZIP2
36 
37 #include <bzlib.h>
38 
39 namespace blob
40 {
41  static const std::size_t CHUNK_SIZE = 10 * 1024; // 10k chunks
42  static const int VERBOSITY = 1;
43 
44  bool compressionAvailable() { return true; }
45 
46  bool deflate(const uint8_t *data, uint32_t size, std::vector<uint8_t>& deflated)
47  {
48  static int BLOCK_SIZE_100K = 5;
49  deflated.clear();
50 
51  bz_stream stream;
52  stream.next_in = reinterpret_cast<char *>(const_cast<uint8_t *>(data));
53  stream.avail_in = size;
54  stream.bzalloc = NULL;
55  stream.bzfree = NULL;
56  stream.opaque = NULL;
57 
58  if (BZ2_bzCompressInit(&stream, BLOCK_SIZE_100K, 0, 0) != BZ_OK) {
59  return false;
60  }
61 
62  deflated.resize(CHUNK_SIZE);
63  stream.next_out = reinterpret_cast<char *>(deflated.data());
64  stream.avail_out = deflated.size();
65 
66  int result = BZ_RUN_OK;
67  int state = BZ_RUN;
68  while(result == BZ_RUN_OK || result == BZ_FLUSH_OK || result == BZ_FINISH_OK) {
69  if (stream.avail_in == 0) {
70  state = BZ_FINISH;
71  }
72 
73  if (stream.avail_out == 0) {
74  deflated.resize(deflated.size() + CHUNK_SIZE);
75  stream.next_out = reinterpret_cast<char *>(deflated.data() + deflated.size() - CHUNK_SIZE);
76  stream.avail_out = CHUNK_SIZE;
77  }
78 
79  result = BZ2_bzCompress(&stream, state);
80  }
81 
82  if (result != BZ_STREAM_END) {
83  deflated.clear();
84  BZ2_bzCompressEnd(&stream);
85  return false;
86  }
87 
88  deflated.resize(deflated.size() - stream.avail_out);
89  BZ2_bzCompressEnd(&stream);
90  return true;
91  }
92 
93  bool inflate(const uint8_t *data, uint32_t size, std::vector<uint8_t>& inflated)
94  {
95  static const int SMALL = 1;
96  inflated.clear();
97 
98  bz_stream stream;
99  stream.next_in = reinterpret_cast<char *>(const_cast<uint8_t *>(data));
100  stream.avail_in = size;
101  stream.bzalloc = NULL;
102  stream.bzfree = NULL;
103  stream.opaque = NULL;
104 
105  if (BZ2_bzDecompressInit(&stream, VERBOSITY, SMALL) != BZ_OK) {
106  return false;
107  }
108 
109  inflated.resize(CHUNK_SIZE);
110  stream.next_out = reinterpret_cast<char *>(inflated.data());
111  stream.avail_out = inflated.size();
112 
113  int result = BZ_OK;
114  while(result == BZ_OK || result == BZ_FLUSH_OK || result == BZ_FINISH_OK) {
115  if (stream.avail_out == 0) {
116  inflated.resize(inflated.size() + CHUNK_SIZE);
117  stream.next_out = reinterpret_cast<char *>(inflated.data() + inflated.size() - CHUNK_SIZE);
118  stream.avail_out = CHUNK_SIZE;
119  }
120 
121  result = BZ2_bzDecompress(&stream);
122  }
123 
124  if (result != BZ_STREAM_END) {
125  inflated.clear();
126  BZ2_bzDecompressEnd(&stream);
127  return false;
128  }
129 
130  inflated.resize(inflated.size() - stream.avail_out);
131  BZ2_bzDecompressEnd(&stream);
132  return true;
133  }
134 
135 } // namespace blob
136 
137 #else // HAVE_BZIP2
138 
139 namespace blob
140 {
141  bool compressionAvailable() { return false; }
142  bool deflate(const uint8_t *data, uint32_t size, std::vector<uint8_t>& deflated) { return false; }
143  bool inflate(const uint8_t *data, uint32_t size, std::vector<uint8_t>& inflated) { return false; }
144 }
145 
146 #endif // HAVE_BZIP2
static const std::size_t CHUNK_SIZE
Definition: compression.cpp:41
bool inflate(const uint8_t *data, uint32_t size, std::vector< uint8_t > &inflated)
Definition: compression.cpp:93
Definition: Blob.h:43
bool compressionAvailable()
Definition: compression.cpp:44
static const int VERBOSITY
Definition: compression.cpp:42
bool deflate(const uint8_t *data, uint32_t size, std::vector< uint8_t > &deflated)
Definition: compression.cpp:46


blob
Author(s): Johannes Meyer
autogenerated on Sat Jul 27 2019 03:35:24