Commit 0885ccdf8 for imagemagick.org

commit 0885ccdf88ecf8141bf42c1822a6dd8a47cceef8
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Tue Dec 1 19:26:26 2026 -0500

    only 1, 2, 4, 8 and 24 bits-per-pixel are accepted now

diff --git a/coders/wpg.c b/coders/wpg.c
index a3589e869..09c264ba2 100644
--- a/coders/wpg.c
+++ b/coders/wpg.c
@@ -229,7 +229,7 @@ static const RGB_Record WPG1_Palette[256]=
 %
 %  The format of the IsWPG method is:
 %
-%      unsigned int IsWPG(const unsigned char *magick,const size_t length)
+%      MagickBooleanType IsWPG(const unsigned char *magick,const size_t length)
 %
 %  A description of each parameter follows:
 %
@@ -240,7 +240,7 @@ static const RGB_Record WPG1_Palette[256]=
 %    o length: Specifies the length of the magick string.
 %
 */
-static unsigned int IsWPG(const unsigned char *magick,const size_t length)
+static MagickBooleanType IsWPG(const unsigned char *magick,const size_t length)
 {
   if (length < 4)
     return(MagickFalse);
@@ -255,24 +255,101 @@ static int Rd_WP_DWORD(Image *image,size_t *d)
   int
     b;

+  /*
+    Read a WordPerfect variable-length integer; returns the number of bytes
+    consumed, or 0 (with *d set to 0) if the blob ended prematurely.
+  */
+  *d=0;
   b=ReadBlobByte(image);
-  *d=b;
+  if (b == EOF)
+    return(0);
+  *d=(size_t) b;
   if (b < 0xff)
     return(1);
   b=ReadBlobByte(image);
+  if (b == EOF)
+    {
+      *d=0;
+      return(0);
+    }
   *d=(size_t) b;
   b=ReadBlobByte(image);
-  *d+=(size_t) b*256l;
+  if (b == EOF)
+    {
+      *d=0;
+      return(0);
+    }
+  *d+=(size_t) b*256;
   if (*d < 0x8000)
     return(3);
   *d=(*d & 0x7fff) << 16;
   b=ReadBlobByte(image);
+  if (b == EOF)
+    {
+      *d=0;
+      return(0);
+    }
   *d+=(size_t) b;
   b=ReadBlobByte(image);
-  *d+=(size_t) b*256l;
+  if (b == EOF)
+    {
+      *d=0;
+      return(0);
+    }
+  *d+=(size_t) b*256;
   return(5);
 }

