1/* Copyright (C) 1993-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>.
17
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
26
27#include <libioP.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <shlib-compat.h>
31
32/* Prototyped for local functions. */
33static _IO_ssize_t _IO_cookie_read (_IO_FILE* fp, void* buf,
34 _IO_ssize_t size);
35static _IO_ssize_t _IO_cookie_write (_IO_FILE* fp,
36 const void* buf, _IO_ssize_t size);
37static _IO_off64_t _IO_cookie_seek (_IO_FILE *fp, _IO_off64_t offset, int dir);
38static _IO_off64_t _IO_cookie_seekoff (_IO_FILE *fp, _IO_off64_t offset,
39 int dir, int mode);
40static int _IO_cookie_close (_IO_FILE* fp);
41
42static _IO_ssize_t
43_IO_cookie_read (_IO_FILE *fp, void *buf, _IO_ssize_t size)
44{
45 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
46 cookie_read_function_t *read_cb = cfile->__io_functions.read;
47#ifdef PTR_DEMANGLE
48 PTR_DEMANGLE (read_cb);
49#endif
50
51 if (read_cb == NULL)
52 return -1;
53
54 return read_cb (cfile->__cookie, buf, size);
55}
56
57static _IO_ssize_t
58_IO_cookie_write (_IO_FILE *fp, const void *buf, _IO_ssize_t size)
59{
60 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
61 cookie_write_function_t *write_cb = cfile->__io_functions.write;
62#ifdef PTR_DEMANGLE
63 PTR_DEMANGLE (write_cb);
64#endif
65
66 if (write_cb == NULL)
67 {
68 fp->_flags |= _IO_ERR_SEEN;
69 return 0;
70 }
71
72 _IO_ssize_t n = write_cb (cfile->__cookie, buf, size);
73 if (n < size)
74 fp->_flags |= _IO_ERR_SEEN;
75
76 return n;
77}
78
79static _IO_off64_t
80_IO_cookie_seek (_IO_FILE *fp, _IO_off64_t offset, int dir)
81{
82 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
83 cookie_seek_function_t *seek_cb = cfile->__io_functions.seek;
84#ifdef PTR_DEMANGLE
85 PTR_DEMANGLE (seek_cb);
86#endif
87
88 return ((seek_cb == NULL
89 || (seek_cb (cfile->__cookie, &offset, dir)
90 == -1)
91 || offset == (_IO_off64_t) -1)
92 ? _IO_pos_BAD : offset);
93}
94
95static int
96_IO_cookie_close (_IO_FILE *fp)
97{
98 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
99 cookie_close_function_t *close_cb = cfile->__io_functions.close;
100#ifdef PTR_DEMANGLE
101 PTR_DEMANGLE (close_cb);
102#endif
103
104 if (close_cb == NULL)
105 return 0;
106
107 return close_cb (cfile->__cookie);
108}
109
110
111static _IO_off64_t
112_IO_cookie_seekoff (_IO_FILE *fp, _IO_off64_t offset, int dir, int mode)
113{
114 /* We must force the fileops code to always use seek to determine
115 the position. */
116 fp->_offset = _IO_pos_BAD;
117 return _IO_file_seekoff (fp, offset, dir, mode);
118}
119
120
121static const struct _IO_jump_t _IO_cookie_jumps libio_vtable = {
122 JUMP_INIT_DUMMY,
123 JUMP_INIT(finish, _IO_file_finish),
124 JUMP_INIT(overflow, _IO_file_overflow),
125 JUMP_INIT(underflow, _IO_file_underflow),
126 JUMP_INIT(uflow, _IO_default_uflow),
127 JUMP_INIT(pbackfail, _IO_default_pbackfail),
128 JUMP_INIT(xsputn, _IO_file_xsputn),
129 JUMP_INIT(xsgetn, _IO_default_xsgetn),
130 JUMP_INIT(seekoff, _IO_cookie_seekoff),
131 JUMP_INIT(seekpos, _IO_default_seekpos),
132 JUMP_INIT(setbuf, _IO_file_setbuf),
133 JUMP_INIT(sync, _IO_file_sync),
134 JUMP_INIT(doallocate, _IO_file_doallocate),
135 JUMP_INIT(read, _IO_cookie_read),
136 JUMP_INIT(write, _IO_cookie_write),
137 JUMP_INIT(seek, _IO_cookie_seek),
138 JUMP_INIT(close, _IO_cookie_close),
139 JUMP_INIT(stat, _IO_default_stat),
140 JUMP_INIT(showmanyc, _IO_default_showmanyc),
141 JUMP_INIT(imbue, _IO_default_imbue),
142};
143
144
145/* Copy the callbacks from SOURCE to *TARGET, with pointer
146 mangling. */
147static void
148set_callbacks (_IO_cookie_io_functions_t *target,
149 _IO_cookie_io_functions_t source)
150{
151#ifdef PTR_MANGLE
152 PTR_MANGLE (source.read);
153 PTR_MANGLE (source.write);
154 PTR_MANGLE (source.seek);
155 PTR_MANGLE (source.close);
156#endif
157 *target = source;
158}
159
160void
161_IO_cookie_init (struct _IO_cookie_file *cfile, int read_write,
162 void *cookie, _IO_cookie_io_functions_t io_functions)
163{
164 _IO_init_internal (&cfile->__fp.file, 0);
165 _IO_JUMPS (&cfile->__fp) = &_IO_cookie_jumps;
166
167 cfile->__cookie = cookie;
168 set_callbacks (&cfile->__io_functions, io_functions);
169
170 _IO_new_file_init_internal (&cfile->__fp);
171
172 _IO_mask_flags (&cfile->__fp.file, read_write,
173 _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
174
175 cfile->__fp.file._flags2 |= _IO_FLAGS2_NEED_LOCK;
176
177 /* We use a negative number different from -1 for _fileno to mark that
178 this special stream is not associated with a real file, but still has
179 to be treated as such. */
180 cfile->__fp.file._fileno = -2;
181}
182
183
184_IO_FILE *
185_IO_fopencookie (void *cookie, const char *mode,
186 _IO_cookie_io_functions_t io_functions)
187{
188 int read_write;
189 struct locked_FILE
190 {
191 struct _IO_cookie_file cfile;
192#ifdef _IO_MTSAFE_IO
193 _IO_lock_t lock;
194#endif
195 } *new_f;
196
197 switch (*mode++)
198 {
199 case 'r':
200 read_write = _IO_NO_WRITES;
201 break;
202 case 'w':
203 read_write = _IO_NO_READS;
204 break;
205 case 'a':
206 read_write = _IO_NO_READS|_IO_IS_APPENDING;
207 break;
208 default:
209 __set_errno (EINVAL);
210 return NULL;
211 }
212 if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
213 read_write &= _IO_IS_APPENDING;
214
215 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
216 if (new_f == NULL)
217 return NULL;
218#ifdef _IO_MTSAFE_IO
219 new_f->cfile.__fp.file._lock = &new_f->lock;
220#endif
221
222 _IO_cookie_init (&new_f->cfile, read_write, cookie, io_functions);
223
224 return (_IO_FILE *) &new_f->cfile.__fp;
225}
226
227versioned_symbol (libc, _IO_fopencookie, fopencookie, GLIBC_2_2);
228
229#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
230
231static _IO_off64_t _IO_old_cookie_seek (_IO_FILE *fp, _IO_off64_t offset,
232 int dir);
233_IO_FILE * _IO_old_fopencookie (void *cookie, const char *mode,
234 _IO_cookie_io_functions_t io_functions);
235
236static _IO_off64_t
237attribute_compat_text_section
238_IO_old_cookie_seek (_IO_FILE *fp, _IO_off64_t offset, int dir)
239{
240 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
241 int (*seek_cb) (_IO_FILE *, _IO_off_t, int)
242 = (int (*) (_IO_FILE *, _IO_off_t, int)) cfile->__io_functions.seek;;
243#ifdef PTR_DEMANGLE
244 PTR_DEMANGLE (seek_cb);
245#endif
246
247 if (seek_cb == NULL)
248 return _IO_pos_BAD;
249
250 int ret = seek_cb (cfile->__cookie, offset, dir);
251
252 return (ret == -1) ? _IO_pos_BAD : ret;
253}
254
255static const struct _IO_jump_t _IO_old_cookie_jumps libio_vtable = {
256 JUMP_INIT_DUMMY,
257 JUMP_INIT(finish, _IO_file_finish),
258 JUMP_INIT(overflow, _IO_file_overflow),
259 JUMP_INIT(underflow, _IO_file_underflow),
260 JUMP_INIT(uflow, _IO_default_uflow),
261 JUMP_INIT(pbackfail, _IO_default_pbackfail),
262 JUMP_INIT(xsputn, _IO_file_xsputn),
263 JUMP_INIT(xsgetn, _IO_default_xsgetn),
264 JUMP_INIT(seekoff, _IO_cookie_seekoff),
265 JUMP_INIT(seekpos, _IO_default_seekpos),
266 JUMP_INIT(setbuf, _IO_file_setbuf),
267 JUMP_INIT(sync, _IO_file_sync),
268 JUMP_INIT(doallocate, _IO_file_doallocate),
269 JUMP_INIT(read, _IO_cookie_read),
270 JUMP_INIT(write, _IO_cookie_write),
271 JUMP_INIT(seek, _IO_old_cookie_seek),
272 JUMP_INIT(close, _IO_cookie_close),
273 JUMP_INIT(stat, _IO_default_stat),
274 JUMP_INIT(showmanyc, _IO_default_showmanyc),
275 JUMP_INIT(imbue, _IO_default_imbue),
276};
277
278_IO_FILE *
279attribute_compat_text_section
280_IO_old_fopencookie (void *cookie, const char *mode,
281 _IO_cookie_io_functions_t io_functions)
282{
283 _IO_FILE *ret;
284
285 ret = _IO_fopencookie (cookie, mode, io_functions);
286 if (ret != NULL)
287 _IO_JUMPS_FILE_plus (ret) = &_IO_old_cookie_jumps;
288
289 return ret;
290}
291
292compat_symbol (libc, _IO_old_fopencookie, fopencookie, GLIBC_2_0);
293
294#endif
295