File: ocutil.c

package info (click to toggle)
netcdf 1:4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 104,952 kB
  • sloc: ansic: 228,683; sh: 10,980; yacc: 2,561; makefile: 1,319; lex: 1,173; xml: 173; awk: 2
file content (708 lines) | stat: -rw-r--r-- 17,502 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc.
   See the COPYRIGHT file for more information. */

#include "config.h"
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#ifdef _MSC_VER
#include <io.h>
#define mode_t int
#endif

#include "ocinternal.h"
#include "ocdebug.h"

/* Order is important: longest first */
static const char* DDSdatamarks[3] = {"Data:\r\n","Data:\n",(char*)NULL};

/* Not all systems have strndup, so provide one*/
char*
ocstrndup(const char* s, size_t len)
{
    char* dup;
    if(s == NULL) return NULL;
    dup = (char*)ocmalloc(len+1);
    MEMCHECK(dup,NULL);
    memcpy((void*)dup,s,len);
    dup[len] = '\0';
    return dup;
}

/* Do not trust strncmp semantics; this one
   compares up to len chars or to null terminators */
int
ocstrncmp(const char* s1, const char* s2, size_t len)
{
    const char *p,*q;
    if(s1 == s2) return 0;
    if(s1 == NULL) return -1;
    if(s2 == NULL) return +1;
    for(p=s1,q=s2;len > 0;p++,q++,len--) {
	if(*p == 0 && *q == 0) return 0; /* *p == *q == 0 */
	if(*p != *q)
	    return (*p - *q);	
    }
    /* 1st len chars are same */
    return 0;
}


#if 0
void
makedimlist(Nclist* path, Nclist* dims)
{
    unsigned int i,j;
    for(i=0;i<nclistlength(path);i++) {
	OCnode* node = (OCnode*)nclistget(path,i);
        unsigned int rank = node->array.rank;
	for(j=0;j<rank;j++) {
	    OCnode* dim = (OCnode*)nclistget(node->array.dimensions,j);
	    nclistpush(dims,(void*)dim);
        }
    }
}
#endif

void
ocfreeprojectionclause(OCprojectionclause* clause)
{
    if(clause->target != NULL) free(clause->target);
    while(nclistlength(clause->indexsets) > 0) {
	NClist* slices = (NClist*)nclistpop(clause->indexsets);
        while(nclistlength(slices) > 0) {
	    OCslice* slice = (OCslice*)nclistpop(slices);
	    if(slice != NULL) free(slice);
	}
        nclistfree(slices);
    }
    nclistfree(clause->indexsets);
    free(clause);
}

#if 0
void
freeAttributes(NClist* attset)
{
    unsigned int i,j;
    for(i=0;i<nclistlength(attset);i++) {
	OCattribute* att = (OCattribute*)nclistget(attset,i);
	if(att->name != NULL) free(att->name);
	if(att->etype == OC_String || att->etype == OC_URL) {
	    for(j=0;j<att->nvalues;j++) {
		char* s = ((char**)att->values)[j];
		if(s != NULL) free(s);
	    }
	} else {
	    free(att->values);
	}
    }
}
#endif

#if 0
void
freeOCnode(OCnode* cdf, int deep)
{
    unsigned int i;
    if(cdf == NULL) return;
    if(cdf->name != NULL) free(cdf->name);
    if(cdf->fullname != NULL) free(cdf->fullname);
    if(cdf->attributes != NULL) freeAttributes(cdf->attributes);
    if(cdf->subnodes != NULL) {
	if(deep) {
            for(i=0;i<nclistlength(cdf->subnodes);i++) {
	        OCnode* node = (OCnode*)nclistget(cdf->subnodes,i);
		freeOCnode(node,deep);
	    }
	}
        nclistfree(cdf->subnodes);
    }
    free(cdf);
}
#endif

int
ocfindbod(NCbytes* buffer, size_t* bodp, size_t* ddslenp)
{
    unsigned int i;
    char* content;
    size_t len = ncbyteslength(buffer);
    const char** marks;
    
    content = ncbytescontents(buffer);

    for(marks = DDSdatamarks;*marks;marks++) {
	const char* mark = *marks;
        size_t tlen = strlen(mark);
        for(i=0;i<len;i++) {
	    if((i+tlen) <= len 
	        && (ocstrncmp(content+i,mark,tlen)==0)) {
	       *ddslenp = i;
	        i += tlen;
	        *bodp = i;
	        return 1;
	    }
	}
    }
    *ddslenp = 0;
    *bodp = 0;
    return 0; /* tag not found; not necessarily an error*/
}

