1 /*********************************************************************
2    Copyright 2018, UCAR/Unidata See netcdf/COPYRIGHT file for
3    copying and redistribution conditions.
4 
5    $Id: nc3dispatch.c,v 2.8 2010/05/26 11:11:26 ed Exp $
6  *********************************************************************/
7 
8 #include "config.h"
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include "netcdf.h"
13 #include "nc3internal.h"
14 #include "nc3dispatch.h"
15 
16 #ifndef NC_CONTIGUOUS
17 #define NC_CONTIGUOUS 1
18 #endif
19 
20 #ifndef NC_ENOTNC4
21 #define NC_ENOTNC4 (-111)
22 #endif
23 
24 #ifndef NC_ENOGRP
25 #define NC_ENOGRP (-125)
26 #endif
27 
28 #ifndef NC_STRING
29 #define NC_STRING (12)
30 #endif
31 
32 
33 static int NC3_inq_var_all(int ncid, int varid, char *name, nc_type *xtypep,
34                int *ndimsp, int *dimidsp, int *nattsp,
35                int *shufflep, int *deflatep, int *deflate_levelp,
36                int *fletcher32p, int *contiguousp, size_t *chunksizesp,
37                int *no_fill, void *fill_valuep, int *endiannessp,
38 	       unsigned int* idp, size_t* nparamsp, unsigned int* params
39                );
40 
41 static int NC3_var_par_access(int,int,int);
42 
43 static int NC3_show_metadata(int);
44 static int NC3_inq_unlimdims(int,int*,int*);
45 static int NC3_inq_ncid(int,const char*,int*);
46 static int NC3_inq_grps(int,int*,int*);
47 static int NC3_inq_grpname(int,char*);
48 static int NC3_inq_grpname_full(int,size_t*,char*);
49 static int NC3_inq_grp_parent(int,int*);
50 static int NC3_inq_grp_full_ncid(int,const char*,int*);
51 static int NC3_inq_varids(int,int* nvars,int*);
52 static int NC3_inq_dimids(int,int* ndims,int*,int);
53 static int NC3_inq_typeids(int,int* ntypes,int*);
54 static int NC3_inq_type_equal(int,nc_type,int,nc_type,int*);
55 static int NC3_def_grp(int,const char*,int*);
56 static int NC3_rename_grp(int,const char*);
57 static int NC3_inq_user_type(int,nc_type,char*,size_t*,nc_type*,size_t*,int*);
58 static int NC3_inq_typeid(int,const char*,nc_type*);
59 static int NC3_def_compound(int,size_t,const char*,nc_type*);
60 static int NC3_insert_compound(int,nc_type,const char*,size_t,nc_type);
61 static int NC3_insert_array_compound(int,nc_type,const char*,size_t,nc_type,int,const int*);
62 static int NC3_inq_compound_field(int,nc_type,int,char*,size_t*,nc_type*,int*,int*);
63 static int NC3_inq_compound_fieldindex(int,nc_type,const char*,int*);
64 static int NC3_def_vlen(int,const char*,nc_type base_typeid,nc_type*);
65 static int NC3_put_vlen_element(int,int,void*,size_t,const void*);
66 static int NC3_get_vlen_element(int,int,const void*,size_t*,void*);
67 static int NC3_def_enum(int,nc_type,const char*,nc_type*);
68 static int NC3_insert_enum(int,nc_type,const char*,const void*);
69 static int NC3_inq_enum_member(int,nc_type,int,char*,void*);
70 static int NC3_inq_enum_ident(int,nc_type,long long,char*);
71 static int NC3_def_opaque(int,size_t,const char*,nc_type*);
72 static int NC3_def_var_deflate(int,int,int,int,int);
73 static int NC3_def_var_fletcher32(int,int,int);
74 static int NC3_def_var_chunking(int,int,int,const size_t*);
75 static int NC3_def_var_endian(int,int,int);
76 static int NC3_def_var_filter(int, int, unsigned int, size_t, const unsigned int*);
77 
78 static int NC3_set_var_chunk_cache(int,int,size_t,size_t,float);
79 static int NC3_get_var_chunk_cache(int,int,size_t*,size_t*,float*);
80 
81 static const NC_Dispatch NC3_dispatcher = {
82 
83 NC_FORMATX_NC3,
84 NC_DISPATCH_VERSION,
85 NC3_create,
86 NC3_open,
87 
88 NC3_redef,
89 NC3__enddef,
90 NC3_sync,
91 NC3_abort,
92 NC3_close,
93 NC3_set_fill,
94 NC3_inq_format,
95 NC3_inq_format_extended,
96 
97 NC3_inq,
98 NC3_inq_type,
99 
100 NC3_def_dim,
101 NC3_inq_dimid,
102 NC3_inq_dim,
103 NC3_inq_unlimdim,
104 NC3_rename_dim,
105 
106 NC3_inq_att,
107 NC3_inq_attid,
108 NC3_inq_attname,
109 NC3_rename_att,
110 NC3_del_att,
111 NC3_get_att,
112 NC3_put_att,
113 
114 NC3_def_var,
115 NC3_inq_varid,
116 NC3_rename_var,
117 NC3_get_vara,
118 NC3_put_vara,
119 NCDEFAULT_get_vars,
120 NCDEFAULT_put_vars,
121 NCDEFAULT_get_varm,
122 NCDEFAULT_put_varm,
123 
124 NC3_inq_var_all,
125 
126 NC3_var_par_access,
127 NC3_def_var_fill,
128 
129 NC3_show_metadata,
130 NC3_inq_unlimdims,
131 NC3_inq_ncid,
132 NC3_inq_grps,
133 NC3_inq_grpname,
134 NC3_inq_grpname_full,
135 NC3_inq_grp_parent,
136 NC3_inq_grp_full_ncid,
137 NC3_inq_varids,
138 NC3_inq_dimids,
139 NC3_inq_typeids,
140 NC3_inq_type_equal,
141 NC3_def_grp,
142 NC3_rename_grp,
143 NC3_inq_user_type,
144 NC3_inq_typeid,
145 
146 NC3_def_compound,
147 NC3_insert_compound,
148 NC3_insert_array_compound,
149 NC3_inq_compound_field,
150 NC3_inq_compound_fieldindex,
151 NC3_def_vlen,
152 NC3_put_vlen_element,
153 NC3_get_vlen_element,
154 NC3_def_enum,
155 NC3_insert_enum,
156 NC3_inq_enum_member,
157 NC3_inq_enum_ident,
158 NC3_def_opaque,
159 NC3_def_var_deflate,
160 NC3_def_var_fletcher32,
161 NC3_def_var_chunking,
162 NC3_def_var_endian,
163 NC3_def_var_filter,
164 NC3_set_var_chunk_cache,
165 NC3_get_var_chunk_cache,
166 
167 NC_NOTNC4_filter_actions,
168 };
169 
170 const NC_Dispatch* NC3_dispatch_table = NULL; /*!< NC3 Dispatch table, moved here from ddispatch.c */
171 
172 int
NC3_initialize(void)173 NC3_initialize(void)
174 {
175     NC3_dispatch_table = &NC3_dispatcher;
176     return NC_NOERR;
177 }
178 
179 int
NC3_finalize(void)180 NC3_finalize(void)
181 {
182     return NC_NOERR;
183 }
184 
185 static int
NC3_inq_var_all(int ncid,int varid,char * name,nc_type * xtypep,int * ndimsp,int * dimidsp,int * nattsp,int * shufflep,int * deflatep,int * deflate_levelp,int * fletcher32p,int * contiguousp,size_t * chunksizesp,int * no_fill,void * fill_valuep,int * endiannessp,unsigned int * idp,size_t * nparamsp,unsigned int * params)186 NC3_inq_var_all(int ncid, int varid, char *name, nc_type *xtypep,
187                int *ndimsp, int *dimidsp, int *nattsp,
188                int *shufflep, int *deflatep, int *deflate_levelp,
189                int *fletcher32p, int *contiguousp, size_t *chunksizesp,
190                int *no_fill, void *fill_valuep, int *endiannessp,
191 	       unsigned int* idp, size_t* nparamsp, unsigned int* params
192 	       )
193 {
194     int stat = NC3_inq_var(ncid,varid,name,xtypep,ndimsp,dimidsp,nattsp,no_fill,fill_valuep);
195     if(stat) return stat;
196     if(shufflep) *shufflep = 0;
197     if(deflatep) *deflatep = 0;
198     if(fletcher32p) *fletcher32p = 0;
199     if(contiguousp) *contiguousp = NC_CONTIGUOUS;
200     if(endiannessp) return NC_ENOTNC4;
201     if(idp) return NC_ENOTNC4;
202     if(nparamsp) return NC_ENOTNC4;
203     if(params) return NC_ENOTNC4;
204     return NC_NOERR;
205 }
206 
207 static int
NC3_var_par_access(int ncid,int varid,int par_access)208 NC3_var_par_access(int ncid, int varid, int par_access)
209 {
210     return NC_NOERR; /* no-op for netcdf classic */
211 }
212 
213 static int
NC3_inq_unlimdims(int ncid,int * ndimsp,int * unlimdimidsp)214 NC3_inq_unlimdims(int ncid, int *ndimsp, int *unlimdimidsp)
215 {
216     int retval;
217     int unlimid;
218 
219     if ((retval = NC3_inq_unlimdim(ncid, &unlimid)))
220         return retval;
221     if (unlimid != -1) {
222         if(ndimsp) *ndimsp = 1;
223         if (unlimdimidsp)
224             unlimdimidsp[0] = unlimid;
225     } else
226         if(ndimsp) *ndimsp = 0;
227     return NC_NOERR;
228 }
229 
230 static int
NC3_def_grp(int parent_ncid,const char * name,int * new_ncid)231 NC3_def_grp(int parent_ncid, const char *name, int *new_ncid)
232 {
233     return NC_ENOTNC4;
234 }
235 
236 static int
NC3_rename_grp(int grpid,const char * name)237 NC3_rename_grp(int grpid, const char *name)
238 {
239     return NC_ENOTNC4;
240 }
241 
242 static int
NC3_inq_ncid(int ncid,const char * name,int * grp_ncid)243 NC3_inq_ncid(int ncid, const char *name, int *grp_ncid)
244 {
245   if(grp_ncid) *grp_ncid = ncid;
246     return NC_NOERR;
247 }
248 
249 static int
NC3_inq_grps(int ncid,int * numgrps,int * ncids)250 NC3_inq_grps(int ncid, int *numgrps, int *ncids)
251 {
252   if (numgrps)
253        *numgrps = 0;
254     return NC_NOERR;
255 }
256 
257 static int
NC3_inq_grpname(int ncid,char * name)258 NC3_inq_grpname(int ncid, char *name)
259 {
260     if (name)
261         strcpy(name, "/");
262     return NC_NOERR;
263 }
264 
265 static int
NC3_inq_grpname_full(int ncid,size_t * lenp,char * full_name)266 NC3_inq_grpname_full(int ncid, size_t *lenp, char *full_name)
267 {
268     if (full_name)
269         strcpy(full_name, "/");
270     if(lenp) *lenp = 1;
271     return NC_NOERR;
272 }
273 
274 static int
NC3_inq_grp_parent(int ncid,int * parent_ncid)275 NC3_inq_grp_parent(int ncid, int *parent_ncid)
276 {
277     return NC_ENOGRP;
278 }
279 
280 static int
NC3_inq_grp_full_ncid(int ncid,const char * full_name,int * grp_ncid)281 NC3_inq_grp_full_ncid(int ncid, const char *full_name, int *grp_ncid)
282 {
283     return NC_ENOGRP;
284 }
285 
286 static int
NC3_inq_varids(int ncid,int * nvarsp,int * varids)287 NC3_inq_varids(int ncid, int *nvarsp, int *varids)
288 {
289     int retval,v,nvars;
290     /* This is a netcdf-3 file, there is only one group, the root
291         group, and its vars have ids 0 thru nvars - 1. */
292     if ((retval = NC3_inq(ncid, NULL, &nvars, NULL, NULL)))
293         return retval;
294     if(nvarsp) *nvarsp = nvars;
295     if (varids)
296         for (v = 0; v < nvars; v++)
297             varids[v] = v;
298     return NC_NOERR;
299 }
300 
301 static int
NC3_inq_dimids(int ncid,int * ndimsp,int * dimids,int include_parents)302 NC3_inq_dimids(int ncid, int *ndimsp, int *dimids, int include_parents)
303 {
304     int retval,d,ndims;
305     /* If this is a netcdf-3 file, then the dimids are going to be 0
306        thru ndims-1, so just provide them. */
307     if ((retval = NC3_inq(ncid, &ndims,  NULL, NULL, NULL)))
308         return retval;
309     if(ndimsp) *ndimsp = ndims;
310     if (dimids)
311         for (d = 0; d < ndims; d++)
312             dimids[d] = d;
313     return NC_NOERR;
314 }
315 
316 static int
NC3_show_metadata(int ncid)317 NC3_show_metadata(int ncid)
318 {
319     return NC_NOERR;
320 }
321 
322 static int
NC3_inq_type_equal(int ncid1,nc_type typeid1,int ncid2,nc_type typeid2,int * equalp)323 NC3_inq_type_equal(int ncid1, nc_type typeid1, int ncid2, nc_type typeid2, int* equalp)
324 {
325     /* Check input. */
326     if(equalp == NULL) return NC_NOERR;
327 
328     if (typeid1 <= NC_NAT || typeid2 <= NC_NAT)
329        return NC_EINVAL;
330 
331     *equalp = 0; /* assume */
332 
333     /* If one is atomic, and the other user-defined, the types are not equal */
334     if ((typeid1 <= NC_STRING && typeid2 > NC_STRING) ||
335         (typeid2 <= NC_STRING && typeid1 > NC_STRING)) {
336         if (equalp) *equalp = 0;
337         return NC_NOERR;
338     }
339 
340     /* If both are atomic types, the answer is easy. */
341     if (typeid1 <= ATOMICTYPEMAX3) {
342         if (equalp) {
343             if (typeid1 == typeid2)
344                 *equalp = 1;
345             else
346                 *equalp = 0;
347         }
348         return NC_NOERR;
349     }
350     return NC_NOERR;
351 }
352 
353 static int
NC3_inq_typeid(int ncid,const char * name,nc_type * typeidp)354 NC3_inq_typeid(int ncid, const char *name, nc_type *typeidp)
355 {
356     int i;
357     for (i = 0; i <= ATOMICTYPEMAX3; i++)
358         if (!strcmp(name, NC_atomictypename(i))) {
359             if (typeidp) *typeidp = i;
360                 return NC_NOERR;
361         }
362     return NC_ENOTNC4;
363 }
364 
365 static int
NC3_inq_typeids(int ncid,int * ntypes,int * typeids)366 NC3_inq_typeids(int ncid, int *ntypes, int *typeids)
367 {
368     if(ntypes) *ntypes = 0;
369     return NC_NOERR;
370 }
371 
372 static int
NC3_inq_user_type(int ncid,nc_type typeid,char * name,size_t * size,nc_type * base_nc_typep,size_t * nfieldsp,int * classp)373 NC3_inq_user_type(int ncid, nc_type typeid, char *name, size_t *size,
374 		 nc_type *base_nc_typep, size_t *nfieldsp, int *classp)
375 {
376     return NC_ENOTNC4;
377 }
378 
379 static int
NC3_def_compound(int ncid,size_t size,const char * name,nc_type * typeidp)380 NC3_def_compound(int ncid, size_t size, const char *name, nc_type *typeidp)
381 {
382     return NC_ENOTNC4;
383 }
384 
385 static int
NC3_insert_compound(int ncid,nc_type typeid,const char * name,size_t offset,nc_type field_typeid)386 NC3_insert_compound(int ncid, nc_type typeid, const char *name, size_t offset,
387                     nc_type field_typeid)
388 {
389     return NC_ENOTNC4;
390 }
391 
392 static int
NC3_insert_array_compound(int ncid,nc_type typeid,const char * name,size_t offset,nc_type field_typeid,int ndims,const int * dim_sizes)393 NC3_insert_array_compound(int ncid, nc_type typeid, const char *name,
394 			 size_t offset, nc_type field_typeid,
395 			 int ndims, const int *dim_sizes)
396 {
397     return NC_ENOTNC4;
398 }
399 
400 
401 static int
NC3_inq_compound_field(int ncid,nc_type typeid,int fieldid,char * name,size_t * offsetp,nc_type * field_typeidp,int * ndimsp,int * dim_sizesp)402 NC3_inq_compound_field(int ncid, nc_type typeid, int fieldid, char *name,
403 		      size_t *offsetp, nc_type *field_typeidp, int *ndimsp,
404 		      int *dim_sizesp)
405 {
406     return NC_ENOTNC4;
407 }
408 
409 static int
NC3_inq_compound_fieldindex(int ncid,nc_type typeid,const char * name,int * fieldidp)410 NC3_inq_compound_fieldindex(int ncid, nc_type typeid, const char *name, int *fieldidp)
411 {
412     return NC_ENOTNC4;
413 }
414 
415 static int
NC3_def_opaque(int ncid,size_t datum_size,const char * name,nc_type * xtypep)416 NC3_def_opaque(int ncid, size_t datum_size, const char *name, nc_type* xtypep)
417 {
418     return NC_ENOTNC4;
419 }
420 
421 static int
NC3_def_vlen(int ncid,const char * name,nc_type base_typeid,nc_type * xtypep)422 NC3_def_vlen(int ncid, const char *name, nc_type base_typeid, nc_type* xtypep)
423 {
424     return NC_ENOTNC4;
425 }
426 
427 static int
NC3_def_enum(int ncid,nc_type base_typeid,const char * name,nc_type * typeidp)428 NC3_def_enum(int ncid, nc_type base_typeid, const char *name,
429 	    nc_type *typeidp)
430 {
431     return NC_ENOTNC4;
432 }
433 
434 static int
NC3_inq_enum_ident(int ncid,nc_type xtype,long long value,char * identifier)435 NC3_inq_enum_ident(int ncid, nc_type xtype, long long value, char *identifier)
436 {
437     return NC_ENOTNC4;
438 }
439 
440 static int
NC3_inq_enum_member(int ncid,nc_type typeid,int idx,char * identifier,void * value)441 NC3_inq_enum_member(int ncid, nc_type typeid, int idx, char *identifier,
442 		   void *value)
443 {
444     return NC_ENOTNC4;
445 }
446 
447 static int
NC3_insert_enum(int ncid,nc_type typeid,const char * identifier,const void * value)448 NC3_insert_enum(int ncid, nc_type typeid, const char *identifier,
449 	       const void *value)
450 {
451     return NC_ENOTNC4;
452 }
453 
454 static int
NC3_put_vlen_element(int ncid,int typeid,void * vlen_element,size_t len,const void * data)455 NC3_put_vlen_element(int ncid, int typeid, void *vlen_element,
456 		    size_t len, const void *data)
457 {
458     return NC_ENOTNC4;
459 }
460 
461 static int
NC3_get_vlen_element(int ncid,int typeid,const void * vlen_element,size_t * len,void * data)462 NC3_get_vlen_element(int ncid, int typeid, const void *vlen_element,
463 		    size_t *len, void *data)
464 {
465     return NC_ENOTNC4;
466 }
467 
468 static int
NC3_set_var_chunk_cache(int ncid,int varid,size_t size,size_t nelems,float preemption)469 NC3_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems, float preemption)
470 {
471     return NC_ENOTNC4;
472 }
473 
474 static int
NC3_get_var_chunk_cache(int ncid,int varid,size_t * sizep,size_t * nelemsp,float * preemptionp)475 NC3_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp, float *preemptionp)
476 {
477     return NC_ENOTNC4;
478 }
479 
480 static int
NC3_def_var_deflate(int ncid,int varid,int shuffle,int deflate,int deflate_level)481 NC3_def_var_deflate(int ncid, int varid, int shuffle, int deflate,
482 		   int deflate_level)
483 {
484     return NC_ENOTNC4;
485 }
486 
487 static int
NC3_def_var_fletcher32(int ncid,int varid,int fletcher32)488 NC3_def_var_fletcher32(int ncid, int varid, int fletcher32)
489 {
490     return NC_ENOTNC4;
491 }
492 
493 static int
NC3_def_var_chunking(int ncid,int varid,int contiguous,const size_t * chunksizesp)494 NC3_def_var_chunking(int ncid, int varid, int contiguous, const size_t *chunksizesp)
495 {
496     return NC_ENOTNC4;
497 }
498 
499 static int
NC3_def_var_endian(int ncid,int varid,int endianness)500 NC3_def_var_endian(int ncid, int varid, int endianness)
501 {
502     return NC_ENOTNC4;
503 }
504 
505 static int
NC3_def_var_filter(int ncid,int varid,unsigned int id,size_t nparams,const unsigned int * parms)506 NC3_def_var_filter(int ncid, int varid, unsigned int id, size_t nparams, const unsigned int* parms)
507 {
508     return NC_ENOTNC4;
509 }
510 
511