/* * bz2.c -- bz2 file handler * (C)Copyright 1999 by Hiroshi Takekawa * This file is part of Enfle. * * Last Modified: Fri May 19 23:27:14 2000. * $Id: bz2.c,v 1.4 2000/05/19 17:01:51 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 "utils.h" #include "plugin.h" #include "bz2.h" #include "bzlib_wrapper.h" int #ifdef PIC get_plugininfo(PluginInfo *p) #else archiver_bz2_get_plugininfo(PluginInfo *p) #endif { p->version = 1; p->type = _Archiver; p->pluginname = "BZ2 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 = bz2_archive_open; return 1; } int bz2_archive_open(Archive *ar) { unsigned char buf[2]; BZ2_info *info; if ((ar->fp = fopen(ar->filename, "rb")) == NULL) return 0; #ifdef DEBUG fprintf(stderr, "bz2_archive_open(): open %s\n", ar->filename); #endif #if 0 fseek(ar->fp, 0L, SEEK_END); ar->asize = ftell(ar->fp); fseek(ar->fp, 0L, SEEK_SET); #endif if (fread(buf, 1, 2, ar->fp) != 2) { fclose(ar->fp); return 0; } fclose(ar->fp); /* check ID */ if (buf[0] != 'B' || buf[1] != 'Z') return 0; if ((info = malloc(sizeof(BZ2_info))) == NULL) { fprintf(stderr, "No enough memory for BZ2_info\n"); exit(1); } if ((info->bzfile = BZOPEN(ar->filename, "rb")) == NULL) { free(info); return 0; } info->pos = 0; #ifdef DEBUG fprintf(stderr, "bz2_archive_open: bzopen'd\n"); #endif ar->nfiles = 1; ar->info = info; ar->format = FORMAT_NAME; ar->select = NULL; ar->seek = bz2_archive_seek; ar->tell = bz2_archive_tell; ar->read = bz2_archive_read; ar->close = bz2_archive_close; return 1; } int bz2_archive_seek(Archive *ar, long pos, int whence) { return (bzseek(ar, pos, whence) == -1) ? -1 : 0; } int bz2_archive_tell(Archive *ar) { BZ2_info *info = ar->info; return info->pos; } int bz2_archive_read(Archive *ar, unsigned char *buf, int size) { BZ2_info *info = ar->info; int r, err; if ((r = BZREAD(info->bzfile, buf, size)) < 0) { fprintf(stderr, "%s\n", BZERROR(info->bzfile, &err)); return -1; } info->pos += r; return r; } int bz2_archive_close(Archive *ar) { BZ2_info *info = ar->info; BZCLOSE(info->bzfile); free(ar->info); return 0; }