diff options
author | Siddhesh Poyarekar <siddhesh@redhat.com> | 2012-09-28 18:20:40 +0530 |
---|---|---|
committer | Siddhesh Poyarekar <siddhesh@redhat.com> | 2012-09-28 18:21:39 +0530 |
commit | 4573c6b09884a93fffa3a754678ef881cadebfb3 (patch) | |
tree | 89734fdb5480204f4bf4ad5c0977c39a5265ec2f /libio/wfileops.c | |
parent | 784421e72b926d027398ee93c6c2f1ddad549c6a (diff) | |
download | glibc-4573c6b09884a93fffa3a754678ef881cadebfb3.zip glibc-4573c6b09884a93fffa3a754678ef881cadebfb3.tar.gz glibc-4573c6b09884a93fffa3a754678ef881cadebfb3.tar.bz2 |
Adjust wide data buffer pointers during fseek and ftell
[BZ #14543]
Set the internal buffer state correctly whenever the external buffer
state is modified by fseek by either computing the current
_IO_read_ptr/end for the internal buffer based on the new _IO_read_ptr
in the external buffer or converting the content read into the
external buffer, up to the extent of the requested fseek offset.
Diffstat (limited to 'libio/wfileops.c')
-rw-r--r-- | libio/wfileops.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/libio/wfileops.c b/libio/wfileops.c index b790029..d3f46b2 100644 --- a/libio/wfileops.c +++ b/libio/wfileops.c @@ -545,6 +545,57 @@ _IO_wfile_sync (fp) } libc_hidden_def (_IO_wfile_sync) +/* Adjust the internal buffer pointers to reflect the state in the external + buffer. The content between fp->_IO_read_base and fp->_IO_read_ptr is + assumed to be converted and available in the range + fp->_wide_data->_IO_read_base and fp->_wide_data->_IO_read_end. + + Returns 0 on success and -1 on error with the _IO_ERR_SEEN flag set. */ +static inline int +adjust_wide_data (_IO_FILE *fp, bool do_convert) +{ + struct _IO_codecvt *cv = fp->_codecvt; + + int clen = (*cv->__codecvt_do_encoding) (cv); + + /* Take the easy way out for constant length encodings if we don't need to + convert. */ + if (!do_convert && clen > 0) + { + fp->_wide_data->_IO_read_end += ((fp->_IO_read_ptr - fp->_IO_read_base) + / clen); + goto done; + } + + enum __codecvt_result status; + const char *read_stop = (const char *) fp->_IO_read_base; + do + { + + fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state; + status = (*cv->__codecvt_do_in) (cv, &fp->_wide_data->_IO_state, + fp->_IO_read_base, fp->_IO_read_ptr, + &read_stop, + fp->_wide_data->_IO_read_base, + fp->_wide_data->_IO_buf_end, + &fp->_wide_data->_IO_read_end); + + /* Should we return EILSEQ? */ + if (__builtin_expect (status == __codecvt_error, 0)) + { + fp->_flags |= _IO_ERR_SEEN; + return -1; + } + } + while (__builtin_expect (status == __codecvt_partial, 0)); + +done: + /* Now seek to _IO_read_end to behave as if we have read it all in. */ + fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end; + + return 0; +} + _IO_off64_t _IO_wfile_seekoff (fp, offset, dir, mode) _IO_FILE *fp; @@ -693,6 +744,10 @@ _IO_wfile_seekoff (fp, offset, dir, mode) fp->_wide_data->_IO_buf_base); _IO_wsetp (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base); + + if (adjust_wide_data (fp, false)) + goto dumb; + _IO_mask_flags (fp, 0, _IO_EOF_SEEN); goto resync; } @@ -733,6 +788,10 @@ _IO_wfile_seekoff (fp, offset, dir, mode) _IO_wsetg (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base); _IO_wsetp (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base); + + if (adjust_wide_data (fp, true)) + goto dumb; + fp->_offset = result + count; _IO_mask_flags (fp, 0, _IO_EOF_SEEN); return offset; |