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