Debian Codegraph @

Contrib/zip2exe/zlib/ioapi.c

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