Fix 25 errors

This commit is contained in:
Oleg Korshul
2020-10-13 20:11:21 +03:00
parent 66a6b7a111
commit 88cf60a3ed
18 changed files with 233 additions and 87 deletions

View File

@ -49,11 +49,11 @@
class DLL_EXP CxFile
{
public:
CxFile(void) { };
virtual ~CxFile() { };
CxFile(void) { }
virtual ~CxFile() { }
virtual bool Close() = 0;
virtual size_t Read(void *buffer, size_t size, size_t count) = 0;
virtual size_t Read(void *buffer, size_t size, size_t count, void* limit_start = NULL, void* limit_end = NULL) = 0;
virtual size_t Write(const void *buffer, size_t size, size_t count) = 0;
virtual bool Seek(int32_t offset, int32_t origin) = 0;
virtual int32_t Tell() = 0;
@ -72,4 +72,27 @@ public:
virtual int32_t Scanf(const char *format, void* output) = 0;
};
static void clamp_buffer(void*& buffer, size_t& size, void* limit_start, void* limit_end)
{
if (NULL == limit_start || NULL == limit_end)
return;
uint8_t* _buffer = (uint8_t*)buffer;
uint8_t* _limit_start = (uint8_t*)limit_start;
uint8_t* _limit_end = (uint8_t*)limit_end;
if (_buffer > _limit_end)
{
buffer = limit_end;
size = 0;
return;
}
if (_buffer < _limit_start)
_buffer = _limit_start;
if ((_buffer + size) > _limit_end)
size = (_limit_end - _buffer);
}
#endif //__xfile_h

View File

