Coverage Report

Created: 2025-06-09 08:44

/src/gdal/netcdf-c-4.7.4/libsrc4/nc4var.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2003-2018, University Corporation for Atmospheric
2
 * Research. See COPYRIGHT file for copying and redistribution
3
 * conditions.*/
4
/**
5
 * @file
6
 * @internal This file is part of netcdf-4, a netCDF-like interface
7
 * for HDF5, or a HDF5 backend for netCDF, depending on your point of
8
 * view. This file handles the NetCDF-4 variable functions.
9
 *
10
 * @author Ed Hartnett, Dennis Heimbigner, Ward Fisher
11
 */
12
13
#include "config.h"
14
#include <nc4internal.h>
15
#include "nc4dispatch.h"
16
#ifdef USE_HDF5
17
#include "hdf5internal.h"
18
#endif
19
#include <math.h>
20
21
/**
22
 * @internal This is called by nc_get_var_chunk_cache(). Get chunk
23
 * cache size for a variable.
24
 *
25
 * @param ncid File ID.
26
 * @param varid Variable ID.
27
 * @param sizep Gets size in bytes of cache.
28
 * @param nelemsp Gets number of element slots in cache.
29
 * @param preemptionp Gets cache swapping setting.
30
 *
31
 * @returns ::NC_NOERR No error.
32
 * @returns ::NC_EBADID Bad ncid.
33
 * @returns ::NC_ENOTVAR Invalid variable ID.
34
 * @returns ::NC_ENOTNC4 Not a netCDF-4 file.
35
 * @author Ed Hartnett
36
 */
37
int
38
NC4_get_var_chunk_cache(int ncid, int varid, size_t *sizep,
39
                        size_t *nelemsp, float *preemptionp)
40
71.5k
{
41
71.5k
    NC *nc;
42
71.5k
    NC_GRP_INFO_T *grp;
43
71.5k
    NC_FILE_INFO_T *h5;
44
71.5k
    NC_VAR_INFO_T *var;
45
71.5k
    int retval;
46
47
    /* Find info for this file and group, and set pointer to each. */
48
71.5k
    if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
49
0
        return retval;
50
71.5k
    assert(nc && grp && h5);
51
52
    /* Find the var. */
53
71.5k
    var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
54
71.5k
    if(!var)
55
0
        return NC_ENOTVAR;
56
71.5k
    assert(var && var->hdr.id == varid);
57
58
    /* Give the user what they want. */
59
71.5k
    if (sizep)
60
71.5k
        *sizep = var->chunk_cache_size;
61
71.5k
    if (nelemsp)
62
71.5k
        *nelemsp = var->chunk_cache_nelems;
63
71.5k
    if (preemptionp)
64
71.5k
        *preemptionp = var->chunk_cache_preemption;
65
66
71.5k
    return NC_NOERR;
67
71.5k
}
68
69
/**
70
 * @internal A wrapper for NC4_get_var_chunk_cache(), we need this
71
 * version for fortran.
72
 *
73
 * @param ncid File ID.
74
 * @param varid Variable ID.
75
 * @param sizep Gets size in MB of cache.
76
 * @param nelemsp Gets number of element slots in cache.
77
 * @param preemptionp Gets cache swapping setting.
78
 *
79
 * @returns ::NC_NOERR No error.
80
 * @returns ::NC_EBADID Bad ncid.
81
 * @returns ::NC_ENOTVAR Invalid variable ID.
82
 * @returns ::NC_ENOTNC4 Not a netCDF-4 file.
83
 * @author Ed Hartnett
84
 */
85
int
86
nc_get_var_chunk_cache_ints(int ncid, int varid, int *sizep,
87
                            int *nelemsp, int *preemptionp)
88
0
{
89
0
    size_t real_size, real_nelems;
90
0
    float real_preemption;
91
0
    int ret;
92
93
0
    if ((ret = NC4_get_var_chunk_cache(ncid, varid, &real_size,
94
0
                                       &real_nelems, &real_preemption)))
95
0
        return ret;
96
97
0
    if (sizep)
98
0
        *sizep = real_size / MEGABYTE;
99
0
    if (nelemsp)
100
0
        *nelemsp = (int)real_nelems;
101
0
    if(preemptionp)
102
0
        *preemptionp = (int)(real_preemption * 100);
103
104
0
    return NC_NOERR;
105
0
}
106
107
/**
108
 * @internal Get all the information about a variable. Pass NULL for
109
 * whatever you don't care about. This is the internal function called
110
 * by nc_inq_var(), nc_inq_var_deflate(), nc_inq_var_fletcher32(),
111
 * nc_inq_var_chunking(), nc_inq_var_chunking_ints(),
112
 * nc_inq_var_fill(), nc_inq_var_endian(), nc_inq_var_filter(), and
113
 * nc_inq_var_szip().
114
 *
115
 * @param ncid File ID.
116
 * @param varid Variable ID.
117
 * @param name Gets name.
118
 * @param xtypep Gets type.
119
 * @param ndimsp Gets number of dims.
120
 * @param dimidsp Gets array of dim IDs.
121
 * @param nattsp Gets number of attributes.
122
 * @param shufflep Gets shuffle setting.
123
 * @param deflatep Gets deflate setting.
124
 * @param deflate_levelp Gets deflate level.
125
 * @param fletcher32p Gets fletcher32 setting.
126
 * @param storagep Gets storage setting.
127
 * @param chunksizesp Gets chunksizes.
128
 * @param no_fill Gets fill mode.
129
 * @param fill_valuep Gets fill value.
130
 * @param endiannessp Gets one of ::NC_ENDIAN_BIG ::NC_ENDIAN_LITTLE
131
 * ::NC_ENDIAN_NATIVE
132
 * @param idp Pointer to memory to store filter id.
133
 * @param nparamsp Pointer to memory to store filter parameter count.
134
 * @param params Pointer to vector of unsigned integers into which
135
 * to store filter parameters.
136
 *
137
 * @returns ::NC_NOERR No error.
138
 * @returns ::NC_EBADID Bad ncid.
139
 * @returns ::NC_ENOTVAR Bad varid.
140
 * @returns ::NC_ENOMEM Out of memory.
141
 * @returns ::NC_EINVAL Invalid input.
142
 * @author Ed Hartnett, Dennis Heimbigner
143
 */
144
int
145
NC4_inq_var_all(int ncid, int varid, char *name, nc_type *xtypep,
146
                int *ndimsp, int *dimidsp, int *nattsp,
147
                int *shufflep, int *deflatep, int *deflate_levelp,
148
                int *fletcher32p, int *storagep, size_t *chunksizesp,
149
                int *no_fill, void *fill_valuep, int *endiannessp,
150
                unsigned int *idp, size_t *nparamsp, unsigned int *params)
