libtiff: Reformatting in all other directories using 'pre-commit run'

From aee0113f5ec1fd39b97458539bd79a1447425ac4 Mon Sep 17 00:00:00 2001
From: pre-commit run by Even Rouault <[EMAIL REDACTED]>
Date: Thu, 8 Dec 2022 19:19:36 +0100
Subject: [PATCH] Reformatting in all other directories using 'pre-commit run'

---
 archive/tools/bmp2tiff.c                  | 1611 +++++++++---------
 archive/tools/gif2tiff.c                  |  631 +++----
 archive/tools/ras2tiff.c                  |  587 +++----
 archive/tools/rasterfile.h                |   45 +-
 archive/tools/sgi2tiff.c                  |  561 ++++---
 archive/tools/sgisv.c                     |  505 +++---
 archive/tools/ycbcr.c                     |  171 +-
 contrib/addtiffo/addtiffo.c               |  102 +-
 contrib/addtiffo/tif_overview.c           |  919 +++++-----
 contrib/addtiffo/tif_ovrcache.c           |  386 ++---
 contrib/addtiffo/tif_ovrcache.h           |   82 +-
 contrib/dbs/tiff-bi.c                     |   18 +-
 contrib/dbs/tiff-grayscale.c              |   78 +-
 contrib/dbs/tiff-palette.c                |  348 ++--
 contrib/dbs/tiff-rgb.c                    |  212 +--
 contrib/dbs/xtiff/xtiff.c                 | 1249 +++++++-------
 contrib/dbs/xtiff/xtifficon.h             |   22 +-
 contrib/iptcutil/iptcutil.c               | 1265 +++++++-------
 contrib/mfs/mfs_file.c                    |  157 +-
 contrib/oss-fuzz/tiff_read_rgba_fuzzer.cc |  162 +-
 contrib/pds/tif_imageiter.c               |  622 +++----
 contrib/pds/tif_imageiter.h               |   84 +-
 contrib/pds/tif_pdsdirread.c              | 1840 +++++++++++----------
 contrib/pds/tif_pdsdirwrite.c             | 1539 +++++++++--------
 contrib/ras/ras2tif.c                     |  237 ++-
 contrib/ras/tif2ras.c                     |  440 ++---
 contrib/stream/tiffstream.cpp             |  316 ++--
 contrib/stream/tiffstream.h               |   79 +-
 contrib/tags/listtif.c                    |   45 +-
 contrib/tags/maketif.c                    |   77 +-
 contrib/tags/xtif_dir.c                   |  500 +++---
 contrib/tags/xtiffio.h                    |   23 +-
 contrib/tags/xtiffiop.h                   |   46 +-
 contrib/win_dib/Tiffile.cpp               |  363 ++--
 contrib/win_dib/tiff2dib.c                |  511 +++---
 port/dummy.c                              |    7 +-
 port/getopt.c                             |  157 +-
 port/libport.h                            |   30 +-
 38 files changed, 8230 insertions(+), 7797 deletions(-)

diff --git a/archive/tools/bmp2tiff.c b/archive/tools/bmp2tiff.c
index 38a07b77..5816ea5d 100644
--- a/archive/tools/bmp2tiff.c
+++ b/archive/tools/bmp2tiff.c
@@ -6,69 +6,69 @@
  ******************************************************************************
  * Copyright (c) 2004, Andrey Kiselev <dron@ak4719.spb.edu>
  *
- * Permission to use, copy, modify, distribute, and sell this software and 
+ * Permission to use, copy, modify, distribute, and sell this software and
  * its documentation for any purpose is hereby granted without fee, provided
  * that (i) the above copyright notices and this permission notice appear in
  * all copies of the software and related documentation, and (ii) the names of
  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  * publicity relating to the software without the specific, prior written
  * permission of Sam Leffler and Silicon Graphics.
- * 
- * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
- * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
- * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
- * 
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
- * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
- * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  * OF THIS SOFTWARE.
  */
 
 #include "tif_config.h"
 
+#include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
-#include <errno.h>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 
 #ifdef HAVE_UNISTD_H
-# include <unistd.h>
+#include <unistd.h>
 #endif
 
 #if HAVE_FCNTL_H
-# include <fcntl.h>
+#include <fcntl.h>
 #endif
 
 #if HAVE_SYS_TYPES_H
