summaryrefslogtreecommitdiffstats
path: root/tools/src/h5repack/h5repack_parse.c
blob: 0317ef709ba87e28d8d7b88a4a28f964f3c60cc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "h5repack.h"
#include "h5tools.h"
#include "h5tools_utils.h"

/*-------------------------------------------------------------------------
 * Function: parse_filter
 *
 * Purpose: read filter information
 *
 * Return: a list of names, the number of names and its compression type
 *
 * <name of filter> can be:
 *  GZIP, to apply the HDF5 GZIP filter (GZIP compression)
 *  SZIP, to apply the HDF5 SZIP filter (SZIP compression)
 *  SHUF, to apply the HDF5 shuffle filter
 *  FLET, to apply the HDF5 checksum filter
 *  NBIT, to apply the HDF5 NBIT filter (NBIT compression)
 *  SOFF, to apply the HDF5 scale+offset filter (compression)
 *  UD, to apply a User Defined filter k,m,n1[,…,nm]
 *  NONE, to remove the filter
 *
 * Examples:
 * "GZIP=6"
 * "A,B:NONE"
 *-------------------------------------------------------------------------
 */
obj_list_t *
parse_filter(const char *str, unsigned *n_objs, filter_info_t *filt, pack_opt_t *options, int *is_glb)
{
    size_t      i, m, u;
    char        c;
    size_t      len = strlen(str);
    int         f, k, l, p, q, end_obj = -1, no_param = 0;
    unsigned    j, n;
    char        sobj[MAX_NC_NAME];
    char        scomp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    char        stype[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    char        smask[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    obj_list_t *obj_list  = NULL;
    unsigned    pixels_per_block;

    /* initialize compression  info */
    memset(filt, 0, sizeof(filter_info_t));
    *is_glb = 0;

    /* check for the end of object list and number of objects */
    for (i = 0, n = 0; i < len; i++) {
        c = str[i];
        if (c == ':') {
            end_obj = (int)i;
            break;
        }
        if (c == ',')
            n++;
    }
    n++;

    /* Check for missing : */
    if (end_obj == -1) {
        /* apply to all objects */
        options->all_filter = 1;
        *is_glb             = 1;
        *n_objs             = 1;
    }
    else
        *n_objs = n;

    obj_list = (obj_list_t *)malloc(n * sizeof(obj_list_t));
    if (obj_list == NULL) {
        error_msg("could not allocate object list\n");
        return NULL;
    }

    /* get object list */
    if (end_obj > 0)
        for (j = 0, k = 0, n = 0; j < (unsigned)end_obj; j++, k++) {
            c       = str[j];
            sobj[k] = c;
            if (c == ',' || j == (unsigned)(end_obj - 1)) {
                if (c == ',')
                    sobj[k] = '\0';
                else
                    sobj[k + 1] = '\0';

                strcpy(obj_list[n].obj, sobj);
                memset(sobj, 0, sizeof(sobj));
                n++;
                k = -1;
            }
        }
    /* nothing after : */
    if (end_obj + 1 == (int)len) {
        if (obj_list)
            free(obj_list);
        error_msg("input Error: Invalid compression type in <%s>\n", str);
        exit(EXIT_FAILURE);
    }

    /* get filter additional parameters */
    m = 0;
    for (i = (size_t)(end_obj + 1), k = 0, j = 0; i < len; i++, k++) {
        c        = str[i];
        scomp[k] = c;
        if (c == '=' || i == len - 1) {
            if (c == '=') {      /*one more parameter */
                scomp[k] = '\0'; /*cut space */
                /*-------------------------------------------------------------------------
                 * H5Z_FILTER_SZIP
                 * szip has the format SZIP=<pixels per block,coding>
                 * pixels per block is a even number in 2-32 and coding method is 'EC' or 'NN'
                 * example SZIP=8,NN
                 *-------------------------------------------------------------------------
                 */
                if (strcmp(scomp, "SZIP") == 0) {
                    l = -1; /* mask index check */
                    for (m = 0, u = i + 1; u < len; u++, m++) {
                        if (str[u] == ',') {
                            stype[m] = '\0'; /* end digit of szip */
                            l        = 0;    /* start EC or NN search */
                            u++;             /* skip ',' */
                        }
                        c = str[u];
                        if (!isdigit(c) && l == -1) {
                            if (obj_list)
                                free(obj_list);
                            error_msg("compression parameter not digit in <%s>\n", str);
                            exit(EXIT_FAILURE);
                        }
                        if (l == -1)
                            stype[m] = c;
                        else {
                            smask[l] = c;
                            l++;
                            if (l == 2) {
                                smask[l] = '\0';
                                i        = len - 1; /* end */
                                if (strcmp(smask, "NN") == 0)
                                    filt->cd_values[j++] = H5_SZIP_NN_OPTION_MASK;
                                else if (strcmp(smask, "EC") == 0)
                                    filt->cd_values[j++] = H5_SZIP_EC_OPTION_MASK;
                                else {
                                    error_msg("szip mask must be 'NN' or 'EC' \n");
                                    exit(EXIT_FAILURE);
                                }
                            }
                        }
                    } /* u */
                }     /*if */

                /*-------------------------------------------------------------------------
                 * H5Z_FILTER_SCALEOFFSET
                 * scaleoffset has the format SOFF=<scale_factor,scale_type>
                 * scale_type can be
                 *   integer datatype, H5Z_SO_INT (IN)
                 *   float datatype using D-scaling method, H5Z_SO_FLOAT_DSCALE  (DS)
                 *   float datatype using E-scaling method, H5Z_SO_FLOAT_ESCALE  (ES) , not yet implemented
                 * for integer datatypes, scale_factor denotes Minimum Bits
                 * for float datatypes, scale_factor denotes decimal scale factor
                 *  examples
                 *  SOFF=31,IN
                 *  SOFF=3,DF
                 *-------------------------------------------------------------------------
                 */
                else if (strcmp(scomp, "SOFF") == 0) {
                    l = -1; /* mask index check */
                    for (m = 0, u = i + 1; u < len; u++, m++) {
                        if (str[u] == ',') {
                            stype[m] = '\0'; /* end digit */
                            l        = 0;    /* start 'IN' , 'DS', or 'ES' search */
                            u++;             /* skip ',' */
                        }
                        c = str[u];
                        if (!isdigit(c) && l == -1) {
                            if (obj_list)
                                free(obj_list);
                            error_msg("compression parameter is not a digit in <%s>\n", str);
                            exit(EXIT_FAILURE);
                        }
                        if (l == -1)
                            stype[m] = c;
                        else {
                            smask[l] = c;
                            l++;
                            if (l == 2) {
                                smask[l] = '\0';
                                i        = len - 1; /* end */
                                if (strcmp(smask, "IN") == 0)
                                    filt->cd_values[j++] = H5Z_SO_INT;
                                else if (strcmp(smask, "DS") == H5Z_SO_FLOAT_DSCALE)
                                    filt->cd_values[j++] = H5Z_SO_FLOAT_DSCALE;
                                else {
                                    error_msg("scale type must be 'IN' or 'DS' \n");
                                    exit(EXIT_FAILURE);
                                }
                            }
                        }
                    } /* u */
                }     /*if */

                /*-------------------------------------------------------------------------
                 * User Defined
                 *   has the format
                 *UD=<filter_number,filter_flag,cd_value_count,value_1[,value_2,...,value_N]> BZIP2 example
                 *  UD=307,0,1,9
                 *-------------------------------------------------------------------------
                 */
                else if (strcmp(scomp, "UD") == 0) {
                    l = -1; /* filter number index check */
                    f = -1; /* filter flag index check */
                    p = -1; /* CD_VAL count check */
                    for (m = 0, q = 0, u = i + 1; u < len; u++, m++, q++) {
                        if (str[u] == ',') {
                            stype[q] = '\0'; /* end digit */
                            if (l == -1) {
                                filt->filtn = atoi(stype);
                                l           = 0;
                            }
                            else if (f == -1) {
                                filt->filt_flag = (unsigned)strtoul(stype, NULL, 0);
                                f               = 0;
                            }
                            else if (p == -1) {
                                filt->cd_nelmts = strtoull(stype, NULL, 0);
                                p               = 0;
                            }
                            else {
                                filt->cd_values[j++] = (unsigned)strtoul(stype, NULL, 0);
                            }
                            q = 0;
                            u++; /* skip ',' */
                        }
                        c = str[u];
                        if (!isdigit(c) && l == -1) {
                            if (obj_list)
                                free(obj_list);
                            error_msg("filter number parameter is not a digit in <%s>\n", str);
                            exit(EXIT_FAILURE);
                        }
                        else if (!isdigit(c) && f == -1) {
                            if (obj_list)
                                free(obj_list);
                            error_msg("filter flag parameter is not a digit in <%s>\n", str);
                            exit(EXIT_FAILURE);
                        }
                        stype[q] = c;
                    } /* for u */
                    stype[q] = '\0';
                } /*if */

                /*-------------------------------------------------------------------------
                 * all other filters
                 *-------------------------------------------------------------------------
                 */
                else {
                    /* here we could have 1 or 2 digits  */
                    for (m = 0, u = i + 1; u < len; u++, m++) {
                        c = str[u];
                        if (!isdigit(c)) {
                            if (obj_list)
                                free(obj_list);
                            error_msg("compression parameter is not a digit in <%s>\n", str);
                            exit(EXIT_FAILURE);
                        }
                        stype[m] = c;
                    } /* u */

                    stype[m] = '\0';
                } /*if */

                filt->cd_values[j++] = (unsigned)strtoul(stype, NULL, 0);
                if (filt->cd_nelmts == 0)
                    j = 0;
                i += m; /* jump */
            }
            else if (i == len - 1) { /*no more parameters */
                scomp[k + 1] = '\0';
                no_param     = 1;
            }

            /*-------------------------------------------------------------------------
             * translate from string to filter symbol
             *-------------------------------------------------------------------------
             */

            /*-------------------------------------------------------------------------
             * H5Z_FILTER_NONE
             *-------------------------------------------------------------------------
             */
            if (strcmp(scomp, "NONE") == 0) {
                filt->filtn     = H5Z_FILTER_NONE;
                filt->cd_nelmts = 0;
            }

            /*-------------------------------------------------------------------------
             * H5Z_FILTER_DEFLATE
             *-------------------------------------------------------------------------
             */
            else if (strcmp(scomp, "GZIP") == 0) {
                filt->filtn     = H5Z_FILTER_DEFLATE;
                filt->cd_nelmts = 1;
                if (no_param) { /*no more parameters, GZIP must have parameter */
                    if (obj_list)
                        free(obj_list);
                    error_msg("missing compression parameter in <%s>\n", str);
                    exit(EXIT_FAILURE);
                }
            }

            /*-------------------------------------------------------------------------
             * H5Z_FILTER_SZIP
             *-------------------------------------------------------------------------
             */
            else if (strcmp(scomp, "SZIP") == 0) {
                filt->filtn     = H5Z_FILTER_SZIP;
                filt->cd_nelmts = 2;
                if (no_param) { /*no more parameters, SZIP must have parameter */
                    if (obj_list)
                        free(obj_list);
                    error_msg("missing compression parameter in <%s>\n", str);
                    exit(EXIT_FAILURE);
                }
            }

            /*-------------------------------------------------------------------------
             * H5Z_FILTER_SHUFFLE
             *-------------------------------------------------------------------------
             */
            else if (strcmp(scomp, "SHUF") == 0) {
                filt->filtn     = H5Z_FILTER_SHUFFLE;
                filt->cd_nelmts = 0;
                if (m > 0) { /*shuffle does not have parameter */
                    if (obj_list)
                        free(obj_list);
                    error_msg("extra parameter in SHUF <%s>\n", str);
                    exit(EXIT_FAILURE);
                }
            }
            /*-------------------------------------------------------------------------
             * H5Z_FILTER_FLETCHER32
             *-------------------------------------------------------------------------
             */
            else if (strcmp(scomp, "FLET") == 0) {
                filt->filtn     = H5Z_FILTER_FLETCHER32;
                filt->cd_nelmts = 0;
                if (m > 0) { /*shuffle does not have parameter */
                    if (obj_list)
                        free(obj_list);
                    error_msg("extra parameter in FLET <%s>\n", str);
                    exit(EXIT_FAILURE);
                }
            }
            /*-------------------------------------------------------------------------
             * H5Z_FILTER_NBIT
             *-------------------------------------------------------------------------
             */
            else if (strcmp(scomp, "NBIT") == 0) {
                filt->filtn     = H5Z_FILTER_NBIT;
                filt->cd_nelmts = 0;
                if (m > 0) { /*nbit does not have parameter */
                    if (obj_list)
                        free(obj_list);
                    error_msg("extra parameter in NBIT <%s>\n", str);
                    exit(EXIT_FAILURE);
                }
            }
            /*-------------------------------------------------------------------------
             * H5Z_FILTER_SCALEOFFSET
             *-------------------------------------------------------------------------
             */
            else if (strcmp(scomp, "SOFF") == 0) {
                filt->filtn     = H5Z_FILTER_SCALEOFFSET;
                filt->cd_nelmts = 2;
                if (no_param) { /*no more parameters, SOFF must have parameter */
                    if (obj_list)
                        free(obj_list);
                    error_msg("missing compression parameter in <%s>\n", str);
                    exit(EXIT_FAILURE);
                }
            }
            /*-------------------------------------------------------------------------
             * User Defined Filter
             *-------------------------------------------------------------------------
             */
            else if (strcmp(scomp, "UD") == 0) {
                /* parameters does not match count */
                if (filt->cd_nelmts != j) {
                    if (obj_list)
                        free(obj_list);
                    error_msg("incorrect number of compression parameters in <%s>\n", str);
                    exit(EXIT_FAILURE);
                }
            }
            else {
                if (obj_list)
                    free(obj_list);
                error_msg("invalid filter type in <%s>\n", str);
                exit(EXIT_FAILURE);
            }
            break;
        }
    } /*i*/

    /*-------------------------------------------------------------------------
     * check valid parameters
     *-------------------------------------------------------------------------
     */

    switch (filt->filtn) {
        /*-------------------------------------------------------------------------
         * H5Z_FILTER_DEFLATE
         *-------------------------------------------------------------------------
         */
        case H5Z_FILTER_DEFLATE:
            if (filt->cd_values[0] > 9) {
                if (obj_list)
                    free(obj_list);
                error_msg("invalid compression parameter in <%s>\n", str);
                exit(EXIT_FAILURE);
            }
            break;
            /*-------------------------------------------------------------------------
             * H5Z_FILTER_SZIP
             *-------------------------------------------------------------------------
             */
        case H5Z_FILTER_SZIP:
            pixels_per_block = filt->cd_values[0];
            if ((pixels_per_block % 2) == 1) {
                if (obj_list)
                    free(obj_list);
                error_msg("pixels_per_block is not even in <%s>\n", str);
                exit(EXIT_FAILURE);
            }
            if (pixels_per_block > H5_SZIP_MAX_PIXELS_PER_BLOCK) {
                if (obj_list)
                    free(obj_list);
                error_msg("pixels_per_block is too large in <%s>\n", str);
                exit(EXIT_FAILURE);
            }
            if ((strcmp(smask, "NN") != 0) && (strcmp(smask, "EC") != 0)) {
                if (obj_list)
                    free(obj_list);
                error_msg("szip mask must be 'NN' or 'EC' \n");
                exit(EXIT_FAILURE);
            }
            break;
        default:
            break;
    }

    return obj_list;
}

/*-------------------------------------------------------------------------
 * Function: parse_layout
 *
 * Purpose: read layout info
 *
 * Return: a list of names, the number of names and its chunking info for
 *  chunked. NULL, on error
 * the layout type can be:
 *  CHUNK, to apply chunking layout
 *  CONTI, to apply contiguous layout
 *  COMPA, to apply compact layout
 *
 * Example:
 * "AA,B,CDE:CHUNK=10X10"
 *
 *-------------------------------------------------------------------------
 */
obj_list_t *
parse_layout(const char *str, unsigned *n_objs, pack_info_t *pack, /* info about layout needed */
             pack_opt_t *options)
{
    obj_list_t *obj_list = NULL;
    unsigned    i, j, n;
    char        c;
    size_t      len = strlen(str);
    int         k, end_obj = -1, c_index;
    char        sobj[MAX_NC_NAME];
    char        sdim[10];
    char        slayout[10];

    memset(sdim, '\0', sizeof(sdim));
    memset(sobj, '\0', sizeof(sobj));
    memset(slayout, '\0', sizeof(slayout));

    /* check for the end of object list and number of objects */
    for (i = 0, n = 0; i < len; i++) {
        c = str[i];
        if (c == ':')
            end_obj = (int)i;
        if (c == ',')
            n++;
    }

    if (end_obj == -1) { /* missing : chunk all */
        options->all_layout = 1;
    }

    n++;
    obj_list = (obj_list_t *)malloc(n * sizeof(obj_list_t));
    if (obj_list == NULL) {
        error_msg("could not allocate object list\n");
        return NULL;
    }
    *n_objs = n;

    /* get object list */
    if (end_obj > 0)
        for (j = 0, k = 0, n = 0; j < (unsigned)end_obj; j++, k++) {
            c       = str[j];
            sobj[k] = c;
            if (c == ',' || j == (unsigned)(end_obj - 1)) {
                if (c == ',')
                    sobj[k] = '\0';
                else
                    sobj[k + 1] = '\0';
                strcpy(obj_list[n].obj, sobj);
                memset(sobj, 0, sizeof(sobj));
                n++;
                k = -1;
            }
        }

    /* nothing after : */
    if (end_obj + 1 == (int)len) {
        if (obj_list)
            free(obj_list);
        error_msg("in parse layout, no characters after : in <%s>\n", str);
        exit(EXIT_FAILURE);
    }

    /* get layout info */
    for (j = (unsigned)(end_obj + 1), n = 0; n <= 5; j++, n++) {
        if (n == 5) {
            slayout[n] = '\0'; /*cut string */
            if (strcmp(slayout, "COMPA") == 0)
                pack->layout = H5D_COMPACT;
            else if (strcmp(slayout, "CONTI") == 0)
                pack->layout = H5D_CONTIGUOUS;
            else if (strcmp(slayout, "CHUNK") == 0)
                pack->layout = H5D_CHUNKED;
            else {
                error_msg("in parse layout, not a valid layout in <%s>\n", str);
                exit(EXIT_FAILURE);
            }
        }
        else {
            c          = str[j];
            slayout[n] = c;
        }
    } /* j */

    if (pack->layout == H5D_CHUNKED) {
        /*-------------------------------------------------------------------------
         * get chunk info
         *-------------------------------------------------------------------------
         */
        k = 0;
        if (j > len) {
            if (obj_list)
                free(obj_list);
            error_msg("in parse layout,  <%s> Chunk dimensions missing\n", str);
            exit(EXIT_FAILURE);
        }

        for (i = j, c_index = 0; i < len; i++) {
            c       = str[i];
            sdim[k] = c;
            k++; /*increment sdim index */

            if (!isdigit(c) && c != 'x' && c != 'N' && c != 'O' && c != 'N' && c != 'E') {
                if (obj_list)
                    free(obj_list);
                error_msg("in parse layout, <%s> Not a valid character in <%s>\n", sdim, str);
                exit(EXIT_FAILURE);
            }

            if (c == 'x' || i == len - 1) {
                if (c == 'x') {
                    sdim[k - 1]                        = '\0';
                    k                                  = 0;
                    pack->chunk.chunk_lengths[c_index] = strtoull(sdim, NULL, 0);
                    if (pack->chunk.chunk_lengths[c_index] == 0) {
                        if (obj_list)
                            free(obj_list);
                        error_msg("in parse layout, <%s> conversion to number in <%s>\n", sdim, str);
                        exit(EXIT_FAILURE);
                    }
                    c_index++;
                }
                else if (i == len - 1) { /*no more parameters */
                    sdim[k] = '\0';
                    k       = 0;
                    if (strcmp(sdim, "NONE") == 0) {
                        pack->chunk.rank = -2;
                    }
                    else {
                        pack->chunk.chunk_lengths[c_index] = strtoull(sdim, NULL, 0);
                        if (pack->chunk.chunk_lengths[c_index] == 0) {
                            if (obj_list)
                                free(obj_list);
                            error_msg("in parse layout, <%s> conversion to number in <%s>\n", sdim, str);
                            exit(EXIT_FAILURE);
                        }
                        pack->chunk.rank = c_index + 1;
                    }
                } /*if */
            }     /*if c=='x' || i==len-1 */
        }         /*i*/
    }             /*H5D_CHUNKED*/

    return obj_list;
}