aboutsummaryrefslogtreecommitdiff
path: root/src/share/native/sun/awt/giflib/dgif_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/share/native/sun/awt/giflib/dgif_lib.c')
-rw-r--r--src/share/native/sun/awt/giflib/dgif_lib.c122
1 files changed, 86 insertions, 36 deletions
diff --git a/src/share/native/sun/awt/giflib/dgif_lib.c b/src/share/native/sun/awt/giflib/dgif_lib.c
index 1c1b217654..0a59e2bcb4 100644
--- a/src/share/native/sun/awt/giflib/dgif_lib.c
+++ b/src/share/native/sun/awt/giflib/dgif_lib.c
@@ -36,13 +36,16 @@ two modules will be linked. Preserve this property!
#include <limits.h>
#include <stdint.h>
#include <fcntl.h>
+/** Begin JDK modifications to support building on Windows **/
+#ifndef _WIN32
+#include <unistd.h>
+#endif
+/** End JDK modifications to support building on Windows **/
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <io.h>
-#else
-#include <unistd.h>
#endif /* _WIN32 */
#include "gif_lib.h"
@@ -52,10 +55,14 @@ two modules will be linked. Preserve this property!
#define UNSIGNED_LITTLE_ENDIAN(lo, hi) ((lo) | ((hi) << 8))
/* avoid extra function call in case we use fread (TVT) */
-#define READ(_gif,_buf,_len) \
- (((GifFilePrivateType*)_gif->Private)->Read ? \
- ((GifFilePrivateType*)_gif->Private)->Read(_gif,_buf,_len) : \
- fread(_buf,1,_len,((GifFilePrivateType*)_gif->Private)->File))
+/** JDK modification "inline" is dropped to support c89 **/
+static /**inline**/ int InternalRead(GifFileType *gif, GifByteType *buf, int len) {
+ //fprintf(stderr, "### Read: %d\n", len);
+ return
+ (((GifFilePrivateType*)gif->Private)->Read ?
+ ((GifFilePrivateType*)gif->Private)->Read(gif,buf,len) :
+ fread(buf,1,len,((GifFilePrivateType*)gif->Private)->File));
+}
static int DGifGetWord(GifFileType *GifFile, GifWord *Word);
static int DGifSetupDecompress(GifFileType *GifFile);
@@ -142,7 +149,7 @@ DGifOpenFileHandle(int FileHandle, int *Error)
/* Let's see if this is a GIF file: */
/* coverity[check_return] */
- if (READ(GifFile, (unsigned char *)Buf, GIF_STAMP_LEN) != GIF_STAMP_LEN) {
+ if (InternalRead(GifFile, (unsigned char *)Buf, GIF_STAMP_LEN) != GIF_STAMP_LEN) {
if (Error != NULL)
*Error = D_GIF_ERR_READ_FAILED;
(void)fclose(f);
@@ -219,7 +226,7 @@ DGifOpen(void *userData, InputFunc readFunc, int *Error)
/* Lets see if this is a GIF file: */
/* coverity[check_return] */
- if (READ(GifFile, (unsigned char *)Buf, GIF_STAMP_LEN) != GIF_STAMP_LEN) {
+ if (InternalRead(GifFile, (unsigned char *)Buf, GIF_STAMP_LEN) != GIF_STAMP_LEN) {
if (Error != NULL)
*Error = D_GIF_ERR_READ_FAILED;
free((char *)Private);
@@ -276,7 +283,7 @@ DGifGetScreenDesc(GifFileType *GifFile)
DGifGetWord(GifFile, &GifFile->SHeight) == GIF_ERROR)
return GIF_ERROR;
- if (READ(GifFile, Buf, 3) != 3) {
+ if (InternalRead(GifFile, Buf, 3) != 3) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
GifFreeMapObject(GifFile->SColorMap);
GifFile->SColorMap = NULL;
@@ -300,7 +307,7 @@ DGifGetScreenDesc(GifFileType *GifFile)
GifFile->SColorMap->SortFlag = SortFlag;
for (i = 0; i < GifFile->SColorMap->ColorCount; i++) {
/* coverity[check_return] */
- if (READ(GifFile, Buf, 3) != 3) {
+ if (InternalRead(GifFile, Buf, 3) != 3) {
GifFreeMapObject(GifFile->SColorMap);
GifFile->SColorMap = NULL;
GifFile->Error = D_GIF_ERR_READ_FAILED;
@@ -314,9 +321,25 @@ DGifGetScreenDesc(GifFileType *GifFile)
GifFile->SColorMap = NULL;
}
+ /*
+ * No check here for whether the background color is in range for the
+ * screen color map. Possibly there should be.
+ */
+
return GIF_OK;
}
+const char *
+DGifGetGifVersion(GifFileType *GifFile)
+{
+ GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
+
+ if (Private->gif89)
+ return GIF89_STAMP;
+ else
+ return GIF87_STAMP;
+}
+
/******************************************************************************
This routine should be called before any attempt to read an image.
******************************************************************************/
@@ -333,11 +356,12 @@ DGifGetRecordType(GifFileType *GifFile, GifRecordType* Type)
}
/* coverity[check_return] */
- if (READ(GifFile, &Buf, 1) != 1) {
+ if (InternalRead(GifFile, &Buf, 1) != 1) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
return GIF_ERROR;
}
+ //fprintf(stderr, "### DGifGetRecordType: %02x\n", Buf);
switch (Buf) {
case DESCRIPTOR_INTRODUCER:
*Type = IMAGE_DESC_RECORD_TYPE;
@@ -357,17 +381,12 @@ DGifGetRecordType(GifFileType *GifFile, GifRecordType* Type)
return GIF_OK;
}
-/******************************************************************************
- This routine should be called before any attempt to read an image.
- Note it is assumed the Image desc. header has been read.
-******************************************************************************/
int
-DGifGetImageDesc(GifFileType *GifFile)
+DGifGetImageHeader(GifFileType *GifFile)
{
unsigned int BitsPerPixel;
GifByteType Buf[3];
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
- SavedImage *sp;
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
@@ -380,7 +399,7 @@ DGifGetImageDesc(GifFileType *GifFile)
DGifGetWord(GifFile, &GifFile->Image.Width) == GIF_ERROR ||
DGifGetWord(GifFile, &GifFile->Image.Height) == GIF_ERROR)
return GIF_ERROR;
- if (READ(GifFile, Buf, 1) != 1) {
+ if (InternalRead(GifFile, Buf, 1) != 1) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
GifFreeMapObject(GifFile->Image.ColorMap);
GifFile->Image.ColorMap = NULL;
@@ -407,7 +426,7 @@ DGifGetImageDesc(GifFileType *GifFile)
/* Get the image local color map: */
for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) {
/* coverity[check_return] */
- if (READ(GifFile, Buf, 3) != 3) {
+ if (InternalRead(GifFile, Buf, 3) != 3) {
GifFreeMapObject(GifFile->Image.ColorMap);
GifFile->Error = D_GIF_ERR_READ_FAILED;
GifFile->Image.ColorMap = NULL;
@@ -419,6 +438,33 @@ DGifGetImageDesc(GifFileType *GifFile)
}
}
+ Private->PixelCount = (long)GifFile->Image.Width *
+ (long)GifFile->Image.Height;
+
+ /* Reset decompress algorithm parameters. */
+ return DGifSetupDecompress(GifFile);
+}
+
+/******************************************************************************
+ This routine should be called before any attempt to read an image.
+ Note it is assumed the Image desc. header has been read.
+******************************************************************************/
+int
+DGifGetImageDesc(GifFileType *GifFile)
+{
+ GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
+ SavedImage *sp;
+
+ if (!IS_READABLE(Private)) {
+ /* This file was NOT open for reading: */
+ GifFile->Error = D_GIF_ERR_NOT_READABLE;
+ return GIF_ERROR;
+ }
+
+ if (DGifGetImageHeader(GifFile) == GIF_ERROR) {
+ return GIF_ERROR;
+ }
+
if (GifFile->SavedImages) {
SavedImage* new_saved_images =
(SavedImage *)reallocarray(GifFile->SavedImages,
@@ -453,11 +499,7 @@ DGifGetImageDesc(GifFileType *GifFile)
GifFile->ImageCount++;
- Private->PixelCount = (long)GifFile->Image.Width *
- (long)GifFile->Image.Height;
-
- /* Reset decompress algorithm parameters. */
- return DGifSetupDecompress(GifFile);
+ return GIF_OK;
}
/******************************************************************************
@@ -550,6 +592,7 @@ DGifGetExtension(GifFileType *GifFile, int *ExtCode, GifByteType **Extension)
GifByteType Buf;
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
+ //fprintf(stderr, "### -> DGifGetExtension:\n");
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
GifFile->Error = D_GIF_ERR_NOT_READABLE;
@@ -557,11 +600,12 @@ DGifGetExtension(GifFileType *GifFile, int *ExtCode, GifByteType **Extension)
}
/* coverity[check_return] */
- if (READ(GifFile, &Buf, 1) != 1) {
+ if (InternalRead(GifFile, &Buf, 1) != 1) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
return GIF_ERROR;
}
*ExtCode = Buf;
+ //fprintf(stderr, "### <- DGifGetExtension: %02x, about to call next\n", Buf);
return DGifGetExtensionNext(GifFile, Extension);
}
@@ -577,20 +621,24 @@ DGifGetExtensionNext(GifFileType *GifFile, GifByteType ** Extension)
GifByteType Buf;
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
- if (READ(GifFile, &Buf, 1) != 1) {
+ //fprintf(stderr, "### -> DGifGetExtensionNext\n");
+ if (InternalRead(GifFile, &Buf, 1) != 1) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
return GIF_ERROR;
}
+ //fprintf(stderr, "### DGifGetExtensionNext sees %d\n", Buf);
+
if (Buf > 0) {
*Extension = Private->Buf; /* Use private unused buffer. */
(*Extension)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
/* coverity[tainted_data,check_return] */
- if (READ(GifFile, &((*Extension)[1]), Buf) != Buf) {
+ if (InternalRead(GifFile, &((*Extension)[1]), Buf) != Buf) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
return GIF_ERROR;
}
} else
*Extension = NULL;
+ //fprintf(stderr, "### <- DGifGetExtensionNext: %p\n", Extension);
return GIF_OK;
}
@@ -707,7 +755,7 @@ DGifGetWord(GifFileType *GifFile, GifWord *Word)
unsigned char c[2];
/* coverity[check_return] */
- if (READ(GifFile, c, 2) != 2) {
+ if (InternalRead(GifFile, c, 2) != 2) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
return GIF_ERROR;
}
@@ -752,7 +800,7 @@ DGifGetCodeNext(GifFileType *GifFile, GifByteType **CodeBlock)
/* coverity[tainted_data_argument] */
/* coverity[check_return] */
- if (READ(GifFile, &Buf, 1) != 1) {
+ if (InternalRead(GifFile, &Buf, 1) != 1) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
return GIF_ERROR;
}
@@ -762,7 +810,7 @@ DGifGetCodeNext(GifFileType *GifFile, GifByteType **CodeBlock)
*CodeBlock = Private->Buf; /* Use private unused buffer. */
(*CodeBlock)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
/* coverity[tainted_data] */
- if (READ(GifFile, &((*CodeBlock)[1]), Buf) != Buf) {
+ if (InternalRead(GifFile, &((*CodeBlock)[1]), Buf) != Buf) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
return GIF_ERROR;
}
@@ -787,7 +835,7 @@ DGifSetupDecompress(GifFileType *GifFile)
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
/* coverity[check_return] */
- if (READ(GifFile, &CodeSize, 1) < 1) { /* Read Code size from file. */
+ if (InternalRead(GifFile, &CodeSize, 1) < 1) { /* Read Code size from file. */
return GIF_ERROR; /* Failed to read Code size. */
}
BitsPerPixel = CodeSize;
@@ -921,7 +969,7 @@ DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line, int LineLen)
while (StackPtr != 0 && i < LineLen)
Line[i++] = Stack[--StackPtr];
}
- if (LastCode != NO_SUCH_CODE && Prefix[Private->RunningCode - 2] == NO_SUCH_CODE) {
+ if (LastCode != NO_SUCH_CODE && Private->RunningCode - 2 < LZ_MAX_CODE && Prefix[Private->RunningCode - 2] == NO_SUCH_CODE) {
Prefix[Private->RunningCode - 2] = LastCode;
if (CrntCode == Private->RunningCode - 2) {
@@ -1069,7 +1117,7 @@ DGifBufferedInput(GifFileType *GifFile, GifByteType *Buf, GifByteType *NextByte)
if (Buf[0] == 0) {
/* Needs to read the next buffer - this one is empty: */
/* coverity[check_return] */
- if (READ(GifFile, Buf, 1) != 1) {
+ if (InternalRead(GifFile, Buf, 1) != 1) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
return GIF_ERROR;
}
@@ -1081,7 +1129,7 @@ DGifBufferedInput(GifFileType *GifFile, GifByteType *Buf, GifByteType *NextByte)
GifFile->Error = D_GIF_ERR_IMAGE_DEFECT;
return GIF_ERROR;
}
- if (READ(GifFile, &Buf[1], Buf[0]) != Buf[0]) {
+ if (InternalRead(GifFile, &Buf[1], Buf[0]) != Buf[0]) {
GifFile->Error = D_GIF_ERR_READ_FAILED;
return GIF_ERROR;
}
@@ -1124,7 +1172,7 @@ DGifSlurp(GifFileType *GifFile)
sp = &GifFile->SavedImages[GifFile->ImageCount - 1];
/* Allocate memory for the image */
- if (sp->ImageDesc.Width < 0 && sp->ImageDesc.Height < 0 &&
+ if (sp->ImageDesc.Width <= 0 || sp->ImageDesc.Height <= 0 ||
sp->ImageDesc.Width > (INT_MAX / sp->ImageDesc.Height)) {
return GIF_ERROR;
}
@@ -1184,9 +1232,11 @@ DGifSlurp(GifFileType *GifFile)
== GIF_ERROR)
return (GIF_ERROR);
}
- while (ExtData != NULL) {
+ for (;;) {
if (DGifGetExtensionNext(GifFile, &ExtData) == GIF_ERROR)
return (GIF_ERROR);
+ if (ExtData == NULL)
+ break;
/* Continue the extension block */
if (ExtData != NULL)
if (GifAddExtensionBlock(&GifFile->ExtensionBlockCount,