+static inline ssize_t WPGClampToSSize(const double value)
+{
+  if (value >= 2147483647.0)
+    return((ssize_t) 2147483647);
+  if (value <= -2147483647.0)
+    return((ssize_t) -2147483647);
+  return((ssize_t) value);
+}
+
+static inline size_t WPGClampToSize(const double value)
+{
+  if (value <= 0.0)
+    return(0);
+  if (value >= 2147483647.0)
+    return((size_t) 2147483647);
+  return((size_t) value);
+}
+
+/*
+  Grow the image colormap to at least the requested number of entries while
+  preserving the entries already present.
+*/
+static MagickBooleanType WPGGrowColormap(Image *image,const size_t colors,
+  ExceptionInfo *exception)
+{
+  PixelInfo
+    *colormap;
+
+  size_t
+    old_colors;
+
+  if (image->colors >= colors)
+    return(MagickTrue);
+  colormap=image->colormap;
+  old_colors=image->colors;
+  image->colormap=(PixelInfo *) NULL;
+  if (AcquireImageColormap(image,colors,exception) == MagickFalse)
+    {
+      colormap=(PixelInfo *) RelinquishMagickMemory(colormap);
+      return(MagickFalse);
+    }
+  if (colormap != (PixelInfo *) NULL)
+    {
+      (void) memcpy(image->colormap,colormap,MagickMin(image->colors,
+        old_colors)*sizeof(*image->colormap));
+      colormap=(PixelInfo *) RelinquishMagickMemory(colormap);
+    }
+  return(MagickTrue);
+}
+
 static MagickBooleanType InsertRow(Image *image,unsigned char *p,ssize_t y,
   int bpp,ExceptionInfo *exception)
 {
@@ -439,10 +516,13 @@ static MagickBooleanType InsertRow(Image *image,unsigned char *p,ssize_t y,
 { \
   BImgBuff[x]=b; \
   x++; \
-  if((ssize_t) x>=ldblk) \
+  if (x >= ldblk) \
   { \
-    if (InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception) != MagickFalse) \
-      y++; \
+    if (y < rows) \
+      { \
+        if (InsertRow(image,BImgBuff,y,bpp,exception) != MagickFalse) \
+          y++; \
+      } \
     x=0; \
   } \
 }
@@ -450,100 +530,104 @@ static MagickBooleanType InsertRow(Image *image,unsigned char *p,ssize_t y,
 static int UnpackWPGRaster(Image *image,int bpp,ExceptionInfo *exception)
 {
   int
+    c;
+
+  ssize_t
+    i,
+    ldblk,
+    rows,
     x,
-    y,
-    i;
+    y;

   unsigned char
     bbuf,
     *BImgBuff,
     RunCount;

-  ssize_t
-    ldblk;
-
   x=0;
   y=0;
-
+  rows=(ssize_t) image->rows;
   ldblk=(ssize_t) ((bpp*(ssize_t) image->columns+7)/8);
   BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
     8*sizeof(*BImgBuff));
-  if(BImgBuff==NULL) return(-2);
+  if (BImgBuff == (unsigned char *) NULL)
+    return(-2);
   (void) memset(BImgBuff,0,(size_t) ldblk*8*sizeof(*BImgBuff));
-  while (y < (ssize_t) image->rows)
+  while (y < rows)
   {
-      int
-        c;
-
-      c=ReadBlobByte(image);
-      if (c == EOF)
-        break;
-      bbuf=(unsigned char) c;
-      RunCount=bbuf & 0x7F;
-      if(bbuf & 0x80)
-        {
-          if(RunCount)  /* repeat next byte runcount * */
-            {
-              bbuf=(unsigned char) ReadBlobByte(image);
-              for(i=0;i<(int) RunCount;i++) InsertByte(bbuf);
-            }
-          else {  /* read next byte as RunCount; repeat 0xFF runcount* */
+    c=ReadBlobByte(image);
+    if (c == EOF)
+      break;
+    bbuf=(unsigned char) c;
+    RunCount=bbuf & 0x7F;
+    if (bbuf & 0x80)
+      {
+        if (RunCount != 0)  /* repeat next byte RunCount times */
+          {
             c=ReadBlobByte(image);
-            if (c < 0)
+            if (c == EOF)
+              break;
+            bbuf=(unsigned char) c;
+            for (i=0; i < (ssize_t) RunCount; i++)
+              InsertByte(bbuf);
+          }
+        else  /* read next byte as RunCount; repeat 0xFF RunCount times */
+          {
+            c=ReadBlobByte(image);
+            if (c == EOF)
               break;
             RunCount=(unsigned char) c;
-            for(i=0;i<(int) RunCount;i++) InsertByte(0xFF);
+            for (i=0; i < (ssize_t) RunCount; i++)
+              InsertByte(0xFF);
           }
-        }
-      else {
-        if(RunCount)   /* next runcount byte are read directly */
+      }
+    else
+      {
+        if (RunCount != 0)  /* next RunCount bytes are read directly */
           {
-            for(i=0;i < (int) RunCount;i++)
+            for (i=0; i < (ssize_t) RunCount; i++)
               {
                 c=ReadBlobByte(image);
-                if (c < 0)
+                if (c == EOF)
                   break;
                 InsertByte((unsigned char) c);
               }
           }
-        else {  /* repeat previous line runcount* */
-          c=ReadBlobByte(image);
-          if (c == EOF)
-            {
-              BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
-              return(-7);
-            }
-          RunCount=(unsigned char) c;
-          if(x!=0) {    /* attempt to duplicate row from x position: */
-            if (InsertRow(image,BImgBuff,y,bpp,exception) == MagickFalse)
-              {
-                x=0;
-                y++;
-              }
-            BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
-            return(-3);
-          }
-          for (i=0; i < (int) RunCount; i++)
+        else  /* repeat previous line RunCount times */
           {
-            if (y >= (ssize_t) image->rows)
+            c=ReadBlobByte(image);
+            if (c == EOF)
               {
                 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
-                return(-4);
+                return(-7);
               }
-            if (InsertRow(image,BImgBuff,y,bpp,exception) == MagickFalse)
+            RunCount=(unsigned char) c;
+            if (x != 0)  /* repeat is only defined on a row boundary */
               {
                 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
-                return(-6);
+                return(-3);
               }
-            y++;
+            for (i=0; i < (ssize_t) RunCount; i++)
+            {
+              if (y >= rows)
+                {
+                  BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
+                  return(-4);
+                }
+              if (InsertRow(image,BImgBuff,y,bpp,exception) == MagickFalse)
+                {
+                  BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
+                  return(-6);
+                }
+              y++;
+            }
           }
-        }
       }
-      if (EOFBlob(image) != MagickFalse)
-        break;
-    }
+    if (EOFBlob(image) != MagickFalse)
+      break;
+  }
   BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
-  return(y <(ssize_t) image->rows ? -5 : 0);
+  return(y < rows ? -5 : 0);
 }


@@ -559,15 +643,19 @@ RestoreMSCWarning \
   x++; \
   if((ssize_t) x >= ldblk) \
   { \
-    if (InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception) != MagickFalse) \
-      y++; \
+    if (y < (ssize_t) image->rows) \
+      { \
+        if (InsertRow(image,BImgBuff,y,bpp,exception) != MagickFalse) \
+          y++; \
+      } \
     x=0; \
-   } \
+  } \
 }
 /* WPG2 raster reader. */
 static int UnpackWPG2Raster(Image *image,int bpp,ExceptionInfo *exception)
 {
   int
+    c,
     RunCount,
     XorMe = 0;

@@ -596,7 +684,10 @@ static int UnpackWPG2Raster(Image *image,int bpp,ExceptionInfo *exception)

   while( y< (ssize_t) image->rows)
   {
-      bbuf=(unsigned char) ReadBlobByte(image);
+      c=ReadBlobByte(image);
+      if (c == EOF)
+        break;
+      bbuf=(unsigned char) c;

       switch(bbuf)
         {
@@ -753,14 +844,13 @@ unsigned Flags;
         if(x>=0) (*CTM)[0][2] = (float)x+(float)DenX/0x10000;
             else (*CTM)[0][2] = (float)x-(float)DenX/0x10000;
   x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image);  /*Ty*/
-  (*CTM)[1][2]=(float)x + ((x>=0)?1:-1)*(float)DenX/0x10000;
         if(x>=0) (*CTM)[1][2] = (float)x+(float)DenX/0x10000;
             else (*CTM)[1][2] = (float)x-(float)DenX/0x10000;
   }
  if(Flags & TPR)
   {
   x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image);  /*Px*/
-  (*CTM)[2][0] = x + (float)DenX/0x10000;;
+  (*CTM)[2][0] = x + (float)DenX/0x10000;
   x=ReadBlobLSBShort(image);  DenX=ReadBlobLSBShort(image); /*Py*/
   (*CTM)[2][1] = x + (float)DenX/0x10000;
   }
