1 #include "stdio_impl.h"
17 static off_t
wms_seek(FILE *f
, off_t off
, int whence
)
20 struct cookie
*c
= f
->cookie
;
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
;
32 static size_t wms_write(FILE *f
, const unsigned char *buf
, size_t len
)
34 struct cookie
*c
= f
->cookie
;
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
));
47 len2
= mbsnrtowcs(c
->buf
+c
->pos
, (void *)&buf
, len
, c
->space
-c
->pos
, &c
->mbs
);
48 if (len2
== -1) return 0;
50 if (c
->pos
>= c
->len
) c
->len
= c
->pos
;
55 static int wms_close(FILE *f
)
60 FILE *open_wmemstream(wchar_t **bufp
, size_t *sizep
)
66 if (!(f
=malloc(sizeof *f
+ sizeof *c
))) return 0;
67 if (!(buf
=malloc(sizeof *buf
))) {
71 memset(f
, 0, sizeof *f
+ sizeof *c
);
72 f
->cookie
= c
= (void *)(f
+1);
76 c
->pos
= c
->len
= c
->space
= *sizep
= 0;
82 f
->buf
= (void *)(c
+1);
89 if (!libc
.threaded
) f
->lock
= -1;