File: occompile.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 (468 lines) | stat: -rw-r--r-- 12,778 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
/* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc.
   See the COPYRIGHT file for more information. */

#include "config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include "ocinternal.h"
#include "ocdebug.h"
#include "ocdump.h"

/* Mnemonic */
#define TOPLEVEL 1

/* Forward */
static OCdata* newocdata(OCnode* pattern);
static size_t ocxdrsize(OCtype etype,int isscalar);
static OCerror occompile1(OCstate*, OCnode*, XXDR*, OCdata**);
static OCerror occompilerecord(OCstate*, OCnode*, XXDR*, OCdata**);
static OCerror occompilefields(OCstate*, OCdata*, XXDR*, int istoplevel);
static OCerror occompileatomic(OCstate*, OCdata*, XXDR*);
static int ocerrorstring(XXDR* xdrs);
static int istoplevel(OCnode* node);

/* Sequence tag constant */
static const char StartOfSequence = '\x5A';
static const char EndOfSequence = '\xA5';

/*
Provide an option that makes a single pass over 
the data packet and record pointers into it
to speed up access.
*/

/* Assume we are operating on the datadds tree */
OCerror
occompile(OCstate* state, OCnode* xroot)
{
    OCerror ocstat = OC_NOERR;
    XXDR* xxdrs;
    OCtree* xtree;
    OCdata* data;

    OCASSERT(state != NULL);
    OCASSERT(xroot != NULL);
    OCASSERT(xroot->tree != NULL);
    OCASSERT(xroot->tree->dxdclass == OCDATADDS);
    OCASSERT(xroot->tree->data.data == NULL);

    xtree = xroot->tree;

    xxdrs = xtree->data.xdrs;
    if(xxdrs == NULL) return OCTHROW(OC_EXDR);

    ocstat = occompile1(state,xroot,xxdrs,&data);
    if(ocstat == OC_NOERR)
	xtree->data.data = data;

#ifdef OCDEBUG
{
    Ncbytes* buffer = ncbytesnew();
    ocdumpdatatree(state,data,buffer,0);
    fprintf(stderr,"datatree:\n%s",ncbytescontents(buffer));
    ncbytesfree(buffer);
}
#endif
    return OCTHROW(ocstat);
}