151
1.13M
{
152
1.13M
    NC_GRP_INFO_T *grp;
153
1.13M
    NC_FILE_INFO_T *h5;
154
1.13M
    NC_VAR_INFO_T *var;
155
1.13M
    int d;
156
1.13M
    int retval;
157
158
1.13M
    LOG((2, "%s: ncid 0x%x varid %d", __func__, ncid, varid));
159
160
    /* Find info for this file and group, and set pointer to each. */
161
1.13M
    if ((retval = nc4_find_nc_grp_h5(ncid, NULL, &grp, &h5)))
162
0
        return retval;
163
1.13M
    assert(grp && h5);
164
165
    /* If the varid is -1, find the global atts and call it a day. */
166
1.13M
    if (varid == NC_GLOBAL && nattsp)
167
0
    {
168
0
        *nattsp = ncindexcount(grp->att);
169
0
        return NC_NOERR;
170
0
    }
171
172
    /* Find the var. */
173
1.13M
    if (!(var = (NC_VAR_INFO_T *)ncindexith(grp->vars, varid)))
174
0
        return NC_ENOTVAR;
175
1.13M
    assert(var && var->hdr.id == varid);
176
177
    /* Copy the data to the user's data buffers. */
178
1.13M
    if (name)
179
420k
        strcpy(name, var->hdr.name);
180
1.13M
    if (xtypep)
181
96.1k
        *xtypep = var->type_info->hdr.id;
182
1.13M
    if (ndimsp)
183
421k
        *ndimsp = var->ndims;
184
1.13M
    if (dimidsp)
185
273k
        for (d = 0; d < var->ndims; d++)
186
171k
            dimidsp[d] = var->dimids[d];
187
1.13M
    if (nattsp)
188
16.5k
        *nattsp = ncindexcount(var->att);
189
190
    /* Did the user want the chunksizes? */
191
1.13M
    if (var->storage == NC_CHUNKED && chunksizesp)
192
4.48k
    {
193
13.1k
        for (d = 0; d < var->ndims; d++)
194
8.62k
        {
195
8.62k
            chunksizesp[d] = var->chunksizes[d];
196
8.62k
            LOG((4, "chunksizesp[%d]=%d", d, chunksizesp[d]));
197
8.62k
        }
198
4.48k
    }
199
200
    /* Did the user inquire about the storage? */
201
1.13M
    if (storagep)
202
7.54k
  *storagep = var->storage;
203
204
    /* Filter stuff. */
205
1.13M
    if (shufflep)
206
71.5k
        *shufflep = (int)var->shuffle;
207
1.13M
    if (fletcher32p)
208
0
        *fletcher32p = (int)var->fletcher32;
209
210
1.13M
    if (deflatep)
211
0
  return NC_EFILTER;
212
213
1.13M
    if (idp) {
214
#if 0
215
        NC* nc = h5->controller;
216
  NC_FILTER_ACTION action;
217
  action.action = NCFILTER_INQ_FILTER;
218
  action.format = NC_FORMATX_NC_HDF5;
219
  action.id =  (idp)?*idp:0;
220
  action.nelems = (nparamsp)?*nparamsp:0;
221
  action.elems = params;
222
  if((retval = nc->dispatch->filter_actions(ncid,varid,&action)) == NC_NOERR) {
223
      if(idp) *idp = action.id;
224
      if(nparamsp) *nparamsp = action.nelems;
225
  }
226
  return retval;
227
#else
228
0
  return NC_EFILTER;
229
0
#endif
230
0
    }
231
232
    /* Fill value stuff. */
233
1.13M
    if (no_fill)
234
0
        *no_fill = (int)var->no_fill;
235
236
    /* Don't do a thing with fill_valuep if no_fill mode is set for
237
     * this var, or if fill_valuep is NULL. */
238
1.13M
    if (!var->no_fill && fill_valuep)
239
0
    {
240
        /* Do we have a fill value for this var? */
241
0
        if (var->fill_value)
242
0
        {
243
0
            if (var->type_info->nc_type_class == NC_STRING)
244
0
            {
245
0
                assert(*(char **)var->fill_value);
246
                /* This will allocate memory and copy the string. */
247
0
                if (!(*(char **)fill_valuep = strdup(*(char **)var->fill_value)))
248
0
                {
249
0
                    free(*(char **)fill_valuep);
250
0
                    return NC_ENOMEM;
251
0
                }
252
0
            }
253
0
            else
254
0
            {
255
0
                assert(var->type_info->size);
256
0
                memcpy(fill_valuep, var->fill_value, var->type_info->size);
257
0
            }
258
0
        }
259
0
        else
260
0
        {
261
0
            if (var->type_info->nc_type_class == NC_STRING)
262
0
            {
263
0
                if (!(*(char **)fill_valuep = calloc(1, sizeof(char *))))
264
0
                    return NC_ENOMEM;
265
266
0
                if ((retval = nc4_get_default_fill_value(var->type_info, (char **)fill_valuep)))
267
0
                {
268
0
                    free(*(char **)fill_valuep);
269
0
                    return retval;
270
0
                }
271
0
            }
272
0
            else
273
0
            {
274
0
                if ((retval = nc4_get_default_fill_value(var->type_info, fill_valuep)))
275
0
                    return retval;
276
0
            }
277
0
        }
278
0
    }
279
280
    /* Does the user want the endianness of this variable? */
281
1.13M
    if (endiannessp)
282
0
        *endiannessp = var->type_info->endianness;
283
284
1.13M
    return NC_NOERR;
285
1.13M
}
286
287
/**
288
 * @internal Inquire about chunking settings for a var. This is used
289
 * by the fortran API.
290
 *
291
 * @param ncid File ID.
292
 * @param varid Variable ID.
293
 * @param storagep Gets contiguous setting.
294
 * @param chunksizesp Gets chunksizes.
295
 *
296
 * @returns ::NC_NOERR No error.
297
 * @returns ::NC_EBADID Bad ncid.
298
 * @returns ::NC_ENOTVAR Invalid variable ID.
299
 * @returns ::NC_EINVAL Invalid input
300
 * @returns ::NC_ENOMEM Out of memory.
301
 * @author Ed Hartnett
302
 */
303
int
304
nc_inq_var_chunking_ints(int ncid, int varid, int *storagep, int *chunksizesp)
305
0
{
306
0
    NC_VAR_INFO_T *var;
307
0
    size_t *cs = NULL;
308
0
    int i, retval;
309
310
    /* Get pointer to the var. */
311
0
    if ((retval = nc4_find_grp_h5_var(ncid, varid, NULL, NULL, &var)))
312
0
        return retval;
313
0
    assert(var);
314
315
    /* Allocate space for the size_t copy of the chunksizes array. */
316
0
    if (var->ndims)
317
0
        if (!(cs = malloc(var->ndims * sizeof(size_t))))
318
0
            return NC_ENOMEM;
319
320
    /* Call the netcdf-4 version directly. */
321
0
    retval = NC4_inq_var_all(ncid, varid, NULL, NULL, NULL, NULL, NULL,
322
0
                             NULL, NULL, NULL, NULL, storagep, cs, NULL,
323
0
                             NULL, NULL, NULL, NULL, NULL);
324
325
    /* Copy from size_t array. */
326
0
    if (!retval && chunksizesp && var->storage == NC_CHUNKED)
327
0
    {
328
0
        for (i = 0; i < var->ndims; i++)
329
0
        {
330
0
            chunksizesp[i] = (int)cs[i];
331
0
            if (cs[i] > NC_MAX_INT)
332
0
                retval = NC_ERANGE;
333
0
        }
334
0
    }
335
336
0
    if (var->ndims)
337
0
        free(cs);
338
0
    return retval;
339
0
}
340
341
/**
342
 * @internal Find the ID of a variable, from the name. This function
343
 * is called by nc_inq_varid().
344
 *
345
 * @param ncid File ID.
346
 * @param name Name of the variable.
347
 * @param varidp Gets variable ID.
348
349
 * @returns ::NC_NOERR No error.
350
 * @returns ::NC_EBADID Bad ncid.
351
 * @returns ::NC_ENOTVAR Bad variable ID.
352
 */
