master
  1#include "stdio_impl.h"
  2#include <wchar.h>
  3#include <errno.h>
  4#include <limits.h>
  5#include <string.h>
  6#include <stdlib.h>
  7#include "libc.h"
  8
  9struct cookie {
 10	wchar_t **bufp;
 11	size_t *sizep;
 12	size_t pos;
 13	wchar_t *buf;
 14	size_t len;
 15	size_t space;
 16	mbstate_t mbs;
 17};
 18
 19struct wms_FILE {
 20	FILE f;
 21	struct cookie c;
 22	unsigned char buf[1];
 23};
 24
 25static off_t wms_seek(FILE *f, off_t off, int whence)
 26{
 27	ssize_t base;
 28	struct cookie *c = f->cookie;
 29	if (whence>2U) {
 30fail:
 31		errno = EINVAL;
 32		return -1;
 33	}
 34#ifdef __wasilibc_unmodified_upstream // WASI's SEEK_* constants have different values.
 35	base = (size_t [3]){0, c->pos, c->len}[whence];
 36#else
 37	base = (size_t [3]) {
 38            [SEEK_SET] = 0,
 39            [SEEK_CUR] = c->pos,
 40            [SEEK_END] = c->len
 41        }[whence];
 42#endif
 43	if (off < -base || off > SSIZE_MAX/4-base) goto fail;
 44	memset(&c->mbs, 0, sizeof c->mbs);
 45	return c->pos = base+off;
 46}
 47
 48static size_t wms_write(FILE *f, const unsigned char *buf, size_t len)
 49{
 50	struct cookie *c = f->cookie;
 51	size_t len2 = f->wpos - f->wbase;
 52	wchar_t *newbuf;
 53	if (len2) {
 54		f->wpos = f->wbase;
 55		if (wms_write(f, f->wbase, len2) < len2) return 0;
 56	}
 57	if (len + c->pos >= c->space) {
 58		len2 = 2*c->space+1 | c->pos+len+1;
 59		if (len2 > SSIZE_MAX/4) return 0;
 60		newbuf = realloc(c->buf, len2*4);
 61		if (!newbuf) return 0;
 62		*c->bufp = c->buf = newbuf;
 63		memset(c->buf + c->space, 0, 4*(len2 - c->space));
 64		c->space = len2;
 65	}
 66	
 67	len2 = mbsnrtowcs(c->buf+c->pos, (void *)&buf, len, c->space-c->pos, &c->mbs);
 68	if (len2 == -1) return 0;
 69	c->pos += len2;
 70	if (c->pos >= c->len) c->len = c->pos;
 71	*c->sizep = c->pos;
 72	return len;
 73}
 74
 75static int wms_close(FILE *f)
 76{
 77	return 0;
 78}
 79
 80FILE *open_wmemstream(wchar_t **bufp, size_t *sizep)
 81{
 82	struct wms_FILE *f;
 83	wchar_t *buf;
 84
 85	if (!(f=malloc(sizeof *f))) return 0;
 86	if (!(buf=malloc(sizeof *buf))) {
 87		free(f);
 88		return 0;
 89	}
 90	memset(&f->f, 0, sizeof f->f);
 91	memset(&f->c, 0, sizeof f->c);
 92	f->f.cookie = &f->c;
 93
 94	f->c.bufp = bufp;
 95	f->c.sizep = sizep;
 96	f->c.pos = f->c.len = f->c.space = *sizep = 0;
 97	f->c.buf = *bufp = buf;
 98	*buf = 0;
 99
100	f->f.flags = F_NORD;
101	f->f.fd = -1;
102	f->f.buf = f->buf;
103	f->f.buf_size = 0;
104	f->f.lbf = EOF;
105	f->f.write = wms_write;
106	f->f.seek = wms_seek;
107	f->f.close = wms_close;
108
109#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
110	if (!libc.threaded) f->f.lock = -1;
111#endif
112
113	fwide(&f->f, 1);
114
115	return __ofl_add(&f->f);
116}