static OCmemdata* makememdata(OCtype octype, OCtype etype, unsigned long count) { unsigned long memsize; OCmemdata* memdata; if(octype == OC_Primitive) memsize = instancesize(etype)*count; else memsize = instancesize(octype)*count; memdata = (OCmemdata*)ocmalloc(sizeof(OCmemdata) + memsize); if(memdata == NULL) return NULL; memdata->octype = (unsigned long)octype; memdata->etype = (unsigned long)etype; if(memdata->etype > 107) OCPANIC("help"); memdata->count = count; return memdata; }
static void dumpocnode1(OCnode* node, int depth) { unsigned int n; switch (node->octype) { case OC_Atomic: { fprintf(stdout,"[%2d]%s ",depth,dent(depth)); if(node->name == NULL) OCPANIC("prim without name"); fprintf(stdout,"%s %s",octypetostring(node->etype),node->name); dumpdimensions(node); fprintf(stdout," &%lx",(unsigned long)node); fprintf(stdout,"\n"); } break; case OC_Dataset: { fprintf(stdout,"[%2d]%s ",depth,dent(depth)); fprintf(stdout,"dataset %s\n", (node->name?node->name:"")); for(n=0;n<oclistlength(node->subnodes);n++) { dumpocnode1((OCnode*)oclistget(node->subnodes,n),depth+1); } } break; case OC_Structure: { fprintf(stdout,"[%2d]%s ",depth,dent(depth)); fprintf(stdout,"struct %s", (node->name?node->name:"")); dumpdimensions(node); fprintf(stdout," &%lx",(unsigned long)node); fprintf(stdout,"\n"); for(n=0;n<oclistlength(node->subnodes);n++) { dumpocnode1((OCnode*)oclistget(node->subnodes,n),depth+1); } } break; case OC_Sequence: { fprintf(stdout,"[%2d]%s ",depth,dent(depth)); fprintf(stdout,"sequence %s", (node->name?node->name:"")); dumpdimensions(node); fprintf(stdout," &%lx",(unsigned long)node); fprintf(stdout,"\n"); for(n=0;n<oclistlength(node->subnodes);n++) { dumpocnode1((OCnode*)oclistget(node->subnodes,n),depth+1); } } break; case OC_Grid: { unsigned int i; fprintf(stdout,"[%2d]%s ",depth,dent(depth)); fprintf(stdout,"grid %s", (node->name?node->name:"")); dumpdimensions(node); fprintf(stdout," &%lx",(unsigned long)node); fprintf(stdout,"\n"); fprintf(stdout,"%sarray:\n",dent2(depth+1)); dumpocnode1((OCnode*)oclistget(node->subnodes,0),depth+2); fprintf(stdout,"%smaps:\n",dent2(depth+1)); for(i=1;i<oclistlength(node->subnodes);i++) { dumpocnode1((OCnode*)oclistget(node->subnodes,i),depth+2); } } break; case OC_Attribute: { fprintf(stdout,"[%2d]%s ",depth,dent(depth)); if(node->name == NULL) OCPANIC("Attribute without name"); fprintf(stdout,"%s %s",octypetostring(node->etype),node->name); for(n=0;n<oclistlength(node->att.values);n++) { char* value = (char*)oclistget(node->att.values,n); if(n > 0) fprintf(stdout,","); fprintf(stdout," %s",value); } fprintf(stdout," &%lx",(unsigned long)node); fprintf(stdout,"\n"); } break; case OC_Attributeset: { fprintf(stdout,"[%2d]%s ",depth,dent(depth)); fprintf(stdout,"%s:\n",node->name?node->name:"Attributes"); for(n=0;n<oclistlength(node->subnodes);n++) { dumpocnode1((OCnode*)oclistget(node->subnodes,n),depth+1); } } break; default: OCPANIC1("encountered unexpected node type: %x",node->octype); } if(node->attributes != NULL) { unsigned int i; for(i=0;i<oclistlength(node->attributes);i++) { OCattribute* att = (OCattribute*)oclistget(node->attributes,i); fprintf(stdout,"%s[%s=",dent2(depth+2),att->name); if(att->nvalues == 0) OCPANIC("Attribute.nvalues == 0"); if(att->nvalues == 1) { dumpattvalue(att->etype,att->values,0); } else { unsigned int j; fprintf(stdout,"{"); for(j=0;j<att->nvalues;j++) { if(j>0) fprintf(stdout,", "); dumpattvalue(att->etype,att->values,j); } fprintf(stdout,"}"); } fprintf(stdout,"]\n"); } } }
static OCerror ocread(OCdata* data, XXDR* xdrs, char* memory, size_t memsize, size_t start, size_t count) { int i; OCnode* pattern; 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); pattern = data->pattern; etype = pattern->etype; scalar = (pattern->array.rank == 0); /* 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); if(!xxdr_getbytes(xdrs,memory,xdrtotal)) {OCTHROW(OC_EDATADDS); goto xdrfail;} 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); if(!xxdr_getbytes(xdrs,memory,xdrtotal)) {OCTHROW(OC_EDATADDS); goto xdrfail;} 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); if(!xxdr_getbytes(xdrs,memory,xdrtotal)) {OCTHROW(OC_EDATADDS); goto xdrfail;} { 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 */ /* Remember also that the short is not packed, so its xdr size is twice its memory size */ xxdr_setpos(xdrs,data->xdroffset+xdrstart); if(scalar) { if(!xxdr_ushort(xdrs,(unsigned short*)memory)) {OCTHROW(OC_EDATADDS); goto xdrfail;} } else { unsigned short* sp = (unsigned short*)memory; for(i=0;i<count;i++,sp++) { unsigned int tmp; if(!xxdr_getbytes(xdrs,(char*)&tmp,(off_t)XDRUNIT)) {OCTHROW(OC_EDATADDS); goto xdrfail;} /* convert from network order if necessary */ if(!xxdr_network_order) swapinline32(&tmp); /* store as unsigned short */ *sp = (unsigned short)tmp; } } } 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); if(!xxdr_uchar(xdrs,(unsigned char*)memory)) {OCTHROW(OC_EDATADDS); goto xdrfail;} } 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); if(!xxdr_getbytes(xdrs,memory,xdrtotal)) {OCTHROW(OC_EDATADDS); goto xdrfail;} } 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; off_t offset = data->strings[start+i]; xxdr_setpos(xdrs,offset); /* get the string */ if(!xxdr_string(xdrs,sp,&len)) {OCTHROW(OC_EDATADDS); goto xdrfail;} } } break; default: OCPANIC("unexpected etype"); break; } return OC_NOERR; xdrfail: oclog(OCLOGERR,"DAP DATADDS packet is apparently too short"); return OCTHROW(OC_EDATADDS); }