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