/src/netcdf-c/libsrc/v1hpg.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2018, University Corporation for Atmospheric Research |
3 | | * See netcdf/COPYRIGHT file for copying and redistribution conditions. |
4 | | */ |
5 | | |
6 | | #if HAVE_CONFIG_H |
7 | | #include <config.h> |
8 | | #endif |
9 | | |
10 | | #include <stdlib.h> |
11 | | #include <stdio.h> |
12 | | #include <string.h> |
13 | | #include <assert.h> |
14 | | #include "nc3internal.h" |
15 | | #include "rnd.h" |
16 | | #include "ncx.h" |
17 | | |
18 | | /* |
19 | | * This module defines the external representation |
20 | | * of the "header" of a netcdf version one file and |
21 | | * the version two variant that uses 64-bit file |
22 | | * offsets instead of the 32-bit file offsets in version |
23 | | * one files. |
24 | | * For each of the components of the NC structure, |
25 | | * There are (static) ncx_len_XXX(), v1h_put_XXX() |
26 | | * and v1h_get_XXX() functions. These define the |
27 | | * external representation of the components. |
28 | | * The exported entry points for the whole NC structure |
29 | | * are built up from these. |
30 | | */ |
31 | | |
32 | | |
33 | | /* |
34 | | * "magic number" at beginning of file: 0x43444601 (big endian) |
35 | | * assert(sizeof(ncmagic) % X_ALIGN == 0); |
36 | | */ |
37 | | static const schar ncmagic[] = {'C', 'D', 'F', 0x02}; |
38 | | static const schar ncmagic1[] = {'C', 'D', 'F', 0x01}; |
39 | | static const schar ncmagic5[] = {'C', 'D', 'F', 0x05}; |
40 | | |
41 | | /* |
42 | | * v1hs == "Version 1 Header Stream" |
43 | | * |
44 | | * The netcdf file version 1 header is |
45 | | * of unknown and potentially unlimited size. |
46 | | * So, we don't know how much to get() on |
47 | | * the initial read. We build a stream, 'v1hs' |
48 | | * on top of ncio to do the header get. |
49 | | */ |
50 | | typedef struct v1hs { |
51 | | ncio *nciop; |
52 | | off_t offset; /* argument to nciop->get() */ |
53 | | size_t extent; /* argument to nciop->get() */ |
54 | | int flags; /* set to RGN_WRITE for write */ |
55 | | int version; /* format variant: NC_FORMAT_CLASSIC, NC_FORMAT_64BIT_OFFSET or NC_FORMAT_CDF5 */ |
56 | | void *base; /* beginning of current buffer */ |
57 | | void *pos; /* current position in buffer */ |
58 | | void *end; /* end of current buffer = base + extent */ |
59 | | } v1hs; |
60 | | |
61 | | |
62 | | /* |
63 | | * Release the stream, invalidate buffer |
64 | | */ |
65 | | static int |
66 | | rel_v1hs(v1hs *gsp) |
67 | 3.32k | { |
68 | 3.32k | int status; |
69 | 3.32k | if(gsp->offset == OFF_NONE || gsp->base == NULL) |
70 | 45 | return NC_NOERR; |
71 | 3.27k | status = ncio_rel(gsp->nciop, gsp->offset, |
72 | 3.27k | gsp->flags == RGN_WRITE ? RGN_MODIFIED : 0); |
73 | 3.27k | gsp->end = NULL; |
74 | 3.27k | gsp->pos = NULL; |
75 | 3.27k | gsp->base = NULL; |
76 | 3.27k | return status; |
77 | 3.32k | } |
78 | | |
79 | | |
80 | | /* |
81 | | * Release the current chunk and get the next one. |
82 | | * Also used for initialization when gsp->base == NULL. |
83 | | */ |
84 | | static int |
85 | | fault_v1hs(v1hs *gsp, size_t extent) |
86 | 3.32k | { |
87 | 3.32k | int status; |
88 | | |
89 | 3.32k | if(gsp->base != NULL) |
90 | 3.25k | { |
91 | 3.25k | const ptrdiff_t incr = (char *)gsp->pos - (char *)gsp->base; |
92 | 3.25k | status = rel_v1hs(gsp); |
93 | 3.25k | if(status) |
94 | 0 | return status; |
95 | 3.25k | gsp->offset += incr; |
96 | 3.25k | } |
97 | | |
98 | 3.32k | if(extent > gsp->extent) |
99 | 109 | gsp->extent = extent; |
100 | | |
101 | 3.32k | status = ncio_get(gsp->nciop, |
102 | 3.32k | gsp->offset, gsp->extent, |
103 | 3.32k | gsp->flags, &gsp->base); |
104 | 3.32k | if(status) |
105 | 45 | return status; |
106 | | |
107 | 3.27k | gsp->pos = gsp->base; |
108 | | |
109 | 3.27k | gsp->end = (char *)gsp->base + gsp->extent; |
110 | 3.27k | return NC_NOERR; |
111 | 3.32k | } |
112 | | |
113 | | |
114 | | /* |
115 | | * Ensure that 'nextread' bytes are available. |
116 | | */ |
117 | | static int |
118 | | check_v1hs(v1hs *gsp, size_t nextread) |
119 | 1.75M | { |
120 | | |
121 | | #if 0 /* DEBUG */ |
122 | | fprintf(stderr, "nextread %lu, remaining %lu\n", |
123 | | (unsigned long)nextread, |
124 | | (unsigned long)((char *)gsp->end - (char *)gsp->pos)); |
125 | | #endif |
126 | | /* |
127 | | * Bound against the space that remains. Adding nextread to gsp->pos |
128 | | * overflows for a large nextread, and the test then wrongly succeeds. |
129 | | */ |
130 | 1.75M | if((char *)gsp->pos <= (char *)gsp->end |
131 | 1.75M | && nextread <= (size_t)((char *)gsp->end - (char *)gsp->pos)) |
132 | 1.75M | return NC_NOERR; |
133 | | |
134 | 3.25k | return fault_v1hs(gsp, nextread); |
135 | 1.75M | } |
136 | | |
137 | | /* End v1hs */ |
138 | | |
139 | | /* Write a size_t to the header */ |
140 | | static int |
141 | | v1h_put_size_t(v1hs *psp, const size_t *sp) |
142 | 0 | { |
143 | 0 | int status; |
144 | 0 | if (psp->version == 5) /* all integers in CDF-5 are 64 bits */ |
145 | 0 | status = check_v1hs(psp, X_SIZEOF_INT64); |
146 | 0 | else |
147 | 0 | status = check_v1hs(psp, X_SIZEOF_SIZE_T); |
148 | 0 | if(status != NC_NOERR) |
149 | 0 | return status; |
150 | 0 | if (psp->version == 5) { |
151 | 0 | unsigned long long tmp = (unsigned long long) (*sp); |
152 | 0 | return ncx_put_uint64(&psp->pos, tmp); |
153 | 0 | } |
154 | 0 | else |
155 | 0 | return ncx_put_size_t(&psp->pos, sp); |
156 | 0 | } |
157 | | |
158 | | /* Read a size_t from the header */ |
159 | | static int |
160 | | v1h_get_size_t(v1hs *gsp, size_t *sp) |
161 | 1.10M | { |
162 | 1.10M | int status; |
163 | 1.10M | if (gsp->version == 5) /* all integers in CDF-5 are 64 bits */ |
164 | 562k | status = check_v1hs(gsp, X_SIZEOF_INT64); |
165 | 540k | else |
166 | 540k | status = check_v1hs(gsp, X_SIZEOF_SIZE_T); |
167 | 1.10M | if(status != NC_NOERR) |
168 | 16 | return status; |
169 | 1.10M | if (gsp->version == 5) { |
170 | 562k | unsigned long long tmp=0; |
171 | 562k | status = ncx_get_uint64((const void **)(&gsp->pos), &tmp); |
172 | 562k | *sp = (size_t)tmp; |
173 | 562k | return status; |
174 | 562k | } |
175 | 540k | else |
176 | 540k | return ncx_get_size_t((const void **)(&gsp->pos), sp); |
177 | 1.10M | } |
178 | | |
179 | | /* Begin nc_type */ |
180 | | |
181 | 0 | #define X_SIZEOF_NC_TYPE X_SIZEOF_INT |
182 | | |
183 | | /* Write a nc_type to the header */ |
184 | | static int |
185 | | v1h_put_nc_type(v1hs *psp, const nc_type *typep) |
186 | 0 | { |
187 | 0 | const unsigned int itype = (unsigned int) *typep; |
188 | 0 | int status = check_v1hs(psp, X_SIZEOF_INT); |
189 | 0 | if(status != NC_NOERR) return status; |
190 | 0 | status = ncx_put_uint32(&psp->pos, itype); |
191 | 0 | return status; |
192 | 0 | } |
193 | | |
194 | | |
195 | | /* Read a nc_type from the header */ |
196 | | static int |
197 | | v1h_get_nc_type(v1hs *gsp, nc_type *typep) |
198 | 33.6k | { |
199 | 33.6k | unsigned int type = 0; |
200 | 33.6k | int status = check_v1hs(gsp, X_SIZEOF_INT); |
201 | 33.6k | if(status != NC_NOERR) return status; |
202 | 33.6k | status = ncx_get_uint32((const void**)(&gsp->pos), &type); |
203 | 33.6k | if(status != NC_NOERR) |
204 | 0 | return status; |
205 | | /* Fix 35382 |
206 | | assert(type == NC_BYTE |
207 | | || type == NC_CHAR |
208 | | || type == NC_SHORT |
209 | | || type == NC_INT |
210 | | || type == NC_FLOAT |
211 | | || type == NC_DOUBLE |
212 | | || type == NC_UBYTE |
213 | | || type == NC_USHORT |
214 | | || type == NC_UINT |
215 | | || type == NC_INT64 |
216 | | || type == NC_UINT64 |
217 | | || type == NC_STRING); |
218 | | */ |
219 | 33.6k | if(type == NC_NAT || type > NC_MAX_ATOMIC_TYPE) |
220 | 5 | return NC_EINVAL; |
221 | 33.6k | else |
222 | 33.6k | *typep = (nc_type) type; |
223 | | |
224 | 33.6k | return NC_NOERR; |
225 | 33.6k | } |
226 | | |
227 | | /* End nc_type */ |
228 | | /* Begin NCtype (internal tags) */ |
229 | | |
230 | 15 | #define X_SIZEOF_NCTYPE X_SIZEOF_INT |
231 | | |
232 | | /* Write a NCtype to the header */ |
233 | | static int |
234 | | v1h_put_NCtype(v1hs *psp, NCtype type) |
235 | 0 | { |
236 | 0 | const unsigned int itype = (unsigned int) type; |
237 | 0 | int status = check_v1hs(psp, X_SIZEOF_INT); |
238 | 0 | if(status != NC_NOERR) return status; |
239 | 0 | status = ncx_put_uint32(&psp->pos, itype); |
240 | 0 | return status; |
241 | 0 | } |
242 | | |
243 | | /* Read a NCtype from the header */ |
244 | | static int |
245 | | v1h_get_NCtype(v1hs *gsp, NCtype *typep) |
246 | 33.1k | { |
247 | 33.1k | unsigned int type = 0; |
248 | 33.1k | int status = check_v1hs(gsp, X_SIZEOF_INT); |
249 | 33.1k | if(status != NC_NOERR) return status; |
250 | 33.1k | status = ncx_get_uint32((const void**)(&gsp->pos), &type); |
251 | 33.1k | if(status != NC_NOERR) return status; |
252 | | /* else */ |
253 | 33.1k | *typep = (NCtype) type; |
254 | 33.1k | return NC_NOERR; |
255 | 33.1k | } |
256 | | |
257 | | /* End NCtype */ |
258 | | /* Begin NC_string */ |
259 | | |
260 | | /* |
261 | | * How much space will the xdr'd string take. |
262 | | * Formerly |
263 | | NC_xlen_string(cdfstr) |
264 | | */ |
265 | | static size_t |
266 | | ncx_len_NC_string(const NC_string *ncstrp, int version) |
267 | 37 | { |
268 | 37 | size_t sz = (version == 5) ? X_SIZEOF_INT64 : X_SIZEOF_INT; /* nchars */ |
269 | | |
270 | 37 | assert(ncstrp != NULL); |
271 | | |
272 | 37 | if(ncstrp->nchars != 0) |
273 | 16 | { |
274 | | #if 0 |
275 | | assert(ncstrp->nchars % X_ALIGN == 0); |
276 | | sz += ncstrp->nchars; |
277 | | #else |
278 | 16 | sz += _RNDUP(ncstrp->nchars, X_ALIGN); |
279 | 16 | #endif |
280 | 16 | } |
281 | 37 | return sz; |
282 | 37 | } |
283 | | |
284 | | |
285 | | /* Write a NC_string to the header */ |
286 | | static int |
287 | | v1h_put_NC_string(v1hs *psp, const NC_string *ncstrp) |
288 | 0 | { |
289 | 0 | int status; |
290 | |
|
291 | | #if 0 |
292 | | assert(ncstrp->nchars % X_ALIGN == 0); |
293 | | #endif |
294 | |
|
295 | 0 | status = v1h_put_size_t(psp, &ncstrp->nchars); |
296 | 0 | if(status != NC_NOERR) |
297 | 0 | return status; |
298 | 0 | status = check_v1hs(psp, _RNDUP(ncstrp->nchars, X_ALIGN)); |
299 | 0 | if(status != NC_NOERR) |
300 | 0 | return status; |
301 | 0 | status = ncx_pad_putn_text(&psp->pos, ncstrp->nchars, ncstrp->cp); |
302 | 0 | if(status != NC_NOERR) |
303 | 0 | return status; |
304 | | |
305 | 0 | return NC_NOERR; |
306 | 0 | } |
307 | | |
308 | | |
309 | | /* Read a NC_string from the header */ |
310 | | static int |
311 | | v1h_get_NC_string(v1hs *gsp, NC_string **ncstrpp) |
312 | 518k | { |
313 | 518k | int status = 0; |
314 | 518k | size_t nchars = 0; |
315 | 518k | NC_string *ncstrp = NULL; |
316 | | #if USE_STRICT_NULL_BYTE_HEADER_PADDING |
317 | | size_t padding = 0; |
318 | | #endif /* USE_STRICT_NULL_BYTE_HEADER_PADDING */ |
319 | | |
320 | 518k | status = v1h_get_size_t(gsp, &nchars); |
321 | 518k | if(status != NC_NOERR) |
322 | 8 | return status; |
323 | | |
324 | 518k | ncstrp = new_NC_string(nchars, NULL); |
325 | 518k | if(ncstrp == NULL) |
326 | 1 | { |
327 | 1 | return NC_ENOMEM; |
328 | 1 | } |
329 | | |
330 | | #if 0 |
331 | | /* assert(ncstrp->nchars == nchars || ncstrp->nchars - nchars < X_ALIGN); */ |
332 | | assert(ncstrp->nchars % X_ALIGN == 0); |
333 | | status = check_v1hs(gsp, ncstrp->nchars); |
334 | | #else |
335 | | |
336 | 518k | status = check_v1hs(gsp, _RNDUP(ncstrp->nchars, X_ALIGN)); |
337 | 518k | #endif |
338 | 518k | if(status != NC_NOERR) |
339 | 19 | goto unwind_alloc; |
340 | | |
341 | 518k | status = ncx_pad_getn_text((const void **)(&gsp->pos), |
342 | 518k | nchars, ncstrp->cp); |
343 | 518k | if(status != NC_NOERR) |
344 | 0 | goto unwind_alloc; |
345 | | |
346 | | #if USE_STRICT_NULL_BYTE_HEADER_PADDING |
347 | | padding = _RNDUP(X_SIZEOF_CHAR * ncstrp->nchars, X_ALIGN) |
348 | | - X_SIZEOF_CHAR * ncstrp->nchars; |
349 | | |
350 | | if (padding > 0) { |
351 | | /* CDF specification: Header padding uses null (\x00) bytes. */ |
352 | | char pad[X_ALIGN-1]; |
353 | | memset(pad, 0, X_ALIGN-1); |
354 | | if (memcmp((char*)gsp->pos-padding, pad, padding) != 0) { |
355 | | free_NC_string(ncstrp); |
356 | | return NC_ENULLPAD; |
357 | | } |
358 | | } |
359 | | #endif |
360 | | |
361 | 518k | *ncstrpp = ncstrp; |
362 | | |
363 | 518k | return NC_NOERR; |
364 | | |
365 | 19 | unwind_alloc: |
366 | 19 | free_NC_string(ncstrp); |
367 | 19 | return status; |
368 | 518k | } |
369 | | |
370 | | /* End NC_string */ |
371 | | /* Begin NC_dim */ |
372 | | |
373 | | /* |
374 | | * How much space will the xdr'd dim take. |
375 | | * Formerly |
376 | | NC_xlen_dim(dpp) |
377 | | */ |
378 | | static size_t |
379 | | ncx_len_NC_dim(const NC_dim *dimp, int version) |
380 | 37 | { |
381 | 37 | size_t sz; |
382 | | |
383 | 37 | assert(dimp != NULL); |
384 | | |
385 | 37 | sz = ncx_len_NC_string(dimp->name, version); |
386 | 37 | sz += (version == 5) ? X_SIZEOF_INT64 : X_SIZEOF_SIZE_T; |
387 | | |
388 | 37 | return(sz); |
389 | 37 | } |
390 | | |
391 | | |
392 | | /* Write a NC_dim to the header */ |
393 | | static int |
394 | | v1h_put_NC_dim(v1hs *psp, const NC_dim *dimp) |
395 | 0 | { |
396 | 0 | int status; |
397 | |
|
398 | 0 | status = v1h_put_NC_string(psp, dimp->name); |
399 | 0 | if(status != NC_NOERR) |
400 | 0 | return status; |
401 | | |
402 | 0 | status = v1h_put_size_t(psp, &dimp->size); |
403 | 0 | if(status != NC_NOERR) |
404 | 0 | return status; |
405 | | |
406 | 0 | return NC_NOERR; |
407 | 0 | } |
408 | | |
409 | | /* Read a NC_dim from the header */ |
410 | | static int |
411 | | v1h_get_NC_dim(v1hs *gsp, NC_dim **dimpp) |
412 | 484k | { |
413 | 484k | int status; |
414 | 484k | NC_string *ncstrp; |
415 | 484k | NC_dim *dimp; |
416 | | |
417 | 484k | status = v1h_get_NC_string(gsp, &ncstrp); |
418 | 484k | if(status != NC_NOERR) |
419 | 24 | return status; |
420 | | |
421 | 484k | dimp = new_x_NC_dim(ncstrp); |
422 | 484k | if(dimp == NULL) |
423 | 0 | { |
424 | 0 | status = NC_ENOMEM; |
425 | 0 | goto unwind_name; |
426 | 0 | } |
427 | | |
428 | 484k | status = v1h_get_size_t(gsp, &dimp->size); |
429 | 484k | if(status != NC_NOERR) |
430 | 7 | { |
431 | 7 | free_NC_dim(dimp); /* frees name */ |
432 | 7 | return status; |
433 | 7 | } |
434 | | |
435 | 484k | *dimpp = dimp; |
436 | | |
437 | 484k | return NC_NOERR; |
438 | | |
439 | 0 | unwind_name: |
440 | 0 | free_NC_string(ncstrp); |
441 | 0 | return status; |
442 | 484k | } |
443 | | |
444 | | |
445 | | /* How much space in the header is required for this NC_dimarray? */ |
446 | | static size_t |
447 | | ncx_len_NC_dimarray(const NC_dimarray *ncap, int version) |
448 | 5 | { |
449 | 5 | size_t xlen = X_SIZEOF_NCTYPE; /* type */ |
450 | 5 | xlen += (version == 5) ? X_SIZEOF_INT64 : X_SIZEOF_SIZE_T; /* count */ |
451 | 5 | if(ncap == NULL) |
452 | 0 | return xlen; |
453 | | /* else */ |
454 | 5 | { |
455 | 5 | const NC_dim **dpp = (const NC_dim **)ncap->value; |
456 | 5 | if (dpp) |
457 | 1 | { |
458 | 1 | const NC_dim *const *const end = &dpp[ncap->nelems]; |
459 | 38 | for( /*NADA*/; dpp < end; dpp++) |
460 | 37 | { |
461 | 37 | xlen += ncx_len_NC_dim(*dpp,version); |
462 | 37 | } |
463 | 1 | } |
464 | 5 | } |
465 | 5 | return xlen; |
466 | 5 | } |
467 | | |
468 | | |
469 | | /* Write a NC_dimarray to the header */ |
470 | | static int |
471 | | v1h_put_NC_dimarray(v1hs *psp, const NC_dimarray *ncap) |
472 | 0 | { |
473 | 0 | int status; |
474 | |
|
475 | 0 | assert(psp != NULL); |
476 | |
|
477 | 0 | if(ncap == NULL |
478 | 0 | #if 1 |
479 | | /* Backward: |
480 | | * This clause is for 'byte for byte' |
481 | | * backward compatibility. |
482 | | * Strickly speaking, it is 'bug for bug'. |
483 | | */ |
484 | 0 | || ncap->nelems == 0 |
485 | 0 | #endif |
486 | 0 | ) |
487 | 0 | { |
488 | | /* |
489 | | * Handle empty netcdf |
490 | | */ |
491 | 0 | const size_t nosz = 0; |
492 | |
|
493 | 0 | status = v1h_put_NCtype(psp, NC_UNSPECIFIED); |
494 | 0 | if(status != NC_NOERR) |
495 | 0 | return status; |
496 | 0 | status = v1h_put_size_t(psp, &nosz); |
497 | 0 | if(status != NC_NOERR) |
498 | 0 | return status; |
499 | 0 | return NC_NOERR; |
500 | 0 | } |
501 | | /* else */ |
502 | | |
503 | 0 | status = v1h_put_NCtype(psp, NC_DIMENSION); |
504 | 0 | if(status != NC_NOERR) |
505 | 0 | return status; |
506 | 0 | status = v1h_put_size_t(psp, &ncap->nelems); |
507 | 0 | if(status != NC_NOERR) |
508 | 0 | return status; |
509 | | |
510 | 0 | { |
511 | 0 | const NC_dim **dpp = (const NC_dim **)ncap->value; |
512 | 0 | const NC_dim *const *const end = &dpp[ncap->nelems]; |
513 | 0 | for( /*NADA*/; dpp < end; dpp++) |
514 | 0 | { |
515 | 0 | status = v1h_put_NC_dim(psp, *dpp); |
516 | 0 | if(status) |
517 | 0 | return status; |
518 | 0 | } |
519 | 0 | } |
520 | 0 | return NC_NOERR; |
521 | 0 | } |
522 | | |
523 | | |
524 | | /* Read a NC_dimarray from the header */ |
525 | | static int |
526 | | v1h_get_NC_dimarray(v1hs *gsp, NC_dimarray *ncap) |
527 | 70 | { |
528 | 70 | int status; |
529 | 70 | NCtype type = NC_UNSPECIFIED; |
530 | | |
531 | 70 | assert(gsp != NULL && gsp->pos != NULL); |
532 | 70 | assert(ncap != NULL); |
533 | 70 | assert(ncap->value == NULL); |
534 | | |
535 | 70 | status = v1h_get_NCtype(gsp, &type); |
536 | 70 | if(status != NC_NOERR) |
537 | 0 | return status; |
538 | | |
539 | 70 | status = v1h_get_size_t(gsp, &ncap->nelems); |
540 | 70 | if(status != NC_NOERR) |
541 | 0 | return status; |
542 | | |
543 | 70 | if(ncap->nelems == 0) |
544 | 36 | return NC_NOERR; |
545 | | /* else */ |
546 | 34 | if(type != NC_DIMENSION) |
547 | 2 | return EINVAL; |
548 | | |
549 | 32 | if (ncap->nelems > SIZE_MAX / sizeof(NC_dim *)) |
550 | 0 | return NC_ERANGE; |
551 | 32 | ncap->value = (NC_dim **) calloc(1,ncap->nelems * sizeof(NC_dim *)); |
552 | 32 | if(ncap->value == NULL) |
553 | 0 | return NC_ENOMEM; |
554 | 32 | ncap->nalloc = ncap->nelems; |
555 | | |
556 | 32 | ncap->hashmap = NC_hashmapnew(ncap->nelems); |
557 | | |
558 | 32 | { |
559 | 32 | NC_dim **dpp = ncap->value; |
560 | 32 | NC_dim *const *const end = &dpp[ncap->nelems]; |
561 | 484k | for( /*NADA*/; dpp < end; dpp++) |
562 | 484k | { |
563 | 484k | status = v1h_get_NC_dim(gsp, dpp); |
564 | 484k | if(status) |
565 | 31 | { |
566 | 31 | ncap->nelems = (size_t)(dpp - ncap->value); |
567 | 31 | free_NC_dimarrayV(ncap); |
568 | 31 | return status; |
569 | 31 | } |
570 | 484k | { |
571 | 484k | uintptr_t dimid = (uintptr_t)(dpp - ncap->value); |
572 | 484k | NC_hashmapadd(ncap->hashmap, dimid, (*dpp)->name->cp, strlen((*dpp)->name->cp)); |
573 | 484k | } |
574 | 484k | } |
575 | 32 | } |
576 | | |
577 | 1 | return NC_NOERR; |
578 | 32 | } |
579 | | |
580 | | |
581 | | /* End NC_dim */ |
582 | | /* Begin NC_attr */ |
583 | | |
584 | | |
585 | | /* |
586 | | * How much space will 'attrp' take in external representation? |
587 | | * Formerly |
588 | | NC_xlen_attr(app) |
589 | | */ |
590 | | static size_t |
591 | | ncx_len_NC_attr(const NC_attr *attrp, int version) |
592 | 0 | { |
593 | 0 | size_t sz; |
594 | |
|
595 | 0 | assert(attrp != NULL); |
596 | |
|
597 | 0 | sz = ncx_len_NC_string(attrp->name, version); |
598 | 0 | sz += X_SIZEOF_NC_TYPE; /* type */ |
599 | 0 | sz += (version == 5) ? X_SIZEOF_INT64 : X_SIZEOF_SIZE_T; /* nelems */ |
600 | 0 | sz += attrp->xsz; |
601 | |
|
602 | 0 | return(sz); |
603 | 0 | } |
604 | | |
605 | | |
606 | | #undef MIN |
607 | 1.30k | #define MIN(mm,nn) (((mm) < (nn)) ? (mm) : (nn)) |
608 | | |
609 | | /*----< ncmpix_len_nctype() >------------------------------------------------*/ |
610 | | /* return the length of external data type */ |
611 | | static size_t |
612 | 0 | ncmpix_len_nctype(nc_type type) { |
613 | 0 | switch(type) { |
614 | 0 | case NC_BYTE: |
615 | 0 | case NC_CHAR: |
616 | 0 | case NC_UBYTE: return X_SIZEOF_CHAR; |
617 | 0 | case NC_SHORT: return X_SIZEOF_SHORT; |
618 | 0 | case NC_USHORT: return X_SIZEOF_USHORT; |
619 | 0 | case NC_INT: return X_SIZEOF_INT; |
620 | 0 | case NC_UINT: return X_SIZEOF_UINT; |
621 | 0 | case NC_FLOAT: return X_SIZEOF_FLOAT; |
622 | 0 | case NC_DOUBLE: return X_SIZEOF_DOUBLE; |
623 | 0 | case NC_INT64: return X_SIZEOF_INT64; |
624 | 0 | case NC_UINT64: return X_SIZEOF_UINT64; |
625 | 0 | default: fprintf(stderr,"ncmpix_len_nctype bad type %d\n",type); |
626 | 0 | assert(0); |
627 | 0 | } |
628 | 0 | return 0; |
629 | 0 | } |
630 | | |
631 | | /* |
632 | | * Put the values of an attribute |
633 | | * The loop is necessary since attrp->nelems |
634 | | * could potentially be quite large. |
635 | | */ |
636 | | static int |
637 | | v1h_put_NC_attrV(v1hs *psp, const NC_attr *attrp) |
638 | 0 | { |
639 | 0 | int status = 0; |
640 | 0 | const size_t perchunk = psp->extent; |
641 | 0 | size_t remaining = attrp->xsz; |
642 | 0 | void *value = attrp->xvalue; |
643 | 0 | size_t nbytes = 0, padding = 0; |
644 | |
|
645 | 0 | assert(psp->extent % X_ALIGN == 0); |
646 | |
|
647 | 0 | do { |
648 | 0 | nbytes = MIN(perchunk, remaining); |
649 | |
|
650 | 0 | status = check_v1hs(psp, nbytes); |
651 | 0 | if(status != NC_NOERR) |
652 | 0 | return status; |
653 | | |
654 | 0 | if (value) { |
655 | 0 | (void) memcpy(psp->pos, value, nbytes); |
656 | 0 | value = (void *)((char *)value + nbytes); |
657 | 0 | } |
658 | | |
659 | 0 | psp->pos = (void *)((char *)psp->pos + nbytes); |
660 | 0 | remaining -= nbytes; |
661 | |
|
662 | 0 | } while(remaining != 0); |
663 | | |
664 | | |
665 | 0 | padding = attrp->xsz - ncmpix_len_nctype(attrp->type) * attrp->nelems; |
666 | 0 | if (padding > 0) { |
667 | | /* CDF specification: Header padding uses null (\x00) bytes. */ |
668 | 0 | memset((char*)psp->pos-padding, 0, padding); |
669 | 0 | } |
670 | |
|
671 | 0 | return NC_NOERR; |
672 | 0 | } |
673 | | |
674 | | /* Write a NC_attr to the header */ |
675 | | static int |
676 | | v1h_put_NC_attr(v1hs *psp, const NC_attr *attrp) |
677 | 0 | { |
678 | 0 | int status; |
679 | |
|
680 | 0 | status = v1h_put_NC_string(psp, attrp->name); |
681 | 0 | if(status != NC_NOERR) |
682 | 0 | return status; |
683 | | |
684 | 0 | status = v1h_put_nc_type(psp, &attrp->type); |
685 | 0 | if(status != NC_NOERR) |
686 | 0 | return status; |
687 | | |
688 | 0 | status = v1h_put_size_t(psp, &attrp->nelems); |
689 | 0 | if(status != NC_NOERR) |
690 | 0 | return status; |
691 | | |
692 | 0 | status = v1h_put_NC_attrV(psp, attrp); |
693 | 0 | if(status != NC_NOERR) |
694 | 0 | return status; |
695 | | |
696 | 0 | return NC_NOERR; |
697 | 0 | } |
698 | | |
699 | | |
700 | | /* |
701 | | * Get the values of an attribute |
702 | | * The loop is necessary since attrp->nelems |
703 | | * could potentially be quite large. |
704 | | */ |
705 | | static int |
706 | | v1h_get_NC_attrV(v1hs *gsp, NC_attr *attrp) |
707 | 702 | { |
708 | 702 | int status; |
709 | 702 | const size_t perchunk = gsp->extent; |
710 | 702 | size_t remaining = attrp->xsz; |
711 | 702 | void *value = attrp->xvalue; |
712 | 702 | size_t nget; |
713 | | #if USE_STRICT_NULL_BYTE_HEADER_PADDING |
714 | | size_t padding; |
715 | | #endif /* USE_STRICT_NULL_BYTE_HEADER_PADDING */ |
716 | | |
717 | 1.30k | do { |
718 | 1.30k | nget = MIN(perchunk, remaining); |
719 | | |
720 | 1.30k | status = check_v1hs(gsp, nget); |
721 | 1.30k | if(status != NC_NOERR) |
722 | 8 | return status; |
723 | | |
724 | 1.29k | if (value) { |
725 | 1.26k | (void) memcpy(value, gsp->pos, nget); |
726 | 1.26k | value = (void *)((signed char *)value + nget); |
727 | 1.26k | } |
728 | | |
729 | 1.29k | gsp->pos = (void*)((unsigned char *)gsp->pos + nget); |
730 | | |
731 | 1.29k | remaining -= nget; |
732 | | |
733 | 1.29k | } while(remaining != 0); |
734 | | |
735 | | #if USE_STRICT_NULL_BYTE_HEADER_PADDING |
736 | | padding = attrp->xsz - ncmpix_len_nctype(attrp->type) * attrp->nelems; |
737 | | if (padding > 0) { |
738 | | /* CDF specification: Header padding uses null (\x00) bytes. */ |
739 | | char pad[X_ALIGN-1]; |
740 | | memset(pad, 0, X_ALIGN-1); |
741 | | if (memcmp((char*)gsp->pos-padding, pad, (size_t)padding) != 0) |
742 | | return NC_ENULLPAD; |
743 | | } |
744 | | #endif |
745 | | |
746 | 694 | return NC_NOERR; |
747 | 702 | } |
748 | | |
749 | | |
750 | | /* Read a NC_attr from the header */ |
751 | | static int |
752 | | v1h_get_NC_attr(v1hs *gsp, NC_attr **attrpp) |
753 | 713 | { |
754 | 713 | NC_string *strp; |
755 | 713 | int status; |
756 | 713 | nc_type type; |
757 | 713 | size_t nelems; |
758 | 713 | NC_attr *attrp; |
759 | | |
760 | 713 | status = v1h_get_NC_string(gsp, &strp); |
761 | 713 | if(status != NC_NOERR) |
762 | 4 | return status; |
763 | | |
764 | 709 | status = v1h_get_nc_type(gsp, &type); |
765 | 709 | if(status != NC_NOERR) |
766 | 5 | goto unwind_name; |
767 | | |
768 | 704 | status = v1h_get_size_t(gsp, &nelems); |
769 | 704 | if(status != NC_NOERR) |
770 | 0 | goto unwind_name; |
771 | | |
772 | 704 | attrp = new_x_NC_attr(strp, type, nelems); |
773 | 704 | if(attrp == NULL) |
774 | 2 | { |
775 | 2 | status = NC_ENOMEM; |
776 | 2 | goto unwind_name; |
777 | 2 | } |
778 | | |
779 | 702 | status = v1h_get_NC_attrV(gsp, attrp); |
780 | 702 | if(status != NC_NOERR) |
781 | 8 | { |
782 | 8 | free_NC_attr(attrp); /* frees strp */ |
783 | 8 | return status; |
784 | 8 | } |
785 | | |
786 | 694 | *attrpp = attrp; |
787 | | |
788 | 694 | return NC_NOERR; |
789 | | |
790 | 7 | unwind_name: |
791 | 7 | free_NC_string(strp); |
792 | 7 | return status; |
793 | 702 | } |
794 | | |
795 | | |
796 | | /* How much space in the header is required for this NC_attrarray? */ |
797 | | static size_t |
798 | | ncx_len_NC_attrarray(const NC_attrarray *ncap, int version) |
799 | 5 | { |
800 | 5 | size_t xlen = X_SIZEOF_NCTYPE; /* type */ |
801 | 5 | xlen += (version == 5) ? X_SIZEOF_INT64 : X_SIZEOF_SIZE_T; /* count */ |
802 | 5 | if(ncap == NULL) |
803 | 0 | return xlen; |
804 | | /* else */ |
805 | 5 | { |
806 | 5 | const NC_attr **app = (const NC_attr **)ncap->value; |
807 | 5 | if (app) |
808 | 0 | { |
809 | 0 | const NC_attr *const *const end = &app[ncap->nelems]; |
810 | 0 | for( /*NADA*/; app < end; app++) |
811 | 0 | { |
812 | 0 | xlen += ncx_len_NC_attr(*app,version); |
813 | 0 | } |
814 | 0 | } |
815 | 5 | } |
816 | 5 | return xlen; |
817 | 5 | } |
818 | | |
819 | | |
820 | | /* Write a NC_attrarray to the header */ |
821 | | static int |
822 | | v1h_put_NC_attrarray(v1hs *psp, const NC_attrarray *ncap) |
823 | 0 | { |
824 | 0 | int status; |
825 | |
|
826 | 0 | assert(psp != NULL); |
827 | |
|
828 | 0 | if(ncap == NULL |
829 | 0 | #if 1 |
830 | | /* Backward: |
831 | | * This clause is for 'byte for byte' |
832 | | * backward compatibility. |
833 | | * Strickly speaking, it is 'bug for bug'. |
834 | | */ |
835 | 0 | || ncap->nelems == 0 |
836 | 0 | #endif |
837 | 0 | ) |
838 | 0 | { |
839 | | /* |
840 | | * Handle empty netcdf |
841 | | */ |
842 | 0 | const size_t nosz = 0; |
843 | |
|
844 | 0 | status = v1h_put_NCtype(psp, NC_UNSPECIFIED); |
845 | 0 | if(status != NC_NOERR) |
846 | 0 | return status; |
847 | 0 | status = v1h_put_size_t(psp, &nosz); |
848 | 0 | if(status != NC_NOERR) |
849 | 0 | return status; |
850 | 0 | return NC_NOERR; |
851 | 0 | } |
852 | | /* else */ |
853 | | |
854 | 0 | status = v1h_put_NCtype(psp, NC_ATTRIBUTE); |
855 | 0 | if(status != NC_NOERR) |
856 | 0 | return status; |
857 | 0 | status = v1h_put_size_t(psp, &ncap->nelems); |
858 | 0 | if(status != NC_NOERR) |
859 | 0 | return status; |
860 | | |
861 | 0 | { |
862 | 0 | const NC_attr **app = (const NC_attr **)ncap->value; |
863 | 0 | const NC_attr *const *const end = &app[ncap->nelems]; |
864 | 0 | for( /*NADA*/; app < end; app++) |
865 | 0 | { |
866 | 0 | status = v1h_put_NC_attr(psp, *app); |
867 | 0 | if(status) |
868 | 0 | return status; |
869 | 0 | } |
870 | 0 | } |
871 | 0 | return NC_NOERR; |
872 | 0 | } |
873 | | |
874 | | |
875 | | /* Read a NC_attrarray from the header */ |
876 | | static int |
877 | | v1h_get_NC_attrarray(v1hs *gsp, NC_attrarray *ncap) |
878 | 33.0k | { |
879 | 33.0k | int status; |
880 | 33.0k | NCtype type = NC_UNSPECIFIED; |
881 | | |
882 | 33.0k | assert(gsp != NULL && gsp->pos != NULL); |
883 | 33.0k | assert(ncap != NULL); |
884 | 33.0k | assert(ncap->value == NULL); |
885 | | |
886 | 33.0k | status = v1h_get_NCtype(gsp, &type); |
887 | 33.0k | if(status != NC_NOERR) |
888 | 0 | return status; |
889 | 33.0k | status = v1h_get_size_t(gsp, &ncap->nelems); |
890 | 33.0k | if(status != NC_NOERR) |
891 | 0 | return status; |
892 | | |
893 | 33.0k | if(ncap->nelems == 0) |
894 | 32.9k | return NC_NOERR; |
895 | | /* else */ |
896 | 28 | if(type != NC_ATTRIBUTE) |
897 | 9 | return EINVAL; |
898 | | |
899 | 19 | ncap->value = (NC_attr **) malloc(ncap->nelems * sizeof(NC_attr *)); |
900 | 19 | if(ncap->value == NULL) |
901 | 0 | return NC_ENOMEM; |
902 | 19 | ncap->nalloc = ncap->nelems; |
903 | | |
904 | 19 | { |
905 | 19 | NC_attr **app = ncap->value; |
906 | 19 | NC_attr *const *const end = &app[ncap->nelems]; |
907 | 713 | for( /*NADA*/; app < end; app++) |
908 | 713 | { |
909 | 713 | status = v1h_get_NC_attr(gsp, app); |
910 | 713 | if(status) |
911 | 19 | { |
912 | 19 | ncap->nelems = (size_t)(app - ncap->value); |
913 | 19 | free_NC_attrarrayV(ncap); |
914 | 19 | return status; |
915 | 19 | } |
916 | 713 | } |
917 | 19 | } |
918 | | |
919 | 0 | return NC_NOERR; |
920 | 19 | } |
921 | | |
922 | | /* End NC_attr */ |
923 | | /* Begin NC_var */ |
924 | | |
925 | | /* |
926 | | * How much space will the xdr'd var take. |
927 | | * Formerly |
928 | | NC_xlen_var(vpp) |
929 | | */ |
930 | | static size_t |
931 | | ncx_len_NC_var(const NC_var *varp, size_t sizeof_off_t, int version) |
932 | 0 | { |
933 | 0 | size_t sz; |
934 | |
|
935 | 0 | assert(varp != NULL); |
936 | 0 | assert(sizeof_off_t != 0); |
937 | |
|
938 | 0 | sz = ncx_len_NC_string(varp->name, version); |
939 | 0 | if (version == 5) { |
940 | 0 | sz += X_SIZEOF_INT64; /* ndims */ |
941 | 0 | sz += ncx_len_int64(varp->ndims); /* dimids */ |
942 | 0 | } |
943 | 0 | else { |
944 | 0 | sz += X_SIZEOF_SIZE_T; /* ndims */ |
945 | 0 | sz += ncx_len_int(varp->ndims); /* dimids */ |
946 | 0 | } |
947 | 0 | sz += ncx_len_NC_attrarray(&varp->attrs, version); |
948 | 0 | sz += X_SIZEOF_NC_TYPE; /* nc_type */ |
949 | 0 | sz += (version == 5) ? X_SIZEOF_INT64 : X_SIZEOF_SIZE_T; /* vsize */ |
950 | 0 | sz += sizeof_off_t; /* begin */ |
951 | |
|
952 | 0 | return(sz); |
953 | 0 | } |
954 | | |
955 | | |
956 | | /* Write a NC_var to the header */ |
957 | | static int |
958 | | v1h_put_NC_var(v1hs *psp, const NC_var *varp) |
959 | 0 | { |
960 | 0 | int status; |
961 | 0 | size_t vsize; |
962 | |
|
963 | 0 | status = v1h_put_NC_string(psp, varp->name); |
964 | 0 | if(status != NC_NOERR) |
965 | 0 | return status; |
966 | | |
967 | 0 | status = v1h_put_size_t(psp, &varp->ndims); |
968 | 0 | if(status != NC_NOERR) |
969 | 0 | return status; |
970 | | |
971 | 0 | if (psp->version == 5) { |
972 | 0 | status = check_v1hs(psp, ncx_len_int64(varp->ndims)); |
973 | 0 | if(status != NC_NOERR) |
974 | 0 | return status; |
975 | 0 | status = ncx_putn_longlong_int(&psp->pos, |
976 | 0 | varp->ndims, varp->dimids, NULL); |
977 | 0 | if(status != NC_NOERR) |
978 | 0 | return status; |
979 | 0 | } |
980 | 0 | else { |
981 | 0 | status = check_v1hs(psp, ncx_len_int(varp->ndims)); |
982 | 0 | if(status != NC_NOERR) |
983 | 0 | return status; |
984 | 0 | status = ncx_putn_int_int(&psp->pos, |
985 | 0 | varp->ndims, varp->dimids, NULL); |
986 | 0 | if(status != NC_NOERR) |
987 | 0 | return status; |
988 | 0 | } |
989 | | |
990 | 0 | status = v1h_put_NC_attrarray(psp, &varp->attrs); |
991 | 0 | if(status != NC_NOERR) |
992 | 0 | return status; |
993 | | |
994 | 0 | status = v1h_put_nc_type(psp, &varp->type); |
995 | 0 | if(status != NC_NOERR) |
996 | 0 | return status; |
997 | | |
998 | | /* write vsize to header. |
999 | | * CDF format specification: The vsize field is actually redundant, because |
1000 | | * its value may be computed from other information in the header. The |
1001 | | * 32-bit vsize field is not large enough to contain the size of variables |
1002 | | * that require more than 2^32 - 4 bytes, so 2^32 - 1 is used in the vsize |
1003 | | * field for such variables. |
1004 | | */ |
1005 | 0 | vsize = varp->len; |
1006 | 0 | if (varp->len > 4294967292UL && (psp->version == NC_FORMAT_CLASSIC || |
1007 | 0 | psp->version == NC_FORMAT_64BIT_OFFSET)) |
1008 | 0 | vsize = 4294967295UL; /* 2^32-1 */ |
1009 | 0 | status = v1h_put_size_t(psp, &vsize); |
1010 | 0 | if(status != NC_NOERR) return status; |
1011 | | |
1012 | 0 | status = check_v1hs(psp, psp->version == 1 ? 4 : 8); /*begin*/ |
1013 | 0 | if(status != NC_NOERR) |
1014 | 0 | return status; |
1015 | 0 | status = ncx_put_off_t(&psp->pos, &varp->begin, psp->version == 1 ? 4 : 8); |
1016 | 0 | if(status != NC_NOERR) |
1017 | 0 | return status; |
1018 | | |
1019 | 0 | return NC_NOERR; |
1020 | 0 | } |
1021 | | |
1022 | | |
1023 | | /* Read a NC_var from the header */ |
1024 | | static int |
1025 | | v1h_get_NC_var(v1hs *gsp, NC_var **varpp) |
1026 | 32.9k | { |
1027 | 32.9k | NC_string *strp; |
1028 | 32.9k | int status; |
1029 | 32.9k | size_t ndims; |
1030 | 32.9k | NC_var *varp; |
1031 | | |
1032 | 32.9k | status = v1h_get_NC_string(gsp, &strp); |
1033 | 32.9k | if(status != NC_NOERR) |
1034 | 0 | return status; |
1035 | | |
1036 | 32.9k | status = v1h_get_size_t(gsp, &ndims); |
1037 | 32.9k | if(status != NC_NOERR) |
1038 | 1 | goto unwind_name; |
1039 | | |
1040 | 32.9k | if(ndims > NC_MAX_VAR_DIMS) { |
1041 | 1 | status = NC_EMAXDIMS; |
1042 | 1 | goto unwind_name; |
1043 | 1 | } |
1044 | | |
1045 | 32.9k | varp = new_x_NC_var(strp, ndims); |
1046 | 32.9k | if(varp == NULL) |
1047 | 0 | { |
1048 | 0 | status = NC_ENOMEM; |
1049 | 0 | goto unwind_name; |
1050 | 0 | } |
1051 | | |
1052 | 32.9k | if (gsp->version == 5) { |
1053 | 0 | status = check_v1hs(gsp, ncx_len_int64(ndims)); |
1054 | 0 | if(status != NC_NOERR) |
1055 | 0 | goto unwind_alloc; |
1056 | 0 | status = ncx_getn_longlong_int((const void **)(&gsp->pos), |
1057 | 0 | ndims, varp->dimids); |
1058 | 0 | if(status != NC_NOERR) |
1059 | 0 | goto unwind_alloc; |
1060 | 0 | } |
1061 | 32.9k | else { |
1062 | 32.9k | status = check_v1hs(gsp, ncx_len_int(ndims)); |
1063 | 32.9k | if(status != NC_NOERR) |
1064 | 0 | goto unwind_alloc; |
1065 | 32.9k | status = ncx_getn_int_int((const void **)(&gsp->pos), |
1066 | 32.9k | ndims, varp->dimids); |
1067 | 32.9k | if(status != NC_NOERR) |
1068 | 0 | goto unwind_alloc; |
1069 | 32.9k | } |
1070 | 32.9k | status = v1h_get_NC_attrarray(gsp, &varp->attrs); |
1071 | 32.9k | if(status != NC_NOERR) |
1072 | 6 | goto unwind_alloc; |
1073 | 32.9k | status = v1h_get_nc_type(gsp, &varp->type); |
1074 | 32.9k | if(status != NC_NOERR) |
1075 | 1 | goto unwind_alloc; |
1076 | | |
1077 | 32.9k | size_t tmp; |
1078 | 32.9k | status = v1h_get_size_t(gsp, &tmp); |
1079 | 32.9k | varp->len = tmp; |
1080 | 32.9k | if(status != NC_NOERR) |
1081 | 0 | goto unwind_alloc; |
1082 | | |
1083 | 32.9k | status = check_v1hs(gsp, gsp->version == 1 ? 4 : 8); |
1084 | 32.9k | if(status != NC_NOERR) |
1085 | 1 | goto unwind_alloc; |
1086 | 32.9k | status = ncx_get_off_t((const void **)&gsp->pos, |
1087 | 32.9k | &varp->begin, gsp->version == 1 ? 4 : 8); |
1088 | 32.9k | if(status != NC_NOERR) |
1089 | 0 | goto unwind_alloc; |
1090 | | |
1091 | 32.9k | *varpp = varp; |
1092 | 32.9k | return NC_NOERR; |
1093 | | |
1094 | 8 | unwind_alloc: |
1095 | 8 | free_NC_var(varp); /* frees name */ |
1096 | 8 | return status; |
1097 | | |
1098 | 2 | unwind_name: |
1099 | 2 | free_NC_string(strp); |
1100 | 2 | return status; |
1101 | 32.9k | } |
1102 | | |
1103 | | |
1104 | | /* How much space in the header is required for this NC_vararray? */ |
1105 | | static size_t |
1106 | | ncx_len_NC_vararray(const NC_vararray *ncap, size_t sizeof_off_t, int version) |
1107 | 5 | { |
1108 | 5 | size_t xlen = X_SIZEOF_NCTYPE; /* type */ |
1109 | 5 | xlen += (version == 5) ? X_SIZEOF_INT64 : X_SIZEOF_SIZE_T; /* count */ |
1110 | 5 | if(ncap == NULL) |
1111 | 0 | return xlen; |
1112 | | /* else */ |
1113 | 5 | { |
1114 | 5 | const NC_var **vpp = (const NC_var **)ncap->value; |
1115 | 5 | if (vpp) |
1116 | 0 | { |
1117 | 0 | const NC_var *const *const end = &vpp[ncap->nelems]; |
1118 | 0 | for( /*NADA*/; vpp < end; vpp++) |
1119 | 0 | { |
1120 | 0 | xlen += ncx_len_NC_var(*vpp, sizeof_off_t, version); |
1121 | 0 | } |
1122 | 0 | } |
1123 | 5 | } |
1124 | 5 | return xlen; |
1125 | 5 | } |
1126 | | |
1127 | | |
1128 | | /* Write a NC_vararray to the header */ |
1129 | | static int |
1130 | | v1h_put_NC_vararray(v1hs *psp, const NC_vararray *ncap) |
1131 | 0 | { |
1132 | 0 | int status; |
1133 | |
|
1134 | 0 | assert(psp != NULL); |
1135 | |
|
1136 | 0 | if(ncap == NULL |
1137 | 0 | #if 1 |
1138 | | /* Backward: |
1139 | | * This clause is for 'byte for byte' |
1140 | | * backward compatibility. |
1141 | | * Strickly speaking, it is 'bug for bug'. |
1142 | | */ |
1143 | 0 | || ncap->nelems == 0 |
1144 | 0 | #endif |
1145 | 0 | ) |
1146 | 0 | { |
1147 | | /* |
1148 | | * Handle empty netcdf |
1149 | | */ |
1150 | 0 | const size_t nosz = 0; |
1151 | |
|
1152 | 0 | status = v1h_put_NCtype(psp, NC_UNSPECIFIED); |
1153 | 0 | if(status != NC_NOERR) |
1154 | 0 | return status; |
1155 | 0 | status = v1h_put_size_t(psp, &nosz); |
1156 | 0 | if(status != NC_NOERR) |
1157 | 0 | return status; |
1158 | 0 | return NC_NOERR; |
1159 | 0 | } |
1160 | | /* else */ |
1161 | | |
1162 | 0 | status = v1h_put_NCtype(psp, NC_VARIABLE); |
1163 | 0 | if(status != NC_NOERR) |
1164 | 0 | return status; |
1165 | 0 | status = v1h_put_size_t(psp, &ncap->nelems); |
1166 | 0 | if(status != NC_NOERR) |
1167 | 0 | return status; |
1168 | | |
1169 | 0 | { |
1170 | 0 | const NC_var **vpp = (const NC_var **)ncap->value; |
1171 | 0 | const NC_var *const *const end = &vpp[ncap->nelems]; |
1172 | 0 | for( /*NADA*/; vpp < end; vpp++) |
1173 | 0 | { |
1174 | 0 | status = v1h_put_NC_var(psp, *vpp); |
1175 | 0 | if(status) |
1176 | 0 | return status; |
1177 | 0 | } |
1178 | 0 | } |
1179 | 0 | return NC_NOERR; |
1180 | 0 | } |
1181 | | |
1182 | | |
1183 | | /* Read a NC_vararray from the header */ |
1184 | | static int |
1185 | | v1h_get_NC_vararray(v1hs *gsp, NC_vararray *ncap) |
1186 | 15 | { |
1187 | 15 | int status; |
1188 | 15 | NCtype type = NC_UNSPECIFIED; |
1189 | | |
1190 | 15 | assert(gsp != NULL && gsp->pos != NULL); |
1191 | 15 | assert(ncap != NULL); |
1192 | 15 | assert(ncap->value == NULL); |
1193 | | |
1194 | 15 | status = v1h_get_NCtype(gsp, &type); |
1195 | 15 | if(status != NC_NOERR) |
1196 | 0 | return status; |
1197 | | |
1198 | 15 | status = v1h_get_size_t(gsp, &ncap->nelems); |
1199 | 15 | if(status != NC_NOERR) |
1200 | 0 | return status; |
1201 | | |
1202 | 15 | if(ncap->nelems == 0) |
1203 | 5 | return NC_NOERR; |
1204 | | /* else */ |
1205 | 10 | if(type != NC_VARIABLE) |
1206 | 0 | return EINVAL; |
1207 | | |
1208 | 10 | if (ncap->nelems > SIZE_MAX / sizeof(NC_var *)) |
1209 | 0 | return NC_ERANGE; |
1210 | 10 | ncap->value = (NC_var **) calloc(1,ncap->nelems * sizeof(NC_var *)); |
1211 | 10 | if(ncap->value == NULL) |
1212 | 0 | return NC_ENOMEM; |
1213 | 10 | ncap->nalloc = ncap->nelems; |
1214 | | |
1215 | 10 | ncap->hashmap = NC_hashmapnew(ncap->nelems); |
1216 | 10 | if (ncap->hashmap == NULL) |
1217 | 0 | return NC_ENOMEM; |
1218 | 10 | { |
1219 | 10 | NC_var **vpp = ncap->value; |
1220 | 10 | NC_var *const *const end = &vpp[ncap->nelems]; |
1221 | 32.9k | for( /*NADA*/; vpp < end; vpp++) |
1222 | 32.9k | { |
1223 | 32.9k | status = v1h_get_NC_var(gsp, vpp); |
1224 | 32.9k | if(status) |
1225 | 10 | { |
1226 | 10 | ncap->nelems = (size_t)(vpp - ncap->value); |
1227 | 10 | free_NC_vararrayV(ncap); |
1228 | 10 | return status; |
1229 | 10 | } |
1230 | 32.9k | { |
1231 | 32.9k | uintptr_t varid = (uintptr_t)(vpp - ncap->value); |
1232 | 32.9k | NC_hashmapadd(ncap->hashmap, varid, (*vpp)->name->cp, strlen((*vpp)->name->cp)); |
1233 | 32.9k | } |
1234 | 32.9k | } |
1235 | 10 | } |
1236 | | |
1237 | 0 | return NC_NOERR; |
1238 | 10 | } |
1239 | | |
1240 | | |
1241 | | /* End NC_var */ |
1242 | | /* Begin NC */ |
1243 | | |
1244 | | /* |
1245 | | * Recompute the shapes of all variables |
1246 | | * Sets ncp->begin_var to start of first variable. |
1247 | | * Sets ncp->begin_rec to start of first record variable. |
1248 | | * Returns -1 on error. The only possible error is a reference |
1249 | | * to a non existent dimension, which could occur for a corrupted |
1250 | | * netcdf file. |
1251 | | */ |
1252 | | static int |
1253 | | NC_computeshapes(NC3_INFO* ncp) |
1254 | 5 | { |
1255 | 5 | NC_var **vpp = (NC_var **)ncp->vars.value; |
1256 | 5 | NC_var *first_var = NULL; /* first "non-record" var */ |
1257 | 5 | NC_var *first_rec = NULL; /* first "record" var */ |
1258 | 5 | int status; |
1259 | | |
1260 | 5 | ncp->begin_var = (off_t) ncp->xsz; |
1261 | 5 | ncp->begin_rec = (off_t) ncp->xsz; |
1262 | 5 | ncp->recsize = 0; |
1263 | | |
1264 | 5 | if(ncp->vars.nelems == 0) |
1265 | 5 | return(0); |
1266 | | |
1267 | 0 | if (vpp) |
1268 | 0 | { |
1269 | 0 | NC_var *const *const end = &vpp[ncp->vars.nelems]; |
1270 | 0 | for( /*NADA*/; vpp < end; vpp++) |
1271 | 0 | { |
1272 | 0 | status = NC_var_shape(*vpp, &ncp->dims); |
1273 | 0 | if(status != NC_NOERR) |
1274 | 0 | return(status); |
1275 | | |
1276 | 0 | if(IS_RECVAR(*vpp)) |
1277 | 0 | { |
1278 | 0 | if(first_rec == NULL) |
1279 | 0 | first_rec = *vpp; |
1280 | 0 | ncp->recsize += (*vpp)->len; |
1281 | 0 | } |
1282 | 0 | else |
1283 | 0 | { |
1284 | 0 | if(first_var == NULL) |
1285 | 0 | first_var = *vpp; |
1286 | | /* |
1287 | | * Overwritten each time thru. |
1288 | | * Usually overwritten in first_rec != NULL clause below. |
1289 | | */ |
1290 | 0 | ncp->begin_rec = (*vpp)->begin + (off_t)(*vpp)->len; |
1291 | 0 | } |
1292 | 0 | } |
1293 | 0 | } |
1294 | | |
1295 | 0 | if(first_rec != NULL) |
1296 | 0 | { |
1297 | 0 | if(ncp->begin_rec > first_rec->begin) |
1298 | 0 | return(NC_ENOTNC); /* not a netCDF file or corrupted */ |
1299 | 0 | ncp->begin_rec = first_rec->begin; |
1300 | | /* |
1301 | | * for special case of exactly one record variable, pack value |
1302 | | */ |
1303 | 0 | if(ncp->recsize == first_rec->len) |
1304 | 0 | ncp->recsize = *first_rec->dsizes * first_rec->xsz; |
1305 | 0 | } |
1306 | | |
1307 | 0 | if(first_var != NULL) |
1308 | 0 | { |
1309 | 0 | ncp->begin_var = first_var->begin; |
1310 | 0 | } |
1311 | 0 | else |
1312 | 0 | { |
1313 | 0 | ncp->begin_var = ncp->begin_rec; |
1314 | 0 | } |
1315 | |
|
1316 | 0 | if(ncp->begin_var <= 0 || |
1317 | 0 | ncp->xsz > (size_t)ncp->begin_var || |
1318 | 0 | ncp->begin_rec <= 0 || |
1319 | 0 | ncp->begin_var > ncp->begin_rec) |
1320 | 0 | return(NC_ENOTNC); /* not a netCDF file or corrupted */ |
1321 | | |
1322 | 0 | return(NC_NOERR); |
1323 | 0 | } |
1324 | | |
1325 | | /* How much space in the header is required for the NC data structure? */ |
1326 | | size_t |
1327 | | ncx_len_NC(const NC3_INFO* ncp, size_t sizeof_off_t) |
1328 | 5 | { |
1329 | 5 | int version=1; |
1330 | 5 | size_t xlen = sizeof(ncmagic); |
1331 | | |
1332 | 5 | assert(ncp != NULL); |
1333 | 5 | if (fIsSet(ncp->flags, NC_64BIT_DATA)) /* CDF-5 */ |
1334 | 3 | version = 5; |
1335 | 2 | else if (fIsSet(ncp->flags, NC_64BIT_OFFSET)) /* CDF-2 */ |
1336 | 1 | version = 2; |
1337 | | |
1338 | 5 | xlen += (version == 5) ? X_SIZEOF_INT64 : X_SIZEOF_SIZE_T; /* numrecs */ |
1339 | 5 | xlen += ncx_len_NC_dimarray(&ncp->dims, version); |
1340 | 5 | xlen += ncx_len_NC_attrarray(&ncp->attrs, version); |
1341 | 5 | xlen += ncx_len_NC_vararray(&ncp->vars, sizeof_off_t, version); |
1342 | | |
1343 | 5 | return xlen; |
1344 | 5 | } |
1345 | | |
1346 | | |
1347 | | /* Write the file header */ |
1348 | | int |
1349 | | ncx_put_NC(const NC3_INFO* ncp, void **xpp, off_t offset, size_t extent) |
1350 | 0 | { |
1351 | 0 | int status = NC_NOERR; |
1352 | 0 | v1hs ps; /* the get stream */ |
1353 | |
|
1354 | 0 | assert(ncp != NULL); |
1355 | | |
1356 | | /* Initialize stream ps */ |
1357 | |
|
1358 | 0 | ps.nciop = ncp->nciop; |
1359 | 0 | ps.flags = RGN_WRITE; |
1360 | |
|
1361 | 0 | if (ncp->flags & NC_64BIT_DATA) |
1362 | 0 | ps.version = 5; |
1363 | 0 | else if (ncp->flags & NC_64BIT_OFFSET) |
1364 | 0 | ps.version = 2; |
1365 | 0 | else |
1366 | 0 | ps.version = 1; |
1367 | |
|
1368 | 0 | if(xpp == NULL) |
1369 | 0 | { |
1370 | | /* |
1371 | | * Come up with a reasonable stream read size. |
1372 | | */ |
1373 | 0 | extent = ncp->xsz; |
1374 | 0 | if(extent <= ((ps.version==5)?MIN_NC5_XSZ:MIN_NC3_XSZ)) |
1375 | 0 | { |
1376 | | /* first time read */ |
1377 | 0 | extent = ncp->chunk; |
1378 | | /* Protection for when ncp->chunk is huge; |
1379 | | * no need to read hugely. */ |
1380 | 0 | if(extent > 4096) |
1381 | 0 | extent = 4096; |
1382 | 0 | } |
1383 | 0 | else if(extent > ncp->chunk) |
1384 | 0 | extent = ncp->chunk; |
1385 | |
|
1386 | 0 | ps.offset = 0; |
1387 | 0 | ps.extent = extent; |
1388 | 0 | ps.base = NULL; |
1389 | 0 | ps.pos = ps.base; |
1390 | |
|
1391 | 0 | status = fault_v1hs(&ps, extent); |
1392 | 0 | if(status) |
1393 | 0 | return status; |
1394 | 0 | } |
1395 | 0 | else |
1396 | 0 | { |
1397 | 0 | ps.offset = offset; |
1398 | 0 | ps.extent = extent; |
1399 | 0 | ps.base = *xpp; |
1400 | 0 | ps.pos = ps.base; |
1401 | 0 | ps.end = (char *)ps.base + ps.extent; |
1402 | 0 | } |
1403 | | |
1404 | 0 | if (ps.version == 5) |
1405 | 0 | status = ncx_putn_schar_schar(&ps.pos, sizeof(ncmagic5), ncmagic5, NULL); |
1406 | 0 | else if (ps.version == 2) |
1407 | 0 | status = ncx_putn_schar_schar(&ps.pos, sizeof(ncmagic), ncmagic, NULL); |
1408 | 0 | else |
1409 | 0 | status = ncx_putn_schar_schar(&ps.pos, sizeof(ncmagic1), ncmagic1, NULL); |
1410 | 0 | if(status != NC_NOERR) |
1411 | 0 | goto release; |
1412 | | |
1413 | 0 | { |
1414 | 0 | const size_t nrecs = NC_get_numrecs(ncp); |
1415 | 0 | if (ps.version == 5) { |
1416 | 0 | unsigned long long tmp = (unsigned long long) nrecs; |
1417 | 0 | status = ncx_put_uint64(&ps.pos, tmp); |
1418 | 0 | } |
1419 | 0 | else |
1420 | 0 | status = ncx_put_size_t(&ps.pos, &nrecs); |
1421 | 0 | if(status != NC_NOERR) |
1422 | 0 | goto release; |
1423 | 0 | } |
1424 | | |
1425 | 0 | assert((char *)ps.pos < (char *)ps.end); |
1426 | |
|
1427 | 0 | status = v1h_put_NC_dimarray(&ps, &ncp->dims); |
1428 | 0 | if(status != NC_NOERR) |
1429 | 0 | goto release; |
1430 | | |
1431 | 0 | status = v1h_put_NC_attrarray(&ps, &ncp->attrs); |
1432 | 0 | if(status != NC_NOERR) |
1433 | 0 | goto release; |
1434 | | |
1435 | 0 | status = v1h_put_NC_vararray(&ps, &ncp->vars); |
1436 | 0 | if(status != NC_NOERR) |
1437 | 0 | goto release; |
1438 | | |
1439 | 0 | release: |
1440 | 0 | (void) rel_v1hs(&ps); |
1441 | |
|
1442 | 0 | return status; |
1443 | 0 | } |
1444 | | |
1445 | | |
1446 | | /* Make the in-memory NC structure from reading the file header */ |
1447 | | int |
1448 | | nc_get_NC(NC3_INFO* ncp) |
1449 | 70 | { |
1450 | 70 | int status; |
1451 | 70 | v1hs gs; /* the get stream */ |
1452 | | |
1453 | 70 | assert(ncp != NULL); |
1454 | | |
1455 | | /* Initialize stream gs */ |
1456 | | |
1457 | 70 | gs.nciop = ncp->nciop; |
1458 | 70 | gs.offset = 0; /* beginning of file */ |
1459 | 70 | gs.extent = 0; |
1460 | 70 | gs.flags = 0; |
1461 | 70 | gs.version = 0; |
1462 | 70 | gs.base = NULL; |
1463 | 70 | gs.pos = gs.base; |
1464 | | |
1465 | 70 | { |
1466 | | /* |
1467 | | * Come up with a reasonable stream read size. |
1468 | | */ |
1469 | 70 | off_t filesize; |
1470 | 70 | size_t extent = ncp->xsz; |
1471 | | |
1472 | 70 | if(extent <= ((fIsSet(ncp->flags, NC_64BIT_DATA))?MIN_NC5_XSZ:MIN_NC3_XSZ)) |
1473 | 70 | { |
1474 | 70 | status = ncio_filesize(ncp->nciop, &filesize); |
1475 | 70 | if(status) |
1476 | 0 | return status; |
1477 | 70 | if(filesize < sizeof(ncmagic)) { /* too small, not netcdf */ |
1478 | |
|
1479 | 0 | status = NC_ENOTNC; |
1480 | 0 | return status; |
1481 | 0 | } |
1482 | | /* first time read */ |
1483 | 70 | extent = ncp->chunk; |
1484 | | /* Protection for when ncp->chunk is huge; |
1485 | | * no need to read hugely. */ |
1486 | 70 | if(extent > 4096) |
1487 | 70 | extent = 4096; |
1488 | 70 | if(extent > filesize) |
1489 | 0 | extent = (size_t)filesize; |
1490 | 70 | } |
1491 | 0 | else if(extent > ncp->chunk) |
1492 | 0 | extent = ncp->chunk; |
1493 | | |
1494 | | /* |
1495 | | * Invalidate the I/O buffers to force a read of the header |
1496 | | * region. |
1497 | | */ |
1498 | 70 | status = ncio_sync(gs.nciop); |
1499 | 70 | if(status) |
1500 | 0 | return status; |
1501 | | |
1502 | 70 | status = fault_v1hs(&gs, extent); |
1503 | 70 | if(status) |
1504 | 0 | return status; |
1505 | 70 | } |
1506 | | |
1507 | | /* get the header from the stream gs */ |
1508 | | |
1509 | 70 | { |
1510 | | /* Get & check magic number */ |
1511 | 70 | schar magic[sizeof(ncmagic)]; |
1512 | 70 | (void) memset(magic, 0, sizeof(magic)); |
1513 | | |
1514 | 70 | status = ncx_getn_schar_schar( |
1515 | 70 | (const void **)(&gs.pos), sizeof(magic), magic); |
1516 | 70 | if(status != NC_NOERR) |
1517 | 0 | goto unwind_get; |
1518 | | |
1519 | 70 | if(memcmp(magic, ncmagic, sizeof(ncmagic)-1) != 0) |
1520 | 0 | { |
1521 | 0 | status = NC_ENOTNC; |
1522 | 0 | goto unwind_get; |
1523 | 0 | } |
1524 | | /* Check version number in last byte of magic */ |
1525 | 70 | if (magic[sizeof(ncmagic)-1] == 0x1) { |
1526 | 22 | gs.version = 1; |
1527 | 48 | } else if (magic[sizeof(ncmagic)-1] == 0x2) { |
1528 | 25 | gs.version = 2; |
1529 | 25 | fSet(ncp->flags, NC_64BIT_OFFSET); |
1530 | | /* Now we support version 2 file access on non-LFS systems -- rkr */ |
1531 | | #if 0 |
1532 | | if (sizeof(off_t) != 8) { |
1533 | | fprintf(stderr, "NETCDF WARNING: Version 2 file on 32-bit system.\n"); |
1534 | | } |
1535 | | #endif |
1536 | 25 | } else if (magic[sizeof(ncmagic)-1] == 0x5) { |
1537 | 23 | gs.version = 5; |
1538 | 23 | fSet(ncp->flags, NC_64BIT_DATA); |
1539 | 23 | } else { |
1540 | 0 | status = NC_ENOTNC; |
1541 | 0 | goto unwind_get; |
1542 | 0 | } |
1543 | 70 | } |
1544 | | |
1545 | 70 | { |
1546 | 70 | size_t nrecs = 0; |
1547 | 70 | if (gs.version == 5) { |
1548 | 23 | unsigned long long tmp = 0; |
1549 | 23 | status = ncx_get_uint64((const void **)(&gs.pos), &tmp); |
1550 | 23 | nrecs = (size_t)tmp; |
1551 | 23 | } |
1552 | 47 | else |
1553 | 47 | status = ncx_get_size_t((const void **)(&gs.pos), &nrecs); |
1554 | 70 | if(status != NC_NOERR) |
1555 | 0 | goto unwind_get; |
1556 | 70 | NC_set_numrecs(ncp, nrecs); |
1557 | 70 | } |
1558 | | |
1559 | 70 | assert((char *)gs.pos < (char *)gs.end); |
1560 | | |
1561 | 70 | status = v1h_get_NC_dimarray(&gs, &ncp->dims); |
1562 | 70 | if(status != NC_NOERR) |
1563 | 33 | goto unwind_get; |
1564 | | |
1565 | 37 | status = v1h_get_NC_attrarray(&gs, &ncp->attrs); |
1566 | 37 | if(status != NC_NOERR) |
1567 | 22 | goto unwind_get; |
1568 | | |
1569 | 15 | status = v1h_get_NC_vararray(&gs, &ncp->vars); |
1570 | 15 | if(status != NC_NOERR) |
1571 | 10 | goto unwind_get; |
1572 | | |
1573 | 5 | ncp->xsz = ncx_len_NC(ncp, (gs.version == 1) ? 4 : 8); |
1574 | | |
1575 | 5 | status = NC_computeshapes(ncp); |
1576 | 5 | if(status != NC_NOERR) |
1577 | 0 | goto unwind_get; |
1578 | | |
1579 | 5 | status = NC_check_vlens(ncp); |
1580 | 5 | if(status != NC_NOERR) |
1581 | 0 | goto unwind_get; |
1582 | | |
1583 | 5 | status = NC_check_voffs(ncp); |
1584 | 5 | if(status != NC_NOERR) |
1585 | 0 | goto unwind_get; |
1586 | | |
1587 | 70 | unwind_get: |
1588 | 70 | (void) rel_v1hs(&gs); |
1589 | 70 | return status; |
1590 | 5 | } |