@ -133,7 +133,7 @@ bool CxImageBMP::Decode(CxFile * hFile)
if (bIsOldBmp){
// convert a old color table (3 byte entries) to a new
// color table (4 byte entries)
hFile->Read((void*)pRgb,DibNumColors(&bmpHeader) * sizeof(RGBTRIPLE),1);
hFile->Read((void*)pRgb,DibNumColors(&bmpHeader) * sizeof(RGBTRIPLE),1,GetDIB(),GetDIBLimit());
for (int32_t i=DibNumColors(&head)-1; i>=0; i--){
pRgb[i].rgbRed = ((RGBTRIPLE *)pRgb)[i].rgbtRed;
pRgb[i].rgbBlue = ((RGBTRIPLE *)pRgb)[i].rgbtBlue;
@ -141,7 +141,7 @@ bool CxImageBMP::Decode(CxFile * hFile)
pRgb[i].rgbReserved = (uint8_t)0;
}
} else {
hFile->Read((void*)pRgb,DibNumColors(&bmpHeader) * sizeof(RGBQUAD),1);
hFile->Read((void*)pRgb,DibNumColors(&bmpHeader) * sizeof(RGBQUAD),1,GetDIB(),GetDIBLimit());
//force rgbReserved=0, to avoid problems with some WinXp bitmaps
for (uint32_t i=0; i<head.biClrUsed; i++) pRgb[i].rgbReserved=0;
}
@ -165,7 +165,7 @@ bool CxImageBMP::Decode(CxFile * hFile)
int32_t imagesize=4*head.biHeight*head.biWidth;
uint8_t* buff32=(uint8_t*)malloc(imagesize);
if (buff32){
hFile->Read(buff32, imagesize,1); // read in the pixels
hFile->Read(buff32, imagesize,1,GetDIB(),GetDIBLimit()); // read in the pixels
#if CXIMAGE_SUPPORT_ALPHA
if (dwCompression == BI_RGB){
@ -195,7 +195,7 @@ bool CxImageBMP::Decode(CxFile * hFile)
case 24 :
if (bf.bfOffBits != 0L) hFile->Seek(off + bf.bfOffBits,SEEK_SET);
if (dwCompression == BI_RGB){
hFile->Read(info.pImage, head.biSizeImage,1); // read in the pixels
hFile->Read(info.pImage, head.biSizeImage,1,GetDIB(),GetDIBLimit()); // read in the pixels
} else cx_throw("unknown compression");
break;
case 16 :
@ -210,7 +210,7 @@ bool CxImageBMP::Decode(CxFile * hFile)
// bf.bfOffBits required after the bitfield mask <Cui Ying Jie>
if (bf.bfOffBits != 0L) hFile->Seek(off + bf.bfOffBits,SEEK_SET);
// read in the pixels
hFile->Read(info.pImage, head.biHeight*((head.biWidth+1)/2)*4,1);
hFile->Read(info.pImage, head.biHeight*((head.biWidth+1)/2)*4,1,GetDIB(),GetDIBLimit());
// transform into RGB
Bitfield2RGB(info.pImage,bfmask[0],bfmask[1],bfmask[2],16);
break;
@ -229,7 +229,7 @@ bool CxImageBMP::Decode(CxFile * hFile)
}
switch (dwCompression) {
case BI_RGB :
hFile->Read(info.pImage, head.biSizeImage,1); // read in the pixels
hFile->Read(info.pImage, head.biSizeImage,1,GetDIB(),GetDIBLimit()); // read in the pixels
break;
case BI_RLE4 :
{
@ -355,7 +355,7 @@ bool CxImageBMP::Decode(CxFile * hFile)
break;
}
default :
hFile->Read((void *)(iter.GetRow(scanline) + bits), sizeof(uint8_t) * status_byte, 1);
hFile->Read((void *)(iter.GetRow(scanline) + bits), sizeof(uint8_t) * status_byte, 1,GetDIB(),GetDIBLimit());
// align run length to even number of bytes
if ((status_byte & 1) == 1)
hFile->Read(&second_byte, sizeof(uint8_t), 1);

View File

@ -21,7 +21,7 @@
void CxImage::Startup(uint32_t imagetype)
{
//init pointers
pDib = pSelection = pAlpha = NULL;
pDib = pDibLimit = pSelection = pAlpha = NULL;
ppLayers = ppFrames = NULL;
//init structures
memset(&head,0,sizeof(BITMAPINFOHEADER));
@ -232,6 +232,7 @@ void* CxImage::Create(uint32_t dwWidth, uint32_t dwHeight, uint32_t wBpp, uint32
strcpy(info.szLastError,"CxImage::Create can't allocate memory");
return NULL;
}
pDibLimit = (void*)((uint8_t*)pDib + GetSize());
//clear the palette
RGBQUAD* pal=GetPalette();
@ -278,9 +279,12 @@ uint8_t* CxImage::GetBits(uint32_t row)
/**
* \return the size in bytes of the internal pDib object
*/
int32_t CxImage::GetSize()
uint32_t CxImage::GetSize()
{
return head.biSize + head.biSizeImage + GetPaletteSize();
uint64_t size64 = head.biSize + head.biSizeImage + GetPaletteSize();
if (size64 > 0xFFFFFFFF)
return 0xFFFFFFFF;
return (uint32_t)size64;
}
////////////////////////////////////////////////////////////////////////////////
/**
@ -324,13 +328,14 @@ bool CxImage::Transfer(CxImage &from, bool bTransferFrames /*=true*/)
memcpy(&info,&from.info,sizeof(CXIMAGEINFO));
pDib = from.pDib;
pDib = from.pDibLimit;
pSelection = from.pSelection;
pAlpha = from.pAlpha;
ppLayers = from.ppLayers;
memset(&from.head,0,sizeof(BITMAPINFOHEADER));
memset(&from.info,0,sizeof(CXIMAGEINFO));
from.pDib = from.pSelection = from.pAlpha = NULL;
from.pDib = from.pDibLimit = from.pSelection = from.pAlpha = NULL;
from.ppLayers = NULL;
if (bTransferFrames){
@ -352,6 +357,7 @@ void CxImage::Ghost(const CxImage *from)
memcpy(&head,&from->head,sizeof(BITMAPINFOHEADER));
memcpy(&info,&from->info,sizeof(CXIMAGEINFO));
pDib = from->pDib;
pDibLimit = from->pDibLimit;
pSelection = from->pSelection;
pAlpha = from->pAlpha;
ppLayers = from->ppLayers;

View File

@ -289,10 +289,11 @@ public:
//@}
/** \addtogroup Attributes */ //@{
int32_t GetSize();
uint32_t GetSize();
uint8_t* GetBits(uint32_t row = 0);
uint8_t GetColorType();
void* GetDIB() const;
void* GetDIBLimit() const;
uint32_t GetHeight() const;
uint32_t GetWidth() const;
uint32_t GetEffWidth() const;
@ -796,6 +797,8 @@ protected:
void bihtoh(BITMAPINFOHEADER* bih);
void* pDib; //contains the header, the palette, the pixels
void* pDibLimit;
BITMAPINFOHEADER head; //standard header
CXIMAGEINFO info; //extended information
uint8_t* pSelection; //selected region

View File

@ -95,9 +95,15 @@ bool CxImageICO::Decode(CxFile *hFile)
// read the palette
RGBQUAD pal[256];
if (bih.biClrUsed)
hFile->Read(pal,bih.biClrUsed*sizeof(RGBQUAD), 1);
{
DWORD _count = bih.biClrUsed; if (_count > 256) _count = 256;
hFile->Read(pal,_count*sizeof(RGBQUAD), 1);
}
else
hFile->Read(pal,head.biClrUsed*sizeof(RGBQUAD), 1);
{
DWORD _count = head.biClrUsed; if (_count > 256) _count = 256;
hFile->Read(pal,_count*sizeof(RGBQUAD), 1);
}
SetPalette(pal,head.biClrUsed); //palette assign

View File

@ -181,6 +181,10 @@ void* CxImage::GetDIB() const
{
return pDib;
}
void* CxImage::GetDIBLimit() const
{
return pDibLimit;
}
////////////////////////////////////////////////////////////////////////////////
uint32_t CxImage::GetHeight() const
{

View File

@ -50,6 +50,9 @@ bool CxImagePCX::Decode(CxFile *hFile)
info.xDPI = pcxHeader.Hres;
info.yDPI = pcxHeader.Vres;
if (Width <= 0 || Height <= 0)
cx_throw("Error: Not a PCX file");
if (info.nEscape == -1){
head.biWidth = Width;
head.biHeight= Height;
@ -76,7 +79,11 @@ bool CxImagePCX::Decode(CxFile *hFile)
//Read the image and check if it's ok
nbytes = pcxHeader.BytesPerLine * pcxHeader.ColorPlanes * Height;
uint32_t pcximage_size = nbytes;
lpHead1 = pcximage = (uint8_t*)malloc(nbytes);
if (!pcximage)
cx_throw("Cancelled");
while (nbytes > 0){
if (hFile == NULL || hFile->Eof()) cx_throw("corrupted PCX");
@ -119,6 +126,9 @@ bool CxImagePCX::Decode(CxFile *hFile)
for (uint32_t idx=0; idx<head.biClrUsed; idx++) SetPaletteColor((uint8_t)idx,ColorMap[idx][0],ColorMap[idx][1],ColorMap[idx][2]);
lpHead2 = pcxpixels = (uint8_t *)malloc(Width + pcxHeader.BytesPerLine * 8);
if (!pcxpixels)
cx_throw("Cancelled");
// Convert the image
for (y = 0; y < Height; y++){
@ -138,7 +148,11 @@ bool CxImagePCX::Decode(CxFile *hFile)
} else if (pcxHeader.ColorPlanes == 4 && pcxHeader.BitsPerPixel == 8){
for (x = 0; x < Width; x++){
SetPixelColor(x,y2,RGB(pcxplanes[x],pcxplanes[pcxHeader.BytesPerLine + x],pcxplanes[2*pcxHeader.BytesPerLine + x]));
AlphaSet(x,y2,pcxplanes[3*pcxHeader.BytesPerLine + x]);
uint32_t alphaIndex = 3*pcxHeader.BytesPerLine + x;
if (alphaIndex < pcximage_size)
AlphaSet(x,y2,pcxplanes[alphaIndex]);
}
continue;
#endif //CXIMAGE_SUPPORT_ALPHA

View File

@ -301,7 +301,8 @@ bool CxImageTIF::Decode(CxFile * hFile)
tilebuf = (uint8_t*)malloc(TIFFTileSize(m_tif));
}
bits = (uint8_t*)malloc(bitspersample==16? bitsize*2 : bitsize); // * VK
int32_t bitsize_correct = bitspersample==16? bitsize*2 : bitsize;
bits = (uint8_t*)malloc(bitsize_correct); // * VK
uint8_t * bits16 = NULL; // + VK
int32_t line16 = 0; // + VK
@ -309,6 +310,7 @@ bool CxImageTIF::Decode(CxFile * hFile)
line16 = line;
line = CalculateLine(width, 8 * samplesperpixel);
bits16 = bits;
bitsize_correct = bitsize;
bits = (uint8_t*)malloc(bitsize);
}
@ -454,9 +456,18 @@ bool CxImageTIF::Decode(CxFile * hFile)
double p,cx,cy,cz,cr,cg,cb;
while (ii</*line*/width){ // * VK
bitsoffset = ii*samplesperpixel+offset;
l=bits[bitsoffset];
a=bits[bitsoffset+1];
b=bits[bitsoffset+2];
if (bitsoffset + 2 < bitsize_correct)
{
l=bits[bitsoffset];
a=bits[bitsoffset+1];
b=bits[bitsoffset+2];
}
else
{
l=0;
a=0;
b=0;
}
if (a>127) a-=256;
if (b>127) b-=256;
// lab to xyz

View File

@ -59,9 +59,10 @@ public:
return (bool)(iErr==0);
}
//////////////////////////////////////////////////////////
virtual size_t Read(void *buffer, size_t size, size_t count)
virtual size_t Read(void *buffer, size_t size, size_t count, void* limit_start = NULL, void* limit_end = NULL)
{
if (!m_fp) return 0;
clamp_buffer(buffer, size, limit_start, limit_end);
return fread(buffer, size, count, m_fp);
}
//////////////////////////////////////////////////////////

View File

@ -45,7 +45,7 @@ uint8_t* CxMemFile::GetBuffer(bool bDetachBuffer)
return m_pBuffer;
}
//////////////////////////////////////////////////////////
size_t CxMemFile::Read(void *buffer, size_t size, size_t count)
size_t CxMemFile::Read(void *buffer, size_t size, size_t count, void* limit_start, void* limit_end)
{
if (buffer==NULL) return 0;
@ -53,7 +53,7 @@ size_t CxMemFile::Read(void *buffer, size_t size, size_t count)
if (m_Position >= (int32_t)m_Size){
m_bEOF = true;
return 0;
}
}
int32_t nCount = (int32_t)(count*size);
if (nCount == 0) return 0;

View File

@ -14,7 +14,7 @@ public:
uint8_t* GetBuffer(bool bDetachBuffer = true);
virtual bool Close();
virtual size_t Read(void *buffer, size_t size, size_t count);
virtual size_t Read(void *buffer, size_t size, size_t count, void* limit_start = NULL, void* limit_end = NULL);
virtual size_t Write(const void *buffer, size_t size, size_t count);
virtual bool Seek(int32_t offset, int32_t origin);
virtual int32_t Tell();