netcdf-c/plugins/H5Zmisc.c
Dennis Heimbigner aeb3ac2809 Mostly revert the filter code to reduce its complexity of use.
re: https://github.com/Unidata/netcdf-c/issues/1836

Revert the internal filter code to simplify it. From the user's
point of view, the only visible changes should be:

1. The functions that convert text to filter specs have had their signature reverted and have been moved to netcdf_aux.h
2. Some filter API functions now return NC_ENOFILTER when inquiry is made about some filter.

Internally,the dispatch table has been modified to get rid of the filter_actions
entry and associated complex structures. It has been replaced with
inq_var_filter_ids and inq_var_filter_info entries and the dispatch table
version has been bumped to 3. Corresponding NOOP and NOTNC4 functions
were added to libdispatch/dnotnc4.c. Also, the filter_action entries
in dispatch tables were replaced for all dispatch code bases (HDF5, DAP2,
etc). This should only impact UDF users.

In the process, it became clear that the form of the filters
field in NC_VAR_INFO_T was format dependent, so I converted it to
be of type void* and pushed its management into the various dispatch
code bases. Specifically libhdf5 and libnczarr now manage the filters
field in their own way.

The auxilliary functions for parsing textual filter specifications
were moved to netcdf_aux.h and were renamed to the following:
* ncaux_h5filterspec_parse
* ncaux_h5filterspec_parselist
* ncaux_h5filterspec_free
* ncaux_h5filter_fix8

Misc. Other Changes:

1. Document NUG/filters.md updated to reflect the changes above.
2. All the old data types (structs and enums)
   used by filter_actions actions were deleted.
   The exception is the NC_H5_Filterspec because it is needed
   by ncaux_h5filterspec_parselist.
3. Clientside filters were removed -- another enhancement
   for which no-one ever asked.
4. The ability to remove filters was itself removed.
5. Some functionality needed by nczarr was moved from libhdf5
   to libsrc4 e.g. nc4_find_default_chunksizes
6. All the filterx code was removed
7. ncfilter.h and nc4filter.c no longer used

Misc. Unrelated Changes:

1. The nczarr_test makefile clean was leaving some directories; so
   add clean-local to take care of them.
2020-09-27 12:43:46 -06:00

280 lines
7.1 KiB
C

