summaryrefslogtreecommitdiffstats
path: root/src/H5Z.c
blob: 5c7f0fadf4127da61bdd4e3bcfc16d424588bacf (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * Copyright (C) 1998 NCSA
 *                    All rights reserved.
 *
 * Programmer:  Robb Matzke <matzke@llnl.gov>
 *              Thursday, April 16, 1998
 *
 * Purpose:	Functions for data compression.
 */
#include <H5private.h>
#include <H5Eprivate.h>
#include <H5MMprivate.h>
#include <H5Oprivate.h>
#include <H5Zprivate.h>

#ifdef HAVE_ZLIB_H
#   include <zlib.h>
#endif

/* Interface initialization */
#define PABLO_MASK	H5Z_mask
#define INTERFACE_INIT H5Z_init_interface
static intn interface_initialize_g = FALSE;
static herr_t H5Z_init_interface (void);
static void H5Z_term_interface (void);

/*
 * The compression table maps compression method number to a struct that
 * contains pointers to the compress and uncompress methods along with timing
 * statistics.
 */
typedef struct H5Z_class_t {
    char	*name;		/*method name for debugging		*/
    H5Z_func_t	compress;	/*compression function			*/
    H5Z_func_t	uncompress;	/*uncompression function		*/

#ifdef H5Z_DEBUG
    struct {
	hsize_t	nbytes;		/*bytes compressed including overruns	*/
	hsize_t over;		/*bytes of overrun			*/
	H5_timer_t timer;	/*total compression time inc. overruns	*/
	hsize_t	failed;		/*bytes of failure (not overruns)	*/
    } comp;

    struct {
	hsize_t	nbytes;		/*bytes uncompressed, including overruns*/
	hsize_t	over;		/*bytes of overrun			*/
	H5_timer_t timer;	/*total uncompression time		*/
	hsize_t failed;		/*bytes of failure (not overruns)	*/
    } uncomp;
#endif
} H5Z_class_t;
static H5Z_class_t	H5Z_g[H5Z_MAXVAL+1];

/* Compression and uncompression methods */
static size_t H5Z_zlib_c (unsigned int flags, size_t __unused__ cd_size,
			  const void __unused__ *client_data,
			  size_t src_nbytes, const void *_src,
			  size_t dst_nbytes, void *dst/*out*/);
static size_t H5Z_zlib_u (unsigned int flags, size_t __unused__ cd_size,
			  const void __unused__ *client_data,
			  size_t src_nbytes, const void *_src,
			  size_t dst_nbytes, void *dst/*out*/);


/*-------------------------------------------------------------------------
 * Function:	H5Z_init_interface
 *
 * Purpose:	Initializes the data compression layer.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Thursday, April 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Z_init_interface (void)
{
    FUNC_ENTER (H5Z_init_interface, FAIL);

    H5_add_exit (H5Z_term_interface);

    H5Z_register (H5Z_NONE, "none", NULL, NULL);
    H5Z_register (H5Z_DEFLATE, "deflate", H5Z_zlib_c, H5Z_zlib_u);

    FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5Z_term_interface
 *
 * Purpose:	Terminate the H5Z layer.
 *
 * Return:	void
 *
 * Programmer:	Robb Matzke
 *              Thursday, April 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
H5Z_term_interface (void)
{
#ifdef H5Z_DEBUG
    int		i, nprint=0;
    char	name[16];

    for (i=0; i<=H5Z_MAXVAL; i++) {
	if (H5Z_g[i].comp.nbytes || H5Z_g[i].uncomp.nbytes) {
	    if (0==nprint++) {
		HDfprintf (stderr, "H5Z: compression statistics accumulated "
			   "over life of library:\n");
		HDfprintf (stderr, "   %-10s   %8s %8s %8s %8s %8s %8s %9s\n",
			   "Method", "Total", "Overrun", "Errors", "User",
			   "System", "Elapsed", "Bandwidth");
		HDfprintf (stderr, "   %-10s   %8s %8s %8s %8s %8s %8s %9s\n",
			   "------", "-----", "-------", "------", "----",
			   "------", "-------", "---------");
	    }
	    sprintf (name, "%s-c", H5Z_g[i].name);
	    HDfprintf (stderr,
		       "   %-12s %8Hd %8Hd %8Hd %8.2f %8.2f %8.2f ",
		       name,
		       H5Z_g[i].comp.nbytes,
		       H5Z_g[i].comp.over,
		       H5Z_g[i].comp.failed,
		       H5Z_g[i].comp.timer.utime,
		       H5Z_g[i].comp.timer.stime,
		       H5Z_g[i].comp.timer.etime);
	    if (H5Z_g[i].comp.timer.etime>0) {
		HDfprintf (stderr, "%9.3e\n",
			   H5Z_g[i].comp.nbytes / H5Z_g[i].comp.timer.etime);
	    } else {
		HDfprintf (stderr, "%9s\n", "NaN");
	    }
	    
	    sprintf (name, "%s-u", H5Z_g[i].name);
	    HDfprintf (stderr,
		       "   %-12s %8Hd %8Hd %8Hd %8.2f %8.2f %8.2f ",
		       name,
		       H5Z_g[i].uncomp.nbytes,
		       H5Z_g[i].uncomp.over,
		       H5Z_g[i].uncomp.failed,
		       H5Z_g[i].uncomp.timer.utime,
		       H5Z_g[i].uncomp.timer.stime,
		       H5Z_g[i].uncomp.timer.etime);
	    if (H5Z_g[i].uncomp.timer.etime>0) {
		HDfprintf (stderr, "%9.3e\n", 
			   H5Z_g[i].uncomp.nbytes/H5Z_g[i].uncomp.timer.etime);
	    } else {
		HDfprintf (stderr, "%9s\n", "NaN");
	    }
	}
    }
#endif
}


/*-------------------------------------------------------------------------
 * Function:	H5Zregister
 *
 * Purpose:	This function registers new compression and uncompression
 *		methods for a method number.  The NAME argument is used for
 *		debugging and may be the null pointer.  Either or both of
 *		CFUNC (the compression function) and UFUNC (the uncompression
 *		method) may be null pointers.
 *
 *		The statistics associated with a method number are not reset
 *		by this function; they accumulate over the life of the
 *		library.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Thursday, April 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Zregister (H5Z_method_t method, const char *name, H5Z_func_t cfunc,
	     H5Z_func_t ufunc)
{
    FUNC_ENTER (H5Zregister, FAIL);

    /* Check args */
    if (method<0 || method>H5Z_MAXVAL) {
	HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL,
		       "invalid data compression method number");
    }
    if (method<16) {
	HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL,
		       "unable to modify predefined compression methods");
    }

    /* Do it */
    if (H5Z_register (method, name, cfunc, ufunc)<0) {
	HRETURN_ERROR (H5E_COMP, H5E_CANTINIT, FAIL,
		       "unable to register compression methods");
    }

    FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5Z_compress
 *
 * Purpose:	Compress NBYTES from SRC into at most NBYTES of DST.
 *
 * Return:	Success:	Number of bytes in DST
 *
 *		Failure:	0 if the DST buffer overflowed or something
 *				else went wrong.
 *
 * Programmer:	Robb Matzke
 *              Thursday, April 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