/* Compute total # of elements if dimensioned*/
size_t
octotaldimsize(size_t rank, size_t* sizes)
{
    unsigned int i;
    size_t count = 1;
    for(i=0;i<rank;i++) {
        count *= sizes[i];
    }
    return count;
}

size_t
octypesize(OCtype etype)
{
    switch (etype) {
    case OC_Char:	return sizeof(char);
    case OC_Byte:	return sizeof(signed char);
    case OC_UByte:	return sizeof(unsigned char);
    case OC_Int16:	return sizeof(short);
    case OC_UInt16:	return sizeof(unsigned short);
    case OC_Int32:	return sizeof(int);
    case OC_UInt32:	return sizeof(unsigned int);
    case OC_Float32:	return sizeof(float);
    case OC_Float64:	return sizeof(double);
#ifdef HAVE_LONG_LONG_INT
    case OC_Int64:	return sizeof(long long);
    case OC_UInt64:	return sizeof(unsigned long long);
#endif
    case OC_String:	return sizeof(char*);
    case OC_URL:	return sizeof(char*);
    default: break;     /* Ignore all others */
    }
    return 0;
}

char*
octypetostring(OCtype octype)
{
    switch (octype) {
    case OC_NAT:          return "OC_NAT";
    case OC_Char:         return "OC_Char";
    case OC_Byte:         return "OC_Byte";
    case OC_UByte:	   return "OC_UByte";
    case OC_Int16:        return "OC_Int16";
    case OC_UInt16:       return "OC_UInt16";
    case OC_Int32:        return "OC_Int32";
    case OC_UInt32:       return "OC_UInt32";
    case OC_Int64:        return "OC_Int64";
    case OC_UInt64:       return "OC_UInt64";
    case OC_Float32:      return "OC_Float32";
    case OC_Float64:      return "OC_Float64";
    case OC_String:       return "OC_String";
    case OC_URL:          return "OC_URL";
    /* Non-primitives*/
    case OC_Dataset:      return "OC_Dataset";
    case OC_Sequence:     return "OC_Sequence";
    case OC_Grid:         return "OC_Grid";
    case OC_Structure:    return "OC_Structure";
    case OC_Dimension:    return "OC_Dimension";
    case OC_Attribute:    return "OC_Attribute";
    case OC_Attributeset: return "OC_Attributeset";
    case OC_Atomic:       return "OC_Atomic";
    default: break;
    }
    return NULL;
}

char*
octypetoddsstring(OCtype octype)
{
    switch (octype) {
    case OC_Byte:         return "Byte";
    case OC_Int16:        return "Int16";
    case OC_UInt16:       return "UInt16";
    case OC_Int32:        return "Int32";
    case OC_UInt32:       return "UInt32";
    case OC_Float32:      return "Float32";
    case OC_Float64:      return "Float64";
    case OC_String:       return "String";
    case OC_URL:          return "Url";
    /* Non-atomics*/
    case OC_Dataset:      return "Dataset";
    case OC_Sequence:     return "Sequence";
    case OC_Grid:         return "Grid";
    case OC_Structure:    return "Structure";
    case OC_Dimension:    return "Dimension";
    case OC_Attribute:    return "Attribute";
    case OC_Attributeset: return "Attributeset";
    case OC_Atomic:       return "Atomic";
    default: break;
    }
    return "<unknown>";
}


