2 * Copyright (c) 2013 Advanced Computing Technologies LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "namespace.h"
39 #include "un-namespace.h"
41 /* XXX: There is no FPOS_MAX. This assumes fpos_t is an off_t. */
42 #define FPOS_MAX OFF_MAX
53 wmemstream_grow(struct wmemstream *ms, fpos_t newoff)
58 if (newoff < 0 || newoff >= SSIZE_MAX / sizeof(wchar_t))
59 newsize = SSIZE_MAX / sizeof(wchar_t) - 1;
62 if (newsize > ms->len) {
63 buf = realloc(*ms->bufp, (newsize + 1) * sizeof(wchar_t));
66 fprintf(stderr, "WMS: %p growing from %zd to %zd\n",
67 ms, ms->len, newsize);
69 wmemset(buf + ms->len + 1, 0, newsize - ms->len);
80 wmemstream_update(struct wmemstream *ms)
83 assert(ms->len >= 0 && ms->offset >= 0);
84 *ms->sizep = ms->len < ms->offset ? ms->len : ms->offset;
88 * Based on a starting multibyte state and an input buffer, determine
89 * how many wchar_t's would be output. This doesn't use mbsnrtowcs()
90 * so that it can handle embedded null characters.
93 wbuflen(const mbstate_t *state, const char *buf, int len)
96 size_t charlen, count;
101 charlen = mbrlen(buf, len, &lenstate);
102 if (charlen == (size_t)-1)
104 if (charlen == (size_t)-2)
107 /* XXX: Not sure how else to handle this. */
117 wmemstream_write(void *cookie, const char *buf, int len)
119 struct wmemstream *ms;
120 ssize_t consumed, wlen;
124 wlen = wbuflen(&ms->mbstate, buf, len);
129 if (!wmemstream_grow(ms, ms->offset + wlen))
133 * This copies characters one at a time rather than using
134 * mbsnrtowcs() so it can properly handle embedded null
138 while (len > 0 && ms->offset < ms->len) {
139 charlen = mbrtowc(*ms->bufp + ms->offset, buf, len,
141 if (charlen == (size_t)-1) {
146 /* Treat it as a successful short write. */
150 /* XXX: Not sure how else to handle this. */
152 if (charlen == (size_t)-2) {
162 wmemstream_update(ms);
164 fprintf(stderr, "WMS: write(%p, %d) = %zd\n", ms, len, consumed);
170 wmemstream_seek(void *cookie, fpos_t pos, int whence)
172 struct wmemstream *ms;
179 /* _fseeko() checks for negative offsets. */
184 /* This is only called by _ftello(). */
189 if (pos + ms->len < 0) {
192 "WMS: bad SEEK_END: pos %jd, len %zd\n",
193 (intmax_t)pos, ms->len);
199 if (FPOS_MAX - ms->len < pos) {
202 "WMS: bad SEEK_END: pos %jd, len %zd\n",
203 (intmax_t)pos, ms->len);
209 ms->offset = ms->len + pos;
212 /* Reset the multibyte state if a seek changes the position. */
213 if (ms->offset != old)
214 memset(&ms->mbstate, 0, sizeof(ms->mbstate));
215 wmemstream_update(ms);
217 fprintf(stderr, "WMS: seek(%p, %jd, %d) %jd -> %jd\n", ms,
218 (intmax_t)pos, whence, (intmax_t)old, (intmax_t)ms->offset);
224 wmemstream_close(void *cookie)
232 open_wmemstream(wchar_t **bufp, size_t *sizep)
234 struct wmemstream *ms;
238 if (bufp == NULL || sizep == NULL) {
242 *bufp = calloc(1, sizeof(wchar_t));
245 ms = malloc(sizeof(*ms));
257 memset(&ms->mbstate, 0, sizeof(mbstate_t));
258 wmemstream_update(ms);
259 fp = funopen(ms, NULL, wmemstream_write, wmemstream_seek,