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