OCerror
octypeprint(OCtype etype, void* value, size_t bufsize, char* buf)
{
    if(buf == NULL || bufsize == 0 || value == NULL) return OC_EINVAL;
    buf[0] = '\0';
    switch (etype) {
    case OC_Char:
	snprintf(buf,bufsize,"'%c'",*(char*)value);
	break;
    case OC_Byte:
	snprintf(buf,bufsize,"%d",*(signed char*)value);
	break;
    case OC_UByte:
	snprintf(buf,bufsize,"%u",*(unsigned char*)value);
	break;
    case OC_Int16:
	snprintf(buf,bufsize,"%d",*(short*)value);
	break;
    case OC_UInt16:
	snprintf(buf,bufsize,"%u",*(unsigned short*)value);
	break;
    case OC_Int32:
	snprintf(buf,bufsize,"%d",*(int*)value);
	break;
    case OC_UInt32:
	snprintf(buf,bufsize,"%u",*(unsigned int*)value);
	break;
    case OC_Float32:
	snprintf(buf,bufsize,"%g",*(float*)value);
	break;
    case OC_Float64:
	snprintf(buf,bufsize,"%g",*(double*)value);
	break;
#ifdef HAVE_LONG_LONG_INT
    case OC_Int64:
	snprintf(buf,bufsize,"%lld",*(long long*)value);
	break;
    case OC_UInt64:
	snprintf(buf,bufsize,"%llu",*(unsigned long long*)value);
	break;
#endif
    case OC_String:
    case OC_URL: {
	char* s = *(char**)value;
	snprintf(buf,bufsize,"\"%s\"",s);
	} break;
    default: break;
    }
    return OC_NOERR;
}

size_t
xxdrsize(OCtype etype)
{
    switch (etype) {
    case OC_Char:
    case OC_Byte:
    case OC_UByte:
    case OC_Int16:
    case OC_UInt16:
    case OC_Int32:
    case OC_UInt32:
	return XDRUNIT;
    case OC_Int64:
    case OC_UInt64:
	return (2*XDRUNIT);
    case OC_Float32:
	return XDRUNIT;
    case OC_Float64:
	return (2*XDRUNIT);
    case OC_String:
    case OC_URL:
    default: break;
    }
    return 0;
}

/**************************************/

char*
ocerrstring(int err)
{
    if(err == 0) return "no error";
    if(err > 0) return strerror(err);
    switch (err) {
	case OC_EBADID:
	    return "OC_EBADID: Not a valid ID";
	case OC_EINVAL:
	    return "OC_EINVAL: Invalid argument";
	case OC_EPERM:
	    return "OC_EPERM: Write to read only";
	case OC_EINVALCOORDS:
	    return "OC_EINVALCOORDS: Index exceeds dimension bound";
	case OC_ENOTVAR:
	    return "OC_ENOTVAR: Variable not found";
	case OC_ECHAR:
	    return "OC_ECHAR: Attempt to convert between text & numbers";
	case OC_EEDGE:
	    return "OC_EEDGE: Start+count exceeds dimension bound";
	case OC_ESTRIDE:
	    return "OC_ESTRIDE: Illegal stride";
	case OC_ENOMEM:
	    return "OC_ENOMEM: Memory allocation (malloc) failure";
	case OC_EDIMSIZE:
	    return "OC_EDIMSIZE: Invalid dimension size";
	case OC_EDAP:
	    return "OC_EDAP: unspecified DAP failure";
	case OC_EXDR:
	    return "OC_EXDR: XDR failure";
	case OC_ECURL:
	    return "OC_ECURL: unspecified libcurl failure";
	case OC_EBADURL:
	    return "OC_EBADURL: malformed url";
	case OC_EBADVAR:
	    return "OC_EBADVAR: no such variable";
	case OC_EOPEN:
	    return "OC_EOPEN: temporary file open failed";	
	case OC_EIO:
	    return "OC_EIO: I/O failure";
	case OC_ENODATA:
	    return "OC_ENODATA: Variable has no data in DAP request";
	case OC_EDAPSVC:
	    return "OC_EDAPSVC: DAP Server error";
	case OC_ENAMEINUSE:
	    return "OC_ENAMEINUSE: Duplicate name in DDS";
	case OC_EDAS:
	    return "OC_EDAS: Malformed or unreadable DAS";
	case OC_EDDS:
	    return "OC_EDDS: Malformed or unreadable DDS";
	case OC_EDATADDS:
	    return "OC_EDATADDS: Malformed or unreadable DATADDS";
	case OC_ERCFILE:
	    return "OC_ERCFILE: Malformed,  unreadable, or bad value in the run-time configuration file";
	case OC_ENOFILE:
	    return "OC_ENOFILE: cannot read content of URL";

	/* oc_data related errors */
	case OC_EINDEX:
	    return "OC_EINDEX: index argument too large";
	case OC_EBADTYPE:
	    return "OC_EBADTYPE: argument of wrong OCtype";

	/* String concatenation overrun */
	case OC_EOVERRUN:
	    return "OC_EOVERRUN: internal concatenation failed";

	/* Authorization Error */
	case OC_EAUTH:
	    return "OC_EAUTH: authorization failure";

	default: break;
    }
    return "<unknown error code>";
}