353
int
354
NC4_inq_varid(int ncid, const char *name, int *varidp)
355
985k
{
356
985k
    NC *nc;
357
985k
    NC_GRP_INFO_T *grp;
358
985k
    NC_VAR_INFO_T *var;
359
985k
    char norm_name[NC_MAX_NAME + 1];
360
985k
    int retval;
361
362
985k
    if (!name)
363
0
        return NC_EINVAL;
364
985k
    if (!varidp)
365
0
        return NC_NOERR;
366
367
985k
    LOG((2, "%s: ncid 0x%x name %s", __func__, ncid, name));
368
369
    /* Find info for this file and group, and set pointer to each. */
370
985k
    if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, NULL)))
371
0
        return retval;
372
373
    /* Normalize name. */
374
985k
    if ((retval = nc4_normalize_name(name, norm_name)))
375
105k
        return retval;
376
377
    /* Find var of this name. */
378
880k
    var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,norm_name);
379
880k
    if(var)
380
182k
    {
381
182k
        *varidp = var->hdr.id;
382
182k
        return NC_NOERR;
383
182k
    }
384
698k
    return NC_ENOTVAR;
385
880k
}
386
387
/**
388
 * @internal
389
 *
390
 * This function will change the parallel access of a variable from
391
 * independent to collective.
392
 *
393
 * @param ncid File ID.
394
 * @param varid Variable ID.
395
 * @param par_access NC_COLLECTIVE or NC_INDEPENDENT.
396
 *
397
 * @returns ::NC_NOERR No error.
398
 * @returns ::NC_EBADID Invalid ncid passed.
399
 * @returns ::NC_ENOTVAR Invalid varid passed.
400
 * @returns ::NC_ENOPAR LFile was not opened with nc_open_par/nc_create_var.
401
 * @returns ::NC_EINVAL Invalid par_access specified.
402
 * @returns ::NC_NOERR for success
403
 * @author Ed Hartnett, Dennis Heimbigner
404
 */
405
int
406
NC4_var_par_access(int ncid, int varid, int par_access)
407
0
{
408
0
#ifndef USE_PARALLEL4
409
0
    NC_UNUSED(ncid);
410
0
    NC_UNUSED(varid);
411
0
    NC_UNUSED(par_access);
412
0
    return NC_ENOPAR;
413
#else
414
    NC *nc;
415
    NC_GRP_INFO_T *grp;
416
    NC_FILE_INFO_T *h5;
417
    NC_VAR_INFO_T *var;
418
    int retval;
419
420
    LOG((1, "%s: ncid 0x%x varid %d par_access %d", __func__, ncid,
421
         varid, par_access));
422
423
    if (par_access != NC_INDEPENDENT && par_access != NC_COLLECTIVE)
424
        return NC_EINVAL;
425
426
    /* Find info for this file and group, and set pointer to each. */
427
    if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
428
        return retval;
429
430
    /* This function only for files opened with nc_open_par or nc_create_par. */
431
    if (!h5->parallel)
432
        return NC_ENOPAR;
433
434
    /* Find the var, and set its preference. */
435
    var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
436
    if (!var) return NC_ENOTVAR;
437
    assert(var->hdr.id == varid);
438
439
    /* If zlib, shuffle, or fletcher32 filters are in use, then access
440
     * must be collective. Fail an attempt to set such a variable to
441
     * independent access. */
442
    if ((nclistlength(var->filters) > 0 || var->shuffle || var->fletcher32) &&
443
        par_access == NC_INDEPENDENT)
444
        return NC_EINVAL;
445
446
    if (par_access)
447
        var->parallel_access = NC_COLLECTIVE;
448
    else
449
        var->parallel_access = NC_INDEPENDENT;
450
    return NC_NOERR;
451
#endif /* USE_PARALLEL4 */
452
0
}
453
454
/**
455
 * @internal Copy data from one buffer to another, performing
456
 * appropriate data conversion.
457
 *
458
 * This function will copy data from one buffer to another, in
459
 * accordance with the types. Range errors will be noted, and the fill
460
 * value used (or the default fill value if none is supplied) for
461
 * values that overflow the type.
462
 *
463
 * @param src Pointer to source of data.
464
 * @param dest Pointer that gets data.
465
 * @param src_type Type ID of source data.
466
 * @param dest_type Type ID of destination data.
467
 * @param len Number of elements of data to copy.
468
 * @param range_error Pointer that gets 1 if there was a range error.
469
 * @param fill_value The fill value.
470
 * @param strict_nc3 Non-zero if strict model in effect.
471
 *
472
 * @returns NC_NOERR No error.
473
 * @returns NC_EBADTYPE Type not found.
474
 * @author Ed Hartnett, Dennis Heimbigner
475
 */
476
int
477
nc4_convert_type(const void *src, void *dest, const nc_type src_type,
478
                 const nc_type dest_type, const size_t len, int *range_error,
479
                 const void *fill_value, int strict_nc3)
