/* * gz.c -- gz file handler * (C)Copyright 1999 by Hiroshi Takekawa * This file is part of Enfle. * * Last Modified: Wed Sep 29 20:32:44 1999. * $Id: gz.c,v 1.3 1999/09/29 12:22:54 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 "gz.h" int #ifdef PIC get_plugininfo(PluginInfo *p) #else archiver_gz_get_plugininfo(PluginInfo *p) #endif { p->version = 1; p->type = _Archiver; p->pluginname = "GZ 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 = gz_archive_open; return 1; } int gz_archive_open(Archive *ar) { unsigned char buf[2]; GZ_info *info; if ((ar->fp = fopen(ar->filename, "rb")) == NULL) return 0; #ifdef DEBUG fprintf(stderr, "gz_archive_open(): open %s\n", ar->filename); #endif fseek(ar->fp, 0L, SEEK_END); ar->asize = ftell(ar->fp); fseek(ar->fp, 0L, SEEK_SET); if (fread(buf, 1, 2, ar->fp) != 2) { fclose(ar->fp); return 0; } fclose(ar->fp); /* check ID1 ID2 */ if (buf[0] != 0x1f || buf[1] != 0x8b) return 0; if ((info = malloc(sizeof(GZ_info))) == NULL) { fprintf(stderr, "No enough memory for GZ_info\n"); exit(1); } if ((info->gzfile = gzopen(ar->filename, "rb")) == NULL) { free(info); return 0; } ar->nfiles = 1; ar->info = info; ar->format = FORMAT_NAME; ar->select = NULL; ar->seek = gz_archive_seek; ar->tell = gz_archive_tell; ar->read = gz_archive_read; ar->close = gz_archive_close; return 1; } int gz_archive_seek(Archive *ar, long ptr, int whence) { GZ_info *info = ar->info; /* hmm... gzseek() does not support SEEK_END. */ return (gzseek(info->gzfile, ptr, whence) == -1) ? -1 : 0; } int gz_archive_tell(Archive *ar) { GZ_info *info = ar->info; return gztell(info->gzfile); } int gz_archive_read(Archive *ar, unsigned char *buf, int size) { GZ_info *info = ar->info; int r, err; if ((r = gzread(info->gzfile, buf, size)) < 0) { fprintf(stderr, "%s\n", gzerror(info->gzfile, &err)); return -1; } return r; } int gz_archive_close(Archive *ar) { GZ_info *info = ar->info; int f = gzclose(info->gzfile); free(ar->info); return f; }