Coverage Report

Created: 2025-09-27 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_source_file_stdio.c
Line
Count
Source
1
/*
2
  zip_source_file_stdio.c -- read-only stdio file source implementation
3
  Copyright (C) 2020-2023 Dieter Baron and Thomas Klausner
4
5
  This file is part of libzip, a library to manipulate ZIP archives.
6
  The authors can be contacted at <info@libzip.org>
7
8
  Redistribution and use in source and binary forms, with or without
9
  modification, are permitted provided that the following conditions
10
  are met:
11
  1. Redistributions of source code must retain the above copyright
12
     notice, this list of conditions and the following disclaimer.
13
  2. Redistributions in binary form must reproduce the above copyright
14
     notice, this list of conditions and the following disclaimer in
15
     the documentation and/or other materials provided with the
16
     distribution.
17
  3. The names of the authors may not be used to endorse or promote
18
     products derived from this software without specific prior
19
     written permission.
20
21
  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22
  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29
  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31
  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
#include "zipint.h"
35
36
#include "zip_source_file.h"
37
#include "zip_source_file_stdio.h"
38
39
#include <fcntl.h>
40
#include <stdio.h>
41
#include <stdlib.h>
42
43
#ifdef _WIN32
44
#ifndef S_IWUSR
45
#define S_IWUSR _S_IWRITE
46
#endif
47
#endif
48
49
/* clang-format off */
50
static zip_source_file_operations_t ops_stdio_read = {
51
    _zip_stdio_op_close,
52
    NULL,
53
    NULL,
54
    NULL,
55
    NULL,
56
    _zip_stdio_op_read,
57
    NULL,
58
    NULL,
59
    _zip_stdio_op_seek,
60
    _zip_stdio_op_stat,
61
    NULL,
62
    _zip_stdio_op_tell,
63
    NULL
64
};
65
/* clang-format on */
66
67
68
ZIP_EXTERN zip_source_t *
69
0
zip_source_filep(zip_t *za, FILE *file, zip_uint64_t start, zip_int64_t len) {
70
0
    if (za == NULL) {
71
0
        return NULL;
72
0
    }
73
74
0
    return zip_source_filep_create(file, start, len, &za->error);
75
0
}
76
77
78
ZIP_EXTERN zip_source_t *
79
0
zip_source_filep_create(FILE *file, zip_uint64_t start, zip_int64_t length, zip_error_t *error) {
80
0
    if (file == NULL || length < ZIP_LENGTH_UNCHECKED) {
81
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
82
0
        return NULL;
83
0
    }
84
85
0
    return zip_source_file_common_new(NULL, file, start, length, NULL, &ops_stdio_read, NULL, error);
86
0
}
87
88
89
void
90
4.56k
_zip_stdio_op_close(zip_source_file_context_t *ctx) {
91
4.56k
    fclose((FILE *)ctx->f);
92
4.56k
}
93
94
95
zip_int64_t
96
100k
_zip_stdio_op_read(zip_source_file_context_t *ctx, void *buf, zip_uint64_t len) {
97
100k
    size_t i;
98
#if SIZE_MAX < ZIP_UINT64_MAX
99
    if (len > SIZE_MAX) {
100
        len = SIZE_MAX;
101
    }
102
#endif
103
104
100k
    if ((i = fread(buf, 1, (size_t)len, ctx->f)) == 0) {
105
7.26k
        if (ferror((FILE *)ctx->f)) {
106
0
            zip_error_set(&ctx->error, ZIP_ER_READ, errno);
107
0
            return -1;
108
0
        }
109
7.26k
    }
110
111
100k
    return (zip_int64_t)i;
112
100k
}
113
114
115
bool
116
63.6k
_zip_stdio_op_seek(zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence) {
117
#if ZIP_FSEEK_MAX > ZIP_INT64_MAX
118
    if (offset > ZIP_FSEEK_MAX || offset < ZIP_FSEEK_MIN) {
119
        zip_error_set(&ctx->error, ZIP_ER_SEEK, EOVERFLOW);
120
        return false;
121
    }
122
#endif
123
124
63.6k
    if (zip_os_fseek((FILE *)f, (zip_off_t)offset, whence) < 0) {
125
0
        zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
126
0
        return false;
127
0
    }
128
63.6k
    return true;
129
63.6k
}
130
131
132
bool
133
5.53k
_zip_stdio_op_stat(zip_source_file_context_t *ctx, zip_source_file_stat_t *st) {
134
5.53k
    zip_os_stat_t sb;
135
136
5.53k
    int ret;
137
138
5.53k
    if (ctx->fname) {
139
5.53k
        ret = zip_os_stat(ctx->fname, &sb);
140
5.53k
    }
141
0
    else {
142
0
        ret = zip_os_fstat(fileno((FILE *)ctx->f), &sb);
143
0
    }
144
145
5.53k
    if (ret < 0) {
146
963
        if (errno == ENOENT) {
147
963
            st->exists = false;
148
963
            return true;
149
963
        }
150
0
        zip_error_set(&ctx->error, ZIP_ER_READ, errno);
151
0
        return false;
152
963
    }
153
154
4.56k
    st->size = (zip_uint64_t)sb.st_size;
155
4.56k
    st->mtime = sb.st_mtime;
156
157
4.56k
    st->regular_file = S_ISREG(sb.st_mode);
158
4.56k
    st->exists = true;
159
160
    /* We're using UNIX file API, even on Windows; thus, we supply external file attributes with Unix values. */
161
    /* TODO: This could be improved on Windows by providing Windows-specific file attributes */
162
4.56k
    ctx->attributes.valid = ZIP_FILE_ATTRIBUTES_HOST_SYSTEM | ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES;
163
4.56k
    ctx->attributes.host_system = ZIP_OPSYS_UNIX;
164
4.56k
    ctx->attributes.external_file_attributes = (((zip_uint32_t)sb.st_mode) << 16) | ((sb.st_mode & S_IWUSR) ? 0 : 1);
165
166
4.56k
    return true;
167
5.53k
}
168
169
170
zip_int64_t
171
7.70k
_zip_stdio_op_tell(zip_source_file_context_t *ctx, void *f) {
172
7.70k
    zip_off_t offset = zip_os_ftell((FILE *)f);
173
174
7.70k
    if (offset < 0) {
175
0
        zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
176
0
    }
177
178
7.70k
    return offset;
179
7.70k
}