/src/gdal/port/cpl_minizip_ioapi.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
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) 2008-2010, Even Rouault <even dot rouault at spatialys.com>  | 
8  |  |  | 
9  |  |    Original licence available in port/LICENCE_minizip  | 
10  |  | */  | 
11  |  |  | 
12  |  | /* ioapi.c -- IO base function header for compress/uncompress .zip  | 
13  |  |    files using zlib + zip or unzip API  | 
14  |  |  | 
15  |  |    Version 1.01e, February 12th, 2005  | 
16  |  |  | 
17  |  |    Copyright (C) 1998-2005 Gilles Vollant  | 
18  |  | */  | 
19  |  |  | 
20  |  | #include "cpl_port.h"  | 
21  |  | #include "cpl_minizip_ioapi.h"  | 
22  |  |  | 
23  |  | #include <cstddef>  | 
24  |  | #include <cstdio>  | 
25  |  | #include <cstdlib>  | 
26  |  | #include <cstring>  | 
27  |  | #if HAVE_FCNTL_H  | 
28  |  | #include <fcntl.h>  | 
29  |  | #endif  | 
30  |  |  | 
31  |  | #include "cpl_minizip_ioapi.h"  | 
32  |  | #include "cpl_vsi.h"  | 
33  |  | #include "zconf.h"  | 
34  |  | #include "zlib.h"  | 
35  |  |  | 
36  |  | static voidpf ZCALLBACK fopen_file_func(voidpf /* opaque */,  | 
37  |  |                                         const char *filename, int mode)  | 
38  | 1.43k  | { | 
39  | 1.43k  |     VSILFILE *file = nullptr;  | 
40  | 1.43k  |     const char *mode_fopen = nullptr;  | 
41  | 1.43k  |     if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)  | 
42  | 1.43k  |         mode_fopen = "rb";  | 
43  | 0  |     else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)  | 
44  | 0  |         mode_fopen = "r+b";  | 
45  | 0  |     else if (mode & ZLIB_FILEFUNC_MODE_CREATE)  | 
46  | 0  |     { | 
47  | 0  |         mode_fopen = "wb";  | 
48  | 0  |         if (filename != nullptr)  | 
49  | 0  |             return VSIFOpenExL(filename, mode_fopen, true);  | 
50  | 0  |     }  | 
51  |  |  | 
52  | 1.43k  |     if ((filename != nullptr) && (mode_fopen != nullptr))  | 
53  | 1.43k  |         file = VSIFOpenL(filename, mode_fopen);  | 
54  | 1.43k  |     return file;  | 
55  | 1.43k  | }  | 
56  |  |  | 
57  |  | static uLong ZCALLBACK fread_file_func(voidpf /* opaque */, voidpf stream,  | 
58  |  |                                        void *buf, uLong size)  | 
59  | 22.3k  | { | 
60  | 22.3k  |     uLong ret = static_cast<uLong>(VSIFReadL(buf, 1, static_cast<size_t>(size),  | 
61  | 22.3k  |                                              static_cast<VSILFILE *>(stream)));  | 
62  | 22.3k  |     return ret;  | 
63  | 22.3k  | }  | 
64  |  |  | 
65  |  | static uLong ZCALLBACK fwrite_file_func(voidpf /* opaque */, voidpf stream,  | 
66  |  |                                         const void *buf, uLong size)  | 
67  | 0  | { | 
68  | 0  |     uLong ret = static_cast<uLong>(VSIFWriteL(buf, 1, static_cast<size_t>(size),  | 
69  | 0  |                                               static_cast<VSILFILE *>(stream)));  | 
70  | 0  |     return ret;  | 
71  | 0  | }  | 
72  |  |  | 
73  |  | static uLong64 ZCALLBACK ftell_file_func(voidpf /* opaque */, voidpf stream)  | 
74  | 2.03k  | { | 
75  | 2.03k  |     uLong64 ret;  | 
76  | 2.03k  |     ret = VSIFTellL(reinterpret_cast<VSILFILE *>(stream));  | 
77  | 2.03k  |     return ret;  | 
78  | 2.03k  | }  | 
79  |  |  | 
80  |  | static long ZCALLBACK fseek_file_func(voidpf /* opaque */, voidpf stream,  | 
81  |  |                                       uLong64 offset, int origin)  | 
82  | 3.05k  | { | 
83  | 3.05k  |     int fseek_origin = 0;  | 
84  | 3.05k  |     switch (origin)  | 
85  | 3.05k  |     { | 
86  | 0  |         case ZLIB_FILEFUNC_SEEK_CUR:  | 
87  | 0  |             fseek_origin = SEEK_CUR;  | 
88  | 0  |             break;  | 
89  | 2.03k  |         case ZLIB_FILEFUNC_SEEK_END:  | 
90  | 2.03k  |             fseek_origin = SEEK_END;  | 
91  | 2.03k  |             break;  | 
92  | 1.01k  |         case ZLIB_FILEFUNC_SEEK_SET:  | 
93  | 1.01k  |             fseek_origin = SEEK_SET;  | 
94  | 1.01k  |             break;  | 
95  | 0  |         default:  | 
96  | 0  |             return -1;  | 
97  | 3.05k  |     }  | 
98  | 3.05k  |     return VSIFSeekL(reinterpret_cast<VSILFILE *>(stream), offset,  | 
99  | 3.05k  |                      fseek_origin);  | 
100  | 3.05k  | }  | 
101  |  |  | 
102  |  | static int ZCALLBACK fclose_file_func(voidpf /* opaque */, voidpf stream)  | 
103  | 1.01k  | { | 
104  | 1.01k  |     return VSIFCloseL(reinterpret_cast<VSILFILE *>(stream));  | 
105  | 1.01k  | }  | 
106  |  |  | 
107  |  | static int ZCALLBACK ferror_file_func(voidpf /* opaque */, voidpf /* stream */)  | 
108  | 22.3k  | { | 
109  |  |     // ret = ferror((FILE *)stream);  | 
110  | 22.3k  |     return 0;  | 
111  | 22.3k  | }  | 
112  |  |  | 
113  |  | void cpl_fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def)  | 
114  | 1.43k  | { | 
115  | 1.43k  |     pzlib_filefunc_def->zopen_file = fopen_file_func;  | 
116  | 1.43k  |     pzlib_filefunc_def->zread_file = fread_file_func;  | 
117  | 1.43k  |     pzlib_filefunc_def->zwrite_file = fwrite_file_func;  | 
118  | 1.43k  |     pzlib_filefunc_def->ztell_file = ftell_file_func;  | 
119  | 1.43k  |     pzlib_filefunc_def->zseek_file = fseek_file_func;  | 
120  | 1.43k  |     pzlib_filefunc_def->zclose_file = fclose_file_func;  | 
121  | 1.43k  |     pzlib_filefunc_def->zerror_file = ferror_file_func;  | 
122  | 1.43k  |     pzlib_filefunc_def->opaque = nullptr;  | 
123  | 1.43k  | }  |