Coverage Report

Created: 2025-06-13 06:18

/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
0
{
39
0
    VSILFILE *file = nullptr;
40
0
    const char *mode_fopen = nullptr;
41
0
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
42
0
        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
0
    if ((filename != nullptr) && (mode_fopen != nullptr))
53
0
        file = VSIFOpenL(filename, mode_fopen);
54
0
    return file;
55
0
}
56
57
static uLong ZCALLBACK fread_file_func(voidpf /* opaque */, voidpf stream,
58
                                       void *buf, uLong size)
59
0
{
60
0
    uLong ret = static_cast<uLong>(VSIFReadL(buf, 1, static_cast<size_t>(size),
61
0
                                             static_cast<VSILFILE *>(stream)));
62
0
    return ret;
63
0
}
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
0
{
75
0
    uLong64 ret;
76
0
    ret = VSIFTellL(reinterpret_cast<VSILFILE *>(stream));
77
0
    return ret;
78
0
}
79
80
static long ZCALLBACK fseek_file_func(voidpf /* opaque */, voidpf stream,
81
                                      uLong64 offset, int origin)
82
0
{
83
0
    int fseek_origin = 0;
84
0
    switch (origin)
85
0
    {
86
0
        case ZLIB_FILEFUNC_SEEK_CUR:
87
0
            fseek_origin = SEEK_CUR;
88
0
            break;
89
0
        case ZLIB_FILEFUNC_SEEK_END:
90
0
            fseek_origin = SEEK_END;
91
0
            break;
92
0
        case ZLIB_FILEFUNC_SEEK_SET:
93
0
            fseek_origin = SEEK_SET;
94
0
            break;
95
0
        default:
96
0
            return -1;
97
0
    }
98
0
    return VSIFSeekL(reinterpret_cast<VSILFILE *>(stream), offset,
99
0
                     fseek_origin);
100
0
}
101
102
static int ZCALLBACK fclose_file_func(voidpf /* opaque */, voidpf stream)
103
0
{
104
0
    return VSIFCloseL(reinterpret_cast<VSILFILE *>(stream));
105
0
}
106
107
static int ZCALLBACK ferror_file_func(voidpf /* opaque */, voidpf /* stream */)
108
0
{
109
    // ret = ferror((FILE *)stream);
110
0
    return 0;
111
0
}
112
113
void cpl_fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
114
0
{
115
0
    pzlib_filefunc_def->zopen_file = fopen_file_func;
116
0
    pzlib_filefunc_def->zread_file = fread_file_func;
117
0
    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
118
0
    pzlib_filefunc_def->ztell_file = ftell_file_func;
119
0
    pzlib_filefunc_def->zseek_file = fseek_file_func;
120
0
    pzlib_filefunc_def->zclose_file = fclose_file_func;
121
0
    pzlib_filefunc_def->zerror_file = ferror_file_func;
122
0
    pzlib_filefunc_def->opaque = nullptr;
123
0
}