2007-08-03 04:23:06 +08:00
|
|
|
/* Copyright (C) 2007 Eric Blake
|
|
|
|
* Permission to use, copy, modify, and distribute this software
|
|
|
|
* is freely granted, provided that this notice is preserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
FUNCTION
|
2009-02-19 01:35:24 +08:00
|
|
|
<<open_memstream>>, <<open_wmemstream>>---open a write stream around an arbitrary-length string
|
2007-08-03 04:23:06 +08:00
|
|
|
|
|
|
|
INDEX
|
|
|
|
open_memstream
|
2009-02-19 01:35:24 +08:00
|
|
|
INDEX
|
|
|
|
open_wmemstream
|
2007-08-03 04:23:06 +08:00
|
|
|
|
2017-11-30 16:05:02 +08:00
|
|
|
SYNOPSIS
|
2007-08-03 04:23:06 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
FILE *open_memstream(char **restrict <[buf]>,
|
|
|
|
size_t *restrict <[size]>);
|
|
|
|
|
2009-02-19 01:35:24 +08:00
|
|
|
#include <wchar.h>
|
|
|
|
FILE *open_wmemstream(wchar_t **restrict <[buf]>,
|
|
|
|
size_t *restrict <[size]>);
|
|
|
|
|
2007-08-03 04:23:06 +08:00
|
|
|
DESCRIPTION
|
2009-02-19 01:35:24 +08:00
|
|
|
<<open_memstream>> creates a seekable, byte-oriented <<FILE>> stream that
|
|
|
|
wraps an arbitrary-length buffer, created as if by <<malloc>>. The current
|
2007-08-03 04:23:06 +08:00
|
|
|
contents of *<[buf]> are ignored; this implementation uses *<[size]>
|
|
|
|
as a hint of the maximum size expected, but does not fail if the hint
|
|
|
|
was wrong. The parameters <[buf]> and <[size]> are later stored
|
|
|
|
through following any call to <<fflush>> or <<fclose>>, set to the
|
|
|
|
current address and usable size of the allocated string; although
|
|
|
|
after fflush, the pointer is only valid until another stream operation
|
|
|
|
that results in a write. Behavior is undefined if the user alters
|
|
|
|
either *<[buf]> or *<[size]> prior to <<fclose>>.
|
|
|
|
|
2009-02-19 01:35:24 +08:00
|
|
|
<<open_wmemstream>> is like <<open_memstream>> just with the associated
|
|
|
|
stream being wide-oriented. The size set in <[size]> in subsequent
|
|
|
|
operations is the number of wide characters.
|
|
|
|
|
2007-08-03 04:23:06 +08:00
|
|
|
The stream is write-only, since the user can directly read *<[buf]>
|
|
|
|
after a flush; see <<fmemopen>> for a way to wrap a string with a
|
|
|
|
readable stream. The user is responsible for calling <<free>> on
|
|
|
|
the final *<[buf]> after <<fclose>>.
|
|
|
|
|
|
|
|
Any time the stream is flushed, a NUL byte is written at the current
|
|
|
|
position (but is not counted in the buffer length), so that the string
|
2009-02-19 01:35:24 +08:00
|
|
|
is always NUL-terminated after at most *<[size]> bytes (or wide characters
|
|
|
|
in case of <<open_wmemstream>>). However, data previously written beyond
|
|
|
|
the current stream offset is not lost, and the NUL value written during a
|
|
|
|
flush is restored to its previous value when seeking elsewhere in the string.
|
2007-08-03 04:23:06 +08:00
|
|
|
|
|
|
|
RETURNS
|
|
|
|
The return value is an open FILE pointer on success. On error,
|
|
|
|
<<NULL>> is returned, and <<errno>> will be set to EINVAL if <[buf]>
|
|
|
|
or <[size]> is NULL, ENOMEM if memory could not be allocated, or
|
|
|
|
EMFILE if too many streams are already open.
|
|
|
|
|
|
|
|
PORTABILITY
|
2009-02-19 01:35:24 +08:00
|
|
|
POSIX.1-2008
|
2007-08-03 04:23:06 +08:00
|
|
|
|
|
|
|
Supporting OS subroutines required: <<sbrk>>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2009-02-19 01:35:24 +08:00
|
|
|
#include <wchar.h>
|
2007-08-03 04:23:06 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/lock.h>
|
2009-02-25 12:00:05 +08:00
|
|
|
#include <stdint.h>
|
2007-08-03 04:23:06 +08:00
|
|
|
#include "local.h"
|
|
|
|
|
|
|
|
#ifndef __LARGE64_FILES
|
|
|
|
# define OFF_T off_t
|
|
|
|
#else
|
|
|
|
# define OFF_T _off64_t
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Describe details of an open memstream. */
|
|
|
|
typedef struct memstream {
|
|
|
|
void *storage; /* storage to free on close */
|
|
|
|
char **pbuf; /* pointer to the current buffer */
|
|
|
|
size_t *psize; /* pointer to the current size, smaller of pos or eof */
|
|
|
|
size_t pos; /* current position */
|
|
|
|
size_t eof; /* current file size */
|
|
|
|
size_t max; /* current malloc buffer size, always > eof */
|
2009-02-19 01:35:24 +08:00
|
|
|
union {
|
|
|
|
char c;
|
|
|
|
wchar_t w;
|
|
|
|
} saved; /* saved character that lived at *psize before NUL */
|
|
|
|
int8_t wide; /* wide-oriented (>0) or byte-oriented (<0) */
|
2007-08-03 04:23:06 +08:00
|
|
|
} memstream;
|
|
|
|
|
|
|
|
/* Write up to non-zero N bytes of BUF into the stream described by COOKIE,
|
|
|
|
returning the number of bytes written or EOF on failure. */
|
|
|
|
static _READ_WRITE_RETURN_TYPE
|
2017-12-04 11:43:30 +08:00
|
|
|
memwriter (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
void *cookie,
|
|
|
|
const char *buf,
|
2013-10-23 18:04:43 +08:00
|
|
|
_READ_WRITE_BUFSIZE_TYPE n)
|
2007-08-03 04:23:06 +08:00
|
|
|
{
|
|
|
|
memstream *c = (memstream *) cookie;
|
|
|
|
char *cbuf = *c->pbuf;
|
|
|
|
|
|
|
|
/* size_t is unsigned, but off_t is signed. Don't let stream get so
|
|
|
|
big that user cannot do ftello. */
|
|
|
|
if (sizeof (OFF_T) == sizeof (size_t) && (ssize_t) (c->pos + n) < 0)
|
|
|
|
{
|
|
|
|
ptr->_errno = EFBIG;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
/* Grow the buffer, if necessary. Choose a geometric growth factor
|
|
|
|
to avoid quadratic realloc behavior, but use a rate less than
|
|
|
|
(1+sqrt(5))/2 to accomodate malloc overhead. Overallocate, so
|
|
|
|
that we can add a trailing \0 without reallocating. The new
|
|
|
|
allocation should thus be max(prev_size*1.5, c->pos+n+1). */
|
|
|
|
if (c->pos + n >= c->max)
|
|
|
|
{
|
|
|
|
size_t newsize = c->max * 3 / 2;
|
|
|
|
if (newsize < c->pos + n + 1)
|
|
|
|
newsize = c->pos + n + 1;
|
|
|
|
cbuf = _realloc_r (ptr, cbuf, newsize);
|
|
|
|
if (! cbuf)
|
|
|
|
return EOF; /* errno already set to ENOMEM */
|
|
|
|
*c->pbuf = cbuf;
|
|
|
|
c->max = newsize;
|
|
|
|
}
|
|
|
|
/* If we have previously done a seek beyond eof, ensure all
|
|
|
|
intermediate bytes are NUL. */
|
|
|
|
if (c->pos > c->eof)
|
|
|
|
memset (cbuf + c->eof, '\0', c->pos - c->eof);
|
|
|
|
memcpy (cbuf + c->pos, buf, n);
|
|
|
|
c->pos += n;
|
|
|
|
/* If the user has previously written further, remember what the
|
|
|
|
trailing NUL is overwriting. Otherwise, extend the stream. */
|
|
|
|
if (c->pos > c->eof)
|
|
|
|
c->eof = c->pos;
|
2009-02-19 01:35:24 +08:00
|
|
|
else if (c->wide > 0)
|
|
|
|
c->saved.w = *(wchar_t *)(cbuf + c->pos);
|
2007-08-03 04:23:06 +08:00
|
|
|
else
|
2009-02-19 01:35:24 +08:00
|
|
|
c->saved.c = cbuf[c->pos];
|
2007-08-03 04:23:06 +08:00
|
|
|
cbuf[c->pos] = '\0';
|
2009-02-19 01:35:24 +08:00
|
|
|
*c->psize = (c->wide > 0) ? c->pos / sizeof (wchar_t) : c->pos;
|
2007-08-03 04:23:06 +08:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek to position POS relative to WHENCE within stream described by
|
|
|
|
COOKIE; return resulting position or fail with EOF. */
|
|
|
|
static _fpos_t
|
2017-12-04 11:43:30 +08:00
|
|
|
memseeker (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
void *cookie,
|
|
|
|
_fpos_t pos,
|
2007-08-03 04:23:06 +08:00
|
|
|
int whence)
|
|
|
|
{
|
|
|
|
memstream *c = (memstream *) cookie;
|
|
|
|
OFF_T offset = (OFF_T) pos;
|
|
|
|
|
|
|
|
if (whence == SEEK_CUR)
|
|
|
|
offset += c->pos;
|
|
|
|
else if (whence == SEEK_END)
|
|
|
|
offset += c->eof;
|
|
|
|
if (offset < 0)
|
|
|
|
{
|
|
|
|
ptr->_errno = EINVAL;
|
|
|
|
offset = -1;
|
|
|
|
}
|
|
|
|
else if ((size_t) offset != offset)
|
|
|
|
{
|
|
|
|
ptr->_errno = ENOSPC;
|
|
|
|
offset = -1;
|
|
|
|
}
|
|
|
|
#ifdef __LARGE64_FILES
|
|
|
|
else if ((_fpos_t) offset != offset)
|
|
|
|
{
|
|
|
|
ptr->_errno = EOVERFLOW;
|
|
|
|
offset = -1;
|
|
|
|
}
|
|
|
|
#endif /* __LARGE64_FILES */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (c->pos < c->eof)
|
|
|
|
{
|
2009-02-19 01:35:24 +08:00
|
|
|
if (c->wide > 0)
|
|
|
|
*(wchar_t *)((*c->pbuf) + c->pos) = c->saved.w;
|
|
|
|
else
|
|
|
|
(*c->pbuf)[c->pos] = c->saved.c;
|
|
|
|
c->saved.w = L'\0';
|
2007-08-03 04:23:06 +08:00
|
|
|
}
|
|
|
|
c->pos = offset;
|
|
|
|
if (c->pos < c->eof)
|
|
|
|
{
|
2009-02-19 01:35:24 +08:00
|
|
|
if (c->wide > 0)
|
|
|
|
{
|
|
|
|
c->saved.w = *(wchar_t *)((*c->pbuf) + c->pos);
|
|
|
|
*(wchar_t *)((*c->pbuf) + c->pos) = L'\0';
|
|
|
|
*c->psize = c->pos / sizeof (wchar_t);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->saved.c = (*c->pbuf)[c->pos];
|
|
|
|
(*c->pbuf)[c->pos] = '\0';
|
|
|
|
*c->psize = c->pos;
|
|
|
|
}
|
2007-08-03 04:23:06 +08:00
|
|
|
}
|
2009-02-19 01:35:24 +08:00
|
|
|
else if (c->wide > 0)
|
|
|
|
*c->psize = c->eof / sizeof (wchar_t);
|
2007-08-03 04:23:06 +08:00
|
|
|
else
|
|
|
|
*c->psize = c->eof;
|
|
|
|
}
|
|
|
|
return (_fpos_t) offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek to position POS relative to WHENCE within stream described by
|
|
|
|
COOKIE; return resulting position or fail with EOF. */
|
|
|
|
#ifdef __LARGE64_FILES
|
|
|
|
static _fpos64_t
|
2017-12-04 11:43:30 +08:00
|
|
|
memseeker64 (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
void *cookie,
|
|
|
|
_fpos64_t pos,
|
2007-08-03 04:23:06 +08:00
|
|
|
int whence)
|
|
|
|
{
|
|
|
|
_off64_t offset = (_off64_t) pos;
|
|
|
|
memstream *c = (memstream *) cookie;
|
|
|
|
|
|
|
|
if (whence == SEEK_CUR)
|
|
|
|
offset += c->pos;
|
|
|
|
else if (whence == SEEK_END)
|
|
|
|
offset += c->eof;
|
|
|
|
if (offset < 0)
|
|
|
|
{
|
|
|
|
ptr->_errno = EINVAL;
|
|
|
|
offset = -1;
|
|
|
|
}
|
|
|
|
else if ((size_t) offset != offset)
|
|
|
|
{
|
|
|
|
ptr->_errno = ENOSPC;
|
|
|
|
offset = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (c->pos < c->eof)
|
|
|
|
{
|
2009-02-19 01:35:24 +08:00
|
|
|
if (c->wide > 0)
|
|
|
|
*(wchar_t *)((*c->pbuf) + c->pos) = c->saved.w;
|
|
|
|
else
|
|
|
|
(*c->pbuf)[c->pos] = c->saved.c;
|
|
|
|
c->saved.w = L'\0';
|
2007-08-03 04:23:06 +08:00
|
|
|
}
|
|
|
|
c->pos = offset;
|
|
|
|
if (c->pos < c->eof)
|
|
|
|
{
|
2009-02-19 01:35:24 +08:00
|
|
|
if (c->wide > 0)
|
|
|
|
{
|
|
|
|
c->saved.w = *(wchar_t *)((*c->pbuf) + c->pos);
|
|
|
|
*(wchar_t *)((*c->pbuf) + c->pos) = L'\0';
|
|
|
|
*c->psize = c->pos / sizeof (wchar_t);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->saved.c = (*c->pbuf)[c->pos];
|
|
|
|
(*c->pbuf)[c->pos] = '\0';
|
|
|
|
*c->psize = c->pos;
|
|
|
|
}
|
2007-08-03 04:23:06 +08:00
|
|
|
}
|
2009-02-19 01:35:24 +08:00
|
|
|
else if (c->wide > 0)
|
|
|
|
*c->psize = c->eof / sizeof (wchar_t);
|
2007-08-03 04:23:06 +08:00
|
|
|
else
|
|
|
|
*c->psize = c->eof;
|
|
|
|
}
|
|
|
|
return (_fpos64_t) offset;
|
|
|
|
}
|
|
|
|
#endif /* __LARGE64_FILES */
|
|
|
|
|
|
|
|
/* Reclaim resources used by stream described by COOKIE. */
|
|
|
|
static int
|
2017-12-04 11:43:30 +08:00
|
|
|
memcloser (struct _reent *ptr,
|
2007-08-03 04:23:06 +08:00
|
|
|
void *cookie)
|
|
|
|
{
|
|
|
|
memstream *c = (memstream *) cookie;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
/* Be nice and try to reduce any unused memory. */
|
2009-02-19 01:35:24 +08:00
|
|
|
buf = _realloc_r (ptr, *c->pbuf,
|
|
|
|
c->wide > 0 ? (*c->psize + 1) * sizeof (wchar_t)
|
|
|
|
: *c->psize + 1);
|
2007-08-03 04:23:06 +08:00
|
|
|
if (buf)
|
|
|
|
*c->pbuf = buf;
|
|
|
|
_free_r (ptr, c->storage);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open a memstream that tracks a dynamic buffer in BUF and SIZE.
|
|
|
|
Return the new stream, or fail with NULL. */
|
2009-02-19 01:35:24 +08:00
|
|
|
static FILE *
|
2017-12-04 11:43:30 +08:00
|
|
|
internal_open_memstream_r (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
char **buf,
|
|
|
|
size_t *size,
|
2009-02-19 01:35:24 +08:00
|
|
|
int wide)
|
2007-08-03 04:23:06 +08:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
memstream *c;
|
|
|
|
|
|
|
|
if (!buf || !size)
|
|
|
|
{
|
|
|
|
ptr->_errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((fp = __sfp (ptr)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
if ((c = (memstream *) _malloc_r (ptr, sizeof *c)) == NULL)
|
|
|
|
{
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_sfp_lock_start ();
|
2007-08-03 04:23:06 +08:00
|
|
|
fp->_flags = 0; /* release */
|
|
|
|
#ifndef __SINGLE_THREAD__
|
|
|
|
__lock_close_recursive (fp->_lock);
|
|
|
|
#endif
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_sfp_lock_end ();
|
2007-08-03 04:23:06 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Use *size as a hint for initial sizing, but bound the initial
|
|
|
|
malloc between 64 bytes (same as asprintf, to avoid frequent
|
|
|
|
mallocs on small strings) and 64k bytes (to avoid overusing the
|
|
|
|
heap if *size was garbage). */
|
|
|
|
c->max = *size;
|
2009-02-19 05:28:41 +08:00
|
|
|
if (wide == 1)
|
|
|
|
c->max *= sizeof(wchar_t);
|
2007-08-03 04:23:06 +08:00
|
|
|
if (c->max < 64)
|
|
|
|
c->max = 64;
|
2011-01-06 01:38:42 +08:00
|
|
|
#if (SIZE_MAX >= 64 * 1024)
|
2020-09-02 22:50:07 +08:00
|
|
|
else if (c->max > (size_t)64 * 1024)
|
|
|
|
c->max = (size_t)64 * 1024;
|
2011-01-06 01:38:42 +08:00
|
|
|
#endif
|
2007-08-03 04:23:06 +08:00
|
|
|
*size = 0;
|
|
|
|
*buf = _malloc_r (ptr, c->max);
|
|
|
|
if (!*buf)
|
|
|
|
{
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_sfp_lock_start ();
|
2007-08-03 04:23:06 +08:00
|
|
|
fp->_flags = 0; /* release */
|
|
|
|
#ifndef __SINGLE_THREAD__
|
|
|
|
__lock_close_recursive (fp->_lock);
|
|
|
|
#endif
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_sfp_lock_end ();
|
2007-08-03 04:23:06 +08:00
|
|
|
_free_r (ptr, c);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-02-19 05:28:41 +08:00
|
|
|
if (wide == 1)
|
|
|
|
**((wchar_t **)buf) = L'\0';
|
|
|
|
else
|
|
|
|
**buf = '\0';
|
2007-08-03 04:23:06 +08:00
|
|
|
|
|
|
|
c->storage = c;
|
|
|
|
c->pbuf = buf;
|
|
|
|
c->psize = size;
|
2014-02-20 21:32:34 +08:00
|
|
|
c->pos = 0;
|
2007-08-03 04:23:06 +08:00
|
|
|
c->eof = 0;
|
2009-02-19 01:35:24 +08:00
|
|
|
c->saved.w = L'\0';
|
|
|
|
c->wide = (int8_t) wide;
|
2007-08-03 04:23:06 +08:00
|
|
|
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_start (fp);
|
2007-08-03 04:23:06 +08:00
|
|
|
fp->_file = -1;
|
|
|
|
fp->_flags = __SWR;
|
|
|
|
fp->_cookie = c;
|
|
|
|
fp->_read = NULL;
|
|
|
|
fp->_write = memwriter;
|
|
|
|
fp->_seek = memseeker;
|
|
|
|
#ifdef __LARGE64_FILES
|
|
|
|
fp->_seek64 = memseeker64;
|
|
|
|
fp->_flags |= __SL64;
|
|
|
|
#endif
|
|
|
|
fp->_close = memcloser;
|
2009-02-19 01:35:24 +08:00
|
|
|
ORIENT (fp, wide);
|
2012-05-30 16:58:42 +08:00
|
|
|
_newlib_flockfile_end (fp);
|
2007-08-03 04:23:06 +08:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
2009-02-19 01:35:24 +08:00
|
|
|
FILE *
|
2017-12-04 11:43:30 +08:00
|
|
|
_open_memstream_r (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
char **buf,
|
2009-02-19 01:35:24 +08:00
|
|
|
size_t *size)
|
|
|
|
{
|
2009-02-19 05:28:41 +08:00
|
|
|
return internal_open_memstream_r (ptr, buf, size, -1);
|
2009-02-19 01:35:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
2017-12-04 11:43:30 +08:00
|
|
|
_open_wmemstream_r (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
wchar_t **buf,
|
2009-02-19 01:35:24 +08:00
|
|
|
size_t *size)
|
|
|
|
{
|
2009-02-19 05:28:41 +08:00
|
|
|
return internal_open_memstream_r (ptr, (char **)buf, size, 1);
|
2009-02-19 01:35:24 +08:00
|
|
|
}
|
|
|
|
|
2007-08-03 04:23:06 +08:00
|
|
|
#ifndef _REENT_ONLY
|
|
|
|
FILE *
|
2017-12-04 11:43:30 +08:00
|
|
|
open_memstream (char **buf,
|
2007-08-03 04:23:06 +08:00
|
|
|
size_t *size)
|
|
|
|
{
|
|
|
|
return _open_memstream_r (_REENT, buf, size);
|
|
|
|
}
|
2009-02-19 01:35:24 +08:00
|
|
|
|
|
|
|
FILE *
|
2017-12-04 11:43:30 +08:00
|
|
|
open_wmemstream (wchar_t **buf,
|
2009-02-19 01:35:24 +08:00
|
|
|
size_t *size)
|
|
|
|
{
|
|
|
|
return _open_wmemstream_r (_REENT, buf, size);
|
|
|
|
}
|
2007-08-03 04:23:06 +08:00
|
|
|
#endif /* !_REENT_ONLY */
|