tools/extract-pkg.c
static int extract(FILE *pkg, const PKGIndexEntry *entry, char *outpath)
{
    char readbuf[65536];

    if (fseek(pkg, entry->offset, SEEK_SET) != 0) {
        perror("fseek()");
        return 0;
    }
    if (!mkdir_parents(outpath)) {
        fprintf(stderr, "%s: Failed to create parent directories\n", outpath);
        return 0;
    }
    FILE *out = fopen(outpath, "wb");
    if (!out) {
        perror(outpath);
        return 0;
    }

    z_stream inflater;
    if (entry->nameofs_flags & PKGF_DEFLATED) {
        inflater.next_in = Z_NULL;
        inflater.avail_in = 0;
        inflater.zalloc = Z_NULL;
        inflater.zfree = Z_NULL;
        if (inflateInit(&inflater) != Z_OK) {
            fprintf(stderr, "%s: Failed to initialize inflater\n", outpath);
            goto fail;
        }
    }

    for (uint32_t pos = 0; pos < entry->datalen; pos += sizeof(readbuf)) {
        const int to_read = ubound(entry->datalen - pos, sizeof(readbuf));
        const int nread = fread(readbuf, 1, to_read, pkg);
        if (nread != to_read) {
            fprintf(stderr, "%s: Short read on package file\n", outpath);
            goto fail;
        }
        if (entry->nameofs_flags & PKGF_DEFLATED) {
            char outbuf[65536];
            inflater.next_in = (void *)readbuf;
            inflater.avail_in = nread;
            int result;
            while (inflater.next_out = (void *)outbuf,
                   inflater.avail_out = sizeof(outbuf),
                   result = inflate(&inflater, Z_NO_FLUSH),
                   result == Z_OK
                   || result == Z_STREAM_END
                   || (result == Z_BUF_ERROR && inflater.avail_in > 0)) {
                const int to_write = (char *)inflater.next_out - outbuf;
                if (to_write > 0) {
                    const int nwritten = fwrite(outbuf, 1, to_write, out);
                    if (nwritten != to_write) {
                        perror(outpath);
                        goto fail;
                    }
                }
                if (result == Z_STREAM_END) {
                    break;
                }
            }
            if (result != Z_STREAM_END && result != Z_BUF_ERROR) {
                fprintf(stderr, "%s: Decompression error %d\n", outpath, result);
                goto fail;
            }
        } else {
            const int nwritten = fwrite(readbuf, 1, nread, out);
            if (nwritten != nread) {
                perror(outpath);
                goto fail;
            }
        }
    }

    fclose(out);
    if (entry->nameofs_flags & PKGF_DEFLATED) {
        inflateEnd(&inflater);
    }
    return 1;

  fail:
    fclose(out);
    remove(outpath);
    if (entry->nameofs_flags & PKGF_DEFLATED) {
        inflateEnd(&inflater);
    }
    return 0;
}
/**
 * extract:  Extract a data file from a package file.
 *
 * [Parameters]
 *     pkg: Open file handle to the package file.
 *     entry: Index entry for the data file to extract.
 *     outpath: Pathname to which to write the data file.
 * [Return value]
 *     True on success, false on error.
 */