static OCerror
occompile1(OCstate* state, OCnode* xnode, XXDR* xxdrs, OCdata** datap)
{
    OCerror ocstat = OC_NOERR;
    size_t i;
    OCdata* data = NULL;
    size_t nelements = 0;
    NClist* records = NULL;

    /* Allocate instance for this node */
    data = newocdata(xnode);
    MEMFAIL(data);

    /* Capture position(s) */
    data->xdroffset = xxdr_getpos(xxdrs);

    switch (xnode->octype) {

    case OC_Dataset:
    case OC_Grid: /* Always scalars */
	ocstat = occompilefields(state,data,xxdrs,istoplevel(xnode));
	if(ocstat != OC_NOERR) goto fail;
	break;

    case OC_Structure:
	if(xnode->array.rank == 0) {/* scalar */
	    ocstat = occompilefields(state,data,xxdrs,istoplevel(xnode));
	    if(ocstat != OC_NOERR) goto fail;
	} else { /* dimensioned structure */
	    unsigned int xdrcount;
    	    fset(data->datamode,OCDT_ARRAY);
	    /* Determine # of instances */
            nelements = octotaldimsize(xnode->array.rank,xnode->array.sizes);
            if(nelements == 0) {ocstat = OCTHROW(OC_ENODATA); goto fail;}
            /* Validate and skip the leading count field */
            if(!xxdr_uint(xxdrs,&xdrcount))
	        {ocstat = OC_EXDR; goto fail;}
	    if(xdrcount != nelements)
   	        {ocstat=OCTHROW(OC_EINVALCOORDS); goto fail;}

	    /* allocate space to capture all the element instances */
	    data->instances = (OCdata**)malloc(nelements*sizeof(OCdata*));
	    MEMGOTO(data->instances,ocstat,fail);
	    data->ninstances = 0;

	    /* create and fill the element instances */
	    for(i=0;i<nelements;i++) {
		OCdata* instance = newocdata(xnode);
		MEMGOTO(instance,ocstat,fail);
		fset(instance->datamode,OCDT_ELEMENT);
		data->instances[i] = instance;
		data->ninstances++;
		/* Capture the back link */
		instance->container = data;
		instance->index = i;
  	        /* capture the current instance position */
		instance->xdroffset = xxdr_getpos(xxdrs);
	        /* Now compile the fields of this instance */
		ocstat = occompilefields(state,instance,xxdrs,!TOPLEVEL);
		if(ocstat != OC_NOERR) {goto fail;}
	    }
	}
        break;

    case OC_Sequence:
	/* Since we do not know the # records beforehand,
           use a list to collect the record instances.
        */
	fset(data->datamode,OCDT_SEQUENCE);
	records = nclistnew();
	for(nelements=0;;nelements++) {
            /* pick up the sequence record begin marker*/
            char tmp[sizeof(unsigned int)];
            /* extract the tag byte*/
	    if(!xxdr_opaque(xxdrs,tmp,(off_t)sizeof(tmp)))
		{ocstat = OC_EXDR; goto fail;}
            if(tmp[0] == StartOfSequence) {
		/* Allocate a record instance */
		OCdata* record = NULL;
		ocstat = occompilerecord(state,xnode,xxdrs,&record);
	        if(ocstat != OC_NOERR || !record) goto fail;
		/* Capture the back link */
		record->container = data;
		record->index = nelements;
		nclistpush(records,(void*)record);
		record = NULL;
            } else if(tmp[0] == EndOfSequence) {
                break; /* we are done with the this sequence instance*/
            } else {
		nclog(NCLOGERR,"missing/invalid begin/end record marker\n");
                ocstat = OCTHROW(OC_EINVALCOORDS);
		goto fail;
            }
	}
        OCASSERT(nelements == nclistlength(records));
	/* extract the content */
	data->ninstances = nelements;
	data->instances = (OCdata**)nclistdup(records);
	MEMGOTO(data,ocstat,fail);
	nclistfree(records);	    
	records = NULL;
        break;

    case OC_Atomic:
	fset(data->datamode,OCDT_ATOMIC);
	ocstat = occompileatomic(state,data,xxdrs);
	if(ocstat != OC_NOERR) goto fail;
	break;

    default:
	OCPANIC1("occompile: encountered unexpected node type: %x",xnode->octype);
        break;
    }

/*ok:*/
    if(datap) {
	*datap = data;
	data = NULL;
    }

    if(data != NULL)
	ocdata_free(state,data);

    return OCTHROW(ocstat);    

fail:
    /* See if we can extract error info from the response */
    ocerrorstring(xxdrs);

    if(records != NULL) {
	for(i=0;i<nclistlength(records);i++)
	    ocdata_free(state,(OCdata*)nclistget(records,i));
	nclistfree(records);
    }

    if(data != NULL)
	ocdata_free(state,data);

    return OCTHROW(ocstat);
}

static OCerror
occompilerecord(OCstate* state, OCnode* xnode, XXDR* xxdrs, OCdata** recordp)
{
    OCerror ocstat = OC_NOERR;
    OCdata* record = newocdata(xnode);/* create record record */
    MEMFAIL(record);
    fset(record->datamode,OCDT_RECORD);
    record->pattern = xnode;
    /* capture the current record position */
    record->xdroffset = xxdr_getpos(xxdrs);
    /* Compile the fields of this record */
    ocstat = OCTHROW(occompilefields(state,record,xxdrs,!TOPLEVEL));
    if(ocstat == OC_NOERR) {
        if(recordp) {
	    *recordp = record;
	    record = NULL;
	}
        if(record != NULL)
	    ocdata_free(state,record);
    }
    return OCTHROW(ocstat);    
}

