remove another invalid skip of locking in ungetwc
[oweals/musl.git] / src / stdio / open_wmemstream.c
1 #include "stdio_impl.h"
2 #include <wchar.h>
3 #include <errno.h>
4 #include <limits.h>
5 #include <string.h>
6
7 struct cookie {
8         wchar_t **bufp;
9         size_t *sizep;
10         size_t pos;
11         wchar_t *buf;
12         size_t len;
13         size_t space;
14         mbstate_t mbs;
15 };
16
17 static off_t wms_seek(FILE *f, off_t off, int whence)
18 {
19         ssize_t base;
20         struct cookie *c = f->cookie;
21         if (whence>2U) {
22 fail:
23                 errno = EINVAL;
24                 return -1;
25         }
26         base = (size_t [3]){0, c->pos, c->len}[whence];
27         if (off < -base || off > SSIZE_MAX/4-base) goto fail;
28         memset(&c->mbs, 0, sizeof c->mbs);
29         return c->pos = base+off;
30 }
31
32 static size_t wms_write(FILE *f, const unsigned char *buf, size_t len)
33 {
34         struct cookie *c = f->cookie;
35         size_t len2;
36         wchar_t *newbuf;
37         if (len + c->pos >= c->space) {
38                 len2 = 2*c->space+1 | c->pos+len+1;
39                 if (len2 > SSIZE_MAX/4) return 0;
40                 newbuf = realloc(c->buf, len2*4);
41                 if (!newbuf) return 0;
42                 *c->bufp = c->buf = newbuf;
43                 memset(c->buf + c->space, 0, 4*(len2 - c->space));
44                 c->space = len2;
45         }
46         
47         len2 = mbsnrtowcs(c->buf+c->pos, (void *)&buf, len, c->space-c->pos, &c->mbs);
48         if (len2 == -1) return 0;
49         c->pos += len2;
50         if (c->pos >= c->len) c->len = c->pos;
51         *c->sizep = c->pos;
52         return len;
53 }
54
55 static int wms_close(FILE *f)
56 {
57         return 0;
58 }
59
60 FILE *open_wmemstream(wchar_t **bufp, size_t *sizep)
61 {
62         FILE *f;
63         struct cookie *c;
64         if (!(f=malloc(sizeof *f + sizeof *c))) return 0;
65         memset(f, 0, sizeof *f + sizeof *c);
66         f->cookie = c = (void *)(f+1);
67
68         c->bufp = bufp;
69         c->sizep = sizep;
70         c->pos = c->len = c->space = 0;
71         c->buf = 0;
72
73         f->flags = F_NORD;
74         f->fd = -1;
75         f->buf = (void *)(c+1);
76         f->buf_size = 0;
77         f->lbf = EOF;
78         f->write = wms_write;
79         f->seek = wms_seek;
80         f->close = wms_close;
81
82         if (!libc.threaded) f->lock = -1;
83
84         OFLLOCK();
85         f->next = libc.ofl_head;
86         if (libc.ofl_head) libc.ofl_head->prev = f;
87         libc.ofl_head = f;
88         OFLUNLOCK();
89
90         return f;
91 }