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 using std::string;
42 
43 namespace rosbag {
44 
46  Stream(file),
47  verbosity_(0),
48  block_size_100k_(9),
49  work_factor_(30),
50  bzfile_(NULL),
51  bzerror_(0)
52 { }
53 
55  return compression::BZ2;
56 }
57 
60 
61  switch (bzerror_) {
62  case BZ_OK: break;
63  default: {
64  BZ2_bzWriteClose(&bzerror_, bzfile_, 0, NULL, NULL);
65  throw BagException("Error opening file for writing compressed stream");
66  }
67  }
68 
69  setCompressedIn(0);
70 }
71 
72 void BZ2Stream::write(void* ptr, size_t size) {
73  if (!bzfile_) {
74  throw BagException("cannot write to unopened bzfile");
75  }
76 
77  BZ2_bzWrite(&bzerror_, bzfile_, ptr, size);
78 
79  switch (bzerror_) {
80  case BZ_IO_ERROR: throw BagException("BZ_IO_ERROR: error writing the compressed file");
81  }
82 
84 }
85 
87  if (!bzfile_) {
88  throw BagException("cannot close unopened bzfile");
89  }
90 
91  unsigned int nbytes_in;
92  unsigned int nbytes_out;
93  BZ2_bzWriteClose(&bzerror_, bzfile_, 0, &nbytes_in, &nbytes_out);
94 
95  switch (bzerror_) {
96  case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR");
97  }
98 
99  advanceOffset(nbytes_out);
100  setCompressedIn(0);
101 }
102 
104  bzfile_ = BZ2_bzReadOpen(&bzerror_, getFilePointer(), verbosity_, 0, getUnused(), getUnusedLength());
105 
106  switch (bzerror_) {
107  case BZ_OK: break;
108  default: {
109  BZ2_bzReadClose(&bzerror_, bzfile_);
110  throw BagException("Error opening file for reading compressed stream");
111  }
112  }
113 
114  clearUnused();
115 }
116 
117 void BZ2Stream::read(void* ptr, size_t size) {
118  if (!bzfile_) {
119  throw BagException("cannot read from unopened bzfile");
120  }
121 
122  BZ2_bzRead(&bzerror_, bzfile_, ptr, size);
123 
124  advanceOffset(size);
125 
126  switch (bzerror_) {
127  case BZ_OK: return;
128  case BZ_STREAM_END:
129  if (getUnused() || getUnusedLength() > 0) {
130  CONSOLE_BRIDGE_logError("unused data already available");
131  } else {
132  char* unused;
133  int nUnused;
134  BZ2_bzReadGetUnused(&bzerror_, bzfile_, (void**) &unused, &nUnused);
135  setUnused(unused);
136  setUnusedLength(nUnused);
137  }
138  return;
139  case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR: error reading from compressed stream"); break;
140  case BZ_UNEXPECTED_EOF: throw BagIOException("BZ_UNEXPECTED_EOF: compressed stream ended before logical end-of-stream detected"); break;
141  case BZ_DATA_ERROR: throw BagIOException("BZ_DATA_ERROR: data integrity error detected in compressed stream"); break;
142  case BZ_DATA_ERROR_MAGIC: throw BagIOException("BZ_DATA_ERROR_MAGIC: stream does not begin with requisite header bytes"); break;
143  case BZ_MEM_ERROR: throw BagIOException("BZ_MEM_ERROR: insufficient memory available"); break;
144  }
145 }
146 
148  if (!bzfile_) {
149  throw BagException("cannot close unopened bzfile");
150  }
151 
152  BZ2_bzReadClose(&bzerror_, bzfile_);
153 
154  switch (bzerror_) {
155  case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR");
156  }
157 }
158 
159 void BZ2Stream::decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len) {
160  int result = BZ2_bzBuffToBuffDecompress((char*) dest, &dest_len, (char*) source, source_len, 0, verbosity_);
161 
162  switch (result) {
163  case BZ_OK: break;
164  case BZ_CONFIG_ERROR: throw BagException("library has been mis-compiled"); break;
165  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;
166  case BZ_MEM_ERROR: throw BagException("insufficient memory is available"); break;
167  case BZ_OUTBUFF_FULL: throw BagException("size of the compressed data exceeds *destLen"); break;
168  case BZ_DATA_ERROR: throw BagException("data integrity error was detected in the compressed data"); break;
169  case BZ_DATA_ERROR_MAGIC: throw BagException("compressed data doesn't begin with the right magic bytes"); break;
170  case BZ_UNEXPECTED_EOF: throw BagException("compressed data ends unexpectedly"); break;
171  }
172 }
173 
174 } // namespace rosbag
rosbag::BZ2Stream::block_size_100k_
int block_size_100k_
compression block size (1-9; 9 default). 9 is best compression, most memory
Definition: stream.h:190
rosbag::ChunkedFile
ChunkedFile reads and writes files which contain interleaved chunks of compressed and uncompressed da...
Definition: chunked_file.h:83
rosbag::BZ2Stream::verbosity_
int verbosity_
level of debugging output (0-4; 0 default). 0 is silent, 4 is max verbose debugging output
Definition: stream.h:189
rosbag::Stream::getFilePointer
FILE * getFilePointer()
Definition: stream.cpp:73
rosbag::BZ2Stream::stopWrite
void stopWrite()
Definition: bz2_stream.cpp:86
rosbag::Stream::clearUnused
void clearUnused()
Definition: stream.cpp:81
rosbag::BZ2Stream::BZ2Stream
BZ2Stream(ChunkedFile *file)
Definition: bz2_stream.cpp:45
rosbag::Stream::setUnused
void setUnused(char *unused)
Definition: stream.cpp:79
rosbag::Stream::setCompressedIn
void setCompressedIn(uint64_t nbytes)
Definition: stream.cpp:75
CONSOLE_BRIDGE_logError
#define CONSOLE_BRIDGE_logError(fmt,...)
rosbag::BZ2Stream::bzfile_
BZFILE * bzfile_
bzlib compressed file stream
Definition: stream.h:193
rosbag::compression::BZ2
@ BZ2
Definition: stream.h:154
rosbag::BZ2Stream::getCompressionType
CompressionType getCompressionType() const
Definition: bz2_stream.cpp:54
rosbag::BagException
Base class for rosbag exceptions.
Definition: exceptions.h:75
rosbag::BagIOException
Exception thrown when on IO problems.
Definition: exceptions.h:82
rosbag::BZ2Stream::write
void write(void *ptr, size_t size)
Definition: bz2_stream.cpp:72
rosbag::Stream::getCompressedIn
uint64_t getCompressedIn()
Definition: stream.cpp:74
rosbag::BZ2Stream::bzerror_
int bzerror_
last error from bzlib
Definition: stream.h:194
rosbag::Stream::getUnusedLength
int getUnusedLength()
Definition: stream.cpp:78
rosbag::BZ2Stream::startRead
void startRead()
Definition: bz2_stream.cpp:103
rosbag::compression::CompressionType
CompressionType
Definition: stream.h:119
rosbag::Stream::getUnused
char * getUnused()
Definition: stream.cpp:77
rosbag::BZ2Stream::decompress
void decompress(uint8_t *dest, unsigned int dest_len, uint8_t *source, unsigned int source_len)
Definition: bz2_stream.cpp:159
rosbag::Stream
Definition: stream.h:100
rosbag::Stream::setUnusedLength
void setUnusedLength(int nUnused)
Definition: stream.cpp:80
rosbag::BZ2Stream::read
void read(void *ptr, size_t size)
Definition: bz2_stream.cpp:117
rosbag::BZ2Stream::startWrite
void startWrite()
Definition: bz2_stream.cpp:58
rosbag
Definition: aes_encryptor.h:43
rosbag::BZ2Stream::work_factor_
int work_factor_
compression behavior for worst case, highly repetitive data (0-250; 30 default)
Definition: stream.h:191
rosbag::Stream::advanceOffset
void advanceOffset(uint64_t nbytes)
Definition: stream.cpp:76
chunked_file.h
rosbag::BZ2Stream::stopRead
void stopRead()
Definition: bz2_stream.cpp:147


rosbag_storage
Author(s): Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:52