/* * pak.c -- PAK archive handler * (C)Copyright 1999 by Hiroshi Takekawa * This file is part of Enfle. * * Last Modified: Sun Jan 9 14:50:52 2000. * $Id: pak.c,v 1.8 2000/01/23 11:18:12 sian Exp $ * * Enfle is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Enfle is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include "enfle.h" #include "pak.h" #include "utils.h" #include "plugin.h" int #ifdef PIC get_plugininfo(PluginInfo *p) #else archiver_pak_get_plugininfo(PluginInfo *p) #endif { p->version = 1; p->type = _Archiver; p->pluginname = "PAK Format Archiver Plugin version 0.1"; p->pluginshortname = FORMAT_NAME; p->author = "Hiroshi Takekawa"; p->dlhandle = NULL; /* set by plugin_load */ p->functions.archiver.open = pak_archive_open; return 1; } int init_pak_info(PAK_info *info, int nfiles) { int i; if ((info->filenames = calloc(nfiles, sizeof(char *))) == NULL) { fprintf(stderr, "pak_archive_open: No enough memory for filenames\n"); return 0; } for (i = 0; i < nfiles; i++) if ((info->filenames[i] = (char *)calloc(16, sizeof(char))) == NULL) { fprintf(stderr, "pak_archive_open: No enough memory for filenames[%d]\n", i); for (i--; i > 0; i--) free(info->filenames[i]); return 0; } if ((info->offsets = (int *)calloc(nfiles, sizeof(int))) == NULL) { fprintf(stderr, "pak_archive_open: No enough memory for offsets\n"); for (i = 0; i < nfiles; i++) free(info->filenames[i]); return 0; } if ((info->sizes = (int *)calloc(nfiles, sizeof(int))) == NULL) { fprintf(stderr, "pak_archive_open: No enough memory for sizes\n"); for (i = 0; i < nfiles; i++) free(info->filenames[i]); free(info->offsets); return 0; } if ((info->flags = (int *)calloc(nfiles, sizeof(int))) == NULL) { fprintf(stderr, "pak_archive_open: No enough memory for flags\n"); for (i = 0; i < nfiles; i++) free(info->filenames[i]); free(info->offsets); free(info->sizes); return 0; } return 1; } static void pak_free(void *d) { if (d != NULL) free(d); } void destroy_pak_info(Archive *ar) { int i; PAK_info *info = ar->info; pak_free(info->data); pak_free(info->flags); pak_free(info->sizes); pak_free(info->offsets); for (i = 0; i < ar->nfiles; i++) pak_free(info->filenames[i]); pak_free(info->filenames); } int pak_archive_open(Archive *ar) { PAK_info *info; PAK_type type; unsigned char buf[8]; int f; #ifdef DEBUG fprintf(stderr, "pak_archive_open: open %s\n", ar->filename); #endif if ((ar->fp = fopen(ar->filename, "rb")) == NULL) return 0; fseek(ar->fp, 0L, SEEK_END); ar->asize = ftell(ar->fp); fseek(ar->fp, 0L, SEEK_SET); if (fread(buf, 1, 8, ar->fp) != 8) { fclose(ar->fp); return 0; } if (memcmp(buf, "LEAFPACK", 8) == 0) { type = _LVN; ar->nfiles = read_little_word(ar); } else if ((ar->nfiles = get_little_dword(buf)) < 4096 && ar->nfiles > 0) /* more check needed */ type = _FANDISK; else { fclose(ar->fp); return 0; } /* allocate plugin unique info */ if ((ar->info = calloc(1, sizeof(PAK_info))) == NULL) { fprintf(stderr, "pak_archive_open: No enough memory for info\n"); return 0; } info = (PAK_info *)ar->info; info->type = type; switch (type) { case _LVN: #ifdef DEBUG fprintf(stderr, "PAK(LVN) identified.\n"); #endif f = pak_lvn_archive_open(ar); if (!f) ar->info = NULL; return f; case _FANDISK: f = pak_fan_archive_open(ar); if (!f) ar->info = NULL; return f; } fprintf(stderr, "Unknown PAK_type.\n"); return 0; } int pak_archive_close(Archive *ar) { int f = fclose(ar->fp); destroy_pak_info(ar); free(ar->info); return f; }