$search
00001 #include <bzlib.h> 00002 #include <boost/shared_array.hpp> 00003 #include <ros/ros.h> 00004 00005 00006 #include "bz2_transport/bz2_codec.h" 00007 00008 namespace bz2_transport { 00009 00010 00011 bool BZ2Codec::compress(const boost::shared_array<uint8_t> &buffer, size_t len, BZ2Packet & out) const { 00012 int ret = 0; 00013 // Size following recommendation in bzip2 manual 101% + 600 bytes 00014 uint32_t destLen = (102*len)/100 + 600; 00015 boost::shared_array<uint8_t> buf(new uint8_t[destLen]); 00016 // Removed the first 4 bytes corresponding the data length 00017 ret = BZ2_bzBuffToBuffCompress((char*)buf.get(),&destLen, (char*)buffer.get(), len, 5, 0, 30); 00018 if (ret != BZ_OK) { 00019 ROS_ERROR("BZ2_bzBuffToBuffCompress return %d",ret); 00020 return false; 00021 } 00022 out.original_length = len; 00023 out.buffer.resize(destLen); 00024 // Could use memcpy, but probably less portable 00025 memcpy(&(out.buffer.front()),buf.get(),destLen); 00026 printf("Message compression: from %d to %d bytes\n",(int)len,(int)destLen); 00027 return true; 00028 } 00029 00030 bool BZ2Codec::decompress(const BZ2Packet & in, 00031 boost::shared_array<uint8_t> &buffer, size_t &len) const { 00032 int ret = 0; 00033 unsigned int destLen; 00034 buffer.reset(new uint8_t[in.original_length]); 00035 ret = BZ2_bzBuffToBuffDecompress((char*)buffer.get(),&destLen, (char*)&(in.buffer.front()), in.buffer.size(), 0, 0); 00036 if (ret != BZ_OK) { 00037 ROS_ERROR("BZ2_bzBuffToBuffDecompress return %d",ret); 00038 return false; 00039 } 00040 len = destLen; 00041 return true; 00042 } 00043 00044 };