LCOV - code coverage report
Current view: top level - port - cpl_minizip_ioapi.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 51 53 96.2 %
Date: 2024-04-28 21:03:45 Functions: 8 8 100.0 %

          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             : #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        5078 : static voidpf ZCALLBACK fopen_file_func(voidpf /* opaque */,
      37             :                                         const char *filename, int mode)
      38             : {
      39        5078 :     VSILFILE *file = nullptr;
      40        5078 :     const char *mode_fopen = nullptr;
      41        5078 :     if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
      42        4648 :         mode_fopen = "rb";
      43         430 :     else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
      44         193 :         mode_fopen = "r+b";
      45         237 :     else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
      46             :     {
      47         237 :         mode_fopen = "wb";
      48         237 :         if (filename != nullptr)
      49         237 :             return VSIFOpenExL(filename, mode_fopen, true);
      50             :     }
      51             : 
      52        4841 :     if ((filename != nullptr) && (mode_fopen != nullptr))
      53        4841 :         file = VSIFOpenL(filename, mode_fopen);
      54        4841 :     return file;
      55             : }
      56             : 
      57     2456880 : static uLong ZCALLBACK fread_file_func(voidpf /* opaque */, voidpf stream,
      58             :                                        void *buf, uLong size)
      59             : {
      60     2456880 :     uLong ret = static_cast<uLong>(VSIFReadL(buf, 1, static_cast<size_t>(size),
      61             :                                              static_cast<VSILFILE *>(stream)));
      62     2456880 :     return ret;
      63             : }
      64             : 
      65        5762 : static uLong ZCALLBACK fwrite_file_func(voidpf /* opaque */, voidpf stream,
      66             :                                         const void *buf, uLong size)
      67             : {
      68        5762 :     uLong ret = static_cast<uLong>(VSIFWriteL(buf, 1, static_cast<size_t>(size),
      69             :                                               static_cast<VSILFILE *>(stream)));
      70        5762 :     return ret;
      71             : }
      72             : 
      73       40423 : static uLong64 ZCALLBACK ftell_file_func(voidpf /* opaque */, voidpf stream)
      74             : {
      75             :     uLong64 ret;
      76       40423 :     ret = VSIFTellL(reinterpret_cast<VSILFILE *>(stream));
      77       40423 :     return ret;
      78             : }
      79             : 
      80      357669 : static long ZCALLBACK fseek_file_func(voidpf /* opaque */, voidpf stream,
      81             :                                       uLong64 offset, int origin)
      82             : {
      83      357669 :     int fseek_origin = 0;
      84      357669 :     switch (origin)
      85             :     {
      86       73882 :         case ZLIB_FILEFUNC_SEEK_CUR:
      87       73882 :             fseek_origin = SEEK_CUR;
      88       73882 :             break;
      89        9678 :         case ZLIB_FILEFUNC_SEEK_END:
      90        9678 :             fseek_origin = SEEK_END;
      91        9678 :             break;
      92      274109 :         case ZLIB_FILEFUNC_SEEK_SET:
      93      274109 :             fseek_origin = SEEK_SET;
      94      274109 :             break;
      95           0 :         default:
      96           0 :             return -1;
      97             :     }
      98      357669 :     return VSIFSeekL(reinterpret_cast<VSILFILE *>(stream), offset,
      99      357669 :                      fseek_origin);
     100             : }
     101             : 
     102        5074 : static int ZCALLBACK fclose_file_func(voidpf /* opaque */, voidpf stream)
     103             : {
     104        5074 :     return VSIFCloseL(reinterpret_cast<VSILFILE *>(stream));
     105             : }
     106             : 
     107          44 : static int ZCALLBACK ferror_file_func(voidpf /* opaque */, voidpf /* stream */)
     108             : {
     109             :     // ret = ferror((FILE *)stream);
     110          44 :     return 0;
     111             : }
     112             : 
     113        5078 : void cpl_fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
     114             : {
     115        5078 :     pzlib_filefunc_def->zopen_file = fopen_file_func;
     116        5078 :     pzlib_filefunc_def->zread_file = fread_file_func;
     117        5078 :     pzlib_filefunc_def->zwrite_file = fwrite_file_func;
     118        5078 :     pzlib_filefunc_def->ztell_file = ftell_file_func;
     119        5078 :     pzlib_filefunc_def->zseek_file = fseek_file_func;
     120        5078 :     pzlib_filefunc_def->zclose_file = fclose_file_func;
     121        5078 :     pzlib_filefunc_def->zerror_file = ferror_file_func;
     122        5078 :     pzlib_filefunc_def->opaque = nullptr;
     123        5078 : }

Generated by: LCOV version 1.14