netcdf-c/ncgen/genlib.c
Dennis Heimbigner 9983b9d911 re e-support UBS-599337
re pull request https://github.com/Unidata/netcdf-c/pull/405
re pull request https://github.com/Unidata/netcdf-c/pull/446

Notes:
1. This branch is a cleanup of the magic.dmh branch.
2. magic.dmh was originally merged, but caused problems with parallel IO.
   It was re-issued as pull request https://github.com/Unidata/netcdf-c/pull/446.
3. This branch + pull request replace any previous pull requests and magic.dmh branch.

Given an otherwise valid netCDF file that has a corrupted header,
the netcdf library currently crashes. Instead, it should return
NC_ENOTNC.

Additionally, the NC_check_file_type code does not do the
forward search required by hdf5 files. It currently only looks
at file position 0 instead of 512, 1024, 2048,... Also, it turns
out that the HDF4 magic number is assumed to always be at the
beginning of the file (unlike HDF5).
The change is localized to libdispatch/dfile.c See
https://support.hdfgroup.org/release4/doc/DSpec_html/DS.pdf

Also, it turns out that the code in NC_check_file_type is duplicated
(mostly) in the function libsrc4/nc4file.c#nc_check_for_hdf.

This branch does the following.
1. Make NC_check_file_type return NC_ENOTNC instead of crashing.
2. Remove nc_check_for_hdf and centralize all file format checking
   NC_check_file_type.
3. Add proper forward search for HDF5 files (but not HDF4 files)
   to look for the magic number at offsets of 0, 512, 1024...
4. Add test tst_hdf5_offset.sh. This tests that hdf5 files with
   an offset are properly recognized. It does so by prefixing
   a legal file with some number of zero bytes: 512, 1024, etc.
5. Off-topic: Added -N flag to ncdump to force a specific output dataset name.
2017-10-24 16:25:09 -06:00

227 lines
5.6 KiB
C

