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