File: hdfls.c

package info (click to toggle)
libhdf4 4.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,384 kB
  • sloc: ansic: 128,700; sh: 15,015; fortran: 12,444; java: 5,863; xml: 1,205; makefile: 794; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (564 lines) | stat: -rw-r--r-- 20,622 bytes parent folder | download | duplicates (2)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF 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://support.hdfgroup.org/ftp/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "hdf.h"
#include "hfile_priv.h"

#define MAXBUFF 8192

dd_t *desc_buf;

intn debug   = FALSE, /* Debugging is off by default */
    ddblocks = FALSE, /* DD block dumping is off by default */
#ifdef DISKBLOCK_DEBUG
    diskblock_check = FALSE,     /* check disk-block boundaries */
#endif                           /* DISKBLOCK_DEBUG */
    sort                = TRUE,  /* Sorting is on by default */
    longout             = FALSE, /* short output by default */
    labels              = FALSE, /* no label info by default */
    special             = FALSE, /* no special info by default */
    groups              = FALSE; /* no group info by default */
static intn v_init_done = FALSE; /* whether the Vset interface has been */
/* initialized for this file */
uint16 only_tag = DFTAG_NULL; /* by default print info about all tags */

char *file_name; /* name of current file being listed */

int  compare(const void *, const void *);
int  main(int, char *a[]);
void lprint(int32, dd_t *, int);
void print_item(int32, dd_t *, intn);
void printfilever(int32 file_id);
void usage(char *argv[]);
int  dumpDD(void);

int
compare(const void *aa, const void *bb)
{
    const dd_t *a = (const dd_t *)aa;
    const dd_t *b = (const dd_t *)bb;

    if (a->tag > b->tag)
        return (1);
    if (a->tag < b->tag)
        return (-1);
    if (a->ref > b->ref)
        return (1);
    if (a->ref < b->ref)
        return (-1);
    return (0);
}