@@ -805,10 +895,11 @@ static Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
   status=MagickFalse;

   /* Obtain temporary file */
-  (void) AcquireUniqueFilename(postscript_file);
+  if (AcquireUniqueFilename(postscript_file) == MagickFalse)
+    goto FINISH;
   ps_file=fopen_utf8(postscript_file,"wb");
   if (ps_file == (FILE *) NULL)
-    goto FINISH;
+    goto FINISH_UNL;

   /* Copy postscript to temporary file */
   if (SeekBlob(image,PS_Offset,SEEK_SET) != PS_Offset)
@@ -863,7 +954,7 @@ static Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
     }

   /* Read nested image */
-  FormatLocaleString(clone_info->filename,MagickPathExtent,"ps:%.1024s",
+  (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"ps:%.1024s",
     postscript_file);
   image2=ReadImage(clone_info,exception);
   if (!image2)
@@ -883,28 +974,38 @@ static Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
       Replace current image with new image while copying base image attributes.
     */
     p=image2;
-    do
+    while (p != (Image *) NULL)
     {
+      Image
+        *next;
+
+      next=p->next;
       (void) CopyMagickString(p->filename,image->filename,MagickPathExtent);
       (void) CopyMagickString(p->magick_filename,image->magick_filename,
         MagickPathExtent);
       (void) CopyMagickString(p->magick,image->magick,MagickPathExtent);
       if ((p->rows == 0) || (p->columns == 0))
         {
-          DeleteImageFromList(&p);
-          if (p == (Image *) NULL)
-            {
-              image2=(Image *) NULL;
-              goto FINISH_UNL;
-            }
+          Image
+            *empty;
+
+          /*
+            Drop empty images; keep image2 pointing at the head of the list.
+          */
+          empty=p;
+          if (empty == image2)
+            image2=next;
+          DeleteImageFromList(&empty);
         }
       else
         {
           DestroyBlob(p);
           p->blob=ReferenceBlob(image->blob);
-          p=p->next;
         }
-    } while (p != (Image *) NULL);
+      p=next;
+    }
+    if (image2 == (Image *) NULL)
+      goto FINISH_UNL;
   }

   if ((image->rows == 0 || image->columns == 0) &&
@@ -1068,8 +1169,7 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)

   int
     i,
