From fb09d0e55360b841c37c9ecf2220126bcd59ae3c Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 29 Apr 2011 14:31:45 +0000 Subject: [PATCH] Refactored the libtiffOStreamSeekProc function so that it extendeds the stream when the requested file position is beyond the current end of the stream. This fix addresses a bug that occurred when writting to a streamstream. --- src/osgPlugins/tiff/ReaderWriterTIFF.cpp | 54 +++++++++++++++++------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/src/osgPlugins/tiff/ReaderWriterTIFF.cpp b/src/osgPlugins/tiff/ReaderWriterTIFF.cpp index 480e21b92..5f66169f0 100644 --- a/src/osgPlugins/tiff/ReaderWriterTIFF.cpp +++ b/src/osgPlugins/tiff/ReaderWriterTIFF.cpp @@ -177,33 +177,55 @@ toff_t libtiffOStreamSeekProc(thandle_t fd, toff_t off, int i) { std::ostream *fout = (std::ostream*)fd; - toff_t ret; + toff_t pos_required = 0; + toff_t stream_end = 0; switch(i) { case SEEK_SET: - fout->seekp(off,std::ios::beg); - ret = fout->tellp(); - if(fout->bad()) - ret = 0; - break; + { + pos_required = off; + fout->seekp(0, std::ios::end); + stream_end = fout->tellp(); + break; + } case SEEK_CUR: - fout->seekp(off,std::ios::cur); - ret = fout->tellp(); - if(fout->bad()) - ret = 0; - break; + { + toff_t stream_curr = fout->tellp(); + pos_required = stream_curr + off; + fout->seekp(0, std::ios::end); + stream_end = fout->tellp(); + break; + } case SEEK_END: - fout->seekp(off,std::ios::end); - ret = fout->tellp(); - if(fout->bad()) - ret = 0; + { + fout->seekp(0, std::ios::end); + stream_end = fout->tellp(); + pos_required = stream_end + off; break; + } default: - ret = 0; break; } + + if (pos_required>stream_end) + { + // position required past the end of the stream so we need to insert extra characters to + // ensure the stream is big enough to encompass the new the position. + fout->seekp(0, std::ios::end); + for(toff_t i=stream_end; iput(char(0)); + } + } + + fout->seekp(pos_required,std::ios::beg); + toff_t ret = fout->tellp(); + if (fout->bad()) + { + ret = 0; + } return ret; }