Home | History | Annotate | Line # | Download | only in stdio
      1  1.2  christos /*	$NetBSD: open_wmemstream.c,v 1.2 2024/01/23 15:32:54 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.2  christos __RCSID("$NetBSD: open_wmemstream.c,v 1.2 2024/01/23 15:32:54 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.2  christos static __inline size_t
     56  1.2  christos off_t_to_size_t(off_t off)
     57  1.2  christos {
     58  1.2  christos 	if (off < 0 || off >= SSIZE_MAX)
     59  1.2  christos 		return SSIZE_MAX - 1;
     60  1.2  christos 	return (size_t)off;
     61  1.2  christos }
     62  1.2  christos 
     63  1.1  christos static int
     64  1.1  christos wmemstream_grow(struct wmemstream *ms, size_t newoff)
     65  1.1  christos {
     66  1.1  christos 	wchar_t *buf;
     67  1.1  christos 	size_t newsize;
     68  1.1  christos 
     69  1.1  christos 	if (newoff >= (off_t)(SSIZE_MAX / sizeof(wchar_t)))
     70  1.1  christos 		newsize = SSIZE_MAX / sizeof(wchar_t) - 1;
     71  1.1  christos 	else
     72  1.1  christos 		newsize = newoff;
     73  1.1  christos 	if (newsize > ms->len) {
     74  1.1  christos 		buf = realloc(*ms->bufp, (newsize + 1) * sizeof(wchar_t));
     75  1.1  christos 		if (buf != NULL) {
     76  1.1  christos #ifdef DEBUG
     77  1.1  christos 			fprintf(stderr, "WMS: %p growing from %zu to %zu\n",
     78  1.1  christos 			    ms, ms->len, newsize);
     79  1.1  christos #endif
     80  1.1  christos 			wmemset(buf + ms->len + 1, 0, newsize - ms->len);
     81  1.1  christos 			*ms->bufp = buf;
     82  1.1  christos 			ms->len = newsize;
     83  1.1  christos 			return (1);
     84  1.1  christos 		}
     85  1.1  christos 		return (0);
     86  1.1  christos 	}
     87  1.1  christos 	return (1);
     88  1.1  christos }
     89  1.1  christos 
     90  1.1  christos static void
     91  1.1  christos wmemstream_update(struct wmemstream *ms)
     92  1.1  christos {
     93  1.1  christos 
     94  1.1  christos 	*ms->sizep = ms->len < ms->offset ? ms->len : ms->offset;
     95  1.1  christos }
     96  1.1  christos 
     97  1.1  christos /*
     98  1.1  christos  * Based on a starting multibyte state and an input buffer, determine
     99  1.1  christos  * how many wchar_t's would be output.  This doesn't use mbsnrtowcs()
    100  1.1  christos  * so that it can handle embedded null characters.
    101  1.1  christos  */
    102  1.1  christos static ssize_t
    103  1.1  christos wbuflen(const mbstate_t *state, const char *buf, size_t len)
    104  1.1  christos {
    105  1.1  christos 	mbstate_t lenstate;
    106  1.1  christos 	size_t charlen, count;
    107  1.1  christos 
    108  1.1  christos 	count = 0;
    109  1.1  christos 	lenstate = *state;
    110  1.1  christos 	while (len > 0) {
    111  1.1  christos 		charlen = mbrlen(buf, len, &lenstate);
    112  1.1  christos 		if (charlen == (size_t)-1)
    113  1.1  christos 			return (-1);
    114  1.1  christos 		if (charlen == (size_t)-2)
    115  1.1  christos 			break;
    116  1.1  christos 		if (charlen == 0)
    117  1.1  christos 			/* XXX: Not sure how else to handle this. */
    118  1.1  christos 			charlen = 1;
    119  1.1  christos 		len -= charlen;
    120  1.1  christos 		buf += charlen;
    121  1.1  christos 		count++;
    122  1.1  christos 	}
    123  1.1  christos 	return (count);
    124  1.1  christos }
    125  1.1  christos 
    126  1.1  christos static ssize_t
    127  1.1  christos wmemstream_write(void *cookie, const void *buf, size_t len)
    128  1.1  christos {
    129  1.1  christos 	struct wmemstream *ms;
    130  1.1  christos 	ssize_t consumed, wlen;
    131  1.1  christos 	size_t charlen;
    132  1.1  christos 
    133  1.1  christos 	ms = cookie;
    134  1.1  christos 	wlen = wbuflen(&ms->mbstate, buf, len);
    135  1.1  christos 	if (wlen < 0) {
    136  1.1  christos 		errno = EILSEQ;
    137  1.1  christos 		return (-1);
    138  1.1  christos 	}
    139  1.1  christos 	if (!wmemstream_grow(ms, ms->offset + wlen))
    140  1.1  christos 		return (-1);
    141  1.1  christos 
    142  1.1  christos 	/*
    143  1.1  christos 	 * This copies characters one at a time rather than using
    144  1.1  christos 	 * mbsnrtowcs() so it can properly handle embedded null
    145  1.1  christos 	 * characters.
    146  1.1  christos 	 */
    147  1.1  christos 	consumed = 0;
    148  1.1  christos 	while (len > 0 && ms->offset < ms->len) {
    149  1.1  christos 		charlen = mbrtowc(*ms->bufp + ms->offset, buf, len,
    150  1.1  christos 		    &ms->mbstate);
    151  1.1  christos 		if (charlen == (size_t)-1) {
    152  1.1  christos 			if (consumed == 0) {
    153  1.1  christos 				errno = EILSEQ;
    154  1.1  christos 				return (-1);
    155  1.1  christos 			}
    156  1.1  christos 			/* Treat it as a successful short write. */
    157  1.1  christos 			break;
    158  1.1  christos 		}
    159  1.1  christos 		if (charlen == 0)
    160  1.1  christos 			/* XXX: Not sure how else to handle this. */
    161  1.1  christos 			charlen = 1;
    162  1.1  christos 		if (charlen == (size_t)-2) {
    163  1.1  christos 			consumed += len;
    164  1.1  christos 			len = 0;
    165  1.1  christos 		} else {
    166  1.1  christos 			consumed += charlen;
    167  1.1  christos 			buf = (const char *)buf + charlen;
    168  1.1  christos 			len -= charlen;
    169  1.1  christos 			ms->offset++;
    170  1.1  christos 		}
    171  1.1  christos 	}
    172  1.1  christos 	wmemstream_update(ms);
    173  1.1  christos #ifdef DEBUG
    174  1.1  christos 	fprintf(stderr, "WMS: write(%p, %zu) = %zd\n", ms, len, consumed);
    175  1.1  christos #endif
    176  1.1  christos 	return (consumed);
    177  1.1  christos }
    178  1.1  christos 
    179  1.1  christos static off_t
    180  1.1  christos wmemstream_seek(void *cookie, off_t pos, int whence)
    181  1.1  christos {
    182  1.1  christos 	struct wmemstream *ms;
    183  1.1  christos 	size_t old;
    184  1.1  christos 
    185  1.1  christos 	ms = cookie;
    186  1.1  christos 	old = ms->offset;
    187  1.1  christos 	switch (whence) {
    188  1.1  christos 	case SEEK_SET:
    189  1.1  christos 		/* _fseeko() checks for negative offsets. */
    190  1.1  christos 		assert(pos >= 0);
    191  1.2  christos 		ms->offset = off_t_to_size_t(pos);
    192  1.1  christos 		break;
    193  1.1  christos 	case SEEK_CUR:
    194  1.1  christos 		/* This is only called by _ftello(). */
    195  1.1  christos 		assert(pos == 0);
    196  1.1  christos 		break;
    197  1.1  christos 	case SEEK_END:
    198  1.1  christos 		if (pos < 0) {
    199  1.1  christos 			if (pos + (ssize_t)ms->len < 0) {
    200  1.1  christos #ifdef DEBUG
    201  1.1  christos 				fprintf(stderr,
    202  1.1  christos 				    "WMS: bad SEEK_END: pos %jd, len %zd\n",
    203  1.1  christos 				    (intmax_t)pos, ms->len);
    204  1.1  christos #endif
    205  1.1  christos 				errno = EINVAL;
    206  1.1  christos 				return (-1);
    207  1.1  christos 			}
    208  1.1  christos 		} else {
    209  1.1  christos 			if (OFF_MAX - ms->len < (size_t)pos) {
    210  1.1  christos #ifdef DEBUG
    211  1.1  christos 				fprintf(stderr,
    212  1.1  christos 				    "WMS: bad SEEK_END: pos %jd, len %zd\n",
    213  1.1  christos 				    (intmax_t)pos, ms->len);
    214  1.1  christos #endif
    215  1.1  christos 				errno = EOVERFLOW;
    216  1.1  christos 				return (-1);
    217  1.1  christos 			}
    218  1.1  christos 		}
    219  1.2  christos 		ms->offset = off_t_to_size_t(ms->len + pos);
    220  1.1  christos 		break;
    221  1.1  christos 	}
    222  1.1  christos 	/* Reset the multibyte state if a seek changes the position. */
    223  1.1  christos 	if (ms->offset != old)
    224  1.1  christos 		memset(&ms->mbstate, 0, sizeof(ms->mbstate));
    225  1.1  christos 	wmemstream_update(ms);
    226  1.1  christos #ifdef DEBUG
    227  1.1  christos 	fprintf(stderr, "WMS: seek(%p, %jd, %d) %jd -> %jd\n", ms,
    228  1.1  christos 	    (intmax_t)pos, whence, (intmax_t)old, (intmax_t)ms->offset);
    229  1.1  christos #endif
    230  1.1  christos 	return (ms->offset);
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos static int
    234  1.1  christos wmemstream_close(void *cookie)
    235  1.1  christos {
    236  1.1  christos 
    237  1.1  christos 	free(cookie);
    238  1.1  christos 	return (0);
    239  1.1  christos }
    240  1.1  christos 
    241  1.1  christos FILE *
    242  1.1  christos open_wmemstream(wchar_t **bufp, size_t *sizep)
    243  1.1  christos {
    244  1.1  christos 	struct wmemstream *ms;
    245  1.1  christos 	int save_errno;
    246  1.1  christos 	FILE *fp;
    247  1.1  christos 
    248  1.1  christos 	if (bufp == NULL || sizep == NULL) {
    249  1.1  christos 		errno = EINVAL;
    250  1.1  christos 		return (NULL);
    251  1.1  christos 	}
    252  1.1  christos 	*bufp = calloc(1, sizeof(wchar_t));
    253  1.1  christos 	if (*bufp == NULL)
    254  1.1  christos 		return (NULL);
    255  1.1  christos 	ms = malloc(sizeof(*ms));
    256  1.1  christos 	if (ms == NULL) {
    257  1.1  christos 		save_errno = errno;
    258  1.1  christos 		free(*bufp);
    259  1.1  christos 		*bufp = NULL;
    260  1.1  christos 		errno = save_errno;
    261  1.1  christos 		return (NULL);
    262  1.1  christos 	}
    263  1.1  christos 	ms->bufp = bufp;
    264  1.1  christos 	ms->sizep = sizep;
    265  1.1  christos 	ms->len = 0;
    266  1.1  christos 	ms->offset = 0;
    267  1.1  christos 	memset(&ms->mbstate, 0, sizeof(mbstate_t));
    268  1.1  christos 	wmemstream_update(ms);
    269  1.1  christos 	fp = funopen2(ms, NULL, wmemstream_write, wmemstream_seek,
    270  1.1  christos 	    NULL, wmemstream_close);
    271  1.1  christos 	if (fp == NULL) {
    272  1.1  christos 		save_errno = errno;
    273  1.1  christos 		free(ms);
    274  1.1  christos 		free(*bufp);
    275  1.1  christos 		*bufp = NULL;
    276  1.1  christos 		errno = save_errno;
    277  1.1  christos 		return (NULL);
    278  1.1  christos 	}
    279  1.1  christos 	fwide(fp, 1);
    280  1.1  christos 	return (fp);
    281  1.1  christos }
    282