Coverage Report

Created: 2025-07-23 08:18

/src/libzip/lib/zip_source_file_stdio.c
Line
Count
Source (jump to first uncovered line)
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
0
_zip_stdio_op_close(zip_source_file_context_t *ctx) {
91
0
    fclose((FILE *)ctx->f);
92
0
}
93
94
95
zip_int64_t
96
0
_zip_stdio_op_read(zip_source_file_context_t *ctx, void *buf, zip_uint64_t len) {
97
0
    size_t i;
98
#if SIZE_MAX < ZIP_UINT64_MAX
99
    if (len > SIZE_MAX) {
100
        len = SIZE_MAX;
101
    }
102
#endif
103
104
0
    if ((i = fread(buf, 1, (size_t)len, ctx->f)) == 0) {
105
0
        if (ferror((FILE *)ctx->f)) {
106
0
            zip_error_set(&ctx->error, ZIP_ER_READ, errno);
107
0
            return -1;
108
0
        }
109
0
    }
110
111
0
    return (zip_int64_t)i;
112
0
}
113
114
115
bool
116
0
_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
0
    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
0
    return true;
129
0
}
130
131
132
bool
133
23
_zip_stdio_op_stat(zip_source_file_context_t *ctx, zip_source_file_stat_t *st) {
134
23
    zip_os_stat_t sb;
135
136
23
    int ret;
137
138
23
    if (ctx->fname) {
139
23
        ret = zip_os_stat(ctx->fname, &sb);
140
23
    }
141
0
    else {
142
0
        ret = zip_os_fstat(fileno((FILE *)ctx->f), &sb);
143
0
    }
144
145
23
    if (ret < 0) {
146
23
        if (errno == ENOENT) {
147
23
            st->exists = false;
148
23
            return true;
149
23
        }
150
0
        zip_error_set(&ctx->error, ZIP_ER_READ, errno);
151
0
        return false;
152
23
    }
153
154
0
    st->size = (zip_uint64_t)sb.st_size;
155
0
    st->mtime = sb.st_mtime;
156
157
0
    st->regular_file = S_ISREG(sb.st_mode);
158
0
    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
0
    ctx->attributes.valid = ZIP_FILE_ATTRIBUTES_HOST_SYSTEM | ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES;
163
0
    ctx->attributes.host_system = ZIP_OPSYS_UNIX;
164
0
    ctx->attributes.external_file_attributes = (((zip_uint32_t)sb.st_mode) << 16) | ((sb.st_mode & S_IWUSR) ? 0 : 1);
165
166
0
    return true;
167
23
}
168
169
170
zip_int64_t
171
0
_zip_stdio_op_tell(zip_source_file_context_t *ctx, void *f) {
172
0
    zip_off_t offset = zip_os_ftell((FILE *)f);
173
174
0
    if (offset < 0) {
175
0
        zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
176
0
    }
177
178
0
    return offset;
179
0
}