compress 'size' bytes at 'data' into 'outputBuffer' compressionLevel, compressor & threadCount are passed directly to blosc ( see blosc.h ) if 'size' is greater than the max buffer blosc can handle we split into a number of independently compressed blocks. returns the number of compression blocks 'outputBuffer' contains the compressed block data and is resized in this function. 'maxBlockSize' is
| 409 | /// 'outputBuffer' contains the compressed block data and is resized in this function. |
| 410 | /// 'maxBlockSize' is useful for testing the compression block size without using buffers greater than 2GB |
| 411 | size_t compress( |
| 412 | const char *data, |
| 413 | size_t size, |
| 414 | std::vector<char> &outputBuffer, |
| 415 | int compressionLevel, |
| 416 | const std::string &compressor, |
| 417 | int threadCount, |
| 418 | std::optional<size_t> maxBlockSize = std::optional<size_t>(), |
| 419 | size_t minCompressedBlockSize = 1024U |
| 420 | ) |
| 421 | { |
| 422 | const size_t maxCompressedBlockSize = maxBlockSize.value_or( BLOSC_MAX_BUFFERSIZE ); |
| 423 | |
| 424 | if( size < minCompressedBlockSize ) |
| 425 | { |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | size_t bytesToCompress = size; |
| 430 | const char *currentBlockCompressed = data; |
| 431 | |
| 432 | size_t numBlocks = 0; |
| 433 | |
| 434 | /// this isn't enough space in some edge cases but is sufficient in the common case |
| 435 | /// and we check if we have enough size in the compression loop |
| 436 | outputBuffer.resize( size + BLOSC_MAX_OVERHEAD ); |
| 437 | char *writePtr = outputBuffer.data(); |
| 438 | size_t writerBufferBytes = outputBuffer.size(); |
| 439 | |
| 440 | size_t totalCompressedSize = 0; |
| 441 | |
| 442 | while ( bytesToCompress ) |
| 443 | { |
| 444 | size_t currentBlockUncompressedSize = std::min( maxCompressedBlockSize, bytesToCompress ); |
| 445 | size_t compressedBufferMaxSize = currentBlockUncompressedSize + BLOSC_MAX_OVERHEAD; |
| 446 | if( writerBufferBytes < compressedBufferMaxSize ) |
| 447 | { |
| 448 | size_t additionalBytes = (size_t) ( compressedBufferMaxSize - writerBufferBytes ); |
| 449 | outputBuffer.resize( outputBuffer.size() + additionalBytes ); |
| 450 | } |
| 451 | |
| 452 | int compressedSize = blosc_compress_ctx( |
| 453 | compressionLevel, |
| 454 | true, |
| 455 | 4, |
| 456 | currentBlockUncompressedSize, |
| 457 | currentBlockCompressed, |
| 458 | writePtr, |
| 459 | compressedBufferMaxSize, |
| 460 | compressor.c_str(), |
| 461 | 0, |
| 462 | threadCount |
| 463 | ); |
| 464 | |
| 465 | if ( compressedSize < 0 ) |
| 466 | { |
| 467 | outputBuffer.clear(); |
| 468 | return 0; |
no test coverage detected