Coverage Report

Created: 2026-07-30 07:17

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-2025 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
0
ZIP_EXTERN zip_source_t *zip_source_filep(zip_t *za, FILE *file, zip_uint64_t start, zip_int64_t len) {
69
0
    if (za == NULL) {
70
0
        return NULL;
71
0
    }
72
73
0
    return zip_source_filep_create(file, start, len, &za->error);
74
0
}
75
76
77
0
ZIP_EXTERN zip_source_t *zip_source_filep_create(FILE *file, zip_uint64_t start, zip_int64_t length, zip_error_t *error) {
78
0
    if (file == NULL || length < ZIP_LENGTH_UNCHECKED) {
79
0
        zip_error_set(error, ZIP_ER_INVAL, 0);
80
0
        return NULL;
81
0
    }
82
83
0
    return zip_source_file_common_new(NULL, file, start, length, NULL, &ops_stdio_read, NULL, error);
84
0
}
85
86
87
1.16k
void _zip_stdio_op_close(zip_source_file_context_t *ctx) {
88
1.16k
    fclose((FILE *)ctx->f);
89
1.16k
}
90
91
92
5.18k
zip_int64_t _zip_stdio_op_read(zip_source_file_context_t *ctx, void *buf, zip_uint64_t len) {
93
5.18k
    size_t i;
94
#if SIZE_MAX < ZIP_UINT64_MAX
95
    if (len > SIZE_MAX) {
96
        len = SIZE_MAX;
97
    }
98
#endif
99
100
5.18k
    if ((i = fread(buf, 1, (size_t)len, ctx->f)) == 0) {
101
44
        if (ferror((FILE *)ctx->f)) {
102
0
            zip_error_set(&ctx->error, ZIP_ER_READ, errno);
103
0
            return -1;
104
0
        }
105
44
    }
106
107
5.18k
    return (zip_int64_t)i;
108
5.18k
}
109
110
111
7.08k
bool _zip_stdio_op_seek(zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence) {
112
#if ZIP_FSEEK_MAX > ZIP_INT64_MAX
113
    if (offset > ZIP_FSEEK_MAX || offset < ZIP_FSEEK_MIN) {
114
        zip_error_set(&ctx->error, ZIP_ER_SEEK, EOVERFLOW);
115
        return false;
116
    }
117
#endif
118
119
7.08k
    if (zip_os_fseek((FILE *)f, (zip_off_t)offset, whence) < 0) {
120
0
        zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
121
0
        return false;
122
0
    }
123
7.08k
    return true;
124
7.08k
}
125
126
127
1.16k
bool _zip_stdio_op_stat(zip_source_file_context_t *ctx, zip_source_file_stat_t *st) {
128
1.16k
    zip_os_stat_t sb;
129
130
1.16k
    int ret;
131
132
1.16k
    if (ctx->fname) {
133
1.16k
        ret = zip_os_stat(ctx->fname, &sb);
134
1.16k
    }
135
0
    else {
136
0
        ret = zip_os_fstat(fileno((FILE *)ctx->f), &sb);
137
0
    }
138
139
1.16k
    if (ret < 0) {
140
0
        if (errno == ENOENT) {
141
0
            st->exists = false;
142
0
            return true;
143
0
        }
144
0
        zip_error_set(&ctx->error, ZIP_ER_READ, errno);
145
0
        return false;
146
0
    }
147
148
1.16k
    st->size = (zip_uint64_t)sb.st_size;
149
1.16k
    st->mtime = sb.st_mtime;
150
151
1.16k
    st->regular_file = S_ISREG(sb.st_mode);
152
1.16k
    st->exists = true;
153
154
    /* We're using UNIX file API, even on Windows; thus, we supply external file attributes with Unix values. */
155
    /* TODO: This could be improved on Windows by providing Windows-specific file attributes */
156
1.16k
    ctx->attributes.valid = ZIP_FILE_ATTRIBUTES_HOST_SYSTEM | ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES;
157
1.16k
    ctx->attributes.host_system = ZIP_OPSYS_UNIX;
158
1.16k
    ctx->attributes.external_file_attributes = (((zip_uint32_t)sb.st_mode) << 16) | ((sb.st_mode & S_IWUSR) ? 0 : 1);
159
160
1.16k
    return true;
161
1.16k
}
162
163
164
0
zip_int64_t _zip_stdio_op_tell(zip_source_file_context_t *ctx, void *f) {
165
0
    zip_off_t offset = zip_os_ftell((FILE *)f);
166
167
0
    if (offset < 0) {
168
0
        zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
169
0
    }
170
171
0
    return offset;
172
0
}