xref: /freebsd/lib/libc/stdio/open_memstream.c (revision a0ee8cc6)
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 "namespace.h"
32 #include <assert.h>
33 #include <errno.h>
34 #include <limits.h>
35 #ifdef DEBUG
36 #include <stdint.h>
37 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <wchar.h>
42 #include "un-namespace.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 		buf = realloc(*ms->bufp, newsize + 1);
66 		if (buf != NULL) {
67 #ifdef DEBUG
68 			fprintf(stderr, "MS: %p growing from %zd to %zd\n",
69 			    ms, ms->len, newsize);
70 #endif
71 			memset(buf + ms->len + 1, 0, newsize - ms->len);
72 			*ms->bufp = buf;
73 			ms->len = newsize;
74 			return (1);
75 		}
76 		return (0);
77 	}
78 	return (1);
79 }
80 
81 static void
82 memstream_update(struct memstream *ms)
83 {
84 
85 	assert(ms->len >= 0 && ms->offset >= 0);
86 	*ms->sizep = ms->len < ms->offset ? ms->len : ms->offset;
87 }
88 
89 static int
90 memstream_write(void *cookie, const char *buf, int len)
91 {
92 	struct memstream *ms;
93 	ssize_t tocopy;
94 
95 	ms = cookie;
96 	if (!memstream_grow(ms, ms->offset + len))
97 		return (-1);
98 	tocopy = ms->len - ms->offset;
99 	if (len < tocopy)
100 		tocopy = len;
101 	memcpy(*ms->bufp + ms->offset, buf, tocopy);
102 	ms->offset += tocopy;
103 	memstream_update(ms);
104 #ifdef DEBUG
105 	fprintf(stderr, "MS: write(%p, %d) = %zd\n", ms, len, tocopy);
106 #endif
107 	return (tocopy);
108 }
109 
110 static fpos_t
111 memstream_seek(void *cookie, fpos_t pos, int whence)
112 {
113 	struct memstream *ms;
114 #ifdef DEBUG
115 	fpos_t old;
116 #endif
117 
118 	ms = cookie;
119 #ifdef DEBUG
120 	old = ms->offset;
121 #endif
122 	switch (whence) {
123 	case SEEK_SET:
124 		/* _fseeko() checks for negative offsets. */
125 		assert(pos >= 0);
126 		ms->offset = pos;
127 		break;
128 	case SEEK_CUR:
129 		/* This is only called by _ftello(). */
130 		assert(pos == 0);
131 		break;
132 	case SEEK_END:
133 		if (pos < 0) {
134 			if (pos + ms->len < 0) {
135 #ifdef DEBUG
136 				fprintf(stderr,
137 				    "MS: bad SEEK_END: pos %jd, len %zd\n",
138 				    (intmax_t)pos, ms->len);
139 #endif
140 				errno = EINVAL;
141 				return (-1);
142 			}
143 		} else {
144 			if (FPOS_MAX - ms->len < pos) {
145 #ifdef DEBUG
146 				fprintf(stderr,
147 				    "MS: bad SEEK_END: pos %jd, len %zd\n",
148 				    (intmax_t)pos, ms->len);
149 #endif
150 				errno = EOVERFLOW;
151 				return (-1);
152 			}
153 		}
154 		ms->offset = ms->len + pos;
155 		break;
156 	}
157 	memstream_update(ms);
158 #ifdef DEBUG
159 	fprintf(stderr, "MS: seek(%p, %jd, %d) %jd -> %jd\n", ms, (intmax_t)pos,
160 	    whence, (intmax_t)old, (intmax_t)ms->offset);
161 #endif
162 	return (ms->offset);
163 }
164 
165 static int
166 memstream_close(void *cookie)
167 {
168 
169 	free(cookie);
170 	return (0);
171 }
172 
173 FILE *
174 open_memstream(char **bufp, size_t *sizep)
175 {
176 	struct memstream *ms;
177 	int save_errno;
178 	FILE *fp;
179 
180 	if (bufp == NULL || sizep == NULL) {
181 		errno = EINVAL;
182 		return (NULL);
183 	}
184 	*bufp = calloc(1, 1);
185 	if (*bufp == NULL)
186 		return (NULL);
187 	ms = malloc(sizeof(*ms));
188 	if (ms == NULL) {
189 		save_errno = errno;
190 		free(*bufp);
191 		*bufp = NULL;
192 		errno = save_errno;
193 		return (NULL);
194 	}
195 	ms->bufp = bufp;
196 	ms->sizep = sizep;
197 	ms->len = 0;
198 	ms->offset = 0;
199 	memstream_update(ms);
200 	fp = funopen(ms, NULL, memstream_write, memstream_seek,
201 	    memstream_close);
202 	if (fp == NULL) {
203 		save_errno = errno;
204 		free(ms);
205 		free(*bufp);
206 		*bufp = NULL;
207 		errno = save_errno;
208 		return (NULL);
209 	}
210 	fwide(fp, -1);
211 	return (fp);
212 }
213