1*f14fb602SLionel Sambuc /* $NetBSD: fmemopen.c,v 1.8 2012/03/29 14:27:33 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c)2007, 2010 Takehiko NOZAKI,
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras *
162fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262fe8fb19SBen Gras * SUCH DAMAGE.
272fe8fb19SBen Gras *
282fe8fb19SBen Gras */
292fe8fb19SBen Gras
302fe8fb19SBen Gras #include <sys/cdefs.h>
312fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
32*f14fb602SLionel Sambuc __RCSID("$NetBSD: fmemopen.c,v 1.8 2012/03/29 14:27:33 christos Exp $");
332fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
342fe8fb19SBen Gras
352fe8fb19SBen Gras #include <assert.h>
362fe8fb19SBen Gras #include <errno.h>
372fe8fb19SBen Gras #include <fcntl.h>
382fe8fb19SBen Gras #include <stddef.h>
392fe8fb19SBen Gras #include <stdio.h>
402fe8fb19SBen Gras #include <stdlib.h>
412fe8fb19SBen Gras
422fe8fb19SBen Gras #include "reentrant.h"
432fe8fb19SBen Gras #include "local.h"
442fe8fb19SBen Gras
452fe8fb19SBen Gras struct fmemopen_cookie {
462fe8fb19SBen Gras char *head, *tail, *cur, *eob;
472fe8fb19SBen Gras };
482fe8fb19SBen Gras
49*f14fb602SLionel Sambuc static ssize_t
fmemopen_read(void * cookie,void * buf,size_t nbytes)50*f14fb602SLionel Sambuc fmemopen_read(void *cookie, void *buf, size_t nbytes)
512fe8fb19SBen Gras {
522fe8fb19SBen Gras struct fmemopen_cookie *p;
53*f14fb602SLionel Sambuc char *s, *b = buf;
542fe8fb19SBen Gras
552fe8fb19SBen Gras _DIAGASSERT(cookie != NULL);
562fe8fb19SBen Gras _DIAGASSERT(buf != NULL && nbytes > 0);
572fe8fb19SBen Gras
582fe8fb19SBen Gras p = (struct fmemopen_cookie *)cookie;
592fe8fb19SBen Gras s = p->cur;
602fe8fb19SBen Gras do {
612fe8fb19SBen Gras if (p->cur == p->tail)
622fe8fb19SBen Gras break;
63*f14fb602SLionel Sambuc *b++ = *p->cur++;
642fe8fb19SBen Gras } while (--nbytes > 0);
652fe8fb19SBen Gras
66*f14fb602SLionel Sambuc return (ssize_t)(p->cur - s);
672fe8fb19SBen Gras }
682fe8fb19SBen Gras
69*f14fb602SLionel Sambuc static ssize_t
fmemopen_write(void * cookie,const void * buf,size_t nbytes)70*f14fb602SLionel Sambuc fmemopen_write(void *cookie, const void *buf, size_t nbytes)
712fe8fb19SBen Gras {
722fe8fb19SBen Gras struct fmemopen_cookie *p;
732fe8fb19SBen Gras char *s;
74*f14fb602SLionel Sambuc const char *b = buf;
752fe8fb19SBen Gras
762fe8fb19SBen Gras _DIAGASSERT(cookie != NULL);
772fe8fb19SBen Gras _DIAGASSERT(buf != NULL && nbytes > 0);
782fe8fb19SBen Gras
792fe8fb19SBen Gras p = (struct fmemopen_cookie *)cookie;
802fe8fb19SBen Gras if (p->cur >= p->tail)
812fe8fb19SBen Gras return 0;
822fe8fb19SBen Gras s = p->cur;
832fe8fb19SBen Gras do {
842fe8fb19SBen Gras if (p->cur == p->tail - 1) {
85*f14fb602SLionel Sambuc if (*b == '\0') {
862fe8fb19SBen Gras *p->cur++ = '\0';
872fe8fb19SBen Gras goto ok;
882fe8fb19SBen Gras }
892fe8fb19SBen Gras break;
902fe8fb19SBen Gras }
91*f14fb602SLionel Sambuc *p->cur++ = *b++;
922fe8fb19SBen Gras } while (--nbytes > 0);
932fe8fb19SBen Gras *p->cur = '\0';
942fe8fb19SBen Gras ok:
952fe8fb19SBen Gras if (p->cur > p->eob)
962fe8fb19SBen Gras p->eob = p->cur;
972fe8fb19SBen Gras
98*f14fb602SLionel Sambuc return (ssize_t)(p->cur - s);
992fe8fb19SBen Gras }
1002fe8fb19SBen Gras
101*f14fb602SLionel Sambuc #ifdef notyet
102*f14fb602SLionel Sambuc static int
fmemopen_flush(void * cookie)103*f14fb602SLionel Sambuc fmemopen_flush(void *cookie)
104*f14fb602SLionel Sambuc {
105*f14fb602SLionel Sambuc struct fmemopen_cookie *p;
106*f14fb602SLionel Sambuc
107*f14fb602SLionel Sambuc _DIAGASSERT(cookie != NULL);
108*f14fb602SLionel Sambuc
109*f14fb602SLionel Sambuc p = (struct fmemopen_cookie *)cookie;
110*f14fb602SLionel Sambuc if (p->cur >= p->tail)
111*f14fb602SLionel Sambuc return -1;
112*f14fb602SLionel Sambuc *p->cur = '\0';
113*f14fb602SLionel Sambuc return 0;
114*f14fb602SLionel Sambuc }
115*f14fb602SLionel Sambuc #endif
116*f14fb602SLionel Sambuc
117*f14fb602SLionel Sambuc static off_t
fmemopen_seek(void * cookie,off_t offset,int whence)118*f14fb602SLionel Sambuc fmemopen_seek(void *cookie, off_t offset, int whence)
1192fe8fb19SBen Gras {
1202fe8fb19SBen Gras struct fmemopen_cookie *p;
1212fe8fb19SBen Gras
1222fe8fb19SBen Gras _DIAGASSERT(cookie != NULL);
1232fe8fb19SBen Gras
1242fe8fb19SBen Gras p = (struct fmemopen_cookie *)cookie;
1252fe8fb19SBen Gras switch (whence) {
1262fe8fb19SBen Gras case SEEK_SET:
1272fe8fb19SBen Gras break;
1282fe8fb19SBen Gras case SEEK_CUR:
1292fe8fb19SBen Gras offset += p->cur - p->head;
1302fe8fb19SBen Gras break;
1312fe8fb19SBen Gras case SEEK_END:
1322fe8fb19SBen Gras offset += p->eob - p->head;
1332fe8fb19SBen Gras break;
1342fe8fb19SBen Gras default:
1352fe8fb19SBen Gras errno = EINVAL;
1362fe8fb19SBen Gras goto error;
1372fe8fb19SBen Gras }
138*f14fb602SLionel Sambuc if (offset >= (off_t)0 && offset <= p->tail - p->head) {
1392fe8fb19SBen Gras p->cur = p->head + (ptrdiff_t)offset;
140*f14fb602SLionel Sambuc return (off_t)(p->cur - p->head);
1412fe8fb19SBen Gras }
1422fe8fb19SBen Gras error:
143*f14fb602SLionel Sambuc return (off_t)-1;
1442fe8fb19SBen Gras }
1452fe8fb19SBen Gras
1462fe8fb19SBen Gras static int
fmemopen_close0(void * cookie)1472fe8fb19SBen Gras fmemopen_close0(void *cookie)
1482fe8fb19SBen Gras {
1492fe8fb19SBen Gras _DIAGASSERT(cookie != NULL);
1502fe8fb19SBen Gras
1512fe8fb19SBen Gras free(cookie);
1522fe8fb19SBen Gras
1532fe8fb19SBen Gras return 0;
1542fe8fb19SBen Gras }
1552fe8fb19SBen Gras
1562fe8fb19SBen Gras static int
fmemopen_close1(void * cookie)1572fe8fb19SBen Gras fmemopen_close1(void *cookie)
1582fe8fb19SBen Gras {
1592fe8fb19SBen Gras struct fmemopen_cookie *p;
1602fe8fb19SBen Gras
1612fe8fb19SBen Gras _DIAGASSERT(cookie != NULL);
1622fe8fb19SBen Gras
1632fe8fb19SBen Gras p = (struct fmemopen_cookie *)cookie;
1642fe8fb19SBen Gras free(p->head);
1652fe8fb19SBen Gras free(p);
1662fe8fb19SBen Gras
1672fe8fb19SBen Gras return 0;
1682fe8fb19SBen Gras }
1692fe8fb19SBen Gras
1702fe8fb19SBen Gras
1712fe8fb19SBen Gras FILE *
fmemopen(void * __restrict buf,size_t size,const char * __restrict mode)1722fe8fb19SBen Gras fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
1732fe8fb19SBen Gras {
1742fe8fb19SBen Gras int flags, oflags;
1752fe8fb19SBen Gras FILE *fp;
1762fe8fb19SBen Gras struct fmemopen_cookie *cookie;
1772fe8fb19SBen Gras
1782fe8fb19SBen Gras if (size < (size_t)1)
1792fe8fb19SBen Gras goto invalid;
1802fe8fb19SBen Gras
1812fe8fb19SBen Gras flags = __sflags(mode, &oflags);
1822fe8fb19SBen Gras if (flags == 0)
1832fe8fb19SBen Gras return NULL;
1842fe8fb19SBen Gras
1852fe8fb19SBen Gras if ((oflags & O_RDWR) == 0 && buf == NULL)
1862fe8fb19SBen Gras goto invalid;
1872fe8fb19SBen Gras
1882fe8fb19SBen Gras fp = __sfp();
1892fe8fb19SBen Gras if (fp == NULL)
1902fe8fb19SBen Gras return NULL;
1912fe8fb19SBen Gras fp->_file = -1;
1922fe8fb19SBen Gras
1932fe8fb19SBen Gras cookie = malloc(sizeof(*cookie));
1942fe8fb19SBen Gras if (cookie == NULL)
1952fe8fb19SBen Gras goto release;
1962fe8fb19SBen Gras
1972fe8fb19SBen Gras if (buf == NULL) {
1982fe8fb19SBen Gras cookie->head = malloc(size);
1992fe8fb19SBen Gras if (cookie->head == NULL) {
2002fe8fb19SBen Gras free(cookie);
2012fe8fb19SBen Gras goto release;
2022fe8fb19SBen Gras }
2032fe8fb19SBen Gras *cookie->head = '\0';
204*f14fb602SLionel Sambuc fp->_close = fmemopen_close1;
2052fe8fb19SBen Gras } else {
2062fe8fb19SBen Gras cookie->head = (char *)buf;
2072fe8fb19SBen Gras if (oflags & O_TRUNC)
2082fe8fb19SBen Gras *cookie->head = '\0';
209*f14fb602SLionel Sambuc fp->_close = fmemopen_close0;
2102fe8fb19SBen Gras }
2112fe8fb19SBen Gras
2122fe8fb19SBen Gras cookie->tail = cookie->head + size;
2132fe8fb19SBen Gras cookie->eob = cookie->head;
2142fe8fb19SBen Gras do {
2152fe8fb19SBen Gras if (*cookie->eob == '\0')
2162fe8fb19SBen Gras break;
2172fe8fb19SBen Gras ++cookie->eob;
2182fe8fb19SBen Gras } while (--size > 0);
2192fe8fb19SBen Gras
2202fe8fb19SBen Gras cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
2212fe8fb19SBen Gras
2222fe8fb19SBen Gras fp->_flags = flags;
223*f14fb602SLionel Sambuc fp->_write = (flags & __SRD) ? NULL : fmemopen_write;
224*f14fb602SLionel Sambuc fp->_read = (flags & __SWR) ? NULL : fmemopen_read;
225*f14fb602SLionel Sambuc fp->_seek = fmemopen_seek;
226*f14fb602SLionel Sambuc #ifdef notyet
227*f14fb602SLionel Sambuc fp->_flush = fmemopen_flush;
228*f14fb602SLionel Sambuc #endif
2292fe8fb19SBen Gras fp->_cookie = (void *)cookie;
2302fe8fb19SBen Gras
2312fe8fb19SBen Gras return fp;
2322fe8fb19SBen Gras
2332fe8fb19SBen Gras invalid:
2342fe8fb19SBen Gras errno = EINVAL;
2352fe8fb19SBen Gras return NULL;
2362fe8fb19SBen Gras
2372fe8fb19SBen Gras release:
2382fe8fb19SBen Gras fp->_flags = 0;
2392fe8fb19SBen Gras return NULL;
2402fe8fb19SBen Gras }
241