/* Write a NC_var to the header */ static int v1h_put_NC_var(v1hs *psp, const NC_var *varp) { int status; status = v1h_put_NC_string(psp, varp->name); if(status != ENOERR) return status; status = v1h_put_size_t(psp, &varp->ndims); if(status != ENOERR) return status; if (psp->version == 5) { status = check_v1hs(psp, ncx_len_int64(varp->ndims)); if(status != ENOERR) return status; status = ncx_putn_longlong_int(&psp->pos, varp->ndims, varp->dimids); if(status != ENOERR) return status; } else { status = check_v1hs(psp, ncx_len_int(varp->ndims)); if(status != ENOERR) return status; status = ncx_putn_int_int(&psp->pos, varp->ndims, varp->dimids); if(status != ENOERR) return status; } status = v1h_put_NC_attrarray(psp, &varp->attrs); if(status != ENOERR) return status; status = v1h_put_nc_type(psp, &varp->type); if(status != ENOERR) return status; status = v1h_put_size_t(psp, &varp->len); if(status != ENOERR) return status; status = check_v1hs(psp, psp->version == 1 ? 4 : 8); /*begin*/ if(status != ENOERR) return status; status = ncx_put_off_t(&psp->pos, &varp->begin, psp->version == 1 ? 4 : 8); if(status != ENOERR) return status; return ENOERR; }
/* Write a size_t to the header */ static int v1h_put_size_t(v1hs *psp, const size_t *sp) { int status; if (psp->version == 5) /* all integers in CDF-5 are 64 bits */ status = check_v1hs(psp, X_SIZEOF_INT64); else status = check_v1hs(psp, X_SIZEOF_SIZE_T); if(status != ENOERR) return status; if (psp->version == 5) return ncx_put_int64(&psp->pos, *sp); else return ncx_put_size_t(&psp->pos, sp); }
/* * Get the values of an attribute * The loop is necessary since attrp->nelems * could potentially be quite large. */ static int v1h_get_NC_attrV(v1hs *gsp, NC_attr *attrp) { int status; const size_t perchunk = gsp->extent; size_t remaining = attrp->xsz; void *value = attrp->xvalue; size_t nget; do { nget = MIN(perchunk, remaining); status = check_v1hs(gsp, nget); if(status != ENOERR) return status; (void) memcpy(value, gsp->pos, nget); gsp->pos = (void *)((char *)gsp->pos + nget); value = (void *)((char *)value + nget); remaining -= nget; } while(remaining != 0); return ENOERR; }
/* * Put the values of an attribute * The loop is necessary since attrp->nelems * could potentially be quite large. */ static int v1h_put_NC_attrV(v1hs *psp, const NC_attr *attrp) { int status; const size_t perchunk = psp->extent; size_t remaining = attrp->xsz; void *value = attrp->xvalue; size_t nbytes; assert(psp->extent % X_ALIGN == 0); do { nbytes = MIN(perchunk, remaining); status = check_v1hs(psp, nbytes); if(status != ENOERR) return status; (void) memcpy(psp->pos, value, nbytes); psp->pos = (void *)((char *)psp->pos + nbytes); value = (void *)((char *)value + nbytes); remaining -= nbytes; } while(remaining != 0); return ENOERR; }
/* Read a nc_type from the header */ static int v1h_get_nc_type(v1hs *gsp, nc_type *typep) { int type = 0; int status = check_v1hs(gsp, X_SIZEOF_INT); if(status != ENOERR) return status; status = ncx_get_int_int(gsp->pos, &type); gsp->pos = (void *)((char *)gsp->pos + X_SIZEOF_INT); if(status != ENOERR) return status; assert(type == NC_BYTE || type == NC_CHAR || type == NC_SHORT || type == NC_INT || type == NC_FLOAT || type == NC_DOUBLE || type == NC_UBYTE || type == NC_USHORT || type == NC_UINT || type == NC_INT64 || type == NC_UINT64 || type == NC_STRING); /* else */ *typep = (nc_type) type; return ENOERR; }
/* Read a size_t from the header */ static int v1h_get_size_t(v1hs *gsp, size_t *sp) { int status = check_v1hs(gsp, X_SIZEOF_SIZE_T); if(status != ENOERR) return status; return ncx_get_size_t((const void **)(&gsp->pos), sp); }
/* Write a size_t to the header */ static int v1h_put_size_t(v1hs *psp, const size_t *sp) { int status = check_v1hs(psp, X_SIZEOF_SIZE_T); if(status != ENOERR) return status; return ncx_put_size_t(&psp->pos, sp); }
/* Read a NC_string from the header */ static int v1h_get_NC_string(v1hs *gsp, NC_string **ncstrpp) { int status; size_t nchars = 0; NC_string *ncstrp; status = v1h_get_size_t(gsp, &nchars); if(status != ENOERR) return status; ncstrp = new_NC_string(nchars, NULL); if(ncstrp == NULL) { return NC_ENOMEM; } #if 0 /* assert(ncstrp->nchars == nchars || ncstrp->nchars - nchars < X_ALIGN); */ assert(ncstrp->nchars % X_ALIGN == 0); status = check_v1hs(gsp, ncstrp->nchars); #else status = check_v1hs(gsp, _RNDUP(ncstrp->nchars, X_ALIGN)); #endif if(status != ENOERR) goto unwind_alloc; status = ncx_pad_getn_text((const void **)(&gsp->pos), nchars, ncstrp->cp); if(status != ENOERR) goto unwind_alloc; *ncstrpp = ncstrp; return ENOERR; unwind_alloc: free_NC_string(ncstrp); return status; }
/* Read a size_t from the header */ static int v1h_get_size_t(v1hs *gsp, size_t *sp) { int status; if (gsp->version == 5) /* all integers in CDF-5 are 64 bits */ status = check_v1hs(gsp, X_SIZEOF_INT64); else status = check_v1hs(gsp, X_SIZEOF_SIZE_T); if(status != ENOERR) return status; if (gsp->version == 5) { long long tmp=0; status = ncx_get_int64((const void **)(&gsp->pos), &tmp); *sp = (size_t)tmp; return status; } else return ncx_get_size_t((const void **)(&gsp->pos), sp); }
/* Write a NCtype to the header */ static int v1h_put_NCtype(v1hs *psp, NCtype type) { const int itype = (int) type; int status = check_v1hs(psp, X_SIZEOF_INT); if(status != ENOERR) return status; status = ncx_put_int_int(psp->pos, &itype); psp->pos = (void *)((char *)psp->pos + X_SIZEOF_INT); return status; }
/* Read a NCtype from the header */ static int v1h_get_NCtype(v1hs *gsp, NCtype *typep) { int type = 0; int status = check_v1hs(gsp, X_SIZEOF_INT); if(status != ENOERR) return status; status = ncx_get_int_int(gsp->pos, &type); gsp->pos = (void *)((char *)gsp->pos + X_SIZEOF_INT); if(status != ENOERR) return status; /* else */ *typep = (NCtype) type; return ENOERR; }
/* Write a NCtype to the header */ static int v1h_put_NCtype(v1hs *psp, NCtype type) { const int itype = (int) type; int status = check_v1hs(psp, X_SIZEOF_INT); if(status != NC_NOERR) return status; status = ncx_put_int_int(psp->pos, &itype); #ifdef __arm__ psp->pos = (void *)((signed char *)psp->pos + X_SIZEOF_INT); #else psp->pos = (void *)((char *)psp->pos + X_SIZEOF_INT); #endif return status; }
/* Read a NCtype from the header */ static int v1h_get_NCtype(v1hs *gsp, NCtype *typep) { int type = 0; int status = check_v1hs(gsp, X_SIZEOF_INT); if(status != NC_NOERR) return status; status = ncx_get_int_int(gsp->pos, &type); #ifdef __arm__ gsp->pos = (void *)((signed char *)gsp->pos + X_SIZEOF_INT); #else gsp->pos = (void *)((char *)gsp->pos + X_SIZEOF_INT); #endif if(status != NC_NOERR) return status; /* else */ *typep = (NCtype) type; return NC_NOERR; }
/* Write a NC_string to the header */ static int v1h_put_NC_string(v1hs *psp, const NC_string *ncstrp) { int status; #if 0 assert(ncstrp->nchars % X_ALIGN == 0); #endif status = v1h_put_size_t(psp, &ncstrp->nchars); if(status != ENOERR) return status; status = check_v1hs(psp, _RNDUP(ncstrp->nchars, X_ALIGN)); if(status != ENOERR) return status; status = ncx_pad_putn_text(&psp->pos, ncstrp->nchars, ncstrp->cp); if(status != ENOERR) return status; return ENOERR; }
/* Read a NC_var from the header */ static int v1h_get_NC_var(v1hs *gsp, NC_var **varpp) { NC_string *strp; int status; size_t ndims; NC_var *varp; status = v1h_get_NC_string(gsp, &strp); if(status != ENOERR) return status; status = v1h_get_size_t(gsp, &ndims); if(status != ENOERR) goto unwind_name; varp = new_x_NC_var(strp, ndims); if(varp == NULL) { status = NC_ENOMEM; goto unwind_name; } if (gsp->version == 5) { status = check_v1hs(gsp, ncx_len_int64(ndims)); if(status != ENOERR) goto unwind_alloc; status = ncx_getn_longlong_int((const void **)(&gsp->pos), ndims, varp->dimids); if(status != ENOERR) goto unwind_alloc; } else { status = check_v1hs(gsp, ncx_len_int(ndims)); if(status != ENOERR) goto unwind_alloc; status = ncx_getn_int_int((const void **)(&gsp->pos), ndims, varp->dimids); if(status != ENOERR) goto unwind_alloc; } status = v1h_get_NC_attrarray(gsp, &varp->attrs); if(status != ENOERR) goto unwind_alloc; status = v1h_get_nc_type(gsp, &varp->type); if(status != ENOERR) goto unwind_alloc; status = v1h_get_size_t(gsp, &varp->len); if(status != ENOERR) goto unwind_alloc; status = check_v1hs(gsp, gsp->version == 1 ? 4 : 8); if(status != ENOERR) goto unwind_alloc; status = ncx_get_off_t((const void **)&gsp->pos, &varp->begin, gsp->version == 1 ? 4 : 8); if(status != ENOERR) goto unwind_alloc; *varpp = varp; return ENOERR; unwind_alloc: free_NC_var(varp); /* frees name */ return status; unwind_name: free_NC_string(strp); return status; }