| 1 |
/* |
| 2 |
* Bitshuffle - Filter for improving compression of typed binary data. |
| 3 |
* |
| 4 |
* Author: Kiyoshi Masui <[email protected]> |
| 5 |
* Website: https://www.github.com/kiyo-masui/bitshuffle |
| 6 |
* Created: 2014 |
| 7 |
* |
| 8 |
* See LICENSE file for details about copyright and rights to use. |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "bitshuffle.h" |
| 13 |
#include "bitshuffle_core.h" |
| 14 |
#include "bitshuffle_internals.h" |
| 15 |
#include "lz4.h" |
| 16 |
|
| 17 |
#ifdef ZSTD_SUPPORT |
| 18 |
#include "zstd.h" |
| 19 |
#endif |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
|
| 25 |
// Macros. |
| 26 |
#define CHECK_ERR_FREE_LZ(count, buf) \ |
| 27 |
do { \ |
| 28 |
if ((count) < 0) { \ |
| 29 |
free(buf); \ |
| 30 |
return (count) - 1000; \ |
| 31 |
} \ |
| 32 |
} while (0) |
| 33 |
|
| 34 |
|
| 35 |
/* Bitshuffle and compress a single block. */ |
| 36 |
int64_t bshuf_compress_lz4_block(ioc_chain *C_ptr, |
| 37 |
const size_t size, const size_t elem_size, const int option) { |
| 38 |
|
| 39 |
int64_t nbytes, count; |
| 40 |
void *tmp_buf_bshuf; |
| 41 |
void *tmp_buf_lz4; |
| 42 |
size_t this_iter; |
| 43 |
const void *in; |
| 44 |
void *out; |
| 45 |
|
| 46 |
tmp_buf_bshuf = malloc(size * elem_size); |
| 47 |
if (tmp_buf_bshuf == NULL) return -1; |
| 48 |
|
| 49 |
int dst_capacity = LZ4_compressBound(size * elem_size); |
| 50 |
tmp_buf_lz4 = malloc(dst_capacity); |
| 51 |
if (tmp_buf_lz4 == NULL){ |
| 52 |
free(tmp_buf_bshuf); |
| 53 |
return -1; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
in = ioc_get_in(C_ptr, &this_iter); |
| 58 |
ioc_set_next_in(C_ptr, &this_iter, (void*) ((char*) in + size * elem_size)); |
| 59 |
|
| 60 |
count = bshuf_trans_bit_elem(in, tmp_buf_bshuf, size, elem_size); |
| 61 |
if (count < 0) { |
| 62 |
free(tmp_buf_lz4); |
| 63 |
free(tmp_buf_bshuf); |
| 64 |
return count; |
| 65 |
} |
| 66 |
nbytes = LZ4_compress_default((const char*) tmp_buf_bshuf, (char*) tmp_buf_lz4, size * elem_size, dst_capacity); |
| 67 |
free(tmp_buf_bshuf); |
| 68 |
CHECK_ERR_FREE_LZ(nbytes, tmp_buf_lz4); |
| 69 |
|
| 70 |
out = ioc_get_out(C_ptr, &this_iter); |
| 71 |
ioc_set_next_out(C_ptr, &this_iter, (void *) ((char *) out + nbytes + 4)); |
| 72 |
|
| 73 |
bshuf_write_uint32_BE(out, nbytes); |
| 74 |
memcpy((char *) out + 4, tmp_buf_lz4, nbytes); |
| 75 |
|
| 76 |
free(tmp_buf_lz4); |
| 77 |
|
| 78 |
return nbytes + 4; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
/* Decompress and bitunshuffle a single block. */ |
| 83 |
int64_t bshuf_decompress_lz4_block(ioc_chain *C_ptr, |
| 84 |
const size_t size, const size_t elem_size, const int option) { |
| 85 |
|
| 86 |
int64_t nbytes, count; |
| 87 |
void *out, *tmp_buf; |
| 88 |
const void *in; |
| 89 |
size_t this_iter; |
| 90 |
int32_t nbytes_from_header; |
| 91 |
|
| 92 |
in = ioc_get_in(C_ptr, &this_iter); |
| 93 |
nbytes_from_header = bshuf_read_uint32_BE(in); |
| 94 |
ioc_set_next_in(C_ptr, &this_iter, |
| 95 |
(void*) ((char*) in + nbytes_from_header + 4)); |
| 96 |
|
| 97 |
out = ioc_get_out(C_ptr, &this_iter); |
| 98 |
ioc_set_next_out(C_ptr, &this_iter, |
| 99 |
(void *) ((char *) out + size * elem_size)); |
| 100 |
|
| 101 |
tmp_buf = malloc(size * elem_size); |
| 102 |
if (tmp_buf == NULL) return -1; |
| 103 |
|
| 104 |
nbytes = LZ4_decompress_safe((const char*) in + 4, (char *) tmp_buf, nbytes_from_header, |
| 105 |
size * elem_size); |
| 106 |
CHECK_ERR_FREE_LZ(nbytes, tmp_buf); |
| 107 |
if (nbytes != size * elem_size) { |
| 108 |
free(tmp_buf); |
| 109 |
return -91; |
| 110 |
} |
| 111 |
nbytes = nbytes_from_header; |
| 112 |
|
| 113 |
count = bshuf_untrans_bit_elem(tmp_buf, out, size, elem_size); |
| 114 |
CHECK_ERR_FREE(count, tmp_buf); |
| 115 |
nbytes += 4; |
| 116 |
|
| 117 |
free(tmp_buf); |
| 118 |
return nbytes; |
| 119 |
} |
| 120 |
|
| 121 |
#ifdef ZSTD_SUPPORT |
| 122 |
/* Bitshuffle and compress a single block. */ |
| 123 |
int64_t bshuf_compress_zstd_block(ioc_chain *C_ptr, |
| 124 |
const size_t size, const size_t elem_size, const int comp_lvl) { |
| 125 |
|
| 126 |
int64_t nbytes, count; |
| 127 |
void *tmp_buf_bshuf; |
| 128 |
void *tmp_buf_zstd; |
| 129 |
size_t this_iter; |
| 130 |
const void *in; |
| 131 |
void *out; |
| 132 |
|
| 133 |
tmp_buf_bshuf = malloc(size * elem_size); |
| 134 |
if (tmp_buf_bshuf == NULL) return -1; |
| 135 |
|
| 136 |
size_t tmp_buf_zstd_size = ZSTD_compressBound(size * elem_size); |
| 137 |
tmp_buf_zstd = malloc(tmp_buf_zstd_size); |
| 138 |
if (tmp_buf_zstd == NULL){ |
| 139 |
free(tmp_buf_bshuf); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
in = ioc_get_in(C_ptr, &this_iter); |
| 144 |
ioc_set_next_in(C_ptr, &this_iter, (void*) ((char*) in + size * elem_size)); |
| 145 |
|
| 146 |
count = bshuf_trans_bit_elem(in, tmp_buf_bshuf, size, elem_size); |
| 147 |
if (count < 0) { |
| 148 |
free(tmp_buf_zstd); |
| 149 |
free(tmp_buf_bshuf); |
| 150 |
return count; |
| 151 |
} |
| 152 |
nbytes = ZSTD_compress(tmp_buf_zstd, tmp_buf_zstd_size, (const void*)tmp_buf_bshuf, size * elem_size, comp_lvl); |
| 153 |
free(tmp_buf_bshuf); |
| 154 |
CHECK_ERR_FREE_LZ(nbytes, tmp_buf_zstd); |
| 155 |
|
| 156 |
out = ioc_get_out(C_ptr, &this_iter); |
| 157 |
ioc_set_next_out(C_ptr, &this_iter, (void *) ((char *) out + nbytes + 4)); |
| 158 |
|
| 159 |
bshuf_write_uint32_BE(out, nbytes); |
| 160 |
memcpy((char *) out + 4, tmp_buf_zstd, nbytes); |
| 161 |
|
| 162 |
free(tmp_buf_zstd); |
| 163 |
|
| 164 |
return nbytes + 4; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
/* Decompress and bitunshuffle a single block. */ |
| 169 |
int64_t bshuf_decompress_zstd_block(ioc_chain *C_ptr, |
| 170 |
const size_t size, const size_t elem_size, const int option) { |
| 171 |
|
| 172 |
int64_t nbytes, count; |
| 173 |
void *out, *tmp_buf; |
| 174 |
const void *in; |
| 175 |
size_t this_iter; |
| 176 |
int32_t nbytes_from_header; |
| 177 |
|
| 178 |
in = ioc_get_in(C_ptr, &this_iter); |
| 179 |
nbytes_from_header = bshuf_read_uint32_BE(in); |
| 180 |
ioc_set_next_in(C_ptr, &this_iter, |
| 181 |
(void*) ((char*) in + nbytes_from_header + 4)); |
| 182 |
|
| 183 |
out = ioc_get_out(C_ptr, &this_iter); |
| 184 |
ioc_set_next_out(C_ptr, &this_iter, |
| 185 |
(void *) ((char *) out + size * elem_size)); |
| 186 |
|
| 187 |
tmp_buf = malloc(size * elem_size); |
| 188 |
if (tmp_buf == NULL) return -1; |
| 189 |
|
| 190 |
nbytes = ZSTD_decompress(tmp_buf, size * elem_size, (void *)((char *) in + 4), nbytes_from_header); |
| 191 |
CHECK_ERR_FREE_LZ(nbytes, tmp_buf); |
| 192 |
if (nbytes != size * elem_size) { |
| 193 |
free(tmp_buf); |
| 194 |
return -91; |
| 195 |
} |
| 196 |
|
| 197 |
nbytes = nbytes_from_header; |
| 198 |
count = bshuf_untrans_bit_elem(tmp_buf, out, size, elem_size); |
| 199 |
CHECK_ERR_FREE(count, tmp_buf); |
| 200 |
nbytes += 4; |
| 201 |
|
| 202 |
free(tmp_buf); |
| 203 |
return nbytes; |
| 204 |
} |
| 205 |
#endif // ZSTD_SUPPORT |
| 206 |
|
| 207 |
|
| 208 |
/* ---- Public functions ---- |
| 209 |
* |
| 210 |
* See header file for description and usage. |
| 211 |
* |
| 212 |
*/ |
| 213 |
|
| 214 |
size_t bshuf_compress_lz4_bound(const size_t size, |
| 215 |
const size_t elem_size, size_t block_size) { |
| 216 |
|
| 217 |
size_t bound, leftover; |
| 218 |
|
| 219 |
if (block_size == 0) { |
| 220 |
block_size = bshuf_default_block_size(elem_size); |
| 221 |
} |
| 222 |
if (block_size % BSHUF_BLOCKED_MULT) return -81; |
| 223 |
|
| 224 |
// Note that each block gets a 4 byte header. |
| 225 |
// Size of full blocks. |
| 226 |
bound = (LZ4_compressBound(block_size * elem_size) + 4) * (size / block_size); |
| 227 |
// Size of partial blocks, if any. |
| 228 |
leftover = ((size % block_size) / BSHUF_BLOCKED_MULT) * BSHUF_BLOCKED_MULT; |
| 229 |
if (leftover) bound += LZ4_compressBound(leftover * elem_size) + 4; |
| 230 |
// Size of uncompressed data not fitting into any blocks. |
| 231 |
bound += (size % BSHUF_BLOCKED_MULT) * elem_size; |
| 232 |
return bound; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
int64_t bshuf_compress_lz4(const void* in, void* out, const size_t size, |
| 237 |
const size_t elem_size, size_t block_size) { |
| 238 |
return bshuf_blocked_wrap_fun(&bshuf_compress_lz4_block, in, out, size, |
| 239 |
elem_size, block_size, 0/*option*/); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
int64_t bshuf_decompress_lz4(const void* in, void* out, const size_t size, |
| 244 |
const size_t elem_size, size_t block_size) { |
| 245 |
return bshuf_blocked_wrap_fun(&bshuf_decompress_lz4_block, in, out, size, |
| 246 |
elem_size, block_size, 0/*option*/); |
| 247 |
} |
| 248 |
|
| 249 |
#ifdef ZSTD_SUPPORT |
| 250 |
size_t bshuf_compress_zstd_bound(const size_t size, |
| 251 |
const size_t elem_size, size_t block_size) { |
| 252 |
|
| 253 |
size_t bound, leftover; |
| 254 |
|
| 255 |
if (block_size == 0) { |
| 256 |
block_size = bshuf_default_block_size(elem_size); |
| 257 |
} |
| 258 |
if (block_size % BSHUF_BLOCKED_MULT) return -81; |
| 259 |
|
| 260 |
// Note that each block gets a 4 byte header. |
| 261 |
// Size of full blocks. |
| 262 |
bound = (ZSTD_compressBound(block_size * elem_size) + 4) * (size / block_size); |
| 263 |
// Size of partial blocks, if any. |
| 264 |
leftover = ((size % block_size) / BSHUF_BLOCKED_MULT) * BSHUF_BLOCKED_MULT; |
| 265 |
if (leftover) bound += ZSTD_compressBound(leftover * elem_size) + 4; |
| 266 |
// Size of uncompressed data not fitting into any blocks. |
| 267 |
bound += (size % BSHUF_BLOCKED_MULT) * elem_size; |
| 268 |
return bound; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
int64_t bshuf_compress_zstd(const void* in, void* out, const size_t size, |
| 273 |
const size_t elem_size, size_t block_size, const int comp_lvl) { |
| 274 |
return bshuf_blocked_wrap_fun(&bshuf_compress_zstd_block, in, out, size, |
| 275 |
elem_size, block_size, comp_lvl); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
int64_t bshuf_decompress_zstd(const void* in, void* out, const size_t size, |
| 280 |
const size_t elem_size, size_t block_size) { |
| 281 |
return bshuf_blocked_wrap_fun(&bshuf_decompress_zstd_block, in, out, size, |
| 282 |
elem_size, block_size, 0/*option*/); |
| 283 |
} |
| 284 |
#endif // ZSTD_SUPPORT |