Skip to content

Commit

Permalink
Fix 25 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
K0R0L committed Oct 13, 2020
1 parent 66a6b7a commit 88cf60a
Show file tree
Hide file tree
Showing 18 changed files with 233 additions and 87 deletions.
151 changes: 103 additions & 48 deletions DesktopEditor/common/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,60 +159,90 @@ namespace NSFile
else if (0x00 == (byteMain & 0x20))
{
// 2 byte
int val = (int)(((byteMain & 0x1F) << 6) |
(pBuffer[lIndex + 1] & 0x3F));
int val = 0;
if ((lIndex + 1) < lCount)
{
val = (int)(((byteMain & 0x1F) << 6) |
(pBuffer[lIndex + 1] & 0x3F));
}

pUnicodeString[lIndexUnicode++] = (WCHAR)(val);
lIndex += 2;
}
else if (0x00 == (byteMain & 0x10))
{
// 3 byte
int val = (int)(((byteMain & 0x0F) << 12) |
((pBuffer[lIndex + 1] & 0x3F) << 6) |
(pBuffer[lIndex + 2] & 0x3F));
int val = 0;
if ((lIndex + 2) < lCount)
{
val = (int)(((byteMain & 0x0F) << 12) |
((pBuffer[lIndex + 1] & 0x3F) << 6) |
(pBuffer[lIndex + 2] & 0x3F));
}

pUnicodeString[lIndexUnicode++] = (WCHAR)(val);
lIndex += 3;
}
else if (0x00 == (byteMain & 0x0F))
{
// 4 byte
int val = (int)(((byteMain & 0x07) << 18) |
((pBuffer[lIndex + 1] & 0x3F) << 12) |
((pBuffer[lIndex + 2] & 0x3F) << 6) |
(pBuffer[lIndex + 3] & 0x3F));
int val = 0;
if ((lIndex + 3) < lCount)
{
val = (int)(((byteMain & 0x07) << 18) |
((pBuffer[lIndex + 1] & 0x3F) << 12) |
((pBuffer[lIndex + 2] & 0x3F) << 6) |
(pBuffer[lIndex + 3] & 0x3F));
}

pUnicodeString[lIndexUnicode++] = (WCHAR)(val);
lIndex += 4;
}
else if (0x00 == (byteMain & 0x08))
{
// 4 byte
int val = (int)(((byteMain & 0x07) << 18) |
((pBuffer[lIndex + 1] & 0x3F) << 12) |
((pBuffer[lIndex + 2] & 0x3F) << 6) |
(pBuffer[lIndex + 3] & 0x3F));
int val = 0;
if ((lIndex + 3) < lCount)
{
val = (int)(((byteMain & 0x07) << 18) |
((pBuffer[lIndex + 1] & 0x3F) << 12) |
((pBuffer[lIndex + 2] & 0x3F) << 6) |
(pBuffer[lIndex + 3] & 0x3F));
}

pUnicodeString[lIndexUnicode++] = (WCHAR)(val);
lIndex += 4;
}
else if (0x00 == (byteMain & 0x04))
{
// 5 byte
int val = (int)(((byteMain & 0x03) << 24) |
((pBuffer[lIndex + 1] & 0x3F) << 18) |
((pBuffer[lIndex + 2] & 0x3F) << 12) |
((pBuffer[lIndex + 3] & 0x3F) << 6) |
(pBuffer[lIndex + 4] & 0x3F));
int val = 0;
if ((lIndex + 4) < lCount)
{
val = (int)(((byteMain & 0x03) << 24) |
((pBuffer[lIndex + 1] & 0x3F) << 18) |
((pBuffer[lIndex + 2] & 0x3F) << 12) |
((pBuffer[lIndex + 3] & 0x3F) << 6) |
(pBuffer[lIndex + 4] & 0x3F));
}

pUnicodeString[lIndexUnicode++] = (WCHAR)(val);
lIndex += 5;
}
else
{
// 6 byte
int val = (int)(((byteMain & 0x01) << 30) |
((pBuffer[lIndex + 1] & 0x3F) << 24) |
((pBuffer[lIndex + 2] & 0x3F) << 18) |
((pBuffer[lIndex + 3] & 0x3F) << 12) |
((pBuffer[lIndex + 4] & 0x3F) << 6) |
(pBuffer[lIndex + 5] & 0x3F));
int val = 0;
if ((lIndex + 5) < lCount)
{
val = (int)(((byteMain & 0x01) << 30) |
((pBuffer[lIndex + 1] & 0x3F) << 24) |
((pBuffer[lIndex + 2] & 0x3F) << 18) |
((pBuffer[lIndex + 3] & 0x3F) << 12) |
((pBuffer[lIndex + 4] & 0x3F) << 6) |
(pBuffer[lIndex + 5] & 0x3F));
}

pUnicodeString[lIndexUnicode++] = (WCHAR)(val);
lIndex += 5;
}
Expand Down Expand Up @@ -242,64 +272,89 @@ namespace NSFile
else if (0x00 == (byteMain & 0x20))
{
// 2 byte
int val = (int)(((byteMain & 0x1F) << 6) |
(pBuffer[lIndex + 1] & 0x3F));
int val = 0;
if ((lIndex + 1) < lCount)
{
val = (int)(((byteMain & 0x1F) << 6) |
(pBuffer[lIndex + 1] & 0x3F));
}

*pUnicodeString++ = (WCHAR)(val);
lIndex += 2;
}
else if (0x00 == (byteMain & 0x10))
{
// 3 byte
int val = (int)(((byteMain & 0x0F) << 12) |
((pBuffer[lIndex + 1] & 0x3F) << 6) |
(pBuffer[lIndex + 2] & 0x3F));
int val = 0;
if ((lIndex + 2) < lCount)
{
val = (int)(((byteMain & 0x0F) << 12) |
((pBuffer[lIndex + 1] & 0x3F) << 6) |
(pBuffer[lIndex + 2] & 0x3F));
}

WriteUtf16_WCHAR(val, pUnicodeString);
lIndex += 3;
}
else if (0x00 == (byteMain & 0x0F))
{
// 4 byte
int val = (int)(((byteMain & 0x07) << 18) |
((pBuffer[lIndex + 1] & 0x3F) << 12) |
((pBuffer[lIndex + 2] & 0x3F) << 6) |
(pBuffer[lIndex + 3] & 0x3F));
int val = 0;
if ((lIndex + 3) < lCount)
{
val = (int)(((byteMain & 0x07) << 18) |
((pBuffer[lIndex + 1] & 0x3F) << 12) |
((pBuffer[lIndex + 2] & 0x3F) << 6) |
(pBuffer[lIndex + 3] & 0x3F));
}

WriteUtf16_WCHAR(val, pUnicodeString);
lIndex += 4;
}
else if (0x00 == (byteMain & 0x08))
{
// 4 byte
int val = (int)(((byteMain & 0x07) << 18) |
((pBuffer[lIndex + 1] & 0x3F) << 12) |
((pBuffer[lIndex + 2] & 0x3F) << 6) |
(pBuffer[lIndex + 3] & 0x3F));
int val = 0;
if ((lIndex + 3) < lCount)
{
val = (int)(((byteMain & 0x07) << 18) |
((pBuffer[lIndex + 1] & 0x3F) << 12) |
((pBuffer[lIndex + 2] & 0x3F) << 6) |
(pBuffer[lIndex + 3] & 0x3F));
}

WriteUtf16_WCHAR(val, pUnicodeString);
lIndex += 4;
}
else if (0x00 == (byteMain & 0x04))
{
// 5 byte
int val = (int)(((byteMain & 0x03) << 24) |
((pBuffer[lIndex + 1] & 0x3F) << 18) |
((pBuffer[lIndex + 2] & 0x3F) << 12) |
((pBuffer[lIndex + 3] & 0x3F) << 6) |
(pBuffer[lIndex + 4] & 0x3F));
int val = 0;
if ((lIndex + 4) < lCount)
{
val = (int)(((byteMain & 0x03) << 24) |
((pBuffer[lIndex + 1] & 0x3F) << 18) |
((pBuffer[lIndex + 2] & 0x3F) << 12) |
((pBuffer[lIndex + 3] & 0x3F) << 6) |
(pBuffer[lIndex + 4] & 0x3F));
}

WriteUtf16_WCHAR(val, pUnicodeString);
lIndex += 5;
}
else
{
// 6 byte
int val = (int)(((byteMain & 0x01) << 30) |
((pBuffer[lIndex + 1] & 0x3F) << 24) |
((pBuffer[lIndex + 2] & 0x3F) << 18) |
((pBuffer[lIndex + 3] & 0x3F) << 12) |
((pBuffer[lIndex + 4] & 0x3F) << 6) |
(pBuffer[lIndex + 5] & 0x3F));
int val = 0;
if ((lIndex + 5) < lCount)
{
val = (int)(((byteMain & 0x01) << 30) |
((pBuffer[lIndex + 1] & 0x3F) << 24) |
((pBuffer[lIndex + 2] & 0x3F) << 18) |
((pBuffer[lIndex + 3] & 0x3F) << 12) |
((pBuffer[lIndex + 4] & 0x3F) << 6) |
(pBuffer[lIndex + 5] & 0x3F));
}

WriteUtf16_WCHAR(val, pUnicodeString);
lIndex += 5;
Expand Down
29 changes: 26 additions & 3 deletions DesktopEditor/cximage/CxImage/xfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -72,4 +72,27 @@ class DLL_EXP CxFile
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
14 changes: 7 additions & 7 deletions DesktopEditor/cximage/CxImage/ximabmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ 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;
pRgb[i].rgbGreen = ((RGBTRIPLE *)pRgb)[i].rgbtGreen;
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;
}
Expand All @@ -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){
Expand Down Expand Up @@ -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 :
Expand All @@ -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;
Expand All @@ -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 :
{
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions DesktopEditor/cximage/CxImage/ximage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
////////////////////////////////////////////////////////////////////////////////
/**
Expand Down Expand Up @@ -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){
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 88cf60a

Please sign in to comment.