Debian Codegraph @

lzf/lzf_filter.c

raw ◀ ▶
1 Debian patch touch this file
1 /***** Preamble block *********************************************************
2 *
3 * This file is part of h5py, a low-level Python interface to the HDF5 library.
4 *
5 * Copyright (C) 2008 Andrew Collette
6 * http://h5py.alfven.org
7 * License: BSD (See LICENSE.txt for full license)
8 *
9 * $Date$
10 *
11 ****** End preamble block ****************************************************/
12
13 /*
14 Implements an LZF filter module for HDF5, using the BSD-licensed library
15 by Marc Alexander Lehmann (http://www.goof.com/pcg/marc/liblzf.html).
16
17 No Python-specific code is used. The filter behaves like the DEFLATE
18 filter, in that it is called for every type and space, and returns 0
19 if the data cannot be compressed.
20
21 The only public function is (int) register_lzf(void), which passes on
22 the result from H5Zregister.
23 */
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include "hdf5.h"
•29 #include "lzf.h"
30 #include "lzf_filter.h"
31
32 /* Our own versions of H5Epush_sim, as it changed in 1.8 */
33 #if H5_VERS_MAJOR == 1 && H5_VERS_MINOR < 7
34
35 #define PUSH_ERR(func, minor, str) H5Epush(__FILE__, func, __LINE__, H5E_PLINE, minor, str)
36 #define H5PY_GET_FILTER H5Pget_filter_by_id
37
38 #else
39
40 #define PUSH_ERR(func, minor, str) H5Epush1(__FILE__, func, __LINE__, H5E_PLINE, minor, str)
41 #define H5PY_GET_FILTER(a,b,c,d,e,f,g) H5Pget_filter_by_id2(a,b,c,d,e,f,g,NULL)
42
43 #endif
44
45 /* Deal with the mutiple definitions for H5Z_class_t.
46 Note: HDF5 1.6 and >= 1.8 are supported.
47 See https://hdfgroup.github.io/hdf5/develop/group___h5_z.html#ga93145acc38c2c60d832b7a9b0123706b
48 for version history.
49
50 (1) The old class should always be used for HDF5 1.6
51 (2) The new class should always be used for HDF5 1.8 < 1.8.3
52 (3) The old class should be used for HDF5 >= 1.8.3 only if the
53 macro H5_USE_16_API is set
54 */
55
56 #if H5_VERS_MAJOR == 1 && H5_VERS_MINOR < 8
57 #define H5PY_H5Z_NEWCLS 0
58 #elif H5_VERS_MAJOR == 1 && H5_VERS_MINOR >= 8 && H5_VERS_RELEASE >= 3 && H5_USE_16_API
59 #define H5PY_H5Z_NEWCLS 0
60 #else
61 #define H5PY_H5Z_NEWCLS 1
62 #endif
63
64 size_t lzf_filter(unsigned flags, size_t cd_nelmts,
65 const unsigned cd_values[], size_t nbytes,
66 size_t *buf_size, void **buf);
67
68 herr_t lzf_set_local(hid_t dcpl, hid_t type, hid_t space);
69
70
71 /* Try to register the filter, passing on the HDF5 return value */
72 int register_lzf(void){
73
74 int retval;
75
76 #if H5PY_H5Z_NEWCLS
77 H5Z_class_t filter_class = {
78 H5Z_CLASS_T_VERS,
79 (H5Z_filter_t)(H5PY_FILTER_LZF),
80 1, 1,
81 "lzf",
82 NULL,
83 (H5Z_set_local_func_t)(lzf_set_local),
84 (H5Z_func_t)(lzf_filter)
85 };
86 #else
87 H5Z_class_t filter_class = {
88 (H5Z_filter_t)(H5PY_FILTER_LZF),
89 "lzf",
90 NULL,
91 (H5Z_set_local_func_t)(lzf_set_local),
92 (H5Z_func_t)(lzf_filter)
93 };
94 #endif
95
96 retval = H5Zregister(&filter_class);
97 if(retval<0){
98 PUSH_ERR("register_lzf", H5E_CANTREGISTER, "Can't register LZF filter");
99 }
100 return retval;
101 }
102
103 /* Filter setup. Records the following inside the DCPL:
104
105 1. If version information is not present, set slots 0 and 1 to the filter
106 revision and LZF API version, respectively.
107
108 2. Compute the chunk size in bytes and store it in slot 2.
109 */
110 herr_t lzf_set_local(hid_t dcpl, hid_t type, hid_t space){
111
112 int ndims;
113 int i;
114 herr_t r;
115
116 unsigned int bufsize;
117 hsize_t chunkdims[32];
118
119 unsigned int flags;
120 size_t nelements = 8;
121 unsigned values[] = {0,0,0,0,0,0,0,0};
122
123 r = H5PY_GET_FILTER(dcpl, H5PY_FILTER_LZF, &flags, &nelements, values, 0, NULL);
124 if(r<0) return -1;
125
126 if(nelements < 3) nelements = 3; /* First 3 slots reserved. If any higher
127 slots are used, preserve the contents. */
128
129 /* It seems the H5Z_FLAG_REVERSE flag doesn't work here, so we have to be
130 careful not to clobber any existing version info */
131 if(values[0]==0) values[0] = H5PY_FILTER_LZF_VERSION;
132 if(values[1]==0) values[1] = LZF_VERSION;
133
134 ndims = H5Pget_chunk(dcpl, 32, chunkdims);
135 if(ndims<0) return -1;
136 if(ndims>32){
137 PUSH_ERR("lzf_set_local", H5E_CALLBACK, "Chunk rank exceeds limit");
138 return -1;
139 }
140
141 bufsize = H5Tget_size(type);
142 if(bufsize==0) return -1;
143
144 for(i=0;i<ndims;i++){
145 bufsize *= chunkdims[i];
146 }
147
148 values[2] = bufsize;
149
150 #ifdef H5PY_LZF_DEBUG
151 fprintf(stderr, "LZF: Computed buffer size %d\n", bufsize);
152 #endif
153
154 r = H5Pmodify_filter(dcpl, H5PY_FILTER_LZF, flags, nelements, values);
155 if(r<0) return -1;
156
157 return 1;
158 }
159
160
161 /* The filter function */
162 size_t lzf_filter(unsigned flags, size_t cd_nelmts,
163 const unsigned cd_values[], size_t nbytes,
164 size_t *buf_size, void **buf){
165
166 void* outbuf = NULL;
167 size_t outbuf_size = 0;
168
169 unsigned int status = 0; /* Return code from lzf routines */
170
171 /* We're compressing */
172 if(!(flags & H5Z_FLAG_REVERSE)){
173
174 /* Allocate an output buffer exactly as long as the input data; if
175 the result is larger, we simply return 0. The filter is flagged
176 as optional, so HDF5 marks the chunk as uncompressed and
177 proceeds.
178 */
179
180 outbuf_size = (*buf_size);
181 outbuf = malloc(outbuf_size);
182
183 if(outbuf == NULL){
184 PUSH_ERR("lzf_filter", H5E_CALLBACK, "Can't allocate compression buffer");
185 goto failed;
186 }
187
188 status = lzf_compress(*buf, nbytes, outbuf, outbuf_size);
189
190 /* We're decompressing */
191 } else {
192
193 if((cd_nelmts>=3)&&(cd_values[2]!=0)){
194 outbuf_size = cd_values[2]; /* Precomputed buffer guess */
195 }else{
196 outbuf_size = (*buf_size);
197 }
198
199 #ifdef H5PY_LZF_DEBUG
200 fprintf(stderr, "Decompress %d chunk w/buffer %d\n", nbytes, outbuf_size);
201 #endif
202
203 while(!status){
204
205 free(outbuf);
206 outbuf = malloc(outbuf_size);
207
208 if(outbuf == NULL){
209 PUSH_ERR("lzf_filter", H5E_CALLBACK, "Can't allocate decompression buffer");
210 goto failed;
211 }
212
213 status = lzf_decompress(*buf, nbytes, outbuf, outbuf_size);
214
215 if(!status){ /* compression failed */
216
217 if(errno == E2BIG){
218 outbuf_size += (*buf_size);
219 #ifdef H5PY_LZF_DEBUG
220 fprintf(stderr, " Too small: %d\n", outbuf_size);
221 #endif
222 } else if(errno == EINVAL) {
223
224 PUSH_ERR("lzf_filter", H5E_CALLBACK, "Invalid data for LZF decompression");
225 goto failed;
226
227 } else {
228 PUSH_ERR("lzf_filter", H5E_CALLBACK, "Unknown LZF decompression error");
229 goto failed;
230 }
231
232 } /* if !status */
233
234 } /* while !status */
235
236 } /* compressing vs decompressing */
237
238 if(status != 0){
239
240 free(*buf);
241 *buf = outbuf;
242 *buf_size = outbuf_size;
243
244 return status; /* Size of compressed/decompressed data */
245 }
246
247 failed:
248
249 free(outbuf);
250 return 0;
251
252 } /* End filter function */
253
254
255
256
257
258
259
260
261
262
263
264
265