480
29.1k
{
481
29.1k
    char *cp, *cp1;
482
29.1k
    float *fp, *fp1;
483
29.1k
    double *dp, *dp1;
484
29.1k
    int *ip, *ip1;
485
29.1k
    short *sp, *sp1;
486
29.1k
    signed char *bp, *bp1;
487
29.1k
    unsigned char *ubp, *ubp1;
488
29.1k
    unsigned short *usp, *usp1;
489
29.1k
    unsigned int *uip, *uip1;
490
29.1k
    long long *lip, *lip1;
491
29.1k
    unsigned long long *ulip, *ulip1;
492
29.1k
    size_t count = 0;
493
494
29.1k
    *range_error = 0;
495
29.1k
    LOG((3, "%s: len %d src_type %d dest_type %d", __func__, len, src_type,
496
29.1k
         dest_type));
497
498
    /* OK, this is ugly. If you can think of anything better, I'm open
499
       to suggestions!
500
501
       Note that we don't use a default fill value for type
502
       NC_BYTE. This is because Lord Voldemort cast a nofilleramous spell
503
       at Harry Potter, but it bounced off his scar and hit the netcdf-4
504
       code.
505
    */
506
29.1k
    switch (src_type)
507
29.1k
    {
508
8.11k
    case NC_CHAR:
509
8.11k
        switch (dest_type)
510
8.11k
        {
511
8.11k
        case NC_CHAR:
512
11.4M
            for (cp = (char *)src, cp1 = dest; count < len; count++)
513
11.4M
                *cp1++ = *cp++;
514
8.11k
            break;
515
0
        default:
516
0
            LOG((0, "%s: Unknown destination type.", __func__));
517
8.11k
        }
518
8.11k
        break;
519
520
8.11k
    case NC_BYTE:
521
3.73k
        switch (dest_type)
522
3.73k
        {
523
3.69k
        case NC_BYTE:
524
7.38k
            for (bp = (signed char *)src, bp1 = dest; count < len; count++)
525
3.69k
                *bp1++ = *bp++;
526
3.69k
            break;
527
41
        case NC_UBYTE:
528
1.82k
            for (bp = (signed char *)src, ubp = dest; count < len; count++)
529
1.78k
            {
530
1.78k
                if (*bp < 0)
531
440
                    (*range_error)++;
532
1.78k
                *ubp++ = *bp++;
533
1.78k
            }
534
41
            break;
535
0
        case NC_SHORT:
536
0
            for (bp = (signed char *)src, sp = dest; count < len; count++)
537
0
                *sp++ = *bp++;
538
0
            break;
539
0
        case NC_USHORT:
540
0
            for (bp = (signed char *)src, usp = dest; count < len; count++)
541
0
            {
542
0
                if (*bp < 0)
543
0
                    (*range_error)++;
544
0
                *usp++ = *bp++;
545
0
            }
546
0
            break;
547
0
        case NC_INT:
548
0
            for (bp = (signed char *)src, ip = dest; count < len; count++)
549
0
                *ip++ = *bp++;
550
0
            break;
551
0
        case NC_UINT:
552
0
            for (bp = (signed char *)src, uip = dest; count < len; count++)
553
0
            {
554
0
                if (*bp < 0)
555
0
                    (*range_error)++;
556
0
                *uip++ = *bp++;
557
0
            }
558
0
            break;
559
0
        case NC_INT64:
560
0
            for (bp = (signed char *)src, lip = dest; count < len; count++)
561
0
                *lip++ = *bp++;
562
0
            break;
563
0
        case NC_UINT64:
564
0
            for (bp = (signed char *)src, ulip = dest; count < len; count++)
565
0
            {
566
0
                if (*bp < 0)
567
0
                    (*range_error)++;
568
0
                *ulip++ = *bp++;
569
0
            }
570
0
            break;
571
0
        case NC_FLOAT:
572
0
            for (bp = (signed char *)src, fp = dest; count < len; count++)
573
0
                *fp++ = *bp++;
574
0
            break;
575
0
        case NC_DOUBLE:
576
0
            for (bp = (signed char *)src, dp = dest; count < len; count++)
577
0
                *dp++ = *bp++;
578
0
            break;
579
0
        default:
580
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
581
0
                 __func__, src_type, dest_type));
582
0
            return NC_EBADTYPE;
583
3.73k
        }
584
3.73k
        break;
585
586
3.73k
    case NC_UBYTE:
587
0
        switch (dest_type)
588
0
        {
589
0
        case NC_BYTE:
590
0
            for (ubp = (unsigned char *)src, bp = dest; count < len; count++)
591
0
            {
592
0
                if (!strict_nc3 && *ubp > X_SCHAR_MAX)
593
0
                    (*range_error)++;
594
0
                *bp++ = *ubp++;
595
0
            }
596
0
            break;
597
0
        case NC_SHORT:
598
0
            for (ubp = (unsigned char *)src, sp = dest; count < len; count++)
599
0
                *sp++ = *ubp++;
600
0
            break;
601
0
        case NC_UBYTE:
602
0
            for (ubp = (unsigned char *)src, ubp1 = dest; count < len; count++)
603
0
                *ubp1++ = *ubp++;
604
0
            break;
605
0
        case NC_USHORT:
606
0
            for (ubp = (unsigned char *)src, usp = dest; count < len; count++)
607
0
                *usp++ = *ubp++;
608
0
            break;
609
0
        case NC_INT:
610
0
            for (ubp = (unsigned char *)src, ip = dest; count < len; count++)
611
0
                *ip++ = *ubp++;
612
0
            break;
613
0
        case NC_UINT:
614
0
            for (ubp = (unsigned char *)src, uip = dest; count < len; count++)
615
0
                *uip++ = *ubp++;
616
0
            break;
617
0
        case NC_INT64:
618
0
            for (ubp = (unsigned char *)src, lip = dest; count < len; count++)
619
0
                *lip++ = *ubp++;
620
0
            break;
621
0
        case NC_UINT64:
622
0
            for (ubp = (unsigned char *)src, ulip = dest; count < len; count++)
623
0
                *ulip++ = *ubp++;
624
0
            break;
625
0
        case NC_FLOAT:
626
0
            for (ubp = (unsigned char *)src, fp = dest; count < len; count++)
627
0
                *fp++ = *ubp++;
628
0
            break;
629
0
        case NC_DOUBLE:
630
0
            for (ubp = (unsigned char *)src, dp = dest; count < len; count++)
631
0
                *dp++ = *ubp++;
632
0
            break;
633
0
        default:
634
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
635
0
                 __func__, src_type, dest_type));
636
0
            return NC_EBADTYPE;
637
0
        }
638
0
        break;
639
640
3.46k
    case NC_SHORT:
641
3.46k
        switch (dest_type)
642
3.46k
        {
643
0
        case NC_UBYTE:
644
0
            for (sp = (short *)src, ubp = dest; count < len; count++)
645
0
            {
646
0
                if (*sp > X_UCHAR_MAX || *sp < 0)
647
0
                    (*range_error)++;
648
0
                *ubp++ = *sp++;
649
0
            }
650
0
            break;
651
0
        case NC_BYTE:
652
0
            for (sp = (short *)src, bp = dest; count < len; count++)
653
0
            {
654
0
                if (*sp > X_SCHAR_MAX || *sp < X_SCHAR_MIN)
655
0
                    (*range_error)++;
656
0
                *bp++ = *sp++;
657
0
            }
658
0
            break;
659
3.46k
        case NC_SHORT:
660
6.92k
            for (sp = (short *)src, sp1 = dest; count < len; count++)
661
3.46k
                *sp1++ = *sp++;
662
3.46k
            break;
663
0
        case NC_USHORT:
664
0
            for (sp = (short *)src, usp = dest; count < len; count++)
665
0
            {
666
0
                if (*sp < 0)
667
0
                    (*range_error)++;
668
0
                *usp++ = *sp++;
669
0
            }
670
0
            break;
671
0
        case NC_INT:
672
0
            for (sp = (short *)src, ip = dest; count < len; count++)
673
0
                *ip++ = *sp++;
674
0
            break;
675
0
        case NC_UINT:
676
0
            for (sp = (short *)src, uip = dest; count < len; count++)
677
0
            {
678
0
                if (*sp < 0)
679
0
                    (*range_error)++;
680
0
                *uip++ = *sp++;
681
0
            }
682
0
            break;
683
0
        case NC_INT64:
684
0
            for (sp = (short *)src, lip = dest; count < len; count++)
685
0
                *lip++ = *sp++;
686
0
            break;
687
0
        case NC_UINT64:
688
0
            for (sp = (short *)src, ulip = dest; count < len; count++)
689
0
            {
690
0
                if (*sp < 0)
691
0
                    (*range_error)++;
692
0
                *ulip++ = *sp++;
693
0
            }
694
0
            break;
695
0
        case NC_FLOAT:
696
0
            for (sp = (short *)src, fp = dest; count < len; count++)
697
0
                *fp++ = *sp++;
698
0
            break;
699
0
        case NC_DOUBLE:
700
0
            for (sp = (short *)src, dp = dest; count < len; count++)
701
0
                *dp++ = *sp++;
702
0
            break;
703
0
        default:
704
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
705
0
                 __func__, src_type, dest_type));