static OCerror
occompilefields(OCstate* state, OCdata* data, XXDR* xxdrs, int istoplevel)
{
    size_t i;
    OCerror ocstat = OC_NOERR;
    size_t nelements;
    OCnode* xnode = data->pattern;

    assert(data != NULL);
    nelements = nclistlength(xnode->subnodes);
    if(nelements == 0)
	goto done;
    data->instances = (OCdata**)malloc(nelements*sizeof(OCdata*));
    MEMFAIL(data->instances);
    for(i=0;i<nelements;i++) {
        OCnode* fieldnode;
        OCdata* fieldinstance;
	fieldnode = (OCnode*)nclistget(xnode->subnodes,i);
        ocstat = occompile1(state,fieldnode,xxdrs,&fieldinstance);
	if(ocstat != OC_NOERR)
	    goto fail;
	fset(fieldinstance->datamode,OCDT_FIELD);
	data->instances[i] = fieldinstance;
	data->ninstances++;
	/* Capture the back link */
	fieldinstance->container = data;
        fieldinstance->index = i;
    }

    /* If top-level, then link the OCnode to the OCdata directly */
    if(istoplevel) {
	for(i=0;i<nelements;i++) {
            OCnode* fieldnode = (OCnode*)nclistget(xnode->subnodes,i);
            OCdata* fieldinstance = data->instances[i];
	    fieldnode->data = fieldinstance;
	}
    }

done:
    return OCTHROW(ocstat);

fail:
    if(data->instances != NULL) {
	for(i=0;i<data->ninstances;i++)
	    ocdata_free(state,data->instances[i]);
	data->ninstances = 0;
    }
    return OCTHROW(ocstat);
}

static OCerror
occompileatomic(OCstate* state, OCdata* data, XXDR* xxdrs)
{
    OCerror ocstat = OC_NOERR;
    int i;
    off_t nelements,xdrsize;
    unsigned int xxdrcount;
    OCnode* xnode = data->pattern;
    int scalar = (xnode->array.rank == 0);
    
    OCASSERT((xnode->octype == OC_Atomic));

    if(!scalar) {
        /* Use the count from the datadds */
        nelements = octotaldimsize(xnode->array.rank,xnode->array.sizes);
        /* Get first copy of the dimension count */
        if(!xxdr_uint(xxdrs,&xxdrcount)) {ocstat = OC_EXDR; goto fail;}
        if(xxdrcount != nelements) {ocstat=OCTHROW(OC_EINVALCOORDS); goto fail;}
        if(xnode->etype != OC_String && xnode->etype != OC_URL) {
            /* Get second copy of the dimension count */
            if(!xxdr_uint(xxdrs,&xxdrcount)) {ocstat = OC_EXDR; goto fail;}
            if(xxdrcount != nelements) {ocstat=OCTHROW(OC_EINVALCOORDS); goto fail;}
        }
    } else { /*scalar*/
	nelements = 1;
	xxdrcount = 1;
    }

    data->xdroffset = xxdr_getpos(xxdrs);
    data->ninstances = xxdrcount;
    data->xdrsize = ocxdrsize(xnode->etype,scalar);

    switch (xnode->etype) {

    /* Do the fixed sized, non-packed cases */
    case OC_Int16: case OC_UInt16:
    case OC_Int32: case OC_UInt32:
    case OC_Int64: case OC_UInt64:
    case OC_Float32: case OC_Float64:
	/* Skip the data */
	xxdr_skip(xxdrs,data->ninstances*data->xdrsize);
	break;

    /* Do the fixed sized, possibly packed cases */
    case OC_Byte:
    case OC_UByte:
    case OC_Char:
	/* Get the totalsize and round up to multiple of XDRUNIT */
	xdrsize = data->ninstances*data->xdrsize;
	xdrsize = RNDUP(xdrsize);
	/* Skip the data */
	xxdr_skip(xxdrs,xdrsize);
	break;

    /* Hard case, because strings are variable length */
    case OC_String: case OC_URL:
	/* Start by allocating a set of pointers for each string */
	data->nstrings = xxdrcount;
	data->strings = (off_t*)malloc(sizeof(off_t)*data->nstrings);
	/* We need to walk each string, get size, then skip */
        for(i=0;i<data->nstrings;i++) {
	    unsigned int len;
	    off_t lenz;
	    data->strings[i] = xxdr_getpos(xxdrs);
	    /* get exact string length */
	    if(!xxdr_uint(xxdrs,&len)) {ocstat = OC_EXDR; goto fail;}
	    lenz = (off_t)len;
	    lenz = RNDUP(lenz);
	    /* Skip the data */
	    xxdr_skip(xxdrs,lenz);
        }
        break;

    default:
	OCPANIC1("unexpected etype: %d",xnode->etype);

    } /* switch */

/*ok:*/
    return OCTHROW(ocstat);

fail:
    if(data->strings != NULL)
	free(data->strings);
    data->strings = NULL;
    data->ninstances = 0;
    return OCTHROW(ocstat);
}

