2018-12-07 06:47:47 +08:00
|
|
|
/* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc.
|
2012-08-01 04:34:13 +08:00
|
|
|
See the COPYRIGHT file for more information. */
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "ocinternal.h"
|
|
|
|
#include "ocdebug.h"
|
|
|
|
#include "ocdump.h"
|
|
|
|
|
|
|
|
/* Forward*/
|
|
|
|
static OCerror ocread(OCdata*, XXDR*, char*, size_t, size_t, size_t);
|
|
|
|
|
|
|
|
#ifdef OCDEBUG
|
|
|
|
|
|
|
|
static void
|
|
|
|
octrace(char* proc, OCstate* state, OCdata* data)
|
|
|
|
{
|
2017-03-18 07:20:02 +08:00
|
|
|
NCbytes* buffer = ncbytesnew();
|
2012-08-01 04:34:13 +08:00
|
|
|
ocdumpdatapath(state,data,buffer);
|
2017-03-18 07:20:02 +08:00
|
|
|
fprintf(stderr,"%s: %s\n",proc,ncbytescontents(buffer));
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define octrace(proc,state,data)
|
|
|
|
#endif /*OCDEBUG*/
|
|
|
|
|
|
|
|
/* Use this to attach to a data tree for a DATADDS */
|
|
|
|
OCerror
|
|
|
|
ocdata_getroot(OCstate* state, OCnode* root, OCdata** datap)
|
|
|
|
{
|
|
|
|
OCdata* data;
|
|
|
|
assert(root->tree->dxdclass == OCDATADDS);
|
|
|
|
assert(root->octype == OC_Dataset);
|
|
|
|
|
|
|
|
if(root->tree->data.data == NULL)
|
|
|
|
return OCTHROW(OC_ENODATA);
|
|
|
|
|
|
|
|
data = root->tree->data.data;
|
|
|
|
if(datap) *datap = data;
|
|
|
|
|
|
|
|
octrace("attach",state,data);
|
|
|
|
|
|
|
|
return OCTHROW(OC_NOERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
OCerror
|
|
|
|
ocdata_container(OCstate* state, OCdata* data, OCdata** containerp)
|
|
|
|
{
|
|
|
|
OCdata* container;
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
OCASSERT(state != NULL);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern = data->pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
if(pattern->container == NULL)
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(OC_EBADTYPE);
|
|
|
|
|
|
|
|
container = data->container;
|
|
|
|
if(container == NULL)
|
|
|
|
return OCTHROW(OC_EBADTYPE);
|
|
|
|
|
|
|
|
if(containerp) *containerp = container;
|
|
|
|
|
|
|
|
octrace("container", state, container);
|
|
|
|
|
|
|
|
return OC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
OCerror
|
|
|
|
ocdata_root(OCstate* state, OCdata* data, OCdata** rootp)
|
|
|
|
{
|
|
|
|
OCdata* root;
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
OCASSERT(state != NULL);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern = data->pattern;
|
|
|
|
root = pattern->tree->data.data;
|
2012-08-01 04:34:13 +08:00
|
|
|
if(rootp) *rootp = root;
|
|
|
|
|
|
|
|
octrace("root", state, root);
|
|
|
|
|
|
|
|
return OC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
OCerror
|
|
|
|
ocdata_ithfield(OCstate* state, OCdata* container, size_t index, OCdata** fieldp)
|
|
|
|
{
|
|
|
|
OCdata* field;
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
OCASSERT(state != NULL);
|
|
|
|
OCASSERT(container != NULL);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern = container->pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
if(!ociscontainer(pattern->octype))
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(OC_EBADTYPE);
|
|
|
|
|
|
|
|
/* Validate index */
|
|
|
|
if(index >= container->ninstances)
|
|
|
|
return OCTHROW(OC_EINDEX);
|
|
|
|
|
|
|
|
field = container->instances[index];
|
|
|
|
if(fieldp) *fieldp = field;
|
|
|
|
|
|
|
|
octrace("ithfield", state, field);
|
|
|
|
|
|
|
|
return OC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
OCerror
|
|
|
|
ocdata_ithelement(OCstate* state, OCdata* data, size_t* indices, OCdata** elementp)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
OCdata* element;
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
size_t index,rank;
|
|
|
|
|
|
|
|
OCASSERT(state != NULL);
|
|
|
|
OCASSERT(data != NULL);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern = data->pattern;
|
|
|
|
rank = pattern->array.rank;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Must be a dimensioned Structure */
|
2015-01-16 05:19:51 +08:00
|
|
|
if(pattern->octype != OC_Structure || rank == 0)
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(OC_EBADTYPE);
|
|
|
|
|
|
|
|
/* Validate indices */
|
2015-01-16 05:19:51 +08:00
|
|
|
if(!ocvalidateindices(rank,pattern->array.sizes,indices))
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(OC_EINVALCOORDS);
|
|
|
|
|
|
|
|
/* compute linearized index */
|
2015-01-16 05:19:51 +08:00
|
|
|
index = ocarrayoffset(rank,pattern->array.sizes,indices);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
if(index >= data->ninstances)
|
|
|
|
return OCTHROW(OC_EINDEX);
|
|
|
|
|
|
|
|
element = data->instances[index];
|
|
|
|
|
|
|
|
if(elementp) *elementp = element;
|
|
|
|
|
|
|
|
octrace("ithelement", state, element);
|
|
|
|
|
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move to the ith sequence record. */
|
|
|
|
OCerror
|
|
|
|
ocdata_ithrecord(OCstate* state, OCdata* data,
|
|
|
|
size_t index, /* record number */
|
|
|
|
OCdata** recordp
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
OCdata* record;
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
OCASSERT(state != NULL);
|
|
|
|
OCASSERT(data != NULL);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern = data->pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Must be a Sequence */
|
2015-01-16 05:19:51 +08:00
|
|
|
if(pattern->octype != OC_Sequence
|
2012-08-01 04:34:13 +08:00
|
|
|
|| !fisset(data->datamode,OCDT_SEQUENCE))
|
|
|
|
return OCTHROW(OC_EBADTYPE);
|
|
|
|
|
|
|
|
/* Validate index */
|
|
|
|
if(index >= data->ninstances)
|
|
|
|
return OCTHROW(OC_EINDEX);
|
|
|
|
|
|
|
|
record = data->instances[index];
|
|
|
|
|
|
|
|
if(recordp) *recordp = record;
|
|
|
|
|
|
|
|
octrace("ithrecord", state, record);
|
|
|
|
|
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
OCerror
|
|
|
|
ocdata_position(OCstate* state, OCdata* data, size_t* indices)
|
|
|
|
{
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
OCASSERT(state != NULL);
|
|
|
|
OCASSERT(data != NULL);
|
|
|
|
OCASSERT(indices != NULL);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern = data->pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
if(fisset(data->datamode,OCDT_RECORD))
|
|
|
|
indices[0] = data->index;
|
|
|
|
else if(fisset(data->datamode,OCDT_ELEMENT)) {
|
|
|
|
/* Transform the linearized array index into a set of indices */
|
2014-03-09 11:41:30 +08:00
|
|
|
ocarrayindices(data->index,
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern->array.rank,
|
|
|
|
pattern->array.sizes,
|
2014-03-09 11:41:30 +08:00
|
|
|
indices);
|
2012-08-01 04:34:13 +08:00
|
|
|
} else
|
|
|
|
return OCTHROW(OC_EBADTYPE);
|
|
|
|
return OCTHROW(OC_NOERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
OCerror
|
|
|
|
ocdata_recordcount(OCstate* state, OCdata* data, size_t* countp)
|
|
|
|
{
|
|
|
|
OCASSERT(state != NULL);
|
|
|
|
OCASSERT(data != NULL);
|
|
|
|
OCASSERT(countp != NULL);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
if(data->pattern->octype != OC_Sequence
|
2012-08-01 04:34:13 +08:00
|
|
|
|| !fisset(data->datamode,OCDT_SEQUENCE))
|
|
|
|
return OCTHROW(OC_EBADTYPE);
|
|
|
|
|
|
|
|
*countp = data->ninstances;
|
|
|
|
|
|
|
|
return OC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
/*
|
|
|
|
In order to actually extract data, one must move to the
|
|
|
|
specific primitive typed field containing the data of
|
|
|
|
interest by using ocdata_fieldith(). Then this procedure
|
|
|
|
is invoked to extract some subsequence of items from the
|
|
|
|
field. For scalars, the start and count are ignored.
|
|
|
|
Note that start and count are linearized from the oc_data_read
|
|
|
|
arguments.
|
|
|
|
*/
|
|
|
|
|
2014-05-09 04:13:51 +08:00
|
|
|
OCerror
|
2012-08-01 04:34:13 +08:00
|
|
|
ocdata_read(OCstate* state, OCdata* data, size_t start, size_t count,
|
|
|
|
void* memory, size_t memsize)
|
|
|
|
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
XXDR* xdrs;
|
2015-05-25 07:31:39 +08:00
|
|
|
OCtype etype;
|
2012-08-01 04:34:13 +08:00
|
|
|
int isscalar;
|
|
|
|
size_t elemsize, totalsize, countsize;
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
octrace("read", state, data);
|
|
|
|
|
|
|
|
assert(state != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
assert(memory != NULL);
|
|
|
|
assert(memsize > 0);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern = data->pattern;
|
2015-05-25 07:31:39 +08:00
|
|
|
assert(pattern->octype == OC_Atomic);
|
2015-01-16 05:19:51 +08:00
|
|
|
etype = pattern->etype;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
isscalar = (pattern->array.rank == 0 ? 1 : 0);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* validate memory space*/
|
|
|
|
elemsize = octypesize(etype);
|
|
|
|
totalsize = elemsize*data->ninstances;
|
|
|
|
countsize = elemsize*count;
|
|
|
|
if(totalsize < countsize || memsize < countsize)
|
|
|
|
return OCTHROW(OC_EINVAL);
|
|
|
|
|
|
|
|
/* Get XXDR* */
|
2015-01-16 05:19:51 +08:00
|
|
|
xdrs = pattern->root->tree->data.xdrs;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
if(isscalar) {
|
|
|
|
/* Extract the data */
|
|
|
|
stat = ocread(data,xdrs,(char*)memory,memsize,0,1);
|
|
|
|
} else {
|
|
|
|
/* Validate the start and count */
|
|
|
|
if(start >= data->ninstances
|
|
|
|
|| (start+count) > data->ninstances)
|
|
|
|
return OCTHROW(OC_EINVALCOORDS);
|
|
|
|
/* Extract the data */
|
|
|
|
stat = ocread(data,xdrs,(char*)memory,memsize,start,count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
/*
|
|
|
|
Extract data from a leaf into memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static OCerror
|
|
|
|
ocread(OCdata* data, XXDR* xdrs, char* memory, size_t memsize, size_t start, size_t count)
|
|
|
|
{
|
|
|
|
int i;
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
OCtype etype;
|
|
|
|
off_t elemsize, totalsize, xdrtotal, xdrstart;
|
|
|
|
int scalar;
|
|
|
|
|
|
|
|
OCASSERT(data != NULL);
|
|
|
|
OCASSERT(memory != NULL);
|
|
|
|
OCASSERT(memsize > 0);
|
|
|
|
OCASSERT(count > 0);
|
|
|
|
OCASSERT((start+count) <= data->ninstances);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern = data->pattern;
|
|
|
|
etype = pattern->etype;
|
|
|
|
scalar = (pattern->array.rank == 0);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Note that for strings, xdrsize == 0 */
|
|
|
|
xdrtotal = count*data->xdrsize; /* amount (in xdr sizes) to read */
|
|
|
|
xdrstart = start*data->xdrsize; /* offset from the start of the data */
|
|
|
|
|
|
|
|
elemsize = octypesize(etype); /* wrt memory, not xdrsize */
|
|
|
|
totalsize = elemsize*count;
|
|
|
|
|
|
|
|
/* validate memory space*/
|
|
|
|
if(memsize < totalsize) return OCTHROW(OC_EINVAL);
|
|
|
|
|
|
|
|
/* copy out with appropriate byte-order conversions */
|
|
|
|
switch (etype) {
|
|
|
|
|
|
|
|
case OC_Int32: case OC_UInt32: case OC_Float32:
|
|
|
|
xxdr_setpos(xdrs,data->xdroffset+xdrstart);
|
2017-03-09 08:01:10 +08:00
|
|
|
if(!xxdr_getbytes(xdrs,memory,xdrtotal)) {goto xdrfail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
if(!xxdr_network_order) {
|
|
|
|
unsigned int* p;
|
|
|
|
for(p=(unsigned int*)memory,i=0;i<count;i++,p++) {
|
|
|
|
swapinline32(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OC_Int64: case OC_UInt64:
|
|
|
|
xxdr_setpos(xdrs,data->xdroffset+xdrstart);
|
2017-03-09 08:01:10 +08:00
|
|
|
if(!xxdr_getbytes(xdrs,memory,xdrtotal)) {goto xdrfail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
if(!xxdr_network_order) {
|
|
|
|
unsigned long long* llp;
|
|
|
|
for(llp=(unsigned long long*)memory,i=0;i<count;i++,llp++) {
|
|
|
|
swapinline64(llp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OC_Float64:
|
|
|
|
xxdr_setpos(xdrs,data->xdroffset+xdrstart);
|
2017-03-09 08:01:10 +08:00
|
|
|
if(!xxdr_getbytes(xdrs,memory,xdrtotal)) {goto xdrfail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
double* dp;
|
|
|
|
for(dp=(double*)memory,i=0;i<count;i++,dp++) {
|
|
|
|
double swap;
|
|
|
|
xxdrntohdouble((char*)dp,&swap);
|
|
|
|
*dp = swap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* non-packed fixed length, but memory size < xdrsize */
|
|
|
|
case OC_Int16: case OC_UInt16: {
|
|
|
|
/* In order to avoid allocating a lot of space, we do this one int at a time */
|
2012-08-13 01:40:08 +08:00
|
|
|
/* Remember also that the short is not packed, so its xdr size is twice
|
|
|
|
its memory size */
|
2012-08-01 04:34:13 +08:00
|
|
|
xxdr_setpos(xdrs,data->xdroffset+xdrstart);
|
|
|
|
if(scalar) {
|
2017-03-09 08:01:10 +08:00
|
|
|
if(!xxdr_ushort(xdrs,(unsigned short*)memory)) {goto xdrfail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
} else {
|
|
|
|
unsigned short* sp = (unsigned short*)memory;
|
|
|
|
for(i=0;i<count;i++,sp++) {
|
2012-08-13 01:40:08 +08:00
|
|
|
unsigned int tmp;
|
2014-03-09 11:41:30 +08:00
|
|
|
if(!xxdr_getbytes(xdrs,(char*)&tmp,(off_t)XDRUNIT))
|
2017-03-09 08:01:10 +08:00
|
|
|
{goto xdrfail;}
|
2012-08-13 01:40:08 +08:00
|
|
|
/* convert from network order if necessary */
|
|
|
|
if(!xxdr_network_order)
|
|
|
|
swapinline32(&tmp);
|
|
|
|
/* store as unsigned short */
|
|
|
|
*sp = (unsigned short)tmp;
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
/* Do the byte types, packed/unpacked */
|
|
|
|
case OC_Byte:
|
|
|
|
case OC_UByte:
|
|
|
|
case OC_Char:
|
|
|
|
if(scalar) {
|
|
|
|
/* scalar bytes are stored in xdr as int */
|
|
|
|
xxdr_setpos(xdrs,data->xdroffset+xdrstart);
|
2017-03-09 08:01:10 +08:00
|
|
|
if(!xxdr_uchar(xdrs,(unsigned char*)memory)) {goto xdrfail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
} else {
|
|
|
|
/* the xdroffset will always be at the start of the
|
|
|
|
packed data, so we need to add the start count to it */
|
|
|
|
xxdr_setpos(xdrs,data->xdroffset+xdrstart);
|
2017-03-09 08:01:10 +08:00
|
|
|
if(!xxdr_getbytes(xdrs,memory,xdrtotal)) {goto xdrfail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Hard case, because strings are variable length */
|
|
|
|
case OC_String: case OC_URL: {
|
|
|
|
/* use the data->nstrings data->string fields */
|
|
|
|
char** sp = (char**)memory;
|
|
|
|
if(count > data->nstrings)
|
|
|
|
return OCTHROW(OC_EDATADDS);
|
|
|
|
for(i=0;i<count;i++,sp++) {
|
|
|
|
off_t len;
|
2014-03-09 11:41:30 +08:00
|
|
|
off_t offset = data->strings[start+i];
|
2012-08-01 04:34:13 +08:00
|
|
|
xxdr_setpos(xdrs,offset);
|
|
|
|
/* get the string */
|
|
|
|
if(!xxdr_string(xdrs,sp,&len))
|
2017-03-09 08:01:10 +08:00
|
|
|
{goto xdrfail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: OCPANIC("unexpected etype"); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OC_NOERR;
|
|
|
|
|
|
|
|
xdrfail:
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGERR,"DAP DATADDS packet is apparently too short");
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(OC_EDATADDS);
|
|
|
|
}
|
|
|
|
|