void
print_item(int32 fid, dd_t *desc_list, intn n)
{
    intn  status;
    int32 len;
    char *name, *label_str;

    printf("\tRef no %6d\t%8d bytes\n", (int)desc_list[n].ref, (int)desc_list[n].length);

    /* print out labels and annotations if desired */
    if (labels) { /* read in all of the labels */
        len = DFANgetlablen(file_name, desc_list[n].tag, desc_list[n].ref);
        if (len != FAIL) {
            label_str      = (char *)malloc((uint32)len + 1);
            status         = DFANgetlabel(file_name, desc_list[n].tag, desc_list[n].ref, label_str, len + 1);
            label_str[len] = '\0';
            if (status == FAIL)
                printf("\t  Unable to read label\n");
            else
                printf("\t  Label: %s\n", label_str);
            free(label_str);
        }

        /* read in all of the annotations */
        len = DFANgetdesclen(file_name, desc_list[n].tag, desc_list[n].ref);
        if (len != FAIL) {
            label_str      = (char *)malloc((uint32)len + 1);
            status         = DFANgetdesc(file_name, desc_list[n].tag, desc_list[n].ref, label_str, len + 1);
            label_str[len] = '\0';
            if (status == FAIL)
                printf("\t  Unable to read description\n");
            else
                printf("\t  Description: %s\n", label_str);
            free(label_str);
        }
    }

    if ((special) && (SPECIALTAG(desc_list[n].tag))) { /* print out special info if desired */
        sp_info_block_t info;
        int32           aid, ret;

        aid = Hstartread(fid, desc_list[n].tag, desc_list[n].ref);
        if (aid == FAIL)
            return;

        ret = HDget_special_info(aid, &info);
        if ((ret == FAIL) || (info.key == FAIL))
            return;

        switch (info.key) {
            case SPECIAL_LINKED:
                printf("\tLinked Block: first %ld standard %ld per unit %ld\n", (long)info.first_len,
                       (long)info.block_len, (long)info.nblocks);
                break;

            case SPECIAL_EXT:
                printf("\tExternal File: path %s  offset %ld\n", info.path, (long)info.offset);
                break;

            case SPECIAL_COMP:
                printf("\tCompressed Element: compression type: %s  modeling type %s\n",
                       (info.comp_type == COMP_CODE_NONE
                            ? "None"
                            : (info.comp_type == COMP_CODE_RLE
                                   ? "Run-Length"
                                   : (info.comp_type == COMP_CODE_NBIT
                                          ? "N-Bit"
                                          : (info.comp_type == COMP_CODE_SKPHUFF
                                                 ? "Skipping Huffman"
                                                 : (info.comp_type == COMP_CODE_DEFLATE ? "Deflated"
                                                                                        : "Unknown"))))),
                       (info.model_type == COMP_MODEL_STDIO ? "Standard" : "Unknown"));
                break;

            case SPECIAL_CHUNKED:
                printf("\tChunked Element: \n \tlogical size: %ld\n \tnumber of dimensions: %ld \n",
                       (long)info.chunk_size, (long)info.ndims);
                printf("\tarray of chunk lengths for each dimension:");
                for (int i = 0; i < info.ndims; i++)
                    printf("\t %ld", (long)info.cdims[i]);
                printf("\n");
                free(info.cdims);
                break;

            default:
                printf("\tDo not understand special element type %d\n", info.key);
                break;
        }
        Hendaccess(aid);
    }

    if ((groups) && (desc_list[n].tag == DFTAG_RIG || desc_list[n].tag == DFTAG_SDG ||
                     desc_list[n].tag == DFTAG_NDG || desc_list[n].tag == DFTAG_VG)) { /* print groups */
        if (desc_list[n].tag != DFTAG_VG) { /* groups other than Vgroups */
            int32 GroupID;
            DFdi  elmt;

            printf("\tContents:\n");
            if ((GroupID = DFdiread(fid, desc_list[n].tag, desc_list[n].ref)) >= 0) {
                while (!DFdiget(GroupID, &elmt.tag, &elmt.ref)) {
                    name = (char *)HDgettagsname(elmt.tag);
                    if (!name)
                        printf("\t\t%-30s: (tag=%6d) ref=%d\n", "Unknown Tag", elmt.tag, elmt.ref);
                    else {
                        printf("\t\t%-30s: (tag=%6d) ref=%d\n", name, elmt.tag, elmt.ref);
                        free(name);
                    } /* end else */
                }     /* end while */
            }         /* end if */
            else
                printf("\t\tNone!\n");
        }      /* end if */
        else { /* dump Vgroup tag/refs */
            int32  ntagrefs;
            int32  vkey;
            int32 *tag_arr, *ref_arr;

            if (v_init_done == FALSE) { /* init the V routines */
                v_init_done = TRUE;
                Vinitialize(fid);
            } /* end if */
            if ((vkey = Vattach(fid, (int32)desc_list[n].ref, "r")) != FAIL) {
                ntagrefs = Vntagrefs(vkey);
                printf("\tContents: %d items\n", (int)ntagrefs);
                if (ntagrefs > 0) {
                    tag_arr = (int32 *)malloc(sizeof(int32) * (size_t)ntagrefs);
                    ref_arr = (int32 *)malloc(sizeof(int32) * (size_t)ntagrefs);
                    if (tag_arr == NULL || ref_arr == NULL) {
                        free(tag_arr);
                        free(ref_arr);
                    }
                    else {
                        if (Vgettagrefs(vkey, tag_arr, ref_arr, ntagrefs) != FAIL) {
                            for (int i = 0; i < ntagrefs; i++) {
                                name = (char *)HDgettagsname((uint16)tag_arr[i]);
                                if (!name)
                                    printf("\t\t%-30s: (tag=%6d) ref=%d\n", "Unknown Tag", (int)tag_arr[i],
                                           (int)ref_arr[i]);
                                else {
                                    printf("\t\t%-30s: (tag=%6d) ref=%d\n", name, (int)tag_arr[i],
                                           (int)ref_arr[i]);
                                    free(name);
                                } /* end else */
                            }     /* end for */
                        }         /* end if */
                        free(tag_arr);
                        free(ref_arr);
                    } /* end else */
                }     /* end if */
                Vdetach(vkey);
            } /* end if */
        }     /* end else */
    }         /* dumping groups */
} /* print_item */

