Coverage Report

Created: 2025-08-03 07:00

/src/libzip/lib/zip_io_util.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 zip_io_util.c -- I/O helper functions
3
 Copyright (C) 1999-2024 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 <limits.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <zlib.h>
38
39
#include "zipint.h"
40
41
int
42
127k
_zip_read(zip_source_t *src, zip_uint8_t *b, zip_uint64_t length, zip_error_t *error) {
43
127k
    zip_int64_t n;
44
45
127k
    if (length > ZIP_INT64_MAX) {
46
0
        zip_error_set(error, ZIP_ER_INTERNAL, 0);
47
0
        return -1;
48
0
    }
49
50
127k
    if ((n = zip_source_read(src, b, length)) < 0) {
51
0
        zip_error_set_from_source(error, src);
52
0
        return -1;
53
0
    }
54
55
127k
    if (n < (zip_int64_t)length) {
56
384
        zip_error_set(error, ZIP_ER_EOF, 0);
57
384
        return -1;
58
384
    }
59
60
127k
    return 0;
61
127k
}
62
63
64
zip_uint8_t *
65
63.2k
_zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error) {
66
63.2k
    zip_uint8_t *r;
67
68
63.2k
    if (length == 0 && !nulp) {
69
0
        return NULL;
70
0
    }
71
72
63.2k
    r = (zip_uint8_t *)malloc(length + (nulp ? 1 : 0));
73
63.2k
    if (r == NULL) {
74
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
75
0
        return NULL;
76
0
    }
77
78
63.2k
    if (buffer) {
79
63.2k
        zip_uint8_t *data = _zip_buffer_get(buffer, length);
80
81
63.2k
        if (data == NULL) {
82
0
            zip_error_set(error, ZIP_ER_MEMORY, 0);
83
0
            free(r);
84
0
            return NULL;
85
0
        }
86
63.2k
        (void)memcpy_s(r, length, data, length);
87
63.2k
    }
88
0
    else {
89
0
        if (_zip_read(src, r, length, error) < 0) {
90
0
            free(r);
91
0
            return NULL;
92
0
        }
93
0
    }
94
95
63.2k
    if (nulp) {
96
24.1k
        zip_uint8_t *o;
97
        /* replace any in-string NUL characters with spaces */
98
24.1k
        r[length] = 0;
99
3.28M
        for (o = r; o < r + length; o++)
100
3.26M
            if (*o == '\0')
101
1.35M
                *o = ' ';
102
24.1k
    }
103
104
63.2k
    return r;
105
63.2k
}
106
107
108
zip_string_t *
109
29.7k
_zip_read_string(zip_buffer_t *buffer, zip_source_t *src, zip_uint16_t len, bool nulp, zip_error_t *error) {
110
29.7k
    zip_uint8_t *raw;
111
29.7k
    zip_string_t *s;
112
113
29.7k
    if ((raw = _zip_read_data(buffer, src, len, nulp, error)) == NULL)
114
0
        return NULL;
115
116
29.7k
    s = _zip_string_new(raw, len, ZIP_FL_ENC_GUESS, error);
117
29.7k
    free(raw);
118
29.7k
    return s;
119
29.7k
}
120
121
122
int
123
28.4k
_zip_write(zip_t *za, const void *data, zip_uint64_t length) {
124
28.4k
    zip_int64_t n;
125
126
28.4k
    if ((n = zip_source_write(za->src, data, length)) < 0) {
127
0
        zip_error_set_from_source(&za->error, za->src);
128
0
        return -1;
129
0
    }
130
28.4k
    if ((zip_uint64_t)n != length) {
131
0
        zip_error_set(&za->error, ZIP_ER_WRITE, EINTR);
132
0
        return -1;
133
0
    }
134
135
28.4k
    if (za->write_crc != NULL) {
136
0
        zip_uint64_t position = 0;
137
0
        while (position < length) {
138
0
            zip_uint64_t nn = ZIP_MIN(UINT_MAX, length - position);
139
140
0
            *za->write_crc = (zip_uint32_t)crc32(*za->write_crc, (const Bytef *)data + position, (uInt)nn);
141
0
            position += nn;
142
0
        }
143
0
    }
144
145
28.4k
    return 0;
146
28.4k
}