diff --git a/frmts/hdf4/hdf-eos/EHapi.c b/frmts/hdf4/hdf-eos/EHapi.c index 51557f5b4e58..0f3b6d96fe65 100644 --- a/frmts/hdf4/hdf-eos/EHapi.c +++ b/frmts/hdf4/hdf-eos/EHapi.c @@ -9,9 +9,9 @@ /* Copyright (C) 1996 Hughes and Applied Research Corporation -Permission to use, modify, and distribute this software and its documentation -for any purpose without fee is hereby granted, provided that the above -copyright notice appear in all copies and that both that copyright notice and +Permission to use, modify, and distribute this software and its documentation +for any purpose without fee is hereby granted, provided that the above +copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. */ @@ -29,10 +29,10 @@ static int32 *EHXfidTable = NULL; static int32 *EHXsdTable = NULL; /* define a macro for the string size of the utility strings and some dimension - list strings. The value in previous versions of this code may not be - enough in some cases. The length now is 512 which seems to be more than + list strings. The value in previous versions of this code may not be + enough in some cases. The length now is 512 which seems to be more than enough to hold larger strings. */ - + #define UTLSTR_MAX_SIZE 512 #define UTLSTRSIZE 32000 @@ -45,7 +45,9 @@ static int32 *EHXsdTable = NULL; #define MAX_RETRIES 10 /* Function Prototypes */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) || defined(HDFEOS_SW_WRITE_SUPPORT) static intn EHmetalist(const char *, char *); +#endif static intn EHreset_maxopenfiles(intn); static intn EHget_maxopenfiles(intn *, intn *); static intn EHget_numfiles(void); @@ -197,7 +199,7 @@ EHopen(const char *filename, intn access) /* ------------------------- */ metabuf = (char *) calloc(32000, 1); if(metabuf == NULL) - { + { HEpush(DFE_NOSPACE,"EHopen", __FILE__, __LINE__); return(-1); } @@ -236,7 +238,7 @@ EHopen(const char *filename, intn access) /* Get HDF file ID */ /* --------------- */ #ifndef _PGS_OLDNFS -/* The following loop around the function Hopen is intended to deal with the NFS cache +/* The following loop around the function Hopen is intended to deal with the NFS cache problem when opening file fails with errno = 150 or 151. When NFS cache is updated, this part of change is no longer necessary. 10/18/1999 */ retryCount = 0; @@ -270,7 +272,7 @@ EHopen(const char *filename, intn access) { /* Set HDFEOS version number in file */ /* --------------------------------- */ - + attrIndex = SDfindattr(sdInterfaceID, "HDFEOSVersion"); if (attrIndex == -1) { @@ -293,7 +295,7 @@ EHopen(const char *filename, intn access) { metabuf = (char *) calloc(32000, 1); if(metabuf == NULL) - { + { HEpush(DFE_NOSPACE,"EHopen", __FILE__, __LINE__); return(-1); } @@ -343,7 +345,7 @@ EHopen(const char *filename, intn access) /* Get HDF file ID */ /* --------------- */ #ifndef _PGS_OLDNFS -/* The following loop around the function Hopen is intended to deal with the NFS cache +/* The following loop around the function Hopen is intended to deal with the NFS cache problem when opening file fails with errno = 150 or 151. When NFS cache is updated, this part of change is no longer necessary. 10/18/1999 */ retryCount = 0; @@ -370,10 +372,7 @@ EHopen(const char *filename, intn access) fid = -1; status = -1; HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__); - strcpy(errbuf, "\""); - strcat(errbuf, filename); - strcat(errbuf, "\" (opened for READONLY access)"); - strcat(errbuf, " does not exist."); + snprintf(errbuf, sizeof(errbuf), "\"%s\" (opened for READONLY access) does not exist.", filename); HEreport("%s\n", errbuf); } else { @@ -385,7 +384,7 @@ EHopen(const char *filename, intn access) /* ------------------------- */ if (sdInterfaceID != -1) { - + /* Set open access to read-only */ /* ---------------------------- */ acs = 0; @@ -582,7 +581,7 @@ EHidinfo(int32 fid, int32 * HDFfid, int32 * sdInterfaceID) } - +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -625,7 +624,7 @@ EHfilename(int32 fid, char *filename) return (status); } - +#endif @@ -1011,13 +1010,13 @@ EHstrwithin(const char *target, const char *search, const char delim) /* ----------------------------------------- */ ptr = (char **) calloc(nentries, sizeof(char *)); if(ptr == NULL) - { + { HEpush(DFE_NOSPACE,"EHstrwithin", __FILE__, __LINE__); return(-1); } slen = (int32 *) calloc(nentries, sizeof(int32)); if(slen == NULL) - { + { HEpush(DFE_NOSPACE,"EHstrwithin", __FILE__, __LINE__); free(ptr); return(-1); @@ -1199,13 +1198,13 @@ EHgetid(int32 fid, int32 vgid, const char *objectname, intn code, /* ---------------------------------- */ tags = (int32 *) malloc(sizeof(int32) * nObjects); if(tags == NULL) - { + { HEpush(DFE_NOSPACE,"EHgetid", __FILE__, __LINE__); return(-1); } refs = (int32 *) malloc(sizeof(int32) * nObjects); if(refs == NULL) - { + { HEpush(DFE_NOSPACE,"EHgetid", __FILE__, __LINE__); free(tags); return(-1); @@ -1331,7 +1330,7 @@ EHrevflds(const char *dimlist, char *revdimlist) /* ------------------------------ */ tempdimlist = (char *) malloc(strlen(dimlist) + 1); if(tempdimlist == NULL) - { + { HEpush(DFE_NOSPACE,"EHrevflds", __FILE__, __LINE__); return(-1); } @@ -1347,14 +1346,14 @@ EHrevflds(const char *dimlist, char *revdimlist) /* ----------------------------------------- */ ptr = (char **) calloc(nentries, sizeof(char *)); if(ptr == NULL) - { + { HEpush(DFE_NOSPACE,"EHrevflds", __FILE__, __LINE__); free(tempdimlist); return(-1); } slen = (int32 *) calloc(nentries, sizeof(int32)); if(slen == NULL) - { + { HEpush(DFE_NOSPACE,"EHrevflds", __FILE__, __LINE__); free(ptr); free(tempdimlist); @@ -1398,6 +1397,7 @@ EHrevflds(const char *dimlist, char *revdimlist) } +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -1513,10 +1513,11 @@ EHcntGROUP(char *metabuf[]) return (count); } +#endif - +#if defined(HDFEOS_GD_WRITE_SUPPORT) || defined(HDFEOS_SW_WRITE_SUPPORT) /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -1566,13 +1567,13 @@ EHmetalist(const char *instring, char *outstring) /* ----------------------------------------- */ ptr = (char **) calloc(nentries, sizeof(char *)); if(ptr == NULL) - { + { HEpush(DFE_NOSPACE,"EHmetalist", __FILE__, __LINE__); return(-1); } slen = (int32 *) calloc(nentries, sizeof(int32)); if(slen == NULL) - { + { HEpush(DFE_NOSPACE,"EHmetalist", __FILE__, __LINE__); free(ptr); return(-1); @@ -1634,11 +1635,11 @@ EHmetalist(const char *instring, char *outstring) return (status); } +#endif - - +#if defined(HDFEOS_GD_WRITE_SUPPORT) || defined(HDFEOS_SW_WRITE_SUPPORT) /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -1698,7 +1699,7 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode char *metaptr; /* Metadata pointer */ char *prevmetaptr;/* Previous position of metadata pointer */ char *ptr[8]; /* String pointer array (for dim map parsing) */ - char type[32]; /* Number type descriptor string */ + char type[32] = {0}; /* Number type descriptor string */ char *metaArr[2]; /* Array of metadata positions */ char *colon; /* Colon position */ char *colon2; /* 2nd colon position */ @@ -1711,14 +1712,14 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* ---------------------------------- */ utlstr = (char *) calloc(UTLSTRSIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__); return(-1); } utlstr2 = (char *) calloc(UTLSTRSIZE, sizeof(char)); if(utlstr2 == NULL) - { + { HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__); free(utlstr); return(-1); @@ -1751,7 +1752,7 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* ----------------------------------------------------- */ metabuf = (char *) calloc(32000 * nmeta, 1); if(metabuf == NULL) - { + { HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__); free(utlstr); free(utlstr2); @@ -2004,9 +2005,14 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* Find colon (parse off field name) */ /* --------------------------------- */ colon = strchr(metastr, ':'); + if (colon == NULL) + { + free(utlstr); + free(utlstr2); + return -1; + } *colon = 0; - /* Search for next colon (compression and/or tiling parameters) */ /* ------------------------------------------------------------ */ colon2 = strchr(colon + 1, ':'); @@ -2066,9 +2072,14 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* Find colon (parse off field name) */ /* --------------------------------- */ colon = strchr(metastr, ':'); + if (colon == NULL) + { + free(utlstr); + free(utlstr2); + return -1; + } *colon = 0; - /* Search for next colon (compression and/or tiling parameters) */ /* ------------------------------------------------------------ */ colon2 = strchr(colon + 1, ':'); @@ -2128,13 +2139,17 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* Find colon (parse off merged fieldname) */ /* --------------------------------------- */ colon = strchr(metastr, ':'); - + if (colon == NULL) + { + free(utlstr); + free(utlstr2); + return -1; + } + *colon = 0; /* Make metadata string list from field list */ /* ----------------------------------------- */ EHmetalist(colon + 1, utlstr2); - *colon = 0; - /* Build metadata entry string */ /* --------------------------- */ @@ -2182,9 +2197,14 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* Find colon (parse off point field name) */ /* --------------------------------------- */ colon = strchr(metastr, ':'); + if (colon == NULL) + { + free(utlstr); + free(utlstr2); + return -1; + } *colon = 0; - /* Find beginning and ending of metadata section */ /* --------------------------------------------- */ strcpy(utlstr, "\t\t\t\tLevelName=\""); @@ -2237,15 +2257,25 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* Find colon (parse off parent/child level names from link field) */ /* --------------------------------------------------------------- */ colon = strchr(metastr, ':'); + if (colon == NULL) + { + free(utlstr); + free(utlstr2); + return -1; + } *colon = 0; - /* Find slash (divide parent and child levels) */ /* ------------------------------------------- */ slash = strchr(metastr, '/'); + if (slash == NULL) + { + free(utlstr); + free(utlstr2); + return -1; + } *slash = 0; - /* Build metadata entry string */ /* --------------------------- */ snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%s%s%s%s%d%s", @@ -2314,7 +2344,7 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* ------------------------------------------------------ */ metabuf = (char *) realloc((void *) metabuf, 32000 * (nmeta + 1)); if(metabuf == NULL) - { + { HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__); free(utlstr); free(utlstr2); @@ -2364,6 +2394,7 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode return (status); } +#endif /*----------------------------------------------------------------------------| @@ -2433,6 +2464,11 @@ EHgetmetavalue(char *metaptrs[], const char *parameter, char *retstr) /* Find newline "\n" character */ /* --------------------------- */ newline = strchr(metaptrs[0], '\n'); + if (newline == NULL) + { + retstr[0] = 0; + return -1; + } /* Copy from "=" to "\n" (exclusive) into return string */ /* ---------------------------------------------------- */ @@ -2510,9 +2546,9 @@ EHmetagroup(int32 sdInterfaceID, const char *structname, const char *structcode, /* ---------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE,sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"EHEHmetagroup", __FILE__, __LINE__); - + return( NULL); } /* Determine number of structural metadata "sections" */ @@ -2541,14 +2577,14 @@ EHmetagroup(int32 sdInterfaceID, const char *structname, const char *structcode, /* Allocate space for metadata (in units of 32000 bytes) */ /* ----------------------------------------------------- */ metabuf = (char *) calloc(32000 * nmeta, 1); - + if(metabuf == NULL) - { + { HEpush(DFE_NOSPACE,"EHmetagroup", __FILE__, __LINE__); free(utlstr); return(metabuf); } - + /* Read structural metadata */ /* ------------------------ */ @@ -2649,7 +2685,7 @@ EHmetagroup(int32 sdInterfaceID, const char *structname, const char *structcode, - +#if defined(HDFEOS_GD_WRITE_SUPPORT) || defined(HDFEOS_SW_WRITE_SUPPORT) /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -2729,11 +2765,11 @@ EHfillfld(int32 sdid, int32 rank, CPL_UNUSED int32 truerank, int32 size, int32 o /* -------------------- */ fillbuf = (char *) malloc(totN * size); if(fillbuf == NULL) - { + { HEpush(DFE_NOSPACE,"EHfillfld", __FILE__, __LINE__); return(-1); } - + /* Fill buffer with fill value */ /* --------------------------- */ @@ -2769,7 +2805,7 @@ EHfillfld(int32 sdid, int32 rank, CPL_UNUSED int32 truerank, int32 size, int32 o /* -------------------- */ fillbuf = (char *) malloc(planeN * size * n); if(fillbuf == NULL) - { + { HEpush(DFE_NOSPACE,"EHfillfld", __FILE__, __LINE__); return(-1); } @@ -2824,7 +2860,7 @@ EHfillfld(int32 sdid, int32 rank, CPL_UNUSED int32 truerank, int32 size, int32 o /* -------------------- */ fillbuf = (char *) malloc(dims[rank - 1] * size * n); if(fillbuf == NULL) - { + { HEpush(DFE_NOSPACE,"EHfillfld", __FILE__, __LINE__); return(-1); } @@ -2877,12 +2913,12 @@ EHfillfld(int32 sdid, int32 rank, CPL_UNUSED int32 truerank, int32 size, int32 o return (status); } +#endif - - +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -2934,7 +2970,7 @@ EHbisect(float64(*func) (float64[]), float64 funcParms[], int32 nParms, /* -------------------------------------- */ parms = (float64 *) calloc(nParms + 1, sizeof(float64)); if(parms == NULL) - { + { HEpush(DFE_NOSPACE, "EHbisect", __FILE__, __LINE__); return(-1); } @@ -3026,7 +3062,7 @@ EHbisect(float64(*func) (float64[]), float64 funcParms[], int32 nParms, return (status); } - +#endif @@ -3271,13 +3307,13 @@ EHattrcat(int32 fid, int32 attrVgrpID, char *attrnames, int32 * strbufsize) /* ------------------------------------------- */ tags = (int32 *) malloc(sizeof(int32) * nObjects); if(tags == NULL) - { + { HEpush(DFE_NOSPACE,"EHattrcat", __FILE__, __LINE__); return(-1); } refs = (int32 *) malloc(sizeof(int32) * nObjects); if(refs == NULL) - { + { HEpush(DFE_NOSPACE,"EHattrcat", __FILE__, __LINE__); free(tags); return(-1); diff --git a/frmts/hdf4/hdf-eos/GDapi.c b/frmts/hdf4/hdf-eos/GDapi.c index 4c79a99694ef..638d6581f684 100644 --- a/frmts/hdf4/hdf-eos/GDapi.c +++ b/frmts/hdf4/hdf-eos/GDapi.c @@ -9,9 +9,9 @@ /* Copyright (C) 1996 Hughes and Applied Research Corporation -Permission to use, modify, and distribute this software and its documentation -for any purpose without fee is hereby granted, provided that the above -copyright notice appear in all copies and that both that copyright notice and +Permission to use, modify, and distribute this software and its documentation +for any purpose without fee is hereby granted, provided that the above +copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. */ /***************************************************************************** @@ -34,53 +34,53 @@ Jan 15, 2003 Abe Taaheri Modified for generalization of EASE Grid. Jun 05, 2003 Bruce Beaumont / Abe Taaheri Fixed SQUARE definition. - Added static projection number/name translation + Added static projection number/name translation Added projection table lookup in GDdefproj. Removed projection table from GDdefproj Added projection table lookup in GDprojinfo Removed projection table from GDprojinfo Added cast for compcode in call to SDsetcompress in GDdeffield to avoid compiler errors - Removed declaration for unused variable endptr + Removed declaration for unused variable endptr in GDSDfldsrch Removed initialization code for unused variables in GDSDfldsrch - Removed declarations for unused variables - BCEA_scale, r0, s0, xMtr0, xMtr1, yMtr0, + Removed declarations for unused variables + BCEA_scale, r0, s0, xMtr0, xMtr1, yMtr0, and yMtr1 in GDll2ij Removed initialization code for unused variables in GDll2ij - Added code in GEO projection handling to allow + Added code in GEO projection handling to allow map to span dateline in GDll2ij - Changed "for each point" loop in GDll2ij to - return -2147483648.0 for xVal and yVal if - for_trans returned an error instead of + Changed "for each point" loop in GDll2ij to + return -2147483648.0 for xVal and yVal if + for_trans returned an error instead of returning an error to the caller (Note: MAXLONG is defined as 2147483647.0 in function cproj.c of GCTP) - Added code in GDij2ll to use for_trans to - translate the BCEA corner points from packed + Added code in GDij2ll to use for_trans to + translate the BCEA corner points from packed degrees to meters - Removed declarations for unused variables - BCEA_scale, r0, s0, xMtr, yMtr, epsilon, - beta, qp_cea, kz_cea, eccen, eccen_sq, - phi1, sinphi1, cosphi1, lon, lat, xcor, + Removed declarations for unused variables + BCEA_scale, r0, s0, xMtr, yMtr, epsilon, + beta, qp_cea, kz_cea, eccen, eccen_sq, + phi1, sinphi1, cosphi1, lon, lat, xcor, ycor, and nlatlon from GDij2ll Removed initialization code for unused variables in GDij2ll - Added declarations for xMtr0, yMtr0, xMtr1, and + Added declarations for xMtr0, yMtr0, xMtr1, and yMtr1 in GDij2ll Added special-case code for BCEA - Changed "for each point" loop in GDij2ll to - return PGSd_GCT_IN_ERROR (1.0e51) for - longitude and latitude values if inv_trans - returned an error instead of return an error + Changed "for each point" loop in GDij2ll to + return PGSd_GCT_IN_ERROR (1.0e51) for + longitude and latitude values if inv_trans + returned an error instead of return an error to the caller - Removed declaration for unused variable ii in + Removed declaration for unused variable ii in GDgetpixvalues - Removed declaration for unused variable + Removed declaration for unused variable numTileDims in GDtileinfo - Added error message and error return at the + Added error message and error return at the end of GDll2mm_cea Added return statement to GDll2mm_cea ******************************************************************************/ @@ -101,14 +101,16 @@ extern void inv_init(int32, int32, float64 *, int32, const char *, const char * #define GDIDOFFSET 4194304 #define SQUARE(x) ((x) * (x)) /* x**2 */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) static int32 GDXSDcomb[512*5]; -static char GDXSDname[HDFE_NAMBUFSIZE]; static char GDXSDdims[HDFE_DIMBUFSIZE]; +static char GDXSDname[HDFE_NAMBUFSIZE]; +#endif #define NGRID 200 /* Grid Structure External Arrays */ -struct gridStructure +struct gridStructure { int32 active; int32 IDTable; @@ -146,10 +148,10 @@ struct gridRegion static struct gridRegion *GDXRegion[NGRIDREGN]; /* define a macro for the string size of the utility strings and some dimension - list strings. The value of 80 in the previous version of this code - may not be enough in some cases. The length now is 512 which seems to + list strings. The value of 80 in the previous version of this code + may not be enough in some cases. The length now is 512 which seems to be more than enough to hold larger strings. */ - + #define UTLSTR_MAX_SIZE 512 /* Static projection table */ @@ -220,7 +222,7 @@ static const char * const pixregNames[] = { /* Grid Function Prototypes (internal routines) */ static intn GDchkgdid(int32, const char *, int32 *, int32 *, int32 *); -static intn GDSDfldsrch(int32, int32, const char *, int32 *, int32 *, +static intn GDSDfldsrch(int32, int32, const char *, int32 *, int32 *, int32 *, int32 *, int32 [], int32 *); static intn GDwrrdfield(int32, const char *, const char *, int32 [], int32 [], int32 [], VOIDP datbuf); @@ -230,16 +232,21 @@ static intn GDll2ij(int32, int32, float64 [], int32, int32, int32, float64[], float64[], float64[]); static intn GDgetdefaults(int32, int32, float64[], int32, float64[], float64[]); +#ifdef HDFEOS_GD_WRITE_SUPPORT static intn GDtangentpnts(int32, float64[], float64[], float64[], float64[], float64 [], int32 *); +#endif static intn GDwrrdtile(int32, const char *, const char *, int32 [], VOIDP); +#ifdef UNUSED_BY_GDAL static intn GDll2mm_cea(int32,int32, int32, float64[], int32, int32, float64[], float64[], int32, float64[],float64[], float64[], float64[], float64 *, float64 *); - +#endif +#ifdef HDFEOS_GD_WRITE_SUPPORT static intn GDmm2ll_cea(int32, int32, int32, float64[], int32, int32, float64[], float64[], int32, float64[], float64[], float64[], float64[]); +#endif /*----------------------------------------------------------------------------| | BEGIN_PROLOG | @@ -286,7 +293,7 @@ GDopen(const char *filename, intn l_access) } - +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -470,7 +477,7 @@ GDcreate(int32 fid, const char *gridname, int32 xdimsize, int32 ydimsize, "\t\tYDim=", (int)ydimsize, "\n"); - snprintf(footer, sizeof(footer), + snprintf(footer, sizeof(footer), "%s%s%s%s%s%s%s%d%s", "\t\tGROUP=Dimension\n", "\t\tEND_GROUP=Dimension\n", @@ -549,7 +556,7 @@ GDcreate(int32 fid, const char *gridname, int32 xdimsize, int32 ydimsize, } return (gridID); } - +#endif /*----------------------------------------------------------------------------| @@ -672,13 +679,13 @@ GDattach(int32 fid, const char *gridname) /* ----------------------------------------------------- */ tags = (int32 *) malloc(sizeof(int32) * 2); if(tags == NULL) - { + { HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__); return(-1); } refs = (int32 *) malloc(sizeof(int32) * 2); if(refs == NULL) - { + { HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__); free(tags); return(-1); @@ -729,13 +736,13 @@ GDattach(int32 fid, const char *gridname) /* ----------------------------------------- */ tags = (int32 *) malloc(sizeof(int32) * nObjects); if(tags == NULL) - { + { HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__); return(-1); } refs = (int32 *) malloc(sizeof(int32) * nObjects); if(refs == NULL) - { + { HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__); free(tags); return(-1); @@ -754,7 +761,7 @@ GDattach(int32 fid, const char *gridname) } GDXGrid[i].sdsID = (int32 *) calloc(nSDS, 4); if(GDXGrid[i].sdsID == NULL && nSDS != 0) - { + { HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__); free(tags); free(refs); @@ -881,7 +888,10 @@ GDchkgdid(int32 gridID, const char *routname, /* Compute "reduced" ID */ /* -------------------- */ gID = gridID % idOffset; - + if (gID >= NGRID) + { + return -1; + } /* Check for active grid ID */ /* ------------------------ */ @@ -905,7 +915,7 @@ GDchkgdid(int32 gridID, const char *routname, } - +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -970,11 +980,17 @@ GDdefdim(int32 gridID, const char *dimname, int32 dim) /* -------------------------------------- */ if (status == 0) { - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); /* The cast to char* is somehow nasty since EHinsertmeta can modify */ /* dimname, but not in this case since we call with metacode = 0 */ + int32 dimArray[1] = { dim }; status = EHinsertmeta(sdInterfaceID, gridname, "g", 0L, - (char*) dimname, &dim); + (char*) dimname, dimArray); } return (status); @@ -1051,26 +1067,27 @@ GDdefproj(int32 gridID, int32 projcode, int32 zonecode, int32 spherecode, for (i = 0; i < 13; i++) { + char utlbufSmall[32]; /* If projparm[i] = 0 ... */ if (projparm[i] == 0.0) { - strcpy(utlbuf, "0,"); + strcpy(utlbufSmall, "0,"); } else { /* if projparm[i] is integer ... */ if ((int32) projparm[i] == projparm[i]) { - snprintf(utlbuf, sizeof(utlbuf), "%d%s", + snprintf(utlbufSmall, sizeof(utlbufSmall), "%d%s", (int) projparm[i], ","); } /* else projparm[i] is non-zero floating point ... */ else { - CPLsnprintf(utlbuf, sizeof(utlbuf), "%f%s", projparm[i], ","); + CPLsnprintf(utlbufSmall, sizeof(utlbufSmall), "%f%s", projparm[i], ","); } } - strcat(projparmbuf, utlbuf); + strcat(projparmbuf, utlbufSmall); } slen = (int)strlen(projparmbuf); @@ -1115,7 +1132,12 @@ GDdefproj(int32 gridID, int32 projcode, int32 zonecode, int32 spherecode, /* Insert in structural metadata */ /* ----------------------------- */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); status = EHinsertmeta(sdInterfaceID, gridname, "g", 101L, utlbuf, NULL); } @@ -1185,7 +1207,12 @@ GDblkSOMoffset(int32 gridID, float32 offset[], int32 count, const char *code) /* If SOM projection with projparm[11] non-zero ... */ if (projcode == GCTP_SOM && projparm[11] != 0) { - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); snprintf(utlbuf, sizeof(utlbuf),"%s%s", "_BLKSOM:", gridname); /* Write offset values as attribute */ @@ -1251,7 +1278,10 @@ GDdefcomp(int32 gridID, int32 compcode, intn compparm[]) if (status == 0) { gID = gridID % idOffset; - + if (gID >= NGRID) + { + return -1; + } /* Set compression code in compression external array */ GDXGrid[gID].compcode = compcode; @@ -1333,6 +1363,8 @@ GDdeftile(int32 gridID, int32 tilecode, int32 tilerank, int32 tiledims[]) if (status == 0) { gID = gridID % idOffset; + if (gID >= NGRID) + return -1; for (i = 0; i < 8; i++) { @@ -1430,12 +1462,17 @@ GDdeforigin(int32 gridID, int32 origincode) { /* If proper origin code then write to structural metadata */ /* ------------------------------------------------------- */ - if (origincode >= 0 && origincode < (int32)sizeof(originNames)) + if (origincode >= 0 && origincode < (int32)(sizeof(originNames) / sizeof(originNames[0]))) { + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } snprintf(utlbuf, sizeof(utlbuf),"%s%s%s", "\t\tGridOrigin=", originNames[origincode], "\n"); - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + Vgetname(GDXGrid[gID].IDTable, gridname); status = EHinsertmeta(sdInterfaceID, gridname, "g", 101L, utlbuf, NULL); } @@ -1502,12 +1539,17 @@ GDdefpixreg(int32 gridID, int32 pixregcode) { /* If proper pix reg code then write to structural metadata */ /* -------------------------------------------------------- */ - if (pixregcode >= 0 && pixregcode < (int32)sizeof(pixregNames)) + if (pixregcode >= 0 && pixregcode < (int32)(sizeof(pixregNames) / sizeof(pixregNames[0]))) { + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } snprintf(utlbuf, sizeof(utlbuf),"%s%s%s", "\t\tPixelRegistration=", pixregNames[pixregcode], "\n"); - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + Vgetname(GDXGrid[gID].IDTable, gridname); status = EHinsertmeta(sdInterfaceID, gridname, "g", 101L, utlbuf, NULL); } @@ -1522,7 +1564,7 @@ GDdefpixreg(int32 gridID, int32 pixregcode) return (status); } - +#endif @@ -1580,7 +1622,7 @@ GDdiminfo(int32 gridID, const char *dimname) /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDdiminfo", __FILE__, __LINE__); return(-1); } @@ -1598,7 +1640,13 @@ GDdiminfo(int32 gridID, const char *dimname) { /* Get grid name */ /* ------------- */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); /* Get pointers to "Dimension" section within SM */ @@ -1610,7 +1658,7 @@ GDdiminfo(int32 gridID, const char *dimname) { free(utlstr); return(-1); - } + } /* Search for dimension name (surrounded by quotes) */ /* ------------------------------------------------ */ @@ -1710,7 +1758,7 @@ GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDgridinfo", __FILE__, __LINE__); return(-1); } @@ -1722,7 +1770,13 @@ GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, { /* Get grid name */ /* ------------- */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); /* Get pointers to grid structure section within SM */ @@ -1734,7 +1788,7 @@ GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, { free(utlstr); return(-1); - } + } /* Get xdimsize if requested */ @@ -1903,7 +1957,7 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDprojinfo", __FILE__, __LINE__); return(-1); } @@ -1916,7 +1970,14 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, { /* Get grid name */ /* ------------- */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + + Vgetname(GDXGrid[gID].IDTable, gridname); /* Get pointers to grid structure section within SM */ @@ -1928,7 +1989,7 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, { free(utlstr); return(-1); - } + } /* Get projcode if requested */ @@ -1968,7 +2029,7 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, /* Get zonecode if requested */ /* ------------------------- */ - if (zonecode != NULL) + if (projcode && zonecode != NULL) { *zonecode = -1; @@ -1995,7 +2056,7 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, /* Get projection parameters if requested */ /* -------------------------------------- */ - if (projparm != NULL) + if (projcode && projparm != NULL) { /* @@ -2051,7 +2112,7 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, /* Get spherecode if requested */ /* --------------------------- */ - if (spherecode != NULL) + if (projcode && spherecode != NULL) { *spherecode = 0; @@ -2126,7 +2187,7 @@ GDorigininfo(int32 gridID, int32 * origincode) /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDorigininfo", __FILE__, __LINE__); return(-1); } @@ -2149,7 +2210,14 @@ GDorigininfo(int32 gridID, int32 * origincode) /* Get grid name */ /* ------------- */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + + Vgetname(GDXGrid[gID].IDTable, gridname); /* Get pointers to grid structure section within SM */ @@ -2161,7 +2229,7 @@ GDorigininfo(int32 gridID, int32 * origincode) { free(utlstr); return(-1); - } + } statmeta = EHgetmetavalue(metaptrs, "GridOrigin", utlstr); @@ -2172,7 +2240,7 @@ GDorigininfo(int32 gridID, int32 * origincode) * If "GridOrigin" string found in metadata then convert to * numeric origin code (fixed added: Jan 97) */ - for (i = 0; i < (intn)sizeof(originNames); i++) + for (i = 0; i < (intn)(sizeof(originNames) / sizeof(originNames[0])); i++) { if (strcmp(utlstr, originNames[i]) == 0) { @@ -2248,7 +2316,7 @@ GDpixreginfo(int32 gridID, int32 * pixregcode) /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDpixreginfo", __FILE__, __LINE__); return(-1); } @@ -2265,7 +2333,14 @@ GDpixreginfo(int32 gridID, int32 * pixregcode) *pixregcode = 0; /* Get grid name */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + + Vgetname(GDXGrid[gID].IDTable, gridname); /* Get pointers to grid structure section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", @@ -2275,7 +2350,7 @@ GDpixreginfo(int32 gridID, int32 * pixregcode) { free(utlstr); return(-1); - } + } statmeta = EHgetmetavalue(metaptrs, "PixelRegistration", utlstr); @@ -2287,7 +2362,7 @@ GDpixreginfo(int32 gridID, int32 * pixregcode) * to numeric origin code (fixed added: Jan 97) */ - for (i = 0; i < (intn)sizeof(pixregNames); i++) + for (i = 0; i < (intn)(sizeof(pixregNames) / sizeof(pixregNames[0])); i++) { if (strcmp(utlstr, pixregNames[i]) == 0) { @@ -2357,7 +2432,7 @@ GDcompinfo(int32 gridID, const char *fieldname, int32 * compcode, intn compparm[ /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDcompinfo", __FILE__, __LINE__); return(-1); } @@ -2368,7 +2443,13 @@ GDcompinfo(int32 gridID, const char *fieldname, int32 * compcode, intn compparm[ if (status == 0) { /* Get grid name */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); /* Get pointers to "DataField" section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", @@ -2377,7 +2458,7 @@ GDcompinfo(int32 gridID, const char *fieldname, int32 * compcode, intn compparm[ { free(utlstr); return(-1); - } + } /* Search for field */ @@ -2406,7 +2487,7 @@ GDcompinfo(int32 gridID, const char *fieldname, int32 * compcode, intn compparm[ if (statmeta == 0) { /* Loop through compression types until match */ - for (i = 0; i < (intn)sizeof(HDFcomp); i++) + for (i = 0; i < (intn)(sizeof(HDFcomp) / sizeof(HDFcomp[0])); i++) { if (strcmp(utlstr, HDFcomp[i]) == 0) { @@ -2555,7 +2636,7 @@ GDfieldinfo(int32 gridID, const char *fieldname, int32 * rank, int32 dims[], /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDfieldinfo", __FILE__, __LINE__); return(-1); } @@ -2566,8 +2647,13 @@ GDfieldinfo(int32 gridID, const char *fieldname, int32 * rank, int32 dims[], if (status == 0) { - - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", "DataField", metaptrs); @@ -2575,7 +2661,7 @@ GDfieldinfo(int32 gridID, const char *fieldname, int32 * rank, int32 dims[], { free(utlstr); return(-1); - } + } /* Search for field */ @@ -2691,7 +2777,7 @@ GDfieldinfo(int32 gridID, const char *fieldname, int32 * rank, int32 dims[], - +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -2818,6 +2904,10 @@ GDdeffield(int32 gridID, const char *fieldname, const char *dimlist, { /* Remove offset from grid ID & get gridname */ gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } Vgetname(GDXGrid[gID].IDTable, gridname); @@ -2825,13 +2915,13 @@ GDdeffield(int32 gridID, const char *fieldname, const char *dimlist, /* ---------------------------------------------------------------- */ dimbuf = (char *) calloc(strlen(dimlist) + 64, 1); if(dimbuf == NULL) - { + { HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); return(-1); } dimlist0 = (char *) calloc(strlen(dimlist) + 64, 1); if(dimlist0 == NULL) - { + { HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); free(dimbuf); return(-1); @@ -2896,7 +2986,7 @@ GDdeffield(int32 gridID, const char *fieldname, const char *dimlist, /* ------------------------------------- */ dimcheck = (char *) calloc(comma - dimbuf + 1, 1); if(dimcheck == NULL) - { + { HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); free(dimbuf); free(dimlist0); @@ -2953,12 +3043,13 @@ GDdeffield(int32 gridID, const char *fieldname, const char *dimlist, foundAllDim = 0; if (first == 1) { - strcpy(utlbuf, dimcheck); + snprintf(utlbuf, sizeof(utlbuf), "%s", dimcheck); } else { - strcat(utlbuf, ","); - strcat(utlbuf, dimcheck); + char utlbufTmp[sizeof(utlbuf)]; + strcpy(utlbufTmp, utlbuf); + CPLsnprintf(utlbuf, sizeof(utlbuf), "%s,%s", utlbufTmp, dimcheck); } first = 0; } @@ -2980,7 +3071,7 @@ GDdeffield(int32 gridID, const char *fieldname, const char *dimlist, { /* if ((intn) strlen(fieldname) > MAX_NC_NAME - 7) ** this was changed because HDF4.1r3 made a change in the -** hlimits.h file. We have notified NCSA and asked to have +** hlimits.h file. We have notified NCSA and asked to have ** it made the same as in previous versions of HDF ** see ncr 26314. DaW Apr 2000 */ @@ -3298,7 +3389,7 @@ GDdeffield(int32 gridID, const char *fieldname, const char *dimlist, realloc((void *) GDXGrid[gID].sdsID, (GDXGrid[gID].nSDS + 1) * 4); if(GDXGrid[gID].sdsID == NULL) - { + { HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); free(dimlist0); return(-1); @@ -3310,7 +3401,7 @@ GDdeffield(int32 gridID, const char *fieldname, const char *dimlist, /* -------------------- */ GDXGrid[gID].sdsID = (int32 *) calloc(1, 4); if(GDXGrid[gID].sdsID == NULL) - { + { HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); free(dimlist0); return(-1); @@ -3473,13 +3564,20 @@ GDwritefieldmeta(int32 gridID, const char *fieldname, const char *dimlist, { snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist); - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } + + Vgetname(GDXGrid[gID].IDTable, gridname); + int32 numbertypeArray[1] = { numbertype }; status = EHinsertmeta(sdInterfaceID, gridname, "g", 4L, - utlbuf, &numbertype); + utlbuf, numbertypeArray); } return (status); } - +#endif @@ -3547,7 +3645,7 @@ GDSDfldsrch(int32 gridID, int32 sdInterfaceID, const char *fieldname, /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDSDfldsrch", __FILE__, __LINE__); return(-1); } @@ -3559,7 +3657,11 @@ GDSDfldsrch(int32 gridID, int32 sdInterfaceID, const char *fieldname, /* Compute "reduced" grid ID */ /* ------------------------- */ gID = gridID % idOffset; - + if (gID >= NGRID) + { + free(utlstr); + return -1; + } /* Loop through all SDSs in grid */ /* ----------------------------- */ @@ -3593,7 +3695,7 @@ GDSDfldsrch(int32 gridID, int32 sdInterfaceID, const char *fieldname, { free(utlstr); return(-1); - } + } #ifdef broken_logic /* Initialize metaptr to beg. of section */ @@ -3982,7 +4084,7 @@ GDwrrdfield(int32 gridID, const char *fieldname, const char *code, return (status); } - +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -4026,7 +4128,7 @@ GDwritefield(int32 gridID, const char *fieldname, data); return (status); } - +#endif @@ -4132,7 +4234,12 @@ GDwrrdattr(int32 gridID, const char *attrname, int32 numbertype, int32 count, { /* Perform Attribute I/O */ /* --------------------- */ - attrVgrpID = GDXGrid[gridID % idOffset].VIDTable[1]; + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } + attrVgrpID = GDXGrid[gID].VIDTable[1]; status = EHattr(fid, attrVgrpID, attrname, numbertype, count, wrcode, datbuf); } @@ -4140,7 +4247,7 @@ GDwrrdattr(int32 gridID, const char *attrname, int32 numbertype, int32 count, } - +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -4184,7 +4291,7 @@ GDwriteattr(int32 gridID, const char *attrname, int32 numbertype, int32 count, return (status); } - +#endif /*----------------------------------------------------------------------------| @@ -4278,7 +4385,12 @@ GDattrinfo(int32 gridID, const char *attrname, int32 * numbertype, int32 * count status = GDchkgdid(gridID, "GDattrinfo", &fid, &dum, &dum); - attrVgrpID = GDXGrid[gridID % idOffset].VIDTable[1]; + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } + attrVgrpID = GDXGrid[gID].VIDTable[1]; status = EHattrinfo(fid, attrVgrpID, attrname, numbertype, count); @@ -4342,7 +4454,12 @@ GDinqattrs(int32 gridID, char *attrnames, int32 * strbufsize) if (status == 0) { - attrVgrpID = GDXGrid[gridID % idOffset].VIDTable[1]; + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } + attrVgrpID = GDXGrid[gID].VIDTable[1]; nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize); } @@ -4419,7 +4536,7 @@ GDinqdims(int32 gridID, char *dimnames, int32 dims[]) /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDinqdims", __FILE__, __LINE__); return(-1); } @@ -4435,7 +4552,13 @@ GDinqdims(int32 gridID, char *dimnames, int32 dims[]) { /* Get grid name */ /* ------------- */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); /* Get pointers to "Dimension" section within SM */ @@ -4446,8 +4569,8 @@ GDinqdims(int32 gridID, char *dimnames, int32 dims[]) { free(utlstr); return(-1); - } - + } + /* If dimension names are requested then "clear" name buffer */ /* --------------------------------------------------------- */ @@ -4575,7 +4698,7 @@ GDinqfields(int32 gridID, char *fieldlist, int32 rank[], /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDinqfields", __FILE__, __LINE__); return(-1); } @@ -4591,7 +4714,13 @@ GDinqfields(int32 gridID, char *fieldlist, int32 rank[], { /* Get grid name */ /* ------------- */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); /* Get pointers to "DataField" section within SM */ @@ -4602,7 +4731,7 @@ GDinqfields(int32 gridID, char *fieldlist, int32 rank[], { free(utlstr); return(-1); - } + } /* If field names are desired then "clear" name buffer */ @@ -4756,16 +4885,18 @@ GDnentries(int32 gridID, int32 entrycode, int32 * strbufsize) int32 nVal = 0; /* Number of strings to search for */ char *metabuf = NULL; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ + char *metaptrs[2] = {NULL, NULL};/* Pointers to begin and end of SM section */ char gridname[80]; /* Grid Name */ char *utlstr;/* Utility string */ char valName[2][32]; /* Strings to search for */ + memset(valName, 0, sizeof(valName)); + /* Allocate space for utility string */ /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"GDnentries", __FILE__, __LINE__); return(-1); } @@ -4774,7 +4905,14 @@ GDnentries(int32 gridID, int32 entrycode, int32 * strbufsize) if (status == 0) { /* Get grid name */ - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + + Vgetname(GDXGrid[gID].IDTable, gridname); /* Zero out string buffer size */ *strbufsize = 0; @@ -4794,8 +4932,8 @@ GDnentries(int32 gridID, int32 entrycode, int32 * strbufsize) { free(utlstr); return(-1); - } - + } + nVal = 1; strcpy(&valName[0][0], "DimensionName"); } @@ -4809,7 +4947,7 @@ GDnentries(int32 gridID, int32 entrycode, int32 * strbufsize) { free(utlstr); return(-1); - } + } nVal = 1; strcpy(&valName[0][0], "DataFieldName"); @@ -4817,6 +4955,11 @@ GDnentries(int32 gridID, int32 entrycode, int32 * strbufsize) break; } + if (!metabuf || metaptrs[0] == NULL) + { + free(metabuf); + return -1; + } /* * Check for presence of 'GROUP="' string If found then old metadata, @@ -4931,6 +5074,7 @@ GDinqgrid(const char *filename, char *gridlist, int32 * strbufsize) } +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -5016,7 +5160,7 @@ GDsetfillvalue(int32 gridID, const char *fieldname, VOIDP fillval) } return (status); } - +#endif @@ -5135,13 +5279,18 @@ GDdetach(int32 gridID) { intn i; /* Loop index */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) intn j; /* Loop index */ +#endif intn k; /* Loop index */ intn status = 0; /* routine return status variable */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) intn statusFill = 0; /* return status from GDgetfillvalue */ +#endif - int32 *namelen; /* Pointer to name string length array */ - int32 *dimlen; /* Pointer to dim string length array */ + int32 *namelen = NULL; /* Pointer to name string length array */ + int32 *dimlen = NULL; /* Pointer to dim string length array */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) int32 slen1[3]; /* String length array 1 */ int32 slen2[3]; /* String length array 2 */ int32 nflds; /* Number of fields */ @@ -5150,29 +5299,39 @@ GDdetach(int32 gridID) int32 sdid; /* SDS ID */ int32 vgid; /* Vgroup ID */ int32 dims[3]; /* Dimension array */ - int32 *offset; /* Pointer to merged field offset array */ - int32 *indvdims; /* Pointer to merged field size array */ +#endif + int32 *offset = NULL; /* Pointer to merged field offset array */ + int32 *indvdims = NULL; /* Pointer to merged field size array */ int32 sdInterfaceID; /* SDS interface ID */ int32 gID; /* Grid ID - offset */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) int32 nflds0; /* Number of fields */ - int32 *namelen0; /* Pointer to name string length array */ +#endif + int32 *namelen0 = NULL; /* Pointer to name string length array */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) int32 rank; /* Rank of merged field */ int32 truerank; /* True rank of merged field */ +#endif int32 idOffset = GDIDOFFSET; /* Grid ID offset */ int32 dum; /* Dummy variable */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) char *nambuf; /* Pointer to name buffer */ - char **nameptr; /* Pointer to name string pointer array */ - char **dimptr; /* Pointer to dim string pointer array */ - char **nameptr0; /* Pointer to name string pointer array */ +#endif + char **nameptr = NULL; /* Pointer to name string pointer array */ + char **dimptr = NULL; /* Pointer to dim string pointer array */ + char **nameptr0 = NULL; /* Pointer to name string pointer array */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) char *ptr1[3]; /* String pointer array */ char *ptr2[3]; /* String pointer array */ char dimbuf1[128]; /* Dimension buffer 1 */ char dimbuf2[128]; /* Dimension buffer 2 */ +#endif char gridname[VGNAMELENMAX + 1]; /* Grid name */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) char *utlbuf; /* Utility buffer */ char fillval[32];/* Fill value buffer */ - +#endif status = GDchkgdid(gridID, "GDdetach", &dum, &sdInterfaceID, &dum); @@ -5180,32 +5339,40 @@ GDdetach(int32 gridID) if (status == 0) { gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } Vgetname(GDXGrid[gID].IDTable, gridname); /* SDS combined fields */ /* ------------------- */ +#if defined(HDFEOS_GD_WRITE_SUPPORT) if (strlen(GDXSDname) == 0) +#endif { +#if defined(HDFEOS_GD_WRITE_SUPPORT) nflds = 0; +#endif /* Allocate "dummy" arrays so free() doesn't bomb later */ /* ---------------------------------------------------- */ nameptr = (char **) calloc(1, sizeof(char *)); if(nameptr == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); return(-1); } namelen = (int32 *) calloc(1, sizeof(int32)); if(namelen == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); return(-1); } nameptr0 = (char **) calloc(1, sizeof(char *)); if(nameptr0 == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5213,7 +5380,7 @@ GDdetach(int32 gridID) } namelen0 = (int32 *) calloc(1, sizeof(int32)); if(namelen0 == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5222,7 +5389,7 @@ GDdetach(int32 gridID) } dimptr = (char **) calloc(1, sizeof(char *)); if(dimptr == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5232,7 +5399,7 @@ GDdetach(int32 gridID) } dimlen = (int32 *) calloc(1, sizeof(int32)); if(dimlen == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5243,7 +5410,7 @@ GDdetach(int32 gridID) } offset = (int32 *) calloc(1, sizeof(int32)); if(offset == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5255,7 +5422,7 @@ GDdetach(int32 gridID) } indvdims = (int32 *) calloc(1, sizeof(int32)); if(indvdims == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5267,6 +5434,7 @@ GDdetach(int32 gridID) return(-1); } } +#if defined(HDFEOS_GD_WRITE_SUPPORT) else { /* @@ -5285,20 +5453,20 @@ GDdetach(int32 gridID) /* ----------------------------------------- */ nameptr = (char **) calloc(nflds, sizeof(char *)); if(nameptr == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); return(-1); } namelen = (int32 *) calloc(nflds, sizeof(int32)); if(namelen == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); return(-1); } nameptr0 = (char **) calloc(nflds, sizeof(char *)); if(nameptr0 == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5306,7 +5474,7 @@ GDdetach(int32 gridID) } namelen0 = (int32 *) calloc(nflds, sizeof(int32)); if(namelen0 == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5315,7 +5483,7 @@ GDdetach(int32 gridID) } dimptr = (char **) calloc(nflds, sizeof(char *)); if(dimptr == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5325,7 +5493,7 @@ GDdetach(int32 gridID) } dimlen = (int32 *) calloc(nflds, sizeof(int32)); if(dimlen == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5336,7 +5504,7 @@ GDdetach(int32 gridID) } offset = (int32 *) calloc(nflds, sizeof(int32)); if(offset == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5348,7 +5516,7 @@ GDdetach(int32 gridID) } indvdims = (int32 *) calloc(nflds, sizeof(int32)); if(indvdims == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5366,7 +5534,6 @@ GDdetach(int32 gridID) nflds = EHparsestr(GDXSDdims, ';', dimptr, dimlen); } - for (i = 0; i < nflds; i++) { if (GDXSDcomb[5 * i] != 0 && @@ -5374,7 +5541,7 @@ GDdetach(int32 gridID) { nambuf = (char *) calloc(strlen(GDXSDname) + 1, 1); if(nambuf == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -5383,11 +5550,12 @@ GDdetach(int32 gridID) free(dimptr); free(dimlen); free(offset); + free(indvdims); return(-1); } utlbuf = (char *) calloc(strlen(GDXSDname) * 2 + 7, 1); if(utlbuf == NULL) - { + { HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); free(nambuf); free(nameptr); @@ -5397,6 +5565,7 @@ GDdetach(int32 gridID) free(dimptr); free(dimlen); free(offset); + free(indvdims); return(-1); } @@ -5621,7 +5790,7 @@ GDdetach(int32 gridID) strcat(GDXSDname, ","); strcat(GDXSDdims, ";"); } - +#endif /* Free up a bunch of dynamically allocated arrays */ @@ -5782,7 +5951,7 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], float64 lon, lat, plat, x, y; float64 plon, tlon, llon, rlon, pplon, LLon, LLat, RLon, RLat; - + /* invoke GCTP initialization routine */ /* ---------------------------------- */ @@ -5822,7 +5991,7 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], LLat = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LAT, HDFE_DEG_RAD); RLon = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LON, HDFE_DEG_RAD); RLat = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LAT, HDFE_DEG_RAD); - + errorcode = for_trans[projcode] (LLon, LLat, &x, &y); if (errorcode != 0) { @@ -5833,7 +6002,7 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], } upleftpt[0] = x; upleftpt[1] = y; - + errorcode = for_trans[projcode] (RLon, RLat, &x, &y); if (errorcode != 0) { @@ -5844,10 +6013,10 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], } lowrightpt[0] = x; lowrightpt[1] = y; - + } - - + + /* Compute Default Boundary Points for Polar Sterographic */ /* ------------------------------------------------------ */ if (projcode == GCTP_PS && @@ -5906,7 +6075,7 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], } upleftpt[0] = x; - + errorcode = for_trans[projcode] (rlon, 0.0, &x, &y); if (errorcode != 0) { @@ -5917,7 +6086,7 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], } lowrightpt[0] = x; - + /* * Compute the upperleft and lowright y values based on the south or * north polar projection @@ -5935,7 +6104,7 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], } upleftpt[1] = y; - + errorcode = for_trans[projcode] (tlon, 0.0, &x, &y); if (errorcode != 0) { @@ -5946,7 +6115,7 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], } lowrightpt[1] = y; - + } else { @@ -5960,7 +6129,7 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], } upleftpt[1] = y; - + errorcode = for_trans[projcode] (plon, 0.0, &x, &y); if (errorcode != 0) { @@ -5971,7 +6140,7 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], } lowrightpt[1] = y; - + } } @@ -6393,8 +6562,8 @@ GDll2ij(int32 projcode, int32 zonecode, float64 projparm[], of r_major and r_minor) using GCTP */ /* ----------------------------------------- */ errorcode = for_trans[projcode] (lonrad0, latrad0, &xMtr0, &yMtr0); - - + + /* Report error if any */ /* ------------------- */ if (errorcode != 0) @@ -6409,8 +6578,8 @@ GDll2ij(int32 projcode, int32 zonecode, float64 projparm[], of r_major and r_minor) using GCTP */ /* ----------------------------------------- */ errorcode = for_trans[projcode] (lonrad, latrad, &xMtr1, &yMtr1); - - + + /* Report error if any */ /* ------------------- */ if (errorcode != 0) @@ -6477,7 +6646,7 @@ GDll2ij(int32 projcode, int32 zonecode, float64 projparm[], /* ----------------------------------------- */ errorcode = for_trans[projcode] (lonrad, latrad, &xMtr, &yMtr); - + /* Report error if any */ /* ------------------- */ if (errorcode != 0) @@ -6493,13 +6662,13 @@ GDll2ij(int32 projcode, int32 zonecode, float64 projparm[], else { /* if projection is BCEA normalize x and y by cell size and measure it from the upperleft corner of the grid */ - + /* Compute scaled distance to point from origin */ /* -------------------------------------------- */ if( projcode == GCTP_BCEA) { xVal = (xMtr - xMtr0) / scaleX; - yVal = (yMtr - yMtr0) / scaleY; + yVal = (yMtr - yMtr0) / scaleY; } else { @@ -6533,7 +6702,7 @@ GDll2ij(int32 projcode, int32 zonecode, float64 projparm[], - +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -6601,9 +6770,9 @@ GDrs2ll(int32 projcode, float64 projparm[], float64 eccen, eccen_sq; float64 phi1, sinphi1, cosphi1; float64 scaleX, scaleY; - + int32 zonecode=0; - + int32 spherecode=0; float64 lon[2],lat[2]; float64 xcor[2], ycor[2]; @@ -6620,7 +6789,7 @@ GDrs2ll(int32 projcode, float64 projparm[], } else { - qp_cea = + qp_cea = (1.0 - eccen_sq)*((1.0/(1.0 - eccen_sq))-(1.0/(2.0*eccen))* log((1.0 - eccen)/(1.0 + eccen))); } @@ -6629,7 +6798,7 @@ GDrs2ll(int32 projcode, float64 projparm[], sinphi1 = sin(phi1); kz_cea = cosphi1/(sqrt(1.0 - (eccen_sq*sinphi1*sinphi1))); } - + @@ -6689,10 +6858,10 @@ GDrs2ll(int32 projcode, float64 projparm[], /* ------------------------------------------------------ */ if (projcode == GCTP_BCEA) { - + inv_init(projcode, 0, projparm, 0, NULL, NULL, &errorcode, inv_trans); - + /* Report error if any */ /* ------------------- */ if (errorcode != 0) @@ -6707,7 +6876,7 @@ GDrs2ll(int32 projcode, float64 projparm[], /* ------------------ */ for (i = 0; i < npnts; i++) { - /* Convert from EASE grid's (r,s) to lon/lat (radians) + /* Convert from EASE grid's (r,s) to lon/lat (radians) using GCTP */ /* --------------------------------------------------- */ nlatlon = 2; @@ -6715,32 +6884,32 @@ GDrs2ll(int32 projcode, float64 projparm[], lon[1] = lowright[0]; lat[0] = upleft[1]; lat[1] = lowright[1]; - status = + status = GDll2mm_cea(projcode,zonecode,spherecode,projparm, xdimsize, ydimsize, upleft, lowright, nlatlon, lon, lat, xcor, ycor, &scaleX, &scaleY); - + if (status == -1) { HEpush(DFE_GENAPP, "GDrs2ll", __FILE__, __LINE__); return (status); } - + xMtr = (r[i]/ scaleX + pixadjX - 0.5)* scaleX; yMtr = - (s[i]/fabs(scaleY) + pixadjY - 0.5)* fabs(scaleY); - - /* allow .5 cell tolerance in arcsin function + + /* allow .5 cell tolerance in arcsin function (used in bceainv function) so that grid coordinates which are less than .5 cells - above 90.00N or below 90.00S are given lat of 90.00 + above 90.00N or below 90.00S are given lat of 90.00 */ epsilon = 1 + 0.5 * (fabs(scaleY)/projparm[0]); beta = 2.0 * (yMtr - projparm[7]) * kz_cea/(projparm[0] * qp_cea); - + if( fabs (beta) > epsilon) { status = -1; @@ -6767,7 +6936,7 @@ GDrs2ll(int32 projcode, float64 projparm[], errorcode = inv_trans[projcode] (xMtr, yMtr, &lonrad, &latrad); } - + /* Report error if any */ /* ------------------- */ if (errorcode != 0) @@ -6777,7 +6946,7 @@ GDrs2ll(int32 projcode, float64 projparm[], HEreport("GCTP Error: %d\n", errorcode); return (status); } - + /* Convert from radians to decimal degrees */ /* --------------------------------------- */ longitude[i] = EHconvAng(lonrad, HDFE_RAD_DEG); @@ -6788,12 +6957,13 @@ GDrs2ll(int32 projcode, float64 projparm[], - + return (status); } +#endif - +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -6981,7 +7151,6 @@ homDyDtheta(float64 parms[]) - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -7062,13 +7231,13 @@ GDtangentpnts(int32 projcode, float64 projparm[], float64 cornerlon[], { case GCTP_MERCAT: { - /* No need for tangent points, since MERCAT projection + /* No need for tangent points, since MERCAT projection is rectangular */ } break; case GCTP_BCEA: { - /* No need for tangent points, since BCEA projection + /* No need for tangent points, since BCEA projection is rectangular */ } break; @@ -7663,13 +7832,13 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) utlbuf = (char *)calloc(128, sizeof(char)); if(utlbuf == NULL) - { + { HEpush(DFE_NOSPACE,"GDdefboxregion", __FILE__, __LINE__); return(-1); } gridname = (char *)calloc(128, sizeof(char)); if(gridname == NULL) - { + { HEpush(DFE_NOSPACE,"GDdefboxregion", __FILE__, __LINE__); free(utlbuf); return(-1); @@ -7687,8 +7856,8 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) /* ------------- */ status = GDgridinfo(gridID, &xdimsize, &ydimsize, upleftpt, lowrightpt); - - + + /* If error then bail */ /* ------------------ */ if (status != 0) @@ -7759,11 +7928,19 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) /* ------------------------------------------------- */ status = GDtangentpnts(projcode, projparm, cornerlon, cornerlat, longitude, latitude, &npnts); - + /* If SOM projection with projparm[11] non-zero ... */ if (projcode == GCTP_SOM && projparm[11] != 0) { - Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlbuf); + free(gridname); + return -1; + } + + Vgetname(GDXGrid[gID].IDTable, gridname); snprintf(utlbuf, 128, "%s%s", "_BLKSOM:", gridname); status = GDreadattr(gridID, utlbuf, offset); @@ -7827,7 +8004,7 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) /* Check whether subset region is outside grid region */ /* -------------------------------------------------- */ - if (minCol >= xdimsize || minRow >= ydimsize || + if (minCol >= xdimsize || minRow >= ydimsize || maxCol < 0 || maxRow < 0) { if ( blockindexstart == -1 && (projparm[11]) == j) @@ -7943,7 +8120,7 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) GDXRegion[i] = (struct gridRegion *) calloc(1, sizeof(struct gridRegion)); if(GDXRegion[i] == NULL) - { + { HEpush(DFE_NOSPACE,"GDdefboxregion", __FILE__, __LINE__); free(utlbuf); free(gridname); @@ -8036,7 +8213,7 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) lon[1] = lowrightpt[0]; lat[0] = upleftpt[1]; lat[1] = lowrightpt[1]; - status = + status = GDll2mm_cea(projcode,zonecode,spherecode,projparm, xdimsize, ydimsize, upleftpt, lowrightpt,nlatlon, @@ -8044,8 +8221,8 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) xcor, ycor, &xscale, &yscale); upleftpt_m[0] = xcor[0]; upleftpt_m[1] = ycor[0]; - - + + if (status == -1) { HEpush(DFE_GENAPP, "GDdefboxregion", __FILE__, __LINE__); @@ -8076,7 +8253,7 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) status = GDmm2ll_cea(projcode, zonecode, spherecode, projparm, xdimsize, ydimsize, upleftpt, lowrightpt, npnts, - xmtr, ymtr, + xmtr, ymtr, longitude, latitude); if (status == -1) { @@ -8116,17 +8293,17 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) } else { - GDXRegion[i]->upleftpt[0] = + GDXRegion[i]->upleftpt[0] = (lowrightpt[0] - upleftpt[0])* (offset[blockindexstart-1]/xdimsize) + upleftpt[0]; - GDXRegion[i]->upleftpt[1] = + GDXRegion[i]->upleftpt[1] = (lowrightpt[1] - upleftpt[1])* (blockindexstart+1-1) + upleftpt[1]; - GDXRegion[i]->lowrightpt[0] = + GDXRegion[i]->lowrightpt[0] = (lowrightpt[0] - upleftpt[0])* (offset[blockindexstart-1]/xdimsize) + lowrightpt[0]; - GDXRegion[i]->lowrightpt[1] = + GDXRegion[i]->lowrightpt[1] = (lowrightpt[1] - upleftpt[1])* (blockindexstart+1-1) + lowrightpt[1]; @@ -8172,20 +8349,20 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) regionID = i; break; } - + } } - + } free(utlbuf); free(gridname); return (regionID); } +#endif - - +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -8770,7 +8947,7 @@ GDdupregion(int32 oldregionID) GDXRegion[i] = (struct gridRegion *) calloc(1, sizeof(struct gridRegion)); if(GDXRegion[i] == NULL) - { + { HEpush(DFE_NOSPACE,"GDdupregion", __FILE__, __LINE__); return(-1); } @@ -8778,7 +8955,7 @@ GDdupregion(int32 oldregionID) /* Copy old region structure data to new region */ /* -------------------------------------------- */ - + GDXRegion[i]->fid = GDXRegion[oldregionID]->fid; GDXRegion[i]->gridID = GDXRegion[oldregionID]->gridID; GDXRegion[i]->xStart = GDXRegion[oldregionID]->xStart; @@ -8794,7 +8971,7 @@ GDdupregion(int32 oldregionID) GDXRegion[i]->StartVertical[j] = GDXRegion[oldregionID]->StartVertical[j]; GDXRegion[i]->StopVertical[j] = GDXRegion[oldregionID]->StopVertical[j]; } - + for (j=0; j<8; j++) { if(GDXRegion[oldregionID]->DimNamePtr[j] != NULL) @@ -8804,7 +8981,7 @@ GDdupregion(int32 oldregionID) strcpy(GDXRegion[i]->DimNamePtr[j],GDXRegion[oldregionID]->DimNamePtr[j]); } } - + /* Define new region ID */ /* -------------------- */ @@ -8815,6 +8992,7 @@ GDdupregion(int32 oldregionID) } return (newregionID); } +#endif /*----------------------------------------------------------------------------| @@ -8852,6 +9030,7 @@ GDdupregion(int32 oldregionID) -----------------------------------------------------------------------------*/ #define SETGRIDREG \ \ +regionID = 0; \ status = GDgridinfo(gridID, &xdimsize, &ydimsize, upleftpt, lowrightpt); \ for (k = 0; k < NGRIDREGN; k++) \ { \ @@ -8894,7 +9073,7 @@ for (j=0; j<8; j++) \ } \ - +#ifdef HDFEOS_GD_WRITE_SUPPORT int32 GDdefvrtregion(int32 gridID, int32 regionID, const char *vertObj, float64 range[]) { @@ -8921,7 +9100,7 @@ GDdefvrtregion(int32 gridID, int32 regionID, const char *vertObj, float64 range[ /* --------------------------------- */ dimlist = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(dimlist == NULL) - { + { HEpush(DFE_NOSPACE,"GDdefvrtregion", __FILE__, __LINE__); return(-1); } @@ -8951,7 +9130,7 @@ GDdefvrtregion(int32 gridID, int32 regionID, const char *vertObj, float64 range[ GDXRegion[regionID]->DimNamePtr[j] = (char *) malloc(slen + 1); if(GDXRegion[regionID]->DimNamePtr[j] == NULL) - { + { HEpush(DFE_NOSPACE,"GDdefvrtregion", __FILE__, __LINE__); free(dimlist); return(-1); @@ -8986,7 +9165,7 @@ GDdefvrtregion(int32 gridID, int32 regionID, const char *vertObj, float64 range[ size = DFKNTsize(nt); vertArr = (char *) calloc(dims[0], size); if(vertArr == NULL) - { + { HEpush(DFE_NOSPACE,"GDdefvrtregion", __FILE__, __LINE__); free(dimlist); return(-1); @@ -9222,7 +9401,7 @@ GDdeftimeperiod(int32 gridID, int32 periodID, float64 starttime, return (periodID); } - +#endif /*----------------------------------------------------------------------------| @@ -9319,13 +9498,13 @@ GDgetpixels(int32 gridID, int32 nLonLat, float64 lonVal[], float64 latVal[], /* ---------------------------------- */ xVal = (float64 *) calloc(nLonLat, sizeof(float64)); if(xVal == NULL) - { + { HEpush(DFE_NOSPACE,"GDgetpixels", __FILE__, __LINE__); return(-1); } yVal = (float64 *) calloc(nLonLat, sizeof(float64)); if(yVal == NULL) - { + { HEpush(DFE_NOSPACE,"GDgetpixels", __FILE__, __LINE__); free(xVal); return(-1); @@ -9495,7 +9674,7 @@ GDgetpixvalues(int32 gridID, int32 nPixels, int32 pixRow[], int32 pixCol[], /* --------------------------------- */ dimlist = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(dimlist == NULL) - { + { HEpush(DFE_NOSPACE,"GDgetpixvalues", __FILE__, __LINE__); return(-1); } @@ -9618,9 +9797,9 @@ GDgetpixvalues(int32 gridID, int32 nPixels, int32 pixRow[], int32 pixCol[], /* Set I/O offset and count Section */ /* ---------------------- */ - + /* - * start and edge != NULL, set I/O offset and count to + * start and edge != NULL, set I/O offset and count to * user values, adjusting the * 0th field with the merged field offset (if any) */ @@ -9648,12 +9827,12 @@ GDgetpixvalues(int32 gridID, int32 nPixels, int32 pixRow[], int32 pixCol[], offset[0] = mrgOffset; count[0] = 1; } - - - + + + /* Set I/O stride Section */ /* ---------------------- */ - + /* In original code stride entered as NULL. Abe Taaheri June 12, 1998 */ /* @@ -9663,7 +9842,7 @@ GDgetpixvalues(int32 gridID, int32 nPixels, int32 pixRow[], int32 pixCol[], { incr[j] = 1; } - + /* Read into data buffer */ /* --------------------- */ @@ -9692,6 +9871,7 @@ GDgetpixvalues(int32 gridID, int32 nPixels, int32 pixRow[], int32 pixCol[], } +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -9783,7 +9963,7 @@ GDinterpolate(int32 gridID, int32 nValues, float64 lonVal[], float64 latVal[], /* --------------------------------- */ dimlist = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(dimlist == NULL) - { + { HEpush(DFE_NOSPACE,"GDinterpolate", __FILE__, __LINE__); return(-1); } @@ -9962,7 +10142,7 @@ GDinterpolate(int32 gridID, int32 nValues, float64 lonVal[], float64 latVal[], /* -------------------------------- */ pixVal = (char *) malloc(4 * size); if(pixVal == NULL) - { + { HEpush(DFE_NOSPACE,"GDinterpolate", __FILE__, __LINE__); free(dimlist); return(-1); @@ -10119,6 +10299,8 @@ GDinterpolate(int32 gridID, int32 nValues, float64 lonVal[], float64 latVal[], } } +#endif + /*********************************************** GDwrrdtile -- This function is the underlying function below GDwritetile and @@ -10241,7 +10423,6 @@ GDwrrdtile(int32 gridID, const char *fieldname, const char *code, int32 start[], return (status); } - /*********************************************** GDtileinfo -- This function queries the field to determine if it is tiled. If it is @@ -10344,6 +10525,7 @@ Alexis Zubrow ********************************************************/ +#ifdef HDFEOS_GD_WRITE_SUPPORT intn GDwritetile(int32 gridID, const char *fieldname, int32 tilecoords[], VOIDP tileData) @@ -10355,6 +10537,8 @@ GDwritetile(int32 gridID, const char *fieldname, int32 tilecoords[], return (status); } +#endif + /*********************************************** GDreadtile -- This function reads one tile from a particular field. @@ -10376,6 +10560,8 @@ GDreadtile(int32 gridID, const char *fieldname, int32 tilecoords[], return (status); } + +#ifdef UNUSED_BY_GDAL /*********************************************** GDsettilecache -- This function sets the cache size for a tiled field. @@ -10448,7 +10634,10 @@ GDsettilecache(int32 gridID, const char *fieldname, int32 maxcache, CPL_UNUSED i return (status); } +#endif + +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -10500,7 +10689,7 @@ GDsettilecomp(int32 gridID, const char *fieldname, int32 tilerank, int32* tiledims, int32 compcode, intn* compparm) { intn status; /* routine return status variable */ - + int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 gdVgrpID; /* Grid root Vgroup ID */ @@ -10513,18 +10702,18 @@ GDsettilecomp(int32 gridID, const char *fieldname, int32 tilerank, int32* comp_info c_info; /* Compression parameter structure */ HDF_CHUNK_DEF chunkDef; /* Tiling structure */ int32 chunkFlag; /* Chunking (Tiling) flag */ - + c_info.nbit.nt = 0; - + /* Check for valid grid ID and get SDS interface ID */ status = GDchkgdid(gridID, "GDsetfillvalue", &fid, &sdInterfaceID, &gdVgrpID); - + if (status == 0) { /* Get field info */ status = GDfieldinfo(gridID, fieldname, &dum, dims, &nt, NULL); - + if (status == 0) { /* Get SDS ID and solo flag */ @@ -10538,8 +10727,8 @@ GDsettilecomp(int32 gridID, const char *fieldname, int32 tilerank, int32* } /* Tiling with Compression */ /* ----------------------- */ - - + + /* Setup Compression */ /* ----------------- */ if (compcode == HDFE_COMP_NBIT) @@ -10558,19 +10747,19 @@ GDsettilecomp(int32 gridID, const char *fieldname, int32 tilerank, int32* { c_info.deflate.level = compparm[0]; } - + /* Setup chunk lengths */ /* ------------------- */ for (i = 0; i < tilerank; i++) { chunkDef.comp.chunk_lengths[i] = tiledims[i]; } - + /* Setup chunk flag & chunk compression type */ /* ----------------------------------------- */ chunkFlag = HDF_CHUNK | HDF_COMP; chunkDef.comp.comp_type = compcode; - + /* Setup chunk compression parameters */ /* ---------------------------------- */ if (compcode == HDFE_COMP_SKPHUFF) @@ -10601,7 +10790,9 @@ GDsettilecomp(int32 gridID, const char *fieldname, int32 tilerank, int32* } return (status); } +#endif +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -10672,22 +10863,22 @@ static intn GDll2mm_cea(int32 projcode,int32 zonecode, int32 spherecode, &errorcode, for_trans); /* Convert upleft and lowright X coords from DMS to radians */ /* -------------------------------------------------------- */ - + lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD); lonrad = EHconvAng(lowrightpt[0], HDFE_DMS_RAD); - + /* Convert upleft and lowright Y coords from DMS to radians */ /* -------------------------------------------------------- */ latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD); latrad = EHconvAng(lowrightpt[1], HDFE_DMS_RAD); - + /* Convert from lon/lat to meters(or whatever unit is, i.e unit of r_major and r_minor) using GCTP */ /* ----------------------------------------- */ errorcode = for_trans[projcode] (lonrad0, latrad0, &xMtr0, &yMtr0); x[0] = xMtr0; y[0] = yMtr0; - + /* Report error if any */ /* ------------------- */ if (errorcode != 0) @@ -10697,14 +10888,14 @@ static intn GDll2mm_cea(int32 projcode,int32 zonecode, int32 spherecode, HEreport("GCTP Error: %d\n", errorcode); return (status); } - + /* Convert from lon/lat to meters(or whatever unit is, i.e unit of r_major and r_minor) using GCTP */ /* ----------------------------------------- */ errorcode = for_trans[projcode] (lonrad, latrad, &xMtr1, &yMtr1); x[1] = xMtr1; y[1] = yMtr1; - + /* Report error if any */ /* ------------------- */ if (errorcode != 0) @@ -10714,11 +10905,11 @@ static intn GDll2mm_cea(int32 projcode,int32 zonecode, int32 spherecode, HEreport("GCTP Error: %d\n", errorcode); return (status); } - + /* Compute x scale factor */ /* ---------------------- */ *scaleX = (xMtr1 - xMtr0) / xdimsize; - + /* Compute y scale factor */ /* ---------------------- */ *scaleY = (yMtr1 - yMtr0) / ydimsize; @@ -10732,8 +10923,10 @@ static intn GDll2mm_cea(int32 projcode,int32 zonecode, int32 spherecode, } return (0); } +#endif +#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -10776,7 +10969,7 @@ static intn GDmm2ll_cea(int32 projcode,int32 zonecode, int32 spherecode, float64 projparm[], CPL_UNUSED int32 xdimsize, CPL_UNUSED int32 ydimsize, CPL_UNUSED float64 upleftpt[], CPL_UNUSED float64 lowrightpt[], int32 npnts, - float64 x[], float64 y[], + float64 x[], float64 y[], float64 longitude[], float64 latitude[]) { intn status = 0; /* routine return status variable */ @@ -10796,13 +10989,13 @@ static intn GDmm2ll_cea(int32 projcode,int32 zonecode, int32 spherecode, { inv_init(projcode, zonecode, projparm, spherecode, NULL, NULL, &errorcode, inv_trans); - + /* Convert from meters(or whatever unit is, i.e unit of r_major and r_minor) to lat/lon using GCTP */ /* ----------------------------------------- */ for(i=0; i= NSWATH) + { + return -1; + } /* Check for active swath ID */ /* ------------------------- */ - if (SWXSwath[swathID % idOffset].active == 0) + if (SWXSwath[sID].active == 0) { status = -1; HEpush(DFE_GENAPP, "SWchkswid", __FILE__, __LINE__); @@ -855,9 +870,9 @@ SWchkswid(int32 swathID, const char *routname, /* Get file & SDS ids and Swath Vgroup */ /* ----------------------------------- */ - status = EHchkfid(SWXSwath[swathID % idOffset].fid, " ", fid, + status = EHchkfid(SWXSwath[sID].fid, " ", fid, sdInterfaceID, &l_access); - *swVgrpID = SWXSwath[swathID % idOffset].IDTable; + *swVgrpID = SWXSwath[sID].IDTable; } } return (status); @@ -868,7 +883,7 @@ SWchkswid(int32 swathID, const char *routname, - +#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -933,13 +948,19 @@ SWdefdim(int32 swathID, char *dimname, int32 dim) /* -------------------------------------- */ if (status == 0) { - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); + int32 dimArray[1] = { dim }; status = EHinsertmeta(sdInterfaceID, swathname, "s", 0L, - dimname, &dim); + dimname, dimArray); } return (status); } - +#endif /*----------------------------------------------------------------------------| @@ -996,7 +1017,7 @@ SWdiminfo(int32 swathID, const char *dimname) /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdiminfo", __FILE__, __LINE__); return(-1); } @@ -1009,7 +1030,13 @@ SWdiminfo(int32 swathID, const char *dimname) if (status == 0) { /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Get pointers to "Dimension" section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", @@ -1019,7 +1046,7 @@ SWdiminfo(int32 swathID, const char *dimname) { free(utlstr); return(-1); - } + } /* Search for dimension name (surrounded by quotes) */ snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", dimname, "\"\n"); @@ -1116,7 +1143,7 @@ SWmapinfo(int32 swathID, const char *geodim, const char *datadim, int32 * offset /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWmapinfo", __FILE__, __LINE__); return(-1); } @@ -1130,7 +1157,13 @@ SWmapinfo(int32 swathID, const char *geodim, const char *datadim, int32 * offset if (status == 0) { /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Get pointers to "DimensionMap" section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", @@ -1249,7 +1282,12 @@ SWidxmapinfo(int32 swathID, const char *geodim, const char *datadim, int32 l_ind { /* Find Index Mapping Vdata with Swath Attributes Vgroup */ snprintf(utlbuf, sizeof(utlbuf), "%s%s%s%s", "INDXMAP:", geodim, "/", datadim); - vgid = SWXSwath[swathID % idOffset].VIDTable[2]; + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + vgid = SWXSwath[sID].VIDTable[2]; vdataID = EHgetid(fid, vgid, utlbuf, 1, "r"); /* If found then get geodim size & read l_index mapping values */ @@ -1330,7 +1368,7 @@ SWcompinfo(int32 swathID, const char *fieldname, int32 * compcode, intn compparm /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWcompinfo", __FILE__, __LINE__); return(-1); } @@ -1342,7 +1380,13 @@ SWcompinfo(int32 swathID, const char *fieldname, int32 * compcode, intn compparm if (status == 0) { /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Get pointers to "DataField" section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", @@ -1464,7 +1508,7 @@ SWcompinfo(int32 swathID, const char *fieldname, int32 * compcode, intn compparm free(metabuf); } free(utlstr); - + return (status); } @@ -1538,7 +1582,7 @@ SWfinfo(int32 swathID, const char *fieldtype, const char *fieldname, /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWfinfo", __FILE__, __LINE__); return(-1); } @@ -1552,14 +1596,20 @@ SWfinfo(int32 swathID, const char *fieldtype, const char *fieldname, status = SWchkswid(swathID, "SWfinfo", &fid, &sdInterfaceID, &dum); /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Get pointers to appropriate "Field" section within SM */ if (strcmp(fieldtype, "Geolocation Fields") == 0) { metabuf = EHmetagroup(sdInterfaceID, swathname, "s", "GeoField", metaptrs); - + if(metabuf == NULL) { free(utlstr); @@ -1672,7 +1722,7 @@ SWfinfo(int32 swathID, const char *fieldtype, const char *fieldname, fsize = VSsizeof(vdataID, (char *)fieldname); buf = (uint8 *) calloc(fsize, 1); if(buf == NULL) - { + { HEpush(DFE_NOSPACE,"SWfinfo", __FILE__, __LINE__); free(utlstr); return(-1); @@ -1717,7 +1767,7 @@ SWfinfo(int32 swathID, const char *fieldtype, const char *fieldname, status = -1; } free(utlstr); - + return (status); } @@ -1799,7 +1849,7 @@ SWfieldinfo(int32 swathID, const char *fieldname, int32 * rank, int32 dims[], - +#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -1886,7 +1936,12 @@ SWdefdimmap(int32 swathID, const char *geodim, const char *datadim, int32 offset metadata[0] = offset; metadata[1] = increment; - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); status = EHinsertmeta(sdInterfaceID, swathname, "s", 1L, mapname, metadata); @@ -1959,7 +2014,7 @@ SWdefidxmap(int32 swathID, const char *geodim, const char *datadim, int32 l_inde /* Geo Dimension Search */ /* -------------------- */ - + gsize = SWdiminfo(swathID, geodim); if (gsize == -1) @@ -1988,10 +2043,15 @@ SWdefidxmap(int32 swathID, const char *geodim, const char *datadim, int32 l_inde { /* Get attribute Vgroup ID and allocate data buffer */ /* ------------------------------------------------ */ - attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2]; + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + attrVgrpID = SWXSwath[sID].VIDTable[2]; buf = (uint8 *) calloc(4 * gsize, 1); if(buf == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefidxmap", __FILE__, __LINE__); return(-1); } @@ -2021,9 +2081,10 @@ SWdefidxmap(int32 swathID, const char *geodim, const char *datadim, int32 l_inde /* Write to Structural Metadata */ snprintf(mapname, sizeof(mapname), "%s%s%s", geodim, "/", datadim); - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + Vgetname(SWXSwath[sID].IDTable, swathname); + int32 dumArray[1] = { dum }; status = EHinsertmeta(sdInterfaceID, swathname, "s", 2L, - mapname, &dum); + mapname, dumArray); } } @@ -2079,6 +2140,10 @@ SWdefcomp(int32 swathID, int32 compcode, intn compparm[]) if (status == 0) { sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } /* Set compression code in compression external array */ SWXSwath[sID].compcode = compcode; @@ -2219,12 +2284,16 @@ SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const { /* Remove offset from swath ID & get swathname */ sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } Vgetname(swVgrpID, swathname); /* Allocate space for dimbuf, copy dimlist into it, & append comma */ dimbuf = (char *) calloc(strlen(dimlist) + 64, 1); if(dimbuf == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); return(-1); } @@ -2244,7 +2313,7 @@ SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const /* Copy dimension list entry to dimcheck */ dimcheck = (char *) calloc(comma - dimbuf + 1, 1); if(dimcheck == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); free(dimbuf); return(-1); @@ -2324,7 +2393,7 @@ SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const { /* ((intn) strlen(fieldname) > MAX_NC_NAME - 7) ** this was changed because HDF4.1r3 made a change in the -** hlimits.h file. We have notified NCSA and asked to have +** hlimits.h file. We have notified NCSA and asked to have ** it made the same as in previous versions of HDF ** see ncr 26314. DaW Apr 2000 */ @@ -2472,7 +2541,7 @@ SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const */ oneDbuf = (uint8 *) calloc(recSize, 1); if(oneDbuf == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); return(-1); } @@ -2488,7 +2557,7 @@ SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const */ oneDbuf = (uint8 *) calloc(recSize, dims[0]); if(oneDbuf == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); return(-1); } @@ -2684,7 +2753,7 @@ SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const realloc((void *) SWXSwath[sID].sdsID, (SWXSwath[sID].nSDS + 1) * 4); if(SWXSwath[sID].sdsID == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); return(-1); } @@ -2694,7 +2763,7 @@ SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const { SWXSwath[sID].sdsID = (int32 *) calloc(1, 4); if(SWXSwath[sID].sdsID == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); return(-1); } @@ -2782,7 +2851,6 @@ SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -2934,9 +3002,15 @@ SWwritegeometa(int32 swathID, const char *fieldname, const char *dimlist, /* ------------------------------ */ snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist); - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); + int32 numbertypeArray[1] = { numbertype }; status = EHinsertmeta(sdInterfaceID, swathname, "s", 3L, - utlbuf, &numbertype); + utlbuf, numbertypeArray); } return (status); @@ -2999,12 +3073,19 @@ SWwritedatameta(int32 swathID, const char *fieldname, const char *dimlist, /* ------------------------------ */ snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist); - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); + int32 numbertypeArray[1] = { numbertype }; status = EHinsertmeta(sdInterfaceID, swathname, "s", 4L, - utlbuf, &numbertype); + utlbuf, numbertypeArray); } return (status); } +#endif @@ -3060,7 +3141,12 @@ SWwrrdattr(int32 swathID, const char *attrname, int32 numbertype, int32 count, { /* Get attribute Vgroup ID and call EHattr to perform I/O */ /* ------------------------------------------------------ */ - attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2]; + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + attrVgrpID = SWXSwath[sID].VIDTable[2]; status = EHattr(fid, attrVgrpID, attrname, numbertype, count, wrcode, datbuf); } @@ -3068,7 +3154,7 @@ SWwrrdattr(int32 swathID, const char *attrname, int32 numbertype, int32 count, } - +#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -3112,7 +3198,7 @@ SWwriteattr(int32 swathID, const char *attrname, int32 numbertype, int32 count, return (status); } - +#endif /*----------------------------------------------------------------------------| @@ -3212,7 +3298,12 @@ SWattrinfo(int32 swathID, const char *attrname, int32 * numbertype, int32 * coun { /* Get attribute Vgroup ID and call EHattrinfo */ /* ------------------------------------------- */ - attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2]; + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + attrVgrpID = SWXSwath[sID].VIDTable[2]; status = EHattrinfo(fid, attrVgrpID, attrname, numbertype, count); @@ -3277,7 +3368,12 @@ SWinqattrs(int32 swathID, char *attrnames, int32 * strbufsize) { /* Get attribute Vgroup ID and call EHattrcat */ /* ------------------------------------------ */ - attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2]; + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + attrVgrpID = SWXSwath[sID].VIDTable[2]; nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize); } @@ -3348,7 +3444,7 @@ SWinqdims(int32 swathID, char *dimnames, int32 dims[]) /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWinqdims", __FILE__, __LINE__); return(-1); } @@ -3363,7 +3459,13 @@ SWinqdims(int32 swathID, char *dimnames, int32 dims[]) if (dimnames != NULL || dims != NULL) { /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Get pointers to "Dimension" section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", @@ -3513,7 +3615,7 @@ SWinqmaps(int32 swathID, char *dimmaps, int32 offset[], int32 increment[]) /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWinqmaps", __FILE__, __LINE__); return(-1); } @@ -3528,7 +3630,13 @@ SWinqmaps(int32 swathID, char *dimmaps, int32 offset[], int32 increment[]) { /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Get pointers to "DimensionMap" section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", @@ -3679,7 +3787,7 @@ SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[]) /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWinqidxmaps", __FILE__, __LINE__); return(-1); } @@ -3694,7 +3802,13 @@ SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[]) if (idxmaps != NULL || idxsizes != NULL) { /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Get pointers to "IndexDimensionMap" section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", @@ -3743,7 +3857,7 @@ SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[]) { /* Parse off geo dimension and find its size */ slash = strchr(utlstr, '/'); - *slash = 0; + if (slash) *slash = 0; idxsizes[nMap] = SWdiminfo(swathID, utlstr); } @@ -3843,14 +3957,14 @@ SWinqfields(int32 swathID, const char *fieldtype, char *fieldlist, int32 rank[], /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__); return(-1); } utlstr2 = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr2 == NULL) - { + { HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__); free(utlstr); return(-1); @@ -3867,7 +3981,14 @@ SWinqfields(int32 swathID, const char *fieldtype, char *fieldlist, int32 rank[], if (fieldlist != NULL || rank != NULL || numbertype != NULL) { /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + free(utlstr2); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Get pointers to "GeoField" or "DataField" section within SM */ if (strcmp(fieldtype, "Geolocation Fields") == 0) @@ -4152,7 +4273,7 @@ SWnentries(int32 swathID, int32 entrycode, int32 * strbufsize) /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWnemtries", __FILE__, __LINE__); return(-1); } @@ -4163,7 +4284,13 @@ SWnentries(int32 swathID, int32 entrycode, int32 * strbufsize) if (status == 0) { /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Zero out string buffer size */ *strbufsize = 0; @@ -4419,7 +4546,10 @@ SW1dfldsrch(int32 fid, int32 swathID, const char *fieldname, const char *i_acces /* Compute "reduced" swath ID */ /* -------------------------- */ sID = swathID % idOffset; - + if (sID >= NSWATH) + { + return -1; + } /* Get Geolocation Vgroup id and 1D field name Vdata id */ /* ---------------------------------------------------- */ @@ -4519,7 +4649,7 @@ SWSDfldsrch(int32 swathID, int32 sdInterfaceID, const char *fieldname, /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) - { + { HEpush(DFE_NOSPACE,"SWSDfldsrch", __FILE__, __LINE__); return(-1); } @@ -4531,7 +4661,10 @@ SWSDfldsrch(int32 swathID, int32 sdInterfaceID, const char *fieldname, /* Compute "reduced" swath ID */ /* -------------------------- */ sID = swathID % idOffset; - + if (sID >= NSWATH) + { + return -1; + } /* Loop through all SDSs in swath */ /* ------------------------------ */ @@ -4981,7 +5114,7 @@ SWwrrdfield(int32 swathID, const char *fieldname, const char *code, fldsize = VSsizeof(vdataID, (char *)fieldname); fillbuf = (uint8 *) calloc(fldsize, 1); if(fillbuf == NULL) - { + { HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__); return(-1); } @@ -4991,8 +5124,9 @@ SWwrrdfield(int32 swathID, const char *fieldname, const char *code, VSQueryvsize(vdataID, &recsize); buf = (uint8 *) calloc(recsize, count[0] * incr[0]); if(buf == NULL) - { + { HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__); + free(fillbuf); return(-1); } @@ -5095,7 +5229,7 @@ SWwrrdfield(int32 swathID, const char *fieldname, const char *code, fldsize = VSsizeof(vdataID, (char *)fieldname); buf = (uint8 *) calloc(fldsize, count[0] * incr[0]); if(buf == NULL) - { + { HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__); return(-1); } @@ -5137,7 +5271,7 @@ SWwrrdfield(int32 swathID, const char *fieldname, const char *code, } - +#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -5181,7 +5315,7 @@ SWwritefield(int32 swathID, const char *fieldname, data); return (status); } - +#endif @@ -5233,7 +5367,7 @@ SWreadfield(int32 swathID, const char *fieldname, - +#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -5288,7 +5422,7 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], intn i; /* Loop index */ intn j = 0; /* Loop index */ intn k; /* Loop index */ - + intn status; /* routine return status variable */ intn statLon; /* Status from SWfieldinfo for longitude */ intn statLat; /* Status from SWfieldinfo for latitude */ @@ -5299,8 +5433,8 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], uint8 found = 0; /* Found flag */ uint8 *flag; /* Pointer to track flag array */ - intn validReg = -1; /* -1 is invalid validReg */ - + intn validReg = -1; /* -1 is invalid validReg */ + int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 swVgrpID; /* Swath Vgroup ID */ @@ -5329,7 +5463,7 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], char dimlist[256]; /* Dimension list (geolocation * fields) */ char latName[17];/* Latitude field name */ - + /* Check for valid swath ID */ /* ------------------------ */ @@ -5370,9 +5504,9 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], dimlist); if (statCoLat != 0) { - /* Check again for Geodeticlatitude */ - statGeodeticLat = SWfieldinfo(swathID, - "GeodeticLatitude", &rank, + /* Check again for Geodeticlatitude */ + statGeodeticLat = SWfieldinfo(swathID, + "GeodeticLatitude", &rank, dims, &nt, dimlist); if (statGeodeticLat != 0) { @@ -5463,14 +5597,14 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], /* --------------------------------------------------- */ lonArr = (char *) calloc(nElem, sizeof(float64)); if(lonArr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); return(-1); } latArr = (char *) calloc(nElem, sizeof(float64)); if(latArr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); free(lonArr); return(-1); @@ -5481,7 +5615,7 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], /* ------------------------------------- */ flag = (uint8 *) calloc(edge[0] + 1, 1); if(flag == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); free(lonArr); free(latArr); @@ -5514,7 +5648,7 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], temp64 = (float64) temp32; memcpy(latArr + 8 * i, &temp64, 8); } - } + } /* Set boundary flag */ @@ -5535,7 +5669,7 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], /* ------------------ */ for (i = 0; i < edge[0]; i++) - { + { /* For each value from Cross Track ... */ /* ----------------------------------- */ for (j = 0; j < edge[1]; j++) @@ -5610,16 +5744,18 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], /* ----------------------------------------------- */ lonArr = (char *) calloc(dims[1], sizeof(float64)); if(lonArr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); + free(flag); return(-1); } latArr = (char *) calloc(dims[1], sizeof(float64)); if(latArr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); free(lonArr); + free(flag); return(-1); } @@ -5629,13 +5765,13 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], anyStart[1] = 0; anyEdge[0] = 1; anyEdge[1] = dims[1]; - - + + /* For each track starting from 0 */ /* ------------------------------ */ - for (i = 0; i < edge[0]; i++) + for (i = 0; i < edge[0]; i++) { - + /* If cross track not in region (with MIDPOINT search ... */ /* ------------------------------------------------------ */ if (flag[i] == 0) @@ -5754,8 +5890,11 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], SWXRegion[k] = (struct swathRegion *) calloc(1, sizeof(struct swathRegion)); if(SWXRegion[k] == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); + free(lonArr); + free(latArr); + free(flag); return(-1); } @@ -5785,9 +5924,11 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], if (k >= NSWATHREGN) { HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__); - HEreport( - "regionID exceeded NSWATHREGN.\n"); - return (-1); + HEreport("regionID exceeded NSWATHREGN.\n"); + free(lonArr); + free(latArr); + free(flag); + return (-1); } /* Find start and stop of regions */ @@ -5815,10 +5956,10 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], /* Increment (multiple) region counter */ /* ----------------------------------- */ j = ++SWXRegion[k]->nRegions; - + /* if SWXRegion[k]->nRegions greater than MAXNREGIONS */ /* free allocated memory and return FAIL */ - + if ((SWXRegion[k]->nRegions) > MAXNREGIONS) { HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__); @@ -5828,10 +5969,10 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], free(flag); return(-1); } - + SWXRegion[k]->StartRegion[j - 1] = i; } - + /* End of region */ /* ------------- */ if (flag[i] == 255) @@ -5854,12 +5995,13 @@ SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], { return (-1); } - -} +} +#endif +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -5919,8 +6061,8 @@ SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], uint8 found = 0; /* Found flag */ uint8 *flag; /* Pointer to track flag array */ - intn validReg = -1; /* -1 is invalid validReg */ - + intn validReg = -1; /* -1 is invalid validReg */ + int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 swVgrpID; /* Swath Vgroup ID */ @@ -5990,9 +6132,9 @@ SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], dimlist); if (statCoLat != 0) { - /* Check again for Geodeticlatitude */ - statGeodeticLat = SWfieldinfo(swathID, - "GeodeticLatitude", &rank, + /* Check again for Geodeticlatitude */ + statGeodeticLat = SWfieldinfo(swathID, + "GeodeticLatitude", &rank, dims, &nt, dimlist); if (statGeodeticLat != 0) { @@ -6088,14 +6230,14 @@ SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], /* --------------------------------------------------- */ lonArr = (char *) calloc(nElem, sizeof(float64)); if(lonArr == NULL) - { + { HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); return(-1); } - + latArr = (char *) calloc(nElem, sizeof(float64)); if(latArr == NULL) - { + { HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); free(lonArr); return(-1); @@ -6106,7 +6248,7 @@ SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], /* ------------------------------------- */ flag = (uint8 *) calloc(edge[0] + 1, 1); if(flag == NULL) - { + { HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); free(lonArr); free(latArr); @@ -6234,13 +6376,13 @@ SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], /* ----------------------------------------------- */ lonArr = (char *) calloc(dims[1], sizeof(float64)); if(lonArr == NULL) - { + { HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); return(-1); } latArr = (char *) calloc(dims[1], sizeof(float64)); if(latArr == NULL) - { + { HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); free(lonArr); return(-1); @@ -6381,8 +6523,11 @@ SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], SWXRegion[k] = (struct swathRegion *) calloc(1, sizeof(struct swathRegion)); if(SWXRegion[k] == NULL) - { + { HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); + free(lonArr); + free(latArr); + free(flag); return(-1); } @@ -6456,7 +6601,7 @@ SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], /* Increment (multiple) region counter */ /* ----------------------------------- */ j = ++SWXRegion[k]->nRegions; - + if (mapstatus == 2) { l = i; @@ -6477,7 +6622,7 @@ SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], validReg = 0; } } - + } } @@ -6494,11 +6639,11 @@ SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], { return (-1); } - -} - +} +#endif +#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -6613,7 +6758,7 @@ SWdeftimeperiod(int32 swathID, float64 starttime, float64 stoptime, /* ---------------------------- */ time64 = (float64 *) calloc(edge[0], 8); if(time64 == NULL) - { + { HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__); return(-1); } @@ -6638,7 +6783,7 @@ SWdeftimeperiod(int32 swathID, float64 starttime, float64 stoptime, /* ---------------------------- */ time64 = (float64 *) calloc(edge[0] * 2, 8); if(time64 == NULL) - { + { HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__); return(-1); } @@ -6690,7 +6835,7 @@ SWdeftimeperiod(int32 swathID, float64 starttime, float64 stoptime, SWXRegion[k] = (struct swathRegion *) calloc(1, sizeof(struct swathRegion)); if(SWXRegion[k] == NULL) - { + { HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__); return(-1); } @@ -6742,45 +6887,47 @@ SWdeftimeperiod(int32 swathID, float64 starttime, float64 stoptime, } - - /* Clear found flag */ - /* ---------------- */ - found = 0; + if (periodID >= 0) + { + /* Clear found flag */ + /* ---------------- */ + found = 0; - /* For each track (from bottom) ... */ - /* -------------------------------- */ - for (i = edge[0] - 1; i >= 0; i--) - { - /* For each value from Cross Track ... */ - /* ----------------------------------- */ - for (j = 0; j < edge[1]; j++) + /* For each track (from bottom) ... */ + /* -------------------------------- */ + for (i = edge[0] - 1; i >= 0; i--) { + /* For each value from Cross Track ... */ + /* ----------------------------------- */ + for (j = 0; j < edge[1]; j++) + { - /* Get time test value */ - /* ------------------- */ - time64Test = time64[i * edge[1] + j]; + /* Get time test value */ + /* ------------------- */ + time64Test = time64[i * edge[1] + j]; - /* If within time period ... */ - /* ------------------------- */ - if (time64Test >= starttime && - time64Test <= stoptime) - { - /* Set found flag */ - /* -------------- */ - found = 1; + /* If within time period ... */ + /* ------------------------- */ + if (time64Test >= starttime && + time64Test <= stoptime) + { + /* Set found flag */ + /* -------------- */ + found = 1; - /* Set start of region to first track found */ - /* ---------------------------------------- */ - SWXRegion[k]->StopRegion[0] = i; + /* Set start of region to first track found */ + /* ---------------------------------------- */ + SWXRegion[periodID]->StopRegion[0] = i; - break; /* Break from "j" loop */ + break; /* Break from "j" loop */ + } + } + if (found == 1) + { + break; /* Break from "i" loop */ } - } - if (found == 1) - { - break; /* Break from "i" loop */ } } @@ -6791,7 +6938,9 @@ SWdeftimeperiod(int32 swathID, float64 starttime, float64 stoptime, return (periodID); } +#endif +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -7058,7 +7207,7 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, { /* If non-vertical subset regions defined ... */ /* ------------------------------------------ */ - if (SWXRegion[regionID]->nRegions > 0) + if (SWXRegion[regionID]->nRegions > 0) { /* Get geolocation dimension name */ @@ -7077,7 +7226,7 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, status = SWfieldinfo(SWXRegion[regionID]->swathID, "Time", &dum, dumdims, &dum, tgeodim); - + if (strcmp(geodim, tgeodim) != 0) { strcpy(geodim, tgeodim); @@ -7182,6 +7331,10 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, /* Get size of geo dim & allocate space of l_index mapping */ /* ----------------------------------------------------- */ dum = SWdiminfo(swathID, geodim); + if( dum < 0 ) + { + return -1; + } /* For Landsat files, the l_index mapping has two values */ /* for each point, a left and right point. So for a 37 */ @@ -7191,14 +7344,14 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, /* dimension. */ /* ----------------------------------------------------- */ if(land_status == 0) - if(strcmp(fieldname, "Latitude") == 0 || + if(strcmp(fieldname, "Latitude") == 0 || strcmp(fieldname, "Longitude") == 0) { dum = dum * 2; } idxmap = (int32 *) calloc(dum, sizeof(int32)); if(idxmap == NULL) - { + { HEpush(DFE_NOSPACE,"SWextractregion", __FILE__, __LINE__); return(-1); } @@ -7269,7 +7422,7 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, if (SWXRegion[regionID]->scanflag == 1) { start[l_index] = SWXRegion[regionID]->StartRegion[k]/incr; - if(SWXRegion[regionID]->band8flag == 2 || + if(SWXRegion[regionID]->band8flag == 2 || SWXRegion[regionID]->band8flag == 3) { start[l_index] = (SWXRegion[regionID]->StartRegion[k]+detect_cnt)/incr; @@ -7451,7 +7604,7 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, if(idxmap[j] == 0 || idxmap[j+1] == 0) l = scene_cnt; } - + } } @@ -7530,11 +7683,11 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, } else { - if (SWXRegion[regionID]->scanflag == 1 && + if (SWXRegion[regionID]->scanflag == 1 && strcmp(fieldname,dfieldlist) == 0) { start[l_index] = SWXRegion[regionID]->StartRegion[k]; - edge[l_index] = SWXRegion[regionID]->StopRegion[k] - + edge[l_index] = SWXRegion[regionID]->StopRegion[k] - SWXRegion[regionID]->StartRegion[k] + 1; if(SWXRegion[regionID]->band8flag == 2 || SWXRegion[regionID]->band8flag == 3 ) @@ -7561,15 +7714,15 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, buffer); if (SWXRegion[regionID]->scanflag == 1) { - + if (strcmp(fieldname,"Longitude") == 0) { - status = SWscan2longlat(swathID, fieldname, buffer, start, + status = SWscan2longlat(swathID, fieldname, buffer, start, edge, idxmap, startscanline, stopscanline); } if (strcmp(fieldname,"Latitude") == 0) { - status = SWscan2longlat(swathID, fieldname, buffer, start, + status = SWscan2longlat(swathID, fieldname, buffer, start, edge, idxmap, startscanline, stopscanline); } } @@ -7585,11 +7738,11 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, { /* Mapping not found */ /* ----------------- */ - status = -1; + status = -1; HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__); HEreport("Mapping Not Defined for \"%s\" Dimension.\n", geodim); - } + } } else { @@ -7615,6 +7768,7 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, } + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -7651,7 +7805,7 @@ SWextractregion(int32 swathID, int32 regionID, const char *fieldname, | END_PROLOG | -----------------------------------------------------------------------------*/ static intn -SWscan2longlat(int32 swathID, const char *fieldname, VOIDP buffer, int32 start[], +SWscan2longlat(int32 swathID, const char *fieldname, VOIDP buffer, int32 start[], int32 edge[], int32 *idxmap, int32 startscanline, int32 stopscanline) { @@ -7862,14 +8016,14 @@ int32 edge[], int32 *idxmap, int32 startscanline, int32 stopscanline) y_p1 = (float)(RADOE * sin((p1_long*deg2rad)) * sin((p1_lat*deg2rad))); z_p1 = (float)(RADOE * cos((p1_lat*deg2rad))); - + x_p2 = (float)(RADOE * cos((p2_long*deg2rad)) * sin((p2_lat*deg2rad))); y_p2 = (float)(RADOE * sin((p2_long*deg2rad)) * sin((p2_lat*deg2rad))); z_p2 = (float)(RADOE * cos((p2_lat*deg2rad))); x_pi = x_p1 + (x_p2 - x_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1); y_pi = y_p1 + (y_p2 - y_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1); - z_pi = z_p1 + (z_p2 - z_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1); + z_pi = z_p1 + (z_p2 - z_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1); if (fieldflag == 1) { @@ -7968,6 +8122,7 @@ int32 edge[], int32 *idxmap, int32 startscanline, int32 stopscanline) } + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -8017,13 +8172,13 @@ SWextractperiod(int32 swathID, int32 periodID, const char *fieldname, if (status != 0) timeflag = 0; /*clear timeflag if SWextractregion failed*/ return (status); } +#endif - - +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -8070,7 +8225,7 @@ SWdupregion(int32 oldregionID) SWXRegion[i] = (struct swathRegion *) calloc(1, sizeof(struct swathRegion)); if(SWXRegion[i] == NULL) - { + { HEpush(DFE_NOSPACE,"SWdupregion", __FILE__, __LINE__); return(-1); } @@ -8090,8 +8245,10 @@ SWdupregion(int32 oldregionID) return (newregionID); } +#endif +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -8142,7 +8299,7 @@ SWregioninfo(int32 swathID, int32 regionID, const char *fieldname, intn long_status = 3; /* routine return status variable for longitude */ intn land_status = 3; /* Used for L7 float scene sub. */ intn statMap = -1; /* Status from SWmapinfo */ - + uint8 found = 0; /* Found flag */ uint8 detect_cnt = 0; /* Used for L7 float scene sub. */ @@ -8291,7 +8448,7 @@ SWregioninfo(int32 swathID, int32 regionID, const char *fieldname, status = SWfieldinfo(SWXRegion[regionID]->swathID, "Time", &dum, dumdims, &dum, tgeodim); - + if (strcmp(geodim, tgeodim) != 0) { strcpy(geodim, tgeodim); @@ -8380,9 +8537,11 @@ SWregioninfo(int32 swathID, int32 regionID, const char *fieldname, /* Get size of geo dim & allocate space of l_index mapping */ /* ----------------------------------------------------- */ dum = SWdiminfo(swathID, geodim); + if (dum < 0) + return -1; idxmap = (int32 *) calloc(dum, sizeof(int32)); if(idxmap == NULL) - { + { HEpush(DFE_NOSPACE,"SWregioninfo", __FILE__, __LINE__); return(-1); } @@ -8666,7 +8825,7 @@ SWregioninfo(int32 swathID, int32 regionID, const char *fieldname, { /* Mapping not found */ /* ----------------- */ - status = -1; + status = -1; HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__); HEreport( @@ -8755,9 +8914,9 @@ SWregioninfo(int32 swathID, int32 regionID, const char *fieldname, return (status); } +#endif - - +#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -8857,6 +9016,7 @@ SWperiodinfo(int32 swathID, int32 periodID, const char *fieldname, #define SETSWTHREG \ \ +regionID = 0; \ for (k = 0; k < NSWATHREGN; k++) \ { \ if (SWXRegion[k] == 0) \ @@ -8984,7 +9144,7 @@ SWdefvrtregion(int32 swathID, int32 regionID, const char *vertObj, float64 range SWXRegion[regionID]->DimNamePtr[j] = (char *) malloc(slen + 1); if(SWXRegion[regionID]->DimNamePtr[j] == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefvrtregion", __FILE__, __LINE__); return(-1); } @@ -9061,7 +9221,7 @@ SWdefvrtregion(int32 swathID, int32 regionID, const char *vertObj, float64 range /* --------------------------------- */ vertArr = (char *) calloc(dims[0], size); if(vertArr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdefvrtregion", __FILE__, __LINE__); return(-1); } @@ -9374,9 +9534,10 @@ SWdefvrtregion(int32 swathID, int32 regionID, const char *vertObj, float64 range return (regionID); } +#endif - +#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -9750,7 +9911,7 @@ SWsetfillvalue(int32 swathID, const char *fieldname, VOIDP fillval) } return (status); } - +#endif @@ -9858,16 +10019,21 @@ SWdetach(int32 swathID) { intn i; /* Loop index */ +#ifdef HDFEOS_SW_WRITE_SUPPORT intn j; /* Loop index */ +#endif intn k; /* Loop index */ intn status = 0; /* routine return status variable */ +#ifdef HDFEOS_SW_WRITE_SUPPORT intn statusFill = 0; /* return status from SWgetfillvalue */ +#endif uint8 *buf; /* Buffer for blank (initial) 1D records */ int32 vdataID; /* Vdata ID */ - int32 *namelen; /* Pointer to name string length array */ - int32 *dimlen; /* Pointer to dim string length array */ + int32 *namelen = NULL; /* Pointer to name string length array */ + int32 *dimlen = NULL; /* Pointer to dim string length array */ +#ifdef HDFEOS_SW_WRITE_SUPPORT int32 slen1[3]; /* String length array 1 */ int32 slen2[3]; /* String length array 2 */ int32 nflds; /* Number of fields */ @@ -9875,29 +10041,38 @@ SWdetach(int32 swathID) int32 cmbfldcnt; /* Number of fields combined */ int32 sdid; /* SDS ID */ int32 vgid; /* Vgroup ID */ +#endif int32 dims[3]; /* Dimension array */ - int32 *offset; /* Pointer to merged field offset array */ - int32 *indvdims; /* Pointer to merged field size array */ + int32 *offset = NULL; /* Pointer to merged field offset array */ + int32 *indvdims = NULL; /* Pointer to merged field size array */ int32 sdInterfaceID; /* SDS interface ID */ int32 sID; /* Swath ID - offset */ +#ifdef HDFEOS_SW_WRITE_SUPPORT int32 nflds0; /* Number of fields */ - int32 *namelen0; /* Pointer to name string length array */ +#endif + int32 *namelen0 = NULL; /* Pointer to name string length array */ +#ifdef HDFEOS_SW_WRITE_SUPPORT int32 rank; /* Rank of merged field */ int32 truerank; /* True rank of merged field */ +#endif int32 idOffset = SWIDOFFSET; /* Swath ID offset */ int32 dum; /* Dummy variable */ - char *nambuf; /* Pointer to name buffer */ - char **nameptr; /* Pointer to name string pointer array */ - char **dimptr; /* Pointer to dim string pointer array */ - char **nameptr0; /* Pointer to name string pointer array */ + char *nambuf = NULL; /* Pointer to name buffer */ + char **nameptr = NULL; /* Pointer to name string pointer array */ + char **dimptr = NULL; /* Pointer to dim string pointer array */ + char **nameptr0 = NULL; /* Pointer to name string pointer array */ +#ifdef HDFEOS_SW_WRITE_SUPPORT char *ptr1[3]; /* String pointer array */ char *ptr2[3]; /* String pointer array */ char dimbuf1[128]; /* Dimension buffer 1 */ char dimbuf2[128]; /* Dimension buffer 2 */ +#endif char swathname[VGNAMELENMAX + 1]; /* Swath name */ +#ifdef HDFEOS_SW_WRITE_SUPPORT char *utlbuf; /* Utility buffer */ char fillval[32];/* Fill value buffer */ +#endif /* Check for proper swath ID and get SD interface ID */ /* ------------------------------------------------- */ @@ -9908,6 +10083,10 @@ SWdetach(int32 swathID) /* Subtract off swath ID offset and get swath name */ /* ----------------------------------------------- */ sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } Vgetname(SWXSwath[sID].IDTable, swathname); @@ -9932,7 +10111,7 @@ SWdetach(int32 swathID) /* ---------------------------- */ nambuf = (char *) calloc(VSNAMELENMAX + 1, 1); if(nambuf == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); return(-1); } @@ -9947,7 +10126,7 @@ SWdetach(int32 swathID) /* --------------------- */ buf = (uint8 *) calloc(VSsizeof(vdataID, nambuf), dims[0]); if(buf == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nambuf); return(-1); @@ -9969,26 +10148,28 @@ SWdetach(int32 swathID) /* ------------------- */ if (strlen(SWXSDname) == 0) { +#ifdef HDFEOS_SW_WRITE_SUPPORT nflds = 0; +#endif /* Allocate "dummy" arrays so free() doesn't bomb later */ /* ---------------------------------------------------- */ nameptr = (char **) calloc(1, sizeof(char *)); if(nameptr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); return(-1); } namelen = (int32 *) calloc(1, sizeof(int32)); if(namelen == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); return(-1); } nameptr0 = (char **) calloc(1, sizeof(char *)); if(nameptr0 == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -9996,7 +10177,7 @@ SWdetach(int32 swathID) } namelen0 = (int32 *) calloc(1, sizeof(int32)); if(namelen0 == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10005,7 +10186,7 @@ SWdetach(int32 swathID) } dimptr = (char **) calloc(1, sizeof(char *)); if(dimptr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10015,7 +10196,7 @@ SWdetach(int32 swathID) } dimlen = (int32 *) calloc(1, sizeof(int32)); if(dimlen == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10026,7 +10207,7 @@ SWdetach(int32 swathID) } offset = (int32 *) calloc(1, sizeof(int32)); if(offset == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10038,7 +10219,7 @@ SWdetach(int32 swathID) } indvdims = (int32 *) calloc(1, sizeof(int32)); if(indvdims == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10047,9 +10228,10 @@ SWdetach(int32 swathID) free(dimptr); free(dimlen); free(offset); - return(-1); + return(-1); } } +#if defined(HDFEOS_SW_WRITE_SUPPORT) else { /* @@ -10069,20 +10251,20 @@ SWdetach(int32 swathID) /* ----------------------------------------- */ nameptr = (char **) calloc(nflds, sizeof(char *)); if(nameptr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); return(-1); } namelen = (int32 *) calloc(nflds, sizeof(int32)); if(namelen == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); return(-1); } nameptr0 = (char **) calloc(nflds, sizeof(char *)); if(nameptr0 == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10090,7 +10272,7 @@ SWdetach(int32 swathID) } namelen0 = (int32 *) calloc(nflds, sizeof(int32)); if(namelen0 == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10099,7 +10281,7 @@ SWdetach(int32 swathID) } dimptr = (char **) calloc(nflds, sizeof(char *)); if(dimptr == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10109,7 +10291,7 @@ SWdetach(int32 swathID) } dimlen = (int32 *) calloc(nflds, sizeof(int32)); if(dimlen == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10120,7 +10302,7 @@ SWdetach(int32 swathID) } offset = (int32 *) calloc(nflds, sizeof(int32)); if(offset == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10132,7 +10314,7 @@ SWdetach(int32 swathID) } indvdims = (int32 *) calloc(nflds, sizeof(int32)); if(indvdims == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nameptr); free(namelen); @@ -10141,7 +10323,7 @@ SWdetach(int32 swathID) free(dimptr); free(dimlen); free(offset); - return(-1); + return(-1); } @@ -10151,7 +10333,6 @@ SWdetach(int32 swathID) nflds = EHparsestr(SWXSDdims, ';', dimptr, dimlen); } - /* Loop through all the fields */ /* --------------------------- */ for (i = 0; i < nflds; i++) @@ -10163,15 +10344,31 @@ SWdetach(int32 swathID) { nambuf = (char *) calloc(strlen(SWXSDname) + 1, 1); if(nambuf == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); + free(nameptr); + free(namelen); + free(nameptr0); + free(namelen0); + free(dimptr); + free(dimlen); + free(offset); + free(indvdims); return(-1); } utlbuf = (char *) calloc(2 * strlen(SWXSDname) + 7, 1); if(utlbuf == NULL) - { + { HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); free(nambuf); + free(nameptr); + free(namelen); + free(nameptr0); + free(namelen0); + free(dimptr); + free(dimlen); + free(offset); + free(indvdims); return(-1); } /* Zero out dimbuf1 */ @@ -10434,6 +10631,7 @@ SWdetach(int32 swathID) free(utlbuf); } } +#endif @@ -10452,7 +10650,7 @@ SWdetach(int32 swathID) i++; } - +#ifdef HDFEOS_SW_WRITE_SUPPORT /* "Contract" SDcomb array */ /* ----------------------- */ for (i = 0; i < nflds; i++) @@ -10494,7 +10692,7 @@ SWdetach(int32 swathID) strcat(SWXSDname, ","); strcat(SWXSDdims, ";"); } - +#endif /* Free up a bunch of dynamically allocated arrays */ @@ -10604,6 +10802,7 @@ SWclose(int32 fid) return (status); } +#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -10636,16 +10835,16 @@ SWupdatescene(int32 swathID, int32 regionID) { intn k; /* Loop index */ int32 status; /* routine return status variable */ - + int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 swVgrpID; /* Swath Vgroup ID */ - + int32 startReg; /* Indexed start region */ int32 stopReg; /* Indexed stop region */ - int32 l_index[MAXNREGIONS]; /* to store indices when stop and + int32 l_index[MAXNREGIONS]; /* to store indices when stop and start are different */ - + int32 ind; /* l_index */ int32 tempnRegions; /* temp number of regions */ @@ -10655,8 +10854,8 @@ SWupdatescene(int32 swathID, int32 regionID) /* ------------------------ */ status = SWchkswid(swathID, "SWupdatescene", &fid, &sdInterfaceID, &swVgrpID); - - + + /* Check for valid region ID */ /* ------------------------- */ if (status == 0) @@ -10668,7 +10867,7 @@ SWupdatescene(int32 swathID, int32 regionID) HEreport("Invalid Region id: %d.\n", regionID); } } - + /* Check for active region ID */ /* -------------------------- */ if (status == 0) @@ -10680,12 +10879,12 @@ SWupdatescene(int32 swathID, int32 regionID) HEreport("Inactive Region ID: %d.\n", regionID); } } - + if (status == 0) { tempnRegions = SWXRegion[regionID]->nRegions; ind =0; - + for (k = 0; k < SWXRegion[regionID]->nRegions; k++) { startReg = SWXRegion[regionID]->StartRegion[k]; @@ -10695,10 +10894,10 @@ SWupdatescene(int32 swathID, int32 regionID) /* reduce number of regions by 1, if tempnRegions is 0 issue error and break from loop*/ tempnRegions -= 1; - + if(tempnRegions == 0) { - /* first free allocated memory for SWXRegion[regionID] + /* first free allocated memory for SWXRegion[regionID] in the function SWdefboxregion and make regionID inactive */ free(SWXRegion[regionID]); @@ -10725,7 +10924,7 @@ SWupdatescene(int32 swathID, int32 regionID) { SWXRegion[regionID]->nRegions = tempnRegions; } - /* keep starts and stops that are different in the structure */ + /* keep starts and stops that are different in the structure */ for (k = 0; k < SWXRegion[regionID]->nRegions; k++) { SWXRegion[regionID]->StartRegion[k] = @@ -10733,38 +10932,40 @@ SWupdatescene(int32 swathID, int32 regionID) SWXRegion[regionID]->StopRegion[k] = SWXRegion[regionID]->StopRegion[l_index[k]]; } - + } - + if (status == 0) { - + for (k = 0; k < SWXRegion[regionID]->nRegions; k++) { - + startReg = SWXRegion[regionID]->StartRegion[k]; stopReg = SWXRegion[regionID]->StopRegion[k]; - + if(startReg % 2 == 1) { - + SWXRegion[regionID]->StartRegion[k] = ++startReg; - + } if(stopReg % 2 == 0) { - + SWXRegion[regionID]->StopRegion[k] = --stopReg; - + } - + } - + } - + return(status); - + } +#endif +#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -10806,14 +11007,14 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou intn k; /* Loop index */ int32 status; /* routine return status variable */ int32 land_status = 3; /* routine return status variable */ - + int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 swVgrpID; /* Swath Vgroup ID */ int32 numtype = 0; /* Used for L7 float scene sub. */ int32 count = 0; /* Used for L7 float scene sub. */ - + int32 startReg = 0; /* Indexed start region */ int32 stopReg = 0; /* Indexed stop region */ int32 nout=-1; /* Number of elements in output l_index array */ @@ -10824,13 +11025,13 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou intn ngtflag = 0; int32 *buffer1 = (int32 *)NULL; int32 *buffer2 = (int32 *)NULL; - + /* Check for valid swath ID */ /* ------------------------ */ status = SWchkswid(swathID, "SWupdateidxmap", &fid, &sdInterfaceID, &swVgrpID); - - + + /* Check for valid region ID */ /* ------------------------- */ if (status == 0) @@ -10842,7 +11043,7 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou HEreport("Invalid Region id: %d.\n", regionID); } } - + /* Check for active region ID */ /* -------------------------- */ if (status == 0) @@ -10854,20 +11055,20 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou HEreport("Inactive Region ID: %d.\n", regionID); } } - + if (status == 0) { /* Loop through all regions */ /* ------------------------ */ for (k = 0; k < SWXRegion[regionID]->nRegions; k++) { - + /* fix overlap l_index mapping problem for Landsat 7 */ - + startReg = SWXRegion[regionID]->StartRegion[k]; stopReg = SWXRegion[regionID]->StopRegion[k]; - - + + if(SWXRegion[regionID]->scanflag == 1) { indicies[0] = -1; @@ -10884,16 +11085,16 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou land_status = SWreadattr(swathID, "detector_count", &detect_cnt); } - + /* calculate the offsets first */ buffer1 = (int32 *)calloc(74, sizeof(int32)); buffer2 = (int32 *)calloc(74, sizeof(int32)); - + status = SWidxmapinfo(swathID,"GeoTrack", "ScanLineTrack", (int32*)buffer1); status = SWidxmapinfo(swathID,"UpperTrack", "ScanLineTrack", (int32*)buffer2); - + indexoffset = buffer2[0] - buffer1[0]; free(buffer1); free(buffer2); @@ -10972,11 +11173,11 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou { for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt) && + if( startReg >= (l_indexin[j] + indexoffset - detect_cnt) && startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) ) if(indicies[0] == -1) indicies[0] = j; - if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && + if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) indicies[1] = j + 1; j = j + 2; @@ -10992,7 +11193,7 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou if(SWXRegion[regionID]->band8flag == 2 || SWXRegion[regionID]->band8flag == 3) { - + if(startReg >= (l_indexin[j - 1] + indexoffset - detect_cnt)) { indicies[0] = -1; @@ -11007,11 +11208,11 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou j = 0; for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt) && + if( startReg >= (l_indexin[j] + indexoffset - detect_cnt) && startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) ) if(indicies[0] == -1) indicies[0] = j; - if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && + if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) indicies[1] = j + 1; j = j + 2; @@ -11025,11 +11226,11 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou j = 0; for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt) && + if( startReg >= (l_indexin[j] + indexoffset - detect_cnt) && startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) ) if(indicies[0] == -1) indicies[0] = j; - + j = j + 2; if(l_indexin[j] == 0 || l_indexin[j+1] == 0) i = scene_cnt; @@ -11044,7 +11245,7 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou j = 0; for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt ) && + if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) indicies[1] = j; j = j + 2; @@ -11145,14 +11346,14 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou j = 0; for(i=0;i= (l_indexin[j] + indexoffset - detect_cnt ) && + if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) indicies[1] = j; j = j + 2; if(l_indexin[j] == 0 || l_indexin[j+1] == 0) i = scene_cnt; } - } + } } if(startReg > (l_indexin[j - 1] + indexoffset - detect_cnt )) @@ -11169,11 +11370,11 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou j = 0; for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt) && + if( startReg >= (l_indexin[j] + indexoffset - detect_cnt) && startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) ) if(indicies[0] == -1) indicies[0] = j; - if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && + if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) indicies[1] = j + 1; j = j + 2; @@ -11198,7 +11399,7 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou { startReg++; } - + /* If end of region is even then decrement */ /* --------------------------------------- */ if (stopReg % 2 == 0) @@ -11210,9 +11411,9 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou indicies[1]=stopReg; } } - + if (l_indexout != NULL) - { + { if(SWXRegion[regionID]->scanflag == 1) { nout = (indicies[1] - indicies[0] + 1); @@ -11240,19 +11441,19 @@ SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexou nout = indicies[1] - indicies[0] + 1; } } - - + + if(status == -1) - { + { return(status); } else { return(nout); } - -} +} +#endif /*----------------------------------------------------------------------------| @@ -11292,7 +11493,7 @@ SWgeomapinfo(int32 swathID, const char *geodim) { intn status; /* routine return status variable */ - + int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 swVgrpID; /* Swath root Vgroup ID */ @@ -11305,19 +11506,19 @@ SWgeomapinfo(int32 swathID, const char *geodim) char swathname[80]; /* Swath Name */ char *utlstrr; /* Utility string */ char *utlstri; /* Utility string */ - - + + /* Allocate space for utility string */ /* --------------------------------- */ utlstrr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstrr == NULL) - { + { HEpush(DFE_NOSPACE,"SWgeomapinfo", __FILE__, __LINE__); return(-1); } utlstri = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstri == NULL) - { + { HEpush(DFE_NOSPACE,"SWgeomapinfo", __FILE__, __LINE__); free(utlstrr); return(-1); @@ -11329,7 +11530,14 @@ SWgeomapinfo(int32 swathID, const char *geodim) if (status == 0) { /* Get swath name */ - Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname); + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstrr); + free(utlstri); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); /* Get pointers to "DimensionMap" section within SM */ metabufr = EHmetagroup(sdInterfaceID, swathname, "s", @@ -11345,7 +11553,7 @@ SWgeomapinfo(int32 swathID, const char *geodim) snprintf(utlstrr, UTLSTR_MAX_SIZE, "%s%s%s", "\t\t\t\tGeoDimension=\"", geodim, "\"\n\t\t\t\tDataDimension="); metaptrsr[0] = strstr(metaptrsr[0], utlstrr); - + /* Get pointers to "IndexDimensionMap" section within SM */ metabufi = EHmetagroup(sdInterfaceID, swathname, "s", "IndexDimensionMap", metaptrsi); @@ -11366,12 +11574,12 @@ SWgeomapinfo(int32 swathID, const char *geodim) */ if (metaptrsr[0] < metaptrsr[1] && metaptrsr[0] != NULL) { - status = status + 1; + status = status + 1; } if (metaptrsi[0] < metaptrsi[1] && metaptrsi[0] != NULL) { - status = status + 2; + status = status + 2; } free(metabufr);