OCerror
ocsvcerrordata(OCstate* state, char** codep, char** msgp, long* httpp)
{
    if(codep) *codep = state->error.code;
    if(msgp) *msgp = state->error.message;
    if(httpp) *httpp = state->error.httpcode;
    return OC_NOERR;    
}

/* if we get OC_EDATADDS error, then try to capture any
   error message and log it; assumes that in this case,
   the datadds is not big.
*/
void
ocdataddsmsg(OCstate* state, OCtree* tree)
{
#define ERRCHUNK 1024
#define ERRFILL ' '
#define ERRTAG "Error {" 
    int i,j;
    size_t len;
    XXDR* xdrs;
    char* contents;
    off_t ckp;

    if(tree == NULL) return;
    /* get available space */
    xdrs = tree->data.xdrs;
    len = xxdr_length(xdrs);
    if(len < strlen(ERRTAG))
	return; /* no room */
    ckp = xxdr_getpos(xdrs);
    xxdr_setpos(xdrs,(off_t)0);
    /* read the whole thing */
    contents = (char*)malloc(len+1);
    (void)xxdr_getbytes(xdrs,contents,(off_t)len);
    contents[len] = '\0';
    /* Look for error tag */
    for(i=0;i<len;i++) {
        if(ocstrncmp(contents+i,ERRTAG,strlen(ERRTAG))==0) {
	    /* log the error message */
	    /* Do a quick and dirty escape */
	    for(j=i;j<len;j++) {
	        int c = contents[i+j];
		if(c > 0 && (c < ' ' || c >= '\177'))
		    contents[i+j] = ERRFILL;
	    }
	    nclog(NCLOGERR,"DATADDS failure, possible message: '%s'\n",
			contents+i);
	    goto done;
	}
    }
    xxdr_setpos(xdrs,ckp);
done:
    return;
}

/* Given some set of indices [i0][i1]...[in] (where n == rank-1)
   and the maximum sizes, compute the linear offset
   for set of dimension indices.
*/
size_t
ocarrayoffset(size_t rank, size_t* sizes, const size_t* indices)
{
    unsigned int i;
    size_t count = 0;
    for(i=0;i<rank;i++) {
	count *= sizes[i];
	count += indices[i];
    }
    return count;
}

/* Inverse of ocarrayoffset: convert linear index to a set of indices */
void
ocarrayindices(size_t index, size_t rank, size_t* sizes, size_t* indices)
{
    int i;
    for(i=rank-1;i>=0;i--) {
	indices[i] = index % sizes[i];
	index = (index - indices[i]) / sizes[i];
    }
}

/* Given some set of edge counts [i0][i1]...[in] (where n == rank-1)
   and the maximum sizes, compute the linear offset
   for the last edge position
*/
size_t
ocedgeoffset(size_t rank, size_t* sizes, size_t* edges)
{
    unsigned int i;
    size_t count = 0;
    for(i=0;i<rank;i++) {
	count *= sizes[i];
	count += (edges[i]-1);
    }
    return count;
}

int
ocvalidateindices(size_t rank, size_t* sizes, size_t* indices)
{
    int i;
    for(i=0;i<rank;i++) {
	if(indices[i] >= sizes[i]) return 0;
    }
    return 1;
}

int
oc_ispacked(OCnode* node)
{
    OCtype octype = node->octype;
    OCtype etype = node->etype;
    int isscalar = (node->array.rank == 0);
    int packed;

    if(isscalar || octype != OC_Atomic)
	return 0; /* is not packed */
    packed = (etype == OC_Byte
	      || etype == OC_UByte
              || etype == OC_Char) ? 1 : 0;
    return packed;
}

/* Must be consistent with ocx.h.OCDT */
#define NMODES 6
#define MAXMODENAME 8 /*max (strlen(modestrings[i])) */
static const char* modestrings[NMODES+1] = {
"FIELD", /* ((OCDT)(1<<0)) field of a container */
"ELEMENT", /* ((OCDT)(1<<1)) element of a structure array */
"RECORD", /* ((OCDT)(1<<2)) record of a sequence */
"ARRAY", /* ((OCDT)(1<<3)) is structure array */
"SEQUENCE", /* ((OCDT)(1<<4)) is sequence */
"ATOMIC", /* ((OCDT)(1<<5)) is atomic leaf */
NULL,
};

