-
Notifications
You must be signed in to change notification settings - Fork 11
/
zstd_h5plugin.c
81 lines (68 loc) · 1.82 KB
/
zstd_h5plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdlib.h>
#include "zstd_h5plugin.h"
#include "zstd.h"
#define ZSTD_FILTER 32015
DLL_EXPORT size_t zstd_filter(unsigned int flags, size_t cd_nelmts,
const unsigned int cd_values[], size_t nbytes,
size_t *buf_size, void **buf)
{
void *outbuf = NULL; /* Pointer to new output buffer */
void *inbuf = NULL; /* Pointer to input buffer */
inbuf = *buf;
size_t ret_value;
size_t origSize = nbytes; /* Number of bytes for output (compressed) buffer */
if (flags & H5Z_FLAG_REVERSE)
{
size_t decompSize = ZSTD_getDecompressedSize(*buf, origSize);
if (NULL == (outbuf = malloc(decompSize)))
goto error;
decompSize = ZSTD_decompress(outbuf, decompSize, inbuf, origSize);
free(*buf);
*buf = outbuf;
outbuf = NULL;
ret_value = (size_t)decompSize;
}
else
{
int aggression;
if (cd_nelmts > 0)
aggression = (int)cd_values[0];
else
aggression = ZSTD_CLEVEL_DEFAULT;
if (aggression < 1 /*ZSTD_minCLevel()*/)
aggression = 1 /*ZSTD_minCLevel()*/;
else if (aggression > ZSTD_maxCLevel())
aggression = ZSTD_maxCLevel();
size_t compSize = ZSTD_compressBound(origSize);
if (NULL == (outbuf = malloc(compSize)))
goto error;
compSize = ZSTD_compress(outbuf, compSize, inbuf, origSize, aggression);
free(*buf);
*buf = outbuf;
*buf_size = compSize;
outbuf = NULL;
ret_value = compSize;
}
if (outbuf != NULL)
free(outbuf);
return ret_value;
error:
return 0;
}
const H5Z_class_t zstd_H5Filter =
{
H5Z_CLASS_T_VERS,
(H5Z_filter_t)(ZSTD_FILTER),
1, 1,
"Zstandard compression: http://www.zstd.net",
NULL, NULL,
(H5Z_func_t)(zstd_filter)
};
DLL_EXPORT H5PL_type_t H5PLget_plugin_type(void)
{
return H5PL_TYPE_FILTER;
}
DLL_EXPORT const void* H5PLget_plugin_info(void)
{
return &zstd_H5Filter;
}