Skip to content

Commit

Permalink
permit 4GB blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristy committed Dec 27, 2021
1 parent e26d718 commit 23b4299
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions coders/mat.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ static void ReadBlobDoublesMSB(Image * image, size_t len, double *data)
}

/* Calculate minimum and maximum from a given block of data */
static void CalcMinMax(Image *image, int endian_indicator, int SizeX, int SizeY, size_t CellType, unsigned ldblk, void *BImgBuff, double *Min, double *Max)
static void CalcMinMax(Image *image, int endian_indicator, int SizeX, int SizeY, size_t CellType, size_t ldblk, void *BImgBuff, double *Min, double *Max)
{
MagickOffsetType filepos;
int i, x;
Expand Down Expand Up @@ -431,8 +431,11 @@ float *fltrow;
}


static void FixSignedValues(PixelPacket *q, int y)
static void FixSignedValues(PixelPacket *q, unsigned int y)
{
if (y == 0)
return;

while(y-->0)
{
/* Please note that negative values will overflow
Expand All @@ -447,11 +450,14 @@ static void FixSignedValues(PixelPacket *q, int y)


/** Fix whole row of logical/binary data. It means pack it. */
static void FixLogical(unsigned char *Buff,int ldblk)
static void FixLogical(unsigned char *Buff,size_t ldblk)
{
unsigned char mask=128;
unsigned char *BuffL = Buff;
unsigned char val = 0;
unsigned char mask=128;
unsigned char *BuffL = Buff;
unsigned char val = 0;

if (ldblk == 0)
return;

while(ldblk-->0)
{
Expand Down Expand Up @@ -612,7 +618,7 @@ static Image *ReadMATImageV4(const ImageInfo *image_info,Image *image,
unsigned int nameLen;
} MAT4_HDR;

long
size_t
ldblk;

EndianType
Expand Down Expand Up @@ -656,7 +662,7 @@ static Image *ReadMATImageV4(const ImageInfo *image_info,Image *image,
ldblk=ReadBlobLSBLong(image);
if (EOFBlob(image) != MagickFalse)
break;
if ((ldblk > 9999) || (ldblk < 0))
if (ldblk > 9999)
break;
HDR.Type[3]=ldblk % 10; ldblk /= 10; /* T digit */
HDR.Type[2]=ldblk % 10; ldblk /= 10; /* P digit */
Expand Down Expand Up @@ -891,7 +897,7 @@ static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
QuantumInfo *quantum_info;
ImageInfo *clone_info;
int i;
ssize_t ldblk;
size_t ldblk;
unsigned char *BImgBuff = NULL;
double MinVal, MaxVal;
size_t Unknown6;
Expand Down Expand Up @@ -1150,25 +1156,25 @@ static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
image->depth = 1;
else
image->depth = 8; /* Byte type cell */
ldblk = (ssize_t) MATLAB_HDR.SizeX;
ldblk = (size_t) MATLAB_HDR.SizeX;
break;
case miINT16:
case miUINT16:
sample_size = 16;
image->depth = 16; /* Word type cell */
ldblk = (ssize_t) (2 * MATLAB_HDR.SizeX);
ldblk = (size_t) (2 * MATLAB_HDR.SizeX);
break;
case miINT32:
case miUINT32:
sample_size = 32;
image->depth = 32; /* Dword type cell */
ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX);
ldblk = (size_t) (4 * MATLAB_HDR.SizeX);
break;
case miINT64:
case miUINT64:
sample_size = 64;
image->depth = 64; /* Qword type cell */
ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX);
ldblk = (size_t) (8 * MATLAB_HDR.SizeX);
break;
case miSINGLE:
sample_size = 32;
Expand All @@ -1177,7 +1183,7 @@ static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
{ /* complex float type cell */
}
ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX);
ldblk = (size_t) (4 * MATLAB_HDR.SizeX);
break;
case miDOUBLE:
sample_size = 64;
Expand All @@ -1196,7 +1202,7 @@ RestoreMSCWarning
if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
{ /* complex double type cell */
}
ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX);
ldblk = (size_t) (8 * MATLAB_HDR.SizeX);
break;
default:
if ((image != image2) && (image2 != (Image *) NULL))
Expand All @@ -1211,7 +1217,7 @@ RestoreMSCWarning
image->colors = GetQuantumRange(image->depth);
if (image->columns == 0 || image->rows == 0)
goto MATLAB_KO;
if((unsigned int)ldblk*MATLAB_HDR.SizeY > MATLAB_HDR.ObjectSize)
if((size_t)ldblk*MATLAB_HDR.SizeY > MATLAB_HDR.ObjectSize)
goto MATLAB_KO;
/* Image is gray when no complex flag is set and 2D Matrix */
if ((MATLAB_HDR.DimFlag == 8) &&
Expand Down Expand Up @@ -1257,7 +1263,7 @@ RestoreMSCWarning
}

/* ----- Load raster data ----- */
BImgBuff = (unsigned char *) AcquireQuantumMemory((size_t) (ldblk),sizeof(double)); /* Ldblk was set in the check phase */
BImgBuff = (unsigned char *) AcquireQuantumMemory(ldblk,sizeof(double)); /* Ldblk was set in the check phase */
if (BImgBuff == NULL)
{
if (clone_info != (ImageInfo *) NULL)
Expand Down Expand Up @@ -1289,7 +1295,7 @@ RestoreMSCWarning
" MAT set image pixels returns unexpected NULL on a row %u.", (unsigned)(MATLAB_HDR.SizeY-i-1));
goto done_reading; /* Skip image rotation, when cannot set image pixels */
}
if(ReadBlob(image2,ldblk,(unsigned char *)BImgBuff) != (ssize_t) ldblk)
if(ReadBlob(image2,ldblk,(unsigned char *)BImgBuff) != ldblk)
{
if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
" MAT cannot read scanrow %u from a file.", (unsigned)(MATLAB_HDR.SizeY-i-1));
Expand Down

0 comments on commit 23b4299

Please sign in to comment.