706
0
            return NC_EBADTYPE;
707
3.46k
        }
708
3.46k
        break;
709
710
3.46k
    case NC_USHORT:
711
0
        switch (dest_type)
712
0
        {
713
0
        case NC_UBYTE:
714
0
            for (usp = (unsigned short *)src, ubp = dest; count < len; count++)
715
0
            {
716
0
                if (*usp > X_UCHAR_MAX)
717
0
                    (*range_error)++;
718
0
                *ubp++ = *usp++;
719
0
            }
720
0
            break;
721
0
        case NC_BYTE:
722
0
            for (usp = (unsigned short *)src, bp = dest; count < len; count++)
723
0
            {
724
0
                if (*usp > X_SCHAR_MAX)
725
0
                    (*range_error)++;
726
0
                *bp++ = *usp++;
727
0
            }
728
0
            break;
729
0
        case NC_SHORT:
730
0
            for (usp = (unsigned short *)src, sp = dest; count < len; count++)
731
0
            {
732
0
                if (*usp > X_SHORT_MAX)
733
0
                    (*range_error)++;
734
0
                *sp++ = *usp++;
735
0
            }
736
0
            break;
737
0
        case NC_USHORT:
738
0
            for (usp = (unsigned short *)src, usp1 = dest; count < len; count++)
739
0
                *usp1++ = *usp++;
740
0
            break;
741
0
        case NC_INT:
742
0
            for (usp = (unsigned short *)src, ip = dest; count < len; count++)
743
0
                *ip++ = *usp++;
744
0
            break;
745
0
        case NC_UINT:
746
0
            for (usp = (unsigned short *)src, uip = dest; count < len; count++)
747
0
                *uip++ = *usp++;
748
0
            break;
749
0
        case NC_INT64:
750
0
            for (usp = (unsigned short *)src, lip = dest; count < len; count++)
751
0
                *lip++ = *usp++;
752
0
            break;
753
0
        case NC_UINT64:
754
0
            for (usp = (unsigned short *)src, ulip = dest; count < len; count++)
755
0
                *ulip++ = *usp++;
756
0
            break;
757
0
        case NC_FLOAT:
758
0
            for (usp = (unsigned short *)src, fp = dest; count < len; count++)
759
0
                *fp++ = *usp++;
760
0
            break;
761
0
        case NC_DOUBLE:
762
0
            for (usp = (unsigned short *)src, dp = dest; count < len; count++)
763
0
                *dp++ = *usp++;
764
0
            break;
765
0
        default:
766
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
767
0
                 __func__, src_type, dest_type));
768
0
            return NC_EBADTYPE;
769
0
        }
770
0
        break;
771
772
12.4k
    case NC_INT:
773
12.4k
        switch (dest_type)
774
12.4k
        {
775
0
        case NC_UBYTE:
776
0
            for (ip = (int *)src, ubp = dest; count < len; count++)
777
0
            {
778
0
                if (*ip > X_UCHAR_MAX || *ip < 0)
779
0
                    (*range_error)++;
780
0
                *ubp++ = *ip++;
781
0
            }
782
0
            break;
783
0
        case NC_BYTE:
784
0
            for (ip = (int *)src, bp = dest; count < len; count++)
785
0
            {
786
0
                if (*ip > X_SCHAR_MAX || *ip < X_SCHAR_MIN)
787
0
                    (*range_error)++;
788
0
                *bp++ = *ip++;
789
0
            }
790
0
            break;
791
0
        case NC_SHORT:
792
0
            for (ip = (int *)src, sp = dest; count < len; count++)
793
0
            {
794
0
                if (*ip > X_SHORT_MAX || *ip < X_SHORT_MIN)
795
0
                    (*range_error)++;
796
0
                *sp++ = *ip++;
797
0
            }
798
0
            break;
799
0
        case NC_USHORT:
800
0
            for (ip = (int *)src, usp = dest; count < len; count++)
801
0
            {
802
0
                if (*ip > X_USHORT_MAX || *ip < 0)
803
0
                    (*range_error)++;
804
0
                *usp++ = *ip++;
805
0
            }
806
0
            break;
807
12.4k
        case NC_INT: /* src is int */
808
24.9k
            for (ip = (int *)src, ip1 = dest; count < len; count++)
809
12.4k
            {
810
12.4k
                if (*ip > X_INT_MAX || *ip < X_INT_MIN)
811
0
                    (*range_error)++;
812
12.4k
                *ip1++ = *ip++;
813
12.4k
            }
814
12.4k
            break;
815
0
        case NC_UINT:
816
0
            for (ip = (int *)src, uip = dest; count < len; count++)
817
0
            {
818
0
                if (*ip > X_UINT_MAX || *ip < 0)
819
0
                    (*range_error)++;
820
0
                *uip++ = *ip++;
821
0
            }
822
0
            break;
823
0
        case NC_INT64:
824
0
            for (ip = (int *)src, lip = dest; count < len; count++)
825
0
                *lip++ = *ip++;
826
0
            break;
827
0
        case NC_UINT64:
828
0
            for (ip = (int *)src, ulip = dest; count < len; count++)
829
0
            {
830
0
                if (*ip < 0)
831
0
                    (*range_error)++;
832
0
                *ulip++ = *ip++;
833
0
            }
834
0
            break;
835
0
        case NC_FLOAT:
836
0
            for (ip = (int *)src, fp = dest; count < len; count++)
837
0
                *fp++ = *ip++;
838
0
            break;
839
0
        case NC_DOUBLE:
840
0
            for (ip = (int *)src, dp = dest; count < len; count++)
841
0
                *dp++ = *ip++;
842
0
            break;
843
0
        default:
844
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
845
0
                 __func__, src_type, dest_type));
846
0
            return NC_EBADTYPE;
847
12.4k
        }
848
12.4k
        break;
849
850
12.4k
    case NC_UINT:
851
0
        switch (dest_type)
