2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Hudson River Trading LLC
5 * Written by: John H. Baldwin <jhb@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "namespace.h"
44 #include "un-namespace.h"
46 /* XXX: There is no FPOS_MAX. This assumes fpos_t is an off_t. */
47 #define FPOS_MAX OFF_MAX
58 wmemstream_grow(struct wmemstream *ms, fpos_t newoff)
63 if (newoff < 0 || newoff >= SSIZE_MAX / sizeof(wchar_t))
64 newsize = SSIZE_MAX / sizeof(wchar_t) - 1;
67 if (newsize > ms->len) {
68 buf = reallocarray(*ms->bufp, newsize + 1, sizeof(wchar_t));
71 fprintf(stderr, "WMS: %p growing from %zd to %zd\n",
72 ms, ms->len, newsize);
74 wmemset(buf + ms->len + 1, 0, newsize - ms->len);
85 wmemstream_update(struct wmemstream *ms)
88 assert(ms->len >= 0 && ms->offset >= 0);
89 *ms->sizep = ms->len < ms->offset ? ms->len : ms->offset;
93 * Based on a starting multibyte state and an input buffer, determine
94 * how many wchar_t's would be output. This doesn't use mbsnrtowcs()
95 * so that it can handle embedded null characters.
98 wbuflen(const mbstate_t *state, const char *buf, int len)
101 size_t charlen, count;
106 charlen = mbrlen(buf, len, &lenstate);
107 if (charlen == (size_t)-1)
109 if (charlen == (size_t)-2)
112 /* XXX: Not sure how else to handle this. */
122 wmemstream_write(void *cookie, const char *buf, int len)
124 struct wmemstream *ms;
125 ssize_t consumed, wlen;
129 wlen = wbuflen(&ms->mbstate, buf, len);
134 if (!wmemstream_grow(ms, ms->offset + wlen))
138 * This copies characters one at a time rather than using
139 * mbsnrtowcs() so it can properly handle embedded null
143 while (len > 0 && ms->offset < ms->len) {
144 charlen = mbrtowc(*ms->bufp + ms->offset, buf, len,
146 if (charlen == (size_t)-1) {
151 /* Treat it as a successful short write. */
155 /* XXX: Not sure how else to handle this. */
157 if (charlen == (size_t)-2) {
167 wmemstream_update(ms);
169 fprintf(stderr, "WMS: write(%p, %d) = %zd\n", ms, len, consumed);
175 wmemstream_seek(void *cookie, fpos_t pos, int whence)
177 struct wmemstream *ms;
184 /* _fseeko() checks for negative offsets. */
189 /* This is only called by _ftello(). */
194 if (pos + ms->len < 0) {
197 "WMS: bad SEEK_END: pos %jd, len %zd\n",
198 (intmax_t)pos, ms->len);
204 if (FPOS_MAX - ms->len < pos) {
207 "WMS: bad SEEK_END: pos %jd, len %zd\n",
208 (intmax_t)pos, ms->len);
214 ms->offset = ms->len + pos;
217 /* Reset the multibyte state if a seek changes the position. */
218 if (ms->offset != old)
219 memset(&ms->mbstate, 0, sizeof(ms->mbstate));
220 wmemstream_update(ms);
222 fprintf(stderr, "WMS: seek(%p, %jd, %d) %jd -> %jd\n", ms,
223 (intmax_t)pos, whence, (intmax_t)old, (intmax_t)ms->offset);
229 wmemstream_close(void *cookie)
237 open_wmemstream(wchar_t **bufp, size_t *sizep)
239 struct wmemstream *ms;
243 if (bufp == NULL || sizep == NULL) {
247 *bufp = calloc(1, sizeof(wchar_t));
250 ms = malloc(sizeof(*ms));
262 memset(&ms->mbstate, 0, sizeof(mbstate_t));
263 wmemstream_update(ms);
264 fp = funopen(ms, NULL, wmemstream_write, wmemstream_seek,