1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*-
* Copyright (c) 2013 Advanced Computing Technologies LLC
* Written by: John H. Baldwin <jhb@FreeBSD.org>
* All rights reserved.
*
* 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 THE 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 THE 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.
*/
#include <sys/cdefs.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
/* XXX: There is no FPOS_MAX. This assumes fpos_t is an off_t. */
#define FPOS_MAX OFF_MAX
struct memstream {
char **bufp;
size_t *sizep;
ssize_t len;
fpos_t offset;
};
static int
memstream_grow(struct memstream *ms, fpos_t newoff)
{
char *buf;
ssize_t newsize;
if (newoff < 0 || newoff >= SSIZE_MAX)
newsize = SSIZE_MAX - 1;
else
newsize = newoff;
if (newsize > ms->len) {
buf = realloc(*ms->bufp, newsize + 1);
if (buf != NULL) {
memset(buf + ms->len + 1, 0, newsize - ms->len);
*ms->bufp = buf;
ms->len = newsize;
return (1);
}
return (0);
}
return (1);
}
static void
memstream_update(struct memstream *ms)
{
assert(ms->len >= 0 && ms->offset >= 0);
*ms->sizep = ms->len < ms->offset ? ms->len : ms->offset;
}
static int
memstream_write(void *cookie, const char *buf, int len)
{
struct memstream *ms;
ssize_t tocopy;
ms = cookie;
if (!memstream_grow(ms, ms->offset + len))
return (-1);
tocopy = ms->len - ms->offset;
if (len < tocopy)
tocopy = len;
memcpy(*ms->bufp + ms->offset, buf, tocopy);
ms->offset += tocopy;
memstream_update(ms);
return (tocopy);
}
static fpos_t
memstream_seek(void *cookie, fpos_t pos, int whence)
{
struct memstream *ms;
#ifdef DEBUG
fpos_t old;
#endif
ms = cookie;
#ifdef DEBUG
old = ms->offset;
#endif
switch (whence) {
case SEEK_SET:
/* _fseeko() checks for negative offsets. */
assert(pos >= 0);
ms->offset = pos;
break;
case SEEK_CUR:
/* This is only called by _ftello(). */
assert(pos == 0);
break;
case SEEK_END:
if (pos < 0) {
if (pos + ms->len < 0) {
errno = EINVAL;
return (-1);
}
} else {
if (FPOS_MAX - ms->len < pos) {
errno = EOVERFLOW;
return (-1);
}
}
ms->offset = ms->len + pos;
break;
}
memstream_update(ms);
return (ms->offset);
}
static int
memstream_close(void *cookie)
{
free(cookie);
return (0);
}
FILE *
open_memstream(char **bufp, size_t *sizep)
{
struct memstream *ms;
int save_errno;
FILE *fp;
if (bufp == NULL || sizep == NULL) {
errno = EINVAL;
return (NULL);
}
*bufp = calloc(1, 1);
if (*bufp == NULL)
return (NULL);
ms = malloc(sizeof(*ms));
if (ms == NULL) {
save_errno = errno;
free(*bufp);
*bufp = NULL;
errno = save_errno;
return (NULL);
}
ms->bufp = bufp;
ms->sizep = sizep;
ms->len = 0;
ms->offset = 0;
memstream_update(ms);
fp = funopen(ms, NULL, memstream_write, memstream_seek,
memstream_close);
if (fp == NULL) {
save_errno = errno;
free(ms);
free(*bufp);
*bufp = NULL;
errno = save_errno;
return (NULL);
}
fwide(fp, -1);
return (fp);
}
|