void
lprint(int32 fid, dd_t *desc_tmp, int num)
{
    intn   j = 0, empty = 0;
    uint16 prev = 0;
    char  *name;

    while (j < num) {
        if (desc_tmp[j].tag == DFTAG_NULL) {
            empty++;
            j++;
            continue; /* don't print anything now */
        }

        /*
         * skip this tag if the user only wants to see some tags and
         *  this is not one of them
         */
        if (only_tag != DFTAG_NULL && only_tag != desc_tmp[j].tag) {
            j++;
            continue;
        }

        /*
        ** Find and print text description of this tag
        */
        name = (char *)HDgettagsname(desc_tmp[j].tag);
        if (!name)
            printf("\n%-30s: (tag %d)\n", "Unknown Tag", desc_tmp[j].tag);
        else {
            printf("\n%-30s: (tag %d)\n", name, desc_tmp[j].tag);
            free(name);
        }

        /*
        ** Print out reference number information
        */
        prev = desc_tmp[j].tag;
        if (longout) {
            while (desc_tmp[j].tag == prev && j < num) {
                print_item(fid, desc_tmp, j);
                j++;
            }
        }
        else {
            printf("\tRef nos:");
            while (desc_tmp[j].tag == (uint16)prev && j < num) {
                printf(" %d", desc_tmp[j].ref);
                j++;
            }
        }
    }

    if (empty)
        printf("\nEmpty (tag %d): %d slots\n", DFTAG_NULL, empty);
}

/* print the library version of the file */
void
printfilever(int32 file_id)
{
    uint32 major, minor, release;
    char   string[LIBVSTR_LEN + 1];

    if (Hgetfileversion(file_id, &major, &minor, &release, string) == SUCCEED) {
        string[LIBVSTR_LEN] = '\0'; /* make it a null terminated string */
        printf("File library version: ");
        printf("Major= %u, Minor=%u, Release=%u\nString=%s\n", (unsigned)major, (unsigned)minor,
               (unsigned)release, string);
    }
    else
        printf("(Does not have library version information)\n");
}

/* print command usage */
void
usage(char *argv[])
{
    printf("%s,  %s\n\n", argv[0], LIBVER_STRING);
#ifdef DISKBLOCK_DEBUG
    printf("%s [-o] [-l] [-d] [-v] [-g] [-s] [-h] [-x] [-t #] fn ....\n", argv[0]);
#else  /* DISKBLOCK_DEBUG */
    printf("%s [-o] [-l] [-d] [-v] [-g] [-s] [-h] [-t #] fn ....\n", argv[0]);
#endif /* DISKBLOCK_DEBUG */
    printf("        This program displays information about the");
    printf(" data elements in\n");
    printf("        HDF file.\n");
    printf("    -d: offset & length info of each element in the file\n");
    printf("    -o: Ordered - display in reference number order\n");
    printf("    -l: Long format - display more information\n");
    printf("    -v: Verbose format - display text of annotations and labels.\n");
    printf("        (Verbose format automatically puts you in Long format).\n");
    printf("    -t #: List only information about a specific type of tag.\n");
    printf("          For example '%s -t 700 foo.hdf' \n", argv[0]);
    printf("          will list information only about Scientific Data\n");
    printf("          Groups.\n");
    printf("    -s: Give detailed descriptions of \"special elements\"\n");
    printf("    -g: List items in groups\n");
    printf("    -h: Dump DD Block information\n");
#ifdef DISKBLOCK_DEBUG
    printf("    -x: Check disk block boundaries (also dumps DD blocks)\n");
#endif /* DISKBLOCK_DEBUG */
}