char*
ocdtmodestring(OCDT mode,int compact)
{
    char* result = NULL;
    int i;
    char* p = NULL;

    result = malloc(1+(NMODES*(MAXMODENAME+1)));
    if(result == NULL) return NULL;
    p = result;
    result[0] = '\0';
    if(mode == 0) {
	if(compact) *p++ = '-';
	else if(!occoncat(result,sizeof(result),1,"NONE"))
	    return NULL;
    } else for(i=0;;i++) {
	const char* ms = modestrings[i];
	if(ms == NULL) break;
	if(!compact && i > 0)
	    if(!occoncat(result,sizeof(result),1,","))
		return NULL;
        if(fisset(mode,(1<<i))) {
	    if(compact) *p++ = ms[0];
	    else if(!occoncat(result,sizeof(result),1,ms))
		return NULL;
	}
    }
    /* pad compact list out to NMODES in length (+1 for null terminator) */
    if(compact) {
	while((p-result) < NMODES) *p++ = ' ';
	*p = '\0';
    }
    return result;
}


/*
Instead of using snprintf to concatenate
multiple strings into a given target,
provide a direct concatenator.
So, this function concats the n argument strings
and overwrites the contents of dst.
Care is taken to never overrun the available
space (the size parameter).
Note that size is assumed to include the null
terminator and that in the event of overrun,
the string will have a null at dst[size-1].
Return 0 if overrun, 1 otherwise.
*/
int
occopycat(char* dst, size_t size, size_t n, ...)
{
    va_list args;
    size_t avail = size - 1;
    int i; 
    int status = 1; /* assume ok */
    char* p = dst;

    if(n == 0) {
	if(size > 0)
	    dst[0] = '\0';
	return (size > 0 ? 1: 0);
    }
	
    va_start(args,n);
    for(i=0;i<n;i++) {
	char* q = va_arg(args, char*);
	for(;;) {
	    int c = *q++;
	    if(c == '\0') break;
	    if(avail == 0) {status = 0; goto done;}
	    *p++ = c;
	    avail--;
	}
    }
    /* make sure we null terminate;
       note that since avail was size-1, there
       will always be room
    */
    *p = '\0';    

done:
    va_end(args);
    return status;    
}

/*
Similar to occopycat, but
the n strings are, in effect,
concatenated and appended to the
current contents of dst.
The size parameter is the total size of dst,
including room for null terminator.
Return 0 if overrun, 1 otherwise.
*/
int
occoncat(char* dst, size_t size, size_t n, ...)
{
    va_list args;
    int status = 1; /* assume ok */
    size_t avail = 0;
    int i; 
    char* p;
    size_t dstused;
    dstused = strlen(dst);
    if(dstused >= size)
	return 0; /* There is no room to append */
    /* move to the end of the current contents of dst
       and act like we are doing copycat
    */
    p = dst + dstused;
    size -= dstused;
    avail = size - 1;
    if(n == 0) {
	if(size > 0)
	    p[0] = '\0';
	return (size > 0 ? 1: 0);
    }
	
    va_start(args,n);
    for(i=0;i<n;i++) {
	char* q = va_arg(args, char*);
	for(;;) {
	    int c = *q++;
	    if(c == '\0') break;
	    if(avail == 0) {status = 0; goto done;}
	    *p++ = c;
	    avail--;
	}
    }
    /* make sure we null terminate;
       note that since avail was size-1, there
       will always be room
    */
    *p = '\0';    

done:
    va_end(args);
    return status;    
}

/* merge two envv style lists */
char**
ocmerge(const char** list1, const char** list2)
{
    int l1, l2;
    char** merge;
    const char** p;
    for(l1=0,p=list1;*p;p++) {l1++;}
    for(l2=0,p=list2;*p;p++) {l2++;}
    merge = (char**)malloc(sizeof(char*)*(l1+l2+1));
    if(merge == NULL)
	return NULL;
    memcpy(merge,list1,sizeof(char*)*l1);
    memcpy(merge+l1,list2,sizeof(char*)*l2);
    merge[l1+l2] = NULL;
    return merge;    
}