tools/extract-pkg.c
static int read_package(const char *path, FILE **fp_ret, int *nfiles_ret,
                        PKGIndexEntry **index_ret, char **namebuf_ret)
{
    /* Open the package file. */
    *fp_ret = fopen(path, "rb");
    if (!*fp_ret) {
        fprintf(stderr, "fopen(%s): %s\n", path, strerror(errno));
        goto error_return;
    }

    /* Read in and verify the header data. */
    PKGHeader header;
    if (fread(&header, 1, 16, *fp_ret) != 16) {
        fprintf(stderr, "EOF reading %s\n", path);
        goto error_close_file;
    }
    if (memcmp(header.magic, PKG_MAGIC, 4) != 0) {
        fprintf(stderr, "Bad magic number reading %s (got %02X%02X%02X%02X,"
                " expected %02X%02X%02X%02X)\n", path, header.magic[0],
                header.magic[1], header.magic[2], header.magic[3],
                PKG_MAGIC[0], PKG_MAGIC[1], PKG_MAGIC[2], PKG_MAGIC[3]);
        goto error_close_file;
    }
    PKG_HEADER_SWAP_BYTES(header);
    if (header.header_size != sizeof(PKGHeader)) {
        fprintf(stderr, "Bad header size %d in %s\n", header.header_size,
                path);
        goto error_close_file;
    }
    if (header.entry_size != sizeof(PKGIndexEntry)) {
        fprintf(stderr, "Bad index entry size %d in %s\n", header.entry_size,
                path);
        goto error_close_file;
    }

    /* Allocate memory for the index and pathname buffers. */
    *nfiles_ret = header.entry_count;
    const int index_size = sizeof(**index_ret) * *nfiles_ret;
    *index_ret = malloc(index_size);
    if (!*index_ret) {
        fprintf(stderr, "No memory for %s directory (%d*%d)\n", path,
             *nfiles_ret, (int)sizeof(**index_ret));
        goto error_close_file;
    }
    *namebuf_ret = malloc(header.name_size);
    if (!*namebuf_ret) {
        fprintf(stderr, "No memory for %s pathnames (%u bytes)\n", path,
             header.name_size);
        goto error_free_index;
    }

    /* Read in the file index. */
    if ((int)fread(*index_ret, 1, index_size, *fp_ret) != index_size) {
        fprintf(stderr, "EOF reading %s directory\n", path);
        goto error_free_namebuf;
    }
    if ((int)fread(*namebuf_ret, 1, header.name_size, *fp_ret)
        != (int)header.name_size)
    {
        fprintf(stderr, "EOF reading %s pathname table\n", path);
        goto error_free_namebuf;
    }
    PKG_INDEX_SWAP_BYTES(*index_ret, *nfiles_ret);

    /* Success! */
    return 1;

  error_free_namebuf:
    free(*namebuf_ret);
  error_free_index:
    free(*index_ret);
  error_close_file:
    fclose(*fp_ret);
  error_return:
    return 0;
}
/**
 * read_package:  Read and return the given package file's index along with
 * an open file handle for the package file.
 *
 * [Parameters]
 *     path: Package file pathname.
 *     fp_ret: Pointer to variable to receive an open file handle for
 *         reading from the package file.
 *     nfiles_ret: Pointer to variable to receive the number of data files
 *         stored in the package file.
 *     index_ret: Pointer to variable to receive a malloc()-allocated array
 *         of index entries for the package.
 *     namebuf_ret: Pointer to variable to receive the malloc()-allocated
 *         filename buffer for data file names.
 * [Return value]
 *     True on success, false on error.
 */
Read and return the given package file's index along with
an open file handle for the package file.

