1 /* $NetBSD: open_wmemstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $ */
4 * Copyright (c) 2013 Advanced Computing Technologies 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>
32 __FBSDID("$FreeBSD: head/lib/libc/stdio/open_wmemstream.c 247411 2013-02-27 19:50:46Z jhb $");
34 __RCSID("$NetBSD: open_wmemstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $");
36 #include "namespace.h"
45 #define OFF_MAX LLONG_MAX
56 wmemstream_grow(struct wmemstream
*ms
, size_t newoff
)
61 if (newoff
>= (off_t
)(SSIZE_MAX
/ sizeof(wchar_t)))
62 newsize
= SSIZE_MAX
/ sizeof(wchar_t) - 1;
65 if (newsize
> ms
->len
) {
66 buf
= realloc(*ms
->bufp
, (newsize
+ 1) * sizeof(wchar_t));
69 fprintf(stderr
, "WMS: %p growing from %zu to %zu\n",
70 ms
, ms
->len
, newsize
);
72 wmemset(buf
+ ms
->len
+ 1, 0, newsize
- ms
->len
);
83 wmemstream_update(struct wmemstream
*ms
)
86 *ms
->sizep
= ms
->len
< ms
->offset
? ms
->len
: ms
->offset
;
90 * Based on a starting multibyte state and an input buffer, determine
91 * how many wchar_t's would be output. This doesn't use mbsnrtowcs()
92 * so that it can handle embedded null characters.
95 wbuflen(const mbstate_t *state
, const char *buf
, size_t len
)
98 size_t charlen
, count
;
103 charlen
= mbrlen(buf
, len
, &lenstate
);
104 if (charlen
== (size_t)-1)
106 if (charlen
== (size_t)-2)
109 /* XXX: Not sure how else to handle this. */
119 wmemstream_write(void *cookie
, const void *buf
, size_t len
)
121 struct wmemstream
*ms
;
122 ssize_t consumed
, wlen
;
126 wlen
= wbuflen(&ms
->mbstate
, buf
, len
);
131 if (!wmemstream_grow(ms
, ms
->offset
+ wlen
))
135 * This copies characters one at a time rather than using
136 * mbsnrtowcs() so it can properly handle embedded null
140 while (len
> 0 && ms
->offset
< ms
->len
) {
141 charlen
= mbrtowc(*ms
->bufp
+ ms
->offset
, buf
, len
,
143 if (charlen
== (size_t)-1) {
148 /* Treat it as a successful short write. */
152 /* XXX: Not sure how else to handle this. */
154 if (charlen
== (size_t)-2) {
159 buf
= (const char *)buf
+ charlen
;
164 wmemstream_update(ms
);
166 fprintf(stderr
, "WMS: write(%p, %zu) = %zd\n", ms
, len
, consumed
);
172 wmemstream_seek(void *cookie
, off_t pos
, int whence
)
174 struct wmemstream
*ms
;
181 /* _fseeko() checks for negative offsets. */
186 /* This is only called by _ftello(). */
191 if (pos
+ (ssize_t
)ms
->len
< 0) {
194 "WMS: bad SEEK_END: pos %jd, len %zd\n",
195 (intmax_t)pos
, ms
->len
);
201 if (OFF_MAX
- ms
->len
< (size_t)pos
) {
204 "WMS: bad SEEK_END: pos %jd, len %zd\n",
205 (intmax_t)pos
, ms
->len
);
211 ms
->offset
= ms
->len
+ pos
;
214 /* Reset the multibyte state if a seek changes the position. */
215 if (ms
->offset
!= old
)
216 memset(&ms
->mbstate
, 0, sizeof(ms
->mbstate
));
217 wmemstream_update(ms
);
219 fprintf(stderr
, "WMS: seek(%p, %jd, %d) %jd -> %jd\n", ms
,
220 (intmax_t)pos
, whence
, (intmax_t)old
, (intmax_t)ms
->offset
);
226 wmemstream_close(void *cookie
)
234 open_wmemstream(wchar_t **bufp
, size_t *sizep
)
236 struct wmemstream
*ms
;
240 if (bufp
== NULL
|| sizep
== NULL
) {
244 *bufp
= calloc(1, sizeof(wchar_t));
247 ms
= malloc(sizeof(*ms
));
259 memset(&ms
->mbstate
, 0, sizeof(mbstate_t));
260 wmemstream_update(ms
);
261 fp
= funopen2(ms
, NULL
, wmemstream_write
, wmemstream_seek
,
262 NULL
, wmemstream_close
);