Home | History | Annotate | Line # | Download | only in minizip
ioapi.c revision 1.1
      1 /*	$NetBSD: ioapi.c,v 1.1 2006/01/14 20:10:57 christos Exp $	*/
      2 
      3 /* ioapi.c -- IO base function header for compress/uncompress .zip
      4    files using zlib + zip or unzip API
      5 
      6    Version 1.01e, February 12th, 2005
      7 
      8    Copyright (C) 1998-2005 Gilles Vollant
      9 */
     10 
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 #include <string.h>
     14 
     15 #include "zlib.h"
     16 #include "ioapi.h"
     17 
     18 
     19 
     20 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
     21 
     22 #ifndef SEEK_CUR
     23 #define SEEK_CUR    1
     24 #endif
     25 
     26 #ifndef SEEK_END
     27 #define SEEK_END    2
     28 #endif
     29 
     30 #ifndef SEEK_SET
     31 #define SEEK_SET    0
     32 #endif
     33 
     34 voidpf ZCALLBACK fopen_file_func OF((
     35    voidpf opaque,
     36    const char* filename,
     37    int mode));
     38 
     39 uLong ZCALLBACK fread_file_func OF((
     40    voidpf opaque,
     41    voidpf stream,
     42    void* buf,
     43    uLong size));
     44 
     45 uLong ZCALLBACK fwrite_file_func OF((
     46    voidpf opaque,
     47    voidpf stream,
     48    const void* buf,
     49    uLong size));
     50 
     51 long ZCALLBACK ftell_file_func OF((
     52    voidpf opaque,
     53    voidpf stream));
     54 
     55 long ZCALLBACK fseek_file_func OF((
     56    voidpf opaque,
     57    voidpf stream,
     58    uLong offset,
     59    int origin));
     60 
     61 int ZCALLBACK fclose_file_func OF((
     62    voidpf opaque,
     63    voidpf stream));
     64 
     65 int ZCALLBACK ferror_file_func OF((
     66    voidpf opaque,
     67    voidpf stream));
     68 
     69 
     70 voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
     71    voidpf opaque;
     72    const char* filename;
     73    int mode;
     74 {
     75     FILE* file = NULL;
     76     const char* mode_fopen = NULL;
     77     if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
     78         mode_fopen = "rb";
     79     else
     80     if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
     81         mode_fopen = "r+b";
     82     else
     83     if (mode & ZLIB_FILEFUNC_MODE_CREATE)
     84         mode_fopen = "wb";
     85 
     86     if ((filename!=NULL) && (mode_fopen != NULL))
     87         file = fopen(filename, mode_fopen);
     88     return file;
     89 }
     90 
     91 
     92 uLong ZCALLBACK fread_file_func (opaque, stream, buf, size)
     93    voidpf opaque;
     94    voidpf stream;
     95    void* buf;
     96    uLong size;
     97 {
     98     uLong ret;
     99     ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
    100     return ret;
    101 }
    102 
    103 
    104 uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size)
    105    voidpf opaque;
    106    voidpf stream;
    107    const void* buf;
    108    uLong size;
    109 {
    110     uLong ret;
    111     ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
    112     return ret;
    113 }
    114 
    115 long ZCALLBACK ftell_file_func (opaque, stream)
    116    voidpf opaque;
    117    voidpf stream;
    118 {
    119     long ret;
    120     ret = ftell((FILE *)stream);
    121     return ret;
    122 }
    123 
    124 long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
    125    voidpf opaque;
    126    voidpf stream;
    127    uLong offset;
    128    int origin;
    129 {
    130     int fseek_origin=0;
    131     long ret;
    132     switch (origin)
    133     {
    134     case ZLIB_FILEFUNC_SEEK_CUR :
    135         fseek_origin = SEEK_CUR;
    136         break;
    137     case ZLIB_FILEFUNC_SEEK_END :
    138         fseek_origin = SEEK_END;
    139         break;
    140     case ZLIB_FILEFUNC_SEEK_SET :
    141         fseek_origin = SEEK_SET;
    142         break;
    143     default: return -1;
    144     }
    145     ret = 0;
    146     fseek((FILE *)stream, offset, fseek_origin);
    147     return ret;
    148 }
    149 
    150 int ZCALLBACK fclose_file_func (opaque, stream)
    151    voidpf opaque;
    152    voidpf stream;
    153 {
    154     int ret;
    155     ret = fclose((FILE *)stream);
    156     return ret;
    157 }
    158 
    159 int ZCALLBACK ferror_file_func (opaque, stream)
    160    voidpf opaque;
    161    voidpf stream;
    162 {
    163     int ret;
    164     ret = ferror((FILE *)stream);
    165     return ret;
    166 }
    167 
    168 void fill_fopen_filefunc (pzlib_filefunc_def)
    169   zlib_filefunc_def* pzlib_filefunc_def;
    170 {
    171     pzlib_filefunc_def->zopen_file = fopen_file_func;
    172     pzlib_filefunc_def->zread_file = fread_file_func;
    173     pzlib_filefunc_def->zwrite_file = fwrite_file_func;
    174     pzlib_filefunc_def->ztell_file = ftell_file_func;
    175     pzlib_filefunc_def->zseek_file = fseek_file_func;
    176     pzlib_filefunc_def->zclose_file = fclose_file_func;
    177     pzlib_filefunc_def->zerror_file = ferror_file_func;
    178     pzlib_filefunc_def->opaque = NULL;
    179 }
    180