void occlose(OCstate* state) { unsigned int i; if(state == NULL) return; /* Warning: ocfreeroot will attempt to remove the root from state->trees */ /* Ok in this case because we are popping the root out of state->trees */ for(i=0;i<oclistlength(state->trees);i++) { OCnode* root = (OCnode*)oclistpop(state->trees); ocfreeroot(root); } oclistfree(state->trees); dapurlclear(&state->url); ocbytesfree(state->packet); ocfree(state->error.code); ocfree(state->error.message); if(state->contentlist != NULL) { struct OCcontent* next; struct OCcontent* curr = state->contentlist; while(curr != NULL) { next = curr->next; ocfree(curr); curr = next; } } if(state->curl != NULL) occurlclose(state->curl); if(state->clientparams != NULL) ocparamfree(state->clientparams); ocfree(state); }
/* Convenience function */ void oc_attr_reclaim(OCtype etype, unsigned int nvalues, void* values) { if(nvalues == 0 || values == NULL) return; if(etype == OC_String || etype == OC_URL) { unsigned int i; char** strings = (char**)values; for(i=0;i<nvalues;i++) {ocfree(strings[i]);} } ocfree(values); }
OCerror ocopen(OCstate** statep, const char* url) { int stat = OC_NOERR; OCstate * state = NULL; OCURI* tmpurl = NULL; CURL* curl = NULL; /* curl handle*/ if(!ocuriparse(url,&tmpurl)) {OCTHROWCHK(stat=OC_EBADURL); goto fail;} stat = occurlopen(&curl); if(stat != OC_NOERR) {OCTHROWCHK(stat); goto fail;} state = (OCstate*)ocmalloc(sizeof(OCstate)); /* ocmalloc zeros memory*/ if(state == NULL) {OCTHROWCHK(stat=OC_ENOMEM); goto fail;} /* Setup DAP state*/ state->header.magic = OCMAGIC; state->header.occlass = OC_State; state->curl = curl; state->trees = oclistnew(); state->uri = tmpurl; if(!ocuridecodeparams(state->uri)) { oclog(OCLOGWARN,"Could not parse client parameters"); } state->packet = ocbytesnew(); ocbytessetalloc(state->packet,DFALTPACKETSIZE); /*initial reasonable size*/ /* capture curl properties for this link from rc file1*/ stat = ocset_curlproperties(state); if(stat != OC_NOERR) goto fail; /* Set the one-time curl flags */ if((stat=ocset_flags_perlink(state))!= OC_NOERR) goto fail; #if 1 /* temporarily make per-link */ if((stat=ocset_flags_perfetch(state))!= OC_NOERR) goto fail; #endif if(statep) *statep = state; else { if(state != NULL) ocfree(state); } return OCTHROW(stat); fail: ocurifree(tmpurl); if(state != NULL) ocfree(state); if(curl != NULL) occurlclose(curl); return OCTHROW(stat); }
void ocfreenodes(OClist* nodes) { unsigned int i,j; for(i=0;i<oclistlength(nodes);i++) { OCnode* node = (OCnode*)oclistget(nodes,i); ocfree(node->name); ocfree(node->fullname); while(oclistlength(node->att.values) > 0) { char* value = (char*)oclistpop(node->att.values); ocfree(value); } while(oclistlength(node->attributes) > 0) { OCattribute* attr = (OCattribute*)oclistpop(node->attributes); ocfree(attr->name); /* If the attribute type is string, then we need to free them*/ if(attr->etype == OC_String || attr->etype == OC_URL) { char** strings = (char**)attr->values; for(j=0;j<attr->nvalues;j++) {ocfree(*strings); strings++;} } ocfree(attr->values); ocfree(attr); } if(node->array.dimensions != NULL) oclistfree(node->array.dimensions); if(node->subnodes != NULL) oclistfree(node->subnodes); if(node->att.values != NULL) oclistfree(node->att.values); if(node->attributes != NULL) oclistfree(node->attributes); ocfree(node); } oclistfree(nodes); }
void freeocmemdata(OCmemdata* md) { char** sp; OCmemdata** mdp; unsigned int i; if(md == NULL) return; switch ((OCtype)md->octype) { case OC_Primitive: switch ((OCtype)md->etype) { case OC_Char: case OC_Byte: case OC_UByte: case OC_Int16: case OC_UInt16: case OC_Int32: case OC_UInt32: case OC_Int64: case OC_UInt64: case OC_Float32: case OC_Float64: break; case OC_String: case OC_URL: sp = (char**)md->data.data; for(i=0;i<md->count;i++) ocfree(sp[i]); break; default: break; } break; case OC_Sequence: case OC_Grid: case OC_Structure: mdp = (OCmemdata**)md->data.data; for(i=0;i<md->count;i++) freeocmemdata(mdp[i]); break; default: break; } ocfree(md); }
void daplexcleanup(DAPlexstate** lexstatep) { DAPlexstate* lexstate = *lexstatep; if(lexstate == NULL) return; if(lexstate->input != NULL) ocfree(lexstate->input); if(lexstate->reclaim != NULL) { while(oclistlength(lexstate->reclaim) > 0) { char* word = (char*)oclistpop(lexstate->reclaim); if(word) free(word); } oclistfree(lexstate->reclaim); } ocbytesfree(lexstate->yytext); free(lexstate); *lexstatep = NULL; }
OCerror ocopen(OCstate** statep, const char* url) { int stat = OC_NOERR; OCstate * state = NULL; DAPURL tmpurl; CURL* curl = NULL; /* curl handle*/ memset((void*)&tmpurl,0,sizeof(tmpurl)); if(!dapurlparse(url,&tmpurl)) {THROWCHK(stat=OC_EBADURL); goto fail;} stat = occurlopen(&curl); if(stat != OC_NOERR) {THROWCHK(stat); goto fail;} state = (OCstate*)ocmalloc(sizeof(OCstate)); /* ocmalloc zeros memory*/ if(state == NULL) {THROWCHK(stat=OC_ENOMEM); goto fail;} /* Setup DAP state*/ state->magic = OCMAGIC; state->curl = curl; state->trees = oclistnew(); state->contentlist = NULL; state->url = tmpurl; state->clientparams = ocparamdecode(state->url.params); if(state->clientparams == NULL) { oc_log(LOGWARN,"Could not parse client parameters"); } state->packet = ocbytesnew(); ocbytessetalloc(state->packet,DFALTPACKETSIZE); /*initial reasonable size*/ /* set curl properties for this link */ ocsetcurlproperties(state); if(statep) *statep = state; return THROW(stat); fail: dapurlclear(&tmpurl); if(state != NULL) ocfree(state); if(curl != NULL) occurlclose(curl); return THROW(stat); }
OCerror ocopen(OCstate** statep, const char* url) { int stat = OC_NOERR; OCstate * state = NULL; OCURI* tmpurl = NULL; CURL* curl = NULL; /* curl handle*/ if(!ocuriparse(url,&tmpurl)) {OCTHROWCHK(stat=OC_EBADURL); goto fail;} stat = occurlopen(&curl); if(stat != OC_NOERR) {OCTHROWCHK(stat); goto fail;} state = (OCstate*)ocmalloc(sizeof(OCstate)); /* ocmalloc zeros memory*/ if(state == NULL) {OCTHROWCHK(stat=OC_ENOMEM); goto fail;} /* Setup DAP state*/ state->magic = OCMAGIC; state->curl = curl; state->trees = oclistnew(); state->uri = tmpurl; if(!ocuridecodeparams(state->uri)) { oc_log(LOGWARN,"Could not parse client parameters"); } state->packet = ocbytesnew(); ocbytessetalloc(state->packet,DFALTPACKETSIZE); /*initial reasonable size*/ /* set curl properties for this link */ ocsetcurlproperties(state); /* Set up list to support reuse/reclamation of OCcontent objects. */ state->contentlist = NULL; if(statep) *statep = state; return OCTHROW(stat); fail: ocurifree(tmpurl); if(state != NULL) ocfree(state); if(curl != NULL) occurlclose(curl); return OCTHROW(stat); }
void dap_parse_error(DAPparsestate* state, const char *fmt, ...) { size_t len, suffixlen, prefixlen; va_list argv; char* tmp = NULL; va_start(argv,fmt); (void) vfprintf(stderr,fmt,argv) ; (void) fputc('\n',stderr) ; len = strlen(state->lexstate->input); suffixlen = strlen(state->lexstate->next); prefixlen = (len - suffixlen); tmp = (char*)ocmalloc(len+1); flatten(state->lexstate->input,tmp,prefixlen); (void) fprintf(stderr,"context: %s",tmp); flatten(state->lexstate->next,tmp,suffixlen); (void) fprintf(stderr,"^%s\n",tmp); (void) fflush(stderr); /* to ensure log files are current */ ocfree(tmp); va_end(argv); }
void ocfreetree(OCtree* tree) { if(tree == NULL) return; ocfreenodes(tree->nodes); ocfree(tree->constraint); ocfree(tree->text); if(tree->data.xdrs != NULL) { xdr_destroy(tree->data.xdrs); ocfree(tree->data.xdrs); } #ifdef OC_DISK_STORAGE if(tree->dxdclass == OCDATA) { ocfree(tree->data.filename); if(tree->data.file != NULL) fclose(tree->data.file); } #else ocfree(tree->data.xdrdata); #endif freeocmemdata(tree->data.memdata); ocfree(tree); }
void occlose(OCstate* state) { unsigned int i; if(state == NULL) return; /* Warning: ocfreeroot will attempt to remove the root from state->trees */ /* Ok in this case because we are popping the root out of state->trees */ for(i=0;i<oclistlength(state->trees);i++) { OCnode* root = (OCnode*)oclistpop(state->trees); ocfreeroot(root); } oclistfree(state->trees); ocurifree(state->uri); ocbytesfree(state->packet); ocfree(state->error.code); ocfree(state->error.message); if(state->contentlist != NULL) { struct OCcontent* next; struct OCcontent* curr = state->contentlist; while(curr != NULL) { next = curr->next; ocfree(curr); curr = next; } } ocfree(state->curlflags.useragent); ocfree(state->curlflags.cookiejar); ocfree(state->curlflags.cookiefile); ocfree(state->ssl.certificate); ocfree(state->ssl.key); ocfree(state->ssl.keypasswd); ocfree(state->ssl.cainfo); ocfree(state->ssl.capath); ocfree(state->proxy.host); ocfree(state->creds.username); ocfree(state->creds.password); if(state->curl != NULL) occurlclose(state->curl); ocfree(state); }
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 occlose(OCstate* state) { unsigned int i; if(state == NULL) return; /* Warning: ocfreeroot will attempt to remove the root from state->trees */ /* Ok in this case because we are popping the root out of state->trees */ for(i=0;i<oclistlength(state->trees);i++) { OCnode* root = (OCnode*)oclistpop(state->trees); ocroot_free(root); } oclistfree(state->trees); ocurifree(state->uri); ocbytesfree(state->packet); ocfree(state->error.code); ocfree(state->error.message); ocfree(state->curlflags.useragent); if(state->curlflags.cookiejar) { #if 0 if(state->curlflags.createdflags & COOKIECREATED) unlink(state->curlflags.cookiejar); #endif ocfree(state->curlflags.cookiejar); } if(state->curlflags.netrc != NULL) { #if 0 if(state->curlflags.createdflags & NETRCCREATED) unlink(state->curlflags.netrc); #endif ocfree(state->curlflags.netrc); } ocfree(state->ssl.certificate); ocfree(state->ssl.key); ocfree(state->ssl.keypasswd); ocfree(state->ssl.cainfo); ocfree(state->ssl.capath); ocfree(state->proxy.host); ocfree(state->proxy.userpwd); ocfree(state->creds.userpwd); if(state->curl != NULL) occurlclose(state->curl); ocfree(state); }
/* Extract data from the xdr packet into a chunk of memory. Normally, it is assumed that we are (at least virtually) "at" a single instance in the xdr packet; which we read. Virtually because for packed data, we need to point to the beginning of the packed data and use the index to indicate which packed element to get. */ int ocxdrread(XDR* xdrs, char* memory, size_t memsize, int packed, OCtype octype, unsigned int start, size_t count) { int stat = OC_NOERR; unsigned int i; size_t elemsize = octypesize(octype); char* localmem = NULL; char* startmem = NULL; size_t totalsize; size_t xdrsize; unsigned int xdrckp = xdr_getpos(xdrs); /* validate memory space*/ totalsize = elemsize*count; if(memsize < totalsize) return THROW(OC_EINVAL); /* Handle packed data specially*/ /* WARNING: assumes that the initial count has been read*/ if(packed) { char tmp[LOCALMEMMAX]; unsigned int readsize = start+count; if(readsize <= LOCALMEMMAX) /* avoid malloc/free for common case*/ localmem = tmp; else { localmem = (char*)ocmalloc(readsize); MEMCHECK(localmem,OC_ENOMEM); } if(!xdr_opaque(xdrs,(char*)localmem,readsize)) return xdrerror(); memcpy((void*)memory,(void*)(localmem+start),count); if(readsize > LOCALMEMMAX) ocfree(localmem); if(!xdr_setpos(xdrs,xdrckp)) return xdrerror(); /* revert to beginning*/ return THROW(OC_NOERR); } /* Not packed: extract count items; use xdr_opaque to speed up*/ if(octype == OC_String || octype == OC_URL) { /* do nothing here; handle below*/ } else if(octype == OC_Float64 || octype == OC_UInt64 || octype == OC_Int64) { unsigned int* p; xdrsize = 2*(start+count)*BYTES_PER_XDR_UNIT; localmem = (char*)ocmalloc(xdrsize); startmem = localmem+(2*start*BYTES_PER_XDR_UNIT); MEMCHECK(localmem,OC_ENOMEM); if(!xdr_opaque(xdrs,(char*)localmem,xdrsize)) return xdrerror(); if(!oc_network_order) { for(p=(unsigned int*)startmem,i=0;i<2*count;i++,p++) { unsigned int swap = *p; swapinline(*p,swap); } } } else { unsigned int* p; xdrsize = (start+count)*BYTES_PER_XDR_UNIT; localmem = (char*)ocmalloc(xdrsize); MEMCHECK(localmem,OC_ENOMEM); startmem = localmem+(start*BYTES_PER_XDR_UNIT); if(!xdr_opaque(xdrs,(char*)localmem,xdrsize)) return xdrerror(); if(!oc_network_order) { for(p=(unsigned int*)startmem,i=0;i<count;i++,p++) { unsigned int swap = *p; swapinline(*p,swap); } } } switch (octype) { case OC_Char: case OC_Byte: case OC_UByte: { char* pmem = (char*)memory; unsigned int* p = (unsigned int*)startmem; for(i=0;i<count;i++) {*pmem++ = (char)(*p++);} } break; case OC_Int16: case OC_UInt16: { unsigned short* pmem = (unsigned short*)memory; unsigned int* p = (unsigned int*)startmem; for(i=0;i<count;i++) {*pmem++ = (unsigned short)(*p++);} } break; case OC_Int32: case OC_UInt32: { memcpy((void*)memory,(void*)startmem,count*sizeof(unsigned int)); } break; case OC_Float32: { memcpy((void*)memory,(void*)startmem,count*sizeof(float)); } break; case OC_Int64: case OC_UInt64: case OC_Float64: { unsigned int* p; unsigned int* pmem = (unsigned int*)memory; /* Sometimes need to invert order*/ for(p=(unsigned int*)startmem,i=0;i<count;i++) { if(oc_invert_xdr_double) { pmem[1] = (unsigned int)(*p++); pmem[0] = (unsigned int)(*p++); } else { pmem[0] = (unsigned int)(*p++); pmem[1] = (unsigned int)(*p++); } pmem += 2; } } break; case OC_String: case OC_URL: { char* s = NULL; char** pmem = (char**)memory; /* First skip to the starting string */ for(i=0;i<start;i++) { s = NULL; /* make xdr_string alloc the space */ if(!xdr_string(xdrs,&s,OC_INT32_MAX)) return xdrerror(); ocfree(s); } /* Read count strings */ for(i=0;i<count;i++) { s = NULL; /* make xdr_string alloc the space */ if(!xdr_string(xdrs,&s,OC_INT32_MAX)) return xdrerror(); pmem[i] = s; } } break; default: return THROW(OC_EINVAL); } ocfree(localmem); if(!xdr_setpos(xdrs,xdrckp)) return xdrerror(); /* revert to beginning*/ return THROW(stat); }