Extract a data file from a package file.
[Parameters]
pkg: Open file handle to the package file.
entry: Index entry for the data file to extract.
outpath: Pathname to which to write the data file.
[Return value]
True on success, false on error.
Extract a data file from a package file . [ Parameters ] pkg : Open file handle to the package file . entry : Index entry for the data file to extract . outpath : Pathname to which to write the data file . [ Return value ] True on success false on error .
static int extract(FILE *pkg, const PKGIndexEntry *entry, char *outpath)
{
    char readbuf[65536];
    if (fseek(pkg, entry->offset, SEEK_SET) != 0) {
        perror("fseek()");
        return 0;
    }
    if (!mkdir_parents(outpath)) {
        fprintf(stderr, "%s: Failed to create parent directories\n", outpath);
        return 0;
    }
    FILE *out = fopen(outpath, "wb");
    if (!out) {
        perror(outpath);
        return 0;
    }
    z_stream inflater;
    if (entry->nameofs_flags & PKGF_DEFLATED) {
        inflater.next_in = Z_NULL;
        inflater.avail_in = 0;
        inflater.zalloc = Z_NULL;
        inflater.zfree = Z_NULL;
        if (inflateInit(&inflater) != Z_OK) {
            fprintf(stderr, "%s: Failed to initialize inflater\n", outpath);
            goto fail;
        }
    }
    for (uint32_t pos = 0; pos < entry->datalen; pos += sizeof(readbuf)) {
        const int to_read = ubound(entry->datalen - pos, sizeof(readbuf));
        const int nread = fread(readbuf, 1, to_read, pkg);
        if (nread != to_read) {
            fprintf(stderr, "%s: Short read on package file\n", outpath);
            goto fail;
        }
        if (entry->nameofs_flags & PKGF_DEFLATED) {
            char outbuf[65536];
            inflater.next_in = (void *)readbuf;
            inflater.avail_in = nread;
            int result;
            while (inflater.next_out = (void *)outbuf,
                   inflater.avail_out = sizeof(outbuf),
                   result = inflate(&inflater, Z_NO_FLUSH),
                   result == Z_OK
                   || result == Z_STREAM_END
                   || (result == Z_BUF_ERROR && inflater.avail_in > 0)) {
                const int to_write = (char *)inflater.next_out - outbuf;
                if (to_write > 0) {
                    const int nwritten = fwrite(outbuf, 1, to_write, out);
                    if (nwritten != to_write) {
                        perror(outpath);
                        goto fail;
                    }
                }
                if (result == Z_STREAM_END) {
                    break;
                }
            }
            if (result != Z_STREAM_END && result != Z_BUF_ERROR) {
                fprintf(stderr, "%s: Decompression error %d\n", outpath, result);
                goto fail;
            }
        } else {
            const int nwritten = fwrite(readbuf, 1, nread, out);
            if (nwritten != nread) {
                perror(outpath);
                goto fail;
            }
        }
    }
    fclose(out);
    if (entry->nameofs_flags & PKGF_DEFLATED) {
        inflateEnd(&inflater);
    }
    return 1;
  fail:
    fclose(out);
    remove(outpath);
    if (entry->nameofs_flags & PKGF_DEFLATED) {
        inflateEnd(&inflater);
    }
    return 0;
}
static int extract ( FILE * pkg , const PKGIndexEntry * entry , char * outpath ) { char readbuf [ 65536 ] ; if ( fseek ( pkg , entry -> offset , SEEK_SET ) != 0 ) { perror ( " " ) ; return 0 ; } if ( ! mkdir_parents ( outpath ) ) { fprintf ( stderr , " \n " , outpath ) ; return 0 ; } FILE * out = fopen ( outpath , " " ) ; if ( ! out ) { perror ( outpath ) ; return 0 ; } z_stream inflater ; if ( entry -> nameofs_flags & PKGF_DEFLATED ) { inflater . next_in = Z_NULL ; inflater . avail_in = 0 ; inflater . zalloc = Z_NULL ; inflater . zfree = Z_NULL ; if ( inflateInit ( & inflater ) != Z_OK ) { fprintf ( stderr , " \n " , outpath ) ; goto fail ; } } for ( uint32_t pos = 0 ; pos < entry -> datalen ; pos += sizeof ( readbuf ) ) { const int to_read = ubound ( entry -> datalen - pos , sizeof ( readbuf ) ) ; const int nread = fread ( readbuf , 1 , to_read , pkg ) ; if ( nread != to_read ) { fprintf ( stderr , " \n " , outpath ) ; goto fail ; } if ( entry -> nameofs_flags & PKGF_DEFLATED ) { char outbuf [ 65536 ] ; inflater . next_in = ( void * ) readbuf ; inflater . avail_in = nread ; int result ; while ( inflater . next_out = ( void * ) outbuf , inflater . avail_out = sizeof ( outbuf ) , result = inflate ( & inflater , Z_NO_FLUSH ) , result == Z_OK || result == Z_STREAM_END || ( result == Z_BUF_ERROR && inflater . avail_in > 0 ) ) { const int to_write = ( char * ) inflater . next_out - outbuf ; if ( to_write > 0 ) { const int nwritten = fwrite ( outbuf , 1 , to_write , out ) ; if ( nwritten != to_write ) { perror ( outpath ) ; goto fail ; } } if ( result == Z_STREAM_END ) { break ; } } if ( result != Z_STREAM_END && result != Z_BUF_ERROR ) { fprintf ( stderr , " \n " , outpath , result ) ; goto fail ; } } else { const int nwritten = fwrite ( readbuf , 1 , nread , out ) ; if ( nwritten != nread ) { perror ( outpath ) ; goto fail ; } } } fclose ( out ) ; if ( entry -> nameofs_flags & PKGF_DEFLATED ) { inflateEnd ( & inflater ) ; } return 1 ; fail : fclose ( out ) ; remove ( outpath ) ; if ( entry -> nameofs_flags & PKGF_DEFLATED ) { inflateEnd ( & inflater ) ; } return 0 ; }
extract:  Extract a data file from a package file.
extract : Extract a data file from a package file .