lz4_stream.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2014, Ben Charrow
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), block_size_id_(6) {
48  buff_ = new char[buff_size_];
49 }
50 
52  delete[] buff_;
53 }
54 
56  return compression::LZ4;
57 }
58 
60  setCompressedIn(0);
61 
63  switch(ret) {
64  case ROSLZ4_OK: break;
65  case ROSLZ4_MEMORY_ERROR: throw BagIOException("ROSLZ4_MEMORY_ERROR: insufficient memory available"); break;
66  case ROSLZ4_PARAM_ERROR: throw BagIOException("ROSLZ4_PARAM_ERROR: bad block size"); break;
67  default: throw BagException("Unhandled return code");
68  }
71 }
72 
73 void LZ4Stream::write(void* ptr, size_t size) {
74  lz4s_.input_left = static_cast<int>(size);
75  lz4s_.input_next = (char*) ptr;
76 
79 }
80 
82  int ret = ROSLZ4_OK;
83  while (lz4s_.input_left > 0 ||
84  (action == ROSLZ4_FINISH && ret != ROSLZ4_STREAM_END)) {
85  ret = roslz4_compress(&lz4s_, action);
86  switch(ret) {
87  case ROSLZ4_OK: break;
89  if (lz4s_.output_next - buff_ == buff_size_) {
90  throw BagIOException("ROSLZ4_OUTPUT_SMALL: output buffer is too small");
91  } else {
92  // There's data to be written in buff_; this will free up space
93  break;
94  }
95  case ROSLZ4_STREAM_END: break;
96  case ROSLZ4_PARAM_ERROR: throw BagIOException("ROSLZ4_PARAM_ERROR: bad block size"); break;
97  case ROSLZ4_ERROR: throw BagIOException("ROSLZ4_ERROR: compression error"); break;
98  default: throw BagException("Unhandled return code");
99  }
100 
101  // If output data is ready, write to disk
102  int to_write = static_cast<int>(lz4s_.output_next - buff_);
103  if (to_write > 0) {
104  if (fwrite(buff_, 1, to_write, getFilePointer()) != static_cast<size_t>(to_write)) {
105  throw BagException("Problem writing data to disk");
106  }
107  advanceOffset(to_write);
110  }
111  }
112 }
113 
116  setCompressedIn(0);
118 }
119 
121  int ret = static_cast<int>(roslz4_decompressStart(&lz4s_));
122  switch(ret) {
123  case ROSLZ4_OK: break;
124  case ROSLZ4_MEMORY_ERROR: throw BagException("ROSLZ4_MEMORY_ERROR: insufficient memory available"); break;
125  default: throw BagException("Unhandled return code");
126  }
127 
128  if (getUnusedLength() > buff_size_) {
129  throw BagException("Too many unused bytes to decompress");
130  }
131 
132  // getUnused() could be pointing to part of buff_, so don't use memcpy
133  memmove(buff_, getUnused(), getUnusedLength());
136  clearUnused();
137 }
138 
139 void LZ4Stream::read(void* ptr, size_t size) {
140  // Setup stream by filling buffer with data from file
141  int to_read = buff_size_ - lz4s_.input_left;
142  char *input_start = buff_ + lz4s_.input_left;
143  int nread = static_cast<int>(fread(input_start, 1, to_read, getFilePointer()));
144  if (ferror(getFilePointer())) {
145  throw BagIOException("Problem reading from file");
146  }
148  lz4s_.input_left += nread;
149  lz4s_.output_next = (char*) ptr;
150  lz4s_.output_left = static_cast<int>(size);
151 
152  // Decompress. If reach end of stream, store unused data
153  int ret = roslz4_decompress(&lz4s_);
154  switch (ret) {
155  case ROSLZ4_OK: break;
156  case ROSLZ4_STREAM_END:
157  if (getUnused() || getUnusedLength() > 0)
158  CONSOLE_BRIDGE_logError("%s", "unused data already available");
159  else {
162  }
163  return;
164  case ROSLZ4_ERROR: throw BagException("ROSLZ4_ERROR: decompression error"); break;
165  case ROSLZ4_MEMORY_ERROR: throw BagException("ROSLZ4_MEMORY_ERROR: insufficient memory available"); break;
166  case ROSLZ4_OUTPUT_SMALL: throw BagException("ROSLZ4_OUTPUT_SMALL: output buffer is too small"); break;
167  case ROSLZ4_DATA_ERROR: throw BagException("ROSLZ4_DATA_ERROR: malformed data to decompress"); break;
168  default: throw BagException("Unhandled return code");
169  }
170  if (feof(getFilePointer())) {
171  throw BagIOException("Reached end of file before reaching end of stream");
172  }
173 
174  size_t total_out = lz4s_.output_next - (char*)ptr;
175  advanceOffset(total_out);
176 
177  // Shift input buffer if there's unconsumed data
178  if (lz4s_.input_left > 0) {
180  }
181 }
182 
185 }
186 
187 void LZ4Stream::decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len) {
188  unsigned int actual_dest_len = dest_len;
189  int ret = roslz4_buffToBuffDecompress((char*)source, source_len,
190  (char*)dest, &actual_dest_len);
191  switch(ret) {
192  case ROSLZ4_OK: break;
193  case ROSLZ4_ERROR: throw BagException("ROSLZ4_ERROR: decompression error"); break;
194  case ROSLZ4_MEMORY_ERROR: throw BagException("ROSLZ4_MEMORY_ERROR: insufficient memory available"); break;
195  case ROSLZ4_OUTPUT_SMALL: throw BagException("ROSLZ4_OUTPUT_SMALL: output buffer is too small"); break;
196  case ROSLZ4_DATA_ERROR: throw BagException("ROSLZ4_DATA_ERROR: malformed data to decompress"); break;
197  default: throw BagException("Unhandled return code");
198  }
199  if (actual_dest_len != dest_len) {
200  throw BagException("Decompression size mismatch in LZ4 chunk");
201  }
202 }
203 
204 } // namespace rosbag
void roslz4_compressEnd(roslz4_stream *stream)
Definition: lz4s.c:345
const int ROSLZ4_MEMORY_ERROR
Definition: lz4s.h:45
LZ4Stream(ChunkedFile *file)
Definition: lz4_stream.cpp:45
ChunkedFile reads and writes files which contain interleaved chunks of compressed and uncompressed da...
Definition: chunked_file.h:49
int roslz4_blockSizeFromIndex(int block_id)
Definition: lz4s.c:299
int roslz4_compress(roslz4_stream *stream, int action)
Definition: lz4s.c:309
Base class for rosbag exceptions.
Definition: exceptions.h:43
const int ROSLZ4_OUTPUT_SMALL
Definition: lz4s.h:48
GLsizei const GLchar *const * string
unsigned char uint8_t
Definition: stdint.h:78
void read(void *ptr, size_t size)
Definition: lz4_stream.cpp:139
int roslz4_decompressStart(roslz4_stream *stream)
Definition: lz4s.c:351
void write(void *ptr, size_t size)
Definition: lz4_stream.cpp:73
void decompress(uint8_t *dest, unsigned int dest_len, uint8_t *source, unsigned int source_len)
Definition: lz4_stream.cpp:187
int roslz4_buffToBuffDecompress(char *input, unsigned int input_size, char *output, unsigned int *output_size)
Definition: lz4s.c:596
const int ROSLZ4_STREAM_END
Definition: lz4s.h:51
int input_left
Definition: lz4s.h:59
GLsizeiptr size
const int ROSLZ4_PARAM_ERROR
Definition: lz4s.h:46
void roslz4_decompressEnd(roslz4_stream *str)
Definition: lz4s.c:559
Exception thrown when on IO problems.
Definition: exceptions.h:50
char * output_next
Definition: lz4s.h:61
char * input_next
Definition: lz4s.h:58
const int ROSLZ4_DATA_ERROR
Definition: lz4s.h:47
const int ROSLZ4_OK
Definition: lz4s.h:50
action
Definition: enums.py:62
int roslz4_compressStart(roslz4_stream *stream, int block_size_id)
Definition: lz4s.c:303
const int ROSLZ4_FINISH
Definition: lz4s.h:55
int roslz4_decompress(roslz4_stream *stream)
Definition: lz4s.c:528
Definition: bag.h:66
const int ROSLZ4_ERROR
Definition: lz4s.h:49
void writeStream(int action)
Definition: lz4_stream.cpp:81
GLsizei GLsizei GLchar * source
LZ4LIB_API char * dest
Definition: lz4.h:438
const int ROSLZ4_RUN
Definition: lz4s.h:54
int output_left
Definition: lz4s.h:62
CompressionType getCompressionType() const
Definition: lz4_stream.cpp:55
#define CONSOLE_BRIDGE_logError(fmt,...)
Definition: console.h:78


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:21