|
| 1 | +/* |
| 2 | + * This file implements a version of the lz4 codec where the data is compressed in blocks. |
| 3 | + * This codec adds a 12-byte header that contains the size of the uncompressed data and the block size. |
| 4 | + * Each block contains a 4-byte header that contains the compressed length of that block. |
| 5 | + * This codec is used by the HDF5 library and by Dectris for the Stream2 interface on their detectors. |
| 6 | + * The documentation is here: https://github.com/dectris/HDF5Plugin/blob/master/HDF5_LZ4.pdf |
| 7 | + * This code for this library is derived from the H5Zlz4.c file in the HDF5 External Filter Plugins library. |
| 8 | + * https://github.com/nexusformat/HDF5-External-Filter-Plugins/blob/master/LZ4/src/H5Zlz4.c |
| 9 | + */ |
| 10 | + |
| 11 | +#include <sys/types.h> |
| 12 | +#include <stdint.h> |
| 13 | +#include <stdlib.h> |
| 14 | +#include <string.h> |
| 15 | +#include <stdio.h> |
| 16 | +#if defined(_WIN32) |
| 17 | +#include <winsock2.h> |
| 18 | +#else |
| 19 | +#include <arpa/inet.h> |
| 20 | +#endif |
| 21 | +#include <lz4.h> |
| 22 | +#include <epicsExport.h> |
| 23 | +#include <lz4hdf5.h> |
| 24 | + |
| 25 | +#define htonll(x) ( ( (uint64_t)(htonl( (uint32_t)((x << 32) >> 32)))<< 32) | htonl( ((uint32_t)(x >> 32)) )) |
| 26 | +#define ntohll(x) htonll(x) |
| 27 | + |
| 28 | +#define htobe16t(x) htons(x) |
| 29 | +#define htobe32t(x) htonl(x) |
| 30 | +#define htobe64t(x) htonll(x) |
| 31 | +#define be16toht(x) ntohs(x) |
| 32 | +#define be32toht(x) ntohl(x) |
| 33 | +#define be64toht(x) ntohll(x) |
| 34 | + |
| 35 | + |
| 36 | +#define DEFAULT_BLOCK_SIZE 1<<30; /* 1GB. LZ4 needs blocks < 1.9GB. */ |
| 37 | + |
| 38 | +epicsShareFunc size_t decompress_lz4hdf5(const char *inbuf, char *outbuf, size_t maxOutputSize, size_t *blockSizeOut) |
| 39 | +{ |
| 40 | + size_t ret_value; |
| 41 | + |
| 42 | + uint32_t *i32Buf; |
| 43 | + uint32_t blockSize; |
| 44 | + char *roBuf; /* pointer to current write position */ |
| 45 | + uint64_t decompSize; |
| 46 | + const char* rpos = inbuf; /* pointer to current read position */ |
| 47 | + const uint64_t * const i64Buf = (uint64_t *) rpos; |
| 48 | + const uint64_t origSize = (uint64_t)(be64toht(*i64Buf));/* is saved in be format */ |
| 49 | + if (origSize > maxOutputSize) { |
| 50 | + printf("maxOutputSize=%d too small, needs to be at least %d\n", (int)maxOutputSize, (int)origSize); |
| 51 | + return 0; |
| 52 | + } |
| 53 | + rpos += 8; /* advance the pointer */ |
| 54 | + |
| 55 | + i32Buf = (uint32_t*)rpos; |
| 56 | + blockSize = (uint32_t)(be32toht(*i32Buf)); |
| 57 | + rpos += 4; |
| 58 | + if(blockSize>origSize) |
| 59 | + blockSize = (uint32_t)origSize; |
| 60 | + *blockSizeOut = blockSize; |
| 61 | + roBuf = (char*)outbuf; /* pointer to current write position */ |
| 62 | + decompSize = 0; |
| 63 | + /// start with the first block /// |
| 64 | + while(decompSize < origSize) |
| 65 | + { |
| 66 | + uint32_t compressedBlockSize; /// is saved in be format |
| 67 | + |
| 68 | + if(origSize-decompSize < blockSize) /* the last block can be smaller than blockSize. */ |
| 69 | + blockSize = (uint32_t)(origSize-decompSize); |
| 70 | + i32Buf = (uint32_t*)rpos; |
| 71 | + compressedBlockSize = be32toht(*i32Buf); /// is saved in be format |
| 72 | + rpos += 4; |
| 73 | + if(compressedBlockSize == blockSize) /* there was no compression */ |
| 74 | + { |
| 75 | + memcpy(roBuf, rpos, blockSize); |
| 76 | + } |
| 77 | + else /* do the decompression */ |
| 78 | + { |
| 79 | +#if LZ4_VERSION_NUMBER > 10300 |
| 80 | + int compressedBytes = LZ4_decompress_fast(rpos, roBuf, blockSize); |
| 81 | +#else |
| 82 | + int compressedBytes = LZ4_uncompress(rpos, roBuf, blockSize); |
| 83 | +#endif |
| 84 | + if(compressedBytes != compressedBlockSize) |
| 85 | + { |
| 86 | + printf("decompressed size not the same: %d, != %d\n", compressedBytes, compressedBlockSize); |
| 87 | + return 0; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + rpos += compressedBlockSize; /* advance the read pointer to the next block */ |
| 92 | + roBuf += blockSize; /* advance the write pointer */ |
| 93 | + decompSize += blockSize; |
| 94 | + } |
| 95 | + ret_value = (size_t)origSize; // should always work, as orig_size cannot be > 2GB (sizeof(size_t) < 4GB) |
| 96 | + return ret_value; |
| 97 | +} |
| 98 | + |
| 99 | +epicsShareFunc size_t compress_lz4hdf5(const char *inbuf, char *outbuf, size_t nbytes, size_t maxOutputSize, size_t blockSize) |
| 100 | +{ |
| 101 | + size_t nBlocks; |
| 102 | + size_t outSize; /* size of the output buffer. Header size (12 bytes) is included */ |
| 103 | + size_t block; |
| 104 | + uint64_t *i64Buf; |
| 105 | + uint32_t *i32Buf; |
| 106 | + size_t maxDestSize; |
| 107 | + const char *rpos; /* pointer to current read position */ |
| 108 | + char *roBuf; /* pointer to current write position */ |
| 109 | + |
| 110 | + if (nbytes > INT32_MAX) |
| 111 | + { |
| 112 | + /* can only compress chunks up to 2GB */ |
| 113 | + return 0; |
| 114 | + } |
| 115 | + |
| 116 | + if (blockSize == 0) blockSize = DEFAULT_BLOCK_SIZE; |
| 117 | + if (blockSize > nbytes) |
| 118 | + { |
| 119 | + blockSize = nbytes; |
| 120 | + } |
| 121 | + nBlocks = (nbytes-1)/blockSize +1; |
| 122 | + maxDestSize = LZ4_compressBound((int)nbytes) + 4 + 8 + nBlocks*4; |
| 123 | + if (maxDestSize > maxOutputSize) { |
| 124 | + printf("maxOutputSize=%d too small, needs to be at least %d\n", (int)maxOutputSize, (int)maxDestSize); |
| 125 | + return 0; |
| 126 | + } |
| 127 | + |
| 128 | + rpos = inbuf; /* pointer to current read position */ |
| 129 | + roBuf = outbuf; /* pointer to current write position */ |
| 130 | + /* header */ |
| 131 | + i64Buf = (uint64_t *) (roBuf); |
| 132 | + i64Buf[0] = htobe64t((uint64_t)nbytes); /* Store decompressed size in be format */ |
| 133 | + roBuf += 8; |
| 134 | + |
| 135 | + i32Buf = (uint32_t *) (roBuf); |
| 136 | + i32Buf[0] = htobe32t((uint32_t)blockSize); /* Store the block size in be format */ |
| 137 | + roBuf += 4; |
| 138 | + |
| 139 | + outSize = 12; /* size of the output buffer. Header size (12 bytes) is included */ |
| 140 | + |
| 141 | + for(block = 0; block < nBlocks; ++block) |
| 142 | + { |
| 143 | + uint32_t compBlockSize; /// reserve space for compBlockSize |
| 144 | + size_t origWritten = block*blockSize; |
| 145 | + if(nbytes - origWritten < blockSize) /* the last block may be < blockSize */ |
| 146 | + blockSize = nbytes - origWritten; |
| 147 | + |
| 148 | +#if LZ4_VERSION_NUMBER > 10300 |
| 149 | + compBlockSize = LZ4_compress_default(rpos, roBuf+4, (int)blockSize, (int)(maxDestSize-outSize)); /// reserve space for compBlockSize |
| 150 | +#else |
| 151 | + compBlockSize = LZ4_compress(rpos, roBuf+4, blockSize); /// reserve space for compBlockSize |
| 152 | +#endif |
| 153 | + if(!compBlockSize) |
| 154 | + return 0; |
| 155 | + if(compBlockSize >= blockSize) /* compression did not save any space, do a memcpy instead */ |
| 156 | + { |
| 157 | + compBlockSize = (uint32_t)blockSize; |
| 158 | + memcpy(roBuf+4, rpos, blockSize); |
| 159 | + } |
| 160 | + |
| 161 | + i32Buf = (uint32_t *) (roBuf); |
| 162 | + i32Buf[0] = htobe32t((uint32_t)compBlockSize); /* write blocksize */ |
| 163 | + roBuf += 4; |
| 164 | + |
| 165 | + rpos += blockSize; /* advance read pointer */ |
| 166 | + roBuf += compBlockSize; /* advance write pointer */ |
| 167 | + outSize += compBlockSize + 4; |
| 168 | + } |
| 169 | + |
| 170 | + return outSize; |
| 171 | + |
| 172 | +} |
| 173 | + |
0 commit comments