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 5787 : static voidpf ZCALLBACK fopen_file_func(voidpf /* opaque */, 35 : const char *filename, int mode) 36 : { 37 5787 : VSILFILE *file = nullptr; 38 5787 : const char *mode_fopen = nullptr; 39 5787 : if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ) 40 5294 : mode_fopen = "rb"; 41 493 : else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 42 234 : mode_fopen = "r+b"; 43 259 : else if (mode & ZLIB_FILEFUNC_MODE_CREATE) 44 : { 45 259 : mode_fopen = "wb"; 46 259 : if (filename != nullptr) 47 259 : return VSIFOpenExL(filename, mode_fopen, true); 48 : } 49 : 50 5528 : if ((filename != nullptr) && (mode_fopen != nullptr)) 51 5528 : file = VSIFOpenL(filename, mode_fopen); 52 5528 : return file; 53 : } 54 : 55 2827120 : static uLong ZCALLBACK fread_file_func(voidpf /* opaque */, voidpf stream, 56 : void *buf, uLong size) 57 : { 58 2827120 : uLong ret = static_cast<uLong>(VSIFReadL(buf, 1, static_cast<size_t>(size), 59 : static_cast<VSILFILE *>(stream))); 60 2827120 : return ret; 61 : } 62 : 63 6731 : static uLong ZCALLBACK fwrite_file_func(voidpf /* opaque */, voidpf stream, 64 : const void *buf, uLong size) 65 : { 66 6731 : uLong ret = static_cast<uLong>(VSIFWriteL(buf, 1, static_cast<size_t>(size), 67 : static_cast<VSILFILE *>(stream))); 68 6731 : return ret; 69 : } 70 : 71 46522 : static uLong64 ZCALLBACK ftell_file_func(voidpf /* opaque */, voidpf stream) 72 : { 73 : uLong64 ret; 74 46522 : ret = VSIFTellL(reinterpret_cast<VSILFILE *>(stream)); 75 46522 : return ret; 76 : } 77 : 78 404647 : static long ZCALLBACK fseek_file_func(voidpf /* opaque */, voidpf stream, 79 : uLong64 offset, int origin) 80 : { 81 404647 : int fseek_origin = 0; 82 404647 : switch (origin) 83 : { 84 84907 : case ZLIB_FILEFUNC_SEEK_CUR: 85 84907 : fseek_origin = SEEK_CUR; 86 84907 : break; 87 11052 : case ZLIB_FILEFUNC_SEEK_END: 88 11052 : fseek_origin = SEEK_END; 89 11052 : break; 90 308688 : case ZLIB_FILEFUNC_SEEK_SET: 91 308688 : fseek_origin = SEEK_SET; 92 308688 : break; 93 0 : default: 94 0 : return -1; 95 : } 96 404647 : return VSIFSeekL(reinterpret_cast<VSILFILE *>(stream), offset, 97 404647 : fseek_origin); 98 : } 99 : 100 5783 : static int ZCALLBACK fclose_file_func(voidpf /* opaque */, voidpf stream) 101 : { 102 5783 : 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 5787 : void cpl_fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def) 112 : { 113 5787 : pzlib_filefunc_def->zopen_file = fopen_file_func; 114 5787 : pzlib_filefunc_def->zread_file = fread_file_func; 115 5787 : pzlib_filefunc_def->zwrite_file = fwrite_file_func; 116 5787 : pzlib_filefunc_def->ztell_file = ftell_file_func; 117 5787 : pzlib_filefunc_def->zseek_file = fseek_file_func; 118 5787 : pzlib_filefunc_def->zclose_file = fclose_file_func; 119 5787 : pzlib_filefunc_def->zerror_file = ferror_file_func; 120 5787 : pzlib_filefunc_def->opaque = nullptr; 121 5787 : }