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

Generated by: LCOV version 1.14