1 | /* Fmemopen implementation. |
2 | Copyright (C) 2000-2017 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | Contributed by Hanno Mueller, kontakt@hanno.de, 2000. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ |
19 | |
20 | /* |
21 | * fmemopen() - "my" version of a string stream |
22 | * Hanno Mueller, kontakt@hanno.de |
23 | * |
24 | * |
25 | * I needed fmemopen() for an application that I currently work on, |
26 | * but couldn't find it in libio. The following snippet of code is an |
27 | * attempt to implement what glibc's documentation describes. |
28 | * |
29 | * |
30 | * |
31 | * I already see some potential problems: |
32 | * |
33 | * - I never used the "original" fmemopen(). I am sure that "my" |
34 | * fmemopen() behaves differently than the original version. |
35 | * |
36 | * - The documentation doesn't say wether a string stream allows |
37 | * seeks. I checked the old fmemopen implementation in glibc's stdio |
38 | * directory, wasn't quite able to see what is going on in that |
39 | * source, but as far as I understand there was no seek there. For |
40 | * my application, I needed fseek() and ftell(), so it's here. |
41 | * |
42 | * - "append" mode and fseek(p, SEEK_END) have two different ideas |
43 | * about the "end" of the stream. |
44 | * |
45 | * As described in the documentation, when opening the file in |
46 | * "append" mode, the position pointer will be set to the first null |
47 | * character of the string buffer (yet the buffer may already |
48 | * contain more data). For fseek(), the last byte of the buffer is |
49 | * used as the end of the stream. |
50 | * |
51 | * - It is unclear to me what the documentation tries to say when it |
52 | * explains what happens when you use fmemopen with a NULL |
53 | * buffer. |
54 | * |
55 | * Quote: "fmemopen [then] allocates an array SIZE bytes long. This |
56 | * is really only useful if you are going to write things to the |
57 | * buffer and then read them back in again." |
58 | * |
59 | * What does that mean if the original fmemopen() did not allow |
60 | * seeking? How do you read what you just wrote without seeking back |
61 | * to the beginning of the stream? |
62 | * |
63 | * - I think there should be a second version of fmemopen() that does |
64 | * not add null characters for each write. (At least in my |
65 | * application, I am not actually using strings but binary data and |
66 | * so I don't need the stream to add null characters on its own.) |
67 | */ |
68 | |
69 | #include "libioP.h" |
70 | |
71 | #if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_22) |
72 | |
73 | #include <errno.h> |
74 | #include <libio.h> |
75 | #include <stdio.h> |
76 | #include <stdlib.h> |
77 | #include <stdint.h> |
78 | #include <string.h> |
79 | #include <sys/types.h> |
80 | |
81 | |
82 | typedef struct fmemopen_cookie_struct fmemopen_cookie_t; |
83 | struct fmemopen_cookie_struct |
84 | { |
85 | char *buffer; |
86 | int mybuffer; |
87 | int binmode; |
88 | size_t size; |
89 | _IO_off64_t pos; |
90 | size_t maxpos; |
91 | }; |
92 | |
93 | |
94 | static ssize_t |
95 | fmemopen_read (void *cookie, char *b, size_t s) |
96 | { |
97 | fmemopen_cookie_t *c; |
98 | |
99 | c = (fmemopen_cookie_t *) cookie; |
100 | |
101 | if (c->pos + s > c->size) |
102 | { |
103 | if ((size_t) c->pos == c->size) |
104 | return 0; |
105 | s = c->size - c->pos; |
106 | } |
107 | |
108 | memcpy (b, &(c->buffer[c->pos]), s); |
109 | |
110 | c->pos += s; |
111 | if ((size_t) c->pos > c->maxpos) |
112 | c->maxpos = c->pos; |
113 | |
114 | return s; |
115 | } |
116 | |
117 | |
118 | static ssize_t |
119 | fmemopen_write (void *cookie, const char *b, size_t s) |
120 | { |
121 | fmemopen_cookie_t *c; |
122 | int addnullc; |
123 | |
124 | c = (fmemopen_cookie_t *) cookie; |
125 | |
126 | addnullc = c->binmode == 0 && (s == 0 || b[s - 1] != '\0'); |
127 | |
128 | if (c->pos + s + addnullc > c->size) |
129 | { |
130 | if ((size_t) (c->pos + addnullc) >= c->size) |
131 | { |
132 | __set_errno (ENOSPC); |
133 | return 0; |
134 | } |
135 | s = c->size - c->pos - addnullc; |
136 | } |
137 | |
138 | memcpy (&(c->buffer[c->pos]), b, s); |
139 | |
140 | c->pos += s; |
141 | if ((size_t) c->pos > c->maxpos) |
142 | { |
143 | c->maxpos = c->pos; |
144 | if (addnullc) |
145 | c->buffer[c->maxpos] = '\0'; |
146 | } |
147 | |
148 | return s; |
149 | } |
150 | |
151 | |
152 | static int |
153 | fmemopen_seek (void *cookie, _IO_off64_t *p, int w) |
154 | { |
155 | _IO_off64_t np; |
156 | fmemopen_cookie_t *c; |
157 | |
158 | c = (fmemopen_cookie_t *) cookie; |
159 | |
160 | switch (w) |
161 | { |
162 | case SEEK_SET: |
163 | np = *p; |
164 | break; |
165 | |
166 | case SEEK_CUR: |
167 | np = c->pos + *p; |
168 | break; |
169 | |
170 | case SEEK_END: |
171 | np = (c->binmode ? c->size : c->maxpos) - *p; |
172 | break; |
173 | |
174 | default: |
175 | return -1; |
176 | } |
177 | |
178 | if (np < 0 || (size_t) np > c->size) |
179 | return -1; |
180 | |
181 | *p = c->pos = np; |
182 | |
183 | return 0; |
184 | } |
185 | |
186 | |
187 | static int |
188 | fmemopen_close (void *cookie) |
189 | { |
190 | fmemopen_cookie_t *c; |
191 | |
192 | c = (fmemopen_cookie_t *) cookie; |
193 | |
194 | if (c->mybuffer) |
195 | free (c->buffer); |
196 | free (c); |
197 | |
198 | return 0; |
199 | } |
200 | |
201 | |
202 | FILE * |
203 | __old_fmemopen (void *buf, size_t len, const char *mode) |
204 | { |
205 | cookie_io_functions_t iof; |
206 | fmemopen_cookie_t *c; |
207 | FILE *result; |
208 | |
209 | if (__glibc_unlikely (len == 0)) |
210 | { |
211 | einval: |
212 | __set_errno (EINVAL); |
213 | return NULL; |
214 | } |
215 | |
216 | c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t)); |
217 | if (c == NULL) |
218 | return NULL; |
219 | |
220 | c->mybuffer = (buf == NULL); |
221 | |
222 | if (c->mybuffer) |
223 | { |
224 | c->buffer = (char *) malloc (len); |
225 | if (c->buffer == NULL) |
226 | { |
227 | free (c); |
228 | return NULL; |
229 | } |
230 | c->buffer[0] = '\0'; |
231 | c->maxpos = 0; |
232 | } |
233 | else |
234 | { |
235 | if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf)) |
236 | { |
237 | free (c); |
238 | goto einval; |
239 | } |
240 | |
241 | c->buffer = buf; |
242 | |
243 | if (mode[0] == 'w') |
244 | c->buffer[0] = '\0'; |
245 | |
246 | c->maxpos = strnlen (c->buffer, len); |
247 | } |
248 | |
249 | c->size = len; |
250 | |
251 | if (mode[0] == 'a') |
252 | c->pos = c->maxpos; |
253 | else |
254 | c->pos = 0; |
255 | |
256 | c->binmode = mode[0] != '\0' && mode[1] == 'b'; |
257 | |
258 | iof.read = fmemopen_read; |
259 | iof.write = fmemopen_write; |
260 | iof.seek = fmemopen_seek; |
261 | iof.close = fmemopen_close; |
262 | |
263 | result = _IO_fopencookie (c, mode, iof); |
264 | if (__glibc_unlikely (result == NULL)) |
265 | { |
266 | if (c->mybuffer) |
267 | free (c->buffer); |
268 | |
269 | free (c); |
270 | } |
271 | |
272 | return result; |
273 | } |
274 | compat_symbol (libc, __old_fmemopen, fmemopen, GLIBC_2_2); |
275 | #endif |
276 | |