blob: 3ab3e8dac48ad4bbb1fc4653793e422b808a2421 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* fmemopen implementation.
2 Copyright (C) 2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19/* fmemopen() from 2.22 and forward works as defined by POSIX. It also
20 provides an older symbol, version 2.2.5, that behaves different regarding
21 SEEK_END (libio/oldfmemopen.c). */
22
23
24#include <errno.h>
25#include <libio.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <stdint.h>
29#include <string.h>
30#include <sys/types.h>
31#include "libioP.h"
32
33
34typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
35struct fmemopen_cookie_struct
36{
37 char *buffer; /* memory buffer. */
38 int mybuffer; /* allocated my buffer? */
39 int append; /* buffer open for append? */
40 size_t size; /* buffer length in bytes. */
41 _IO_off64_t pos; /* current position at the buffer. */
42 size_t maxpos; /* max position in buffer. */
43};
44
45
46static ssize_t
47fmemopen_read (void *cookie, char *b, size_t s)
48{
49 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
50
51 if (c->pos + s > c->maxpos)
52 {
53 if ((size_t) c->pos == c->maxpos)
54 return 0;
55 s = c->size - c->pos;
56 }
57
58 memcpy (b, &(c->buffer[c->pos]), s);
59
60 c->pos += s;
61 if ((size_t) c->pos > c->maxpos)
62 c->maxpos = c->pos;
63
64 return s;
65}
66
67
68static ssize_t
69fmemopen_write (void *cookie, const char *b, size_t s)
70{
71 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;;
72 _IO_off64_t pos = c->append ? c->maxpos : c->pos;
73 int addnullc;
74
75 addnullc = (s == 0 || b[s - 1] != '\0');
76
77 if (pos + s + addnullc > c->size)
78 {
79 if ((size_t) (c->pos + addnullc) >= c->size)
80 {
81 __set_errno (ENOSPC);
82 return 0;
83 }
84 s = c->size - pos - addnullc;
85 }
86
87 memcpy (&(c->buffer[pos]), b, s);
88
89 pos += s;
90 if ((size_t) pos > c->maxpos)
91 {
92 c->maxpos = pos;
93 if (addnullc)
94 c->buffer[c->maxpos] = '\0';
95 }
96
97 return s;
98}
99
100
101static int
102fmemopen_seek (void *cookie, _IO_off64_t *p, int w)
103{
104 _IO_off64_t np;
105 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
106
107 switch (w)
108 {
109 case SEEK_SET:
110 np = *p;
111 break;
112
113 case SEEK_CUR:
114 np = c->pos + *p;
115 break;
116
117 case SEEK_END:
118 np = c->maxpos + *p;
119 break;
120
121 default:
122 return -1;
123 }
124
125 if (np < 0 || (size_t) np > c->size)
126 return -1;
127
128 *p = c->pos = np;
129
130 return 0;
131}
132
133
134static int
135fmemopen_close (void *cookie)
136{
137 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
138
139 if (c->mybuffer)
140 free (c->buffer);
141 free (c);
142
143 return 0;
144}
145
146
147FILE *
148__fmemopen (void *buf, size_t len, const char *mode)
149{
150 cookie_io_functions_t iof;
151 fmemopen_cookie_t *c;
152
153 c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1);
154 if (c == NULL)
155 return NULL;
156
157 c->mybuffer = (buf == NULL);
158
159 if (c->mybuffer)
160 {
161 c->buffer = (char *) malloc (len);
162 if (c->buffer == NULL)
163 {
164 free (c);
165 return NULL;
166 }
167 c->buffer[0] = '\0';
168 }
169 else
170 {
171 if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf))
172 {
173 free (c);
174 __set_errno (EINVAL);
175 return NULL;
176 }
177
178 c->buffer = buf;
179
180 /* POSIX states that w+ mode should truncate the buffer. */
181 if (mode[0] == 'w' && mode[1] == '+')
182 c->buffer[0] = '\0';
183
184 if (mode[0] == 'a')
185 c->maxpos = strnlen (c->buffer, len);
186 }
187
188
189 /* Mode | starting position (cookie::pos) | size (cookie::size)
190 ------ |----------------------------------|-----------------------------
191 read | beginning of the buffer | size argument
192 write | beginning of the buffer | zero
193 append | first null or size buffer + 1 | first null or size argument
194 */
195
196 c->size = len;
197
198 if (mode[0] == 'r')
199 c->maxpos = len;
200
201 c->append = mode[0] == 'a';
202 if (c->append)
203 c->pos = c->maxpos;
204 else
205 c->pos = 0;
206
207 iof.read = fmemopen_read;
208 iof.write = fmemopen_write;
209 iof.seek = fmemopen_seek;
210 iof.close = fmemopen_close;
211
212 return _IO_fopencookie (c, mode, iof);
213}
214libc_hidden_def (__fmemopen)
215versioned_symbol (libc, __fmemopen, fmemopen, GLIBC_2_22);