-# include <sys/types.h>
+#include <sys/types.h>
 #endif
 
 #if HAVE_IO_H
-# include <io.h>
+#include <io.h>
 #endif
 
 #ifdef NEED_LIBPORT
-# include "libport.h"
+#include "libport.h"
 #endif
 
-#include "tiffiop.h"
 #include "tiffio.h"
+#include "tiffiop.h"
 
 #ifndef O_BINARY
-# define O_BINARY 0
+#define O_BINARY 0
 #endif
 
 enum BMPType
 {
-    BMPT_WIN4,      /* BMP used in Windows 3.0/NT 3.51/95 */
-    BMPT_WIN5,      /* BMP used in Windows NT 4.0/98/Me/2000/XP */
-    BMPT_OS21,      /* BMP used in OS/2 PM 1.x */
-    BMPT_OS22       /* BMP used in OS/2 PM 2.x */
+    BMPT_WIN4, /* BMP used in Windows 3.0/NT 3.51/95 */
+    BMPT_WIN5, /* BMP used in Windows NT 4.0/98/Me/2000/XP */
+    BMPT_OS21, /* BMP used in OS/2 PM 1.x */
+    BMPT_OS22  /* BMP used in OS/2 PM 2.x */
 };
 
 /*
@@ -94,115 +94,115 @@ enum BMPType
 
 enum BMPComprMethod
 {
-    BMPC_RGB = 0L,          /* Uncompressed */
-    BMPC_RLE8 = 1L,         /* RLE for 8 bpp images */
-    BMPC_RLE4 = 2L,         /* RLE for 4 bpp images */
-    BMPC_BITFIELDS = 3L,    /* Bitmap is not compressed and the colour table
-			     * consists of three DWORD color masks that specify
-			     * the red, green, and blue components of each
-			     * pixel. This is valid when used with
-			     * 16- and 32-bpp bitmaps. */
-    BMPC_JPEG = 4L,         /* Indicates that the image is a JPEG image. */
-    BMPC_PNG = 5L           /* Indicates that the image is a PNG image. */
+    BMPC_RGB = 0L,       /* Uncompressed */
+    BMPC_RLE8 = 1L,      /* RLE for 8 bpp images */
+    BMPC_RLE4 = 2L,      /* RLE for 4 bpp images */
+    BMPC_BITFIELDS = 3L, /* Bitmap is not compressed and the colour table
+                          * consists of three DWORD color masks that specify
+                          * the red, green, and blue components of each
+                          * pixel. This is valid when used with
+                          * 16- and 32-bpp bitmaps. */
+    BMPC_JPEG = 4L,      /* Indicates that the image is a JPEG image. */
+    BMPC_PNG = 5L        /* Indicates that the image is a PNG image. */
 };
 
-enum BMPLCSType                 /* Type of logical color space. */
+enum BMPLCSType /* Type of logical color space. */
 {
-    BMPLT_CALIBRATED_RGB = 0,	/* This value indicates that endpoints and
-				 * gamma values are given in the appropriate
-				 * fields. */
+    BMPLT_CALIBRATED_RGB = 0, /* This value indicates that endpoints and
+                               * gamma values are given in the appropriate
+                               * fields. */
     BMPLT_DEVICE_RGB = 1,
     BMPLT_DEVICE_CMYK = 2
 };
 
 typedef struct
 {
-    int32_t   iCIEX;
-    int32_t   iCIEY;
-    int32_t   iCIEZ;
+    int32_t iCIEX;
+    int32_t iCIEY;
+    int32_t iCIEZ;
 } BMPCIEXYZ;
 
