1 | /* Copyright (C) 1993-2023 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 | <https://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 | #include <pointer_guard.h> |
32 | |
33 | static ssize_t |
34 | _IO_cookie_read (FILE *fp, void *buf, ssize_t size) |
35 | { |
36 | struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp; |
37 | cookie_read_function_t *read_cb = cfile->__io_functions.read; |
38 | PTR_DEMANGLE (read_cb); |
39 | |
40 | if (read_cb == NULL) |
41 | return -1; |
42 | |
43 | return read_cb (cfile->__cookie, buf, size); |
44 | } |
45 | |
46 | static ssize_t |
47 | _IO_cookie_write (FILE *fp, const void *buf, ssize_t size) |
48 | { |
49 | struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp; |
50 | cookie_write_function_t *write_cb = cfile->__io_functions.write; |
51 | PTR_DEMANGLE (write_cb); |
52 | |
53 | if (write_cb == NULL) |
54 | { |
55 | fp->_flags |= _IO_ERR_SEEN; |
56 | return 0; |
57 | } |
58 | |
59 | ssize_t n = write_cb (cfile->__cookie, buf, size); |
60 | if (n < size) |
61 | fp->_flags |= _IO_ERR_SEEN; |
62 | |
63 | return n; |
64 | } |
65 | |
66 | static off64_t |
67 | _IO_cookie_seek (FILE *fp, off64_t offset, int dir) |
68 | { |
69 | struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp; |
70 | cookie_seek_function_t *seek_cb = cfile->__io_functions.seek; |
71 | PTR_DEMANGLE (seek_cb); |
72 | |
73 | return ((seek_cb == NULL |
74 | || (seek_cb (cfile->__cookie, &offset, dir) |
75 | == -1) |
76 | || offset == (off64_t) -1) |
77 | ? _IO_pos_BAD : offset); |
78 | } |
79 | |
80 | static int |
81 | _IO_cookie_close (FILE *fp) |
82 | { |
83 | struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp; |
84 | cookie_close_function_t *close_cb = cfile->__io_functions.close; |
85 | PTR_DEMANGLE (close_cb); |
86 | |
87 | if (close_cb == NULL) |
88 | return 0; |
89 | |
90 | return close_cb (cfile->__cookie); |
91 | } |
92 | |
93 | |
94 | static off64_t |
95 | _IO_cookie_seekoff (FILE *fp, off64_t offset, int dir, int mode) |
96 | { |
97 | /* We must force the fileops code to always use seek to determine |
98 | the position. */ |
99 | fp->_offset = _IO_pos_BAD; |
100 | return _IO_file_seekoff (fp, offset, dir, mode); |
101 | } |
102 | |
103 | |
104 | static const struct _IO_jump_t _IO_cookie_jumps libio_vtable = { |
105 | JUMP_INIT_DUMMY, |
106 | JUMP_INIT(finish, _IO_file_finish), |
107 | JUMP_INIT(overflow, _IO_file_overflow), |
108 | JUMP_INIT(underflow, _IO_file_underflow), |
109 | JUMP_INIT(uflow, _IO_default_uflow), |
110 | JUMP_INIT(pbackfail, _IO_default_pbackfail), |
111 | JUMP_INIT(xsputn, _IO_file_xsputn), |
112 | JUMP_INIT(xsgetn, _IO_default_xsgetn), |
113 | JUMP_INIT(seekoff, _IO_cookie_seekoff), |
114 | JUMP_INIT(seekpos, _IO_default_seekpos), |
115 | JUMP_INIT(setbuf, _IO_file_setbuf), |
116 | JUMP_INIT(sync, _IO_file_sync), |
117 | JUMP_INIT(doallocate, _IO_file_doallocate), |
118 | JUMP_INIT(read, _IO_cookie_read), |
119 | JUMP_INIT(write, _IO_cookie_write), |
120 | JUMP_INIT(seek, _IO_cookie_seek), |
121 | JUMP_INIT(close, _IO_cookie_close), |
122 | JUMP_INIT(stat, _IO_default_stat), |
123 | JUMP_INIT(showmanyc, _IO_default_showmanyc), |
124 | JUMP_INIT(imbue, _IO_default_imbue), |
125 | }; |
126 | |
127 | |
128 | /* Copy the callbacks from SOURCE to *TARGET, with pointer |
129 | mangling. */ |
130 | static void |
131 | set_callbacks (cookie_io_functions_t *target, |
132 | cookie_io_functions_t source) |
133 | { |
134 | PTR_MANGLE (source.read); |
135 | PTR_MANGLE (source.write); |
136 | PTR_MANGLE (source.seek); |
137 | PTR_MANGLE (source.close); |
138 | *target = source; |
139 | } |
140 | |
141 | void |
142 | _IO_cookie_init (struct _IO_cookie_file *cfile, int read_write, |
143 | void *cookie, cookie_io_functions_t io_functions) |
144 | { |
145 | _IO_init_internal (&cfile->__fp.file, 0); |
146 | _IO_JUMPS (&cfile->__fp) = &_IO_cookie_jumps; |
147 | |
148 | cfile->__cookie = cookie; |
149 | set_callbacks (&cfile->__io_functions, io_functions); |
150 | |
151 | _IO_new_file_init_internal (&cfile->__fp); |
152 | |
153 | _IO_mask_flags (&cfile->__fp.file, read_write, |
154 | _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING); |
155 | |
156 | cfile->__fp.file._flags2 |= _IO_FLAGS2_NEED_LOCK; |
157 | |
158 | /* We use a negative number different from -1 for _fileno to mark that |
159 | this special stream is not associated with a real file, but still has |
160 | to be treated as such. */ |
161 | cfile->__fp.file._fileno = -2; |
162 | } |
163 | |
164 | |
165 | FILE * |
166 | _IO_fopencookie (void *cookie, const char *mode, |
167 | cookie_io_functions_t io_functions) |
168 | { |
169 | int read_write; |
170 | struct locked_FILE |
171 | { |
172 | struct _IO_cookie_file cfile; |
173 | #ifdef _IO_MTSAFE_IO |
174 | _IO_lock_t lock; |
175 | #endif |
176 | } *new_f; |
177 | |
178 | switch (*mode++) |
179 | { |
180 | case 'r': |
181 | read_write = _IO_NO_WRITES; |
182 | break; |
183 | case 'w': |
184 | read_write = _IO_NO_READS; |
185 | break; |
186 | case 'a': |
187 | read_write = _IO_NO_READS|_IO_IS_APPENDING; |
188 | break; |
189 | default: |
190 | __set_errno (EINVAL); |
191 | return NULL; |
192 | } |
193 | if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+')) |
194 | read_write &= _IO_IS_APPENDING; |
195 | |
196 | new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE)); |
197 | if (new_f == NULL) |
198 | return NULL; |
199 | #ifdef _IO_MTSAFE_IO |
200 | new_f->cfile.__fp.file._lock = &new_f->lock; |
201 | #endif |
202 | |
203 | _IO_cookie_init (&new_f->cfile, read_write, cookie, io_functions); |
204 | |
205 | return (FILE *) &new_f->cfile.__fp; |
206 | } |
207 | |
208 | versioned_symbol (libc, _IO_fopencookie, fopencookie, GLIBC_2_2); |
209 | |
210 | #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2) |
211 | |
212 | static off64_t |
213 | attribute_compat_text_section |
214 | _IO_old_cookie_seek (FILE *fp, off64_t offset, int dir) |
215 | { |
216 | struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp; |
217 | int (*seek_cb) (FILE *, off_t, int) |
218 | = (int (*) (FILE *, off_t, int)) cfile->__io_functions.seek; |
219 | PTR_DEMANGLE (seek_cb); |
220 | |
221 | if (seek_cb == NULL) |
222 | return _IO_pos_BAD; |
223 | |
224 | int ret = seek_cb (cfile->__cookie, offset, dir); |
225 | |
226 | return (ret == -1) ? _IO_pos_BAD : ret; |
227 | } |
228 | |
229 | static const struct _IO_jump_t _IO_old_cookie_jumps libio_vtable = { |
230 | JUMP_INIT_DUMMY, |
231 | JUMP_INIT(finish, _IO_file_finish), |
232 | JUMP_INIT(overflow, _IO_file_overflow), |
233 | JUMP_INIT(underflow, _IO_file_underflow), |
234 | JUMP_INIT(uflow, _IO_default_uflow), |
235 | JUMP_INIT(pbackfail, _IO_default_pbackfail), |
236 | JUMP_INIT(xsputn, _IO_file_xsputn), |
237 | JUMP_INIT(xsgetn, _IO_default_xsgetn), |
238 | JUMP_INIT(seekoff, _IO_cookie_seekoff), |
239 | JUMP_INIT(seekpos, _IO_default_seekpos), |
240 | JUMP_INIT(setbuf, _IO_file_setbuf), |
241 | JUMP_INIT(sync, _IO_file_sync), |
242 | JUMP_INIT(doallocate, _IO_file_doallocate), |
243 | JUMP_INIT(read, _IO_cookie_read), |
244 | JUMP_INIT(write, _IO_cookie_write), |
245 | JUMP_INIT(seek, _IO_old_cookie_seek), |
246 | JUMP_INIT(close, _IO_cookie_close), |
247 | JUMP_INIT(stat, _IO_default_stat), |
248 | JUMP_INIT(showmanyc, _IO_default_showmanyc), |
249 | JUMP_INIT(imbue, _IO_default_imbue), |
250 | }; |
251 | |
252 | FILE * |
253 | attribute_compat_text_section |
254 | _IO_old_fopencookie (void *cookie, const char *mode, |
255 | cookie_io_functions_t io_functions) |
256 | { |
257 | FILE *ret; |
258 | |
259 | ret = _IO_fopencookie (cookie, mode, io_functions); |
260 | if (ret != NULL) |
261 | _IO_JUMPS_FILE_plus (ret) = &_IO_old_cookie_jumps; |
262 | |
263 | return ret; |
264 | } |
265 | |
266 | compat_symbol (libc, _IO_old_fopencookie, fopencookie, GLIBC_2_0); |
267 | |
268 | #endif |
269 | |