void
ocdata_free(OCstate* state, OCdata* data)
{
    if(data == NULL)
	return;

    if(data->instances != NULL) {
	int i;
        for(i=0;i<data->ninstances;i++)
	    ocdata_free(state,data->instances[i]);
	free(data->instances);
    }
    if(data->strings != NULL)
	free(data->strings);
    free(data);
}

static OCdata*
newocdata(OCnode* pattern)
{
    OCdata* data = (OCdata*)calloc(1,sizeof(OCdata));
    MEMCHECK(data,NULL);
    data->header.magic = OCMAGIC;
    data->header.occlass = OC_Data;
    data->pattern = pattern;
    return data;
}

static int
istoplevel(OCnode* node)
{
    if(node == NULL)
	return 1; /* base case */
    if(!istoplevel(node->container))
	return 0;
    switch (node->octype) {
    case OC_Dataset: case OC_Grid: case OC_Atomic: return 1;
    case OC_Structure:
	return (node->array.rank == 0 ? 1 : 0); /* Toplevel if scalar */ 
    case OC_Sequence: default: return 0;
    }
    return 1;
}


/* XDR representation size depends on if this is scalar or not */
static size_t
ocxdrsize(OCtype etype, int isscalar)
{
    switch (etype) {
    case OC_Char:
    case OC_Byte:
    case OC_UByte:
	return (isscalar? XDRUNIT : 1);
    case OC_Int16:
    case OC_UInt16:
    case OC_Int32:
    case OC_UInt32:
    case OC_Float32:
	return XDRUNIT;
    case OC_Float64:
    case OC_Int64:
    case OC_UInt64:
	return 2*XDRUNIT;
    default:
	break; /* no simple size */
    }
    return 0;
}

#define tag "Error {\n"

static int
ocerrorstring(XXDR* xdrs)
{
    /* Check to see if the xdrs contains "Error {\n'; assume it is at the beginning of data */
    off_t avail = xxdr_getavail(xdrs);
    char* data;
    if(!xxdr_setpos(xdrs,(off_t)0)) return 0;
    data = (char*)malloc((size_t)avail);
    MEMCHECK(data,0);    
    if(!xxdr_opaque(xdrs,data,avail)) {free(data); return 0;}
    /* check for error tag at front */
    if(ocstrncmp(data,tag,sizeof(tag))==0) {
	char* p;
        if((p=strchr(data,'}')) != NULL) *(++p)='\0';
        nclog(NCLOGERR,"Server error: %s",data);
        /* Since important, report to stderr as well */
        fprintf(stderr,"Server error: %s",data);
	return 1;
    }
    return 0;
}