00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "pent_include.h"
00012
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016
00017 #include "zlib.h"
00018 #include "ioapi.h"
00019
00020
00021
00022
00023
00024 #ifndef SEEK_CUR
00025 #define SEEK_CUR 1
00026 #endif
00027
00028 #ifndef SEEK_END
00029 #define SEEK_END 2
00030 #endif
00031
00032 #ifndef SEEK_SET
00033 #define SEEK_SET 0
00034 #endif
00035
00036 namespace PentZip {
00037
00038 voidpf ZCALLBACK fopen_file_func OF((
00039 voidpf opaque,
00040 const char* filename,
00041 int mode));
00042
00043 uLong ZCALLBACK fread_file_func OF((
00044 voidpf opaque,
00045 voidpf stream,
00046 void* buf,
00047 uLong size));
00048
00049 uLong ZCALLBACK fwrite_file_func OF((
00050 voidpf opaque,
00051 voidpf stream,
00052 const void* buf,
00053 uLong size));
00054
00055 long ZCALLBACK ftell_file_func OF((
00056 voidpf opaque,
00057 voidpf stream));
00058
00059 long ZCALLBACK fseek_file_func OF((
00060 voidpf opaque,
00061 voidpf stream,
00062 uLong offset,
00063 int origin));
00064
00065 int ZCALLBACK fclose_file_func OF((
00066 voidpf opaque,
00067 voidpf stream));
00068
00069 int ZCALLBACK ferror_file_func OF((
00070 voidpf opaque,
00071 voidpf stream));
00072
00073
00074 voidpf ZCALLBACK fopen_file_func (voidpf opaque,const char* filename,int mode)
00075 {
00076 FILE* file = NULL;
00077 const char* mode_fopen = NULL;
00078 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
00079 mode_fopen = "rb";
00080 else
00081 if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
00082 mode_fopen = "r+b";
00083 else
00084 if (mode & ZLIB_FILEFUNC_MODE_CREATE)
00085 mode_fopen = "wb";
00086
00087 if ((filename!=NULL) && (mode_fopen != NULL))
00088 file = fopen(filename, mode_fopen);
00089 return file;
00090 }
00091
00092
00093 uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream,
00094 void* buf, uLong size)
00095 {
00096 uLong ret;
00097 ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
00098 return ret;
00099 }
00100
00101
00102 uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream,
00103 const void* buf, uLong size)
00104 {
00105 uLong ret;
00106 ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
00107 return ret;
00108 }
00109
00110 long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
00111 {
00112 long ret;
00113 ret = ftell((FILE *)stream);
00114 return ret;
00115 }
00116
00117 long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream,
00118 uLong offset, int origin)
00119 {
00120 int fseek_origin=0;
00121 long ret;
00122 switch (origin)
00123 {
00124 case ZLIB_FILEFUNC_SEEK_CUR :
00125 fseek_origin = SEEK_CUR;
00126 break;
00127 case ZLIB_FILEFUNC_SEEK_END :
00128 fseek_origin = SEEK_END;
00129 break;
00130 case ZLIB_FILEFUNC_SEEK_SET :
00131 fseek_origin = SEEK_SET;
00132 break;
00133 default: return -1;
00134 }
00135 ret = 0;
00136 fseek((FILE *)stream, offset, fseek_origin);
00137 return ret;
00138 }
00139
00140 int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
00141 {
00142 int ret;
00143 ret = fclose((FILE *)stream);
00144 return ret;
00145 }
00146
00147 int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
00148 {
00149 int ret;
00150 ret = ferror((FILE *)stream);
00151 return ret;
00152 }
00153
00154 void fill_fopen_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
00155 {
00156 pzlib_filefunc_def->zopen_file = fopen_file_func;
00157 pzlib_filefunc_def->zread_file = fread_file_func;
00158 pzlib_filefunc_def->zwrite_file = fwrite_file_func;
00159 pzlib_filefunc_def->ztell_file = ftell_file_func;
00160 pzlib_filefunc_def->zseek_file = fseek_file_func;
00161 pzlib_filefunc_def->zclose_file = fclose_file_func;
00162 pzlib_filefunc_def->zerror_file = ferror_file_func;
00163 pzlib_filefunc_def->opaque = NULL;
00164 }
00165
00166 }