]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/open_memstream.c
Libc-1353.11.2.tar.gz
[apple/libc.git] / stdio / FreeBSD / open_memstream.c
1 /*-
2 * Copyright (c) 2013 Hudson River Trading LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <assert.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <os/overflow.h>
35 #ifdef DEBUG
36 #include <stdint.h>
37 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/param.h>
42 #include <wchar.h>
43
44 /* XXX: There is no FPOS_MAX. This assumes fpos_t is an off_t. */
45 #define FPOS_MAX OFF_MAX
46
47 struct memstream {
48 char **bufp;
49 size_t *sizep;
50 ssize_t len;
51 fpos_t offset;
52 };
53
54 static int
55 memstream_grow(struct memstream *ms, fpos_t newoff)
56 {
57 char *buf;
58 ssize_t newsize;
59
60 if (newoff < 0 || newoff >= SSIZE_MAX)
61 newsize = SSIZE_MAX - 1;
62 else
63 newsize = newoff;
64 if (newsize > ms->len) {
65 /*
66 * Grow by 1.5x (15 / 10).
67 */
68 ssize_t growsize;
69 bool ovf = os_mul_overflow(ms->len, 15, &growsize);
70 if (ovf) {
71 growsize = SSIZE_MAX - 1;
72 } else {
73 growsize /= 10;
74 }
75 newsize = MAX(growsize, newsize);
76
77 buf = realloc(*ms->bufp, newsize + 1);
78 if (buf != NULL) {
79 #ifdef DEBUG
80 fprintf(stderr, "MS: %p growing from %zd to %zd\n",
81 ms, ms->len, newsize);
82 #endif
83 memset(buf + ms->len + 1, 0, newsize - ms->len);
84 *ms->bufp = buf;
85 ms->len = newsize;
86 return (1);
87 }
88 return (0);
89 }
90 return (1);
91 }
92
93 static void
94 memstream_update(struct memstream *ms)
95 {
96 assert(ms->len >= 0 && ms->offset >= 0);
97 *ms->sizep = ms->len < ms->offset ? ms->len : ms->offset;
98 }
99
100 static int
101 memstream_write(void *cookie, const char *buf, int len)
102 {
103 struct memstream *ms;
104 ssize_t tocopy;
105
106 ms = cookie;
107 if (!memstream_grow(ms, ms->offset + len))
108 return (-1);
109 tocopy = ms->len - ms->offset;
110 if (len < tocopy)
111 tocopy = len;
112 memcpy(*ms->bufp + ms->offset, buf, tocopy);
113 ms->offset += tocopy;
114 memstream_update(ms);
115 #ifdef DEBUG
116 fprintf(stderr, "MS: write(%p, %d) = %zd\n", ms, len, tocopy);
117 #endif
118 return (tocopy);
119 }
120
121 static fpos_t
122 memstream_seek(void *cookie, fpos_t pos, int whence)
123 {
124 struct memstream *ms;
125 #ifdef DEBUG
126 fpos_t old;
127 #endif
128
129 ms = cookie;
130 #ifdef DEBUG
131 old = ms->offset;
132 #endif
133 switch (whence) {
134 case SEEK_SET:
135 /* _fseeko() checks for negative offsets. */
136 assert(pos >= 0);
137 ms->offset = pos;
138 break;
139 case SEEK_CUR:
140 /* This is only called by _ftello(). */
141 assert(pos == 0);
142 break;
143 case SEEK_END:
144 if (pos < 0) {
145 if (pos + ms->len < 0) {
146 #ifdef DEBUG
147 fprintf(stderr,
148 "MS: bad SEEK_END: pos %jd, len %zd\n",
149 (intmax_t)pos, ms->len);
150 #endif
151 errno = EINVAL;
152 return (-1);
153 }
154 } else {
155 if (FPOS_MAX - ms->len < pos) {
156 #ifdef DEBUG
157 fprintf(stderr,
158 "MS: bad SEEK_END: pos %jd, len %zd\n",
159 (intmax_t)pos, ms->len);
160 #endif
161 errno = EOVERFLOW;
162 return (-1);
163 }
164 }
165 ms->offset = ms->len + pos;
166 break;
167 }
168 memstream_update(ms);
169 #ifdef DEBUG
170 fprintf(stderr, "MS: seek(%p, %jd, %d) %jd -> %jd\n", ms, (intmax_t)pos,
171 whence, (intmax_t)old, (intmax_t)ms->offset);
172 #endif
173 return (ms->offset);
174 }
175
176 static int
177 memstream_close(void *cookie)
178 {
179
180 free(cookie);
181 return (0);
182 }
183
184 FILE *
185 open_memstream(char **bufp, size_t *sizep)
186 {
187 struct memstream *ms;
188 int save_errno;
189 FILE *fp;
190
191 if (bufp == NULL || sizep == NULL) {
192 errno = EINVAL;
193 return (NULL);
194 }
195 *bufp = calloc(1, 1);
196 if (*bufp == NULL)
197 return (NULL);
198 ms = malloc(sizeof(*ms));
199 if (ms == NULL) {
200 save_errno = errno;
201 free(*bufp);
202 *bufp = NULL;
203 errno = save_errno;
204 return (NULL);
205 }
206 ms->bufp = bufp;
207 ms->sizep = sizep;
208 ms->len = 0;
209 ms->offset = 0;
210 memstream_update(ms);
211 fp = funopen(ms, NULL, memstream_write, memstream_seek,
212 memstream_close);
213 if (fp == NULL) {
214 save_errno = errno;
215 free(ms);
216 free(*bufp);
217 *bufp = NULL;
218 errno = save_errno;
219 return (NULL);
220 }
221 fwide(fp, -1);
222 return (fp);
223 }