/src/netcdf-c/libdispatch/dinstance.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata |
3 | | See COPYRIGHT for license information. |
4 | | */ |
5 | | |
6 | | /* |
7 | | This file contains various instance operations that operate |
8 | | on a deep level rather than the shallow level of e.g. nc_free_vlen_t. |
9 | | Currently two operations are defined: |
10 | | 1. reclaim a vector of instances |
11 | | 2. copy a vector of instances |
12 | | */ |
13 | | |
14 | | #include "config.h" |
15 | | #include <stdlib.h> |
16 | | #include <string.h> |
17 | | #include <assert.h> |
18 | | #include "netcdf.h" |
19 | | #include "nc4internal.h" |
20 | | #include "nc4dispatch.h" |
21 | | #include "ncoffsets.h" |
22 | | #include "ncbytes.h" |
23 | | |
24 | | #undef REPORT |
25 | | #undef DEBUG |
26 | | |
27 | | |
28 | | /* It is helpful to have a structure that contains memory and an offset */ |
29 | | typedef struct Position{char* memory; ptrdiff_t offset;} Position; |
30 | | |
31 | | static int type_alignment_initialized = 0; |
32 | | |
33 | | /* Forward */ |
34 | | #ifdef USE_NETCDF4 |
35 | | static int reclaim_datar(int ncid, nc_type xtype, Position*); |
36 | | static int reclaim_compound(int ncid, nc_type xtype, size_t size, size_t nfields, Position* offset); |
37 | | static int reclaim_vlen(int ncid, nc_type xtype, nc_type basetype, Position* offset); |
38 | | static int reclaim_enum(int ncid, nc_type xtype, nc_type basetype, Position* offset); |
39 | | static int reclaim_opaque(int ncid, nc_type xtype, size_t size, Position* offset); |
40 | | |
41 | | static int copy_datar(int ncid, nc_type xtype, Position* src, Position* dst); |
42 | | static int copy_compound(int ncid, nc_type xtype, size_t size, size_t nfields, Position* src, Position* dst); |
43 | | static int copy_vlen(int ncid, nc_type xtype, nc_type basetype, Position* src, Position* dst); |
44 | | static int copy_enum(int ncid, nc_type xtype, nc_type basetype, Position* src, Position* dst); |
45 | | static int copy_opaque(int ncid, nc_type xtype, size_t size, Position* src,Position* dst); |
46 | | |
47 | | static int dump_datar(int ncid, nc_type xtype, Position*, NCbytes* buf); |
48 | | static int dump_compound(int ncid, nc_type xtype, size_t size, size_t nfields, Position* offset, NCbytes* buf); |
49 | | static int dump_vlen(int ncid, nc_type xtype, nc_type basetype, Position* offset, NCbytes* buf); |
50 | | static int dump_enum(int ncid, nc_type xtype, nc_type basetype, Position* offset, NCbytes* buf); |
51 | | static int dump_opaque(int ncid, nc_type xtype, size_t size, Position* offset, NCbytes* buf); |
52 | | |
53 | | static ptrdiff_t read_align(ptrdiff_t offset, size_t alignment); |
54 | | #endif |
55 | | |
56 | | static int NC_inq_any_type(int ncid, nc_type typeid, char *name, size_t *size, nc_type *basetypep, size_t *nfieldsp, int *classp); |
57 | | |
58 | | /** |
59 | | |
60 | | Reclaim a vector of instances of a type. This improves upon |
61 | | e.g. nc_free_vlen. This recursively walks the top-level |
62 | | instances to reclaim any nested data such as vlen or strings or such. |
63 | | |
64 | | Assumes it is passed a pointer to count instances of xtype. |
65 | | Reclaims any nested data. |
66 | | |
67 | | WARNING: This needs access to the type metadata of the file, so |
68 | | a valid ncid and typeid must be available, which means the file |
69 | | must not have been closed or aborted. |
70 | | |
71 | | WARNING: DOES NOT RECLAIM THE TOP-LEVEL MEMORY (see the |
72 | | nc_reclaim_data_all function). The reason is that we do not |
73 | | know how it was allocated (e.g. static vs dynamic); only the |
74 | | caller can know that. But note that it assumes all memory |
75 | | blocks other than the top were dynamically allocated, so they |
76 | | will be free'd. |
77 | | |
78 | | Should work for any netcdf format. |
79 | | |
80 | | @param ncid root id |
81 | | @param xtype type id |
82 | | @param memory ptr to top-level memory to reclaim |
83 | | @param count number of instances of the type in memory block |
84 | | @return error code |
85 | | */ |
86 | | |
87 | | int |
88 | | nc_reclaim_data(int ncid, nc_type xtype, void* memory, size_t count) |
89 | 0 | { |
90 | 0 | int stat = NC_NOERR; |
91 | 0 | size_t i; |
92 | 0 | Position offset; |
93 | 0 | int isf; |
94 | |
|
95 | 0 | if(ncid < 0 || xtype <= 0) |
96 | 0 | {stat = NC_EINVAL; goto done;} |
97 | 0 | if(memory == NULL && count > 0) |
98 | 0 | {stat = NC_EINVAL; goto done;} |
99 | 0 | if(memory == NULL || count == 0) |
100 | 0 | goto done; /* ok, do nothing */ |
101 | | #ifdef REPORT |
102 | | fprintf(stderr,">>> reclaim: memory=%p count=%lu ncid=%d xtype=%d\n",memory,(unsigned long)count,ncid,xtype); |
103 | | #endif |
104 | | |
105 | | /* Optimizations */ |
106 | | /* 1. Vector of fixed size types */ |
107 | 0 | if((stat = NC4_inq_type_fixed_size(ncid,xtype,&isf))) goto done; |
108 | 0 | if(isf) goto done; /* no need to reclaim anything */ |
109 | | |
110 | 0 | #ifdef USE_NETCDF4 |
111 | | /* 2.Vector of strings */ |
112 | 0 | if(xtype == NC_STRING) { |
113 | 0 | char** ss = (char**)memory; |
114 | 0 | for(i=0;i<count;i++) { |
115 | 0 | nullfree(ss[i]); |
116 | 0 | } |
117 | 0 | goto done; |
118 | 0 | } |
119 | 0 | offset.memory = (char*)memory; /* use char* so we can do pointer arithmetic */ |
120 | 0 | offset.offset = 0; |
121 | 0 | for(i=0;i<count;i++) { |
122 | 0 | if((stat=reclaim_datar(ncid,xtype,&offset))) /* reclaim one instance */ |
123 | 0 | break; |
124 | 0 | } |
125 | | #else |
126 | | stat = NC_EBADTYPE; |
127 | | #endif |
128 | |
|
129 | 0 | done: |
130 | 0 | return stat; |
131 | 0 | } |
132 | | |
133 | | /* Alternate entry point: includes recovering the top-level memory */ |
134 | | int |
135 | | nc_reclaim_data_all(int ncid, nc_type xtypeid, void* memory, size_t count) |
136 | 0 | { |
137 | 0 | int stat = NC_NOERR; |
138 | 0 | stat = nc_reclaim_data(ncid,xtypeid,memory,count); |
139 | 0 | if(stat == NC_NOERR && memory != NULL) |
140 | 0 | free(memory); |
141 | 0 | return stat; |
142 | 0 | } |
143 | | |
144 | | #ifdef USE_NETCDF4 |
145 | | /* Recursive type walker: reclaim a single instance */ |
146 | | static int |
147 | | reclaim_datar(int ncid, nc_type xtype, Position* offset) |
148 | 0 | { |
149 | 0 | int stat = NC_NOERR; |
150 | 0 | size_t xsize; |
151 | 0 | nc_type basetype; |
152 | 0 | size_t nfields; |
153 | 0 | int klass, isf; |
154 | |
|
155 | 0 | if((stat = NC4_inq_type_fixed_size(ncid,xtype,&isf))) goto done; |
156 | | |
157 | | /* Get relevant type info */ |
158 | 0 | if((stat = NC_inq_any_type(ncid,xtype,NULL,&xsize,&basetype,&nfields,&klass))) goto done; |
159 | | |
160 | 0 | if(isf) { /* no need to reclaim anything */ |
161 | 0 | offset->offset += xsize; |
162 | 0 | goto done; |
163 | 0 | } |
164 | | |
165 | 0 | switch (xtype) { |
166 | 0 | case NC_STRING: { |
167 | 0 | char** sp = (char**)(offset->memory + offset->offset); |
168 | | /* Need to reclaim string */ |
169 | 0 | if(*sp != NULL) free(*sp); |
170 | 0 | offset->offset += xsize; |
171 | 0 | } break; |
172 | 0 | default: |
173 | | /* reclaim a user type */ |
174 | 0 | switch (klass) { |
175 | 0 | case NC_OPAQUE: stat = reclaim_opaque(ncid,xtype,xsize,offset); break; |
176 | 0 | case NC_ENUM: stat = reclaim_enum(ncid,xtype,basetype,offset); break; |
177 | 0 | case NC_COMPOUND: stat = reclaim_compound(ncid,xtype,xsize,nfields,offset); break; |
178 | 0 | case NC_VLEN: |
179 | 0 | stat = reclaim_vlen(ncid,xtype,basetype,offset); |
180 | 0 | break; |
181 | 0 | default: |
182 | 0 | stat = NC_EINVAL; |
183 | 0 | break; |
184 | 0 | } |
185 | 0 | break; |
186 | 0 | } |
187 | 0 | done: |
188 | 0 | return stat; |
189 | 0 | } |
190 | | |
191 | | static int |
192 | | reclaim_vlen(int ncid, nc_type xtype, nc_type basetype, Position* offset) |
193 | 0 | { |
194 | 0 | int stat = NC_NOERR; |
195 | 0 | size_t i; |
196 | 0 | nc_vlen_t* vl = (nc_vlen_t*)(offset->memory+offset->offset); |
197 | |
|
198 | 0 | if(vl->len > 0 && vl->p == NULL) |
199 | 0 | {stat = NC_EINVAL; goto done;} |
200 | | |
201 | | /* Free up each entry in the vlen list */ |
202 | 0 | if(vl->len > 0) { |
203 | 0 | Position voffset; |
204 | 0 | size_t alignment = 0; |
205 | 0 | if((stat = NC_type_alignment(ncid,basetype,&alignment))) goto done;; |
206 | 0 | voffset.memory = vl->p; |
207 | 0 | voffset.offset = 0; |
208 | 0 | for(i=0;i<vl->len;i++) { |
209 | 0 | voffset.offset = read_align(voffset.offset,alignment); |
210 | 0 | if((stat = reclaim_datar(ncid,basetype,&voffset))) goto done; |
211 | 0 | } |
212 | 0 | free(vl->p); |
213 | 0 | } |
214 | 0 | offset->offset += sizeof(nc_vlen_t); |
215 | |
|
216 | 0 | done: |
217 | 0 | return stat; |
218 | 0 | } |
219 | | |
220 | | static int |
221 | | reclaim_enum(int ncid, nc_type xtype, nc_type basetype, Position* offset) |
222 | 0 | { |
223 | 0 | int stat = NC_NOERR; |
224 | 0 | abort(); |
225 | | |
226 | | /* basically same as an instance of the enum's integer basetype */ |
227 | 0 | stat = reclaim_datar(ncid,basetype,offset); |
228 | 0 | return stat; |
229 | 0 | } |
230 | | |
231 | | static int |
232 | | reclaim_opaque(int ncid, nc_type xtype, size_t size, Position* offset) |
233 | 0 | { |
234 | 0 | abort(); |
235 | | /* basically a fixed size sequence of bytes */ |
236 | 0 | offset->offset += size; |
237 | 0 | return NC_NOERR; |
238 | 0 | } |
239 | | |
240 | | static int |
241 | | reclaim_compound(int ncid, nc_type xtype, size_t size, size_t nfields, Position* offset) |
242 | 0 | { |
243 | 0 | int stat = NC_NOERR; |
244 | 0 | size_t fid, i, arraycount; |
245 | 0 | ptrdiff_t saveoffset; |
246 | 0 | int ndims; |
247 | 0 | int dimsizes[NC_MAX_VAR_DIMS]; |
248 | |
|
249 | 0 | saveoffset = offset->offset; |
250 | | |
251 | | /* Get info about each field in turn and reclaim it */ |
252 | 0 | for(fid=0;fid<nfields;fid++) { |
253 | 0 | size_t fieldalignment; |
254 | 0 | nc_type fieldtype; |
255 | | |
256 | | /* Get all relevant info about the field */ |
257 | 0 | if((stat = nc_inq_compound_field(ncid,xtype,fid,NULL,&fieldalignment,&fieldtype,&ndims,dimsizes))) goto done; |
258 | | |
259 | 0 | if(ndims == 0) {ndims=1; dimsizes[0]=1;} /* fake the scalar case */ |
260 | | /* Align to this field */ |
261 | 0 | offset->offset = saveoffset + fieldalignment; |
262 | | /* compute the total number of elements in the field array */ |
263 | 0 | arraycount = 1; |
264 | 0 | for(i=0;i<ndims;i++) arraycount *= dimsizes[i]; |
265 | 0 | for(i=0;i<arraycount;i++) { |
266 | 0 | if((stat = reclaim_datar(ncid, fieldtype, offset))) goto done; |
267 | 0 | } |
268 | 0 | } |
269 | | /* Return to beginning of the compound and move |compound| */ |
270 | 0 | offset->offset = saveoffset; |
271 | 0 | offset->offset += size; |
272 | |
|
273 | 0 | done: |
274 | 0 | return stat; |
275 | 0 | } |
276 | | #endif |
277 | | |
278 | | /**************************************************/ |
279 | | |
280 | | /** |
281 | | Copy a vector of instances of a type. This recursively walks |
282 | | the top-level instances to copy any nested data such as vlen or |
283 | | strings or such. |
284 | | |
285 | | Assumes it is passed a pointer to count instances of xtype and a |
286 | | space into which to copy the instance. Copys any nested data. |
287 | | |
288 | | WARNING: This needs access to the type metadata of the file, so |
289 | | a valid ncid and typeid must be available, which means the file |
290 | | must not have been closed or aborted. |
291 | | |
292 | | WARNING: DOES NOT ALLOCATE THE TOP-LEVEL MEMORY (see the |
293 | | nc_copy_data_all function). Note that all memory blocks other |
294 | | than the top are dynamically allocated. |
295 | | |
296 | | Should work for any netcdf format. |
297 | | |
298 | | @param ncid root id |
299 | | @param xtype type id |
300 | | @param memory ptr to top-level memory to copy |
301 | | @param count number of instances of the type in memory block |
302 | | @param copy top-level space into which to copy the instance |
303 | | @return error code |
304 | | */ |
305 | | |
306 | | int |
307 | | nc_copy_data(int ncid, nc_type xtype, const void* memory, size_t count, void* copy) |
308 | 0 | { |
309 | 0 | int stat = NC_NOERR; |
310 | 0 | size_t i; |
311 | 0 | Position src; |
312 | 0 | Position dst; |
313 | 0 | size_t xsize; |
314 | 0 | int isf; |
315 | |
|
316 | 0 | if(ncid < 0 || xtype <= 0) |
317 | 0 | {stat = NC_EINVAL; goto done;} |
318 | 0 | if(memory == NULL && count > 0) |
319 | 0 | {stat = NC_EINVAL; goto done;} |
320 | 0 | if(copy == NULL && count > 0) |
321 | 0 | {stat = NC_EINVAL; goto done;} |
322 | 0 | if(memory == NULL || count == 0) |
323 | 0 | goto done; /* ok, do nothing */ |
324 | | |
325 | | #ifdef REPORT |
326 | | fprintf(stderr,">>> copy : copy =%p memory=%p count=%lu ncid=%d xtype=%d\n",copy,memory,(unsigned long)count,ncid,xtype); |
327 | | #endif |
328 | | |
329 | | /* Get type size */ |
330 | 0 | if((stat = NC_inq_any_type(ncid,xtype,NULL,&xsize,NULL,NULL,NULL))) goto done; |
331 | | |
332 | | /* Optimizations */ |
333 | | /* 1. Vector of fixed sized objects */ |
334 | 0 | if((stat = NC4_inq_type_fixed_size(ncid,xtype,&isf))) goto done; |
335 | 0 | if(isf) { |
336 | 0 | memcpy(copy,memory,xsize*count); |
337 | 0 | goto done; |
338 | 0 | } |
339 | | |
340 | 0 | #ifdef USE_NETCDF4 |
341 | 0 | src.memory = (char*)memory; /* use char* so we can do pointer arithmetic */ |
342 | 0 | src.offset = 0; |
343 | 0 | dst.memory = (char*)copy; /* use char* so we can do pointer arithmetic */ |
344 | 0 | dst.offset = 0; |
345 | 0 | for(i=0;i<count;i++) { |
346 | 0 | if((stat=copy_datar(ncid,xtype,&src,&dst))) /* copy one instance copy_datar will increment src and dst*/ |
347 | 0 | break; |
348 | 0 | } |
349 | | #else |
350 | | stat = NC_EBADTYPE; |
351 | | #endif |
352 | 0 | done: |
353 | 0 | return stat; |
354 | 0 | } |
355 | | |
356 | | /* Alternate entry point: includes recovering the top-level memory */ |
357 | | int |
358 | | nc_copy_data_all(int ncid, nc_type xtype, const void* memory, size_t count, void** copyp) |
359 | 0 | { |
360 | 0 | int stat = NC_NOERR; |
361 | 0 | size_t xsize = 0; |
362 | 0 | void* copy = NULL; |
363 | | |
364 | | /* Get type size */ |
365 | 0 | if((stat = NC_inq_any_type(ncid,xtype,NULL,&xsize,NULL,NULL,NULL))) goto done; |
366 | | |
367 | | /* allocate the top-level */ |
368 | 0 | if(count > 0) { |
369 | 0 | if((copy = calloc(xsize,count))==NULL) |
370 | 0 | {stat = NC_ENOMEM; goto done;} |
371 | 0 | } |
372 | 0 | stat = nc_copy_data(ncid,xtype,memory,count,copy); |
373 | 0 | if(copyp) {*copyp = copy; copy = NULL;} |
374 | |
|
375 | 0 | done: |
376 | 0 | if(copy) |
377 | 0 | stat = nc_reclaim_data_all(ncid,xtype,copy,count); |
378 | |
|
379 | 0 | return stat; |
380 | 0 | } |
381 | | |
382 | | #ifdef USE_NETCDF4 |
383 | | /* Recursive type walker: copy a single instance */ |
384 | | static int |
385 | | copy_datar(int ncid, nc_type xtype, Position* src, Position* dst) |
386 | 0 | { |
387 | 0 | int stat = NC_NOERR; |
388 | 0 | size_t xsize; |
389 | 0 | nc_type basetype; |
390 | 0 | size_t nfields; |
391 | 0 | int xclass,isf; |
392 | |
|
393 | 0 | if((stat = NC_inq_any_type(ncid,xtype,NULL,&xsize,&basetype,&nfields,&xclass))) goto done; |
394 | | |
395 | | /* Optimizations */ |
396 | | /* 1. Vector of fixed size types */ |
397 | 0 | if((stat = NC4_inq_type_fixed_size(ncid,xtype,&isf))) goto done; |
398 | 0 | if(isf) { |
399 | 0 | memcpy(dst->memory+dst->offset,src->memory+src->offset,xsize*1); |
400 | 0 | src->offset += xsize; |
401 | 0 | dst->offset += xsize; |
402 | 0 | goto done; |
403 | 0 | } |
404 | | |
405 | 0 | switch (xtype) { |
406 | 0 | case NC_STRING: { |
407 | 0 | char** sp = (char**)(src->memory + src->offset); |
408 | 0 | char* copy = NULL; |
409 | | /* Need to copy string */ |
410 | 0 | if(*sp != NULL) { |
411 | 0 | if((copy = strdup(*sp))==NULL) {stat = NC_ENOMEM; goto done;} |
412 | 0 | } |
413 | 0 | memcpy(dst->memory+dst->offset,(void*)©,sizeof(char*)); |
414 | 0 | src->offset += xsize; |
415 | 0 | dst->offset += xsize; |
416 | 0 | } break; |
417 | 0 | default: |
418 | | /* copy a user type */ |
419 | 0 | switch (xclass) { |
420 | 0 | case NC_OPAQUE: stat = copy_opaque(ncid,xtype,xsize,src,dst); break; |
421 | 0 | case NC_ENUM: stat = copy_enum(ncid,xtype,basetype,src,dst); break; |
422 | 0 | case NC_COMPOUND: stat = copy_compound(ncid,xtype,xsize,nfields,src,dst); break; |
423 | 0 | case NC_VLEN: stat = copy_vlen(ncid,xtype,basetype,src,dst); break; |
424 | 0 | default: stat = NC_EINVAL; break; |
425 | 0 | } |
426 | 0 | break; |
427 | 0 | } |
428 | 0 | done: |
429 | 0 | return stat; |
430 | 0 | } |
431 | | |
432 | | static int |
433 | | copy_vlen(int ncid, nc_type xtype, nc_type basetype, Position* src, Position* dst) |
434 | 0 | { |
435 | 0 | int stat = NC_NOERR; |
436 | 0 | size_t i, basetypesize; |
437 | 0 | nc_vlen_t* vl = (nc_vlen_t*)(src->memory+src->offset); |
438 | 0 | nc_vlen_t copy = {0,NULL}; |
439 | |
|
440 | 0 | if(vl->len > 0 && vl->p == NULL) |
441 | 0 | {stat = NC_EINVAL; goto done;} |
442 | | |
443 | | /* Get basetype info */ |
444 | 0 | if((stat = NC_inq_any_type(ncid,basetype,NULL,&basetypesize,NULL,NULL,NULL))) goto done; |
445 | | |
446 | | /* Make space in the copy vlen */ |
447 | 0 | if(vl->len > 0) { |
448 | 0 | copy.len = vl->len; |
449 | 0 | if((copy.p = calloc(copy.len,basetypesize))==NULL) {stat = NC_ENOMEM; goto done;} |
450 | 0 | } |
451 | | /* Copy each entry in the vlen list */ |
452 | 0 | if(vl->len > 0) { |
453 | 0 | Position vsrc, vdst; |
454 | 0 | size_t alignment = 0; |
455 | 0 | if((stat = NC_type_alignment(ncid,basetype,&alignment))) goto done;; |
456 | 0 | vsrc.memory = vl->p; |
457 | 0 | vsrc.offset = 0; |
458 | 0 | vdst.memory = copy.p; |
459 | 0 | vdst.offset = 0; |
460 | 0 | for(i=0;i<vl->len;i++) { |
461 | 0 | vsrc.offset= read_align(vsrc.offset,alignment); |
462 | 0 | vdst.offset= read_align(vdst.offset,alignment); |
463 | 0 | if((stat = copy_datar(ncid,basetype,&vsrc,&vdst))) goto done; |
464 | 0 | } |
465 | 0 | } |
466 | | /* Move into place */ |
467 | 0 | memcpy(dst->memory+dst->offset,©,sizeof(nc_vlen_t)); |
468 | 0 | src->offset += sizeof(nc_vlen_t); |
469 | 0 | dst->offset += sizeof(nc_vlen_t); |
470 | |
|
471 | 0 | done: |
472 | 0 | if(stat) { |
473 | 0 | nullfree(copy.p); |
474 | 0 | } |
475 | 0 | return stat; |
476 | 0 | } |
477 | | |
478 | | static int |
479 | | copy_enum(int ncid, nc_type xtype, nc_type basetype, Position* src, Position* dst) |
480 | 0 | { |
481 | 0 | int stat = NC_NOERR; |
482 | 0 | abort(); |
483 | | /* basically same as an instance of the enum's integer basetype */ |
484 | 0 | stat = copy_datar(ncid,basetype,src,dst); |
485 | 0 | return stat; |
486 | 0 | } |
487 | | |
488 | | static int |
489 | | copy_opaque(int ncid, nc_type xtype, size_t size, Position* src, Position* dst) |
490 | 0 | { |
491 | 0 | abort(); |
492 | | /* basically a fixed size sequence of bytes */ |
493 | 0 | memcpy(dst->memory+dst->offset,src->memory+src->offset,size); |
494 | 0 | src->offset += size; |
495 | 0 | dst->offset += size; |
496 | 0 | return NC_NOERR; |
497 | 0 | } |
498 | | |
499 | | static int |
500 | | copy_compound(int ncid, nc_type xtype, size_t size, size_t nfields, Position* src, Position* dst) |
501 | 0 | { |
502 | 0 | int stat = NC_NOERR; |
503 | 0 | size_t fid, i, arraycount; |
504 | 0 | ptrdiff_t savesrcoffset, savedstoffset; |
505 | 0 | int ndims; |
506 | 0 | int dimsizes[NC_MAX_VAR_DIMS]; |
507 | |
|
508 | 0 | savesrcoffset = src->offset; |
509 | 0 | savedstoffset = dst->offset; |
510 | | |
511 | | /* Get info about each field in turn and copy it */ |
512 | 0 | for(fid=0;fid<nfields;fid++) { |
513 | 0 | size_t fieldoffset; |
514 | 0 | nc_type fieldtype; |
515 | 0 | char name[NC_MAX_NAME]; |
516 | | |
517 | | /* Get all relevant info about the field */ |
518 | 0 | if((stat = nc_inq_compound_field(ncid,xtype,fid,name,&fieldoffset,&fieldtype,&ndims,dimsizes))) goto done; |
519 | | |
520 | 0 | if(ndims == 0) {ndims=1; dimsizes[0]=1;} /* fake the scalar case */ |
521 | | /* Set offset for this field */ |
522 | | #ifdef DEBUG |
523 | | fprintf(stderr,"before: offset = %d after: offset = %d\n",(int)src->offset,(int)(savesrcoffset+fieldoffset)); |
524 | | #endif |
525 | 0 | src->offset = savesrcoffset+fieldoffset; |
526 | 0 | dst->offset = savedstoffset+fieldoffset; |
527 | | #ifdef DEBUG |
528 | | fprintf(stderr,"field %s(%d) = %d\n",name,(int)fieldoffset,(int)src->offset); |
529 | | #endif |
530 | | /* compute the total number of elements in the field array */ |
531 | 0 | arraycount = 1; |
532 | 0 | for(i=0;i<ndims;i++) arraycount *= dimsizes[i]; |
533 | 0 | for(i=0;i<arraycount;i++) { |
534 | 0 | if((stat = copy_datar(ncid, fieldtype, src, dst))) goto done; |
535 | 0 | } |
536 | | #ifdef DEBUG |
537 | | fprintf(stderr,"src=(%d,%p)\n",(int)src->offset,src->memory); |
538 | | #endif |
539 | 0 | } |
540 | | #ifdef DEBUG |
541 | | fprintf(stderr,"\n"); |
542 | | #endif |
543 | | /* Return to beginning of the compound and move |compound| */ |
544 | 0 | src->offset = savesrcoffset; |
545 | 0 | dst->offset = savedstoffset; |
546 | 0 | src->offset += size; |
547 | 0 | dst->offset += size; |
548 | |
|
549 | 0 | done: |
550 | 0 | return stat; |
551 | 0 | } |
552 | | #endif |
553 | | |
554 | | /**************************************************/ |
555 | | /* Alignment functions */ |
556 | | |
557 | | #ifdef USE_NETCDF4 |
558 | | static ptrdiff_t |
559 | | read_align(ptrdiff_t offset, size_t alignment) |
560 | 0 | { |
561 | 0 | size_t loc_align = (alignment == 0 ? 1 : alignment); |
562 | 0 | size_t delta = (offset % loc_align); |
563 | 0 | if(delta == 0) return offset; |
564 | 0 | return offset + (alignment - delta); |
565 | 0 | } |
566 | | |
567 | | |
568 | | /** |
569 | | @param ncid - only needed for a compound type |
570 | | @param xtype - type for which alignment is requested |
571 | | @return 0 if not found |
572 | | */ |
573 | | int |
574 | | NC_type_alignment(int ncid, nc_type xtype, size_t* alignp) |
575 | 0 | { |
576 | 0 | int stat = NC_NOERR; |
577 | 0 | size_t align = 0; |
578 | 0 | int klass; |
579 | |
|
580 | 0 | if(!type_alignment_initialized) { |
581 | 0 | NC_compute_alignments(); |
582 | 0 | type_alignment_initialized = 1; |
583 | 0 | } |
584 | 0 | if(xtype <= NC_MAX_ATOMIC_TYPE) |
585 | 0 | {stat = NC_class_alignment(xtype,&align); goto done;} |
586 | 0 | else {/* Presumably a user type */ |
587 | 0 | if((stat = NC_inq_any_type(ncid,xtype,NULL,NULL,NULL,NULL,&klass))) goto done; |
588 | 0 | switch(klass) { |
589 | 0 | case NC_VLEN: stat = NC_class_alignment(klass,&align); break; |
590 | 0 | case NC_OPAQUE: stat = NC_class_alignment(klass,&align); break; |
591 | 0 | case NC_COMPOUND: {/* get alignment of the first field of the compound */ |
592 | 0 | nc_type fieldtype; |
593 | | /* Get all relevant info about the first field */ |
594 | 0 | if((stat = nc_inq_compound_field(ncid,xtype,0,NULL,NULL,&fieldtype,NULL,NULL))) goto done; |
595 | 0 | stat = NC_type_alignment(ncid,fieldtype,&align); /* may recurse repeatedly */ |
596 | 0 | } break; |
597 | 0 | default: break; |
598 | 0 | } |
599 | 0 | } |
600 | 0 | if(alignp) *alignp = align; |
601 | |
|
602 | 0 | done: |
603 | | #if 0 |
604 | | Why was this here? |
605 | | if(stat == NC_NOERR && align == 0) stat = NC_EINVAL; |
606 | | #endif |
607 | 0 | return stat; |
608 | 0 | } |
609 | | #endif |
610 | | |
611 | | /**************************************************/ |
612 | | /* Dump an instance into a bytebuffer |
613 | | |
614 | | @param ncid root id |
615 | | @param xtype type id |
616 | | @param memory ptr to top-level memory to dump |
617 | | @param count number of instances of the type in memory block |
618 | | @return error code |
619 | | */ |
620 | | |
621 | | int |
622 | | nc_dump_data(int ncid, nc_type xtype, void* memory, size_t count, char** bufp) |
623 | 0 | { |
624 | 0 | int stat = NC_NOERR; |
625 | 0 | size_t i; |
626 | 0 | Position offset; |
627 | 0 | NCbytes* buf = ncbytesnew(); |
628 | |
|
629 | 0 | if(ncid < 0 || xtype <= 0) |
630 | 0 | {stat = NC_EINVAL; goto done;} |
631 | 0 | if(memory == NULL && count > 0) |
632 | 0 | {stat = NC_EINVAL; goto done;} |
633 | 0 | if(memory == NULL || count == 0) |
634 | 0 | goto done; /* ok, do nothing */ |
635 | | #ifdef REPORT |
636 | | fprintf(stderr,">>> dump: memory=%p count=%lu ncid=%d xtype=%d\n",memory,(unsigned long)count,ncid,xtype); |
637 | | #endif |
638 | 0 | offset.memory = (char*)memory; /* use char* so we can do pointer arithmetic */ |
639 | 0 | offset.offset = 0; |
640 | 0 | for(i=0;i<count;i++) { |
641 | 0 | if(i > 0) ncbytescat(buf," "); |
642 | 0 | if((stat=dump_datar(ncid,xtype,&offset,buf))) /* dump one instance */ |
643 | 0 | break; |
644 | 0 | } |
645 | |
|
646 | 0 | if(bufp) *bufp = ncbytesextract(buf); |
647 | |
|
648 | 0 | done: |
649 | 0 | ncbytesfree(buf); |
650 | 0 | return stat; |
651 | 0 | } |
652 | | |
653 | | int |
654 | | nc_print_data(int ncid, nc_type xtype, void* memory, size_t count) |
655 | 0 | { |
656 | 0 | char* s = NULL; |
657 | 0 | int stat = NC_NOERR; |
658 | 0 | if((stat=nc_dump_data(ncid,xtype,memory,count,&s))) return stat; |
659 | 0 | fprintf(stderr,"%s\n",s); |
660 | 0 | nullfree(s) |
661 | 0 | return stat; |
662 | 0 | } |
663 | | |
664 | | /* Recursive type walker: dump a single instance */ |
665 | | static int |
666 | | dump_datar(int ncid, nc_type xtype, Position* offset, NCbytes* buf) |
667 | 0 | { |
668 | 0 | int stat = NC_NOERR; |
669 | 0 | size_t xsize; |
670 | 0 | nc_type basetype; |
671 | 0 | size_t nfields; |
672 | 0 | int klass; |
673 | 0 | char s[128]; |
674 | | |
675 | | /* Get relevant type info */ |
676 | 0 | if((stat = NC_inq_any_type(ncid,xtype,NULL,&xsize,&basetype,&nfields,&klass))) goto done; |
677 | | |
678 | 0 | switch (xtype) { |
679 | 0 | case NC_CHAR: |
680 | 0 | snprintf(s,sizeof(s),"'%c'",*(char*)(offset->memory+offset->offset)); |
681 | 0 | ncbytescat(buf,s); |
682 | 0 | break; |
683 | 0 | case NC_BYTE: |
684 | 0 | snprintf(s,sizeof(s),"%d",*(char*)(offset->memory+offset->offset)); |
685 | 0 | ncbytescat(buf,s); |
686 | 0 | break; |
687 | 0 | case NC_UBYTE: |
688 | 0 | snprintf(s,sizeof(s),"%u",*(unsigned char*)(offset->memory+offset->offset)); |
689 | 0 | ncbytescat(buf,s); |
690 | 0 | break; |
691 | 0 | case NC_SHORT: |
692 | 0 | snprintf(s,sizeof(s),"%d",*(short*)(offset->memory+offset->offset)); |
693 | 0 | ncbytescat(buf,s); |
694 | 0 | break; |
695 | 0 | case NC_USHORT: |
696 | 0 | snprintf(s,sizeof(s),"%d",*(unsigned short*)(offset->memory+offset->offset)); |
697 | 0 | ncbytescat(buf,s); |
698 | 0 | break; |
699 | 0 | case NC_INT: |
700 | 0 | snprintf(s,sizeof(s),"%d",*(int*)(offset->memory+offset->offset)); |
701 | 0 | ncbytescat(buf,s); |
702 | 0 | break; |
703 | 0 | case NC_UINT: |
704 | 0 | snprintf(s,sizeof(s),"%d",*(unsigned int*)(offset->memory+offset->offset)); |
705 | 0 | ncbytescat(buf,s); |
706 | 0 | break; |
707 | 0 | case NC_FLOAT: |
708 | 0 | snprintf(s,sizeof(s),"%f",*(float*)(offset->memory+offset->offset)); |
709 | 0 | ncbytescat(buf,s); |
710 | 0 | break; |
711 | 0 | case NC_INT64: |
712 | 0 | snprintf(s,sizeof(s),"%lld",*(long long*)(offset->memory+offset->offset)); |
713 | 0 | ncbytescat(buf,s); |
714 | 0 | break; |
715 | 0 | case NC_UINT64: |
716 | 0 | snprintf(s,sizeof(s),"%llu",*(unsigned long long*)(offset->memory+offset->offset)); |
717 | 0 | ncbytescat(buf,s); |
718 | 0 | break; |
719 | 0 | case NC_DOUBLE: |
720 | 0 | snprintf(s,sizeof(s),"%lf",*(double*)(offset->memory+offset->offset)); |
721 | 0 | ncbytescat(buf,s); |
722 | 0 | break; |
723 | 0 | #ifdef USE_NETCDF4 |
724 | 0 | case NC_STRING: { |
725 | 0 | char* s = *(char**)(offset->memory + offset->offset); |
726 | 0 | ncbytescat(buf,"\""); |
727 | 0 | ncbytescat(buf,s); |
728 | 0 | ncbytescat(buf,"\""); |
729 | 0 | } break; |
730 | 0 | #endif |
731 | 0 | default: |
732 | 0 | #ifdef USE_NETCDF4 |
733 | | /* dump a user type */ |
734 | 0 | switch (klass) { |
735 | 0 | case NC_OPAQUE: stat = dump_opaque(ncid,xtype,xsize,offset,buf); break; |
736 | 0 | case NC_ENUM: stat = dump_enum(ncid,xtype,basetype,offset,buf); break; |
737 | 0 | case NC_COMPOUND: stat = dump_compound(ncid,xtype,xsize,nfields,offset,buf); break; |
738 | 0 | case NC_VLEN: stat = dump_vlen(ncid,xtype,basetype,offset,buf); break; |
739 | 0 | default: stat = NC_EBADTYPE; break; |
740 | 0 | } |
741 | | #else |
742 | | stat = NC_EBADTYPE; |
743 | | #endif |
744 | 0 | break; |
745 | 0 | } |
746 | 0 | if(xtype <= NC_MAX_ATOMIC_TYPE) |
747 | 0 | offset->offset += xsize; |
748 | |
|
749 | 0 | done: |
750 | 0 | return stat; |
751 | 0 | } |
752 | | |
753 | | #ifdef USE_NETCDF4 |
754 | | static int |
755 | | dump_vlen(int ncid, nc_type xtype, nc_type basetype, Position* offset, NCbytes* buf) |
756 | 0 | { |
757 | 0 | int stat = NC_NOERR; |
758 | 0 | size_t i; |
759 | 0 | nc_vlen_t* vl = (nc_vlen_t*)(offset->memory+offset->offset); |
760 | 0 | char s[128]; |
761 | |
|
762 | 0 | if(vl->len > 0 && vl->p == NULL) |
763 | 0 | {stat = NC_EINVAL; goto done;} |
764 | | |
765 | 0 | snprintf(s,sizeof(s),"{len=%u,p=(",(unsigned)vl->len); |
766 | 0 | ncbytescat(buf,s); |
767 | | /* dump each entry in the vlen list */ |
768 | 0 | if(vl->len > 0) { |
769 | 0 | Position voffset; |
770 | 0 | size_t alignment = 0; |
771 | 0 | if((stat = NC_type_alignment(ncid,basetype,&alignment))) goto done;; |
772 | 0 | voffset.memory = vl->p; |
773 | 0 | voffset.offset = 0; |
774 | 0 | for(i=0;i<vl->len;i++) { |
775 | 0 | if(i > 0) ncbytescat(buf," "); |
776 | 0 | voffset.offset = read_align(voffset.offset,alignment); |
777 | 0 | if((stat = dump_datar(ncid,basetype,&voffset,buf))) goto done; |
778 | 0 | } |
779 | 0 | } |
780 | 0 | ncbytescat(buf,")}"); |
781 | 0 | offset->offset += sizeof(nc_vlen_t); |
782 | | |
783 | 0 | done: |
784 | 0 | return stat; |
785 | 0 | } |
786 | | |
787 | | static int |
788 | | dump_enum(int ncid, nc_type xtype, nc_type basetype, Position* offset, NCbytes* buf) |
789 | 0 | { |
790 | 0 | int stat = NC_NOERR; |
791 | | |
792 | | /* basically same as an instance of the enum's integer basetype */ |
793 | 0 | stat = dump_datar(ncid,basetype,offset,buf); |
794 | 0 | return stat; |
795 | 0 | } |
796 | | |
797 | | static int |
798 | | dump_opaque(int ncid, nc_type xtype, size_t size, Position* offset, NCbytes* buf) |
799 | 0 | { |
800 | 0 | size_t i; |
801 | 0 | char sx[16]; |
802 | | /* basically a fixed size sequence of bytes */ |
803 | 0 | ncbytescat(buf,"|"); |
804 | 0 | for(i=0;i<size;i++) { |
805 | 0 | unsigned char x = *(offset->memory+offset->offset+i); |
806 | 0 | snprintf(sx,sizeof(sx),"%2x",x); |
807 | 0 | ncbytescat(buf,sx); |
808 | 0 | } |
809 | 0 | ncbytescat(buf,"|"); |
810 | 0 | offset->offset += size; |
811 | 0 | return NC_NOERR; |
812 | 0 | } |
813 | | |
814 | | static int |
815 | | dump_compound(int ncid, nc_type xtype, size_t size, size_t nfields, Position* offset, NCbytes* buf) |
816 | 0 | { |
817 | 0 | int stat = NC_NOERR; |
818 | 0 | size_t fid, i, arraycount; |
819 | 0 | ptrdiff_t saveoffset; |
820 | 0 | int ndims; |
821 | 0 | int dimsizes[NC_MAX_VAR_DIMS]; |
822 | |
|
823 | 0 | saveoffset = offset->offset; |
824 | |
|
825 | 0 | ncbytescat(buf,"<"); |
826 | | |
827 | | /* Get info about each field in turn and dump it */ |
828 | 0 | for(fid=0;fid<nfields;fid++) { |
829 | 0 | size_t fieldalignment; |
830 | 0 | nc_type fieldtype; |
831 | 0 | char name[NC_MAX_NAME]; |
832 | 0 | char sd[128]; |
833 | | |
834 | | /* Get all relevant info about the field */ |
835 | 0 | if((stat = nc_inq_compound_field(ncid,xtype,fid,name,&fieldalignment,&fieldtype,&ndims,dimsizes))) goto done; |
836 | 0 | if(fid > 0) ncbytescat(buf,";"); |
837 | 0 | ncbytescat(buf,name); |
838 | 0 | if(ndims > 0) { |
839 | 0 | int j; |
840 | 0 | for(j=0;j<ndims;j++) { |
841 | 0 | snprintf(sd,sizeof(sd),"[%d]",(int)dimsizes[j]); |
842 | 0 | ncbytescat(buf,sd); |
843 | 0 | } |
844 | 0 | } |
845 | 0 | if(ndims == 0) {ndims=1; dimsizes[0]=1;} /* fake the scalar case */ |
846 | | /* Align to this field */ |
847 | 0 | offset->offset = saveoffset + fieldalignment; |
848 | | /* compute the total number of elements in the field array */ |
849 | 0 | arraycount = 1; |
850 | 0 | for(i=0;i<ndims;i++) arraycount *= dimsizes[i]; |
851 | 0 | for(i=0;i<arraycount;i++) { |
852 | 0 | if(i > 0) ncbytescat(buf," "); |
853 | 0 | if((stat = dump_datar(ncid, fieldtype, offset,buf))) goto done; |
854 | 0 | } |
855 | 0 | } |
856 | 0 | ncbytescat(buf,">"); |
857 | | /* Return to beginning of the compound and move |compound| */ |
858 | 0 | offset->offset = saveoffset; |
859 | 0 | offset->offset += size; |
860 | |
|
861 | 0 | done: |
862 | 0 | return stat; |
863 | 0 | } |
864 | | #endif |
865 | | |
866 | | /* Extended version that can handle atomic typeids */ |
867 | | int |
868 | | NC_inq_any_type(int ncid, nc_type typeid, char *name, size_t *size, |
869 | | nc_type *basetypep, size_t *nfieldsp, int *classp) |
870 | 0 | { |
871 | 0 | int stat = NC_NOERR; |
872 | 0 | #ifdef USE_NETCDF4 |
873 | 0 | if(typeid >= NC_FIRSTUSERTYPEID) { |
874 | 0 | stat = nc_inq_user_type(ncid,typeid,name,size,basetypep,nfieldsp,classp); |
875 | 0 | } else |
876 | 0 | #endif |
877 | 0 | if(typeid > NC_NAT && typeid <= NC_MAX_ATOMIC_TYPE) { |
878 | 0 | if(basetypep) *basetypep = NC_NAT; |
879 | 0 | if(nfieldsp) *nfieldsp = 0; |
880 | 0 | if(classp) *classp = typeid; |
881 | 0 | stat = NC4_inq_atomic_type(typeid,name,size); |
882 | 0 | } else |
883 | 0 | stat = NC_EBADTYPE; |
884 | 0 | return stat; |
885 | 0 | } |