size_t
H5Z_compress (const H5O_compress_t *comp, size_t nbytes, const void *src,
	      void *dst/*out*/)
{
    size_t		ret_value = 0;
    H5Z_method_t	method = comp ? comp->method : H5Z_NONE;
    H5Z_func_t		cfunc = NULL;
#ifdef H5Z_DEBUG
    intn		over = 0;
    H5_timer_t		timer;
#endif
    
    FUNC_ENTER (H5Z_compress, 0);

#ifdef H5Z_DEBUG
    H5_timer_begin (&timer);
#endif

    if (H5Z_NONE==method) {
	/* No compression method */
	HGOTO_DONE (0);
	
    } else if (NULL==(cfunc=H5Z_g[method].compress)) {
	/* No compress function */
	HGOTO_ERROR (H5E_COMP, H5E_UNSUPPORTED, 0,
		     "compression method is not supported");
	
    } else if (0==(ret_value=(cfunc)(comp->flags, comp->cd_size,
				     comp->client_data, nbytes,
				     src, nbytes, dst))) {
	/* Compress failed */
	HGOTO_ERROR (H5E_COMP, H5E_CANTINIT, 0, "compression failed");
	
    } else if (ret_value>=nbytes) {
	/* Output is not smaller than input */
#ifdef H5Z_DEBUG
	H5Z_g[method].comp.over += nbytes;
	over = 1;
#endif
	HGOTO_DONE (0);
    }

 done:
#ifdef H5Z_DEBUG
    H5Z_g[method].comp.nbytes += nbytes;
    if (0==ret_value && !over) H5Z_g[method].comp.failed += nbytes;
    H5_timer_end (&(H5Z_g[method].comp.timer), &timer);
#endif
    FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5Z_uncompress
 *
 * Purpose:	Uncompress SRC_NBYTES from SRC into at most DST_NBYTES of
 *		DST. 
 *
 * Return:	Success:	Number of bytes in DST buffer.
 *
 *		Failure:	0 if the uncompression failed or DST wasn't
 *				big enough to hold the result.
 *
 * Programmer:	Robb Matzke
 *              Thursday, April 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
size_t
H5Z_uncompress (const H5O_compress_t *comp, size_t src_nbytes, const void *src,
		size_t dst_nbytes, void *dst/*out*/)
{
    size_t		ret_value = 0;
    H5Z_func_t		ufunc = NULL;
    H5Z_method_t	method = comp ? comp->method : H5Z_NONE;
#ifdef H5Z_DEBUG
    H5_timer_t		timer;
#endif
    
    FUNC_ENTER (H5Z_uncompress, 0);

#ifdef H5Z_DEBUG
    H5_timer_begin (&timer);
#endif

    if (H5Z_NONE==method) {
	/* No compression method */
	assert (src_nbytes<=dst_nbytes);
	HDmemcpy (dst, src, src_nbytes);
	ret_value = src_nbytes;
	
    } else if (src_nbytes==dst_nbytes) {
	/* Data is not compressed */
#ifdef H5Z_DEBUG
	H5Z_g[method].uncomp.over += src_nbytes;
#endif
	HDmemcpy (dst, src, src_nbytes);
	ret_value = src_nbytes;
	
    } else if (NULL==(ufunc=H5Z_g[method].uncompress)) {
	/* No uncompress function */
	HGOTO_ERROR (H5E_COMP, H5E_UNSUPPORTED, 0,
		     "uncompression method is not supported");
	
    } else if (0==(ret_value=(ufunc)(comp->flags, comp->cd_size,
				     comp->client_data, src_nbytes,
				     src, dst_nbytes, dst))) {
	/* Uncompress failed */
	HGOTO_ERROR (H5E_COMP, H5E_CANTINIT, 0, "uncompression failed");
    }

 done:
#ifdef H5Z_DEBUG
    H5Z_g[method].uncomp.nbytes += dst_nbytes;
    if (0==ret_value) H5Z_g[method].uncomp.failed += dst_nbytes;
    H5_timer_end (&(H5Z_g[method].uncomp.timer), &timer);
#endif

    FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5Z_register
 *
 * Purpose:	Same as the public version except this one allows compression
 *		methods to be set for predefined method numbers <16.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Thursday, April 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Z_register (H5Z_method_t method, const char *name, H5Z_func_t cfunc,
	      H5Z_func_t ufunc)
{
    FUNC_ENTER (H5Z_register, FAIL);

    assert (method>=0 && method<=H5Z_MAXVAL);
    H5MM_xfree (H5Z_g[method].name);
    H5Z_g[method].name = H5MM_xstrdup (name);
    H5Z_g[method].compress = cfunc;
    H5Z_g[method].uncompress = ufunc;

    FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5Z_zlib_c
 *
 * Purpose:	Compress SRC_NBYTES bytes from SRC into at most DST_NBYTES of
 *		DST using the compression level as specified in FLAGS.
 *
 * Return:	Success:	Number of bytes compressed into DST limited
 *				by DST_NBYTES.
 *
 *		Failure:	0
 *
 * Programmer:	Robb Matzke
 *              Thursday, April 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static size_t
H5Z_zlib_c (unsigned int flags, size_t __unused__ cd_size,
	    const void __unused__ *client_data, size_t src_nbytes,
	    const void *src, size_t dst_nbytes, void *dst/*out*/)
{
    size_t	ret_value = 0;
#ifdef HAVE_ZLIB_H
    const Bytef	*z_src = (const Bytef*)src;
    Bytef	*z_dst = (Bytef*)dst;
    uLongf	z_dst_nbytes = (uLongf)dst_nbytes;
    uLong	z_src_nbytes = (uLong)src_nbytes;
    int		level = flags % 10;
    int		status;
#endif
    
    FUNC_ENTER (H5Z_zlib_c, 0);

#if defined(HAVE_LIBZ) && defined (HAVE_ZLIB_H)
    status = compress2 (z_dst, &z_dst_nbytes, z_src, z_src_nbytes, level);
    if (Z_BUF_ERROR==status) {
	ret_value = dst_nbytes;
    } else if (Z_MEM_ERROR==status) {
	HGOTO_ERROR (H5E_COMP, H5E_CANTINIT, 0, "deflate memory error");
    } else if (Z_OK!=status) {
	HGOTO_ERROR (H5E_COMP, H5E_CANTINIT, 0, "deflate error");
    } else {
	ret_value = z_dst_nbytes;
    }
#else
    HGOTO_ERROR (H5E_COMP, H5E_UNSUPPORTED, 0,
		 "hdf5 was not compiled with zlib-1.0.2 or better");
#endif

 done:
    FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5Z_zlib_u
 *
 * Purpose:	Uncompress SRC_NBYTES from SRC into at most DST_NBYTES of
 *		DST.
 *
 * Return:	Success:	Number of bytes returned in DST.
 *
 *		Failure:	0
 *
 * Programmer:	Robb Matzke
 *              Thursday, April 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static size_t
H5Z_zlib_u (unsigned int __unused__ flags, size_t __unused__ cd_size,
	    const void __unused__ *client_data, size_t src_nbytes,
	    const void *src, size_t dst_nbytes, void *dst/*out*/)
{
    size_t	ret_value = 0;
#ifdef HAVE_ZLIB_H
    const Bytef	*z_src = (const Bytef*)src;
    Bytef	*z_dst = (Bytef*)dst;
    uLongf	z_dst_nbytes = (uLongf)dst_nbytes;
    uLong	z_src_nbytes = (uLong)src_nbytes;
    int		status;
#endif
    
    FUNC_ENTER (H5Z_zlib_u, 0);

#if defined(HAVE_LIBZ) && defined (HAVE_ZLIB_H)
    status = uncompress (z_dst, &z_dst_nbytes, z_src, z_src_nbytes);
    if (Z_BUF_ERROR==status) {
	HGOTO_ERROR (H5E_COMP, H5E_CANTINIT, 0,
		     "deflate destination buffer was too small");
    } else if (Z_MEM_ERROR==status) {
	HGOTO_ERROR (H5E_COMP, H5E_CANTINIT, 0, "deflate memory error");
    } else if (Z_DATA_ERROR==status) {
	HGOTO_ERROR (H5E_COMP, H5E_CANTINIT, 0, "deflate corrupted data");
    } else if (Z_OK!=status) {
	HGOTO_ERROR (H5E_COMP, H5E_CANTINIT, 0, "deflate error");
    }
    ret_value = z_dst_nbytes;
#else
    HGOTO_ERROR (H5E_COMP, H5E_UNSUPPORTED, 0,
		 "hdf5 was not compiled with zlib-1.0.2 or better");
#endif

 done:
    FUNC_LEAVE (ret_value);
}