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
This commit is contained in:
Francesco Montorsi
2009-01-13 19:16:08 +00:00
parent 2bc566539b
commit b81e450659
7 changed files with 110 additions and 64 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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)

View File

@@ -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;
}

View File

@@ -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;
}