/*********************************************************************
* Copyright 1993, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/ncgen/genlib.c,v 1.57 2010/04/04 19:39:47 dmh Exp $
*********************************************************************/
#include "includes.h"
/* invoke netcdf calls (or generate C or Fortran code) to create netcdf
* from in-memory structure.
The output file name is chosen by using the following in priority order:
1. -o flag name
2. command line input file with .cdl changed to .nc
3. dataset name as specified in netcdf <name> {...}
*/
void
define_netcdf(void)
{
char filename[2048+1];
/* Rule for specifying the dataset name:
1. use explicit datasetname
2. use the datasetname from the .cdl file (see ncgen.l)
3. use -o name ?implemented?
4. use input cdl file name (with .cdl removed) ?implemented?
*/
/* Rule for specifying the output file name:
1. use -o name
2. use input cdl file name (with .cdl removed)
3. use the datasetname
*/
if(netcdf_name) { /* -o flag name */
strncpy(filename,netcdf_name,2048);
} else { /* construct a usable output file name */
if (cdlname != NULL && strcmp(cdlname,"-") != 0) {/* cmd line name */
char* p;
strncpy(filename,cdlname,2048);
/* remove any suffix and prefix => create in cwd */
p = strrchr(filename,'.');
if(p != NULL) {*p= '\0';}
p = strrchr(filename,'/');
if(p != NULL) {memmove(filename,(p+1),2048);}
} else {/* construct name from dataset name */
strncpy(filename,datasetname,2048); /* Reserve space for extension, terminating '\0' */
}
/* Append the proper extension */
strncat(filename,binary_ext,2048-(strlen(filename) + strlen(binary_ext)));
}
/* Execute exactly one of these */
#ifdef ENABLE_C
if (l_flag == L_C) gen_ncc(filename); else /* create C code to create netcdf */
#endif
#ifdef ENABLE_F77
if (l_flag == L_F77) gen_ncf77(filename); else /* create Fortran code */
#endif
#ifdef ENABLE_JAVA
if(l_flag == L_JAVA) {
gen_ncjava(filename);
} else
#endif
/* Binary is the default */
#ifdef ENABLE_BINARY
gen_netcdf(filename); /* create netcdf */
#else
derror("No language specified");
#endif
close_netcdf();
cleanup();
}
void
close_netcdf(void)
{
#ifdef ENABLE_C
if (l_flag == L_C) cl_c(); else /* create C code to close netcdf */
#endif
#ifdef ENABLE_F77
if (l_flag == L_F77) cl_f77(); else
#endif
#ifdef ENABLE_JAVA
if (l_flag == L_JAVA) cl_java(); else
#endif
#ifdef ENABLE_BINARY
if (l_flag == L_BINARY) cl_netcdf();
#endif
}
/**
Return a string representing
the fully qualified name of the symbol.
Symbol must be top level
Caller must free.
*/
void
topfqn(Symbol* sym)
{
char* fqn;
char* fqnname;
char* parentfqn;
Symbol* parent;
if(sym->fqn != NULL)
return; /* already defined */
#ifdef USE_NETCDF4
if(!usingclassic) {
parent = sym->container;
/* Recursively compute parent fqn */
if(parent == NULL) { /* implies this is the rootgroup */
assert(sym->grp.is_root);
sym->fqn = strdup("");
return;
} else if(parent->fqn == NULL) {
topfqn(parent);
}
parentfqn = parent->fqn;
fqnname = fqnescape(sym->name);
fqn = (char*)malloc(strlen(fqnname) + strlen(parentfqn) + 1 + 1);
strcpy(fqn,parentfqn);
strcat(fqn,"/");
strcat(fqn,fqnname);
sym->fqn = fqn;
} else
#endif /*USE_NETCDF4*/
{
sym->fqn = strdup(sym->name);
}
}
/**
Return a string representing
the fully qualified name of a nested symbol
(i.e. field or econst).
Caller must free.
*/
void
nestedfqn(Symbol* sym)
{
char* fqn;
char* fqnname;
Symbol* parent;
if(sym->fqn != NULL)
return; /* already defined */
/* Parent must be a type */
parent = sym->container;
assert (parent->objectclass == NC_TYPE);
assert(parent->fqn != NULL);
fqnname = fqnescape(sym->name);
fqn = (char*)malloc(strlen(fqnname) + strlen(parent->fqn) + 1 + 1);
strcpy(fqn,parent->fqn);
strcat(fqn,".");
strcat(fqn,fqnname);
sym->fqn = fqn;
}
/**
Return a string representing
the fully qualified name of an attribute.
Caller must free.
*/
void
attfqn(Symbol* sym)
{
char* fqn;
char* fqnname;
char* parentfqn;
Symbol* parent;
if(sym->fqn != NULL)
return; /* already defined */
assert (sym->objectclass == NC_ATT);
parent = sym->container;
if(parent == NULL)
parentfqn = "";
else
parentfqn = parent->fqn;
fqnname = fqnescape(sym->name);
fqn = (char*)malloc(strlen(fqnname) + strlen(parentfqn) + 1 + 1);
strcpy(fqn,parentfqn);
strcat(fqn,"_");
strcat(fqn,fqnname);
sym->fqn = fqn;
}
#if 0
/* Result is pool alloc'd*/
char*
cprefixed(List* prefix, char* suffix, char* separator)
{
int slen;
int plen;
int i;
char* result;
ASSERT(suffix != NULL);
plen = prefixlen(prefix);
if(prefix == NULL || plen == 0) return codify(suffix);
/* plen > 0*/
slen = 0;
for(i=0;i<plen;i++) {
Symbol* sym = (Symbol*)listget(prefix,i);
slen += (strlen(sym->name)+strlen(separator));
}
slen += strlen(suffix);
slen++; /* for null terminator*/
result = poolalloc(slen);
result[0] = '\0';
/* Leave off the root*/
i = (rootgroup == (Symbol*)listget(prefix,0))?1:0;
for(;i<plen;i++) {
Symbol* sym = (Symbol*)listget(prefix,i);
strcat(result,sym->name); /* append "<prefix[i]/>"*/
strcat(result,separator);
}
strcat(result,suffix); /* append "<suffix>"*/
return result;
}
#endif /*0*/