Skip to content

Commit 55e9b06

Browse files
committed
Re-add the check for small buffers and a new test (see Blosc/python-blosc2#46)
1 parent 3c63098 commit 55e9b06

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

blosc/blosc2.c

+4
Original file line numberDiff line numberDiff line change
@@ -2220,6 +2220,10 @@ static int write_compression_header(blosc2_context* context, bool extended_heade
22202220
/* Compression level 0 means buffer to be memcpy'ed */
22212221
context->header_flags |= (uint8_t)BLOSC_MEMCPYED;
22222222
}
2223+
if (context->sourcesize < BLOSC_MIN_BUFFERSIZE) {
2224+
/* Buffer is too small. Try memcpy'ing. */
2225+
context->header_flags |= (uint8_t)BLOSC_MEMCPYED;
2226+
}
22232227

22242228
bool memcpyed = context->header_flags & (uint8_t)BLOSC_MEMCPYED;
22252229
if (extended_header) {

include/blosc2.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ enum {
173173
//!< Maximum typesize before considering source buffer as a stream of bytes.
174174
//!< Cannot be larger than 255.
175175
#endif // BLOSC_H
176-
BLOSC_MIN_BUFFERSIZE = 128,
177-
//!< Minimum buffer size to be compressed. Cannot be smaller than 66.
176+
BLOSC_MIN_BUFFERSIZE = 32,
177+
//!< Minimum buffer size to be compressed.
178178
};
179179

180180

tests/test_compressor.c

+35
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,40 @@ static char *test_small_blocksize(void) {
237237
}
238238

239239

240+
/* Check small buffer */
241+
static char *test_small_buffer(void) {
242+
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
243+
cparams.typesize = 1;
244+
blosc2_context *cctx = blosc2_create_cctx(cparams);
245+
blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS;
246+
blosc2_context *dctx = blosc2_create_dctx(dparams);
247+
size = 2;
248+
uint8_t *src2 = calloc(size, 1);
249+
for (int i = 0; i < size; i++) {
250+
src2[i] = (uint8_t)i;
251+
}
252+
253+
/* Using contexts */
254+
cbytes = blosc2_compress_ctx(cctx, src2, size, dest, size + BLOSC2_MAX_OVERHEAD);
255+
nbytes = blosc2_decompress_ctx(dctx, dest, size + BLOSC2_MAX_OVERHEAD, src, size);
256+
mu_assert("ERROR: nbytes is not correct", nbytes == size);
257+
258+
/* Not using contexts */
259+
cbytes = blosc2_compress(9, 1, cparams.typesize, src2, size, dest, size + BLOSC2_MAX_OVERHEAD);
260+
nbytes = blosc2_decompress(dest, size + BLOSC2_MAX_OVERHEAD, src, size);
261+
mu_assert("ERROR: nbytes is not correct", nbytes == size);
262+
263+
/* Using Blosc1 interface */
264+
cbytes = blosc1_compress(9, 1, cparams.typesize, size, src2, dest, size + BLOSC2_MAX_OVERHEAD);
265+
nbytes = blosc1_decompress(dest, src, size);
266+
mu_assert("ERROR: nbytes is not correct", nbytes == size);
267+
268+
free(src2);
269+
blosc2_free_ctx(cctx);
270+
blosc2_free_ctx(dctx);
271+
return 0;
272+
}
273+
240274

241275
static char *all_tests(void) {
242276
mu_run_test(test_compressor);
@@ -248,6 +282,7 @@ static char *all_tests(void) {
248282
mu_run_test(test_delta);
249283
mu_run_test(test_typesize);
250284
mu_run_test(test_small_blocksize);
285+
mu_run_test(test_small_buffer);
251286

252287
return 0;
253288
}

0 commit comments

Comments
 (0)