ocutil.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
  2. See the COPYRIGHT file for more information. */
  3. #include "config.h"
  4. #ifdef HAVE_UNISTD_H
  5. #include <unistd.h>
  6. #endif
  7. #include <fcntl.h>
  8. #include "ocinternal.h"
  9. #include "ocdebug.h"
  10. #ifdef WIN32
  11. #define snprintf _snprintf
  12. #endif
  13. /* Order is important: longest first */
  14. static char* DDSdatamarks[3] = {"Data:\r\n","Data:\n",(char*)NULL};
  15. /* Not all systems have strndup, so provide one*/
  16. char*
  17. ocstrndup(const char* s, size_t len)
  18. {
  19. char* dup;
  20. if(s == NULL) return NULL;
  21. dup = (char*)ocmalloc(len+1);
  22. MEMCHECK(dup,NULL);
  23. memcpy((void*)dup,s,len);
  24. dup[len] = '\0';
  25. return dup;
  26. }
  27. /* Do not trust strncmp semantics */
  28. int
  29. ocstrncmp(const char* s1, const char* s2, size_t len)
  30. {
  31. const char *p,*q;
  32. if(s1 == s2) return 0;
  33. if(s1 == NULL) return -1;
  34. if(s2 == NULL) return +1;
  35. for(p=s1,q=s2;len > 0;p++,q++,len--) {
  36. if(*p == 0 && *q == 0) return 0; /* *p == *q == 0 */
  37. if(*p != *q)
  38. return (*p - *q);
  39. }
  40. /* 1st len chars are same */
  41. return 0;
  42. }
  43. void
  44. makedimlist(OClist* path, OClist* dims)
  45. {
  46. unsigned int i,j;
  47. for(i=0;i<oclistlength(path);i++) {
  48. OCnode* node = (OCnode*)oclistget(path,i);
  49. unsigned int rank = node->array.rank;
  50. for(j=0;j<rank;j++) {
  51. OCnode* dim = (OCnode*)oclistget(node->array.dimensions,j);
  52. oclistpush(dims,(ocelem)dim);
  53. }
  54. }
  55. }
  56. void
  57. ocfreeprojectionclause(OCprojectionclause* clause)
  58. {
  59. if(clause->target != NULL) free(clause->target);
  60. while(oclistlength(clause->indexsets) > 0) {
  61. OClist* slices = (OClist*)oclistpop(clause->indexsets);
  62. while(oclistlength(slices) > 0) {
  63. OCslice* slice = (OCslice*)oclistpop(slices);
  64. if(slice != NULL) free(slice);
  65. }
  66. oclistfree(slices);
  67. }
  68. oclistfree(clause->indexsets);
  69. free(clause);
  70. }
  71. static void
  72. freeAttributes(OClist* attset)
  73. {
  74. unsigned int i,j;
  75. for(i=0;i<oclistlength(attset);i++) {
  76. OCattribute* att = (OCattribute*)oclistget(attset,i);
  77. if(att->name != NULL) free(att->name);
  78. if(att->etype == OC_String || att->etype == OC_URL) {
  79. for(j=0;j<att->nvalues;j++) {
  80. char* s = ((char**)att->values)[j];
  81. if(s != NULL) free(s);
  82. }
  83. } else {
  84. free(att->values);
  85. }
  86. }
  87. }
  88. void
  89. freeOCnode(OCnode* cdf, int deep)
  90. {
  91. unsigned int i;
  92. if(cdf == NULL) return;
  93. if(cdf->name != NULL) free(cdf->name);
  94. if(cdf->fullname != NULL) free(cdf->fullname);
  95. if(cdf->attributes != NULL) freeAttributes(cdf->attributes);
  96. if(cdf->subnodes != NULL) {
  97. if(deep) {
  98. for(i=0;i<oclistlength(cdf->subnodes);i++) {
  99. OCnode* node = (OCnode*)oclistget(cdf->subnodes,i);
  100. freeOCnode(node,deep);
  101. }
  102. }
  103. oclistfree(cdf->subnodes);
  104. }
  105. free(cdf);
  106. }
  107. int
  108. findbod(OCbytes* buffer, size_t* bodp, size_t* ddslenp)
  109. {
  110. unsigned int i;
  111. char* content;
  112. size_t len = ocbyteslength(buffer);
  113. char** marks;
  114. content = ocbytescontents(buffer);
  115. for(marks = DDSdatamarks;*marks;marks++) {
  116. char* mark = *marks;
  117. int tlen = strlen(mark);
  118. for(i=0;i<len;i++) {
  119. if((i+tlen) <= len
  120. && (ocstrncmp(content+i,mark,tlen)==0)) {
  121. *ddslenp = i;
  122. i += tlen;
  123. *bodp = i;
  124. return 1;
  125. }
  126. }
  127. }
  128. *ddslenp = 0;
  129. *bodp = 0;
  130. return 0; /* tag not found; not necessarily an error*/
  131. }
  132. /* Compute total # of elements if dimensioned*/
  133. size_t
  134. totaldimsize(OCnode* node)
  135. {
  136. unsigned int i;
  137. size_t count = 1;
  138. for(i=0;i<node->array.rank;i++) {
  139. OCnode* dim = (OCnode*)oclistget(node->array.dimensions,i);
  140. count *= (dim->dim.declsize);
  141. }
  142. return count;
  143. }
  144. #ifdef OCIGNORE
  145. size_t
  146. totaldimsize(unsigned int rank, size_t* dimsizes)
  147. {
  148. unsigned int i;
  149. int unlim = 0;
  150. unsigned long size = 1;
  151. for(i=0;i<rank;i++) {
  152. if(dimsizes[i] != 0) size = (size * dimsizes[i]); else unlim = 1;
  153. }
  154. return size;
  155. }
  156. #endif
  157. size_t
  158. octypesize(OCtype etype)
  159. {
  160. switch (etype) {
  161. case OC_Char: return sizeof(char);
  162. case OC_Byte: return sizeof(signed char);
  163. case OC_UByte: return sizeof(unsigned char);
  164. case OC_Int16: return sizeof(short);
  165. case OC_UInt16: return sizeof(unsigned short);
  166. case OC_Int32: return sizeof(int);
  167. case OC_UInt32: return sizeof(unsigned int);
  168. case OC_Float32: return sizeof(float);
  169. case OC_Float64: return sizeof(double);
  170. #ifdef HAVE_LONG_LONG_INT
  171. case OC_Int64: return sizeof(long long);
  172. case OC_UInt64: return sizeof(unsigned long long);
  173. #endif
  174. case OC_String: return sizeof(char*);
  175. case OC_URL: return sizeof(char*);
  176. default: break; /* Ignore all others */
  177. }
  178. return 0;
  179. }
  180. char*
  181. octypetostring(OCtype octype)
  182. {
  183. switch (octype) {
  184. case OC_NAT: return "OC_NAT";
  185. case OC_Char: return "OC_Char";
  186. case OC_Byte: return "OC_Byte";
  187. case OC_UByte: return "OC_UByte";
  188. case OC_Int16: return "OC_Int16";
  189. case OC_UInt16: return "OC_UInt16";
  190. case OC_Int32: return "OC_Int32";
  191. case OC_UInt32: return "OC_UInt32";
  192. case OC_Int64: return "OC_Int64";
  193. case OC_UInt64: return "OC_UInt64";
  194. case OC_Float32: return "OC_Float32";
  195. case OC_Float64: return "OC_Float64";
  196. case OC_String: return "OC_String";
  197. case OC_URL: return "OC_URL";
  198. /* Non-primitives*/
  199. case OC_Dataset: return "OC_Dataset";
  200. case OC_Sequence: return "OC_Sequence";
  201. case OC_Grid: return "OC_Grid";
  202. case OC_Structure: return "OC_Structure";
  203. case OC_Dimension: return "OC_Dimension";
  204. case OC_Attribute: return "OC_Attribute";
  205. case OC_Attributeset: return "OC_Attributeset";
  206. case OC_Primitive: return "OC_Primitive";
  207. default: break;
  208. }
  209. return NULL;
  210. }
  211. char*
  212. octypetoddsstring(OCtype octype)
  213. {
  214. switch (octype) {
  215. case OC_Byte: return "Byte";
  216. case OC_Int16: return "Int16";
  217. case OC_UInt16: return "UInt16";
  218. case OC_Int32: return "Int32";
  219. case OC_UInt32: return "UInt32";
  220. case OC_Float32: return "Float32";
  221. case OC_Float64: return "Float64";
  222. case OC_String: return "String";
  223. case OC_URL: return "Url";
  224. default: break;
  225. }
  226. return "<unknown>";
  227. }
  228. OCerror
  229. octypeprint(OCtype etype, char* buf, size_t bufsize, void* value)
  230. {
  231. if(buf == NULL || bufsize == 0 || value == NULL) return OC_EINVAL;
  232. buf[0] = '\0';
  233. switch (etype) {
  234. case OC_Char:
  235. snprintf(buf,bufsize,"'%c'",*(char*)value);
  236. break;
  237. case OC_Byte:
  238. snprintf(buf,bufsize,"%d",*(signed char*)value);
  239. break;
  240. case OC_UByte:
  241. snprintf(buf,bufsize,"%u",*(unsigned char*)value);
  242. break;
  243. case OC_Int16:
  244. snprintf(buf,bufsize,"%d",*(short*)value);
  245. break;
  246. case OC_UInt16:
  247. snprintf(buf,bufsize,"%u",*(unsigned short*)value);
  248. break;
  249. case OC_Int32:
  250. snprintf(buf,bufsize,"%d",*(int*)value);
  251. break;
  252. case OC_UInt32:
  253. snprintf(buf,bufsize,"%u",*(unsigned int*)value);
  254. break;
  255. case OC_Float32:
  256. snprintf(buf,bufsize,"%g",*(float*)value);
  257. break;
  258. case OC_Float64:
  259. snprintf(buf,bufsize,"%g",*(double*)value);
  260. break;
  261. #ifdef HAVE_LONG_LONG_INT
  262. case OC_Int64:
  263. snprintf(buf,bufsize,"%lld",*(long long*)value);
  264. break;
  265. case OC_UInt64:
  266. snprintf(buf,bufsize,"%llu",*(unsigned long long*)value);
  267. break;
  268. #endif
  269. case OC_String:
  270. case OC_URL: {
  271. char* s = *(char**)value;
  272. snprintf(buf,bufsize,"\"%s\"",s);
  273. } break;
  274. default: break;
  275. }
  276. return OC_NOERR;
  277. }
  278. size_t
  279. xxdrsize(OCtype etype)
  280. {
  281. switch (etype) {
  282. case OC_Char:
  283. case OC_Byte:
  284. case OC_UByte:
  285. case OC_Int16:
  286. case OC_UInt16:
  287. case OC_Int32:
  288. case OC_UInt32:
  289. return XDRUNIT;
  290. case OC_Int64:
  291. case OC_UInt64:
  292. return (2*XDRUNIT);
  293. case OC_Float32:
  294. return XDRUNIT;
  295. case OC_Float64:
  296. return (2*XDRUNIT);
  297. case OC_String:
  298. case OC_URL:
  299. default: break;
  300. }
  301. return 0;
  302. }
  303. /**************************************/
  304. char*
  305. ocerrstring(int err)
  306. {
  307. if(err == 0) return "no error";
  308. if(err > 0) return strerror(err);
  309. switch (err) {
  310. case OC_EBADID:
  311. return "OC_EBADID: Not a valid ID";
  312. case OC_EINVAL:
  313. return "OC_EINVAL: Invalid argument";
  314. case OC_EPERM:
  315. return "OC_EPERM: Write to read only";
  316. case OC_EINVALCOORDS:
  317. return "OC_EINVALCOORDS: Index exceeds dimension bound";
  318. case OC_ENOTVAR:
  319. return "OC_ENOTVAR: Variable not found";
  320. case OC_ECHAR:
  321. return "OC_ECHAR: Attempt to convert between text & numbers";
  322. case OC_EEDGE:
  323. return "OC_EEDGE: Start+count exceeds dimension bound";
  324. case OC_ESTRIDE:
  325. return "OC_ESTRIDE: Illegal stride";
  326. case OC_ENOMEM:
  327. return "OC_ENOMEM: Memory allocation (malloc) failure";
  328. case OC_EDIMSIZE:
  329. return "OC_EDIMSIZE: Invalid dimension size";
  330. case OC_EDAP:
  331. return "OC_EDAP: DAP failure";
  332. case OC_EXDR:
  333. return "OC_EXDR: XDR failure";
  334. case OC_ECURL:
  335. return "OC_ECURL: libcurl failure";
  336. case OC_EBADURL:
  337. return "OC_EBADURL: malformed url";
  338. case OC_EBADVAR:
  339. return "OC_EBADVAR: no such variable";
  340. case OC_EOPEN:
  341. return "OC_EOPEN: temporary file open failed";
  342. case OC_EIO:
  343. return "OC_EIO: I/O failure";
  344. case OC_ENODATA:
  345. return "OC_ENODATA: Variable has no data in DAP request";
  346. case OC_EDAPSVC:
  347. return "OC_EDAPSVC: DAP Server error";
  348. case OC_ENAMEINUSE:
  349. return "OC_ENAMEINUSE: Duplicate name in DDS";
  350. case OC_EDAS:
  351. return "OC_EDAS: Malformed or unreadable DAS";
  352. case OC_EDDS:
  353. return "OC_EDDS: Malformed or unreadable DDS";
  354. case OC_EDATADDS:
  355. return "OC_EDATADDS: Malformed or unreadable DATADDS";
  356. case OC_ERCFILE:
  357. return "OC_ERCFILE: Malformed or unreadable run-time configuration file";
  358. case OC_ENOFILE:
  359. return "OC_ENOFILE: cannot read content of URL";
  360. default: break;
  361. }
  362. return "<unknown error code>";
  363. }
  364. OCerror
  365. ocsvcerrordata(OCstate* state, char** codep, char** msgp, long* httpp)
  366. {
  367. if(codep) *codep = state->error.code;
  368. if(msgp) *msgp = state->error.message;
  369. if(httpp) *httpp = state->error.httpcode;
  370. return OC_NOERR;
  371. }
  372. /* if we get OC_EDATADDS error, then try to capture any
  373. error message and log it; assumes that in this case,
  374. the datadds is not big.
  375. */
  376. void
  377. ocdataddsmsg(OCstate* state, OCtree* tree)
  378. {
  379. #define ERRCHUNK 1024
  380. #define ERRFILL ' '
  381. #define ERRTAG "Error {"
  382. unsigned int i,j,len;
  383. XXDR* xdrs;
  384. char* contents;
  385. off_t ckp;
  386. if(tree == NULL) return;
  387. /* get available space */
  388. xdrs = tree->data.xdrs;
  389. len = xxdr_length(xdrs);
  390. if(len < strlen(ERRTAG))
  391. return; /* no room */
  392. ckp = xxdr_getpos(xdrs);
  393. xxdr_setpos(xdrs,0);
  394. /* read the whole thing */
  395. contents = (char*)malloc(len+1);
  396. xxdr_getbytes(xdrs,contents,len);
  397. contents[len] = '\0';
  398. /* Look for error tag */
  399. for(i=0;i<len;i++) {
  400. if(ocstrncmp(contents+i,ERRTAG,strlen(ERRTAG))==0) {
  401. /* log the error message */
  402. /* Do a quick and dirty escape */
  403. for(j=i;j<len;j++) {
  404. int c = contents[i+j];
  405. if(c > 0 && (c < ' ' || c >= '\177'))
  406. contents[i+j] = ERRFILL;
  407. }
  408. oc_log(LOGERR,"DATADDS failure, possible message: '%s'\n",
  409. contents+i);
  410. goto done;
  411. }
  412. }
  413. xxdr_setpos(xdrs,ckp);
  414. done:
  415. return;
  416. }