2013-01-30 14:59:26 +00:00
|
|
|
/*-
|
2013-02-01 13:04:06 +00:00
|
|
|
* Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
#include <fcntl.h>
|
2013-02-01 13:04:06 +00:00
|
|
|
#include <stdbool.h>
|
2013-01-30 14:59:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2013-01-31 16:39:50 +00:00
|
|
|
#include "local.h"
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
struct fmemopen_cookie
|
2013-01-30 14:59:26 +00:00
|
|
|
{
|
2013-01-31 16:39:50 +00:00
|
|
|
char *buf; /* pointer to the memory region */
|
2013-02-01 13:04:06 +00:00
|
|
|
bool own; /* did we allocate the buffer ourselves? */
|
2013-01-31 16:39:50 +00:00
|
|
|
char bin; /* is this a binary buffer? */
|
|
|
|
size_t size; /* buffer length in bytes */
|
|
|
|
size_t len; /* data length in bytes */
|
|
|
|
size_t off; /* current offset into the buffer */
|
2013-01-30 14:59:26 +00:00
|
|
|
};
|
|
|
|
|
2013-02-01 13:04:06 +00:00
|
|
|
static int fmemopen_read(void *cookie, char *buf, int nbytes);
|
|
|
|
static int fmemopen_write(void *cookie, const char *buf, int nbytes);
|
|
|
|
static fpos_t fmemopen_seek(void *cookie, fpos_t offset, int whence);
|
|
|
|
static int fmemopen_close(void *cookie);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
FILE *
|
2013-02-01 13:04:06 +00:00
|
|
|
fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
|
2013-01-30 14:59:26 +00:00
|
|
|
{
|
2013-01-31 16:39:50 +00:00
|
|
|
struct fmemopen_cookie *ck;
|
|
|
|
FILE *f;
|
|
|
|
int flags, rc;
|
|
|
|
|
2013-02-01 13:04:06 +00:00
|
|
|
/*
|
2013-01-31 16:39:50 +00:00
|
|
|
* Retrieve the flags as used by open(2) from the mode argument, and
|
|
|
|
* validate them.
|
2013-02-01 13:04:06 +00:00
|
|
|
*/
|
|
|
|
rc = __sflags(mode, &flags);
|
2013-01-31 16:39:50 +00:00
|
|
|
if (rc == 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2013-02-01 13:04:06 +00:00
|
|
|
/*
|
2013-01-31 16:39:50 +00:00
|
|
|
* There's no point in requiring an automatically allocated buffer
|
|
|
|
* in write-only mode.
|
|
|
|
*/
|
|
|
|
if (!(flags & O_RDWR) && buf == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2013-02-01 13:04:06 +00:00
|
|
|
ck = malloc(sizeof(struct fmemopen_cookie));
|
2013-01-30 14:59:26 +00:00
|
|
|
if (ck == NULL) {
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
ck->off = 0;
|
|
|
|
ck->size = size;
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Check whether we have to allocate the buffer ourselves. */
|
2013-01-30 14:59:26 +00:00
|
|
|
ck->own = ((ck->buf = buf) == NULL);
|
|
|
|
if (ck->own) {
|
2013-02-01 13:04:06 +00:00
|
|
|
ck->buf = malloc(size);
|
2013-01-30 14:59:26 +00:00
|
|
|
if (ck->buf == NULL) {
|
2013-02-01 13:04:06 +00:00
|
|
|
free(ck);
|
2013-01-30 14:59:26 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2013-01-31 16:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* POSIX distinguishes between w+ and r+, in that w+ is supposed to
|
|
|
|
* truncate the buffer.
|
|
|
|
*/
|
|
|
|
if (ck->own || mode[0] == 'w') {
|
2013-01-30 14:59:26 +00:00
|
|
|
ck->buf[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Check for binary mode. */
|
|
|
|
ck->bin = strchr(mode, 'b') != NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The size of the current buffer contents is set depending on the
|
|
|
|
* mode:
|
2013-02-01 13:04:06 +00:00
|
|
|
*
|
2013-01-31 16:39:50 +00:00
|
|
|
* for append (text-mode), the position of the first NULL byte, or the
|
|
|
|
* size of the buffer if none is found
|
|
|
|
*
|
|
|
|
* for append (binary-mode), the size of the buffer
|
2013-02-01 13:04:06 +00:00
|
|
|
*
|
2013-01-31 16:39:50 +00:00
|
|
|
* for read, the size of the buffer
|
2013-02-01 13:04:06 +00:00
|
|
|
*
|
2013-01-31 16:39:50 +00:00
|
|
|
* for write, 0
|
|
|
|
*/
|
|
|
|
switch (mode[0]) {
|
|
|
|
case 'a':
|
|
|
|
if (ck->bin) {
|
2013-02-01 13:04:06 +00:00
|
|
|
/*
|
|
|
|
* This isn't useful, since the buffer isn't allowed
|
|
|
|
* to grow.
|
2013-01-31 16:39:50 +00:00
|
|
|
*/
|
|
|
|
ck->off = ck->len = size;
|
|
|
|
} else
|
|
|
|
ck->off = ck->len = strnlen(ck->buf, ck->size);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
ck->len = size;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
ck->len = 0;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-02-01 13:04:06 +00:00
|
|
|
f = funopen(ck,
|
2013-01-31 16:39:50 +00:00
|
|
|
flags & O_WRONLY ? NULL : fmemopen_read,
|
|
|
|
flags & O_RDONLY ? NULL : fmemopen_write,
|
2013-01-30 14:59:26 +00:00
|
|
|
fmemopen_seek, fmemopen_close);
|
|
|
|
|
|
|
|
if (f == NULL) {
|
|
|
|
if (ck->own)
|
2013-02-01 13:04:06 +00:00
|
|
|
free(ck->buf);
|
|
|
|
free(ck);
|
2013-01-30 14:59:26 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/*
|
|
|
|
* Turn off buffering, so a write past the end of the buffer
|
|
|
|
* correctly returns a short object count.
|
|
|
|
*/
|
2013-02-01 13:04:06 +00:00
|
|
|
setvbuf(f, NULL, _IONBF, 0);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
return (f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-02-01 13:04:06 +00:00
|
|
|
fmemopen_read(void *cookie, char *buf, int nbytes)
|
2013-01-30 14:59:26 +00:00
|
|
|
{
|
2013-01-31 16:39:50 +00:00
|
|
|
struct fmemopen_cookie *ck = cookie;
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
if (nbytes > ck->len - ck->off)
|
|
|
|
nbytes = ck->len - ck->off;
|
|
|
|
|
|
|
|
if (nbytes == 0)
|
|
|
|
return (0);
|
|
|
|
|
2013-02-01 13:04:06 +00:00
|
|
|
memcpy(buf, ck->buf + ck->off, nbytes);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
ck->off += nbytes;
|
|
|
|
|
|
|
|
return (nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-02-01 13:04:06 +00:00
|
|
|
fmemopen_write(void *cookie, const char *buf, int nbytes)
|
2013-01-30 14:59:26 +00:00
|
|
|
{
|
2013-01-31 16:39:50 +00:00
|
|
|
struct fmemopen_cookie *ck = cookie;
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
if (nbytes > ck->size - ck->off)
|
|
|
|
nbytes = ck->size - ck->off;
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
if (nbytes == 0)
|
|
|
|
return (0);
|
|
|
|
|
2013-02-01 13:04:06 +00:00
|
|
|
memcpy(ck->buf + ck->off, buf, nbytes);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
ck->off += nbytes;
|
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
if (ck->off > ck->len)
|
|
|
|
ck->len = ck->off;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We append a NULL byte if all these conditions are met:
|
|
|
|
* - the buffer is not binary
|
|
|
|
* - the buffer is not full
|
|
|
|
* - the data just written doesn't already end with a NULL byte
|
|
|
|
*/
|
|
|
|
if (!ck->bin && ck->off < ck->size && ck->buf[ck->off - 1] != '\0')
|
2013-01-30 14:59:26 +00:00
|
|
|
ck->buf[ck->off] = '\0';
|
|
|
|
|
|
|
|
return (nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fpos_t
|
2013-02-01 13:04:06 +00:00
|
|
|
fmemopen_seek(void *cookie, fpos_t offset, int whence)
|
2013-01-30 14:59:26 +00:00
|
|
|
{
|
2013-01-31 16:39:50 +00:00
|
|
|
struct fmemopen_cookie *ck = cookie;
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET:
|
2013-01-31 16:39:50 +00:00
|
|
|
if (offset > ck->size) {
|
2013-01-30 14:59:26 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
ck->off = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
2013-01-31 16:39:50 +00:00
|
|
|
if (ck->off + offset > ck->size) {
|
2013-01-30 14:59:26 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
ck->off += offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
|
|
|
if (offset > 0 || -offset > ck->len) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
ck->off = ck->len + offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ck->off);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-02-01 13:04:06 +00:00
|
|
|
fmemopen_close(void *cookie)
|
2013-01-30 14:59:26 +00:00
|
|
|
{
|
2013-01-31 16:39:50 +00:00
|
|
|
struct fmemopen_cookie *ck = cookie;
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
if (ck->own)
|
2013-02-01 13:04:06 +00:00
|
|
|
free(ck->buf);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-02-01 13:04:06 +00:00
|
|
|
free(ck);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|