852
0
        {
853
0
        case NC_UBYTE:
854
0
            for (uip = (unsigned int *)src, ubp = dest; count < len; count++)
855
0
            {
856
0
                if (*uip > X_UCHAR_MAX)
857
0
                    (*range_error)++;
858
0
                *ubp++ = *uip++;
859
0
            }
860
0
            break;
861
0
        case NC_BYTE:
862
0
            for (uip = (unsigned int *)src, bp = dest; count < len; count++)
863
0
            {
864
0
                if (*uip > X_SCHAR_MAX)
865
0
                    (*range_error)++;
866
0
                *bp++ = *uip++;
867
0
            }
868
0
            break;
869
0
        case NC_SHORT:
870
0
            for (uip = (unsigned int *)src, sp = dest; count < len; count++)
871
0
            {
872
0
                if (*uip > X_SHORT_MAX)
873
0
                    (*range_error)++;
874
0
                *sp++ = *uip++;
875
0
            }
876
0
            break;
877
0
        case NC_USHORT:
878
0
            for (uip = (unsigned int *)src, usp = dest; count < len; count++)
879
0
            {
880
0
                if (*uip > X_USHORT_MAX)
881
0
                    (*range_error)++;
882
0
                *usp++ = *uip++;
883
0
            }
884
0
            break;
885
0
        case NC_INT:
886
0
            for (uip = (unsigned int *)src, ip = dest; count < len; count++)
887
0
            {
888
0
                if (*uip > X_INT_MAX)
889
0
                    (*range_error)++;
890
0
                *ip++ = *uip++;
891
0
            }
892
0
            break;
893
0
        case NC_UINT:
894
0
            for (uip = (unsigned int *)src, uip1 = dest; count < len; count++)
895
0
            {
896
0
                if (*uip > X_UINT_MAX)
897
0
                    (*range_error)++;
898
0
                *uip1++ = *uip++;
899
0
            }
900
0
            break;
901
0
        case NC_INT64:
902
0
            for (uip = (unsigned int *)src, lip = dest; count < len; count++)
903
0
                *lip++ = *uip++;
904
0
            break;
905
0
        case NC_UINT64:
906
0
            for (uip = (unsigned int *)src, ulip = dest; count < len; count++)
907
0
                *ulip++ = *uip++;
908
0
            break;
909
0
        case NC_FLOAT:
910
0
            for (uip = (unsigned int *)src, fp = dest; count < len; count++)
911
0
                *fp++ = *uip++;
912
0
            break;
913
0
        case NC_DOUBLE:
914
0
            for (uip = (unsigned int *)src, dp = dest; count < len; count++)
915
0
                *dp++ = *uip++;
916
0
            break;
917
0
        default:
918
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
919
0
                 __func__, src_type, dest_type));
920
0
            return NC_EBADTYPE;
921
0
        }
922
0
        break;
923
924
0
    case NC_INT64:
925
0
        switch (dest_type)
926
0
        {
927
0
        case NC_UBYTE:
928
0
            for (lip = (long long *)src, ubp = dest; count < len; count++)
929
0
            {
930
0
                if (*lip > X_UCHAR_MAX || *lip < 0)
931
0
                    (*range_error)++;
932
0
                *ubp++ = *lip++;
933
0
            }
934
0
            break;
935
0
        case NC_BYTE:
936
0
            for (lip = (long long *)src, bp = dest; count < len; count++)
937
0
            {
938
0
                if (*lip > X_SCHAR_MAX || *lip < X_SCHAR_MIN)
939
0
                    (*range_error)++;
940
0
                *bp++ = *lip++;
941
0
            }
942
0
            break;
943
0
        case NC_SHORT:
944
0
            for (lip = (long long *)src, sp = dest; count < len; count++)
945
0
            {
946
0
                if (*lip > X_SHORT_MAX || *lip < X_SHORT_MIN)
947
0
                    (*range_error)++;
948
0
                *sp++ = *lip++;
949
0
            }
950
0
            break;
951
0
        case NC_USHORT:
952
0
            for (lip = (long long *)src, usp = dest; count < len; count++)
953
0
            {
954
0
                if (*lip > X_USHORT_MAX || *lip < 0)
955
0
                    (*range_error)++;
956
0
                *usp++ = *lip++;
957
0
            }
958
0
            break;
959
0
        case NC_UINT:
960
0
            for (lip = (long long *)src, uip = dest; count < len; count++)
961
0
            {
962
0
                if (*lip > X_UINT_MAX || *lip < 0)
963
0
                    (*range_error)++;
964
0
                *uip++ = *lip++;
965
0
            }
966
0
            break;
967
0
        case NC_INT:
968
0
            for (lip = (long long *)src, ip = dest; count < len; count++)
969
0
            {
970
0
                if (*lip > X_INT_MAX || *lip < X_INT_MIN)
971
0
                    (*range_error)++;
972
0
                *ip++ = *lip++;
973
0
            }
974
0
            break;
975
0
        case NC_INT64:
976
0
            for (lip = (long long *)src, lip1 = dest; count < len; count++)
977
0
                *lip1++ = *lip++;
978
0
            break;
979
0
        case NC_UINT64:
980
0
            for (lip = (long long *)src, ulip = dest; count < len; count++)
981
0
            {
982
0
                if (*lip < 0)
983
0
                    (*range_error)++;
984
0
                *ulip++ = *lip++;
985
0
            }
986
0
            break;
987
0
        case NC_FLOAT:
988
0
            for (lip = (long long *)src, fp = dest; count < len; count++)
989
0
                *fp++ = *lip++;
990
0
            break;
991
0
        case NC_DOUBLE:
992
0
            for (lip = (long long *)src, dp = dest; count < len; count++)
993
0
                *dp++ = *lip++;
994
0
            break;
995
0
        default:
996
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
997
0
                 __func__, src_type, dest_type));
998
0
            return NC_EBADTYPE;
999
0
        }
1000
0
        break;
1001
1002
0
    case NC_UINT64:
1003
0
        switch (dest_type)
1004
0
        {
1005
0
        case NC_UBYTE:
1006
0
            for (ulip = (unsigned long long *)src, ubp = dest; count < len; count++)
1007
0
            {
1008
0
                if (*ulip > X_UCHAR_MAX)
1009
0
                    (*range_error)++;
1010
0
                *ubp++ = *ulip++;
1011
0
            }
1012
0
            break;
1013
0
        case NC_BYTE:
1014
0
            for (ulip = (unsigned long long *)src, bp = dest; count < len; count++)
1015
0
            {
1016
0
                if (*ulip > X_SCHAR_MAX)
1017
0
                    (*range_error)++;
1018
0
                *bp++ = *ulip++;
1019
0
            }
1020
0
            break;
1021
0
        case NC_SHORT:
1022
0
            for (ulip = (unsigned long long *)src, sp = dest; count < len; count++)
1023
0
            {
1024
0
                if (*ulip > X_SHORT_MAX)
1025
0
                    (*range_error)++;
1026
0
                *sp++ = *ulip++;
1027
0
            }
1028
0
            break;
1029
0
        case NC_USHORT:
1030
0
            for (ulip = (unsigned long long *)src, usp = dest; count < len; count++)
1031
0
            {
1032
0
                if (*ulip > X_USHORT_MAX)
1033
0
                    (*range_error)++;
1034
0
                *usp++ = *ulip++;
1035
0
            }
1036
0
            break;
1037
0
        case NC_UINT:
1038
0
            for (ulip = (unsigned long long *)src, uip = dest; count < len; count++)
1039
0
            {
1040
0
                if (*ulip > X_UINT_MAX)
1041
0
                    (*range_error)++;
1042
0
                *uip++ = *ulip++;
1043
0
            }
1044
0
            break;
1045
0
        case NC_INT:
1046
0
            for (ulip = (unsigned long long *)src, ip = dest; count < len; count++)
1047
0
            {
1048
0
                if (*ulip > X_INT_MAX)
1049
0
                    (*range_error)++;
1050
0
                *ip++ = *ulip++;
1051
0
            }
1052
0
            break;
1053
0
        case NC_INT64:
1054
0
            for (ulip = (unsigned long long *)src, lip = dest; count < len; count++)
1055
0
            {
1056
0
                if (*ulip > X_INT64_MAX)
1057
0
                    (*range_error)++;
1058
0
                *lip++ = *ulip++;
1059
0
            }
1060
0
            break;
1061
0
        case NC_UINT64:
1062
0
            for (ulip = (unsigned long long *)src, ulip1 = dest; count < len; count++)
1063
0
                *ulip1++ = *ulip++;
1064
0
            break;
1065
0
        case NC_FLOAT:
1066
0
            for (ulip = (unsigned long long *)src, fp = dest; count < len; count++)
1067
0
                *fp++ = *ulip++;
1068
0
            break;
1069
0
        case NC_DOUBLE:
1070
0
            for (ulip = (unsigned long long *)src, dp = dest; count < len; count++)
1071
0
                *dp++ = *ulip++;
1072
0
            break;
1073
0
        default:
1074
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1075
0
                 __func__, src_type, dest_type));
