1 /* fmemopen implementation.
2 Copyright (C) 2015-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <https://www.gnu.org/licenses/>. */
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). */
29 #include <sys/types.h>
33 typedef struct fmemopen_cookie_struct fmemopen_cookie_t
;
34 struct fmemopen_cookie_struct
36 char *buffer
; /* memory buffer. */
37 int mybuffer
; /* allocated my buffer? */
38 int append
; /* buffer open for append? */
39 size_t size
; /* buffer length in bytes. */
40 off64_t pos
; /* current position at the buffer. */
41 size_t maxpos
; /* max position in buffer. */
46 fmemopen_read (void *cookie
, char *b
, size_t s
)
48 fmemopen_cookie_t
*c
= (fmemopen_cookie_t
*) cookie
;
50 if (c
->pos
+ s
> c
->maxpos
)
52 s
= c
->maxpos
- c
->pos
;
53 if ((size_t) c
->pos
> c
->maxpos
)
57 memcpy (b
, &(c
->buffer
[c
->pos
]), s
);
66 fmemopen_write (void *cookie
, const char *b
, size_t s
)
68 fmemopen_cookie_t
*c
= (fmemopen_cookie_t
*) cookie
;;
69 off64_t pos
= c
->append
? c
->maxpos
: c
->pos
;
70 int addnullc
= (s
== 0 || b
[s
- 1] != '\0');
72 if (pos
+ s
> c
->size
)
74 if ((size_t) (c
->pos
+ addnullc
) >= c
->size
)
82 memcpy (&(c
->buffer
[pos
]), b
, s
);
85 if ((size_t) c
->pos
> c
->maxpos
)
88 if (c
->maxpos
< c
->size
&& addnullc
)
89 c
->buffer
[c
->maxpos
] = '\0';
90 /* A null byte is written in a stream open for update iff it fits. */
91 else if (c
->append
== 0 && addnullc
!= 0)
92 c
->buffer
[c
->size
-1] = '\0';
100 fmemopen_seek (void *cookie
, off64_t
*p
, int w
)
103 fmemopen_cookie_t
*c
= (fmemopen_cookie_t
*) cookie
;
123 if (np
< 0 || (size_t) np
> c
->size
)
125 __set_errno (EINVAL
);
136 fmemopen_close (void *cookie
)
138 fmemopen_cookie_t
*c
= (fmemopen_cookie_t
*) cookie
;
149 __fmemopen (void *buf
, size_t len
, const char *mode
)
151 cookie_io_functions_t iof
;
152 fmemopen_cookie_t
*c
;
155 c
= (fmemopen_cookie_t
*) calloc (sizeof (fmemopen_cookie_t
), 1);
159 c
->mybuffer
= (buf
== NULL
);
163 c
->buffer
= (char *) malloc (len
);
164 if (c
->buffer
== NULL
)
173 if (__glibc_unlikely ((uintptr_t) len
> -(uintptr_t) buf
))
176 __set_errno (EINVAL
);
182 /* POSIX states that w+ mode should truncate the buffer. */
183 if (mode
[0] == 'w' && mode
[1] == '+')
187 c
->maxpos
= strnlen (c
->buffer
, len
);
191 /* Mode | starting position (cookie::pos) | size (cookie::size)
192 ------ |----------------------------------|-----------------------------
193 read | beginning of the buffer | size argument
194 write | beginning of the buffer | zero
195 append | first null or size buffer + 1 | first null or size argument
203 c
->append
= mode
[0] == 'a';
209 iof
.read
= fmemopen_read
;
210 iof
.write
= fmemopen_write
;
211 iof
.seek
= fmemopen_seek
;
212 iof
.close
= fmemopen_close
;
214 result
= _IO_fopencookie (c
, mode
, iof
);
215 if (__glibc_unlikely (result
== NULL
))
225 libc_hidden_def (__fmemopen
)
226 versioned_symbol (libc
, __fmemopen
, fmemopen
, GLIBC_2_22
);