bz2_stream.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 ********************************************************************/
34 
35 #include "rosbag/chunked_file.h"
36 
37 #include <iostream>
38 #include <cstring>
39 #include "console_bridge/console.h"
40 
41 // Remove this include when no longer supporting platforms with libconsole-bridge-dev < 0.3.0,
42 // in particular Debian Jessie: https://packages.debian.org/jessie/libconsole-bridge-dev
44 
45 using std::string;
46 
47 namespace rosbag {
48 
50  Stream(file),
51  verbosity_(0),
52  block_size_100k_(9),
53  work_factor_(30),
54  bzfile_(NULL),
55  bzerror_(0)
56 { }
57 
59  return compression::BZ2;
60 }
61 
64 
65  switch (bzerror_) {
66  case BZ_OK: break;
67  default: {
68  BZ2_bzWriteClose(&bzerror_, bzfile_, 0, NULL, NULL);
69  throw BagException("Error opening file for writing compressed stream");
70  }
71  }
72 
73  setCompressedIn(0);
74 }
75 
76 void BZ2Stream::write(void* ptr, size_t size) {
77  if (!bzfile_) {
78  throw BagException("cannot write to unopened bzfile");
79  }
80 
81  BZ2_bzWrite(&bzerror_, bzfile_, ptr, size);
82 
83  switch (bzerror_) {
84  case BZ_IO_ERROR: throw BagException("BZ_IO_ERROR: error writing the compressed file");
85  }
86 
88 }
89 
91  if (!bzfile_) {
92  throw BagException("cannot close unopened bzfile");
93  }
94 
95  unsigned int nbytes_in;
96  unsigned int nbytes_out;
97  BZ2_bzWriteClose(&bzerror_, bzfile_, 0, &nbytes_in, &nbytes_out);
98 
99  switch (bzerror_) {
100  case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR");
101  }
102 
103  advanceOffset(nbytes_out);
104  setCompressedIn(0);
105 }
106 
108  bzfile_ = BZ2_bzReadOpen(&bzerror_, getFilePointer(), verbosity_, 0, getUnused(), getUnusedLength());
109 
110  switch (bzerror_) {
111  case BZ_OK: break;
112  default: {
113  BZ2_bzReadClose(&bzerror_, bzfile_);
114  throw BagException("Error opening file for reading compressed stream");
115  }
116  }
117 
118  clearUnused();
119 }
120 
121 void BZ2Stream::read(void* ptr, size_t size) {
122  if (!bzfile_) {
123  throw BagException("cannot read from unopened bzfile");
124  }
125 
126  BZ2_bzRead(&bzerror_, bzfile_, ptr, size);
127 
128  advanceOffset(size);
129 
130  switch (bzerror_) {
131  case BZ_OK: return;
132  case BZ_STREAM_END:
133  if (getUnused() || getUnusedLength() > 0)
134  CONSOLE_BRIDGE_logError("unused data already available");
135  else {
136  char* unused;
137  int nUnused;
138  BZ2_bzReadGetUnused(&bzerror_, bzfile_, (void**) &unused, &nUnused);
139  setUnused(unused);
140  setUnusedLength(nUnused);
141  }
142  return;
143  case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR: error reading from compressed stream"); break;
144  case BZ_UNEXPECTED_EOF: throw BagIOException("BZ_UNEXPECTED_EOF: compressed stream ended before logical end-of-stream detected"); break;
145  case BZ_DATA_ERROR: throw BagIOException("BZ_DATA_ERROR: data integrity error detected in compressed stream"); break;
146  case BZ_DATA_ERROR_MAGIC: throw BagIOException("BZ_DATA_ERROR_MAGIC: stream does not begin with requisite header bytes"); break;
147  case BZ_MEM_ERROR: throw BagIOException("BZ_MEM_ERROR: insufficient memory available"); break;
148  }
149 }
150 
152  if (!bzfile_) {
153  throw BagException("cannot close unopened bzfile");
154  }
155 
156  BZ2_bzReadClose(&bzerror_, bzfile_);
157 
158  switch (bzerror_) {
159  case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR");
160  }
161 }
162 
163 void BZ2Stream::decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len) {
164  int result = BZ2_bzBuffToBuffDecompress((char*) dest, &dest_len, (char*) source, source_len, 0, verbosity_);
165 
166  switch (result) {
167  case BZ_OK: break;
168  case BZ_CONFIG_ERROR: throw BagException("library has been mis-compiled"); break;
169  case BZ_PARAM_ERROR: throw BagException("dest is NULL or destLen is NULL or small != 0 && small != 1 or verbosity < 0 or verbosity > 4"); break;
170  case BZ_MEM_ERROR: throw BagException("insufficient memory is available"); break;
171  case BZ_OUTBUFF_FULL: throw BagException("size of the compressed data exceeds *destLen"); break;
172  case BZ_DATA_ERROR: throw BagException("data integrity error was detected in the compressed data"); break;
173  case BZ_DATA_ERROR_MAGIC: throw BagException("compressed data doesn't begin with the right magic bytes"); break;
174  case BZ_UNEXPECTED_EOF: throw BagException("compressed data ends unexpectedly"); break;
175  }
176 }
177 
178 } // namespace rosbag
uint64_t getCompressedIn()
Definition: stream.cpp:74
int getUnusedLength()
Definition: stream.cpp:78
ChunkedFile reads and writes files which contain interleaved chunks of compressed and uncompressed da...
Definition: chunked_file.h:51
CompressionType getCompressionType() const
Definition: bz2_stream.cpp:58
#define CONSOLE_BRIDGE_logError(fmt,...)
void setUnusedLength(int nUnused)
Definition: stream.cpp:80
int verbosity_
level of debugging output (0-4; 0 default). 0 is silent, 4 is max verbose debugging output ...
Definition: stream.h:157
void decompress(uint8_t *dest, unsigned int dest_len, uint8_t *source, unsigned int source_len)
Definition: bz2_stream.cpp:163
char * getUnused()
Definition: stream.cpp:77
BZ2Stream(ChunkedFile *file)
Definition: bz2_stream.cpp:49
Base class for rosbag exceptions.
Definition: exceptions.h:43
void write(void *ptr, size_t size)
Definition: bz2_stream.cpp:76
int block_size_100k_
compression block size (1-9; 9 default). 9 is best compression, most memory
Definition: stream.h:158
void advanceOffset(uint64_t nbytes)
Definition: stream.cpp:76
Exception thrown when on IO problems.
Definition: exceptions.h:50
int work_factor_
compression behavior for worst case, highly repetitive data (0-250; 30 default)
Definition: stream.h:159
FILE * getFilePointer()
Definition: stream.cpp:73
void setCompressedIn(uint64_t nbytes)
Definition: stream.cpp:75
void clearUnused()
Definition: stream.cpp:81
BZFILE * bzfile_
bzlib compressed file stream
Definition: stream.h:161
Definition: bag.h:68
void setUnused(char *unused)
Definition: stream.cpp:79
int bzerror_
last error from bzlib
Definition: stream.h:162
void read(void *ptr, size_t size)
Definition: bz2_stream.cpp:121


rosbag_storage
Author(s): Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:19