Line data Source code
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 : #include <fcntl.h> 28 : 29 : #include "cpl_minizip_ioapi.h" 30 : #include "cpl_vsi.h" 31 : #include "zconf.h" 32 : #include "zlib.h" 33 : 34 6164 : static voidpf ZCALLBACK fopen_file_func(voidpf /* opaque */, 35 : const char *filename, int mode) 36 : { 37 6164 : VSILFILE *file = nullptr; 38 6164 : const char *mode_fopen = nullptr; 39 6164 : if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ) 40 5670 : mode_fopen = "rb"; 41 494 : else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 42 234 : mode_fopen = "r+b"; 43 260 : else if (mode & ZLIB_FILEFUNC_MODE_CREATE) 44 : { 45 260 : mode_fopen = "wb"; 46 260 : if (filename != nullptr) 47 260 : return VSIFOpenExL(filename, mode_fopen, true); 48 : } 49 : 50 5904 : if ((filename != nullptr) && (mode_fopen != nullptr)) 51 5904 : file = VSIFOpenL(filename, mode_fopen); 52 5904 : return file; 53 : } 54 : 55 2930080 : static uLong ZCALLBACK fread_file_func(voidpf /* opaque */, voidpf stream, 56 : void *buf, uLong size) 57 : { 58 2930080 : uLong ret = static_cast<uLong>(VSIFReadL(buf, 1, static_cast<size_t>(size), 59 : static_cast<VSILFILE *>(stream))); 60 2930080 : return ret; 61 : } 62 : 63 6744 : static uLong ZCALLBACK fwrite_file_func(voidpf /* opaque */, voidpf stream, 64 : const void *buf, uLong size) 65 : { 66 6744 : uLong ret = static_cast<uLong>(VSIFWriteL(buf, 1, static_cast<size_t>(size), 67 : static_cast<VSILFILE *>(stream))); 68 6744 : return ret; 69 : } 70 : 71 48303 : static uLong64 ZCALLBACK ftell_file_func(voidpf /* opaque */, voidpf stream) 72 : { 73 : uLong64 ret; 74 48303 : ret = VSIFTellL(reinterpret_cast<VSILFILE *>(stream)); 75 48303 : return ret; 76 : } 77 : 78 419050 : static long ZCALLBACK fseek_file_func(voidpf /* opaque */, voidpf stream, 79 : uLong64 offset, int origin) 80 : { 81 419050 : int fseek_origin = 0; 82 419050 : switch (origin) 83 : { 84 88210 : case ZLIB_FILEFUNC_SEEK_CUR: 85 88210 : fseek_origin = SEEK_CUR; 86 88210 : break; 87 11578 : case ZLIB_FILEFUNC_SEEK_END: 88 11578 : fseek_origin = SEEK_END; 89 11578 : break; 90 319262 : case ZLIB_FILEFUNC_SEEK_SET: 91 319262 : fseek_origin = SEEK_SET; 92 319262 : break; 93 0 : default: 94 0 : return -1; 95 : } 96 419050 : return VSIFSeekL(reinterpret_cast<VSILFILE *>(stream), offset, 97 419050 : fseek_origin); 98 : } 99 : 100 6160 : static int ZCALLBACK fclose_file_func(voidpf /* opaque */, voidpf stream) 101 : { 102 6160 : return VSIFCloseL(reinterpret_cast<VSILFILE *>(stream)); 103 : } 104 : 105 44 : static int ZCALLBACK ferror_file_func(voidpf /* opaque */, voidpf /* stream */) 106 : { 107 : // ret = ferror((FILE *)stream); 108 44 : return 0; 109 : } 110 : 111 6164 : void cpl_fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def) 112 : { 113 6164 : pzlib_filefunc_def->zopen_file = fopen_file_func; 114 6164 : pzlib_filefunc_def->zread_file = fread_file_func; 115 6164 : pzlib_filefunc_def->zwrite_file = fwrite_file_func; 116 6164 : pzlib_filefunc_def->ztell_file = ftell_file_func; 117 6164 : pzlib_filefunc_def->zseek_file = fseek_file_func; 118 6164 : pzlib_filefunc_def->zclose_file = fclose_file_func; 119 6164 : pzlib_filefunc_def->zerror_file = ferror_file_func; 120 6164 : pzlib_filefunc_def->opaque = nullptr; 121 6164 : }