Coverage Report

Created: 2026-12-01 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/src/text/memstream.c
Line
Count
Source
1
/*****************************************************************************
2
 * memstream.c: heap-based output streams
3
 *****************************************************************************
4
 * Copyright (C) 2016 Rémi Denis-Courmont
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19
 *****************************************************************************/
20
21
#ifdef HAVE_CONFIG_H
22
# include "config.h"
23
#endif
24
25
#include <stdckdint.h>
26
27
#include <vlc_common.h>
28
#include <vlc_memstream.h>
29
30
#ifdef HAVE_OPEN_MEMSTREAM
31
int vlc_memstream_open(struct vlc_memstream *ms)
32
2.13M
{
33
2.13M
    ms->stream = open_memstream(&ms->ptr, &ms->length);
34
2.13M
    return likely(ms->stream != NULL) ? 0 : EOF;
35
2.13M
}
36
37
int vlc_memstream_flush(struct vlc_memstream *ms)
38
0
{
39
0
    if (unlikely(ms->stream == NULL))
40
0
        return EOF;
41
42
0
    if (ferror(ms->stream))
43
0
        return EOF;
44
45
0
    return fflush(ms->stream);
46
0
}
47
48
int vlc_memstream_close(struct vlc_memstream *ms)
49
2.13M
{
50
2.13M
    FILE *stream = ms->stream;
51
2.13M
    int ret;
52
53
2.13M
    if (unlikely(stream == NULL))
54
0
    {
55
        // was never properly opened
56
0
        ms->ptr = NULL;
57
0
        return EOF;
58
0
    }
59
60
2.13M
    ms->stream = NULL;
61
2.13M
    ret = ferror(stream);
62
63
2.13M
    if (fclose(stream))
64
0
    {
65
        // assuming it's free'd by the memstream
66
0
        ms->ptr = NULL;
67
0
        return EOF;
68
0
    }
69
70
2.13M
    if (unlikely(ret))
71
0
    {
72
0
        free(ms->ptr);
73
0
        ms->ptr = NULL;
74
0
        return EOF;
75
0
    }
76
2.13M
    return 0;
77
2.13M
} 
78
79
size_t vlc_memstream_write(struct vlc_memstream *ms, const void *ptr,
80
                           size_t len)
81
59.3M
{
82
59.3M
    if (unlikely(ms->stream == NULL))
83
0
        return 0;
84
85
59.3M
    if (len == 0)
86
489k
        return 0;
87
88
58.8M
    return fwrite(ptr, 1, len, ms->stream);
89
59.3M
}
90
91
int vlc_memstream_putc(struct vlc_memstream *ms, int c)
92
1.72M
{
93
1.72M
    if (unlikely(ms->stream == NULL))
94
0
        return EOF;
95
96
1.72M
    return fputc(c, ms->stream);
97
1.72M
}
98
99
int (vlc_memstream_puts)(struct vlc_memstream *ms, const char *str)
100
1.35M
{
101
1.35M
    if (unlikely(ms->stream == NULL))
102
0
        return EOF;
103
104
1.35M
    return fputs(str, ms->stream);
105
1.35M
}
106
107
int vlc_memstream_vprintf(struct vlc_memstream *ms, const char *fmt,
108
                          va_list args)
109
639k
{
110
639k
    if (unlikely(ms->stream == NULL))
111
0
        return EOF;
112
113
639k
    return vfprintf(ms->stream, fmt, args);
114
639k
}
115
116
#else
117
#include <stdlib.h>
118
119
int vlc_memstream_open(struct vlc_memstream *ms)
120
{
121
    ms->ptr = calloc(1, 1);
122
    if (unlikely(ms->ptr == NULL))
123
    {
124
        ms->length = SIZE_MAX;
125
        return EOF;
126
    }
127
    ms->length = 0;
128
    return 0;
129
}
130
131
int vlc_memstream_flush(struct vlc_memstream *ms)
132
{
133
    return ms->length == SIZE_MAX ? EOF : 0;
134
}
135
136
int vlc_memstream_close(struct vlc_memstream *ms)
137
{
138
    if (ms->length == SIZE_MAX)
139
    {
140
        free(ms->ptr);
141
        ms->ptr = NULL;
142
        return EOF;
143
    }
144
    return 0;
145
}
146
147
size_t vlc_memstream_write(struct vlc_memstream *ms, const void *ptr,
148
                           size_t len)
149
{
150
    size_t newlen;
151
152
    if (len == 0)
153
        return 0;
154
155
    if (unlikely(ckd_add(&newlen, ms->length, len))
156
     || unlikely(ckd_add(&newlen, newlen, 1)))
157
        goto error;
158
159
    char *base = realloc(ms->ptr, newlen);
160
    if (unlikely(base == NULL))
161
        goto error;
162
163
    memcpy(base + ms->length, ptr, len);
164
    ms->ptr = base;
165
    ms->length += len;
166
    base[ms->length] = '\0';
167
    return len;
168
169
error:
170
    ms->length = SIZE_MAX;
171
    return 0;
172
}
173
174
int vlc_memstream_putc(struct vlc_memstream *ms, int c)
175
{
176
    return (vlc_memstream_write(ms, &(unsigned char){ c }, 1u) == 1) ? c : EOF;
177
}
178
179
int (vlc_memstream_puts)(struct vlc_memstream *ms, const char *str)
180
{
181
    size_t len = strlen(str);
182
    return (vlc_memstream_write(ms, str, len) == len) ? 0 : EOF;
183
}
184
185
int vlc_memstream_vprintf(struct vlc_memstream *ms, const char *fmt,
186
                          va_list args)
187
{
188
    va_list ap;
189
    char *ptr;
190
    int len;
191
    size_t newlen;
192
193
    va_copy(ap, args);
194
    len = vsnprintf(NULL, 0, fmt, ap);
195
    va_end(ap);
196
197
    if (len < 0
198
     || unlikely(ckd_add(&newlen, ms->length, len))
199
     || unlikely(ckd_add(&newlen, newlen, 1)))
200
        goto error;
201
202
    ptr = realloc(ms->ptr, newlen);
203
    if (ptr == NULL)
204
        goto error;
205
206
    vsnprintf(ptr + ms->length, len + 1, fmt, args);
207
    ms->ptr = ptr;
208
    ms->length += len;
209
    return len;
210
211
error:
212
    ms->length = SIZE_MAX;
213
    return EOF;
214
}
215
#endif
216
217
int vlc_memstream_printf(struct vlc_memstream *ms, const char *fmt, ...)
218
639k
{
219
639k
    va_list ap;
220
639k
    int ret;
221
222
639k
    va_start(ap, fmt);
223
639k
    ret = vlc_memstream_vprintf(ms, fmt, ap);
224
    va_end(ap);
225
639k
    return ret;
226
639k
}