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