2 * Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
37 struct fmemopen_cookie
39 char *buf; /* pointer to the memory region */
40 bool own; /* did we allocate the buffer ourselves? */
41 char bin; /* is this a binary buffer? */
42 size_t size; /* buffer length in bytes */
43 size_t len; /* data length in bytes */
44 size_t off; /* current offset into the buffer */
47 static int fmemopen_read(void *cookie, char *buf, int nbytes);
48 static int fmemopen_write(void *cookie, const char *buf, int nbytes);
49 static fpos_t fmemopen_seek(void *cookie, fpos_t offset, int whence);
50 static int fmemopen_close(void *cookie);
53 fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
55 struct fmemopen_cookie *ck;
60 * Retrieve the flags as used by open(2) from the mode argument, and
63 rc = __sflags(mode, &flags);
70 * There's no point in requiring an automatically allocated buffer
73 if (!(flags & O_RDWR) && buf == NULL) {
78 ck = malloc(sizeof(struct fmemopen_cookie));
86 /* Check whether we have to allocate the buffer ourselves. */
87 ck->own = ((ck->buf = buf) == NULL);
89 ck->buf = malloc(size);
90 if (ck->buf == NULL) {
97 * POSIX distinguishes between w+ and r+, in that w+ is supposed to
98 * truncate the buffer.
100 if (ck->own || mode[0] == 'w') {
104 /* Check for binary mode. */
105 ck->bin = strchr(mode, 'b') != NULL;
108 * The size of the current buffer contents is set depending on the
111 * for append (text-mode), the position of the first NULL byte, or the
112 * size of the buffer if none is found
114 * for append (binary-mode), the size of the buffer
116 * for read, the size of the buffer
124 * This isn't useful, since the buffer isn't allowed
127 ck->off = ck->len = size;
129 ck->off = ck->len = strnlen(ck->buf, ck->size);
140 flags & O_WRONLY ? NULL : fmemopen_read,
141 flags & O_RDONLY ? NULL : fmemopen_write,
142 fmemopen_seek, fmemopen_close);
152 * Turn off buffering, so a write past the end of the buffer
153 * correctly returns a short object count.
155 setvbuf(f, NULL, _IONBF, 0);
161 fmemopen_read(void *cookie, char *buf, int nbytes)
163 struct fmemopen_cookie *ck = cookie;
165 if (nbytes > ck->len - ck->off)
166 nbytes = ck->len - ck->off;
171 memcpy(buf, ck->buf + ck->off, nbytes);
179 fmemopen_write(void *cookie, const char *buf, int nbytes)
181 struct fmemopen_cookie *ck = cookie;
183 if (nbytes > ck->size - ck->off)
184 nbytes = ck->size - ck->off;
189 memcpy(ck->buf + ck->off, buf, nbytes);
193 if (ck->off > ck->len)
197 * We append a NULL byte if all these conditions are met:
198 * - the buffer is not binary
199 * - the buffer is not full
200 * - the data just written doesn't already end with a NULL byte
202 if (!ck->bin && ck->off < ck->size && ck->buf[ck->off - 1] != '\0')
203 ck->buf[ck->off] = '\0';
209 fmemopen_seek(void *cookie, fpos_t offset, int whence)
211 struct fmemopen_cookie *ck = cookie;
216 if (offset > ck->size) {
224 if (ck->off + offset > ck->size) {
232 if (offset > 0 || -offset > ck->len) {
236 ck->off = ck->len + offset;
248 fmemopen_close(void *cookie)
250 struct fmemopen_cookie *ck = cookie;