re_search_internal: Avoid overflow in computing re_malloc buffer size
[glibc.git] / libio / wfileops.c
blob503d341a96df505cb659a3bc2324259a82089ef4
1 /* Copyright (C) 1993,1995,1997-2004,2006,2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Ulrich Drepper <drepper@cygnus.com>.
4 Based on the single byte version by Per Bothner <bothner@cygnus.com>.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 As a special exception, if you link the code in this file with
22 files compiled with a GNU compiler to produce an executable,
23 that does not cause the resulting executable to be covered by
24 the GNU Lesser General Public License. This exception does not
25 however invalidate any other reasons why the executable file
26 might be covered by the GNU Lesser General Public License.
27 This exception applies to code released by its copyright holders
28 in files containing the exception. */
30 #include <assert.h>
31 #include <libioP.h>
32 #include <wchar.h>
33 #include <gconv.h>
34 #include <stdlib.h>
35 #include <string.h>
38 #ifndef _LIBC
39 # define _IO_new_do_write _IO_do_write
40 # define _IO_new_file_attach _IO_file_attach
41 # define _IO_new_file_close_it _IO_file_close_it
42 # define _IO_new_file_finish _IO_file_finish
43 # define _IO_new_file_fopen _IO_file_fopen
44 # define _IO_new_file_init _IO_file_init
45 # define _IO_new_file_setbuf _IO_file_setbuf
46 # define _IO_new_file_sync _IO_file_sync
47 # define _IO_new_file_overflow _IO_file_overflow
48 # define _IO_new_file_seekoff _IO_file_seekoff
49 # define _IO_new_file_underflow _IO_file_underflow
50 # define _IO_new_file_write _IO_file_write
51 # define _IO_new_file_xsputn _IO_file_xsputn
52 #endif
55 /* Convert TO_DO wide character from DATA to FP.
56 Then mark FP as having empty buffers. */
57 int
58 _IO_wdo_write (fp, data, to_do)
59 _IO_FILE *fp;
60 const wchar_t *data;
61 _IO_size_t to_do;
63 struct _IO_codecvt *cc = fp->_codecvt;
65 if (to_do > 0)
67 if (fp->_IO_write_end == fp->_IO_write_ptr
68 && fp->_IO_write_end != fp->_IO_write_base)
70 if (_IO_new_do_write (fp, fp->_IO_write_base,
71 fp->_IO_write_ptr - fp->_IO_write_base) == EOF)
72 return WEOF;
77 enum __codecvt_result result;
78 const wchar_t *new_data;
80 /* Now convert from the internal format into the external buffer. */
81 result = (*cc->__codecvt_do_out) (cc, &fp->_wide_data->_IO_state,
82 data, data + to_do, &new_data,
83 fp->_IO_write_ptr,
84 fp->_IO_buf_end,
85 &fp->_IO_write_ptr);
87 /* Write out what we produced so far. */
88 if (_IO_new_do_write (fp, fp->_IO_write_base,
89 fp->_IO_write_ptr - fp->_IO_write_base) == EOF)
90 /* Something went wrong. */
91 return WEOF;
93 to_do -= new_data - data;
95 /* Next see whether we had problems during the conversion. If yes,
96 we cannot go on. */
97 if (result != __codecvt_ok
98 && (result != __codecvt_partial || new_data - data == 0))
99 break;
101 data = new_data;
103 while (to_do > 0);
106 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base,
107 fp->_wide_data->_IO_buf_base);
108 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr
109 = fp->_wide_data->_IO_buf_base;
110 fp->_wide_data->_IO_write_end = ((fp->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
111 ? fp->_wide_data->_IO_buf_base
112 : fp->_wide_data->_IO_buf_end);
114 return to_do == 0 ? 0 : WEOF;
116 INTDEF(_IO_wdo_write)
119 wint_t
120 _IO_wfile_underflow (fp)
121 _IO_FILE *fp;
123 struct _IO_codecvt *cd;
124 enum __codecvt_result status;
125 _IO_ssize_t count;
127 if (__builtin_expect (fp->_flags & _IO_NO_READS, 0))
129 fp->_flags |= _IO_ERR_SEEN;
130 __set_errno (EBADF);
131 return WEOF;
133 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
134 return *fp->_wide_data->_IO_read_ptr;
136 cd = fp->_codecvt;
138 /* Maybe there is something left in the external buffer. */
139 if (fp->_IO_read_ptr < fp->_IO_read_end)
141 /* There is more in the external. Convert it. */
142 const char *read_stop = (const char *) fp->_IO_read_ptr;
144 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
145 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
146 fp->_wide_data->_IO_buf_base;
147 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
148 fp->_IO_read_ptr, fp->_IO_read_end,
149 &read_stop,
150 fp->_wide_data->_IO_read_ptr,
151 fp->_wide_data->_IO_buf_end,
152 &fp->_wide_data->_IO_read_end);
154 fp->_IO_read_base = fp->_IO_read_ptr;
155 fp->_IO_read_ptr = (char *) read_stop;
157 /* If we managed to generate some text return the next character. */
158 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
159 return *fp->_wide_data->_IO_read_ptr;
161 if (status == __codecvt_error)
163 __set_errno (EILSEQ);
164 fp->_flags |= _IO_ERR_SEEN;
165 return WEOF;
168 /* Move the remaining content of the read buffer to the beginning. */
169 memmove (fp->_IO_buf_base, fp->_IO_read_ptr,
170 fp->_IO_read_end - fp->_IO_read_ptr);
171 fp->_IO_read_end = (fp->_IO_buf_base
172 + (fp->_IO_read_end - fp->_IO_read_ptr));
173 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base;
175 else
176 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
177 fp->_IO_buf_base;
179 if (fp->_IO_buf_base == NULL)
181 /* Maybe we already have a push back pointer. */
182 if (fp->_IO_save_base != NULL)
184 free (fp->_IO_save_base);
185 fp->_flags &= ~_IO_IN_BACKUP;
187 INTUSE(_IO_doallocbuf) (fp);
189 fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
190 fp->_IO_buf_base;
193 fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end =
194 fp->_IO_buf_base;
196 if (fp->_wide_data->_IO_buf_base == NULL)
198 /* Maybe we already have a push back pointer. */
199 if (fp->_wide_data->_IO_save_base != NULL)
201 free (fp->_wide_data->_IO_save_base);
202 fp->_flags &= ~_IO_IN_BACKUP;
204 INTUSE(_IO_wdoallocbuf) (fp);
207 /* Flush all line buffered files before reading. */
208 /* FIXME This can/should be moved to genops ?? */
209 if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
211 #if 0
212 INTUSE(_IO_flush_all_linebuffered) ();
213 #else
214 /* We used to flush all line-buffered stream. This really isn't
215 required by any standard. My recollection is that
216 traditional Unix systems did this for stdout. stderr better
217 not be line buffered. So we do just that here
218 explicitly. --drepper */
219 _IO_acquire_lock (_IO_stdout);
221 if ((_IO_stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
222 == (_IO_LINKED | _IO_LINE_BUF))
223 _IO_OVERFLOW (_IO_stdout, EOF);
225 _IO_release_lock (_IO_stdout);
226 #endif
229 INTUSE(_IO_switch_to_get_mode) (fp);
231 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
232 fp->_wide_data->_IO_buf_base;
233 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_buf_base;
234 fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr =
235 fp->_wide_data->_IO_write_end = fp->_wide_data->_IO_buf_base;
237 const char *read_ptr_copy;
238 char accbuf[MB_LEN_MAX];
239 size_t naccbuf = 0;
240 again:
241 count = _IO_SYSREAD (fp, fp->_IO_read_end,
242 fp->_IO_buf_end - fp->_IO_read_end);
243 if (count <= 0)
245 if (count == 0 && naccbuf == 0)
246 fp->_flags |= _IO_EOF_SEEN;
247 else
248 fp->_flags |= _IO_ERR_SEEN, count = 0;
250 fp->_IO_read_end += count;
251 if (count == 0)
253 if (naccbuf != 0)
254 /* There are some bytes in the external buffer but they don't
255 convert to anything. */
256 __set_errno (EILSEQ);
257 return WEOF;
259 if (fp->_offset != _IO_pos_BAD)
260 _IO_pos_adjust (fp->_offset, count);
262 /* Now convert the read input. */
263 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
264 fp->_IO_read_base = fp->_IO_read_ptr;
265 const char *from = fp->_IO_read_ptr;
266 const char *to = fp->_IO_read_end;
267 size_t to_copy = count;
268 if (__builtin_expect (naccbuf != 0, 0))
270 to_copy = MIN (sizeof (accbuf) - naccbuf, count);
271 to = __mempcpy (&accbuf[naccbuf], from, to_copy);
272 naccbuf += to_copy;
273 from = accbuf;
275 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
276 from, to, &read_ptr_copy,
277 fp->_wide_data->_IO_read_end,
278 fp->_wide_data->_IO_buf_end,
279 &fp->_wide_data->_IO_read_end);
281 if (__builtin_expect (naccbuf != 0, 0))
282 fp->_IO_read_ptr += MAX (0, read_ptr_copy - &accbuf[naccbuf - to_copy]);
283 else
284 fp->_IO_read_ptr = (char *) read_ptr_copy;
285 if (fp->_wide_data->_IO_read_end == fp->_wide_data->_IO_buf_base)
287 if (status == __codecvt_error)
289 out_eilseq:
290 __set_errno (EILSEQ);
291 fp->_flags |= _IO_ERR_SEEN;
292 return WEOF;
295 /* The read bytes make no complete character. Try reading again. */
296 assert (status == __codecvt_partial);
298 if (naccbuf == 0)
300 if (fp->_IO_read_base < fp->_IO_read_ptr)
302 /* Partially used the buffer for some input data that
303 produces no output. */
304 size_t avail = fp->_IO_read_end - fp->_IO_read_ptr;
305 memmove (fp->_IO_read_base, fp->_IO_read_ptr, avail);
306 fp->_IO_read_ptr = fp->_IO_read_base;
307 fp->_IO_read_end -= avail;
308 goto again;
310 naccbuf = fp->_IO_read_end - fp->_IO_read_ptr;
311 if (naccbuf >= sizeof (accbuf))
312 goto out_eilseq;
314 memcpy (accbuf, fp->_IO_read_ptr, naccbuf);
316 else
318 size_t used = read_ptr_copy - accbuf;
319 if (used > 0)
321 memmove (accbuf, read_ptr_copy, naccbuf - used);
322 naccbuf -= used;
325 if (naccbuf == sizeof (accbuf))
326 goto out_eilseq;
329 fp->_IO_read_ptr = fp->_IO_read_end = fp->_IO_read_base;
331 goto again;
334 return *fp->_wide_data->_IO_read_ptr;
336 INTDEF(_IO_wfile_underflow)
339 static wint_t
340 _IO_wfile_underflow_mmap (_IO_FILE *fp)
342 struct _IO_codecvt *cd;
343 enum __codecvt_result status;
344 const char *read_stop;
346 if (__builtin_expect (fp->_flags & _IO_NO_READS, 0))
348 fp->_flags |= _IO_ERR_SEEN;
349 __set_errno (EBADF);
350 return WEOF;
352 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
353 return *fp->_wide_data->_IO_read_ptr;
355 cd = fp->_codecvt;
357 /* Maybe there is something left in the external buffer. */
358 if (fp->_IO_read_ptr >= fp->_IO_read_end
359 /* No. But maybe the read buffer is not fully set up. */
360 && _IO_file_underflow_mmap (fp) == EOF)
361 /* Nothing available. _IO_file_underflow_mmap has set the EOF or error
362 flags as appropriate. */
363 return WEOF;
365 /* There is more in the external. Convert it. */
366 read_stop = (const char *) fp->_IO_read_ptr;
368 if (fp->_wide_data->_IO_buf_base == NULL)
370 /* Maybe we already have a push back pointer. */
371 if (fp->_wide_data->_IO_save_base != NULL)
373 free (fp->_wide_data->_IO_save_base);
374 fp->_flags &= ~_IO_IN_BACKUP;
376 INTUSE(_IO_wdoallocbuf) (fp);
379 fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
380 fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
381 fp->_wide_data->_IO_buf_base;
382 status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
383 fp->_IO_read_ptr, fp->_IO_read_end,
384 &read_stop,
385 fp->_wide_data->_IO_read_ptr,
386 fp->_wide_data->_IO_buf_end,
387 &fp->_wide_data->_IO_read_end);
389 fp->_IO_read_ptr = (char *) read_stop;
391 /* If we managed to generate some text return the next character. */
392 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
393 return *fp->_wide_data->_IO_read_ptr;
395 /* There is some garbage at the end of the file. */
396 __set_errno (EILSEQ);
397 fp->_flags |= _IO_ERR_SEEN;
398 return WEOF;
401 static wint_t
402 _IO_wfile_underflow_maybe_mmap (_IO_FILE *fp)
404 /* This is the first read attempt. Doing the underflow will choose mmap
405 or vanilla operations and then punt to the chosen underflow routine.
406 Then we can punt to ours. */
407 if (_IO_file_underflow_maybe_mmap (fp) == EOF)
408 return WEOF;
410 return _IO_WUNDERFLOW (fp);
414 wint_t
415 _IO_wfile_overflow (f, wch)
416 _IO_FILE *f;
417 wint_t wch;
419 if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
421 f->_flags |= _IO_ERR_SEEN;
422 __set_errno (EBADF);
423 return WEOF;
425 /* If currently reading or no buffer allocated. */
426 if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0)
428 /* Allocate a buffer if needed. */
429 if (f->_wide_data->_IO_write_base == 0)
431 INTUSE(_IO_wdoallocbuf) (f);
432 _IO_wsetg (f, f->_wide_data->_IO_buf_base,
433 f->_wide_data->_IO_buf_base, f->_wide_data->_IO_buf_base);
435 if (f->_IO_write_base == NULL)
437 INTUSE(_IO_doallocbuf) (f);
438 _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base);
441 else
443 /* Otherwise must be currently reading. If _IO_read_ptr
444 (and hence also _IO_read_end) is at the buffer end,
445 logically slide the buffer forwards one block (by setting
446 the read pointers to all point at the beginning of the
447 block). This makes room for subsequent output.
448 Otherwise, set the read pointers to _IO_read_end (leaving
449 that alone, so it can continue to correspond to the
450 external position). */
451 if (f->_wide_data->_IO_read_ptr == f->_wide_data->_IO_buf_end)
453 f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
454 f->_wide_data->_IO_read_end = f->_wide_data->_IO_read_ptr =
455 f->_wide_data->_IO_buf_base;
458 f->_wide_data->_IO_write_ptr = f->_wide_data->_IO_read_ptr;
459 f->_wide_data->_IO_write_base = f->_wide_data->_IO_write_ptr;
460 f->_wide_data->_IO_write_end = f->_wide_data->_IO_buf_end;
461 f->_wide_data->_IO_read_base = f->_wide_data->_IO_read_ptr =
462 f->_wide_data->_IO_read_end;
464 f->_IO_write_ptr = f->_IO_read_ptr;
465 f->_IO_write_base = f->_IO_write_ptr;
466 f->_IO_write_end = f->_IO_buf_end;
467 f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
469 f->_flags |= _IO_CURRENTLY_PUTTING;
470 if (f->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
471 f->_wide_data->_IO_write_end = f->_wide_data->_IO_write_ptr;
473 if (wch == WEOF)
474 return _IO_do_flush (f);
475 if (f->_wide_data->_IO_write_ptr == f->_wide_data->_IO_buf_end)
476 /* Buffer is really full */
477 if (_IO_do_flush (f) == EOF)
478 return WEOF;
479 *f->_wide_data->_IO_write_ptr++ = wch;
480 if ((f->_flags & _IO_UNBUFFERED)
481 || ((f->_flags & _IO_LINE_BUF) && wch == L'\n'))
482 if (_IO_do_flush (f) == EOF)
483 return WEOF;
484 return wch;
486 INTDEF(_IO_wfile_overflow)
488 wint_t
489 _IO_wfile_sync (fp)
490 _IO_FILE *fp;
492 _IO_ssize_t delta;
493 wint_t retval = 0;
495 /* char* ptr = cur_ptr(); */
496 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base)
497 if (_IO_do_flush (fp))
498 return WEOF;
499 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_end;
500 if (delta != 0)
502 /* We have to find out how many bytes we have to go back in the
503 external buffer. */
504 struct _IO_codecvt *cv = fp->_codecvt;
505 _IO_off64_t new_pos;
507 int clen = (*cv->__codecvt_do_encoding) (cv);
509 if (clen > 0)
510 /* It is easy, a fixed number of input bytes are used for each
511 wide character. */
512 delta *= clen;
513 else
515 /* We have to find out the hard way how much to back off.
516 To do this we determine how much input we needed to
517 generate the wide characters up to the current reading
518 position. */
519 int nread;
521 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
522 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
523 fp->_IO_read_base,
524 fp->_IO_read_end, delta);
525 fp->_IO_read_ptr = fp->_IO_read_base + nread;
526 delta = -(fp->_IO_read_end - fp->_IO_read_base - nread);
529 new_pos = _IO_SYSSEEK (fp, delta, 1);
530 if (new_pos != (_IO_off64_t) EOF)
532 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
533 fp->_IO_read_end = fp->_IO_read_ptr;
535 #ifdef ESPIPE
536 else if (errno == ESPIPE)
537 ; /* Ignore error from unseekable devices. */
538 #endif
539 else
540 retval = WEOF;
542 if (retval != WEOF)
543 fp->_offset = _IO_pos_BAD;
544 /* FIXME: Cleanup - can this be shared? */
545 /* setg(base(), ptr, ptr); */
546 return retval;
548 INTDEF(_IO_wfile_sync)
550 _IO_off64_t
551 _IO_wfile_seekoff (fp, offset, dir, mode)
552 _IO_FILE *fp;
553 _IO_off64_t offset;
554 int dir;
555 int mode;
557 _IO_off64_t result;
558 _IO_off64_t delta, new_offset;
559 long int count;
560 /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
561 offset of the underlying file must be exact. */
562 int must_be_exact = ((fp->_wide_data->_IO_read_base
563 == fp->_wide_data->_IO_read_end)
564 && (fp->_wide_data->_IO_write_base
565 == fp->_wide_data->_IO_write_ptr));
567 if (mode == 0)
569 /* XXX For wide stream with backup store it is not very
570 reasonable to determine the offset. The pushed-back
571 character might require a state change and we need not be
572 able to compute the initial state by reverse transformation
573 since there is no guarantee of symmetry. So we don't even
574 try and return an error. */
575 if (_IO_in_backup (fp))
577 if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
579 __set_errno (EINVAL);
580 return -1;
583 /* There is no more data in the backup buffer. We can
584 switch back. */
585 INTUSE(_IO_switch_to_main_wget_area) (fp);
588 dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */
591 /* Flush unwritten characters.
592 (This may do an unneeded write if we seek within the buffer.
593 But to be able to switch to reading, we would need to set
594 egptr to ptr. That can't be done in the current design,
595 which assumes file_ptr() is eGptr. Anyway, since we probably
596 end up flushing when we close(), it doesn't make much difference.)
597 FIXME: simulate mem-mapped files. */
599 if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base
600 || _IO_in_put_mode (fp))
601 if (INTUSE(_IO_switch_to_wget_mode) (fp))
602 return WEOF;
604 if (fp->_wide_data->_IO_buf_base == NULL)
606 /* It could be that we already have a pushback buffer. */
607 if (fp->_wide_data->_IO_read_base != NULL)
609 free (fp->_wide_data->_IO_read_base);
610 fp->_flags &= ~_IO_IN_BACKUP;
612 INTUSE(_IO_doallocbuf) (fp);
613 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
614 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
615 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
616 fp->_wide_data->_IO_buf_base);
617 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
618 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
621 switch (dir)
623 struct _IO_codecvt *cv;
624 int clen;
626 case _IO_seek_cur:
627 /* Adjust for read-ahead (bytes is buffer). To do this we must
628 find out which position in the external buffer corresponds to
629 the current position in the internal buffer. */
630 cv = fp->_codecvt;
631 clen = (*cv->__codecvt_do_encoding) (cv);
633 if (clen > 0)
635 offset -= (fp->_wide_data->_IO_read_end
636 - fp->_wide_data->_IO_read_ptr) * clen;
637 /* Adjust by readahead in external buffer. */
638 offset -= fp->_IO_read_end - fp->_IO_read_ptr;
640 else
642 int nread;
644 delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_base;
645 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
646 nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
647 fp->_IO_read_base,
648 fp->_IO_read_end, delta);
649 fp->_IO_read_ptr = fp->_IO_read_base + nread;
650 fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
651 offset -= fp->_IO_read_end - fp->_IO_read_base - nread;
654 if (fp->_offset == _IO_pos_BAD)
655 goto dumb;
656 /* Make offset absolute, assuming current pointer is file_ptr(). */
657 offset += fp->_offset;
659 dir = _IO_seek_set;
660 break;
661 case _IO_seek_set:
662 break;
663 case _IO_seek_end:
665 struct _G_stat64 st;
666 if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode))
668 offset += st.st_size;
669 dir = _IO_seek_set;
671 else
672 goto dumb;
675 /* At this point, dir==_IO_seek_set. */
677 /* If we are only interested in the current position we've found it now. */
678 if (mode == 0)
679 return offset;
681 /* If destination is within current buffer, optimize: */
682 if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
683 && !_IO_in_backup (fp))
685 _IO_off64_t start_offset = (fp->_offset
686 - (fp->_IO_read_end - fp->_IO_buf_base));
687 if (offset >= start_offset && offset < fp->_offset)
689 _IO_setg (fp, fp->_IO_buf_base,
690 fp->_IO_buf_base + (offset - start_offset),
691 fp->_IO_read_end);
692 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
693 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
694 fp->_wide_data->_IO_buf_base,
695 fp->_wide_data->_IO_buf_base);
696 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
697 fp->_wide_data->_IO_buf_base);
698 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
699 goto resync;
703 if (fp->_flags & _IO_NO_READS)
704 goto dumb;
706 /* Try to seek to a block boundary, to improve kernel page management. */
707 new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1);
708 delta = offset - new_offset;
709 if (delta > fp->_IO_buf_end - fp->_IO_buf_base)
711 new_offset = offset;
712 delta = 0;
714 result = _IO_SYSSEEK (fp, new_offset, 0);
715 if (result < 0)
716 return EOF;
717 if (delta == 0)
718 count = 0;
719 else
721 count = _IO_SYSREAD (fp, fp->_IO_buf_base,
722 (must_be_exact
723 ? delta : fp->_IO_buf_end - fp->_IO_buf_base));
724 if (count < delta)
726 /* We weren't allowed to read, but try to seek the remainder. */
727 offset = count == EOF ? delta : delta-count;
728 dir = _IO_seek_cur;
729 goto dumb;
732 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta,
733 fp->_IO_buf_base + count);
734 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
735 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
736 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
737 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
738 fp->_offset = result + count;
739 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
740 return offset;
741 dumb:
743 INTUSE(_IO_unsave_markers) (fp);
744 result = _IO_SYSSEEK (fp, offset, dir);
745 if (result != EOF)
747 _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
748 fp->_offset = result;
749 _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
750 _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
751 _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
752 fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
753 _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
754 fp->_wide_data->_IO_buf_base);
756 return result;
758 resync:
759 /* We need to do it since it is possible that the file offset in
760 the kernel may be changed behind our back. It may happen when
761 we fopen a file and then do a fork. One process may access the
762 the file and the kernel file offset will be changed. */
763 if (fp->_offset >= 0)
764 _IO_SYSSEEK (fp, fp->_offset, 0);
766 return offset;
768 INTDEF(_IO_wfile_seekoff)
771 _IO_size_t
772 _IO_wfile_xsputn (f, data, n)
773 _IO_FILE *f;
774 const void *data;
775 _IO_size_t n;
777 register const wchar_t *s = (const wchar_t *) data;
778 _IO_size_t to_do = n;
779 int must_flush = 0;
780 _IO_size_t count;
782 if (n <= 0)
783 return 0;
784 /* This is an optimized implementation.
785 If the amount to be written straddles a block boundary
786 (or the filebuf is unbuffered), use sys_write directly. */
788 /* First figure out how much space is available in the buffer. */
789 count = f->_wide_data->_IO_write_end - f->_wide_data->_IO_write_ptr;
790 if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
792 count = f->_wide_data->_IO_buf_end - f->_wide_data->_IO_write_ptr;
793 if (count >= n)
795 register const wchar_t *p;
796 for (p = s + n; p > s; )
798 if (*--p == L'\n')
800 count = p - s + 1;
801 must_flush = 1;
802 break;
807 /* Then fill the buffer. */
808 if (count > 0)
810 if (count > to_do)
811 count = to_do;
812 if (count > 20)
814 #ifdef _LIBC
815 f->_wide_data->_IO_write_ptr =
816 __wmempcpy (f->_wide_data->_IO_write_ptr, s, count);
817 #else
818 wmemcpy (f->_wide_data->_IO_write_ptr, s, count);
819 f->_wide_data->_IO_write_ptr += count;
820 #endif
821 s += count;
823 else
825 register wchar_t *p = f->_wide_data->_IO_write_ptr;
826 register int i = (int) count;
827 while (--i >= 0)
828 *p++ = *s++;
829 f->_wide_data->_IO_write_ptr = p;
831 to_do -= count;
833 if (to_do > 0)
834 to_do -= INTUSE(_IO_wdefault_xsputn) (f, s, to_do);
835 if (must_flush
836 && f->_wide_data->_IO_write_ptr != f->_wide_data->_IO_write_base)
837 INTUSE(_IO_wdo_write) (f, f->_wide_data->_IO_write_base,
838 f->_wide_data->_IO_write_ptr
839 - f->_wide_data->_IO_write_base);
841 return n - to_do;
843 INTDEF(_IO_wfile_xsputn)
846 const struct _IO_jump_t _IO_wfile_jumps =
848 JUMP_INIT_DUMMY,
849 JUMP_INIT(finish, _IO_new_file_finish),
850 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
851 JUMP_INIT(underflow, (_IO_underflow_t) INTUSE(_IO_wfile_underflow)),
852 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
853 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
854 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
855 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
856 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
857 JUMP_INIT(seekpos, _IO_default_seekpos),
858 JUMP_INIT(setbuf, _IO_new_file_setbuf),
859 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
860 JUMP_INIT(doallocate, _IO_wfile_doallocate),
861 JUMP_INIT(read, INTUSE(_IO_file_read)),
862 JUMP_INIT(write, _IO_new_file_write),
863 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
864 JUMP_INIT(close, INTUSE(_IO_file_close)),
865 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
866 JUMP_INIT(showmanyc, _IO_default_showmanyc),
867 JUMP_INIT(imbue, _IO_default_imbue)
869 libc_hidden_data_def (_IO_wfile_jumps)
872 const struct _IO_jump_t _IO_wfile_jumps_mmap =
874 JUMP_INIT_DUMMY,
875 JUMP_INIT(finish, _IO_new_file_finish),
876 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
877 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_mmap),
878 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
879 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
880 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
881 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
882 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
883 JUMP_INIT(seekpos, _IO_default_seekpos),
884 JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
885 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
886 JUMP_INIT(doallocate, _IO_wfile_doallocate),
887 JUMP_INIT(read, INTUSE(_IO_file_read)),
888 JUMP_INIT(write, _IO_new_file_write),
889 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
890 JUMP_INIT(close, _IO_file_close_mmap),
891 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
892 JUMP_INIT(showmanyc, _IO_default_showmanyc),
893 JUMP_INIT(imbue, _IO_default_imbue)
896 const struct _IO_jump_t _IO_wfile_jumps_maybe_mmap =
898 JUMP_INIT_DUMMY,
899 JUMP_INIT(finish, _IO_new_file_finish),
900 JUMP_INIT(overflow, (_IO_overflow_t) INTUSE(_IO_wfile_overflow)),
901 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_maybe_mmap),
902 JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
903 JUMP_INIT(pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
904 JUMP_INIT(xsputn, INTUSE(_IO_wfile_xsputn)),
905 JUMP_INIT(xsgetn, INTUSE(_IO_file_xsgetn)),
906 JUMP_INIT(seekoff, INTUSE(_IO_wfile_seekoff)),
907 JUMP_INIT(seekpos, _IO_default_seekpos),
908 JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
909 JUMP_INIT(sync, (_IO_sync_t) INTUSE(_IO_wfile_sync)),
910 JUMP_INIT(doallocate, _IO_wfile_doallocate),
911 JUMP_INIT(read, INTUSE(_IO_file_read)),
912 JUMP_INIT(write, _IO_new_file_write),
913 JUMP_INIT(seek, INTUSE(_IO_file_seek)),
914 JUMP_INIT(close, INTUSE(_IO_file_close)),
915 JUMP_INIT(stat, INTUSE(_IO_file_stat)),
916 JUMP_INIT(showmanyc, _IO_default_showmanyc),
917 JUMP_INIT(imbue, _IO_default_imbue)