/* dump the DD blocks */
int
dumpDD(void)
{
    hdf_file_t file_id; /* stdio file ID */
    int32      next_block = MAGICLEN;
    uint8     *ddbuf;                    /* buffer to store the DD information */
    uint8      buf[NDDS_SZ + OFFSET_SZ]; /* buffer to hold DD block info */
    uint8     *b;
    int16      n_dds;    /* number of DDs in the current block */
    uint16     tag, ref; /* DD tag & ref */
    int32      off, len; /* DD offset & length */
    intn       l;        /* local counting variable */

    file_id = HI_OPEN(file_name, DFACC_READ);
    if (OPENERR(file_id)) {
        printf("Error opening file: %s\n", file_name);
        return (FAIL);
    } /* end if */
    while (next_block != 0) {
        if (HI_SEEK(file_id, next_block) == FAIL) {
            printf("Error seeking in file: %s\n", file_name);
            return (FAIL);
        } /* end if */
        if (HI_READ(file_id, buf, NDDS_SZ + OFFSET_SZ) == FAIL) {
            printf("Error reading in file: %s\n", file_name);
            return (FAIL);
        } /* end if */
        printf("current block: %ld,", (long)next_block);
        b = buf;
        INT16DECODE(b, n_dds);
        INT32DECODE(b, next_block);
        printf(" size of block: %ld, number of DDs:%d, next block: %ld\n",
               (long)(NDDS_SZ + OFFSET_SZ + (n_dds * DD_SZ)), (int)n_dds, (long)next_block);

        ddbuf = (uint8 *)malloc(n_dds * DD_SZ);
        if (HI_READ(file_id, ddbuf, n_dds * DD_SZ) == FAIL) {
            printf("Error reading in file: %s\n", file_name);
            return (FAIL);
        } /* end if */
        b = ddbuf;
        for (l = 0; l < n_dds; l++) {
#ifdef DISKBLOCK_DEBUG
            uint8 block_head[DISKBLOCK_HSIZE];
            uint8 block_tail[DISKBLOCK_TSIZE];
#endif /* DISKBLOCK_DEBUG */

            UINT16DECODE(b, tag);
            UINT16DECODE(b, ref);
            INT32DECODE(b, off);
            INT32DECODE(b, len);
            printf("\t[%5d] tag=%5u ref=%5u offset=%10ld length=%10ld\n", (int)l, (unsigned)tag,
                   (unsigned)ref, (long)off, (long)len);
#ifdef DISKBLOCK_DEBUG
            if ((tag != DFTAG_NULL && tag != DFTAG_FREE) &&
                (len != INVALID_LENGTH && off != INVALID_OFFSET)) {
                if (HI_SEEK(file_id, off - DISKBLOCK_HSIZE) == FAIL) {
                    printf("Error seeking in file: %s\n", file_name);
                    return (FAIL);
                } /* end if */
                if (HI_READ(file_id, block_head, DISKBLOCK_HSIZE) == FAIL) {
                    printf("Error reading in file: %s\n", file_name);
                    return (FAIL);
                } /* end if */
                if (memcmp(block_head, diskblock_header, DISKBLOCK_HSIZE) != 0) {
                    intn k;

                    printf("Header wrong for disk block!\n");
                    for (k = 0; k < DISKBLOCK_HSIZE; k++) {
                        printf("\tbyte: %d, current: %x, correct: %x\n", k, (unsigned)block_head[k],
                               (unsigned)diskblock_header[k]);
                    } /* end for */
                }     /* end if */
                if (HI_SEEK(file_id, off + len) == FAIL) {
                    printf("Error seeking in file: %s\n", file_name);
                    return (FAIL);
                } /* end if */
                if (HI_READ(file_id, block_tail, DISKBLOCK_TSIZE) == FAIL) {
                    printf("Error reading in file: %s\n", file_name);
                    return (FAIL);
                } /* end if */
                if (memcmp(block_tail, diskblock_tail, DISKBLOCK_TSIZE) != 0) {
                    intn k;

                    printf("Tail wrong for disk block!\n");
                    for (k = 0; k < DISKBLOCK_HSIZE; k++) {
                        printf("\tbyte: %d, current: %x, correct: %x\n", k, (unsigned)block_tail[k],
                               (unsigned)diskblock_tail[k]);
                    } /* end for */
                }     /* end if */
            }         /* end if */
#endif                /* DISKBLOCK_DEBUG */
        }             /* end for */
        free(ddbuf);
    } /* end while */
    HI_CLOSE(file_id);
    return (SUCCEED);
}