1076
0
            return NC_EBADTYPE;
1077
0
        }
1078
0
        break;
1079
1080
0
    case NC_FLOAT:
1081
0
        switch (dest_type)
1082
0
        {
1083
0
        case NC_UBYTE:
1084
0
            for (fp = (float *)src, ubp = dest; count < len; count++)
1085
0
            {
1086
0
                if (*fp > X_UCHAR_MAX || *fp < 0)
1087
0
                    (*range_error)++;
1088
0
                *ubp++ = *fp++;
1089
0
            }
1090
0
            break;
1091
0
        case NC_BYTE:
1092
0
            for (fp = (float *)src, bp = dest; count < len; count++)
1093
0
            {
1094
0
                if (*fp > (double)X_SCHAR_MAX || *fp < (double)X_SCHAR_MIN)
1095
0
                    (*range_error)++;
1096
0
                *bp++ = *fp++;
1097
0
            }
1098
0
            break;
1099
0
        case NC_SHORT:
1100
0
            for (fp = (float *)src, sp = dest; count < len; count++)
1101
0
            {
1102
0
                if (*fp > (double)X_SHORT_MAX || *fp < (double)X_SHORT_MIN)
1103
0
                    (*range_error)++;
1104
0
                *sp++ = *fp++;
1105
0
            }
1106
0
            break;
1107
0
        case NC_USHORT:
1108
0
            for (fp = (float *)src, usp = dest; count < len; count++)
1109
0
            {
1110
0
                if (*fp > X_USHORT_MAX || *fp < 0)
1111
0
                    (*range_error)++;
1112
0
                *usp++ = *fp++;
1113
0
            }
1114
0
            break;
1115
0
        case NC_UINT:
1116
0
            for (fp = (float *)src, uip = dest; count < len; count++)
1117
0
            {
1118
0
                if (*fp > X_UINT_MAX || *fp < 0)
1119
0
                    (*range_error)++;
1120
0
                *uip++ = *fp++;
1121
0
            }
1122
0
            break;
1123
0
        case NC_INT:
1124
0
            for (fp = (float *)src, ip = dest; count < len; count++)
1125
0
            {
1126
0
                if (*fp > (double)X_INT_MAX || *fp < (double)X_INT_MIN)
1127
0
                    (*range_error)++;
1128
0
                *ip++ = *fp++;
1129
0
            }
1130
0
            break;
1131
0
        case NC_INT64:
1132
0
            for (fp = (float *)src, lip = dest; count < len; count++)
1133
0
            {
1134
0
                if (*fp > X_INT64_MAX || *fp <X_INT64_MIN)
1135
0
                    (*range_error)++;
1136
0
                *lip++ = *fp++;
1137
0
            }
1138
0
            break;
1139
0
        case NC_UINT64:
1140
0
            for (fp = (float *)src, lip = dest; count < len; count++)
1141
0
            {
1142
0
                if (*fp > X_UINT64_MAX || *fp < 0)
1143
0
                    (*range_error)++;
1144
0
                *lip++ = *fp++;
1145
0
            }
1146
0
            break;
1147
0
        case NC_FLOAT:
1148
0
            for (fp = (float *)src, fp1 = dest; count < len; count++)
1149
0
            {
1150
                /*                if (*fp > X_FLOAT_MAX || *fp < X_FLOAT_MIN)
1151
                                  (*range_error)++;*/
1152
0
                *fp1++ = *fp++;
1153
0
            }
1154
0
            break;
1155
0
        case NC_DOUBLE:
1156
0
            for (fp = (float *)src, dp = dest; count < len; count++)
1157
0
                *dp++ = *fp++;
1158
0
            break;
1159
0
        default:
1160
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1161
0
                 __func__, src_type, dest_type));
1162
0
            return NC_EBADTYPE;
1163
0
        }
1164
0
        break;
1165
1166
1.37k
    case NC_DOUBLE:
1167
1.37k
        switch (dest_type)
1168
1.37k
        {
1169
0
        case NC_UBYTE:
1170
0
            for (dp = (double *)src, ubp = dest; count < len; count++)
1171
0
            {
1172
0
                if (*dp > X_UCHAR_MAX || *dp < 0)
1173
0
                    (*range_error)++;
1174
0
                *ubp++ = *dp++;
1175
0
            }
1176
0
            break;
1177
0
        case NC_BYTE:
1178
0
            for (dp = (double *)src, bp = dest; count < len; count++)
1179
0
            {
1180
0
                if (*dp > X_SCHAR_MAX || *dp < X_SCHAR_MIN)
1181
0
                    (*range_error)++;
1182
0
                *bp++ = *dp++;
1183
0
            }
1184
0
            break;
1185
0
        case NC_SHORT:
1186
0
            for (dp = (double *)src, sp = dest; count < len; count++)
1187
0
            {
1188
0
                if (*dp > X_SHORT_MAX || *dp < X_SHORT_MIN)
1189
0
                    (*range_error)++;
1190
0
                *sp++ = *dp++;
1191
0
            }
1192
0
            break;
1193
0
        case NC_USHORT:
1194
0
            for (dp = (double *)src, usp = dest; count < len; count++)
1195
0
            {
1196
0
                if (*dp > X_USHORT_MAX || *dp < 0)
1197
0
                    (*range_error)++;
1198
0
                *usp++ = *dp++;
1199
0
            }
1200
0
            break;
1201
0
        case NC_UINT:
1202
0
            for (dp = (double *)src, uip = dest; count < len; count++)
1203
0
            {
1204
0
                if (*dp > X_UINT_MAX || *dp < 0)
1205
0
                    (*range_error)++;
1206
0
                *uip++ = *dp++;
1207
0
            }
1208
0
            break;
1209
0
        case NC_INT:
1210
0
            for (dp = (double *)src, ip = dest; count < len; count++)
1211
0
            {
1212
0
                if (*dp > X_INT_MAX || *dp < X_INT_MIN)
1213
0
                    (*range_error)++;
1214
0
                *ip++ = *dp++;
1215
0
            }
1216
0
            break;
1217
0
        case NC_INT64:
1218
0
            for (dp = (double *)src, lip = dest; count < len; count++)
1219
0
            {
1220
0
                if (*dp > X_INT64_MAX || *dp < X_INT64_MIN)
1221
0
                    (*range_error)++;
1222
0
                *lip++ = *dp++;
1223
0
            }
1224
0
            break;
1225
0
        case NC_UINT64:
1226
0
            for (dp = (double *)src, lip = dest; count < len; count++)
1227
0
            {
1228
0
                if (*dp > X_UINT64_MAX || *dp < 0)
1229
0
                    (*range_error)++;
1230
0
                *lip++ = *dp++;
1231
0
            }
1232
0
            break;
1233
0
        case NC_FLOAT:
1234
0
            for (dp = (double *)src, fp = dest; count < len; count++)
1235
0
            {
1236
0
                if (isgreater(*dp, X_FLOAT_MAX) || isless(*dp, X_FLOAT_MIN))
1237
0
                    (*range_error)++;
1238
0
                *fp++ = *dp++;
1239
0
            }
1240
0
            break;
1241
1.37k
        case NC_DOUBLE:
1242
2.74k
            for (dp = (double *)src, dp1 = dest; count < len; count++)
1243
1.37k
            {
1244
                /* if (*dp > X_DOUBLE_MAX || *dp < X_DOUBLE_MIN) */
1245
                /*    (*range_error)++; */
1246
1.37k
                *dp1++ = *dp++;
1247
1.37k
            }
1248
1.37k
            break;
1249
0
        default:
1250
0
            LOG((0, "%s: unexpected dest type. src_type %d, dest_type %d",
1251
0
                 __func__, src_type, dest_type));
1252
0
            return NC_EBADTYPE;
1253
1.37k
        }