-typedef struct                  /* This structure contains the x, y, and z */
-{				/* coordinates of the three colors that */
-				/* correspond */
-    BMPCIEXYZ   iCIERed;        /* to the red, green, and blue endpoints for */
-    BMPCIEXYZ   iCIEGreen;      /* a specified logical color space. */
-    BMPCIEXYZ	iCIEBlue;
+typedef struct /* This structure contains the x, y, and z */
+{              /* coordinates of the three colors that */
+    /* correspond */
+    BMPCIEXYZ iCIERed;   /* to the red, green, and blue endpoints for */
+    BMPCIEXYZ iCIEGreen; /* a specified logical color space. */
+    BMPCIEXYZ iCIEBlue;
 } BMPCIEXYZTriple;
 
 typedef struct
 {
-    char	bType[2];       /* Signature "BM" */
-    uint32_t	iSize;          /* Size in bytes of the bitmap file. Should
-				 * always be ignored while reading because
-				 * of error in Windows 3.0 SDK's description
-				 * of this field */
-    uint16_t	iReserved1;     /* Reserved, set as 0 */
-    uint16_t	iReserved2;     /* Reserved, set as 0 */
-    uint32_t	iOffBits;       /* Offset of the image from file start in bytes */
+    char bType[2];       /* Signature "BM" */
+    uint32_t iSize;      /* Size in bytes of the bitmap file. Should
+                          * always be ignored while reading because
+                          * of error in Windows 3.0 SDK's description
+                          * of this field */
+    uint16_t iReserved1; /* Reserved, set as 0 */
+    uint16_t iReserved2; /* Reserved, set as 0 */
+    uint32_t iOffBits;   /* Offset of the image from file start in bytes */
 } BMPFileHeader;
 
 /* File header size in bytes: */
-const int       BFH_SIZE = 14;
+const int BFH_SIZE = 14;
 
 typedef struct
 {
-    uint32_t	iSize;          /* Size of BMPInfoHeader structure in bytes.
-				 * Should be used to determine start of the
-				 * colour table */
-    int32_t	iWidth;         /* Image width */
-    int32_t	iHeight;        /* Image height. If positive, image has bottom
-				 * left origin, if negative --- top left. */
-    int16_t	iPlanes;        /* Number of image planes (must be set to 1) */
-    int16_t	iBitCount;      /* Number of bits per pixel (1, 4, 8, 16, 24
-				 * or 32). If 0 then the number of bits per
-				 * pixel is specified or is implied by the
-				 * JPEG or PNG format. */
-    uint32_t	iCompression;	/* Compression method */
-    uint32_t	iSizeImage;     /* Size of uncomressed image in bytes. May
-				 * be 0 for BMPC_RGB bitmaps. If iCompression
-				 * is BI_JPEG or BI_PNG, iSizeImage indicates
-				 * the size of the JPEG or PNG image buffer. */
-    int32_t	iXPelsPerMeter; /* X resolution, pixels per meter (0 if not used) */
-    int32_t	iYPelsPerMeter; /* Y resolution, pixels per meter (0 if not used) */
-    uint32_t	iClrUsed;       /* Size of colour table. If 0, iBitCount should
-				 * be used to calculate this value
-				 * (1<<iBitCount). This value should be
-				 * unsigned for proper shifting. */
-    int32_t	iClrImportant;  /* Number of important colours. If 0, all
-				 * colours are required */
+    uint32_t iSize;         /* Size of BMPInfoHeader structure in bytes.
+                             * Should be used to determine start of the
+                             * colour table */
+    int32_t iWidth;         /* Image width */
+    int32_t iHeight;        /* Image height. If positive, image has bottom
+                             * left origin, if negative --- top left. */
+    int16_t iPlanes;        /* Number of image planes (must be set to 1) */
+    int16_t iBitCount;      /* Number of bits per pixel (1, 4, 8, 16, 24
+                             * or 32). If 0 then the number of bits per
+                             * pixel is specified or is implied by the
+                             * JPEG or PNG format. */
+    uint32_t iCompression;  /* Compression method */
+    uint32_t iSizeImage;    /* Size of uncomressed image in bytes. May
+                             * be 0 for BMPC_RGB bitmaps. If iCompression
+                             * is BI_JPEG or BI_PNG, iSizeImage indicates
+                             * the size of the JPEG or PNG image buffer. */
+    int32_t iXPelsPerMeter; /* X resolution, pixels per meter (0 if not used) */
+    int32_t iYPelsPerMeter; /* Y resolution, pixels per meter (0 if not used) */
+    uint32_t iClrUsed;      /* Size of colour table. If 0, iBitCount should
+                             * be used to calculate this value
+                             * (1<<iBitCount). This value should be
+                             * unsigned for proper shifting. */
+    int32_t iClrImportant;  /* Number of important colours. If 0, all
+                             * colours are required */
 
     /*
      * Fields above should be used for bitmaps, compatible with Windows NT 3.51
      * and earlier. Windows 98/Me, Windows 2000/XP introduces additional fields:
      */
 
-    int32_t	iRedMask;       /* Colour mask that specifies the red component
-				 * of each pixel, valid only if iCompression
-				 * is set to BI_BITFIELDS. */
-    int32_t	iGreenMask;     /* The same for green component */
-    int32_t	iBlueMask;      /* The same for blue component */
-    int32_t	iAlphaMask;     /* Colour mask that specifies the alpha
-				 * component of each pixel. */
-    uint32_t	iCSType;        /* Colour space of the DIB. */
+    int32_t iRedMask;           /* Colour mask that specifies the red component
+                                 * of each pixel, valid only if iCompression
+                                 * is set to BI_BITFIELDS. */
+    int32_t iGreenMask;         /* The same for green component */
+    int32_t iBlueMask;          /* The same for blue component */
+    int32_t iAlphaMask;         /* Colour mask that specifies the alpha
+                                 * component of each pixel. */
+    uint32_t iCSType;           /* Colour space of the DIB. */
     BMPCIEXYZTriple sEndpoints; /* This member is ignored unless the iCSType
-				 * member specifies BMPLT_CALIBRATED_RGB. */
-    int32_t	iGammaRed;      /* Toned response curve for red. This member
-				 * is ignored unless color values are
-				 * calibrated RGB values and iCSType is set to
-				 * BMPLT_CALIBRATED_RGB. Specified
-				 * in 16^16 format. */
-    int32_t	iGammaGreen;    /* Toned response curve for green. */
-    int32_t	iGammaBlue;     /* Toned response curve for blue. */
+                                 * member specifies BMPLT_CALIBRATED_RGB. */
+    int32_t iGammaRed;          /* Toned response curve for red. This member
+                                 * is ignored unless color values are
+                                 * calibrated RGB values and iCSType is set to
+                                 * BMPLT_CALIBRATED_RGB. Specified
+                                 * in 16^16 format. */
+    int32_t iGammaGreen;        /* Toned response curve for green. */
+    int32_t iGammaBlue;         /* Toned response curve for blue. */
 } BMPInfoHeader;
 
 /*
  * Info header size in bytes:
  */
-const unsigned int  BIH_WIN4SIZE = 40; /* for BMPT_WIN4 */
-const unsigned int  BIH_WIN5SIZE = 57; /* for BMPT_WIN5 */
-const unsigned int  BIH_OS21SIZE = 12; /* for BMPT_OS21 */
-const unsigned int  BIH_OS22SIZE = 64; /* for BMPT_OS22 */
+const unsigned int BIH_WIN4SIZE = 40; /* for BMPT_WIN4 */
+const unsigned int BIH_WIN5SIZE = 57; /* for BMPT_WIN5 */
+const unsigned int BIH_OS21SIZE = 12; /* for BMPT_OS21 */
+const unsigned int BIH_OS22SIZE = 64; /* for BMPT_OS22 */
 
 /*
  * We will use plain byte array instead of this structure, but declaration
@@ -210,719 +210,796 @@ const unsigned int  BIH_OS22SIZE = 64; /* for BMPT_OS22 */
  */
 typedef struct
 {
-    char       bBlue;
-    char       bGreen;
-    char       bRed;
-    char       bReserved;      /* Must be 0 */
+    char bBlue;
+    char bGreen;
+    char bRed;
+    char bReserved; /* Must be 0 */
 } BMPColorEntry;
 
-static	uint16_t compression = (uint16_t) -1;
-static	int jpegcolormode = JPEGCOLORMODE_RGB;
-static	int quality = 75;		/* JPEG quality */
-static	uint16_t predictor = 0;
+static uint16_t compression = (uint16_t)-1;
+static int jpegcolormode = JPEGCOLORMODE_RGB;
+static int quality = 75; /* JPEG quality */
+static uint16_t predictor = 0;
 
 static void usage(void);
-static int processCompressOptions(char*);
+static int processCompressOptions(char *);
 static void rearrangePixels(char *, uint32_t, uint32_t);
 
-int
-main(int argc, char* argv[])
+int main(int argc, char *argv[])
 {
-	uint32_t	width, length;
-	uint16_t	nbands = 1;		/* number of bands in input image */
-        uint16_t	depth = 8;		/* bits per pixel in input image */
-	uint32_t	rowsperstrip = (uint32_t) -1;
-        uint16_t	photometric = PHOTOMETRIC_MINISBLACK;
-	int	fd = 0;
-	_TIFF_stat_s instat;
-	char	*outfilename = NULL, *infilename = NULL;
-	TIFF	*out = NULL;
-
-	BMPFileHeader file_hdr;
-        BMPInfoHeader info_hdr;
-        int     bmp_type;
-        uint32_t  clr_tbl_size, n_clr_elems = 3;
-        unsigned char *clr_tbl;
-	unsigned short *red_tbl = NULL, *green_tbl = NULL, *blue_tbl = NULL;
-	uint32_t	row, clr;
-
-	int	c;
+    uint32_t width, length;
+    uint16_t nbands = 1; /* number of bands in input image */
+    uint16_t depth = 8;  /* bits per pixel in input image */
+    uint32_t rowsperstrip = (uint32_t)-1;
+    uint16_t photometric = PHOTOMETRIC_MINISBLACK;
+    int fd = 0;
+    _TIFF_stat_s instat;
+    char *outfilename = NULL, *infilename = NULL;
+    TIFF *out = NULL;
+
+    BMPFileHeader file_hdr;
+    BMPInfoHeader info_hdr;
+    int bmp_type;
+    uint32_t clr_tbl_size, n_clr_elems = 3;
+    unsigned char *clr_tbl;
+    unsigned short *red_tbl = NULL, *green_tbl = NULL, *blue_tbl = NULL;
+    uint32_t row, clr;
+
+    int c;
 #if !HAVE_DECL_OPTARG
-	extern int optind;
-	extern char* optarg;
+    extern int optind;
+    extern char *optarg;
 #endif
 
-	while ((c = getopt(argc, argv, "c:r:o:h")) != -1) {
-		switch (c) {
-		case 'c':		/* compression scheme */
-			if (!processCompressOptions(optarg))
-				usage();
-			break;
-		case 'r':		/* rows/strip */
-			rowsperstrip = atoi(optarg);
-			break;
-		case 'o':
-			outfilename = optarg;
-			break;
-		case 'h':
-			usage();
-		default:
-			break;
-		}
-	}
-
-	if (argc - optind < 2)
-		usage();
-
-	if (outfilename == NULL)
-		outfilename = argv[argc-1];
-	out = TIFFOpen(outfilename, "w");
-	if (out == NULL) {
-		TIFFError(infilename, "Cannot open file %s for output",
-			  outfilename);
-		goto bad3;
-	}
-	
-
-	while (optind < argc-1) {
-		infilename = argv[optind];
-		optind++;
-	    
-		fd = open(infilename, O_RDONLY|O_BINARY, 0);
-		if (fd < 0) {
-			TIFFError(infilename, "Cannot open input file");
-			return -1;
-		}
-
-		if (read(fd, file_hdr.bType, 2) != 2) {
-                        TIFFError(infilename, "Failed to read from file (%s)",
-                                  strerror(errno));
-			goto bad;
-                }
-		if(file_hdr.bType[0] != 'B' || file_hdr.bType[1] != 'M') {
-			TIFFError(infilename, "File is not BMP");
-			goto bad;
-		}
-
-                /* -------------------------------------------------------------------- */
-                /*      Read the BMPFileHeader. We need iOffBits value only             */
-                /* -------------------------------------------------------------------- */
-                if (_TIFF_lseek_f(fd, 10, SEEK_SET) == (_TIFF_off_t)-1) {
-                        TIFFError(infilename, "Failed to seek to offset");
-                        goto bad;
-                }
-                if (read(fd, &file_hdr.iOffBits, 4) != 4) {
-                        TIFFError(infilename, "Failed to read from file (%s)",
-                                  strerror(errno));
-			goto bad;
-                }
-#ifdef WORDS_BIGENDIAN
-		TIFFSwabLong(&file_hdr.iOffBits);
-#endif
-		if (_TIFF_fstat_f(fd, &instat) == -1) {
-                        TIFFError(infilename, "Failed obtain file information");
-                        goto bad;
-                }
-		file_hdr.iSize = instat.st_size;
+    while ((c = getopt(argc, argv, "c:r:o:h")) != -1)
+    {
+        switch (c)
+        {
+            case 'c': /* compression scheme */
+                if (!processCompressOptions(optarg))
+                    usage();
+                break;
+            case 'r': /* rows/strip */
+                rowsperstrip = atoi(optarg);
+                break;
+            case 'o':
+                outfilename = optarg;
+                break;
+            case 'h':
+                usage();
+            default:
+                break;
+        }
+    }
+
+    if (argc - optind < 2)
+        usage();
+
+    if (outfilename == NULL)
+        outfilename = argv[argc - 1];
+    out = TIFFOpen(outfilename, "w");
+    if (out == NULL)
+    {
+        TIFFError(infilename, "Cannot open file %s for output", outfilename);
+        goto bad3;
+    }
+
+    while (optind < argc - 1)
+    {
+        infilename = argv[optind];
+        optind++;
+
+        fd = open(infilename, O_RDONLY | O_BINARY, 0);
+        if (fd < 0)
+        {
+            TIFFError(infilename, "Cannot open input file");
+            return -1;
+        }
 
-                /* -------------------------------------------------------------------- */
-                /*      Read the BMPInfoHeader.                                         */
-                /* -------------------------------------------------------------------- */
+        if (read(fd, file_hdr.bType, 2) != 2)
+        {
+            TIFFError(infilename, "Failed to read from file (%s)",
+                      strerror(errno));
+            goto bad;
+        }
+        if (file_hdr.bType[0] != 'B' || file_hdr.bType[1] != 'M')
+        {
+            TIFFError(infilename, "File is not BMP");
+            goto bad;
+        }
 
-		if (_TIFF_lseek_f(fd, BFH_SIZE, SEEK_SET) == (_TIFF_off_t)-1) {
-                        TIFFError(infilename, "Failed to seek to offset");
-                        goto bad;
-                }
-                if (read(fd, &info_hdr.iSize, 4) != 4) {
-                        TIFFError(infilename, "Failed to read from file (%s)",
-                                  strerror(errno));
-			goto bad;
-                }
+        /* --------------------------------------------------------------------
+         */
+        /*      Read the BMPFileHeader. We need iOffBits value only */
+        /* --------------------------------------------------------------------
+         */
+        if (_TIFF_lseek_f(fd, 10, SEEK_SET) == (_TIFF_off_t)-1)
+        {
+            TIFFError(infilename, "Failed to seek to offset");
+            goto bad;
+        }
+        if (read(fd, &file_hdr.iOffBits, 4) != 4)
+        {
+            TIFFError(infilename, "Failed to read from file (%s)",
+                      strerror(errno));
+            goto bad;
+        }
+#ifdef WORDS_BIGENDIAN
+        TIFFSwabLong(&file_hdr.iOffBits);
+#endif
+        if (_TIFF_fstat_f(fd, &instat) == -1)
+        {
+            TIFFError(infilename, "Failed obtain file information");
+            goto bad;
+        }
+        file_hdr.iSize = instat.st_size;
+
+        /* --------------------------------------------------------------------
+         */
+        /*      Read the BMPInfoHeader. */
+        /* --------------------------------------------------------------------
+         */
+
+        if (_TIFF_lseek_f(fd, BFH_SIZE, SEEK_SET) == (_TIFF_off_t)-1)
+        {
+            TIFFError(infilename, "Failed to seek to offset");
+            goto bad;
+        }
+        if (read(fd, &info_hdr.iSize, 4) != 4)
+        {
+            TIFFError(infilename, "Failed to read from file (%s)",
+                      strerror(errno));
+            goto bad;
+        }
 #ifdef WORDS_BIGENDIAN
-		TIFFSwabLong(&info_hdr.iSize);
+        TIFFSwabLong(&info_hdr.iSize);
 #endif
 
-		if (info_hdr.iSize == BIH_WIN4SIZE)
-			bmp_type = BMPT_WIN4;
-		else if (info_hdr.iSize == BIH_OS21SIZE)
-			bmp_type = BMPT_OS21;
-		else if (info_hdr.iSize == BIH_OS22SIZE
-			 || info_hdr.iSize == 16)
-			bmp_type = BMPT_OS22;
-		else
-			bmp_type = BMPT_WIN5;
-
-		if (bmp_type == BMPT_WIN4
-		    || bmp_type == BMPT_WIN5
-		    || bmp_type == BMPT_OS22) {
-			if ((read(fd, &info_hdr.iWidth, 4) != 4) ||
-                            (read(fd, &info_hdr.iHeight, 4) != 4) ||
-                            (read(fd, &info_hdr.iPlanes, 2) != 2) ||
-                            (read(fd, &info_hdr.iBitCount, 2) != 2) ||
-                            (read(fd, &info_hdr.iCompression, 4) != 4) ||
-                            (read(fd, &info_hdr.iSizeImage, 4) != 4) ||
-                            (read(fd, &info_hdr.iXPelsPerMeter, 4) != 4) ||
-                            (read(fd, &info_hdr.iYPelsPerMeter, 4) != 4) ||
-                            (read(fd, &info_hdr.iClrUsed, 4) != 4) ||
-                            (read(fd, &info_hdr.iClrImportant, 4) != 4)) {
-                                TIFFError(infilename, "Failed to read from file (%s)",
-                                          strerror(errno));
-                                goto bad;
-                        }
+        if (info_hdr.iSize == BIH_WIN4SIZE)
+            bmp_type = BMPT_WIN4;
+        else if (info_hdr.iSize == BIH_OS21SIZE)
+            bmp_type = BMPT_OS21;
+        else if (info_hdr.iSize == BIH_OS22SIZE || info_hdr.iSize == 16)
+            bmp_type = BMPT_OS22;
+        else
+            bmp_type = BMPT_WIN5;
+
+        if (bmp_type == BMPT_WIN4 || bmp_type == BMPT_WIN5 ||
+            bmp_type == BMPT_OS22)
+        {
+            if ((read(fd, &info_hdr.iWidth, 4) != 4) ||
+                (read(fd, &info_hdr.iHeight, 4) != 4) ||
+                (read(fd, &info_hdr.iPlanes, 2) != 2) ||
+                (read(fd, &info_hdr.iBitCount, 2) != 2) ||
+                (read(fd, &info_hdr.iCompression, 4) != 4) ||
+                (read(fd, &info_hdr.iSizeImage, 4) != 4) ||
+                (read(fd, &info_hdr.iXPelsPerMeter, 4) != 4) ||
+                (read(fd, &info_hdr.iYPelsPerMeter, 4) != 4) ||
+                (read(fd, &info_hdr.iClrUsed, 4) != 4) ||
+                (read(fd, &info_hdr.iClrImportant, 4) != 4))
+            {
+                TIFFError(infilename, "Failed to read from file (%s)",
+                          strerror(errno));
+                goto bad;
+            }
 #ifdef WORDS_BIGENDIAN
-			TIFFSwabLong((uint32_t*) &info_hdr.iWidth);
-			TIFFSwabLong((uint32_t*) &info_hdr.iHeight);
-			TIFFSwabShort((uint16_t*) &info_hdr.iPlanes);
-			TIFFSwabShort((uint16_t*) &info_hdr.iBitCount);
-			TIFFSwabLong((uint32_t*) &info_hdr.iCompression);
-			TIFFSwabLong((uint32_t*) &info_hdr.iSizeImage);
-			TIFFSwabLong((uint32_t*) &info_hdr.iXPelsPerMeter);
-			TIFFSwabLong((uint32_t*) &info_hdr.iYPelsPerMeter);
-			TIFFSwabLong((uint32_t*) &info_hdr.iClrUsed);
-			TIFFSwabLong((uint32_t*) &info_hdr.iClrImportant);
+            TIFFSwabLong((uint32_t *)&info_hdr.iWidth);
+            TIFFSwabLong((uint32_t *)&info_hdr.iHeight);
+            TIFFSwabShort((uint16_t *)&info_hdr.iPlanes);
+            TIFFSwabShort((uint16_t *)&info_hdr.iBitCount);
+            TIFFSwabLong((uint32_t *)&info_hdr.iCompression);
+            TIFFSwabLong((uint32_t *)&info_hdr.iSizeImage);
+            TIFFSwabLong((uint32_t *)&info_hdr.iXPelsPerMeter);
+            TIFFSwabLong((uint32_t *)&info_hdr.iYPelsPerMeter);
+            TIFFSwabLong((uint32_t *)&info_hdr.iClrUsed);
+            TIFFSwabLong((uint32_t *)&info_hdr.iClrImportant);
 #endif
-			n_clr_elems = 4;
-		}
-
-		if (bmp_type == BMPT_OS22) {
-			/* 
-			 * FIXME: different info in different documents
-			 * regarding this!
-			 */
-                        n_clr_elems = 3;
-		}
-
-		if (bmp_type == BMPT_OS21) {
-			int16_t  iShort;
-
-			if ( read(fd, &iShort, 2) != 2 ) {
-                                TIFFError(infilename, "Failed to read from file (%s)",
-                                          strerror(errno));
-                                goto bad;
-                        }
+            n_clr_elems = 4;
+        }
+
+        if (bmp_type == BMPT_OS22)
+        {
+            /*
+             * FIXME: different info in different documents
+             * regarding this!
+             */
+            n_clr_elems = 3;
+        }
+
+        if (bmp_type == BMPT_OS21)
+        {
+            int16_t iShort;
+
+            if (read(fd, &iShort, 2) != 2)
+            {
+                TIFFError(infilename, "Failed to read from file (%s)",
+                          strerror(errno));
+                goto bad;
+            }
 #ifdef WORDS_BIGENDIAN
-			TIFFSwabShort((uint16_t*) &iShort);
+            TIFFSwabShort((uint16_t *)&iShort);
 #endif
-			info_hdr.iWidth = iShort;
-			if ( read(fd, &iShort, 2) != 2 ) {
-                                TIFFError(infilename, "Failed to read from file (%s)",
-                                          strerror(errno));
-                                goto bad;
-                        }
+            info_hdr.iWidth = iShort;
+            if (read(fd, &iShort, 2) != 2)
+            {
+                TIFFError(infilename, "Failed to read from file (%s)",
+                          strerror(errno));
+                goto bad;
+            }
 #ifdef WORDS_BIGENDIAN
-			TIFFSwabShort((uint16_t*) &iShort);
+            TIFFSwabShort((uint16_t *)&iShort);
 #endif
-			info_hdr.iHeight = iShort;
-			if (read(fd, &iShort, 2) != 2 ) {
-                                TIFFError(infilename, "Failed to read from file (%s)",
-                                          strerror(errno));
-                                goto bad;
-                        }
+            info_hdr.iHeight = iShort;
+            if (read(fd, &iShort, 2) != 2)
+            {
+                TIFFError(infilename, "Failed to read from file (%s)",
+                          strerror(errno));
+                goto bad;
+            }
 #ifdef WORDS_BIGENDIAN
-			TIFFSwabShort((uint16_t*) &iShort);
+            TIFFSwabShort((uint16_t *)&iShort);
 #endif
-			info_hdr.iPlanes = iShort;
-			if ( read(fd, &iShort, 2) != 2 ) {
-                                TIFFError(infilename, "Failed to read from file (%s)",
-                                          strerror(errno));
-                                goto bad;
-                        }
+            info_hdr.iPlanes = iShort;
+            if (read(fd, &iShort, 2) != 2)
+            {
+                TIFFError(infilename, "Failed to read from file (%s)",
+                          strerror(errno));
+                goto bad;
+            }
 #ifdef WORDS_BIGENDIAN
-			TIFFSwabShort((uint16_t*) &iShort);
+            TIFFSwabShort((uint16_t *)&iShort);
 #endif
-			info_hdr.iBitCount = iShort;
-			info_hdr.iCompression = BMPC_RGB;
-			n_clr_elems = 3;
-		}
-
-		if (info_hdr.iBitCount != 1  && info_hdr.iBitCount != 4  &&
-		    info_hdr.iBitCount != 8  && info_hdr.iBitCount != 16 &&
-		    info_hdr.iBitCount != 24 && info_hdr.iBitCount != 32) {
-                        TIFFError(infilename,
-                                  "Cannot process BMP file with bit count %d",
-                                  info_hdr.iBitCount);
-                        close(fd);
-                        return 0;
-		}
-
-		width = info_hdr.iWidth;
-		length = (info_hdr.iHeight > 0) ? info_hdr.iHeight : -info_hdr.iHeight;
-                if( width <= 0 || length <= 0 )
-                        {
-                                TIFFError(infilename,
-                                          "Invalid dimensions of BMP file" );
-                                close(fd);
-                                return -1;
-                        }
+            info_hdr.iBitCount = iShort;
+            info_hdr.iCompression = BMPC_RGB;
+            n_clr_elems = 3;
+        }
+
+        if (info_hdr.iBitCount != 1 && info_hdr.iBitCount != 4 &&
+            info_hdr.iBitCount != 8 && info_hdr.iBitCount != 16 &&
+            info_hdr.iBitCount != 24 && info_hdr.iBitCount != 32)
+        {
+            TIFFError(infilename, "Cannot proce

(Patch may be truncated, please check the link at the top of this post.)