]>
Commit | Line | Data |
---|---|---|
46e4bd3b | 1 | /* Fmemopen implementation. |
d4697bc9 | 2 | Copyright (C) 2000-2014 Free Software Foundation, Inc. |
46e4bd3b | 3 | This file is part of the GNU C Library. |
7b3ba2c4 | 4 | Contributed by Hanno Mueller, kontakt@hanno.de, 2000. |
46e4bd3b UD |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
46e4bd3b UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
46e4bd3b | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
46e4bd3b UD |
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 | * | |
46e4bd3b UD |
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 | ||
de153e7f | 69 | #include <errno.h> |
46e4bd3b UD |
70 | #include <libio.h> |
71 | #include <stdio.h> | |
72 | #include <stdlib.h> | |
69553a9b | 73 | #include <stdint.h> |
46e4bd3b UD |
74 | #include <string.h> |
75 | #include <sys/types.h> | |
3ba06713 UD |
76 | #include "libioP.h" |
77 | ||
46e4bd3b UD |
78 | |
79 | typedef struct fmemopen_cookie_struct fmemopen_cookie_t; | |
80 | struct fmemopen_cookie_struct | |
81 | { | |
82 | char *buffer; | |
83 | int mybuffer; | |
7b3ba2c4 | 84 | int binmode; |
46e4bd3b UD |
85 | size_t size; |
86 | _IO_off64_t pos; | |
87 | size_t maxpos; | |
88 | }; | |
89 | ||
90 | ||
1a511d31 | 91 | static ssize_t |
46e4bd3b UD |
92 | fmemopen_read (void *cookie, char *b, size_t s) |
93 | { | |
94 | fmemopen_cookie_t *c; | |
95 | ||
96 | c = (fmemopen_cookie_t *) cookie; | |
97 | ||
c6cc2207 | 98 | if (c->pos + s > c->size) |
46e4bd3b | 99 | { |
6dd67bd5 | 100 | if ((size_t) c->pos == c->size) |
de153e7f | 101 | return 0; |
46e4bd3b UD |
102 | s = c->size - c->pos; |
103 | } | |
104 | ||
105 | memcpy (b, &(c->buffer[c->pos]), s); | |
106 | ||
107 | c->pos += s; | |
6dd67bd5 | 108 | if ((size_t) c->pos > c->maxpos) |
46e4bd3b UD |
109 | c->maxpos = c->pos; |
110 | ||
111 | return s; | |
112 | } | |
113 | ||
114 | ||
1a511d31 | 115 | static ssize_t |
46e4bd3b UD |
116 | fmemopen_write (void *cookie, const char *b, size_t s) |
117 | { | |
118 | fmemopen_cookie_t *c; | |
119 | int addnullc; | |
120 | ||
121 | c = (fmemopen_cookie_t *) cookie; | |
122 | ||
7b3ba2c4 | 123 | addnullc = c->binmode == 0 && (s == 0 || b[s - 1] != '\0'); |
46e4bd3b | 124 | |
c6cc2207 | 125 | if (c->pos + s + addnullc > c->size) |
46e4bd3b | 126 | { |
6dd67bd5 | 127 | if ((size_t) (c->pos + addnullc) == c->size) |
de153e7f UD |
128 | { |
129 | __set_errno (ENOSPC); | |
32053042 | 130 | return 0; |
de153e7f | 131 | } |
46e4bd3b UD |
132 | s = c->size - c->pos - addnullc; |
133 | } | |
134 | ||
135 | memcpy (&(c->buffer[c->pos]), b, s); | |
136 | ||
137 | c->pos += s; | |
6dd67bd5 | 138 | if ((size_t) c->pos > c->maxpos) |
46e4bd3b UD |
139 | { |
140 | c->maxpos = c->pos; | |
141 | if (addnullc) | |
142 | c->buffer[c->maxpos] = '\0'; | |
143 | } | |
144 | ||
145 | return s; | |
146 | } | |
147 | ||
148 | ||
1a511d31 | 149 | static int |
de153e7f | 150 | fmemopen_seek (void *cookie, _IO_off64_t *p, int w) |
46e4bd3b UD |
151 | { |
152 | _IO_off64_t np; | |
153 | fmemopen_cookie_t *c; | |
154 | ||
155 | c = (fmemopen_cookie_t *) cookie; | |
156 | ||
157 | switch (w) | |
158 | { | |
46e4bd3b UD |
159 | case SEEK_SET: |
160 | np = *p; | |
161 | break; | |
162 | ||
163 | case SEEK_CUR: | |
164 | np = c->pos + *p; | |
165 | break; | |
166 | ||
167 | case SEEK_END: | |
7b3ba2c4 | 168 | np = (c->binmode ? c->size : c->maxpos) - *p; |
46e4bd3b UD |
169 | break; |
170 | ||
c6cc2207 UD |
171 | default: |
172 | return -1; | |
46e4bd3b UD |
173 | } |
174 | ||
6dd67bd5 | 175 | if (np < 0 || (size_t) np > c->size) |
46e4bd3b UD |
176 | return -1; |
177 | ||
1e7cceb9 | 178 | *p = c->pos = np; |
46e4bd3b | 179 | |
1e7cceb9 | 180 | return 0; |
46e4bd3b UD |
181 | } |
182 | ||
183 | ||
1a511d31 | 184 | static int |
46e4bd3b UD |
185 | fmemopen_close (void *cookie) |
186 | { | |
187 | fmemopen_cookie_t *c; | |
188 | ||
189 | c = (fmemopen_cookie_t *) cookie; | |
190 | ||
3146ad42 AJ |
191 | if (c->mybuffer) |
192 | free (c->buffer); | |
46e4bd3b UD |
193 | free (c); |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
198 | ||
199 | FILE * | |
200 | fmemopen (void *buf, size_t len, const char *mode) | |
201 | { | |
202 | cookie_io_functions_t iof; | |
203 | fmemopen_cookie_t *c; | |
204 | ||
0292b0dd | 205 | if (__builtin_expect (len == 0, 0)) |
1b99d99f UD |
206 | { |
207 | einval: | |
208 | __set_errno (EINVAL); | |
209 | return NULL; | |
210 | } | |
211 | ||
46e4bd3b UD |
212 | c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t)); |
213 | if (c == NULL) | |
214 | return NULL; | |
215 | ||
216 | c->mybuffer = (buf == NULL); | |
217 | ||
218 | if (c->mybuffer) | |
219 | { | |
220 | c->buffer = (char *) malloc (len); | |
221 | if (c->buffer == NULL) | |
222 | { | |
223 | free (c); | |
224 | return NULL; | |
225 | } | |
226 | c->buffer[0] = '\0'; | |
13a804de | 227 | c->maxpos = 0; |
46e4bd3b UD |
228 | } |
229 | else | |
1b99d99f | 230 | { |
0292b0dd UD |
231 | if (__builtin_expect ((uintptr_t) len > -(uintptr_t) buf, 0)) |
232 | { | |
233 | free (c); | |
234 | goto einval; | |
235 | } | |
1b99d99f UD |
236 | |
237 | c->buffer = buf; | |
46e4bd3b | 238 | |
13a804de UD |
239 | if (mode[0] == 'w') |
240 | c->buffer[0] = '\0'; | |
46e4bd3b | 241 | |
13a804de UD |
242 | c->maxpos = strnlen (c->buffer, len); |
243 | } | |
46e4bd3b | 244 | |
13a804de | 245 | c->size = len; |
46e4bd3b UD |
246 | |
247 | if (mode[0] == 'a') | |
c6cc2207 | 248 | c->pos = c->maxpos; |
46e4bd3b | 249 | else |
c6cc2207 | 250 | c->pos = 0; |
46e4bd3b | 251 | |
7b3ba2c4 UD |
252 | c->binmode = mode[0] != '\0' && mode[1] == 'b'; |
253 | ||
46e4bd3b UD |
254 | iof.read = fmemopen_read; |
255 | iof.write = fmemopen_write; | |
256 | iof.seek = fmemopen_seek; | |
257 | iof.close = fmemopen_close; | |
258 | ||
3ba06713 | 259 | return _IO_fopencookie (c, mode, iof); |
46e4bd3b | 260 | } |
a99e59d7 | 261 | libc_hidden_def (fmemopen) |