-    bpp,
-    WPG2Flags;
+    bpp;

   ssize_t
     ldblk;
@@ -1112,7 +1212,8 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
   Header.EncryptKey=ReadBlobLSBShort(image);
   Header.Reserved=ReadBlobLSBShort(image);

-  if ((Header.FileId != 0x435057FF) || (Header.FileType != 0x16))
+  if ((EOFBlob(image) != MagickFalse) || (Header.FileId != 0x435057FF) ||
+      (Header.FileType != 0x16))
     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   if (Header.EncryptKey != 0)
     ThrowReaderException(CoderError,"EncryptedWPGImageFileNotSupported");
@@ -1172,6 +1273,8 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
             case 0x0E:  /*Color palette */
               WPG_Palette.StartIndex=ReadBlobLSBShort(image);
               WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
+              if (Rec.RecordLength < 4)
+                ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
               if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) > (Rec.RecordLength-2-2)/3)
                 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
               if (WPG_Palette.StartIndex > WPG_Palette.NumOfEntries)
@@ -1218,12 +1321,14 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
               BitmapHeader2.VertRes=ReadBlobLSBShort(image);

               image->units=PixelsPerCentimeterResolution;
-              image->page.width=(unsigned int)
-                ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightX)/470.0);
-              image->page.height=(unsigned int)
-                ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightY)/470.0);
-              image->page.x=(int) (BitmapHeader2.LowLeftX/470.0);
-              image->page.y=(int) (BitmapHeader2.LowLeftX/470.0);
+              image->page.width=WPGClampToSize(
+                ((double) BitmapHeader2.LowLeftX-
+                (double) BitmapHeader2.UpRightX)/470.0);
+              image->page.height=WPGClampToSize(
+                ((double) BitmapHeader2.LowLeftY-
+                (double) BitmapHeader2.UpRightY)/470.0);
+              image->page.x=WPGClampToSSize(BitmapHeader2.LowLeftX/470.0);
+              image->page.y=WPGClampToSSize(BitmapHeader2.LowLeftY/470.0);
               if(BitmapHeader2.HorzRes && BitmapHeader2.VertRes)
                 {
                   image->resolution.x=BitmapHeader2.HorzRes/470.0;
@@ -1234,13 +1339,20 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
               bpp=(int) BitmapHeader2.Depth;

             UnpackRaster:
+              if ((bpp != 1) && (bpp != 2) && (bpp != 4) && (bpp != 8) &&
+                  (bpp != 24))
+                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
               if ((image->columns > GetBlobSize(image)) ||
                   (image->rows > GetBlobSize(image)))
                 ThrowReaderException(CorruptImageError,
                   "InsufficientImageDataInFile");
               status=SetImageExtent(image,image->columns,image->rows,exception);
               if (status == MagickFalse)
-                break;
+                {
+                  (void) CloseBlob(image);
+                  image=DestroyImageList(image);
+                  return((Image *) NULL);
+                }
               (void) ResetImagePixels(image,exception);
               if ((image->storage_class != PseudoClass) && (bpp < 24))
                 {
@@ -1264,29 +1376,9 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 }
               else
                 {
-                  if (bpp < 24)
-                  if ( (image->colors < (one << bpp)) && (bpp != 24) )
-                    {
-                      PixelInfo
-                        *colormap;
-
-                      size_t
-                        colors;
-
-                      colormap=image->colormap;
-                      colors=image->colors;
-                      image->colormap=(PixelInfo *) NULL;
-                      if (AcquireImageColormap(image,one << bpp,exception) == MagickFalse)
-                        {
-                          colormap=(PixelInfo *)
-                            RelinquishMagickMemory(colormap);
-                          goto NoMemory;
-                        }
-                      (void) memcpy(image->colormap,colormap,MagickMin(
-                        image->colors,colors)*sizeof(*image->colormap));
-                      colormap=(PixelInfo *)
-                        RelinquishMagickMemory(colormap);
-                    }
+                  if ((bpp < 24) && (image->colors < (one << bpp)))
+                    if (WPGGrowColormap(image,one << bpp,exception) == MagickFalse)
+                      goto NoMemory;
                 }

               if (bpp == 1)
@@ -1353,10 +1445,10 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
                   goto Finish;
               AcquireNextImage(image_info,image,exception);
-              image->depth=8;
               if (image->next == (Image *) NULL)
                 goto Finish;
               image=SyncNextImageInList(image);
+              image->depth=8;
               image->columns=image->rows=0;
               image->colors=0;
               break;
@@ -1378,7 +1470,7 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)

     case 2:  /* WPG level 2 */
       (void) memset(CTM,0,sizeof(CTM));
