1 : /* Modified version by Even Rouault. :
2 : - change fill_fopen_filefunc to cpl_fill_fopen_filefunc
3 : - port to VSIL*L API
4 : - Remove old C style function prototypes
5 : - Add support for ZIP64
6 :
7 : Copyright (C) 2007-2008 Even Rouault
8 :
9 : Original licence available in port/LICENCE_minizip
10 : */
11 :
12 :
13 : /* ioapi.c -- IO base function header for compress/uncompress .zip
14 : files using zlib + zip or unzip API
15 :
16 : Version 1.01e, February 12th, 2005
17 :
18 : Copyright (C) 1998-2005 Gilles Vollant
19 : */
20 :
21 : #include <stdio.h>
22 : #include <stdlib.h>
23 : #include <string.h>
24 :
25 : #include "cpl_vsi.h"
26 :
27 : #include "zlib.h"
28 : #include "cpl_minizip_ioapi.h"
29 :
30 :
31 : static
32 : voidpf ZCALLBACK fopen_file_func OF((
33 : voidpf opaque,
34 : const char* filename,
35 : int mode));
36 :
37 : static
38 : uLong ZCALLBACK fread_file_func OF((
39 : voidpf opaque,
40 : voidpf stream,
41 : void* buf,
42 : uLong size));
43 :
44 : static
45 : uLong ZCALLBACK fwrite_file_func OF((
46 : voidpf opaque,
47 : voidpf stream,
48 : const void* buf,
49 : uLong size));
50 :
51 : static
52 : uLong64 ZCALLBACK ftell_file_func OF((
53 : voidpf opaque,
54 : voidpf stream));
55 :
56 : static
57 : long ZCALLBACK fseek_file_func OF((
58 : voidpf opaque,
59 : voidpf stream,
60 : uLong64 offset,
61 : int origin));
62 :
63 : static
64 : int ZCALLBACK fclose_file_func OF((
65 : voidpf opaque,
66 : voidpf stream));
67 :
68 : static
69 : int ZCALLBACK ferror_file_func OF((
70 : voidpf opaque,
71 : voidpf stream));
72 :
73 : static
74 19 : voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
75 : {
76 19 : FILE* file = NULL;
77 19 : const char* mode_fopen = NULL;
78 19 : if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
79 19 : mode_fopen = "rb";
80 : else
81 0 : if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
82 0 : mode_fopen = "r+b";
83 : else
84 0 : if (mode & ZLIB_FILEFUNC_MODE_CREATE)
85 0 : mode_fopen = "wb";
86 :
87 19 : if ((filename!=NULL) && (mode_fopen != NULL))
88 19 : file = VSIFOpenL(filename, mode_fopen);
89 19 : return file;
90 : }
91 :
92 : static
93 2993 : uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
94 : {
95 : uLong ret;
96 2993 : ret = (uLong)VSIFReadL(buf, 1, (size_t)size, (FILE *)stream);
97 2993 : return ret;
98 : }
99 :
100 : static
101 0 : uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
102 : {
103 : uLong ret;
104 0 : ret = (uLong)VSIFWriteL(buf, 1, (size_t)size, (FILE *)stream);
105 0 : return ret;
106 : }
107 :
108 : static
109 38 : uLong64 ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
110 : {
111 : uLong64 ret;
112 38 : ret = VSIFTellL((FILE *)stream);
113 38 : return ret;
114 : }
115 :
116 : static
117 269 : long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong64 offset, int origin)
118 : {
119 269 : int fseek_origin=0;
120 : long ret;
121 269 : switch (origin)
122 : {
123 : case ZLIB_FILEFUNC_SEEK_CUR :
124 124 : fseek_origin = SEEK_CUR;
125 124 : break;
126 : case ZLIB_FILEFUNC_SEEK_END :
127 38 : fseek_origin = SEEK_END;
128 38 : break;
129 : case ZLIB_FILEFUNC_SEEK_SET :
130 107 : fseek_origin = SEEK_SET;
131 107 : break;
132 0 : default: return -1;
133 : }
134 269 : ret = 0;
135 269 : VSIFSeekL((FILE *)stream, offset, fseek_origin);
136 269 : return ret;
137 : }
138 :
139 : static
140 19 : int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
141 : {
142 : int ret;
143 19 : ret = VSIFCloseL((FILE *)stream);
144 19 : return ret;
145 : }
146 :
147 : static
148 0 : int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
149 : {
150 : int ret;
151 0 : ret = 0; // FIXME
152 : //ret = ferror((FILE *)stream);
153 0 : return ret;
154 : }
155 :
156 19 : void cpl_fill_fopen_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
157 : {
158 19 : pzlib_filefunc_def->zopen_file = fopen_file_func;
159 19 : pzlib_filefunc_def->zread_file = fread_file_func;
160 19 : pzlib_filefunc_def->zwrite_file = fwrite_file_func;
161 19 : pzlib_filefunc_def->ztell_file = ftell_file_func;
162 19 : pzlib_filefunc_def->zseek_file = fseek_file_func;
163 19 : pzlib_filefunc_def->zclose_file = fclose_file_func;
164 19 : pzlib_filefunc_def->zerror_file = ferror_file_func;
165 19 : pzlib_filefunc_def->opaque = NULL;
166 19 : }
|