#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <hdf5.h>
/* Older versions of the hdf library may define H5PL_type_t here */
#include <H5PLextern.h>
#include "h5misc.h"
#ifndef DLL_EXPORT
#define DLL_EXPORT
#endif
/* WARNING:
Starting with HDF5 version 1.10.x, the plugin code MUST be
careful when using the standard *malloc()*, *realloc()*, and
*free()* function.
In the event that the code is allocating, reallocating, for
free'ing memory that either came from or will be exported to the
calling HDF5 library, then one MUST use the corresponding HDF5
functions *H5allocate_memory()*, *H5resize_memory()*,
*H5free_memory()* [5] to avoid memory failures.
Additionally, if your filter code leaks memory, then the HDF5 library
will generate an error.
*/
#undef DEBUG
/* The C standard apparently defines all floating point constants as double;
we rely on that in this code.
*/
#define DBLVAL 12345678.12345678
static int paramcheck(size_t nparams, const unsigned int* params);
static void mismatch(size_t i, const char* which);
const H5Z_class2_t H5Z_TEST[1] = {{
H5Z_CLASS_T_VERS, /* H5Z_class_t version */
(H5Z_filter_t)(H5Z_FILTER_TEST), /* Filter id number */
1, /* encoder_present flag (set to true) */
1, /* decoder_present flag (set to true) */
"test", /* Filter name for debugging */
(H5Z_can_apply_func_t)H5Z_test_can_apply, /* The "can apply" callback */
NULL, /* The "set local" callback */
(H5Z_func_t)H5Z_filter_test, /* The actual filter function */
}};
/* External Discovery Functions */
H5PL_type_t
H5PLget_plugin_type(void)
{
return H5PL_TYPE_FILTER;
}
const void*
H5PLget_plugin_info(void)
{
return H5Z_TEST;
}
/* Make this explicit */
/*
* The "can_apply" callback returns positive a valid combination, zero for an
* invalid combination and negative for an error.
*/
htri_t
H5Z_test_can_apply(hid_t dcpl_id, hid_t type_id, hid_t space_id)
{
return 1; /* Assume it can always apply */
}
/*
This filter does some verification
that the parameters passed to the filter
are correct. Specifically, that endian-ness
is correct. As a filter, it is the identify
function, passing input to output unchanged.
It also prints out the size of each chunk.
Test cases format:
1.The first param is the test index i.e. which test to execute.
2. The remaining parameters are those for the test chosen in #1
*/
size_t
H5Z_filter_test(unsigned int flags, size_t cd_nelmts,
const unsigned int cd_values[], size_t nbytes,
size_t *buf_size, void **buf)
{
void* newbuf;
unsigned int testcase = 0;
if(cd_nelmts == 0)
goto fail;
testcase = cd_values[0];
switch (testcase) {
case TC_ENDIAN:
if(!paramcheck(cd_nelmts,cd_values))
goto fail;
break;
case TC_ODDSIZE:
/* Print out the chunk size */
fprintf(stderr,"nbytes = %lld chunk size = %lld\n",(long long)nbytes,(long long)*buf_size);
fflush(stderr);
break;
default: break;
}
if (flags & H5Z_FLAG_REVERSE) {
/* Replace buffer */
#ifdef HAVE_H5ALLOCATE_MEMORY
newbuf = H5allocate_memory(*buf_size,0);
#else
newbuf = malloc(*buf_size);
#endif
if(newbuf == NULL) abort();
memcpy(newbuf,*buf,*buf_size);
/* reclaim old buffer */
#ifdef HAVE_H5FREE_MEMORY
H5free_memory(*buf);
#else
free(*buf);
#endif
*buf = newbuf;
} else {
/* Replace buffer */
#ifdef HAVE_H5ALLOCATE_MEMORY
newbuf = H5allocate_memory(*buf_size,0);
#else
newbuf = malloc(*buf_size);
#endif
if(newbuf == NULL) abort();
memcpy(newbuf,*buf,*buf_size);
/* reclaim old buffer */
#ifdef HAVE_H5FREE_MEMORY
H5free_memory(*buf);
#else
free(*buf);
#endif
*buf = newbuf;
}
return *buf_size;
fail:
return 0;
}
static int
paramcheck(size_t nparams, const unsigned int* params)
{
size_t i;
unsigned char mem[8];
if(nparams != 14) {
fprintf(stderr,"Too few parameters: need=14 sent=%ld\n",(unsigned long)nparams);
goto fail;
}
for(i=0;i<nparams;i++) {
unsigned int ival;
unsigned long long lval;
float fval;
double dval;
switch (i) {
case 0: break; /* this is the testcase # */
case 1:
ival = (-17) & 0xff;
if(ival != (signed int)(params[i]))
{mismatch(i,"signed byte"); goto fail; };
break;
case 2:
ival = 23;
if(ival != (unsigned int)(params[i]))
{mismatch(i,"unsigned byte"); goto fail; };
break;
case 3:
ival = (-25) & 0xffff;
if(ival != (signed int)(params[i]))
{mismatch(i,"signed short"); goto fail; };
break;
case 4:
ival = 27;
if(ival != (unsigned int)(params[i]))
{mismatch(i,"unsigned short"); goto fail; };
break;
case 5:
ival = 77;
if(ival != (signed int)(params[i]))
{mismatch(i,"signed int"); goto fail; };
break;
case 6:
ival = 93u;
if(ival != (unsigned int)(params[i]))
{mismatch(i,"unsigned int"); goto fail; };
break;
case 7:
fval = 789.0f;
if(fval != *(float*)(&params[i]))
{mismatch(i,"float"); goto fail; };
break;
case 8: {/*double*/
double x;
memcpy(mem,&params[i],sizeof(mem));
NC_h5filterspec_fix8(mem,1); /* Fix up endianness */
x = *(double*)mem;
dval = DBLVAL;
i++; /* takes two parameters */
if(dval != x) {
mismatch(i,"double");
goto fail;
}
}; break;
case 10: {/*signed long long*/
signed long long x;
memcpy(mem,&params[i],sizeof(mem));
NC_h5filterspec_fix8(mem,1); /* Fix up endianness */
x = *(signed long long*)mem;
NC_h5filterspec_fix8(&x,1); /* Fix up endianness */
lval = -9223372036854775807L;
i++; /* takes two parameters */
if(lval != x) {
mismatch(i,"signed long long");
goto fail;
}
}; break;
case 12: {/*unsigned long long*/
unsigned long long x;
memcpy(mem,&params[i],sizeof(mem));
NC_h5filterspec_fix8(mem,1); /* Fix up endianness */
x = *(unsigned long long*)mem;
lval = 18446744073709551615UL;
i++; /* takes two parameters */
if(lval != x) {
mismatch(i,"unsigned long long");
goto fail;
}
}; break;
default:
mismatch(i,"unexpected parameter");
goto fail;
break;
}
}
#ifdef DEBUG
{
size_t i;
fprintf(stderr,"bigendian=%d nparams=%d params=\n",bigendian,nparams);
for(i=0;i<nparams;i++) {
fprintf(stderr,"[%d] %ud %d %f\n", (unsigned int)i, params[i],(signed int)params[i],*(float*)&params[i]);
}
fflush(stderr);
}
#endif
return 1;
fail:
return 0;
}
static void
mismatch(size_t i, const char* which)
{
fprintf(stderr,"mismatch: [%ld] %s\n",(unsigned long)i,which);
fflush(stderr);
}