1 /* $NetBSD: open_wmemstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 Advanced Computing Technologies LLC 5 * Written by: John H. Baldwin <jhb@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 #if 0 32 __FBSDID("$FreeBSD: head/lib/libc/stdio/open_wmemstream.c 247411 2013-02-27 19:50:46Z jhb $"); 33 #endif 34 __RCSID("$NetBSD: open_wmemstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $"); 35 36 #include "namespace.h" 37 #include <assert.h> 38 #include <errno.h> 39 #include <limits.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <wchar.h> 44 45 #define OFF_MAX LLONG_MAX 46 47 struct wmemstream { 48 wchar_t **bufp; 49 size_t *sizep; 50 size_t len; 51 size_t offset; 52 mbstate_t mbstate; 53 }; 54 55 static int 56 wmemstream_grow(struct wmemstream *ms, size_t newoff) 57 { 58 wchar_t *buf; 59 size_t newsize; 60 61 if (newoff >= (off_t)(SSIZE_MAX / sizeof(wchar_t))) 62 newsize = SSIZE_MAX / sizeof(wchar_t) - 1; 63 else 64 newsize = newoff; 65 if (newsize > ms->len) { 66 buf = realloc(*ms->bufp, (newsize + 1) * sizeof(wchar_t)); 67 if (buf != NULL) { 68 #ifdef DEBUG 69 fprintf(stderr, "WMS: %p growing from %zu to %zu\n", 70 ms, ms->len, newsize); 71 #endif 72 wmemset(buf + ms->len + 1, 0, newsize - ms->len); 73 *ms->bufp = buf; 74 ms->len = newsize; 75 return (1); 76 } 77 return (0); 78 } 79 return (1); 80 } 81 82 static void 83 wmemstream_update(struct wmemstream *ms) 84 { 85 86 *ms->sizep = ms->len < ms->offset ? ms->len : ms->offset; 87 } 88 89 /* 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. 93 */ 94 static ssize_t 95 wbuflen(const mbstate_t *state, const char *buf, size_t len) 96 { 97 mbstate_t lenstate; 98 size_t charlen, count; 99 100 count = 0; 101 lenstate = *state; 102 while (len > 0) { 103 charlen = mbrlen(buf, len, &lenstate); 104 if (charlen == (size_t)-1) 105 return (-1); 106 if (charlen == (size_t)-2) 107 break; 108 if (charlen == 0) 109 /* XXX: Not sure how else to handle this. */ 110 charlen = 1; 111 len -= charlen; 112 buf += charlen; 113 count++; 114 } 115 return (count); 116 } 117 118 static ssize_t 119 wmemstream_write(void *cookie, const void *buf, size_t len) 120 { 121 struct wmemstream *ms; 122 ssize_t consumed, wlen; 123 size_t charlen; 124 125 ms = cookie; 126 wlen = wbuflen(&ms->mbstate, buf, len); 127 if (wlen < 0) { 128 errno = EILSEQ; 129 return (-1); 130 } 131 if (!wmemstream_grow(ms, ms->offset + wlen)) 132 return (-1); 133 134 /* 135 * This copies characters one at a time rather than using 136 * mbsnrtowcs() so it can properly handle embedded null 137 * characters. 138 */ 139 consumed = 0; 140 while (len > 0 && ms->offset < ms->len) { 141 charlen = mbrtowc(*ms->bufp + ms->offset, buf, len, 142 &ms->mbstate); 143 if (charlen == (size_t)-1) { 144 if (consumed == 0) { 145 errno = EILSEQ; 146 return (-1); 147 } 148 /* Treat it as a successful short write. */ 149 break; 150 } 151 if (charlen == 0) 152 /* XXX: Not sure how else to handle this. */ 153 charlen = 1; 154 if (charlen == (size_t)-2) { 155 consumed += len; 156 len = 0; 157 } else { 158 consumed += charlen; 159 buf = (const char *)buf + charlen; 160 len -= charlen; 161 ms->offset++; 162 } 163 } 164 wmemstream_update(ms); 165 #ifdef DEBUG 166 fprintf(stderr, "WMS: write(%p, %zu) = %zd\n", ms, len, consumed); 167 #endif 168 return (consumed); 169 } 170 171 static off_t 172 wmemstream_seek(void *cookie, off_t pos, int whence) 173 { 174 struct wmemstream *ms; 175 size_t old; 176 177 ms = cookie; 178 old = ms->offset; 179 switch (whence) { 180 case SEEK_SET: 181 /* _fseeko() checks for negative offsets. */ 182 assert(pos >= 0); 183 ms->offset = pos; 184 break; 185 case SEEK_CUR: 186 /* This is only called by _ftello(). */ 187 assert(pos == 0); 188 break; 189 case SEEK_END: 190 if (pos < 0) { 191 if (pos + (ssize_t)ms->len < 0) { 192 #ifdef DEBUG 193 fprintf(stderr, 194 "WMS: bad SEEK_END: pos %jd, len %zd\n", 195 (intmax_t)pos, ms->len); 196 #endif 197 errno = EINVAL; 198 return (-1); 199 } 200 } else { 201 if (OFF_MAX - ms->len < (size_t)pos) { 202 #ifdef DEBUG 203 fprintf(stderr, 204 "WMS: bad SEEK_END: pos %jd, len %zd\n", 205 (intmax_t)pos, ms->len); 206 #endif 207 errno = EOVERFLOW; 208 return (-1); 209 } 210 } 211 ms->offset = ms->len + pos; 212 break; 213 } 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); 218 #ifdef DEBUG 219 fprintf(stderr, "WMS: seek(%p, %jd, %d) %jd -> %jd\n", ms, 220 (intmax_t)pos, whence, (intmax_t)old, (intmax_t)ms->offset); 221 #endif 222 return (ms->offset); 223 } 224 225 static int 226 wmemstream_close(void *cookie) 227 { 228 229 free(cookie); 230 return (0); 231 } 232 233 FILE * 234 open_wmemstream(wchar_t **bufp, size_t *sizep) 235 { 236 struct wmemstream *ms; 237 int save_errno; 238 FILE *fp; 239 240 if (bufp == NULL || sizep == NULL) { 241 errno = EINVAL; 242 return (NULL); 243 } 244 *bufp = calloc(1, sizeof(wchar_t)); 245 if (*bufp == NULL) 246 return (NULL); 247 ms = malloc(sizeof(*ms)); 248 if (ms == NULL) { 249 save_errno = errno; 250 free(*bufp); 251 *bufp = NULL; 252 errno = save_errno; 253 return (NULL); 254 } 255 ms->bufp = bufp; 256 ms->sizep = sizep; 257 ms->len = 0; 258 ms->offset = 0; 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); 263 if (fp == NULL) { 264 save_errno = errno; 265 free(ms); 266 free(*bufp); 267 *bufp = NULL; 268 errno = save_errno; 269 return (NULL); 270 } 271 fwide(fp, 1); 272 return (fp); 273 } 274