1254
1.37k
        break;
1255
1256
1.37k
    default:
1257
0
        LOG((0, "%s: unexpected src type. src_type %d, dest_type %d",
1258
0
             __func__, src_type, dest_type));
1259
0
        return NC_EBADTYPE;
1260
29.1k
    }
1261
29.1k
    return NC_NOERR;
1262
29.1k
}
1263
1264
/**
1265
 * @internal Get the default fill value for an atomic type. Memory for
1266
 * fill_value must already be allocated, or you are DOOMED!
1267
 *
1268
 * @param type_info Pointer to type info struct.
1269
 * @param fill_value Pointer that gets the default fill value.
1270
 *
1271
 * @returns NC_NOERR No error.
1272
 * @returns NC_EINVAL Can't find atomic type.
1273
 * @author Ed Hartnett
1274
 */
1275
int
1276
nc4_get_default_fill_value(const NC_TYPE_INFO_T *type_info, void *fill_value)
1277
69.0k
{
1278
69.0k
    switch (type_info->hdr.id)
1279
69.0k
    {
1280
5.34k
    case NC_CHAR:
1281
5.34k
        *(char *)fill_value = NC_FILL_CHAR;
1282
5.34k
        break;
1283
1284
0
    case NC_STRING:
1285
0
        *(char **)fill_value = strdup(NC_FILL_STRING);
1286
0
        break;
1287
1288
60.3k
    case NC_BYTE:
1289
60.3k
        *(signed char *)fill_value = NC_FILL_BYTE;
1290
60.3k
        break;
1291
1292
1
    case NC_SHORT:
1293
1
        *(short *)fill_value = NC_FILL_SHORT;
1294
1
        break;
1295
1296
1.33k
    case NC_INT:
1297
1.33k
        *(int *)fill_value = NC_FILL_INT;
1298
1.33k
        break;
1299
1300
137
    case NC_UBYTE:
1301
137
        *(unsigned char *)fill_value = NC_FILL_UBYTE;
1302
137
        break;
1303
1304
26
    case NC_USHORT:
1305
26
        *(unsigned short *)fill_value = NC_FILL_USHORT;
1306
26
        break;
1307
1308
421
    case NC_UINT:
1309
421
        *(unsigned int *)fill_value = NC_FILL_UINT;
1310
421
        break;
1311
1312
176
    case NC_INT64:
1313
176
        *(long long *)fill_value = NC_FILL_INT64;
1314
176
        break;
1315
1316
0
    case NC_UINT64:
1317
0
        *(unsigned long long *)fill_value = NC_FILL_UINT64;
1318
0
        break;
1319
1320
756
    case NC_FLOAT:
1321
756
        *(float *)fill_value = NC_FILL_FLOAT;
1322
756
        break;
1323
1324
481
    case NC_DOUBLE:
1325
481
        *(double *)fill_value = NC_FILL_DOUBLE;
1326
481
        break;
1327
1328
0
    default:
1329
0
        return NC_EINVAL;
1330
69.0k
    }
1331
1332
69.0k
    return NC_NOERR;
1333
69.0k
}
1334
1335
/**
1336
 * @internal Get the length, in bytes, of one element of a type in
1337
 * memory.
1338
 *
1339
 * @param h5 Pointer to HDF5 file info struct.
1340
 * @param xtype NetCDF type ID.
1341
 * @param len Pointer that gets length in bytes.
1342
 *
1343
 * @returns NC_NOERR No error.
1344
 * @returns NC_EBADTYPE Type not found.
1345
 * @author Ed Hartnett
1346
 */
1347
int
1348
nc4_get_typelen_mem(NC_FILE_INFO_T *h5, nc_type xtype, size_t *len)
1349
322k
{
1350
322k
    NC_TYPE_INFO_T *type;
1351
322k
    int retval;
1352
1353
322k
    LOG((4, "%s xtype: %d", __func__, xtype));
1354
322k
    assert(len);
1355
1356
    /* If this is an atomic type, the answer is easy. */
1357
322k
    switch (xtype)
1358
322k
    {
1359
157k
    case NC_BYTE:
1360
230k
    case NC_CHAR:
1361
231k
    case NC_UBYTE:
1362
231k
        *len = sizeof(char);
1363
231k
        return NC_NOERR;
1364
14.0k
    case NC_SHORT:
1365
14.1k
    case NC_USHORT:
1366
14.1k
        *len = sizeof(short);
1367
14.1k
        return NC_NOERR;
1368
66.4k
    case NC_INT:
1369
67.5k
    case NC_UINT:
1370
67.5k
        *len = sizeof(int);
1371
67.5k
        return NC_NOERR;
1372
2.12k
    case NC_FLOAT:
1373
2.12k
        *len = sizeof(float);
1374
2.12k
        return NC_NOERR;
1375
7.23k
    case NC_DOUBLE:
1376
7.23k
        *len = sizeof(double);
1377
7.23k
        return NC_NOERR;
1378
520
    case NC_INT64:
1379
520
    case NC_UINT64:
1380
520
        *len = sizeof(long long);
1381
520
        return NC_NOERR;
1382
0
    case NC_STRING:
1383
0
        *len = sizeof(char *);
1384
0
        return NC_NOERR;
1385
322k
    }
1386
1387
    /* See if var is compound type. */
1388
0
    if ((retval = nc4_find_type(h5, xtype, &type)))
1389
0
        return retval;
1390
1391
0
    if (!type)
1392
0
        return NC_EBADTYPE;
1393
1394
0
    *len = type->size;
1395
1396
0
    LOG((5, "type->size: %d", type->size));
1397
1398
0
    return NC_NOERR;
1399
0
}