]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/open_memstream.c
2 * Copyright (c) 2013 Hudson River Trading LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
34 #include <os/overflow.h>
41 #include <sys/param.h>
44 /* XXX: There is no FPOS_MAX. This assumes fpos_t is an off_t. */
45 #define FPOS_MAX OFF_MAX
55 memstream_grow(struct memstream
*ms
, fpos_t newoff
)
60 if (newoff
< 0 || newoff
>= SSIZE_MAX
)
61 newsize
= SSIZE_MAX
- 1;
64 if (newsize
> ms
->len
) {
66 * Grow by 1.5x (15 / 10).
69 bool ovf
= os_mul_overflow(ms
->len
, 15, &growsize
);
71 growsize
= SSIZE_MAX
- 1;
75 newsize
= MAX(growsize
, newsize
);
77 buf
= realloc(*ms
->bufp
, newsize
+ 1);
80 fprintf(stderr
, "MS: %p growing from %zd to %zd\n",
81 ms
, ms
->len
, newsize
);
83 memset(buf
+ ms
->len
+ 1, 0, newsize
- ms
->len
);
94 memstream_update(struct memstream
*ms
)
96 assert(ms
->len
>= 0 && ms
->offset
>= 0);
97 *ms
->sizep
= ms
->len
< ms
->offset
? ms
->len
: ms
->offset
;
101 memstream_write(void *cookie
, const char *buf
, int len
)
103 struct memstream
*ms
;
107 if (!memstream_grow(ms
, ms
->offset
+ len
))
109 tocopy
= ms
->len
- ms
->offset
;
112 memcpy(*ms
->bufp
+ ms
->offset
, buf
, tocopy
);
113 ms
->offset
+= tocopy
;
114 memstream_update(ms
);
116 fprintf(stderr
, "MS: write(%p, %d) = %zd\n", ms
, len
, tocopy
);
122 memstream_seek(void *cookie
, fpos_t pos
, int whence
)
124 struct memstream
*ms
;
135 /* _fseeko() checks for negative offsets. */
140 /* This is only called by _ftello(). */
145 if (pos
+ ms
->len
< 0) {
148 "MS: bad SEEK_END: pos %jd, len %zd\n",
149 (intmax_t)pos
, ms
->len
);
155 if (FPOS_MAX
- ms
->len
< pos
) {
158 "MS: bad SEEK_END: pos %jd, len %zd\n",
159 (intmax_t)pos
, ms
->len
);
165 ms
->offset
= ms
->len
+ pos
;
168 memstream_update(ms
);
170 fprintf(stderr
, "MS: seek(%p, %jd, %d) %jd -> %jd\n", ms
, (intmax_t)pos
,
171 whence
, (intmax_t)old
, (intmax_t)ms
->offset
);
177 memstream_close(void *cookie
)
185 open_memstream(char **bufp
, size_t *sizep
)
187 struct memstream
*ms
;
191 if (bufp
== NULL
|| sizep
== NULL
) {
195 *bufp
= calloc(1, 1);
198 ms
= malloc(sizeof(*ms
));
210 memstream_update(ms
);
211 fp
= funopen(ms
, NULL
, memstream_write
, memstream_seek
,