-      StartWPG.PosSizePrecision = 0;
+      (void) memset(&StartWPG,0,sizeof(StartWPG));
       while(!EOFBlob(image)) /* object parser loop */
         {
           if (SeekBlob(image,Header.DataOffset,SEEK_SET) != Header.DataOffset)
@@ -1402,7 +1494,7 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)

           switch(Rec2.RecType)
             {
-      case 1:
+            case 1:
               StartWPG.HorizontalUnits=ReadBlobLSBShort(image);
               StartWPG.VerticalUnits=ReadBlobLSBShort(image);
               StartWPG.PosSizePrecision=(unsigned char) ReadBlobByte(image);
@@ -1410,8 +1502,10 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
             case 0x0C:    /* Color palette */
               WPG_Palette.StartIndex=ReadBlobLSBShort(image);
               WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
+              if (Rec2.RecordLength < 4)
+                ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
               if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) >
-                  (Rec2.RecordLength-2-2) / 3)
+                  (Rec2.RecordLength-2-2) / 4)
                 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
               if (WPG_Palette.StartIndex >= WPG_Palette.NumOfEntries)
                 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
@@ -1465,7 +1559,7 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
               image->columns=Bitmap2Header1.Width;
               image->rows=Bitmap2Header1.Height;
               if (image_info->ping != MagickFalse)
-                return(image);
+                goto Finish;
               if ((image->columns > GetBlobSize(image)) ||
                   (image->rows > GetBlobSize(image)))
                 ThrowReaderException(CorruptImageError,
@@ -1474,7 +1568,11 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
               if (status != MagickFalse)
                 status=ResetImagePixels(image,exception);
               if (status == MagickFalse)
-                break;
+                {
+                  (void) CloseBlob(image);
+                  image=DestroyImageList(image);
+                  return((Image *) NULL);
+                }
               if ((image->colors == 0) && (bpp != 24))
                 {
                   image->colors=one << bpp;
@@ -1483,11 +1581,9 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 }
               else
                 {
-                  if(bpp < 24)
-                    if( image->colors<(one << bpp) && bpp!=24 )
-                      image->colormap=(PixelInfo *) ResizeQuantumMemory(
-                       image->colormap,(size_t) (one << bpp),
-                       sizeof(*image->colormap));
+                  if ((bpp < 24) && (image->colors < (one << bpp)))
+                    if (WPGGrowColormap(image,one << bpp,exception) == MagickFalse)
+                      goto NoMemory;
                 }


@@ -1565,17 +1661,17 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
                   goto Finish;
               AcquireNextImage(image_info,image,exception);
-              image->depth=8;
               if (image->next == (Image *) NULL)
                 goto Finish;
               image=SyncNextImageInList(image);
+              image->depth=8;
               image->columns=image->rows=0;
               image->colors=0;
               break;

             case 0x12:  /* Postscript WPG2*/
-        i=ReadBlobLSBShort(image);
-              if (Rec2.RecordLength > (unsigned int) i)
+              i=(int) ReadBlobLSBShort(image);
+              if (Rec2.RecordLength > (size_t) i+2)
                 {
                   image=ExtractPostscript(image,image_info,
                     TellBlob(image)+i,    /*skip PS header in the wpg2*/
@@ -1586,10 +1682,9 @@ static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 }
               break;

-      case 0x1B:          /*bitmap rectangle*/
-              WPG2Flags = (int) LoadWPG2Flags(image,(char)
-                StartWPG.PosSizePrecision,NULL,&CTM);
-              (void) WPG2Flags;
+            case 0x1B:          /*bitmap rectangle*/
+              (void) LoadWPG2Flags(image,(char) StartWPG.PosSizePrecision,NULL,
+                &CTM);
               break;
             }
         }
