Home | History | Annotate | Line # | Download | only in stdio
open_wmemstream.c revision 1.1
      1  1.1  christos /*	$NetBSD: open_wmemstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2013 Advanced Computing Technologies LLC
      5  1.1  christos  * Written by: John H. Baldwin <jhb (at) FreeBSD.org>
      6  1.1  christos  * All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.1  christos  * SUCH DAMAGE.
     28  1.1  christos  */
     29  1.1  christos 
     30  1.1  christos #include <sys/cdefs.h>
     31  1.1  christos #if 0
     32  1.1  christos __FBSDID("$FreeBSD: head/lib/libc/stdio/open_wmemstream.c 247411 2013-02-27 19:50:46Z jhb $");
     33  1.1  christos #endif
     34  1.1  christos __RCSID("$NetBSD: open_wmemstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $");
     35  1.1  christos 
     36  1.1  christos #include "namespace.h"
     37  1.1  christos #include <assert.h>
     38  1.1  christos #include <errno.h>
     39  1.1  christos #include <limits.h>
     40  1.1  christos #include <stdio.h>
     41  1.1  christos #include <stdlib.h>
     42  1.1  christos #include <string.h>
     43  1.1  christos #include <wchar.h>
     44  1.1  christos 
     45  1.1  christos #define	OFF_MAX LLONG_MAX
     46  1.1  christos 
     47  1.1  christos struct wmemstream {
     48  1.1  christos 	wchar_t **bufp;
     49  1.1  christos 	size_t *sizep;
     50  1.1  christos 	size_t len;
     51  1.1  christos 	size_t offset;
     52  1.1  christos 	mbstate_t mbstate;
     53  1.1  christos };
     54  1.1  christos 
     55  1.1  christos static int
     56  1.1  christos wmemstream_grow(struct wmemstream *ms, size_t newoff)
     57  1.1  christos {
     58  1.1  christos 	wchar_t *buf;
     59  1.1  christos 	size_t newsize;
     60  1.1  christos 
     61  1.1  christos 	if (newoff >= (off_t)(SSIZE_MAX / sizeof(wchar_t)))
     62  1.1  christos 		newsize = SSIZE_MAX / sizeof(wchar_t) - 1;
     63  1.1  christos 	else
     64  1.1  christos 		newsize = newoff;
     65  1.1  christos 	if (newsize > ms->len) {
     66  1.1  christos 		buf = realloc(*ms->bufp, (newsize + 1) * sizeof(wchar_t));
     67  1.1  christos 		if (buf != NULL) {
     68  1.1  christos #ifdef DEBUG
     69  1.1  christos 			fprintf(stderr, "WMS: %p growing from %zu to %zu\n",
     70  1.1  christos 			    ms, ms->len, newsize);
     71  1.1  christos #endif
     72  1.1  christos 			wmemset(buf + ms->len + 1, 0, newsize - ms->len);
     73  1.1  christos 			*ms->bufp = buf;
     74  1.1  christos 			ms->len = newsize;
     75  1.1  christos 			return (1);
     76  1.1  christos 		}
     77  1.1  christos 		return (0);
     78  1.1  christos 	}
     79  1.1  christos 	return (1);
     80  1.1  christos }
     81  1.1  christos 
     82  1.1  christos static void
     83  1.1  christos wmemstream_update(struct wmemstream *ms)
     84  1.1  christos {
     85  1.1  christos 
     86  1.1  christos 	*ms->sizep = ms->len < ms->offset ? ms->len : ms->offset;
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos /*
     90  1.1  christos  * Based on a starting multibyte state and an input buffer, determine
     91  1.1  christos  * how many wchar_t's would be output.  This doesn't use mbsnrtowcs()
     92  1.1  christos  * so that it can handle embedded null characters.
     93  1.1  christos  */
     94  1.1  christos static ssize_t
     95  1.1  christos wbuflen(const mbstate_t *state, const char *buf, size_t len)
     96  1.1  christos {
     97  1.1  christos 	mbstate_t lenstate;
     98  1.1  christos 	size_t charlen, count;
     99  1.1  christos 
    100  1.1  christos 	count = 0;
    101  1.1  christos 	lenstate = *state;
    102  1.1  christos 	while (len > 0) {
    103  1.1  christos 		charlen = mbrlen(buf, len, &lenstate);
    104  1.1  christos 		if (charlen == (size_t)-1)
    105  1.1  christos 			return (-1);
    106  1.1  christos 		if (charlen == (size_t)-2)
    107  1.1  christos 			break;
    108  1.1  christos 		if (charlen == 0)
    109  1.1  christos 			/* XXX: Not sure how else to handle this. */
    110  1.1  christos 			charlen = 1;
    111  1.1  christos 		len -= charlen;
    112  1.1  christos 		buf += charlen;
    113  1.1  christos 		count++;
    114  1.1  christos 	}
    115  1.1  christos 	return (count);
    116  1.1  christos }
    117  1.1  christos 
    118  1.1  christos static ssize_t
    119  1.1  christos wmemstream_write(void *cookie, const void *buf, size_t len)
    120  1.1  christos {
    121  1.1  christos 	struct wmemstream *ms;
    122  1.1  christos 	ssize_t consumed, wlen;
    123  1.1  christos 	size_t charlen;
    124  1.1  christos 
    125  1.1  christos 	ms = cookie;
    126  1.1  christos 	wlen = wbuflen(&ms->mbstate, buf, len);
    127  1.1  christos 	if (wlen < 0) {
    128  1.1  christos 		errno = EILSEQ;
    129  1.1  christos 		return (-1);
    130  1.1  christos 	}
    131  1.1  christos 	if (!wmemstream_grow(ms, ms->offset + wlen))
    132  1.1  christos 		return (-1);
    133  1.1  christos 
    134  1.1  christos 	/*
    135  1.1  christos 	 * This copies characters one at a time rather than using
    136  1.1  christos 	 * mbsnrtowcs() so it can properly handle embedded null
    137  1.1  christos 	 * characters.
    138  1.1  christos 	 */
    139  1.1  christos 	consumed = 0;
    140  1.1  christos 	while (len > 0 && ms->offset < ms->len) {
    141  1.1  christos 		charlen = mbrtowc(*ms->bufp + ms->offset, buf, len,
    142  1.1  christos 		    &ms->mbstate);
    143  1.1  christos 		if (charlen == (size_t)-1) {
    144  1.1  christos 			if (consumed == 0) {
    145  1.1  christos 				errno = EILSEQ;
    146  1.1  christos 				return (-1);
    147  1.1  christos 			}
    148  1.1  christos 			/* Treat it as a successful short write. */
    149  1.1  christos 			break;
    150  1.1  christos 		}
    151  1.1  christos 		if (charlen == 0)
    152  1.1  christos 			/* XXX: Not sure how else to handle this. */
    153  1.1  christos 			charlen = 1;
    154  1.1  christos 		if (charlen == (size_t)-2) {
    155  1.1  christos 			consumed += len;
    156  1.1  christos 			len = 0;
    157  1.1  christos 		} else {
    158  1.1  christos 			consumed += charlen;
    159  1.1  christos 			buf = (const char *)buf + charlen;
    160  1.1  christos 			len -= charlen;
    161  1.1  christos 			ms->offset++;
    162  1.1  christos 		}
    163  1.1  christos 	}
    164  1.1  christos 	wmemstream_update(ms);
    165  1.1  christos #ifdef DEBUG
    166  1.1  christos 	fprintf(stderr, "WMS: write(%p, %zu) = %zd\n", ms, len, consumed);
    167  1.1  christos #endif
    168  1.1  christos 	return (consumed);
    169  1.1  christos }
    170  1.1  christos 
    171  1.1  christos static off_t
    172  1.1  christos wmemstream_seek(void *cookie, off_t pos, int whence)
    173  1.1  christos {
    174  1.1  christos 	struct wmemstream *ms;
    175  1.1  christos 	size_t old;
    176  1.1  christos 
    177  1.1  christos 	ms = cookie;
    178  1.1  christos 	old = ms->offset;
    179  1.1  christos 	switch (whence) {
    180  1.1  christos 	case SEEK_SET:
    181  1.1  christos 		/* _fseeko() checks for negative offsets. */
    182  1.1  christos 		assert(pos >= 0);
    183  1.1  christos 		ms->offset = pos;
    184  1.1  christos 		break;
    185  1.1  christos 	case SEEK_CUR:
    186  1.1  christos 		/* This is only called by _ftello(). */
    187  1.1  christos 		assert(pos == 0);
    188  1.1  christos 		break;
    189  1.1  christos 	case SEEK_END:
    190  1.1  christos 		if (pos < 0) {
    191  1.1  christos 			if (pos + (ssize_t)ms->len < 0) {
    192  1.1  christos #ifdef DEBUG
    193  1.1  christos 				fprintf(stderr,
    194  1.1  christos 				    "WMS: bad SEEK_END: pos %jd, len %zd\n",
    195  1.1  christos 				    (intmax_t)pos, ms->len);
    196  1.1  christos #endif
    197  1.1  christos 				errno = EINVAL;
    198  1.1  christos 				return (-1);
    199  1.1  christos 			}
    200  1.1  christos 		} else {
    201  1.1  christos 			if (OFF_MAX - ms->len < (size_t)pos) {
    202  1.1  christos #ifdef DEBUG
    203  1.1  christos 				fprintf(stderr,
    204  1.1  christos 				    "WMS: bad SEEK_END: pos %jd, len %zd\n",
    205  1.1  christos 				    (intmax_t)pos, ms->len);
    206  1.1  christos #endif
    207  1.1  christos 				errno = EOVERFLOW;
    208  1.1  christos 				return (-1);
    209  1.1  christos 			}
    210  1.1  christos 		}
    211  1.1  christos 		ms->offset = ms->len + pos;
    212  1.1  christos 		break;
    213  1.1  christos 	}
    214  1.1  christos 	/* Reset the multibyte state if a seek changes the position. */
    215  1.1  christos 	if (ms->offset != old)
    216  1.1  christos 		memset(&ms->mbstate, 0, sizeof(ms->mbstate));
    217  1.1  christos 	wmemstream_update(ms);
    218  1.1  christos #ifdef DEBUG
    219  1.1  christos 	fprintf(stderr, "WMS: seek(%p, %jd, %d) %jd -> %jd\n", ms,
    220  1.1  christos 	    (intmax_t)pos, whence, (intmax_t)old, (intmax_t)ms->offset);
    221  1.1  christos #endif
    222  1.1  christos 	return (ms->offset);
    223  1.1  christos }
    224  1.1  christos 
    225  1.1  christos static int
    226  1.1  christos wmemstream_close(void *cookie)
    227  1.1  christos {
    228  1.1  christos 
    229  1.1  christos 	free(cookie);
    230  1.1  christos 	return (0);
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos FILE *
    234  1.1  christos open_wmemstream(wchar_t **bufp, size_t *sizep)
    235  1.1  christos {
    236  1.1  christos 	struct wmemstream *ms;
    237  1.1  christos 	int save_errno;
    238  1.1  christos 	FILE *fp;
    239  1.1  christos 
    240  1.1  christos 	if (bufp == NULL || sizep == NULL) {
    241  1.1  christos 		errno = EINVAL;
    242  1.1  christos 		return (NULL);
    243  1.1  christos 	}
    244  1.1  christos 	*bufp = calloc(1, sizeof(wchar_t));
    245  1.1  christos 	if (*bufp == NULL)
    246  1.1  christos 		return (NULL);
    247  1.1  christos 	ms = malloc(sizeof(*ms));
    248  1.1  christos 	if (ms == NULL) {
    249  1.1  christos 		save_errno = errno;
    250  1.1  christos 		free(*bufp);
    251  1.1  christos 		*bufp = NULL;
    252  1.1  christos 		errno = save_errno;
    253  1.1  christos 		return (NULL);
    254  1.1  christos 	}
    255  1.1  christos 	ms->bufp = bufp;
    256  1.1  christos 	ms->sizep = sizep;
    257  1.1  christos 	ms->len = 0;
    258  1.1  christos 	ms->offset = 0;
    259  1.1  christos 	memset(&ms->mbstate, 0, sizeof(mbstate_t));
    260  1.1  christos 	wmemstream_update(ms);
    261  1.1  christos 	fp = funopen2(ms, NULL, wmemstream_write, wmemstream_seek,
    262  1.1  christos 	    NULL, wmemstream_close);
    263  1.1  christos 	if (fp == NULL) {
    264  1.1  christos 		save_errno = errno;
    265  1.1  christos 		free(ms);
    266  1.1  christos 		free(*bufp);
    267  1.1  christos 		*bufp = NULL;
    268  1.1  christos 		errno = save_errno;
    269  1.1  christos 		return (NULL);
    270  1.1  christos 	}
    271  1.1  christos 	fwide(fp, 1);
    272  1.1  christos 	return (fp);
    273  1.1  christos }
    274