Coverage Report

Created: 2026-08-13 07:06

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