@@ -1671,9 +1766,10 @@ ModuleExport size_t RegisterWPGImage(void)
   entry=AcquireMagickInfo("WPG","WPG","Word Perfect Graphics");
   entry->decoder=(DecodeImageHandler *) ReadWPGImage;
   entry->encoder=(EncodeImageHandler *) WriteWPGImage;
-  entry->magick=(IsImageFormatHandler *) IsWPG;
-  entry->flags^=CoderAdjoinFlag;
+  entry->magick=IsWPG;
+  entry->flags&=~CoderAdjoinFlag;
   entry->flags|=CoderDecoderSeekableStreamFlag;
+  entry->flags|=CoderEncoderSeekableStreamFlag;  /* size field is patched */
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }
@@ -1732,13 +1828,13 @@ ModuleExport void UnregisterWPGImage(void)

 typedef struct
 {
-	size_t
+  size_t
     count;

-	ssize_t
+  ssize_t
     offset;

-	unsigned char
+  unsigned char
     pixels[256];
 } WPGRLEInfo;

@@ -1757,7 +1853,8 @@ static MagickBooleanType WPGFlushRLE(WPGRLEInfo *rle_info,Image *image,
         return(MagickFalse);
       rle_info->offset-=n;
       if (rle_info->offset > 0)
-        (void) memmove(rle_info->pixels,rle_info->pixels+n,n);
+        (void) memmove(rle_info->pixels,rle_info->pixels+n,
+          (size_t) rle_info->offset);
       else
         rle_info->count=0;
     }
@@ -1976,11 +2073,17 @@ static MagickBooleanType WriteWPGImage(const ImageInfo *image_info,Image *image,

     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
     if (p == (const Quantum *) NULL)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
       image->depth == 1 ? GrayQuantum : IndexQuantum,pixels,exception);
     if (length == 0)
-      break;
+      {
+        status=MagickFalse;
+        break;
+      }
     status=WPGAddRLEBlock(&rle_info,image,pixels,(unsigned short) length);
     if (status == MagickFalse)
       break;
@@ -1995,10 +2098,15 @@ static MagickBooleanType WriteWPGImage(const ImageInfo *image_info,Image *image,
       current_offset=TellBlob(image);
       (void) WriteBlobByte(image,0x10);
       (void) WriteBlobByte(image,0);
-      (void) SeekBlob(image,offset,SEEK_SET);
-      offset=current_offset-offset-4;
-      (void) WriteBlobLSBShort(image,(unsigned short) (0x8000 | (offset >> 16)));
-      (void) WriteBlobLSBShort(image,offset & 0xffff);
+      if (SeekBlob(image,offset,SEEK_SET) != offset)
+        status=MagickFalse;
+      else
+        {
+          offset=current_offset-offset-4;
+          (void) WriteBlobLSBShort(image,(unsigned short) (0x8000 |
+            (offset >> 16)));
+          (void) WriteBlobLSBShort(image,(unsigned short) (offset & 0xffff));
+        }
     }
   if (CloseBlob(image) == MagickFalse)
     status=MagickFalse;