]>
Commit | Line | Data |
---|---|---|
fdb7d390 | 1 | /* Fmemopen implementation. |
04277e02 | 2 | Copyright (C) 2000-2019 Free Software Foundation, Inc. |
fdb7d390 AZ |
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 | |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
fdb7d390 AZ |
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> | |
fdb7d390 AZ |
74 | #include <stdio.h> |
75 | #include <stdlib.h> | |
76 | #include <stdint.h> | |
77 | #include <string.h> | |
78 | #include <sys/types.h> | |
79 | ||
80 | ||
81 | typedef struct fmemopen_cookie_struct fmemopen_cookie_t; | |
82 | struct fmemopen_cookie_struct | |
83 | { | |
84 | char *buffer; | |
85 | int mybuffer; | |
86 | int binmode; | |
87 | size_t size; | |
9964a145 | 88 | off64_t pos; |
fdb7d390 AZ |
89 | size_t maxpos; |
90 | }; | |
91 | ||
92 | ||
93 | static ssize_t | |
94 | fmemopen_read (void *cookie, char *b, size_t s) | |
95 | { | |
96 | fmemopen_cookie_t *c; | |
97 | ||
98 | c = (fmemopen_cookie_t *) cookie; | |
99 | ||
100 | if (c->pos + s > c->size) | |
101 | { | |
102 | if ((size_t) c->pos == c->size) | |
103 | return 0; | |
104 | s = c->size - c->pos; | |
105 | } | |
106 | ||
107 | memcpy (b, &(c->buffer[c->pos]), s); | |
108 | ||
109 | c->pos += s; | |
110 | if ((size_t) c->pos > c->maxpos) | |
111 | c->maxpos = c->pos; | |
112 | ||
113 | return s; | |
114 | } | |
115 | ||
116 | ||
117 | static ssize_t | |
118 | fmemopen_write (void *cookie, const char *b, size_t s) | |
119 | { | |
120 | fmemopen_cookie_t *c; | |
121 | int addnullc; | |
122 | ||
123 | c = (fmemopen_cookie_t *) cookie; | |
124 | ||
125 | addnullc = c->binmode == 0 && (s == 0 || b[s - 1] != '\0'); | |
126 | ||
127 | if (c->pos + s + addnullc > c->size) | |
128 | { | |
129 | if ((size_t) (c->pos + addnullc) >= c->size) | |
130 | { | |
131 | __set_errno (ENOSPC); | |
132 | return 0; | |
133 | } | |
134 | s = c->size - c->pos - addnullc; | |
135 | } | |
136 | ||
137 | memcpy (&(c->buffer[c->pos]), b, s); | |
138 | ||
139 | c->pos += s; | |
140 | if ((size_t) c->pos > c->maxpos) | |
141 | { | |
142 | c->maxpos = c->pos; | |
143 | if (addnullc) | |
144 | c->buffer[c->maxpos] = '\0'; | |
145 | } | |
146 | ||
147 | return s; | |
148 | } | |
149 | ||
150 | ||
151 | static int | |
9964a145 | 152 | fmemopen_seek (void *cookie, off64_t *p, int w) |
fdb7d390 | 153 | { |
9964a145 | 154 | off64_t np; |
fdb7d390 AZ |
155 | fmemopen_cookie_t *c; |
156 | ||
157 | c = (fmemopen_cookie_t *) cookie; | |
158 | ||
159 | switch (w) | |
160 | { | |
161 | case SEEK_SET: | |
162 | np = *p; | |
163 | break; | |
164 | ||
165 | case SEEK_CUR: | |
166 | np = c->pos + *p; | |
167 | break; | |
168 | ||
169 | case SEEK_END: | |
170 | np = (c->binmode ? c->size : c->maxpos) - *p; | |
171 | break; | |
172 | ||
173 | default: | |
174 | return -1; | |
175 | } | |
176 | ||
177 | if (np < 0 || (size_t) np > c->size) | |
178 | return -1; | |
179 | ||
180 | *p = c->pos = np; | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
185 | ||
186 | static int | |
187 | fmemopen_close (void *cookie) | |
188 | { | |
189 | fmemopen_cookie_t *c; | |
190 | ||
191 | c = (fmemopen_cookie_t *) cookie; | |
192 | ||
193 | if (c->mybuffer) | |
194 | free (c->buffer); | |
195 | free (c); | |
196 | ||
197 | return 0; | |
198 | } | |
199 | ||
200 | ||
201 | FILE * | |
202 | __old_fmemopen (void *buf, size_t len, const char *mode) | |
203 | { | |
204 | cookie_io_functions_t iof; | |
205 | fmemopen_cookie_t *c; | |
63e952d9 | 206 | FILE *result; |
fdb7d390 AZ |
207 | |
208 | if (__glibc_unlikely (len == 0)) | |
209 | { | |
210 | einval: | |
211 | __set_errno (EINVAL); | |
212 | return NULL; | |
213 | } | |
214 | ||
215 | c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t)); | |
216 | if (c == NULL) | |
217 | return NULL; | |
218 | ||
219 | c->mybuffer = (buf == NULL); | |
220 | ||
221 | if (c->mybuffer) | |
222 | { | |
223 | c->buffer = (char *) malloc (len); | |
224 | if (c->buffer == NULL) | |
225 | { | |
226 | free (c); | |
227 | return NULL; | |
228 | } | |
229 | c->buffer[0] = '\0'; | |
230 | c->maxpos = 0; | |
231 | } | |
232 | else | |
233 | { | |
234 | if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf)) | |
235 | { | |
236 | free (c); | |
237 | goto einval; | |
238 | } | |
239 | ||
240 | c->buffer = buf; | |
241 | ||
242 | if (mode[0] == 'w') | |
243 | c->buffer[0] = '\0'; | |
244 | ||
245 | c->maxpos = strnlen (c->buffer, len); | |
246 | } | |
247 | ||
248 | c->size = len; | |
249 | ||
250 | if (mode[0] == 'a') | |
251 | c->pos = c->maxpos; | |
252 | else | |
253 | c->pos = 0; | |
254 | ||
255 | c->binmode = mode[0] != '\0' && mode[1] == 'b'; | |
256 | ||
257 | iof.read = fmemopen_read; | |
258 | iof.write = fmemopen_write; | |
259 | iof.seek = fmemopen_seek; | |
260 | iof.close = fmemopen_close; | |
261 | ||
63e952d9 PP |
262 | result = _IO_fopencookie (c, mode, iof); |
263 | if (__glibc_unlikely (result == NULL)) | |
264 | { | |
265 | if (c->mybuffer) | |
266 | free (c->buffer); | |
267 | ||
268 | free (c); | |
269 | } | |
270 | ||
271 | return result; | |
fdb7d390 AZ |
272 | } |
273 | compat_symbol (libc, __old_fmemopen, fmemopen, GLIBC_2_2); | |
274 | #endif |