xref: /minix3/lib/libc/stdio/open_memstream.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: open_memstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 Advanced Computing Technologies LLC
5  * Written by: John H. Baldwin <jhb@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #if 0
32 __FBSDID("$FreeBSD: head/lib/libc/stdio/open_memstream.c 247411 2013-02-27 19:50:46Z jhb $");
33 #endif
34 __RCSID("$NetBSD: open_memstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $");
35 
36 #include "namespace.h"
37 #include <assert.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <wchar.h>
44 
45 #define	OFF_MAX	LLONG_MAX
46 
47 struct memstream {
48 	char **bufp;
49 	size_t *sizep;
50 	size_t len;
51 	size_t offset;
52 };
53 
54 static int
memstream_grow(struct memstream * ms,off_t newoff)55 memstream_grow(struct memstream *ms, off_t newoff)
56 {
57 	char *buf;
58 	size_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
memstream_update(struct memstream * ms)82 memstream_update(struct memstream *ms)
83 {
84 
85 	*ms->sizep = ms->len < ms->offset ? ms->len : ms->offset;
86 }
87 
88 static ssize_t
memstream_write(void * cookie,const void * buf,size_t len)89 memstream_write(void *cookie, const void *buf, size_t len)
90 {
91 	struct memstream *ms;
92 	size_t tocopy;
93 	off_t more;
94 
95 	ms = cookie;
96 	more = ms->offset;
97 	more += len;
98 	if (!memstream_grow(ms, more))
99 		return (-1);
100 	tocopy = ms->len - ms->offset;
101 	if (len < tocopy)
102 		tocopy = len;
103 	memcpy(*ms->bufp + ms->offset, buf, tocopy);
104 	ms->offset += tocopy;
105 	memstream_update(ms);
106 #ifdef DEBUG
107 	fprintf(stderr, "MS: write(%p, %zu) = %zu\n", ms, len, tocopy);
108 #endif
109 	return (ssize_t)tocopy;
110 }
111 
112 static off_t
memstream_seek(void * cookie,off_t pos,int whence)113 memstream_seek(void *cookie, off_t pos, int whence)
114 {
115 	struct memstream *ms;
116 #ifdef DEBUG
117 	size_t old;
118 #endif
119 
120 	ms = cookie;
121 #ifdef DEBUG
122 	old = ms->offset;
123 #endif
124 	switch (whence) {
125 	case SEEK_SET:
126 		/* _fseeko() checks for negative offsets. */
127 		assert(pos >= 0);
128 		ms->offset = pos;
129 		break;
130 	case SEEK_CUR:
131 		/* This is only called by _ftello(). */
132 		assert(pos == 0);
133 		break;
134 	case SEEK_END:
135 		if (pos < 0) {
136 			if (pos + (ssize_t)ms->len < 0) {
137 #ifdef DEBUG
138 				fprintf(stderr,
139 				    "MS: bad SEEK_END: pos %jd, len %zu\n",
140 				    (intmax_t)pos, ms->len);
141 #endif
142 				errno = EINVAL;
143 				return (-1);
144 			}
145 		} else {
146 			if (OFF_MAX - (ssize_t)ms->len < pos) {
147 #ifdef DEBUG
148 				fprintf(stderr,
149 				    "MS: bad SEEK_END: pos %jd, len %zu\n",
150 				    (intmax_t)pos, ms->len);
151 #endif
152 				errno = EOVERFLOW;
153 				return (-1);
154 			}
155 		}
156 		ms->offset = ms->len + pos;
157 		break;
158 	}
159 	memstream_update(ms);
160 #ifdef DEBUG
161 	fprintf(stderr, "MS: seek(%p, %jd, %d) %zu -> %zu\n", ms,
162 	    (intmax_t)pos, whence, old, ms->offset);
163 #endif
164 	return (ms->offset);
165 }
166 
167 static int
memstream_close(void * cookie)168 memstream_close(void *cookie)
169 {
170 
171 	free(cookie);
172 	return (0);
173 }
174 
175 FILE *
open_memstream(char ** bufp,size_t * sizep)176 open_memstream(char **bufp, size_t *sizep)
177 {
178 	struct memstream *ms;
179 	int save_errno;
180 	FILE *fp;
181 
182 	if (bufp == NULL || sizep == NULL) {
183 		errno = EINVAL;
184 		return (NULL);
185 	}
186 	*bufp = calloc(1, 1);
187 	if (*bufp == NULL)
188 		return (NULL);
189 	ms = malloc(sizeof(*ms));
190 	if (ms == NULL) {
191 		save_errno = errno;
192 		free(*bufp);
193 		*bufp = NULL;
194 		errno = save_errno;
195 		return (NULL);
196 	}
197 	ms->bufp = bufp;
198 	ms->sizep = sizep;
199 	ms->len = 0;
200 	ms->offset = 0;
201 	memstream_update(ms);
202 	fp = funopen2(ms, NULL, memstream_write, memstream_seek,
203 	    NULL, memstream_close);
204 	if (fp == NULL) {
205 		save_errno = errno;
206 		free(ms);
207 		free(*bufp);
208 		*bufp = NULL;
209 		errno = save_errno;
210 		return (NULL);
211 	}
212 	fwide(fp, -1);
213 	return (fp);
214 }
215