1 /* fmemopen implementation.
2 Copyright (C) 2015 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 <http://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). */
30 #include <sys/types.h>
34 typedef struct fmemopen_cookie_struct fmemopen_cookie_t
;
35 struct fmemopen_cookie_struct
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. */
47 fmemopen_read (void *cookie
, char *b
, size_t s
)
49 fmemopen_cookie_t
*c
= (fmemopen_cookie_t
*) cookie
;
51 if (c
->pos
+ s
> c
->maxpos
)
53 if ((size_t) c
->pos
== c
->maxpos
)
58 memcpy (b
, &(c
->buffer
[c
->pos
]), s
);
61 if ((size_t) c
->pos
> c
->maxpos
)
69 fmemopen_write (void *cookie
, const char *b
, size_t s
)
71 fmemopen_cookie_t
*c
= (fmemopen_cookie_t
*) cookie
;;
72 _IO_off64_t pos
= c
->append
? c
->maxpos
: c
->pos
;
75 addnullc
= (s
== 0 || b
[s
- 1] != '\0');
77 if (pos
+ s
+ addnullc
> c
->size
)
79 if ((size_t) (c
->pos
+ addnullc
) >= c
->size
)
84 s
= c
->size
- pos
- addnullc
;
87 memcpy (&(c
->buffer
[pos
]), b
, s
);
90 if ((size_t) pos
> c
->maxpos
)
94 c
->buffer
[c
->maxpos
] = '\0';
102 fmemopen_seek (void *cookie
, _IO_off64_t
*p
, int w
)
105 fmemopen_cookie_t
*c
= (fmemopen_cookie_t
*) cookie
;
125 if (np
< 0 || (size_t) np
> c
->size
)
135 fmemopen_close (void *cookie
)
137 fmemopen_cookie_t
*c
= (fmemopen_cookie_t
*) cookie
;
148 __fmemopen (void *buf
, size_t len
, const char *mode
)
150 cookie_io_functions_t iof
;
151 fmemopen_cookie_t
*c
;
154 c
= (fmemopen_cookie_t
*) calloc (sizeof (fmemopen_cookie_t
), 1);
158 c
->mybuffer
= (buf
== NULL
);
162 c
->buffer
= (char *) malloc (len
);
163 if (c
->buffer
== NULL
)
172 if (__glibc_unlikely ((uintptr_t) len
> -(uintptr_t) buf
))
175 __set_errno (EINVAL
);
181 /* POSIX states that w+ mode should truncate the buffer. */
182 if (mode
[0] == 'w' && mode
[1] == '+')
186 c
->maxpos
= strnlen (c
->buffer
, len
);
190 /* Mode | starting position (cookie::pos) | size (cookie::size)
191 ------ |----------------------------------|-----------------------------
192 read | beginning of the buffer | size argument
193 write | beginning of the buffer | zero
194 append | first null or size buffer + 1 | first null or size argument
202 c
->append
= mode
[0] == 'a';
208 iof
.read
= fmemopen_read
;
209 iof
.write
= fmemopen_write
;
210 iof
.seek
= fmemopen_seek
;
211 iof
.close
= fmemopen_close
;
213 result
= _IO_fopencookie (c
, mode
, iof
);
214 if (__glibc_unlikely (result
== NULL
))
224 libc_hidden_def (__fmemopen
)
225 versioned_symbol (libc
, __fmemopen
, fmemopen
, GLIBC_2_22
);