static void dumpmemory0(char* memory, int len, int fromxdr, int bod) { unsigned int i,count,rem; int* imemory; #ifndef CRUDE char hdr[1024]; #endif assert(memory[len] == 0); #ifndef CRUDE /* build the header*/ hdr[0] = '\0'; dumpmem2("offset",hdr,6); dumpmem2("hex",hdr,8); dumpmem2("uint",hdr,12); dumpmem2("int",hdr,12); dumpmem2("float",hdr,12); dumpmem2("char[4]",hdr,16); dumpmem2("double",hdr,12); strcat(hdr,"\n"); fprintf(stderr,"%s",hdr); #endif count = (len / sizeof(int)); rem = (len % sizeof(int)); imemory = (int*)memory; for(i=0;i<count;i++) { unsigned int tmp0 = (unsigned int)imemory[i]; unsigned int tmp1 = (unsigned int)(i<count?imemory[i+1]:0); if(fromxdr) {tmp0 = ocntoh(tmp0); tmp1 = ocntoh(tmp1);} dumpmem1(i*sizeof(unsigned int)+bod,tmp0,tmp1); } if(rem > 0) { unsigned int tmp = 0; memcpy((void*)&tmp,(void*)(imemory+(sizeof(len)*count)),rem); if(fromxdr) tmp = ocntoh(tmp); dumpmem1(count*sizeof(unsigned int)+bod,tmp,0); } fflush(stderr); }
int ocinternalinitialize(void) { int stat = OC_NOERR; /* Compute if we are same as network order v-a-v xdr */ #ifdef XDRBYTEORDER { XDR xdrs; union { char tmp[sizeof(unsigned int)]; unsigned int i; } u; int testint = 1; xrmem_create(&xdrs, (caddr_t)&u.tmp,sizeof(u.tmp), XDR_ENCODE); xdr_int(&xdrs, &testint); oc_network_order = (udub.i == testint?1:0); oc_big_endian = oc_network_order; } #else { int testint = 0x00000001; char *byte = (char *)&testint; oc_big_endian = (byte[0] == 0 ? 1 : 0); oc_network_order = oc_big_endian; } #endif /*XDRBYTEORDER*/ { /* It turns out that various machines store double in different formats. This affects the conversion code ocdata.c when reconstructing; probably could deduce this from oc_big_endian, but not sure. */ XDR xdrs; union { double dv; unsigned int i; unsigned int iv[2]; char tmp[sizeof(double)]; } udub; double testdub = 18000; /* verify double vs int size */ if(sizeof(double) != (2 * sizeof(unsigned int))) ocpanic("|double| != 2*|int|"); if(sizeof(udub) != sizeof(double)) ocpanic("|double| != |udub|"); memset((void*)&udub,0,sizeof(udub)); xdrmem_create(&xdrs, (caddr_t)&udub.tmp,sizeof(udub.tmp), XDR_ENCODE); xdr_double(&xdrs, &testdub); udub.iv[0] = ocntoh(udub.iv[0]); udub.iv[1] = ocntoh(udub.iv[1]); if (udub.dv == testdub) oc_invert_xdr_double = 0; else { /* swap and try again */ unsigned int temp = udub.iv[0]; udub.iv[0] = udub.iv[1]; udub.iv[1] = temp; if (udub.dv != testdub) ocpanic("cannot unpack xdr_double"); oc_invert_xdr_double = 1; } } oc_loginit(); /* Determine if this version of curl supports "file://..." &/or "https://..." urls. */ { const char* const* proto; /*weird*/ curl_version_info_data* curldata; curldata = curl_version_info(CURLVERSION_NOW); oc_curl_file_supported = 0; oc_curl_https_supported = 0; for(proto=curldata->protocols;*proto;proto++) { if(strcmp("file",*proto)==0) {oc_curl_file_supported=1;break;} if(strcmp("https",*proto)==0) {oc_curl_https_supported=1;break;} } if(ocdebug > 0) { oc_log(LOGNOTE,"Curl file:// support = %d",oc_curl_file_supported); oc_log(LOGNOTE,"Curl https:// support = %d",oc_curl_file_supported); } } /* compile the .dodsrc, if any */ { char* path = NULL; char* homepath = NULL; FILE* f = NULL; /* locate the configuration files: . first, then $HOME */ path = (char*)malloc(strlen("./")+strlen(DODSRC)+1); if(path == NULL) return OC_ENOMEM; strcpy(path,"./"); strcat(path,DODSRC); /* see if file is readable */ f = fopen(path,"r"); if(f == NULL) { /* try $HOME */ homepath = getenv("HOME"); if (homepath!= NULL) { if(path != NULL) free(path); path = (char*)malloc(strlen(homepath)+1+strlen(DODSRC)+1); if(path == NULL) return OC_ENOMEM; strcpy(path,homepath); strcat(path,"/"); strcat(path,DODSRC); f = fopen(path,"r"); } } if(f == NULL) { oc_log(LOGWARN,"Cannot find runtime .dodsrc configuration file"); } else { fclose(f); if(ocdebug > 1) fprintf(stderr, "DODS RC file: %s\n", path); if(ocdodsrc_read(path) == 0) oc_log(LOGERR, "Error parsing %s\n",path); } if(path != NULL) {free(path) ; path = NULL;} } return THROW(stat); }
static int occompileprim(OCstate* state, OCnode* xnode, OCmemdata** memdatap, XDR* xdrs) { unsigned int xdrcount,i; size_t nelements = 0; OCerror ocstat = OC_NOERR; OCmemdata* memdata = NULL; OCASSERT((xnode->octype == OC_Primitive)); /* Use the count from the datadds */ nelements = totaldimsize(xnode); if(xnode->array.rank > 0) { /* Get first copy of the dimension count */ if(!xdr_u_int(xdrs,&xdrcount)) {ocstat = OC_EXDR; goto fail;} if(xdrcount != nelements) {ocstat=OC_EINVALCOORDS; goto fail;} if(xnode->etype != OC_String && xnode->etype != OC_URL) { /* Get second copy of the dimension count */ if(!xdr_u_int(xdrs,&xdrcount)) {ocstat = OC_EXDR; goto fail;} if(xdrcount != nelements) {ocstat=OC_EINVALCOORDS; goto fail;} } } else { nelements = 1; xdrcount = 1; } memdata = makememdata(xnode->octype,xnode->etype,nelements); MEMCHECK(memdata,OC_ENOMEM); memdata->mode = (xnode->array.rank > 0?Dimmode:Datamode); switch (xnode->etype) { case OC_String: case OC_URL: {/* Get the array of strings and store pointers in buf */ char** dst = (char**)memdata->data.data; for(i=0;i<xdrcount;i++) { char* s = NULL; if(!xdr_string(xdrs,(void*)&s,OC_INT32_MAX)) {ocstat = OC_EXDR; goto fail;} dst[i] = s; } } break; case OC_Byte: case OC_UByte: case OC_Char: { if(xnode->array.rank == 0) { /* Single unpacked character/byte */ union {unsigned int i; char c[BYTES_PER_XDR_UNIT];} u; if(!xdr_opaque(xdrs,u.c,BYTES_PER_XDR_UNIT)) {ocstat = OC_EXDR; goto fail;} u.i = ocntoh(u.i); memdata->data.data[0] = (char)u.i; } else { /* Packed characters; count will have already been read */ char* dst = memdata->data.data; if(!xdr_opaque(xdrs,dst,xdrcount)) {ocstat = OC_EXDR; goto fail;} } } break; case OC_Int16: case OC_UInt16: { unsigned short* dst = (unsigned short*)memdata->data.data; unsigned int* src; size_t xdrsize = xdrcount*BYTES_PER_XDR_UNIT; src = (unsigned int*)ocmalloc(xdrsize); if(!xdr_opaque(xdrs,(char*)src,xdrsize)) {ocfree(src); ocstat = OC_EXDR; goto fail;} if(!oc_network_order) { for(i=0;i<xdrcount;i++) { /* Write in place */ unsigned int hostint = src[i]; swapinline(dst[i],hostint); } } ocfree(src); } break; case OC_Int32: case OC_UInt32: case OC_Float32: { unsigned int* dst = (unsigned int*)memdata->data.data; size_t xdrsize = xdrcount*BYTES_PER_XDR_UNIT; if(!xdr_opaque(xdrs,(char*)dst,xdrsize)) {ocstat = OC_EXDR; goto fail;} if(!oc_network_order) { for(i=0;i<xdrcount;i++) { unsigned int hostint = dst[i]; swapinline(dst[i],hostint); } } } break; case OC_Int64: case OC_UInt64: case OC_Float64: { unsigned int* dst = (unsigned int*)memdata->data.data; size_t xdrsize = 2*xdrcount*BYTES_PER_XDR_UNIT; if(!xdr_opaque(xdrs,(char*)dst,xdrsize)) {ocstat = OC_EXDR; goto fail;} if(!oc_network_order) { for(i=0;i<2*xdrcount;i++) { unsigned int hostint = dst[i]; swapinline(dst[i],hostint); } } if(oc_invert_xdr_double) { /* May need to invert each pair */ for(i=0;i<2*xdrcount;i+=2) { unsigned int tmp = dst[i]; dst[i] = dst[i+1]; dst[i+1] = tmp; } } } break; default: OCPANIC1("unexpected etype: %d",xnode->etype); } /* switch */ /*ok:*/ if(memdatap) *memdatap = memdata; return THROW(ocstat); fail: freeocmemdata(memdata); return THROW(ocstat); }
void ocdumpfile(FILE* file, int datastart) { int i,count,rem,len; long pos; char dds[4096]; char hdr[1024]; struct stat stats; unsigned int imemory; unsigned int imemory1; pos = ftell(file); fseek(file,0,SEEK_SET); fstat(fileno(file),&stats); len = stats.st_size; fprintf(stderr,"\nlength=%d datastart=%d\n",len,datastart); if(datastart > 0) { fread(dds,1,datastart,file); dds[datastart] = '\0'; fprintf(stderr,"DDS:\n"); fprintf(stderr,"====================\n"); fprintf(stderr,"%s\n",dds); } else { fprintf(stderr,"DDS: none specified\n"); } fprintf(stderr,"====================\n"); /* build the header*/ hdr[0] = '\0'; dumpmem2("offset",hdr,5); dumpmem2("hex",hdr,8); dumpmem2("uint",hdr,12); dumpmem2("int",hdr,12); dumpmem2("float",hdr,12); dumpmem2("char[4]",hdr,16); dumpmem2("double",hdr,12); strcat(hdr,"\n"); fprintf(stderr,"%s",hdr); len -= datastart; count = (len / sizeof(unsigned int)); rem = (len % sizeof(unsigned int)); for(i=0;i<count;i++) { long pos; fread(&imemory,sizeof(unsigned int),1,file); pos = ftell(file); fread(&imemory1,sizeof(unsigned int),1,file); fseek(file,pos,SEEK_SET); imemory = ocntoh(imemory); imemory1 = ocntoh(imemory1); dumpmem1(i*4+datastart,imemory,imemory1); } if(rem > 0) { fprintf(stderr,">>>>remainder=%d\n",rem); } fflush(stderr); fseek(file,pos,SEEK_SET); /* leave it as we found it*/ }