@ -616,26 +616,30 @@ inline bool Snappy_Compress(const CompressionInfo& /*info*/, const char* input,
# endif
}
inline bool Snappy_GetUncompressedLength ( const char * input , size_t length ,
size_t * result ) {
inline CacheAllocationPtr Snappy_Uncompress (
const char * input , size_t length , size_t * uncompressed_size ,
MemoryAllocator * allocator = nullptr ) {
# ifdef SNAPPY
return snappy : : GetUncompressedLength ( input , length , result ) ;
# else
( void ) input ;
( void ) length ;
( void ) result ;
return false ;
# endif
}
size_t uncompressed_length = 0 ;
if ( ! snappy : : GetUncompressedLength ( input , length , & uncompressed_length ) ) {
return nullptr ;
}
inline bool Snappy_Uncompress ( const char * input , size_t length , char * output ) {
# ifdef SNAPPY
return snappy : : RawUncompress ( input , length , output ) ;
CacheAllocationPtr output = AllocateBlock ( uncompressed_length , allocator ) ;
if ( ! snappy : : RawUncompress ( input , length , output . get ( ) ) ) {
return nullptr ;
}
* uncompressed_size = uncompressed_length ;
return output ;
# else
( void ) input ;
( void ) length ;
( void ) output ;
return false ;
( void ) uncompressed_size ;
( void ) allocator ;
return nullptr ;
# endif
}
@ -754,7 +758,7 @@ inline bool Zlib_Compress(const CompressionInfo& info,
// dictionary.
inline CacheAllocationPtr Zlib_Uncompress (
const UncompressionInfo & info , const char * input_data , size_t input_length ,
int * decompress _size, uint32_t compress_format_version ,
size_t * uncompressed _size, uint32_t compress_format_version ,
MemoryAllocator * allocator = nullptr , int windowBits = - 14 ) {
# ifdef ZLIB
uint32_t output_len = 0 ;
@ -836,14 +840,15 @@ inline CacheAllocationPtr Zlib_Uncompress(
// If we encoded decompressed block size, we should have no bytes left
assert ( compress_format_version ! = 2 | | _stream . avail_out = = 0 ) ;
* decompress_size = static_cast < int > ( output_len - _stream . avail_out ) ;
assert ( output_len > = _stream . avail_out ) ;
* uncompressed_size = output_len - _stream . avail_out ;
inflateEnd ( & _stream ) ;
return output ;
# else
( void ) info ;
( void ) input_data ;
( void ) input_length ;
( void ) decompress _size;
( void ) uncompressed _size;
( void ) compress_format_version ;
( void ) allocator ;
( void ) windowBits ;
@ -917,7 +922,7 @@ inline bool BZip2_Compress(const CompressionInfo& /*info*/,
// compress_format_version == 2 -- decompressed size is included in the block
// header in varint32 format
inline CacheAllocationPtr BZip2_Uncompress (
const char * input_data , size_t input_length , int * decompress _size,
const char * input_data , size_t input_length , size_t * uncompressed _size,
uint32_t compress_format_version , MemoryAllocator * allocator = nullptr ) {
# ifdef BZIP2
uint32_t output_len = 0 ;
@ -982,13 +987,14 @@ inline CacheAllocationPtr BZip2_Uncompress(
// If we encoded decompressed block size, we should have no bytes left
assert ( compress_format_version ! = 2 | | _stream . avail_out = = 0 ) ;
* decompress_size = static_cast < int > ( output_len - _stream . avail_out ) ;
assert ( output_len > = _stream . avail_out ) ;
* uncompressed_size = output_len - _stream . avail_out ;
BZ2_bzDecompressEnd ( & _stream ) ;
return output ;
# else
( void ) input_data ;
( void ) input_length ;
( void ) decompress _size;
( void ) uncompressed _size;
( void ) compress_format_version ;
( void ) allocator ;
return nullptr ;
@ -1073,7 +1079,7 @@ inline bool LZ4_Compress(const CompressionInfo& info,
inline CacheAllocationPtr LZ4_Uncompress ( const UncompressionInfo & info ,
const char * input_data ,
size_t input_length ,
int * decompress _size,
size_t * uncompressed _size,
uint32_t compress_format_version ,
MemoryAllocator * allocator = nullptr ) {
# ifdef LZ4
@ -1096,6 +1102,9 @@ inline CacheAllocationPtr LZ4_Uncompress(const UncompressionInfo& info,
}
auto output = AllocateBlock ( output_len , allocator ) ;
int decompress_bytes = 0 ;
# if LZ4_VERSION_NUMBER >= 10400 // r124+
LZ4_streamDecode_t * stream = LZ4_createStreamDecode ( ) ;
const Slice & compression_dict = info . dict ( ) . GetRawDict ( ) ;
@ -1103,26 +1112,27 @@ inline CacheAllocationPtr LZ4_Uncompress(const UncompressionInfo& info,
LZ4_setStreamDecode ( stream , compression_dict . data ( ) ,
static_cast < int > ( compression_dict . size ( ) ) ) ;
}
* decompress_size = LZ4_decompress_safe_continue (
decompress_byte s = LZ4_decompress_safe_continue (
stream , input_data , output . get ( ) , static_cast < int > ( input_length ) ,
static_cast < int > ( output_len ) ) ;
LZ4_freeStreamDecode ( stream ) ;
# else // up to r123
* decompress_size = LZ4_decompress_safe ( input_data , output . get ( ) ,
decompress_byte s = LZ4_decompress_safe ( input_data , output . get ( ) ,
static_cast < int > ( input_length ) ,
static_cast < int > ( output_len ) ) ;
# endif // LZ4_VERSION_NUMBER >= 10400
if ( * decompress_size < 0 ) {
if ( decompress_byte s < 0 ) {
return nullptr ;
}
assert ( * decompress_size = = static_cast < int > ( output_len ) ) ;
assert ( decompress_bytes = = static_cast < int > ( output_len ) ) ;
* uncompressed_size = decompress_bytes ;
return output ;
# else // LZ4
( void ) info ;
( void ) input_data ;
( void ) input_length ;
( void ) decompress _size;
( void ) uncompressed _size;
( void ) compress_format_version ;
( void ) allocator ;
return nullptr ;
@ -1227,13 +1237,13 @@ inline bool XPRESS_Compress(const char* /*input*/, size_t /*length*/,
# ifdef XPRESS
inline char * XPRESS_Uncompress ( const char * input_data , size_t input_length ,
int * decompress _size) {
return port : : xpress : : Decompress ( input_data , input_length , decompress _size) ;
size_t * uncompressed _size) {
return port : : xpress : : Decompress ( input_data , input_length , uncompressed _size) ;
}
# else
inline char * XPRESS_Uncompress ( const char * /*input_data*/ ,
size_t /*input_length*/ ,
int * /*decompress _size*/) {
size_t * /*uncompressed _size*/) {
return nullptr ;
}
# endif
@ -1298,7 +1308,7 @@ inline bool ZSTD_Compress(const CompressionInfo& info, const char* input,
// dictionary.
inline CacheAllocationPtr ZSTD_Uncompress (
const UncompressionInfo & info , const char * input_data , size_t input_length ,
int * decompress _size, MemoryAllocator * allocator = nullptr ) {
size_t * uncompressed _size, MemoryAllocator * allocator = nullptr ) {
# ifdef ZSTD
uint32_t output_len = 0 ;
if ( ! compression : : GetDecompressedSizeInfo ( & input_data , & input_length ,
@ -1329,13 +1339,13 @@ inline CacheAllocationPtr ZSTD_Uncompress(
ZSTD_decompress ( output . get ( ) , output_len , input_data , input_length ) ;
# endif // ZSTD_VERSION_NUMBER >= 500
assert ( actual_output_length = = output_len ) ;
* decompress _size = static_cast < int > ( actual_output_length ) ;
* uncompressed _size = actual_output_length ;
return output ;
# else // ZSTD
( void ) info ;
( void ) input_data ;
( void ) input_length ;
( void ) decompress _size;
( void ) uncompressed _size;
( void ) allocator ;
return nullptr ;
# endif
@ -1449,4 +1459,34 @@ inline bool CompressData(const Slice& raw,
return ret ;
}
inline CacheAllocationPtr UncompressData (
const UncompressionInfo & uncompression_info , const char * data , size_t n ,
size_t * uncompressed_size , uint32_t compress_format_version ,
MemoryAllocator * allocator = nullptr ) {
switch ( uncompression_info . type ( ) ) {
case kSnappyCompression :
return Snappy_Uncompress ( data , n , uncompressed_size , allocator ) ;
case kZlibCompression :
return Zlib_Uncompress ( uncompression_info , data , n , uncompressed_size ,
compress_format_version , allocator ) ;
case kBZip2Compression :
return BZip2_Uncompress ( data , n , uncompressed_size ,
compress_format_version , allocator ) ;
case kLZ4Compression :
case kLZ4HCCompression :
return LZ4_Uncompress ( uncompression_info , data , n , uncompressed_size ,
compress_format_version , allocator ) ;
case kXpressCompression :
// XPRESS allocates memory internally, thus no support for custom
// allocator.
return CacheAllocationPtr ( XPRESS_Uncompress ( data , n , uncompressed_size ) ) ;
case kZSTD :
case kZSTDNotFinalCompression :
return ZSTD_Uncompress ( uncompression_info , data , n , uncompressed_size ,
allocator ) ;
default :
return CacheAllocationPtr ( ) ;
}
}
} // namespace ROCKSDB_NAMESPACE