int
main(int argc, char *argv[])
{
    int32 fid, aid;
    int   i = 1, j, n, status;

    while ((i < argc) && (argv[i][0] == '-')) {
        switch (argv[i][1]) {
            case 'o': /* give non ordered output */
                sort = FALSE;
                break;

            case 'd': /* go into debugging mode */
                debug = TRUE;
                break;

            case 'h': /* dump the dd blocks */
                ddblocks = TRUE;
                break;

#ifdef DISKBLOCK_DEBUG
            case 'x': /* check the disk-block boundaries (implies dumping dd blocks) */
                diskblock_check = TRUE;
                ddblocks        = TRUE;
                break;
#endif /* DISKBLOCK_DEBUG */

            case 'v': /* print labels for elements */
                labels  = TRUE;
                longout = TRUE;
                break;

            case 'l': /* give long output */
                longout = TRUE;
                break;

            case 's': /* give info on special elements */
                special = TRUE;
                longout = TRUE;
                break;

            case 'g': /* give info on groups */
                groups  = TRUE;
                longout = TRUE;
                break;

            case 't':
                if (argv[i][2] != '\0')
                    only_tag = (uint16)atoi(&(argv[i][2]));
                else
                    only_tag = (uint16)atoi(&(argv[++i][0]));
                break;

            default:
                printf("Unknown option : -%c\n", argv[1][1]);
                break;
        }
        i++;
    }

    /*
     * If a file name has not been supplied print the usage message
     */
    if (i == argc) {
        usage(argv);
        exit(1);
    }

    desc_buf = (dd_t *)calloc(MAXBUFF, sizeof(dd_t));

    while (i < argc) {
        file_name = argv[i];
        printf("%s:\n", file_name);

        /* Dump the DD blocks, if asked */
        if (ddblocks == TRUE) {
            if (dumpDD() == FAIL) {
                i++;
                continue;
            }
        } /* end if */

        fid = Hopen(file_name, DFACC_READ, -1);
        if (fid == FAIL) {
            if (HEvalue(1) == DFE_NOTDFFILE) {
                printf("\tNot an HDF file.\n");
                i++;
                continue;
            }
            else {
                HEprint(stderr, 0);
            }
        }

        printfilever(fid);

        aid = Hstartread(fid, DFTAG_WILDCARD, DFREF_WILDCARD);
        if (aid == FAIL) {
            HEprint(stderr, 0);
            i++;
            continue;
        }

        status = SUCCEED;
        for (n = 0; (n < MAXBUFF) && (status != FAIL); n++) {
            Hinquire(aid, NULL, &desc_buf[n].tag, &desc_buf[n].ref, &desc_buf[n].length, &desc_buf[n].offset,
                     NULL, NULL, NULL);
            status = Hnextread(aid, DFTAG_WILDCARD, DFREF_WILDCARD, DF_CURRENT);
        }

        if (debug) {
            printf("\n");
            for (j = 0; j < n; j++) {
                printf("%6d) tag %6d ref %6d ", j, desc_buf[j].tag, desc_buf[j].ref);
                printf(" offset %10d length %10d\n", (int)desc_buf[j].offset, (int)desc_buf[j].length);
            }
        }

        if (sort)
            qsort((char *)desc_buf, (size_t)n, sizeof(dd_t), compare);

        v_init_done = FALSE;

        lprint(fid, desc_buf, n);

        if (v_init_done == TRUE)
            Vfinish(fid);

        if (Hendaccess(aid) == FAIL)
            HEprint(stderr, 0);

        if (Hclose(fid) == FAIL)
            HEprint(stderr, 0);

        i++;
        printf("\n");

        if (n >= MAXBUFF)
            fprintf(stderr, "Warning:  File may have more DD's than hdfls can display\n");
    }

    free(desc_buf);

    return (0);
}