[Parameters]
path: Package file pathname.
fp_ret: Pointer to variable to receive an open file handle for
reading from the package file.
nfiles_ret: Pointer to variable to receive the number of data files
stored in the package file.
index_ret: Pointer to variable to receive a malloc()-allocated array
of index entries for the package.
namebuf_ret: Pointer to variable to receive the malloc()-allocated
filename buffer for data file names.
[Return value]
True on success, false on error.
Read and return the given package file ' s index along with an open file handle for the package file . [ Parameters ] path : Package file pathname . fp_ret : Pointer to variable to receive an open file handle for reading from the package file . nfiles_ret : Pointer to variable to receive the number of data files stored in the package file . index_ret : Pointer to variable to receive a malloc () - allocated array of index entries for the package . namebuf_ret : Pointer to variable to receive the malloc () - allocated filename buffer for data file names . [ Return value ] True on success false on error .
static int read_package(const char *path, FILE **fp_ret, int *nfiles_ret,
                        PKGIndexEntry **index_ret, char **namebuf_ret)
{
    *fp_ret = fopen(path, "rb");
    if (!*fp_ret) {
        fprintf(stderr, "fopen(%s): %s\n", path, strerror(errno));
        goto error_return;
    }
    PKGHeader header;
    if (fread(&header, 1, 16, *fp_ret) != 16) {
        fprintf(stderr, "EOF reading %s\n", path);
        goto error_close_file;
    }
    if (memcmp(header.magic, PKG_MAGIC, 4) != 0) {
        fprintf(stderr, "Bad magic number reading %s (got %02X%02X%02X%02X,"
                " expected %02X%02X%02X%02X)\n", path, header.magic[0],
                header.magic[1], header.magic[2], header.magic[3],
                PKG_MAGIC[0], PKG_MAGIC[1], PKG_MAGIC[2], PKG_MAGIC[3]);
        goto error_close_file;
    }
    PKG_HEADER_SWAP_BYTES(header);
    if (header.header_size != sizeof(PKGHeader)) {
        fprintf(stderr, "Bad header size %d in %s\n", header.header_size,
                path);
        goto error_close_file;
    }
    if (header.entry_size != sizeof(PKGIndexEntry)) {
        fprintf(stderr, "Bad index entry size %d in %s\n", header.entry_size,
                path);
        goto error_close_file;
    }
    *nfiles_ret = header.entry_count;
    const int index_size = sizeof(**index_ret) * *nfiles_ret;
    *index_ret = malloc(index_size);
    if (!*index_ret) {
        fprintf(stderr, "No memory for %s directory (%d*%d)\n", path,
             *nfiles_ret, (int)sizeof(**index_ret));
        goto error_close_file;
    }
    *namebuf_ret = malloc(header.name_size);
    if (!*namebuf_ret) {
        fprintf(stderr, "No memory for %s pathnames (%u bytes)\n", path,
             header.name_size);
        goto error_free_index;
    }
    if ((int)fread(*index_ret, 1, index_size, *fp_ret) != index_size) {
        fprintf(stderr, "EOF reading %s directory\n", path);
        goto error_free_namebuf;
    }
    if ((int)fread(*namebuf_ret, 1, header.name_size, *fp_ret)
        != (int)header.name_size)
    {
        fprintf(stderr, "EOF reading %s pathname table\n", path);
        goto error_free_namebuf;
    }
    PKG_INDEX_SWAP_BYTES(*index_ret, *nfiles_ret);
    return 1;
  error_free_namebuf:
    free(*namebuf_ret);
  error_free_index:
    free(*index_ret);
  error_close_file:
    fclose(*fp_ret);
  error_return:
    return 0;
}
static int read_package ( const char * path , FILE * * fp_ret , int * nfiles_ret , PKGIndexEntry * * index_ret , char * * namebuf_ret ) { * fp_ret = fopen ( path , " " ) ; if ( ! * fp_ret ) { fprintf ( stderr , " \n " , path , strerror ( errno ) ) ; goto error_return ; } PKGHeader header ; if ( fread ( & header , 1 , 16 , * fp_ret ) != 16 ) { fprintf ( stderr , " \n " , path ) ; goto error_close_file ; } if ( memcmp ( header . magic , PKG_MAGIC , 4 ) != 0 ) { fprintf ( stderr , " " " \n " , path , header . magic [ 0 ] , header . magic [ 1 ] , header . magic [ 2 ] , header . magic [ 3 ] , PKG_MAGIC [ 0 ] , PKG_MAGIC [ 1 ] , PKG_MAGIC [ 2 ] , PKG_MAGIC [ 3 ] ) ; goto error_close_file ; } PKG_HEADER_SWAP_BYTES ( header ) ; if ( header . header_size != sizeof ( PKGHeader ) ) { fprintf ( stderr , " \n " , header . header_size , path ) ; goto error_close_file ; } if ( header . entry_size != sizeof ( PKGIndexEntry ) ) { fprintf ( stderr , " \n " , header . entry_size , path ) ; goto error_close_file ; } * nfiles_ret = header . entry_count ; const int index_size = sizeof ( * * index_ret ) * * nfiles_ret ; * index_ret = malloc ( index_size ) ; if ( ! * index_ret ) { fprintf ( stderr , " \n " , path , * nfiles_ret , ( int ) sizeof ( * * index_ret ) ) ; goto error_close_file ; } * namebuf_ret = malloc ( header . name_size ) ; if ( ! * namebuf_ret ) { fprintf ( stderr , " \n " , path , header . name_size ) ; goto error_free_index ; } if ( ( int ) fread ( * index_ret , 1 , index_size , * fp_ret ) != index_size ) { fprintf ( stderr , " \n " , path ) ; goto error_free_namebuf ; } if ( ( int ) fread ( * namebuf_ret , 1 , header . name_size , * fp_ret ) != ( int ) header . name_size ) { fprintf ( stderr , " \n " , path ) ; goto error_free_namebuf ; } PKG_INDEX_SWAP_BYTES ( * index_ret , * nfiles_ret ) ; return 1 ; error_free_namebuf : free ( * namebuf_ret ) ; error_free_index : free ( * index_ret ) ; error_close_file : fclose ( * fp_ret ) ; error_return : return 0 ; }
read_package:  Read and return the given package file's index along with
 an open file handle for the package file.
read_package : Read and return the given package file ' s index along with an open file handle for the package file .
/* Open the package file. */ /* Read in and verify the header data. */ /* Allocate memory for the index and pathname buffers. */ /* Read in the file index. */ /* Success! */