From b81e45065986abc34676fecc31515efeb3f3a8d4 Mon Sep 17 00:00:00 2001 From: Francesco Montorsi Date: Tue, 13 Jan 2009 19:16:08 +0000 Subject: [PATCH] always properly check if SeekI() calls succeded; this makes CanRead() functions of animation decoders return false for non-seekable streams (which is a wanted side-effect) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58080 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/anidecod.cpp | 18 ++++++---- src/common/gifdecod.cpp | 21 +++++++----- src/common/imagbmp.cpp | 27 ++++++++++----- src/common/imagiff.cpp | 76 +++++++++++++++++++++++------------------ src/common/imagtga.cpp | 3 +- src/common/xpmdecod.cpp | 3 +- src/html/chm.cpp | 26 ++++++++++---- 7 files changed, 110 insertions(+), 64 deletions(-) diff --git a/src/common/anidecod.cpp b/src/common/anidecod.cpp index 7c8dd53a99..bf5e22855a 100644 --- a/src/common/anidecod.cpp +++ b/src/common/anidecod.cpp @@ -127,7 +127,8 @@ bool wxANIDecoder::CanRead(wxInputStream& stream) const wxInt32 anih32; memcpy( &anih32, "anih", 4 ); - stream.SeekI(0); + if ( stream.SeekI(0) == wxInvalidOffset ) + return false; if ( !stream.Read(&FCC1, 4) ) return false; @@ -154,7 +155,8 @@ bool wxANIDecoder::CanRead(wxInputStream& stream) const } else { - stream.SeekI(stream.TellI() + datalen); + if ( stream.SeekI(stream.TellI() + datalen) == wxInvalidOffset ) + return false; } // try to read next data chunk: @@ -220,8 +222,10 @@ bool wxANIDecoder::Load( wxInputStream& stream ) wxInt32 seq32; memcpy( &seq32, "seq ", 4 ); - stream.SeekI(0); - stream.Read(&FCC1, 4); + if ( stream.SeekI(0) == wxInvalidOffset) + return false; + if ( !stream.Read(&FCC1, 4) ) + return false; if ( FCC1 != riff32 ) return false; @@ -309,11 +313,13 @@ bool wxANIDecoder::Load( wxInputStream& stream ) } else { - stream.SeekI(stream.TellI() + datalen); + if ( stream.SeekI(stream.TellI() + datalen) == wxInvalidOffset ) + return false; } // try to read next data chunk: - stream.Read(&FCC1, 4); + if ( !stream.Read(&FCC1, 4) ) + return false; } if (m_nFrames==0) diff --git a/src/common/gifdecod.cpp b/src/common/gifdecod.cpp index 9d8cc1dd71..0a2dad2724 100644 --- a/src/common/gifdecod.cpp +++ b/src/common/gifdecod.cpp @@ -580,7 +580,8 @@ bool wxGIFDecoder::CanRead(wxInputStream &stream) const if ( !stream.Read(buf, WXSIZEOF(buf)) ) return false; - stream.SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent); + if (stream.SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent) == wxInvalidOffset) + return false; // this happens e.g. for non-seekable streams return memcmp(buf, "GIF", WXSIZEOF(buf)) == 0; } @@ -720,12 +721,12 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream) { while ((i = (unsigned char)stream.GetC()) != 0) { - if (stream.Eof() || (stream.LastRead() == 0)) + if (stream.Eof() || (stream.LastRead() == 0) || + stream.SeekI(i, wxFromCurrent) == wxInvalidOffset) { done = true; break; } - stream.SeekI(i, wxFromCurrent); } } } @@ -834,12 +835,12 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream) // skip all data while ((i = (unsigned char)stream.GetC()) != 0) { - if (stream.Eof() || (stream.LastRead() == 0)) + if (stream.Eof() || (stream.LastRead() == 0) || + stream.SeekI(i, wxFromCurrent) == wxInvalidOffset) { Destroy(); return wxGIF_INVFORMAT; } - stream.SeekI(i, wxFromCurrent); } } else if (type == 0x2C) @@ -858,7 +859,11 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream) { unsigned int local_ncolors = 2 << (buf[8] & 0x07); wxFileOffset numBytes = 3 * local_ncolors; - stream.SeekI(numBytes, wxFromCurrent); + if (stream.SeekI(numBytes, wxFromCurrent) == wxInvalidOffset) + { + Destroy(); + return wxGIF_INVFORMAT; + } } // initial code size @@ -872,12 +877,12 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream) // skip all data while ((i = (unsigned char)stream.GetC()) != 0) { - if (stream.Eof() || (stream.LastRead() == 0)) + if (stream.Eof() || (stream.LastRead() == 0) || + stream.SeekI(i, wxFromCurrent) == wxInvalidOffset) { Destroy(); return wxGIF_INVFORMAT; } - stream.SeekI(i, wxFromCurrent); } } else if ((type != 0x3B) && (type != 00)) // testing diff --git a/src/common/imagbmp.cpp b/src/common/imagbmp.cpp index 72398fcda1..6f3a298d18 100644 --- a/src/common/imagbmp.cpp +++ b/src/common/imagbmp.cpp @@ -640,7 +640,11 @@ bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height, * Reading the image data */ if ( IsBmp ) - stream.SeekI(bmpOffset); // else icon, just carry on + { + if (stream.SeekI(bmpOffset) == wxInvalidOffset) + return false; + //else: icon, just carry on + } unsigned char *data = ptr; @@ -1255,7 +1259,8 @@ bool wxICOHandler::SaveFile(wxImage *image, bool wxICOHandler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose, int index) { - stream.SeekI(0); + if (stream.SeekI(0) == wxInvalidOffset) + return false; return DoLoadFile(image, stream, verbose, index); } @@ -1315,7 +1320,8 @@ bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream, { // seek to selected icon: pCurrentEntry = pIconDirEntry + iSel; - stream.SeekI(iPos + wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart); + if (stream.SeekI(iPos + wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart) == wxInvalidOffset) + return false; bResult = LoadDib(image, stream, true, IsBmp); bool bIsCursorType = (this->GetType() == wxBITMAP_TYPE_CUR) || (this->GetType() == wxBITMAP_TYPE_ANI); if ( bResult && bIsCursorType && nType == 2 ) @@ -1333,16 +1339,20 @@ int wxICOHandler::GetImageCount(wxInputStream& stream) { ICONDIR IconDir; wxFileOffset iPos = stream.TellI(); - stream.SeekI(0); - stream.Read(&IconDir, sizeof(IconDir)); + if (stream.SeekI(0) == wxInvalidOffset) + return 0; + if (stream.Read(&IconDir, sizeof(IconDir)).LastRead() != sizeof(IconDir)) + return 0; wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount); - stream.SeekI(iPos); + if (stream.SeekI(iPos) == wxInvalidOffset) + return 0; return (int)nIcons; } bool wxICOHandler::DoCanRead(wxInputStream& stream) { - stream.SeekI(0); + if (stream.SeekI(0) == wxInvalidOffset) + return false; unsigned char hdr[4]; if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) return false; @@ -1364,7 +1374,8 @@ IMPLEMENT_DYNAMIC_CLASS(wxCURHandler, wxICOHandler) bool wxCURHandler::DoCanRead(wxInputStream& stream) { - stream.SeekI(0); + if (stream.SeekI(0) == wxInvalidOffset) + return false; unsigned char hdr[4]; if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) return false; diff --git a/src/common/imagiff.cpp b/src/common/imagiff.cpp index 058d6e5cd0..9fd15214d4 100644 --- a/src/common/imagiff.cpp +++ b/src/common/imagiff.cpp @@ -234,7 +234,8 @@ bool wxIFFDecoder::CanRead() if ( !m_f->Read(buf, WXSIZEOF(buf)) ) return false; - m_f->SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent); + if ( m_f->SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent) == wxInvalidOffset ) + return false; return (memcmp(buf, "FORM", 4) == 0) && (memcmp(buf+8, "ILBM", 4) == 0); } @@ -248,13 +249,13 @@ typedef unsigned char byte; #define IFFDEBUG 0 /************************************************************************* - void decomprle(source, destination, source length, buffer size) +void decomprle(source, destination, source length, buffer size) - Decompress run-length encoded data from source to destination. Terminates - when source is decoded completely or destination buffer is full. +Decompress run-length encoded data from source to destination. Terminates +when source is decoded completely or destination buffer is full. - The decruncher is as optimized as I could make it, without risking - safety in case of corrupt BODY chunks. +The decruncher is as optimized as I could make it, without risking +safety in case of corrupt BODY chunks. **************************************************************************/ static void decomprle(const byte *sptr, byte *dptr, long slen, long dlen) @@ -329,20 +330,27 @@ int wxIFFDecoder::ReadIFF() m_image = new IFFImage(); if (m_image == 0) { - Destroy(); - return wxIFF_MEMERR; + Destroy(); + return wxIFF_MEMERR; } // compute file length wxFileOffset currentPos = m_f->TellI(); - m_f->SeekI(0, wxFromEnd); + if (m_f->SeekI(0, wxFromEnd) == wxInvalidOffset) { + Destroy(); + return wxIFF_MEMERR; + } + long filesize = m_f->TellI(); - m_f->SeekI(currentPos, wxFromStart); + if (m_f->SeekI(currentPos, wxFromStart) == wxInvalidOffset) { + Destroy(); + return wxIFF_MEMERR; + } // allocate memory for complete file if ((databuf = new byte[filesize]) == 0) { - Destroy(); - return wxIFF_MEMERR; + Destroy(); + return wxIFF_MEMERR; } m_f->Read(databuf, filesize); @@ -353,22 +361,22 @@ int wxIFFDecoder::ReadIFF() // check for minmal size if (dataptr + 12 > dataend) { - Destroy(); - return wxIFF_INVFORMAT; + Destroy(); + return wxIFF_INVFORMAT; } // check if we really got an IFF file if (strncmp((char *)dataptr, "FORM", 4) != 0) { - Destroy(); - return wxIFF_INVFORMAT; + Destroy(); + return wxIFF_INVFORMAT; } dataptr = dataptr + 8; // skip ID and length of FORM // check if the IFF file is an ILBM (picture) file if (strncmp((char *) dataptr, "ILBM", 4) != 0) { - Destroy(); - return wxIFF_INVFORMAT; + Destroy(); + return wxIFF_INVFORMAT; } wxLogTrace(_T("iff"), _T("IFF ILBM file recognized")); @@ -388,7 +396,7 @@ int wxIFFDecoder::ReadIFF() // get chunk length and make even long chunkLen = (iff_getlong(dataptr + 4) + 1) & 0xfffffffe; if (chunkLen < 0) { // format error? - break; + break; } bool truncated = (dataptr + 8 + chunkLen > dataend); @@ -586,36 +594,36 @@ int wxIFFDecoder::ReadIFF() int c = (col & 0x0f); switch (col & 0x30) { case 0x00: if (c >= 0 && c < colors) { - rval = pal[3*c + 0]; - gval = pal[3*c + 1]; - bval = pal[3*c + 2]; - } - break; + rval = pal[3*c + 0]; + gval = pal[3*c + 1]; + bval = pal[3*c + 2]; + } + break; case 0x10: bval = c * 17; - break; + break; case 0x20: rval = c * 17; - break; + break; case 0x30: gval = c * 17; - break; + break; } } else if (fmt == ILBM_HAM8) { int c = (col & 0x3f); switch(col & 0xc0) { case 0x00: if (c >= 0 && c < colors) { - rval = pal[3*c + 0]; - gval = pal[3*c + 1]; - bval = pal[3*c + 2]; - } - break; + rval = pal[3*c + 0]; + gval = pal[3*c + 1]; + bval = pal[3*c + 2]; + } + break; case 0x40: bval = (bval & 3) | (c << 2); - break; + break; case 0x80: rval = (rval & 3) | (c << 2); - break; + break; case 0xc0: gval = (rval & 3) | (c << 2); } diff --git a/src/common/imagtga.cpp b/src/common/imagtga.cpp index fc7373822b..9fd65003a7 100644 --- a/src/common/imagtga.cpp +++ b/src/common/imagtga.cpp @@ -211,7 +211,8 @@ int ReadTGA(wxImage* image, wxInputStream& stream) } // Seek from the offset we got from the TGA header. - stream.SeekI(offset, wxFromStart); + if (stream.SeekI(offset, wxFromStart) == wxInvalidOffset) + return wxTGA_INVFORMAT; // Load a palette if we have one. if (colorType == wxTGA_MAPPED) diff --git a/src/common/xpmdecod.cpp b/src/common/xpmdecod.cpp index 8d38a3132e..54e54c5221 100644 --- a/src/common/xpmdecod.cpp +++ b/src/common/xpmdecod.cpp @@ -122,7 +122,8 @@ bool wxXPMDecoder::CanRead(wxInputStream& stream) if ( !stream.Read(buf, WXSIZEOF(buf)) ) return false; - stream.SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent); + if (stream.SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent) == wxInvalidOffset) + return false; return memcmp(buf, "/* XPM */", WXSIZEOF(buf)) == 0; } diff --git a/src/html/chm.cpp b/src/html/chm.cpp index 5e14bcd55d..584f7fb2cc 100644 --- a/src/html/chm.cpp +++ b/src/html/chm.cpp @@ -487,15 +487,29 @@ size_t wxChmInputStream::OnSysRead(void *buffer, size_t bufsize) m_lasterror = wxSTREAM_NO_ERROR; // If the rest to read from the stream is less - // than the buffer size, than only read the rest + // than the buffer size, then only read the rest if ( m_pos + bufsize > m_size ) bufsize = m_size - m_pos; - m_contentStream->SeekI(m_pos); - m_contentStream->Read(buffer, bufsize); - m_pos +=bufsize; - m_contentStream->SeekI(m_pos); - return bufsize; + if (m_contentStream->SeekI(m_pos) == wxInvalidOffset) + { + m_lasterror = wxSTREAM_EOF; + return 0; + } + + size_t read = m_contentStream->Read(buffer, bufsize).LastRead(); + m_pos += read; + + if (m_contentStream->SeekI(m_pos) == wxInvalidOffset) + { + m_lasterror = wxSTREAM_READ_ERROR; + return 0; + } + + if (read != bufsize) + m_lasterror = m_contentStream->GetLastError(); + + return read; } -- 2.45.2