$OpenBSD: patch-Imlib_load_c,v 1.8 2013/09/25 07:56:59 sthen Exp $

handle giflib-5.0+
https://projects.archlinux.org/svntogit/packages.git/plain/trunk/imlib-giflib5.patch?h=packages/imlib

and other patches

--- Imlib/load.c.orig	Fri Mar 22 07:43:04 2002
+++ Imlib/load.c	Thu Sep 19 05:44:29 2013
@@ -4,6 +4,14 @@
 #include "Imlib_private.h"
 #include <setjmp.h>
 
+static void
+localPrintGifError(void)
+{
+    int ErrorCode=0;
+    GifErrorString(ErrorCode);
+    fprintf(stderr, "\nGIF-LIB error: %i.\n", ErrorCode);
+}
+
 /*      Split the ID - damages input    */
 
 static char        *
@@ -191,12 +199,12 @@ _LoadPNG(ImlibData * id, FILE * f, int *w, int *h, int
       png_destroy_read_struct(&png_ptr, NULL, NULL);
       return NULL;
     }
-  if (setjmp(png_ptr->jmpbuf))
+  if (setjmp(png_jmpbuf(png_ptr)))
     {
       png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
       return NULL;
     }
-  if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
+  if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB_ALPHA)
     {
       png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
       return NULL;
@@ -254,7 +262,8 @@ _LoadPNG(ImlibData * id, FILE * f, int *w, int *h, int
   png_read_image(png_ptr, lines);
   png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
   ptr = data;
-  if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+  if (color_type == PNG_COLOR_TYPE_GRAY
+      || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
     {
       for (y = 0; y < *h; y++)
 	{
@@ -279,6 +288,7 @@ _LoadPNG(ImlibData * id, FILE * f, int *w, int *h, int
 	    }
 	}
     }
+#if 0
   else if (color_type == PNG_COLOR_TYPE_GRAY)
     {
       for (y = 0; y < *h; y++)
@@ -294,6 +304,7 @@ _LoadPNG(ImlibData * id, FILE * f, int *w, int *h, int
 	    }
 	}
     }
+#endif
   else
     {
       for (y = 0; y < *h; y++)
@@ -433,6 +444,7 @@ _LoadGIF(ImlibData * id, FILE *f, int *w, int *h, int 
   int                 intjump[] = {8, 8, 4, 2};
   int                 istransp, transp;
   int                 fd;
+  int                 Error;
 
   done = 0;
   istransp = 0;
@@ -443,7 +455,7 @@ _LoadGIF(ImlibData * id, FILE *f, int *w, int *h, int 
   fd = fileno(f);
   /* Apparently rewind(f) isn't sufficient */
   lseek(fd, (long) 0, 0);
-  gif = DGifOpenFileHandle(fd);
+  gif = DGifOpenFileHandle(fd, &Error);
 
   if (!gif)
     return NULL;
@@ -451,14 +463,14 @@ _LoadGIF(ImlibData * id, FILE *f, int *w, int *h, int 
     {
       if (DGifGetRecordType(gif, &rec) == GIF_ERROR)
 	{
-	  PrintGifError();
+	  localPrintGifError();
 	  rec = TERMINATE_RECORD_TYPE;
 	}
       if ((rec == IMAGE_DESC_RECORD_TYPE) && (!done))
 	{
 	  if (DGifGetImageDesc(gif) == GIF_ERROR)
 	    {
-	      PrintGifError();
+	      localPrintGifError();
 	      rec = TERMINATE_RECORD_TYPE;
 	    }
 	  *w = gif->Image.Width;
@@ -631,12 +643,12 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
   fread(dbuf, 4, 2, file);
   *w = (int)dbuf[0];
   *h = (int)dbuf[1];
-  if (*w > 32767)
+  if ((*w < 0) || (*w > 32767))
     {
       fprintf(stderr, "IMLIB ERROR: Image width > 32767 pixels for file\n");
       return NULL;
     }
-  if (*h > 32767)
+  if ((*h < 0) || (*h > 32767))
     {
       fprintf(stderr, "IMLIB ERROR: Image height > 32767 pixels for file\n");
       return NULL;
@@ -645,9 +657,9 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
   planes = (int)word;
   fread(&word, 2, 1, file);
   bpp = (int)word;
-  if (bpp != 1 && bpp != 4 && bpp != 8 && bpp && 16 && bpp != 24 && bpp != 32)
+  if (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32)
     {
-      fprintf(stderr, "IMLIB ERROR: unknown bitdepth in file\n");
+      fprintf(stderr, "IMLIB ERROR: unknown bitdepth %d in file\n", bpp);
       return NULL;
     }
   fread(dbuf, 4, 4, file);
@@ -661,6 +673,9 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
   ncolors = (int)dbuf[0];
   if (ncolors == 0)
     ncolors = 1 << bpp;
+  if ((ncolors < 0) || (ncolors > (1 << bpp)))
+    ncolors = 1 << bpp;
+
   /* some more sanity checks */
   if (((comp == BI_RLE4) && (bpp != 4)) || ((comp == BI_RLE8) && (bpp != 8)) || ((comp == BI_BITFIELDS) && (bpp != 16 && bpp != 32)))
     {
@@ -786,9 +801,13 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
 		  for (bit = 0; bit < 8; bit++)
 		    {
 		      index = ((byte & (0x80 >> bit)) ? 1 : 0);
+		      /* possibly corrupted file? */
+		      if (index < ncolors && poffset < *w * *h * 3)
+			{
 		      ptr[poffset] = cmap[index].r;
 		      ptr[poffset + 1] = cmap[index].g;
 		      ptr[poffset + 2] = cmap[index].b;
+			}
 		      column++;
 		    }
 		}
@@ -810,9 +829,13 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
 			  index = ((byte & (0xF0 >> nibble * 4)) >> (!nibble * 4));
 			  if (index >= 16)
 			    index = 15;
+			  /* possibly corrupted file? */
+			  if (index < ncolors && poffset < *w * *h * 3)
+			    {
 			  ptr[poffset] = cmap[index].r;
 			  ptr[poffset + 1] = cmap[index].g;
 			  ptr[poffset + 2] = cmap[index].b;
+			    }
 			  column++;
 			}
 		    }
@@ -851,9 +874,13 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
 				{
 				  linepos++;
 				  byte = getc(file);
+				  /* possibly corrupted file? */
+				  if (byte < ncolors && poffset < *w * *h * 3)
+				    {
 				  ptr[poffset] = cmap[byte].r;
 				  ptr[poffset + 1] = cmap[byte].g;
 				  ptr[poffset + 2] = cmap[byte].b;
+				    }
 				  column++;
 				}
 			      if (absolute & 0x01)
@@ -864,9 +891,13 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
 			{
 			  for (i = 0; i < first; i++)
 			    {
+			      /* possibly corrupted file? */
+			      if (byte < ncolors && poffset < *w * *h * 3)
+				{
 			      ptr[poffset] = cmap[byte].r;
 			      ptr[poffset + 1] = cmap[byte].g;
 			      ptr[poffset + 2] = cmap[byte].b;
+				}
 			      column++;
 			      linepos++;
 			    }
@@ -874,9 +905,13 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
 		    }
 		  else
 		    {
+		      /* possibly corrupted file? */
+		      if (byte < ncolors && poffset < *w * *h * 3)
+			{
 		      ptr[poffset] = cmap[byte].r;
 		      ptr[poffset + 1] = cmap[byte].g;
 		      ptr[poffset + 2] = cmap[byte].b;
+			}
 		      column++;
 		    }
 		}
@@ -884,9 +919,12 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
 	  else if (bpp == 24)
 	    {
 	      linepos += fread(bbuf, 1, 3, file);
+	      if (poffset< *w * *h * 3)
+		{
 	      ptr[poffset] = (unsigned char)bbuf[2];
 	      ptr[poffset + 1] = (unsigned char)bbuf[1];
 	      ptr[poffset + 2] = (unsigned char)bbuf[0];
+		}
 	      column++;
 	    }
 	  else if (bpp == 16)
@@ -894,12 +932,16 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
 	      unsigned char       temp;
 
 	      linepos += fread(&word, 2, 1, file);
+	      /* possibly corrupted file? */
+	      if (poffset < *w * *h * 3)
+		{
 	      temp = (word & rmask) >> rshift;
 	      ptr[poffset] = temp;
 	      temp = (word & gmask) >> gshift;
 	      ptr[poffset + 1] = temp;
 	      temp = (word & bmask) >> gshift;
 	      ptr[poffset + 2] = temp;
+		}
 	      column++;
 	    }
 	  else
@@ -907,12 +949,16 @@ _LoadBMP(ImlibData * id, FILE *file, int *w, int *h, i
 	      unsigned char       temp;
 
 	      linepos += fread(&dword, 4, 1, file);
+	      /* possibly corrupted file? */
+	      if (poffset < *w * *h * 3)
+		{
 	      temp = (dword & rmask) >> rshift;
 	      ptr[poffset] = temp;
 	      temp = (dword & gmask) >> gshift;
 	      ptr[poffset + 1] = temp;
 	      temp = (dword & bmask) >> bshift;
 	      ptr[poffset + 2] = temp;
+		}
 	      column++;
 	    }
 	}
@@ -1061,7 +1107,7 @@ _LoadXPM(ImlibData * id, FILE *file, int *w, int *h, i
 			  if (line[k] != ' ')
 			    {
 			      s[0] = 0;
-			      sscanf(&line[k], "%256s", s);
+			      sscanf(&line[k], "%255s", s);
 			      slen = strlen(s);
 			      k += slen;
 			      if (!strcmp(s, "c"))
@@ -1861,7 +1907,7 @@ Imlib_load_image(ImlibData * id, char *file)
 	  free(im);
 	  return NULL;
 	}
-      sscanf(s, "%256s %i", s1, &num);
+      sscanf(s, "%255s %i", s1, &num);
       if (num <= 0)
 	{
 	  fclose(p);
@@ -1870,10 +1916,10 @@ Imlib_load_image(ImlibData * id, char *file)
 	}
       while (fgets(s, 4096, p))
 	{
-	  sscanf(s, "%256s", s1);
+	  sscanf(s, "%255s", s1);
 	  if (!strcmp("IMAGE", s1))
 	    {
-	      sscanf(s, "%256s %i %256s %i %i %i %i %i %i %i %i %i", s1, &size,
+	      sscanf(s, "%255s %i %255s %i %i %i %i %i %i %i %i %i", s1, &size,
 		     s2, &w, &h, &r, &g, &b, &bl, &br, &bt, &bb);
 	      if (!iden[0])
 		break;
