[BACK]Return to ctfdump.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ctfdump

Annotation of src/usr.bin/ctfdump/ctfdump.c, Revision 1.4

1.4     ! jasper      1: /*     $OpenBSD: ctfdump.c,v 1.3 2017/08/11 18:22:22 jasper Exp $ */
1.2       jasper      2:
1.1       mpi         3: /*
                      4:  * Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/param.h>
                     20: #include <sys/types.h>
                     21: #include <sys/stat.h>
                     22: #include <sys/exec_elf.h>
                     23: #include <sys/mman.h>
                     24: #include <sys/ctf.h>
                     25:
                     26: #include <err.h>
                     27: #include <fcntl.h>
                     28: #include <locale.h>
                     29: #include <stdio.h>
                     30: #include <stdint.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <unistd.h>
                     34:
                     35: #ifdef ZLIB
                     36: #include <zlib.h>
                     37: #endif /* ZLIB */
                     38:
                     39: #ifndef nitems
                     40: #define nitems(_a)     (sizeof((_a)) / sizeof((_a)[0]))
                     41: #endif
                     42:
                     43: #define DUMP_OBJECT    (1 << 0)
                     44: #define DUMP_FUNCTION  (1 << 1)
                     45: #define DUMP_HEADER    (1 << 2)
                     46: #define DUMP_LABEL     (1 << 3)
                     47: #define DUMP_STRTAB    (1 << 4)
                     48: #define DUMP_STATISTIC (1 << 5)
                     49: #define DUMP_TYPE      (1 << 6)
                     50:
                     51: int             dump(const char *, uint8_t);
                     52: int             isctf(const char *, size_t);
                     53: __dead void     usage(void);
                     54:
                     55: int             ctf_dump(const char *, size_t, uint8_t);
                     56: uint32_t        ctf_dump_type(struct ctf_header *, const char *, off_t,
                     57:                     uint32_t, uint32_t);
                     58: const char     *ctf_kind2name(uint16_t);
                     59: const char     *ctf_enc2name(uint16_t);
                     60: const char     *ctf_off2name(struct ctf_header *, const char *, off_t,
                     61:                     uint32_t);
                     62:
                     63: int             elf_dump(char *, size_t, uint8_t);
                     64: const char     *elf_idx2sym(size_t *, uint8_t);
                     65:
                     66: /* elf.c */
                     67: int             iself(const char *, size_t);
                     68: int             elf_getshstab(const char *, size_t, const char **, size_t *);
                     69: ssize_t                 elf_getsymtab(const char *, const char *, size_t,
                     70:                     const Elf_Sym **, size_t *);
                     71: ssize_t                 elf_getsection(char *, const char *, const char *,
                     72:                     size_t, const char **, size_t *);
                     73:
                     74: char           *decompress(const char *, size_t, off_t);
                     75:
                     76: int
                     77: main(int argc, char *argv[])
                     78: {
                     79:        const char *filename;
                     80:        uint8_t flags = 0;
                     81:        int ch, error = 0;
1.3       jasper     82:
1.4     ! jasper     83:        setlocale(LC_ALL, "");
        !            84:
1.3       jasper     85:        if (pledge("stdio rpath", NULL) == -1)
                     86:                err(1, "pledge");
1.1       mpi        87:
                     88:        while ((ch = getopt(argc, argv, "dfhlst")) != -1) {
                     89:                switch (ch) {
                     90:                case 'd':
                     91:                        flags |= DUMP_OBJECT;
                     92:                        break;
                     93:                case 'f':
                     94:                        flags |= DUMP_FUNCTION;
                     95:                        break;
                     96:                case 'h':
                     97:                        flags |= DUMP_HEADER;
                     98:                        break;
                     99:                case 'l':
                    100:                        flags |= DUMP_LABEL;
                    101:                        break;
                    102:                case 's':
                    103:                        flags |= DUMP_STRTAB;
                    104:                        break;
                    105:                case 't':
                    106:                        flags |= DUMP_TYPE;
                    107:                        break;
                    108:                default:
                    109:                        usage();
                    110:                }
                    111:        }
                    112:
                    113:        argc -= optind;
                    114:        argv += optind;
                    115:
                    116:        if (argc <= 0)
                    117:                usage();
                    118:
                    119:        /* Dump everything by default */
                    120:        if (flags == 0)
                    121:                flags = 0xff;
                    122:
                    123:        while ((filename = *argv++) != NULL)
                    124:                error |= dump(filename, flags);
                    125:
                    126:        return error;
                    127: }
                    128:
                    129: int
                    130: dump(const char *path, uint8_t flags)
                    131: {
                    132:        struct stat              st;
                    133:        int                      fd, error = 1;
                    134:        char                    *p;
                    135:
                    136:        fd = open(path, O_RDONLY);
                    137:        if (fd == -1) {
                    138:                warn("open");
                    139:                return 1;
                    140:        }
                    141:        if (fstat(fd, &st) == -1) {
                    142:                warn("fstat");
                    143:                return 1;
                    144:        }
                    145:        if ((uintmax_t)st.st_size > SIZE_MAX) {
                    146:                warnx("file too big to fit memory");
                    147:                return 1;
                    148:        }
                    149:
                    150:        p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
                    151:        if (p == MAP_FAILED)
                    152:                err(1, "mmap");
                    153:
                    154:        if (iself(p, st.st_size)) {
                    155:                error = elf_dump(p, st.st_size, flags);
                    156:        } else if (isctf(p, st.st_size)) {
                    157:                error = ctf_dump(p, st.st_size, flags);
                    158:        }
                    159:
                    160:        munmap(p, st.st_size);
                    161:        close(fd);
                    162:
                    163:        return error;
                    164: }
                    165:
                    166: const char             *strtab;
                    167: const Elf_Sym          *symtab;
                    168: size_t                  strtabsz, nsymb;
                    169:
                    170: const char *
                    171: elf_idx2sym(size_t *idx, uint8_t type)
                    172: {
                    173:        const Elf_Sym   *st;
                    174:        size_t           i;
                    175:
                    176:        for (i = *idx + 1; i < nsymb; i++) {
                    177:                st = &symtab[i];
                    178:
                    179:                if (ELF_ST_TYPE(st->st_info) != type)
                    180:                        continue;
                    181:
                    182:                *idx = i;
                    183:                return strtab + st->st_name;
                    184:        }
                    185:
                    186:        return NULL;
                    187: }
                    188:
                    189: int
                    190: elf_dump(char *p, size_t filesize, uint8_t flags)
                    191: {
                    192:        Elf_Ehdr                *eh = (Elf_Ehdr *)p;
                    193:        Elf_Shdr                *sh;
                    194:        const char              *shstab;
                    195:        size_t                   i, shstabsz;
                    196:
                    197:        /* Find section header string table location and size. */
                    198:        if (elf_getshstab(p, filesize, &shstab, &shstabsz))
                    199:                return 1;
                    200:
                    201:        /* Find symbol table location and number of symbols. */
                    202:        if (elf_getsymtab(p, shstab, shstabsz, &symtab, &nsymb) == -1)
                    203:                warnx("symbol table not found");
                    204:
                    205:        /* Find string table location and size. */
                    206:        if (elf_getsection(p, ELF_STRTAB, shstab, shstabsz, &strtab,
                    207:            &strtabsz) == -1)
                    208:                warnx("string table not found");
                    209:
                    210:        /* Find CTF section and dump it. */
                    211:        for (i = 0; i < eh->e_shnum; i++) {
                    212:                sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
                    213:
                    214:                if ((sh->sh_link >= eh->e_shnum) ||
                    215:                    (sh->sh_name >= shstabsz))
                    216:                        continue;
                    217:
                    218:                if (strncmp(shstab + sh->sh_name, ELF_CTF, strlen(ELF_CTF)))
                    219:                        continue;
                    220:
                    221:                if (!isctf(p + sh->sh_offset, sh->sh_size))
                    222:                        break;
                    223:
                    224:                return ctf_dump(p + sh->sh_offset, sh->sh_size, flags);
                    225:        }
                    226:
                    227:        warnx("%s section not found", ELF_CTF);
                    228:        return 1;
                    229: }
                    230:
                    231: int
                    232: isctf(const char *p, size_t filesize)
                    233: {
                    234:        struct ctf_header       *cth = (struct ctf_header *)p;
                    235:        off_t                    dlen;
                    236:
                    237:        if (filesize < sizeof(struct ctf_header)) {
                    238:                warnx("file too small to be CTF");
                    239:                return 0;
                    240:        }
                    241:
                    242:        if (cth->cth_magic != CTF_MAGIC || cth->cth_version != CTF_VERSION)
                    243:                return 0;
                    244:
                    245:        dlen = cth->cth_stroff + cth->cth_strlen;
                    246:        if (dlen > (off_t)filesize && !(cth->cth_flags & CTF_F_COMPRESS)) {
                    247:                warnx("bogus file size");
                    248:                return 0;
                    249:        }
                    250:
                    251:        if ((cth->cth_lbloff & 3) || (cth->cth_objtoff & 1) ||
                    252:            (cth->cth_funcoff & 1) || (cth->cth_typeoff & 3)) {
                    253:                warnx("wrongly aligned offset");
                    254:                return 0;
                    255:        }
                    256:
                    257:        if ((cth->cth_lbloff >= dlen) || (cth->cth_objtoff >= dlen) ||
                    258:            (cth->cth_funcoff >= dlen) || (cth->cth_typeoff >= dlen)) {
                    259:                warnx("truncated file");
                    260:                return 0;
                    261:        }
                    262:
                    263:        if ((cth->cth_lbloff > cth->cth_objtoff) ||
                    264:            (cth->cth_objtoff > cth->cth_funcoff) ||
                    265:            (cth->cth_funcoff > cth->cth_typeoff) ||
                    266:            (cth->cth_typeoff > cth->cth_stroff)) {
                    267:                warnx("corrupted file");
                    268:                return 0;
                    269:        }
                    270:
                    271:        return 1;
                    272: }
                    273:
                    274: int
                    275: ctf_dump(const char *p, size_t size, uint8_t flags)
                    276: {
                    277:        struct ctf_header       *cth = (struct ctf_header *)p;
                    278:        off_t                    dlen = cth->cth_stroff + cth->cth_strlen;
                    279:        char                    *data;
                    280:
                    281:        if (cth->cth_flags & CTF_F_COMPRESS) {
                    282:                data = decompress(p + sizeof(*cth), size - sizeof(*cth), dlen);
                    283:                if (data == NULL)
                    284:                        return 1;
                    285:        } else {
                    286:                data = (char *)p + sizeof(*cth);
                    287:        }
                    288:
                    289:        if (flags & DUMP_HEADER) {
                    290:                printf("  cth_magic    = 0x%04x\n", cth->cth_magic);
                    291:                printf("  cth_version  = %d\n", cth->cth_version);
                    292:                printf("  cth_flags    = 0x%02x\n", cth->cth_flags);
                    293:                printf("  cth_parlabel = %s\n",
                    294:                    ctf_off2name(cth, data, dlen, cth->cth_parname));
                    295:                printf("  cth_parname  = %s\n",
                    296:                    ctf_off2name(cth, data, dlen, cth->cth_parname));
                    297:                printf("  cth_lbloff   = %d\n", cth->cth_lbloff);
                    298:                printf("  cth_objtoff  = %d\n", cth->cth_objtoff);
                    299:                printf("  cth_funcoff  = %d\n", cth->cth_funcoff);
                    300:                printf("  cth_typeoff  = %d\n", cth->cth_typeoff);
                    301:                printf("  cth_stroff   = %d\n", cth->cth_stroff);
                    302:                printf("  cth_strlen   = %d\n", cth->cth_strlen);
                    303:                printf("\n");
                    304:        }
                    305:
                    306:        if (flags & DUMP_LABEL) {
                    307:                uint32_t                 lbloff = cth->cth_lbloff;
                    308:                struct ctf_lblent       *ctl;
                    309:
                    310:                while (lbloff < cth->cth_objtoff) {
                    311:                        ctl = (struct ctf_lblent *)(data + lbloff);
                    312:
                    313:                        printf("  %5u %s\n", ctl->ctl_typeidx,
                    314:                            ctf_off2name(cth, data, dlen, ctl->ctl_label));
                    315:
                    316:                        lbloff += sizeof(*ctl);
                    317:                }
                    318:                printf("\n");
                    319:        }
                    320:
                    321:        if (flags & DUMP_OBJECT) {
                    322:                uint32_t                 objtoff = cth->cth_objtoff;
                    323:                size_t                   idx = 0, i = 0;
                    324:                uint16_t                *dsp;
                    325:                const char              *s;
                    326:                int                      l;
                    327:
                    328:                while (objtoff < cth->cth_funcoff) {
                    329:                        dsp = (uint16_t *)(data + objtoff);
                    330:
                    331:                        l = printf("  [%zu] %u", i++, *dsp);
                    332:                        if ((s = elf_idx2sym(&idx, STT_OBJECT)) != NULL)
                    333:                                printf("%*s %s (%zu)\n", (14 - l), "", s, idx);
                    334:                        else
                    335:                                printf("\n");
                    336:
                    337:                        objtoff += sizeof(*dsp);
                    338:                }
                    339:                printf("\n");
                    340:        }
                    341:
                    342:        if (flags & DUMP_FUNCTION) {
                    343:                uint16_t                *fsp, kind, vlen;
                    344:                size_t                   idx = 0, i = -1;
                    345:                const char              *s;
                    346:                int                      l;
                    347:
                    348:                fsp = (uint16_t *)(data + cth->cth_funcoff);
                    349:                while (fsp < (uint16_t *)(data + cth->cth_typeoff)) {
                    350:                        kind = CTF_INFO_KIND(*fsp);
                    351:                        vlen = CTF_INFO_VLEN(*fsp);
                    352:                        s = elf_idx2sym(&idx, STT_FUNC);
                    353:                        fsp++;
                    354:                        i++;
                    355:
                    356:                        if (kind == CTF_K_UNKNOWN && vlen == 0)
                    357:                                continue;
                    358:
                    359:                        l = printf("  [%zu] FUNC ", i);
                    360:                        if (s != NULL)
                    361:                                printf("(%s)", s);
                    362:                        printf(" returns: %u args: (", *fsp++);
                    363:                        while (vlen-- > 0)
                    364:                                printf("%u%s", *fsp++, (vlen > 0) ? ", " : "");
                    365:                        printf(")\n");
                    366:                }
                    367:                printf("\n");
                    368:        }
                    369:
                    370:        if (flags & DUMP_TYPE) {
                    371:                uint32_t                 idx = 1, offset = cth->cth_typeoff;
                    372:
                    373:                while (offset < cth->cth_stroff) {
                    374:                        offset += ctf_dump_type(cth, data, dlen, offset, idx++);
                    375:                }
                    376:                printf("\n");
                    377:        }
                    378:
                    379:        if (flags & DUMP_STRTAB) {
                    380:                uint32_t                 offset = 0;
                    381:                const char              *str;
                    382:
                    383:                while (offset < cth->cth_strlen) {
                    384:                        str = ctf_off2name(cth, data, dlen, offset);
                    385:
                    386:                        printf("  [%u] ", offset);
                    387:                        if (strcmp(str, "(anon)"))
                    388:                                offset += printf("%s\n", str);
                    389:                        else {
                    390:                                printf("\\0\n");
                    391:                                offset++;
                    392:                        }
                    393:                }
                    394:                printf("\n");
                    395:        }
                    396:
                    397:        if (cth->cth_flags & CTF_F_COMPRESS)
                    398:                free(data);
                    399:
                    400:        return 0;
                    401: }
                    402:
                    403: uint32_t
                    404: ctf_dump_type(struct ctf_header *cth, const char *data, off_t dlen,
                    405:     uint32_t offset, uint32_t idx)
                    406: {
                    407:        const char              *p = data + offset;
                    408:        const struct ctf_type   *ctt = (struct ctf_type *)p;
                    409:        const struct ctf_array  *cta;
                    410:        uint16_t                *argp, i, kind, vlen, root;
                    411:        uint32_t                 eob, toff;
                    412:        uint64_t                 size;
                    413:        const char              *name, *kname;
                    414:
                    415:        kind = CTF_INFO_KIND(ctt->ctt_info);
                    416:        vlen = CTF_INFO_VLEN(ctt->ctt_info);
                    417:        root = CTF_INFO_ISROOT(ctt->ctt_info);
                    418:        name = ctf_off2name(cth, data, dlen, ctt->ctt_name);
                    419:
                    420:        if (root)
                    421:                printf("  <%u> ", idx);
                    422:        else
                    423:                printf("  [%u] ", idx);
                    424:
                    425:        if ((kname = ctf_kind2name(kind)) != NULL)
                    426:                printf("%s %s", kname, name);
                    427:
                    428:        if (ctt->ctt_size <= CTF_MAX_SIZE) {
                    429:                size = ctt->ctt_size;
                    430:                toff = sizeof(struct ctf_stype);
                    431:        } else {
                    432:                size = CTF_TYPE_LSIZE(ctt);
                    433:                toff = sizeof(struct ctf_type);
                    434:        }
                    435:
                    436:        switch (kind) {
                    437:        case CTF_K_UNKNOWN:
                    438:        case CTF_K_FORWARD:
                    439:                break;
                    440:        case CTF_K_INTEGER:
                    441:                eob = *((uint32_t *)(p + toff));
                    442:                toff += sizeof(uint32_t);
                    443:                printf(" encoding=%s offset=%u bits=%u",
                    444:                    ctf_enc2name(CTF_INT_ENCODING(eob)), CTF_INT_OFFSET(eob),
                    445:                    CTF_INT_BITS(eob));
                    446:                break;
                    447:        case CTF_K_FLOAT:
                    448:                eob = *((uint32_t *)(p + toff));
                    449:                toff += sizeof(uint32_t);
                    450:                printf(" encoding=0x%x offset=%u bits=%u",
                    451:                    CTF_FP_ENCODING(eob), CTF_FP_OFFSET(eob), CTF_FP_BITS(eob));
                    452:                break;
                    453:        case CTF_K_ARRAY:
                    454:                cta = (struct ctf_array *)(p + toff);
                    455:                printf(" content: %u index: %u nelems: %u\n", cta->cta_contents,
                    456:                    cta->cta_index, cta->cta_nelems);
                    457:                toff += sizeof(struct ctf_array);
                    458:                break;
                    459:        case CTF_K_FUNCTION:
                    460:                argp = (uint16_t *)(p + toff);
                    461:                printf(" returns: %u args: (%u", ctt->ctt_type, *argp);
                    462:                for (i = 1; i < vlen; i++) {
                    463:                        argp++;
                    464:                        printf(", %u", *argp);
                    465:                }
                    466:                printf(")");
                    467:                toff += (vlen + (vlen & 1)) * sizeof(uint16_t);
                    468:                break;
                    469:        case CTF_K_STRUCT:
                    470:        case CTF_K_UNION:
                    471:                printf(" (%llu bytes)\n", size);
                    472:
                    473:                if (size < CTF_LSTRUCT_THRESH) {
                    474:                        for (i = 0; i < vlen; i++) {
                    475:                                struct ctf_member       *ctm;
                    476:
                    477:                                ctm = (struct ctf_member *)(p + toff);
                    478:                                toff += sizeof(struct ctf_member);
                    479:
                    480:                                printf("\t%s type=%u off=%u\n",
                    481:                                    ctf_off2name(cth, data, dlen,
                    482:                                        ctm->ctm_name),
                    483:                                    ctm->ctm_type, ctm->ctm_offset);
                    484:                        }
                    485:                } else {
                    486:                        for (i = 0; i < vlen; i++) {
                    487:                                struct ctf_lmember      *ctlm;
                    488:
                    489:                                ctlm = (struct ctf_lmember *)(p + toff);
                    490:                                toff += sizeof(struct ctf_lmember);
                    491:
                    492:                                printf("\t%s type=%u off=%llu\n",
                    493:                                    ctf_off2name(cth, data, dlen,
                    494:                                        ctlm->ctlm_name),
                    495:                                    ctlm->ctlm_type, CTF_LMEM_OFFSET(ctlm));
                    496:                        }
                    497:                }
                    498:                break;
                    499:        case CTF_K_ENUM:
                    500:                printf("\n");
                    501:                for (i = 0; i < vlen; i++) {
                    502:                        struct ctf_enum *cte;
                    503:
                    504:                        cte = (struct ctf_enum *)(p + toff);
                    505:                        toff += sizeof(struct ctf_enum);
                    506:
                    507:                        printf("\t%s = %d\n",
                    508:                            ctf_off2name(cth, data, dlen, cte->cte_name),
                    509:                            cte->cte_value);
                    510:                }
                    511:                break;
                    512:        case CTF_K_POINTER:
                    513:        case CTF_K_TYPEDEF:
                    514:        case CTF_K_VOLATILE:
                    515:        case CTF_K_CONST:
                    516:        case CTF_K_RESTRICT:
                    517:                printf(" refers to %u", ctt->ctt_type);
                    518:                break;
                    519:        default:
                    520:                errx(1, "incorrect type %u at offset %u", kind, offset);
                    521:        }
                    522:
                    523:        printf("\n");
                    524:
                    525:        return toff;
                    526: }
                    527:
                    528: const char *
                    529: ctf_kind2name(uint16_t kind)
                    530: {
                    531:        static const char *kind_name[] = { NULL, "INTEGER", "FLOAT", "POINTER",
                    532:           "ARRAY", "FUNCTION", "STRUCT", "UNION", "ENUM", "FORWARD",
                    533:           "TYPEDEF", "VOLATILE", "CONST", "RESTRICT" };
                    534:
                    535:        if (kind >= nitems(kind_name))
                    536:                return NULL;
                    537:
                    538:        return kind_name[kind];
                    539: }
                    540:
                    541: const char *
                    542: ctf_enc2name(uint16_t enc)
                    543: {
                    544:        static const char *enc_name[] = { "SIGNED", "CHAR", "SIGNED CHAR",
                    545:            "BOOL", "SIGNED BOOL" };
                    546:        static char invalid[7];
                    547:
                    548:        if (enc == CTF_INT_VARARGS)
                    549:                return "VARARGS";
                    550:
                    551:        if (enc > 0 && enc < nitems(enc_name))
                    552:                return enc_name[enc - 1];
                    553:
                    554:        snprintf(invalid, sizeof(invalid), "0x%x", enc);
                    555:        return invalid;
                    556: }
                    557:
                    558: const char *
                    559: ctf_off2name(struct ctf_header *cth, const char *data, off_t dlen,
                    560:     uint32_t offset)
                    561: {
                    562:        const char              *name;
                    563:
                    564:        if (CTF_NAME_STID(offset) != CTF_STRTAB_0)
                    565:                return "external";
                    566:
                    567:        if (CTF_NAME_OFFSET(offset) >= cth->cth_strlen)
                    568:                return "exceeds strlab";
                    569:
                    570:        if (cth->cth_stroff + CTF_NAME_OFFSET(offset) >= dlen)
                    571:                return "invalid";
                    572:
                    573:        name = data + cth->cth_stroff + CTF_NAME_OFFSET(offset);
                    574:        if (*name == '\0')
                    575:                return "(anon)";
                    576:
                    577:        return name;
                    578: }
                    579:
                    580: char *
                    581: decompress(const char *buf, size_t size, off_t len)
                    582: {
                    583: #ifdef ZLIB
                    584:        z_stream                 stream;
                    585:        char                    *data;
                    586:        int                      error;
                    587:
                    588:        data = malloc(len);
                    589:        if (data == NULL) {
                    590:                warn(NULL);
                    591:                return NULL;
                    592:        }
                    593:
                    594:        memset(&stream, 0, sizeof(stream));
                    595:        stream.next_in = (void *)buf;
                    596:        stream.avail_in = size;
                    597:        stream.next_out = (uint8_t *)data;
                    598:        stream.avail_out = len;
                    599:
                    600:        if ((error = inflateInit(&stream)) != Z_OK) {
                    601:                warnx("zlib inflateInit failed: %s", zError(error));
                    602:                goto exit;
                    603:        }
                    604:
                    605:        if ((error = inflate(&stream, Z_FINISH)) != Z_STREAM_END) {
                    606:                warnx("zlib inflate failed: %s", zError(error));
                    607:                inflateEnd(&stream);
                    608:                goto exit;
                    609:        }
                    610:
                    611:        if ((error = inflateEnd(&stream)) != Z_OK) {
                    612:                warnx("zlib inflateEnd failed: %s", zError(error));
                    613:                goto exit;
                    614:        }
                    615:
                    616:        if (stream.total_out != len) {
                    617:                warnx("decompression failed: %llu != %llu",
                    618:                    stream.total_out, len);
                    619:                goto exit;
                    620:        }
                    621:
                    622:        return data;
                    623:
                    624: exit:
                    625:        free(data);
                    626: #endif /* ZLIB */
                    627:        return NULL;
                    628: }
                    629:
                    630: __dead void
                    631: usage(void)
                    632: {
                    633:        fprintf(stderr, "usage: %s [-dfhlst] file ...\n",
                    634:            getprogname());
                    635:        exit(1);
                    636: }