diff --git a/include/netcdf.h b/include/netcdf.h index f0863f570..3a7bf6e33 100644 --- a/include/netcdf.h +++ b/include/netcdf.h @@ -158,8 +158,10 @@ Use this in mode flags for both nc_create() and nc_open(). */ #define NC_PNETCDF (NC_MPIIO) /**< Use parallel-netcdf library; alias for NC_MPIIO. */ -#define NC_UDF0 0x0080 /**< User-defined format. */ -#define NC_UDF1 0x0002 /**< User-defined format. */ +#define NC_UDF0 0x0080 /**< User-defined format 0. */ +#define NC_UDF1 0x0002 /**< User-defined format 1. */ + +#define NC_MAX_MAGIC_NUMBER_LEN 8 /**< Max len of ser-defined format magic number. */ /** Format specifier for nc_set_default_format() and returned * by nc_inq_format. This returns the format as provided by diff --git a/libdispatch/dfile.c b/libdispatch/dfile.c index ba4b447c5..c02a2331d 100644 --- a/libdispatch/dfile.c +++ b/libdispatch/dfile.c @@ -67,7 +67,9 @@ static char HDF5_SIGNATURE[MAGIC_NUMBER_LEN] = "\211HDF\r\n\032\n"; #ifdef USE_NETCDF4 /* User-defined formats. */ NC_Dispatch *UDF0_dispatch_table = NULL; +char UDF0_magic_number[NC_MAX_MAGIC_NUMBER_LEN + 1]; NC_Dispatch *UDF1_dispatch_table = NULL; +char UDF1_magic_number[NC_MAX_MAGIC_NUMBER_LEN + 1]; #endif /* USE_NETCDF4 */ /** \defgroup datasets NetCDF File and Data I/O @@ -128,15 +130,22 @@ nc_def_user_format(int mode_flag, NC_Dispatch *dispatch_table, char *magic_numbe return NC_EINVAL; if (!dispatch_table) return NC_EINVAL; + if (magic_number && strlen(magic_number) > NC_MAX_MAGIC_NUMBER_LEN) + return NC_EINVAL; - /* Retain a pointer to the dispatch_table. */ + /* Retain a pointer to the dispatch_table and a copy of the magic + * number, if one was provided. */ switch(mode_flag) { case NC_UDF0: UDF0_dispatch_table = dispatch_table; + if (magic_number) + strncpy(UDF0_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN); break; case NC_UDF1: UDF1_dispatch_table = dispatch_table; + if (magic_number) + strncpy(UDF0_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN); break; } diff --git a/nc_test4/tst_udf.c b/nc_test4/tst_udf.c index 4ff0f262c..d849f977b 100644 --- a/nc_test4/tst_udf.c +++ b/nc_test4/tst_udf.c @@ -191,12 +191,14 @@ main(int argc, char **argv) printf("*** testing user-defined format with magic number..."); { int ncid; + char magic_number[5] = "1111"; + /* Create an empty file to play with. */ if (nc_create(FILE_NAME, 0, &ncid)) ERR; if (nc_close(ncid)) ERR; /* Add our test user defined format. */ - if (nc_def_user_format(NC_UDF0, &tst_dispatcher, NULL)) ERR; + if (nc_def_user_format(NC_UDF0, &tst_dispatcher, magic_number)) ERR; /* Open file with our defined functions. */ if (nc_open(FILE_NAME, NC_UDF0, &ncid)) ERR;