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