https://git.reactos.org/?p=reactos.git;a=commitdiff;h=743951eccfef786bcb3c6e866058b3449232efbc

commit 743951eccfef786bcb3c6e866058b3449232efbc
Author:     Thomas Faber <[email protected]>
AuthorDate: Mon Mar 11 20:33:40 2019 +0100
Commit:     Thomas Faber <[email protected]>
CommitDate: Tue Mar 26 10:14:47 2019 +0100

    [LIBTIFF] Update to version 4.0.10. CORE-15854
---
 dll/3rdparty/libtiff/CMakeLists.txt                |   2 +
 dll/3rdparty/libtiff/mkg3states.c                  |   2 -
 dll/3rdparty/libtiff/tif_aux.c                     |   2 -
 dll/3rdparty/libtiff/tif_close.c                   |   2 -
 dll/3rdparty/libtiff/tif_codec.c                   |  10 +-
 dll/3rdparty/libtiff/tif_color.c                   |   4 +-
 dll/3rdparty/libtiff/tif_compress.c                |   2 -
 dll/3rdparty/libtiff/tif_dir.c                     |  29 +-
 dll/3rdparty/libtiff/tif_dirinfo.c                 |  17 +-
 dll/3rdparty/libtiff/tif_dirread.c                 | 117 +++-
 dll/3rdparty/libtiff/tif_dirwrite.c                |   9 +-
 dll/3rdparty/libtiff/tif_dumpmode.c                |   2 -
 dll/3rdparty/libtiff/tif_error.c                   |   2 -
 dll/3rdparty/libtiff/tif_extension.c               |   2 -
 dll/3rdparty/libtiff/tif_fax3.c                    |   2 -
 dll/3rdparty/libtiff/tif_flush.c                   |   2 -
 dll/3rdparty/libtiff/tif_getimage.c                |   2 -
 dll/3rdparty/libtiff/tif_jbig.c                    |  36 +-
 dll/3rdparty/libtiff/tif_jpeg.c                    |   7 +-
 dll/3rdparty/libtiff/tif_luv.c                     |  12 +-
 dll/3rdparty/libtiff/tif_lzma.c                    |  11 +-
 dll/3rdparty/libtiff/tif_lzw.c                     |  32 +-
 dll/3rdparty/libtiff/tif_next.c                    |   2 -
 dll/3rdparty/libtiff/tif_ojpeg.c                   |   5 +-
 dll/3rdparty/libtiff/tif_open.c                    |   2 -
 dll/3rdparty/libtiff/tif_packbits.c                |   2 -
 dll/3rdparty/libtiff/tif_pixarlog.c                |   2 -
 dll/3rdparty/libtiff/tif_predict.c                 |   2 -
 dll/3rdparty/libtiff/tif_print.c                   |  12 +-
 dll/3rdparty/libtiff/tif_read.c                    |  13 +-
 dll/3rdparty/libtiff/tif_stream.cxx                |   8 +-
 dll/3rdparty/libtiff/tif_strip.c                   |   2 -
 dll/3rdparty/libtiff/tif_swab.c                    |   2 -
 dll/3rdparty/libtiff/tif_thunder.c                 |   2 -
 dll/3rdparty/libtiff/tif_tile.c                    |   2 -
 dll/3rdparty/libtiff/tif_unix.c                    |   2 -
 dll/3rdparty/libtiff/tif_version.c                 |   1 -
 dll/3rdparty/libtiff/tif_warning.c                 |   2 -
 dll/3rdparty/libtiff/tif_webp.c                    | 684 +++++++++++++++++++++
 dll/3rdparty/libtiff/tif_win32.c                   |  41 --
 dll/3rdparty/libtiff/tif_write.c                   |   8 +-
 dll/3rdparty/libtiff/tif_zip.c                     |   2 -
 dll/3rdparty/libtiff/tif_zstd.c                    | 440 +++++++++++++
 media/doc/3rd Party Files.txt                      |   2 +-
 sdk/include/reactos/libs/libtiff/t4.h              |   2 -
 .../reactos/libs/libtiff/tif_config.wince.h        |   2 -
 sdk/include/reactos/libs/libtiff/tif_dir.h         |   6 +-
 sdk/include/reactos/libs/libtiff/tif_fax3.h        |   2 -
 sdk/include/reactos/libs/libtiff/tif_predict.h     |   6 +-
 sdk/include/reactos/libs/libtiff/tiff.h            |  18 +-
 sdk/include/reactos/libs/libtiff/tiffconf.h        |   9 -
 sdk/include/reactos/libs/libtiff/tiffconf.vc.h     |   9 -
 sdk/include/reactos/libs/libtiff/tiffconf.wince.h  |  11 -
 sdk/include/reactos/libs/libtiff/tiffio.h          |   4 +-
 sdk/include/reactos/libs/libtiff/tiffio.hxx        |   3 +-
 sdk/include/reactos/libs/libtiff/tiffiop.h         |  15 +-
 sdk/include/reactos/libs/libtiff/tiffvers.h        |   4 +-
 57 files changed, 1395 insertions(+), 240 deletions(-)

diff --git a/dll/3rdparty/libtiff/CMakeLists.txt 
b/dll/3rdparty/libtiff/CMakeLists.txt
index 6c28292ec1d..476e2b1f726 100644
--- a/dll/3rdparty/libtiff/CMakeLists.txt
+++ b/dll/3rdparty/libtiff/CMakeLists.txt
@@ -47,9 +47,11 @@ list(APPEND SOURCE
     tif_tile.c
     tif_version.c
     tif_warning.c
+    #tif_webp.c
     tif_win32.c
     tif_write.c
     tif_zip.c
+    #tif_zstd.c
     precomp.h)
 
 add_library(libtiff SHARED ${SOURCE} libtiff.def)
diff --git a/dll/3rdparty/libtiff/mkg3states.c 
b/dll/3rdparty/libtiff/mkg3states.c
index 16ad3c23727..54fc0599874 100644
--- a/dll/3rdparty/libtiff/mkg3states.c
+++ b/dll/3rdparty/libtiff/mkg3states.c
@@ -1,5 +1,3 @@
-/* "$Id: mkg3states.c,v 1.12 2015-06-21 01:09:09 bfriesen Exp $ */
-
 /*
  * Copyright (c) 1991-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_aux.c b/dll/3rdparty/libtiff/tif_aux.c
index 1f2963031fe..f70f533f1c7 100644
--- a/dll/3rdparty/libtiff/tif_aux.c
+++ b/dll/3rdparty/libtiff/tif_aux.c
@@ -1,5 +1,3 @@
-/* $Id: tif_aux.c,v 1.31 2017-11-17 20:21:00 erouault Exp $ */
-
 /*
  * Copyright (c) 1991-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_close.c b/dll/3rdparty/libtiff/tif_close.c
index fffae4acd72..4b0ea59eb95 100644
--- a/dll/3rdparty/libtiff/tif_close.c
+++ b/dll/3rdparty/libtiff/tif_close.c
@@ -1,5 +1,3 @@
-/* $Id: tif_close.c,v 1.21 2016-01-23 21:20:34 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_codec.c b/dll/3rdparty/libtiff/tif_codec.c
index 136a483359b..693cedb7882 100644
--- a/dll/3rdparty/libtiff/tif_codec.c
+++ b/dll/3rdparty/libtiff/tif_codec.c
@@ -1,5 +1,3 @@
-/* $Id: tif_codec.c,v 1.17 2015-08-19 02:31:04 bfriesen Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -73,6 +71,12 @@ static int NotConfigured(TIFF*, int);
 #ifndef LZMA_SUPPORT
 #define TIFFInitLZMA NotConfigured
 #endif
+#ifndef ZSTD_SUPPORT
+#define TIFFInitZSTD NotConfigured
+#endif
+#ifndef WEBP_SUPPORT
+#define TIFFInitWebP NotConfigured
+#endif
 
 /*
  * Compression schemes statically built into the library.
@@ -100,6 +104,8 @@ TIFFCodec _TIFFBuiltinCODECS[] = {
     { "SGILog",                COMPRESSION_SGILOG,     TIFFInitSGILog },
     { "SGILog24",      COMPRESSION_SGILOG24,   TIFFInitSGILog },
     { "LZMA",          COMPRESSION_LZMA,       TIFFInitLZMA },
+    { "ZSTD",          COMPRESSION_ZSTD,       TIFFInitZSTD },
+    { "WEBP",          COMPRESSION_WEBP,       TIFFInitWebP },
     { NULL,             0,                      NULL }
 };
 
diff --git a/dll/3rdparty/libtiff/tif_color.c b/dll/3rdparty/libtiff/tif_color.c
index 54d542cda9d..592c6c1abdb 100644
--- a/dll/3rdparty/libtiff/tif_color.c
+++ b/dll/3rdparty/libtiff/tif_color.c
@@ -1,5 +1,3 @@
-/* $Id: tif_color.c,v 1.24 2017-05-29 10:12:54 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -168,7 +166,7 @@ TIFFCIELabToRGBInit(TIFFCIELabToRGB* cielab,
 }
 
 /* 
- * Convert color value from the YCbCr space to CIE XYZ.
+ * Convert color value from the YCbCr space to RGB.
  * The colorspace conversion algorithm comes from the IJG v5a code;
  * see below for more information on how it works.
  */
diff --git a/dll/3rdparty/libtiff/tif_compress.c 
b/dll/3rdparty/libtiff/tif_compress.c
index ce3a73fc2f4..bba6deaaea9 100644
--- a/dll/3rdparty/libtiff/tif_compress.c
+++ b/dll/3rdparty/libtiff/tif_compress.c
@@ -1,5 +1,3 @@
-/* $Id: tif_compress.c,v 1.25 2016-10-25 20:04:22 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_dir.c b/dll/3rdparty/libtiff/tif_dir.c
index e38bb23226c..66c40790548 100644
--- a/dll/3rdparty/libtiff/tif_dir.c
+++ b/dll/3rdparty/libtiff/tif_dir.c
@@ -1,5 +1,3 @@
-/* $Id: tif_dir.c,v 1.131 2017-07-11 21:38:04 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -863,14 +861,24 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
        const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
        if( fip == NULL ) /* cannot happen since TIFFGetField() already checks 
it */
            return 0;
+
+       /*
+        * We want to force the custom code to be used for custom
+        * fields even if the tag happens to match a well known 
+        * one - important for reinterpreted handling of standard
+        * tag values in custom directories (i.e. EXIF) 
+        */
+       if (fip->field_bit == FIELD_CUSTOM) {
+               standard_tag = 0;
+       }
        
-        if( tag == TIFFTAG_NUMBEROFINKS )
+        if( standard_tag == TIFFTAG_NUMBEROFINKS )
         {
             int i;
             for (i = 0; i < td->td_customValueCount; i++) {
                 uint16 val;
                 TIFFTagValue *tv = td->td_customValues + i;
-                if (tv->info->field_tag != tag)
+                if (tv->info->field_tag != standard_tag)
                     continue;
                 if( tv->value == NULL )
                     return 0;
@@ -892,16 +900,6 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
             return 0;
         }
 
-       /*
-        * We want to force the custom code to be used for custom
-        * fields even if the tag happens to match a well known 
-        * one - important for reinterpreted handling of standard
-        * tag values in custom directories (i.e. EXIF) 
-        */
-       if (fip->field_bit == FIELD_CUSTOM) {
-               standard_tag = 0;
-       }
-
        switch (standard_tag) {
                case TIFFTAG_SUBFILETYPE:
                        *va_arg(ap, uint32*) = td->td_subfiletype;
@@ -1067,6 +1065,9 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
                        if (td->td_samplesperpixel - td->td_extrasamples > 1) {
                                *va_arg(ap, uint16**) = 
td->td_transferfunction[1];
                                *va_arg(ap, uint16**) = 
td->td_transferfunction[2];
+                       } else {
+                               *va_arg(ap, uint16**) = NULL;
+                               *va_arg(ap, uint16**) = NULL;
                        }
                        break;
                case TIFFTAG_REFERENCEBLACKWHITE:
diff --git a/dll/3rdparty/libtiff/tif_dirinfo.c 
b/dll/3rdparty/libtiff/tif_dirinfo.c
index bf1eebfb583..24f05aa348f 100644
--- a/dll/3rdparty/libtiff/tif_dirinfo.c
+++ b/dll/3rdparty/libtiff/tif_dirinfo.c
@@ -1,5 +1,3 @@
-/* $Id: tif_dirinfo.c,v 1.127 2017-06-01 12:44:04 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -979,6 +977,8 @@ _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag)
            case TIFFTAG_CONSECUTIVEBADFAXLINES:
            case TIFFTAG_GROUP3OPTIONS:
            case TIFFTAG_GROUP4OPTIONS:
+           /* LERC */
+           case TIFFTAG_LERC_PARAMETERS:
                break;
            default:
                return 1;
@@ -1054,7 +1054,18 @@ _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag)
                if (tag == TIFFTAG_PREDICTOR)
                    return 1;
                break;
-
+           case COMPRESSION_ZSTD:
+               if (tag == TIFFTAG_PREDICTOR)
+                   return 1;
+               break;
+           case COMPRESSION_LERC:
+               if (tag == TIFFTAG_LERC_PARAMETERS)
+                   return 1;
+               break;
+                 case COMPRESSION_WEBP:
+               if (tag == TIFFTAG_PREDICTOR)
+                               return 1;
+               break;
        }
        return 0;
 }
diff --git a/dll/3rdparty/libtiff/tif_dirread.c 
b/dll/3rdparty/libtiff/tif_dirread.c
index 5352fcd5986..ad33d6272f0 100644
--- a/dll/3rdparty/libtiff/tif_dirread.c
+++ b/dll/3rdparty/libtiff/tif_dirread.c
@@ -1,5 +1,3 @@
-/* $Id: tif_dirread.c,v 1.218 2017-09-09 21:44:42 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -167,6 +165,7 @@ static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* 
dir, uint32 nstrips, uin
 static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
 static void ChopUpSingleUncompressedStrip(TIFF*);
 static uint64 TIFFReadUInt64(const uint8 *value);
+static int _TIFFGetMaxColorChannels(uint16 photometric);
 
 static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount );
 
@@ -776,7 +775,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* 
tif, TIFFDirEntry* di
 static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(
                     TIFF* tif, uint64 offset, tmsize_t size, void** pdest)
 {
-#if SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8
+#if SIZEOF_SIZE_T == 8
         tmsize_t threshold = INITIAL_THRESHOLD;
 #endif
         tmsize_t already_read = 0;
@@ -797,7 +796,7 @@ static enum TIFFReadDirEntryErr 
TIFFReadDirEntryDataAndRealloc(
             void* new_dest;
             tmsize_t bytes_read;
             tmsize_t to_read = size - already_read;
-#if SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8
+#if SIZEOF_SIZE_T == 8
             if( to_read >= threshold && threshold < MAX_THRESHOLD )
             {
                 to_read = threshold;
@@ -3506,6 +3505,35 @@ static void TIFFReadDirEntryOutputErr(TIFF* tif, enum 
TIFFReadDirEntryErr err, c
        }
 }
 
+/*
+ * Return the maximum number of color channels specified for a given 
photometric
+ * type. 0 is returned if photometric type isn't supported or no default value
+ * is defined by the specification.
+ */
+static int _TIFFGetMaxColorChannels( uint16 photometric )
+{
+    switch (photometric) {
+       case PHOTOMETRIC_PALETTE:
+       case PHOTOMETRIC_MINISWHITE:
+       case PHOTOMETRIC_MINISBLACK:
+            return 1;
+       case PHOTOMETRIC_YCBCR:
+       case PHOTOMETRIC_RGB:
+       case PHOTOMETRIC_CIELAB:
+       case PHOTOMETRIC_LOGLUV:
+       case PHOTOMETRIC_ITULAB:
+       case PHOTOMETRIC_ICCLAB:
+            return 3;
+       case PHOTOMETRIC_SEPARATED:
+       case PHOTOMETRIC_MASK:
+            return 4;
+       case PHOTOMETRIC_LOGL:
+       case PHOTOMETRIC_CFA:
+       default:
+            return 0;
+    }
+}
+
 /*
  * Read the next TIFF directory from a file and convert it to the internal
  * format. We read directories sequentially.
@@ -3522,6 +3550,7 @@ TIFFReadDirectory(TIFF* tif)
        uint32 fii=FAILED_FII;
         toff_t nextdiroff;
     int bitspersample_read = FALSE;
+        int color_channels;
 
        tif->tif_diroff=tif->tif_nextdiroff;
        if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
@@ -4026,6 +4055,37 @@ TIFFReadDirectory(TIFF* tif)
                        }
                }
        }
+
+       /*
+        * Make sure all non-color channels are extrasamples.
+        * If it's not the case, define them as such.
+        */
+        color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
+        if (color_channels && tif->tif_dir.td_samplesperpixel - 
tif->tif_dir.td_extrasamples > color_channels) {
+                uint16 old_extrasamples;
+                uint16 *new_sampleinfo;
+
+                TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric 
type-related "
+                    "color channels and ExtraSamples doesn't match 
SamplesPerPixel. "
+                    "Defining non-color channels as ExtraSamples.");
+
+                old_extrasamples = tif->tif_dir.td_extrasamples;
+                tif->tif_dir.td_extrasamples = (uint16) 
(tif->tif_dir.td_samplesperpixel - color_channels);
+
+                // sampleinfo should contain information relative to these new 
extra samples
+                new_sampleinfo = (uint16*) 
_TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));
+                if (!new_sampleinfo) {
+                    TIFFErrorExt(tif->tif_clientdata, module, "Failed to 
allocate memory for "
+                                "temporary new sampleinfo array (%d 16 bit 
elements)",
+                                tif->tif_dir.td_extrasamples);
+                    goto bad;
+                }
+
+                memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, 
old_extrasamples * sizeof(uint16));
+                _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, 
new_sampleinfo, tif->tif_dir.td_extrasamples);
+                _TIFFfree(new_sampleinfo);
+        }
+
        /*
         * Verify Palette image has a Colormap.
         */
@@ -4881,17 +4941,18 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int 
recover)
                                err=TIFFReadDirEntryByteArray(tif,dp,&data);
                                if (err==TIFFReadDirEntryErrOk)
                                {
-                                       uint8* ma;
-                                       uint32 mb;
+                                       uint32 mb = 0;
                                        int n;
-                                       ma=data;
-                                       mb=0;
-                                       while (mb<(uint32)dp->tdir_count)
+                                       if (data != NULL)
                                        {
-                                               if (*ma==0)
-                                                       break;
-                                               ma++;
-                                               mb++;
+                                           uint8* ma = data;
+                                           while (mb<(uint32)dp->tdir_count)
+                                           {
+                                                   if (*ma==0)
+                                                           break;
+                                                   ma++;
+                                                   mb++;
+                                           }
                                        }
                                        if (mb+1<(uint32)dp->tdir_count)
                                                
TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" contains 
null byte in value; value incorrectly truncated during reading due to 
implementation limitations",fip->field_name);
@@ -5141,11 +5202,11 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int 
recover)
                                        if (err==TIFFReadDirEntryErrOk)
                                        {
                                                int m;
-                        if( dp->tdir_count > 0 && data[dp->tdir_count-1] != 
'\0' )
-                        {
-                            TIFFWarningExt(tif->tif_clientdata,module,"ASCII 
value for tag \"%s\" does not end in null byte. Forcing it to be 
null",fip->field_name);
-                            data[dp->tdir_count-1] = '\0';
-                        }
+                                               if( data != 0 && dp->tdir_count 
> 0 && data[dp->tdir_count-1] != '\0' )
+                                               {
+                                                   
TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not 
end in null byte. Forcing it to be null",fip->field_name);
+                                                   data[dp->tdir_count-1] = 
'\0';
+                                               }
                                                
m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
                                                if (data!=0)
                                                        _TIFFfree(data);
@@ -5318,11 +5379,11 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int 
recover)
                                if (err==TIFFReadDirEntryErrOk)
                                {
                                        int m;
-                    if( dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
-                    {
-                        TIFFWarningExt(tif->tif_clientdata,module,"ASCII value 
for tag \"%s\" does not end in null byte. Forcing it to be 
null",fip->field_name);
-                        data[dp->tdir_count-1] = '\0';
-                    }
+                                       if( data != 0 && dp->tdir_count > 0 && 
data[dp->tdir_count-1] != '\0' )
+                                       {
+                                           
TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not 
end in null byte. Forcing it to be null",fip->field_name);
+                                            data[dp->tdir_count-1] = '\0';
+                                       }
                                        
m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
                                        if (data!=0)
                                                _TIFFfree(data);
@@ -5698,6 +5759,16 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
         if( nstrips == 0 )
             return;
 
+        /* If we are going to allocate a lot of memory, make sure that the */
+        /* file is as big as needed */
+        if( tif->tif_mode == O_RDONLY &&
+            nstrips > 1000000 &&
+            (offset >= TIFFGetFileSize(tif) ||
+             stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)) )
+        {
+            return;
+        }
+
        newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
                                "for chopped \"StripByteCounts\" array");
        newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
diff --git a/dll/3rdparty/libtiff/tif_dirwrite.c 
b/dll/3rdparty/libtiff/tif_dirwrite.c
index 21f57a12c0f..acc41c89662 100644
--- a/dll/3rdparty/libtiff/tif_dirwrite.c
+++ b/dll/3rdparty/libtiff/tif_dirwrite.c
@@ -1,5 +1,3 @@
-/* $Id: tif_dirwrite.c,v 1.89 2017-08-23 13:33:42 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -697,8 +695,11 @@ TIFFWriteDirectorySec(TIFF* tif, int isimage, int 
imagedone, uint64* pdiroff)
                                                                }
                                                                break;
                                                        default:
-                                                               assert(0);   /* 
we should never get here */
-                                                               break;
+                                                               
TIFFErrorExt(tif->tif_clientdata,module,
+                                                                           
"Cannot write tag %d (%s)",
+                                                                           
TIFFFieldTag(o),
+                                                                            
o->field_name ? o->field_name : "unknown");
+                                                               goto bad;
                                                }
                                        }
                                }
diff --git a/dll/3rdparty/libtiff/tif_dumpmode.c 
b/dll/3rdparty/libtiff/tif_dumpmode.c
index 1e1425a3a0d..910e6167ef0 100644
--- a/dll/3rdparty/libtiff/tif_dumpmode.c
+++ b/dll/3rdparty/libtiff/tif_dumpmode.c
@@ -1,5 +1,3 @@
-/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dumpmode.c,v 1.15 
2015-12-12 18:04:26 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_error.c b/dll/3rdparty/libtiff/tif_error.c
index a8f1af856ca..72695633630 100644
--- a/dll/3rdparty/libtiff/tif_error.c
+++ b/dll/3rdparty/libtiff/tif_error.c
@@ -1,5 +1,3 @@
-/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_error.c,v 1.6 2017-07-04 
12:54:42 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_extension.c 
b/dll/3rdparty/libtiff/tif_extension.c
index c10fa34bb13..18407b03790 100644
--- a/dll/3rdparty/libtiff/tif_extension.c
+++ b/dll/3rdparty/libtiff/tif_extension.c
@@ -1,5 +1,3 @@
-/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_extension.c,v 1.8 
2015-12-06 11:13:43 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_fax3.c b/dll/3rdparty/libtiff/tif_fax3.c
index 2047bc60b6e..447a5760292 100644
--- a/dll/3rdparty/libtiff/tif_fax3.c
+++ b/dll/3rdparty/libtiff/tif_fax3.c
@@ -1,5 +1,3 @@
-/* $Id: tif_fax3.c,v 1.81 2017-06-18 10:31:50 erouault Exp $ */
-
 /*
  * Copyright (c) 1990-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_flush.c b/dll/3rdparty/libtiff/tif_flush.c
index 94f97c58d46..d76738a1915 100644
--- a/dll/3rdparty/libtiff/tif_flush.c
+++ b/dll/3rdparty/libtiff/tif_flush.c
@@ -1,5 +1,3 @@
-/* $Id: tif_flush.c,v 1.9 2010-03-31 06:40:10 fwarmerdam Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_getimage.c 
b/dll/3rdparty/libtiff/tif_getimage.c
index d0f4af4fb27..7fc00d6ab65 100644
--- a/dll/3rdparty/libtiff/tif_getimage.c
+++ b/dll/3rdparty/libtiff/tif_getimage.c
@@ -1,5 +1,3 @@
-/* $Id: tif_getimage.c,v 1.114 2017-11-17 20:21:00 erouault Exp $ */
-
 /*
  * Copyright (c) 1991-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_jbig.c b/dll/3rdparty/libtiff/tif_jbig.c
index 707fa9c791a..7ffe8851e8d 100644
--- a/dll/3rdparty/libtiff/tif_jbig.c
+++ b/dll/3rdparty/libtiff/tif_jbig.c
@@ -1,5 +1,3 @@
-/* $Id: tif_jbig.c,v 1.16 2017-06-26 15:20:00 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -32,7 +30,7 @@
  *
  */
 
-#include <precomp.h>
+#include "tiffiop.h"
 
 #ifdef JBIG_SUPPORT
 #include "jbig.h"
@@ -53,17 +51,18 @@ static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t 
size, uint16 s)
        struct jbg_dec_state decoder;
        int decodeStatus = 0;
        unsigned char* pImage = NULL;
-       (void) size, (void) s;
+       unsigned long decodedSize;
+       (void) s;
 
        if (isFillOrder(tif, tif->tif_dir.td_fillorder))
        {
-               TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
+               TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc);
        }
 
        jbg_dec_init(&decoder);
 
 #if defined(HAVE_JBG_NEWLEN)
-       jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
+       jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc);
        /*
         * I do not check the return status of jbg_newlen because even if this
         * function fails it does not necessarily mean that decoding the image
@@ -76,8 +75,8 @@ static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t 
size, uint16 s)
         */
 #endif /* HAVE_JBG_NEWLEN */
 
-       decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,
-                                 (size_t)tif->tif_rawdatasize, NULL);
+       decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawcp,
+                                 (size_t)tif->tif_rawcc, NULL);
        if (JBG_EOK != decodeStatus)
        {
                /*
@@ -98,9 +97,28 @@ static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t 
size, uint16 s)
                return 0;
        }
 
+       decodedSize = jbg_dec_getsize(&decoder);
+       if( (tmsize_t)decodedSize < size )
+       {
+           TIFFWarningExt(tif->tif_clientdata, "JBIG",
+                          "Only decoded %lu bytes, whereas %lu requested",
+                          decodedSize, (unsigned long)size);
+       }
+       else if( (tmsize_t)decodedSize > size )
+       {
+           TIFFErrorExt(tif->tif_clientdata, "JBIG",
+                        "Decoded %lu bytes, whereas %lu were requested",
+                        decodedSize, (unsigned long)size);
+           jbg_dec_free(&decoder);
+           return 0;
+       }
        pImage = jbg_dec_getimage(&decoder, 0);
-       _TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
+       _TIFFmemcpy(buffer, pImage, decodedSize);
        jbg_dec_free(&decoder);
+
+        tif->tif_rawcp += tif->tif_rawcc;
+        tif->tif_rawcc = 0;
+
        return 1;
 }
 
diff --git a/dll/3rdparty/libtiff/tif_jpeg.c b/dll/3rdparty/libtiff/tif_jpeg.c
index 9d4f3dd7007..f2ddc331a0e 100644
--- a/dll/3rdparty/libtiff/tif_jpeg.c
+++ b/dll/3rdparty/libtiff/tif_jpeg.c
@@ -1,5 +1,3 @@
-/* $Id: tif_jpeg.c,v 1.134 2017-10-17 19:04:47 erouault Exp $ */
-
 /*
  * Copyright (c) 1994-1997 Sam Leffler
  * Copyright (c) 1994-1997 Silicon Graphics, Inc.
@@ -27,7 +25,8 @@
 #define WIN32_LEAN_AND_MEAN
 #define VC_EXTRALEAN
 
-#include <precomp.h>
+#include "tiffiop.h"
+#include <stdlib.h>
 
 #ifdef JPEG_SUPPORT
 
@@ -75,7 +74,7 @@ int TIFFJPEGIsFullStripRequired_12(TIFF* tif);
    "JPEGLib: JPEG parameter struct mismatch: library thinks size is 432,
    caller expects 464"
 
-   For such users we wil fix the problem here. See install.doc file from
+   For such users we will fix the problem here. See install.doc file from
    the JPEG library distribution for details.
 */
 
diff --git a/dll/3rdparty/libtiff/tif_luv.c b/dll/3rdparty/libtiff/tif_luv.c
index e6be21e4dbd..3b9e6337f7d 100644
--- a/dll/3rdparty/libtiff/tif_luv.c
+++ b/dll/3rdparty/libtiff/tif_luv.c
@@ -1,5 +1,3 @@
-/* $Id: tif_luv.c,v 1.49 2017-07-24 12:47:30 erouault Exp $ */
-
 /*
  * Copyright (c) 1997 Greg Ward Larson
  * Copyright (c) 1997 Silicon Graphics, Inc.
@@ -215,7 +213,7 @@ LogL16Decode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
        bp = (unsigned char*) tif->tif_rawcp;
        cc = tif->tif_rawcc;
        /* get each byte string */
-       for (shft = 2*8; (shft -= 8) >= 0; ) {
+       for (shft = 8; shft >= 0; shft -=8) {
                for (i = 0; i < npixels && cc > 0; ) {
                        if (*bp >= 128) {               /* run */
                                if( cc < 2 )
@@ -349,7 +347,7 @@ LogLuvDecode32(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
        bp = (unsigned char*) tif->tif_rawcp;
        cc = tif->tif_rawcc;
        /* get each byte string */
-       for (shft = 4*8; (shft -= 8) >= 0; ) {
+       for (shft = 24; shft >= 0; shft -=8) {
                for (i = 0; i < npixels && cc > 0; ) {
                        if (*bp >= 128) {               /* run */
                                if( cc < 2 )
@@ -467,7 +465,7 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
        /* compress each byte string */
        op = tif->tif_rawcp;
        occ = tif->tif_rawdatasize - tif->tif_rawcc;
-       for (shft = 2*8; (shft -= 8) >= 0; )
+       for (shft = 8; shft >= 0; shft -=8) {
                for (i = 0; i < npixels; i += rc) {
                        if (occ < 4) {
                                tif->tif_rawcp = op;
@@ -522,6 +520,7 @@ LogL16Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
                        } else
                                rc = 0;
                }
+       }
        tif->tif_rawcp = op;
        tif->tif_rawcc = tif->tif_rawdatasize - occ;
 
@@ -618,7 +617,7 @@ LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
        /* compress each byte string */
        op = tif->tif_rawcp;
        occ = tif->tif_rawdatasize - tif->tif_rawcc;
-       for (shft = 4*8; (shft -= 8) >= 0; )
+       for (shft = 24; shft >= 0; shft -=8) {
                for (i = 0; i < npixels; i += rc) {
                        if (occ < 4) {
                                tif->tif_rawcp = op;
@@ -673,6 +672,7 @@ LogLuvEncode32(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
                        } else
                                rc = 0;
                }
+       }
        tif->tif_rawcp = op;
        tif->tif_rawcc = tif->tif_rawdatasize - occ;
 
diff --git a/dll/3rdparty/libtiff/tif_lzma.c b/dll/3rdparty/libtiff/tif_lzma.c
index b95fe46f850..19f960fa77d 100644
--- a/dll/3rdparty/libtiff/tif_lzma.c
+++ b/dll/3rdparty/libtiff/tif_lzma.c
@@ -1,5 +1,3 @@
-/* $Id: tif_lzma.c,v 1.6 2016-09-17 09:18:59 erouault Exp $ */
-
 /*
  * Copyright (c) 2010, Andrey Kiselev <[email protected]>
  *
@@ -250,6 +248,7 @@ LZMAPreEncode(TIFF* tif, uint16 s)
 {
        static const char module[] = "LZMAPreEncode";
        LZMAState *sp = EncoderState(tif);
+       lzma_ret ret;
 
        (void) s;
        assert(sp != NULL);
@@ -263,7 +262,13 @@ LZMAPreEncode(TIFF* tif, uint16 s)
                             "Liblzma cannot deal with buffers this size");
                return 0;
        }
-       return (lzma_stream_encoder(&sp->stream, sp->filters, sp->check) == 
LZMA_OK);
+       ret = lzma_stream_encoder(&sp->stream, sp->filters, sp->check);
+       if (ret != LZMA_OK) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                       "Error in lzma_stream_encoder(): %s", 
LZMAStrerror(ret));
+               return 0;
+       }
+       return 1;
 }
 
 /*
diff --git a/dll/3rdparty/libtiff/tif_lzw.c b/dll/3rdparty/libtiff/tif_lzw.c
index c6ab1bed44e..47260533e6e 100644
--- a/dll/3rdparty/libtiff/tif_lzw.c
+++ b/dll/3rdparty/libtiff/tif_lzw.c
@@ -1,5 +1,3 @@
-/* $Id: tif_lzw.c,v 1.57 2017-07-11 10:54:29 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -135,6 +133,7 @@ typedef struct {
        long    dec_restart;            /* restart count */
 #ifdef LZW_CHECKEOS
        uint64  dec_bitsleft;           /* available bits in raw data */
+       tmsize_t old_tif_rawcc;         /* value of tif_rawcc at the end of the 
previous TIFLZWDecode() call */
 #endif
        decodeFunc dec_decode;          /* regular or backwards compatible */
        code_t* dec_codep;              /* current recognized code */
@@ -320,6 +319,7 @@ LZWPreDecode(TIFF* tif, uint16 s)
        sp->dec_nbitsmask = MAXCODE(BITS_MIN);
 #ifdef LZW_CHECKEOS
        sp->dec_bitsleft = 0;
+        sp->old_tif_rawcc = 0;
 #endif
        sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
        /*
@@ -427,7 +427,7 @@ LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
 
        bp = (unsigned char *)tif->tif_rawcp;
 #ifdef LZW_CHECKEOS
-       sp->dec_bitsleft = (((uint64)tif->tif_rawcc) << 3);
+       sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
 #endif
        nbits = sp->lzw_nbits;
        nextdata = sp->lzw_nextdata;
@@ -555,6 +555,9 @@ LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
 
        tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
        tif->tif_rawcp = (uint8*) bp;
+#ifdef LZW_CHECKEOS
+       sp->old_tif_rawcc = tif->tif_rawcc;
+#endif
        sp->lzw_nbits = (unsigned short) nbits;
        sp->lzw_nextdata = nextdata;
        sp->lzw_nextbits = nextbits;
@@ -604,6 +607,7 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, 
uint16 s)
        char *tp;
        unsigned char *bp;
        int code, nbits;
+       int len;
        long nextbits, nextdata, nbitsmask;
        code_t *codep, *free_entp, *maxcodep, *oldcodep;
 
@@ -657,7 +661,7 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, 
uint16 s)
 
        bp = (unsigned char *)tif->tif_rawcp;
 #ifdef LZW_CHECKEOS
-       sp->dec_bitsleft = (((uint64)tif->tif_rawcc) << 3);
+       sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
 #endif
        nbits = sp->lzw_nbits;
        nextdata = sp->lzw_nextdata;
@@ -755,13 +759,18 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, 
uint16 s)
                                }  while (--occ);
                                break;
                        }
-                       assert(occ >= codep->length);
-                       op += codep->length;
-                       occ -= codep->length;
-                       tp = op;
+                       len = codep->length;
+                       tp = op + len;
                        do {
-                               *--tp = codep->value;
-                       } while( (codep = codep->next) != NULL );
+                               int t;
+                               --tp;
+                               t = codep->value;
+                               codep = codep->next;
+                               *tp = (char)t;
+                       } while (codep && tp > op);
+                       assert(occ >= len);
+                       op += len;
+                       occ -= len;
                } else {
                        *op++ = (char)code;
                        occ--;
@@ -770,6 +779,9 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, 
uint16 s)
 
        tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
        tif->tif_rawcp = (uint8*) bp;
+#ifdef LZW_CHECKEOS
+       sp->old_tif_rawcc = tif->tif_rawcc;
+#endif
        sp->lzw_nbits = (unsigned short)nbits;
        sp->lzw_nextdata = nextdata;
        sp->lzw_nextbits = nextbits;
diff --git a/dll/3rdparty/libtiff/tif_next.c b/dll/3rdparty/libtiff/tif_next.c
index c75f7f1469f..69e8fc6d3e3 100644
--- a/dll/3rdparty/libtiff/tif_next.c
+++ b/dll/3rdparty/libtiff/tif_next.c
@@ -1,5 +1,3 @@
-/* $Id: tif_next.c,v 1.19 2016-09-04 21:32:56 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_ojpeg.c b/dll/3rdparty/libtiff/tif_ojpeg.c
index 4da45778a08..27385d8c47d 100644
--- a/dll/3rdparty/libtiff/tif_ojpeg.c
+++ b/dll/3rdparty/libtiff/tif_ojpeg.c
@@ -1,5 +1,3 @@
-/* $Id: tif_ojpeg.c,v 1.69 2017-04-27 17:29:26 erouault Exp $ */
-
 /* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
    specification is now totally obsolete and deprecated for new applications 
and
    images. This file was was created solely in order to read unconverted images
@@ -123,8 +121,7 @@
 #define WIN32_LEAN_AND_MEAN
 #define VC_EXTRALEAN
 
-#include <precomp.h>
-
+#include "tiffiop.h"
 #ifdef OJPEG_SUPPORT
 
 /* Configuration defines here are:
diff --git a/dll/3rdparty/libtiff/tif_open.c b/dll/3rdparty/libtiff/tif_open.c
index 58fbb0a8be6..7a9f074c8ea 100644
--- a/dll/3rdparty/libtiff/tif_open.c
+++ b/dll/3rdparty/libtiff/tif_open.c
@@ -1,5 +1,3 @@
-/* $Id: tif_open.c,v 1.48 2016-11-20 22:29:47 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_packbits.c 
b/dll/3rdparty/libtiff/tif_packbits.c
index e813312be29..f464e177390 100644
--- a/dll/3rdparty/libtiff/tif_packbits.c
+++ b/dll/3rdparty/libtiff/tif_packbits.c
@@ -1,5 +1,3 @@
-/* $Id: tif_packbits.c,v 1.26 2017-05-14 02:26:07 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_pixarlog.c 
b/dll/3rdparty/libtiff/tif_pixarlog.c
index 53bdd4d55e9..655a12b597e 100644
--- a/dll/3rdparty/libtiff/tif_pixarlog.c
+++ b/dll/3rdparty/libtiff/tif_pixarlog.c
@@ -1,5 +1,3 @@
-/* $Id: tif_pixarlog.c,v 1.54 2017-07-10 10:40:28 erouault Exp $ */
-
 /*
  * Copyright (c) 1996-1997 Sam Leffler
  * Copyright (c) 1996 Pixar
diff --git a/dll/3rdparty/libtiff/tif_predict.c 
b/dll/3rdparty/libtiff/tif_predict.c
index f83b19fe62c..ebdf6c8cd13 100644
--- a/dll/3rdparty/libtiff/tif_predict.c
+++ b/dll/3rdparty/libtiff/tif_predict.c
@@ -1,5 +1,3 @@
-/* $Id: tif_predict.c,v 1.44 2017-06-18 10:31:50 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_print.c b/dll/3rdparty/libtiff/tif_print.c
index 37a21110203..b5a3e8cb7c5 100644
--- a/dll/3rdparty/libtiff/tif_print.c
+++ b/dll/3rdparty/libtiff/tif_print.c
@@ -1,5 +1,3 @@
-/* $Id: tif_print.c,v 1.65 2016-11-20 22:31:22 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -547,7 +545,7 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
                                uint16 i;
                                fprintf(fd, "    %2ld: %5u",
                                    l, td->td_transferfunction[0][l]);
-                               for (i = 1; i < td->td_samplesperpixel; i++)
+                               for (i = 1; i < td->td_samplesperpixel - 
td->td_extrasamples && i < 3; i++)
                                        fprintf(fd, " %5u",
                                            td->td_transferfunction[i][l]);
                                fputc('\n', fd);
@@ -668,13 +666,13 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
                        fprintf(fd, "    %3lu: [%8I64u, %8I64u]\n",
                            (unsigned long) s,
-                           (unsigned __int64) td->td_stripoffset[s],
-                           (unsigned __int64) td->td_stripbytecount[s]);
+                           td->td_stripoffset ? (unsigned __int64) 
td->td_stripoffset[s] : 0,
+                           td->td_stripbytecount ? (unsigned __int64) 
td->td_stripbytecount[s] : 0);
 #else
                        fprintf(fd, "    %3lu: [%8llu, %8llu]\n",
                            (unsigned long) s,
-                           (unsigned long long) td->td_stripoffset[s],
-                           (unsigned long long) td->td_stripbytecount[s]);
+                           td->td_stripoffset ? (unsigned long long) 
td->td_stripoffset[s] : 0,
+                           td->td_stripbytecount ? (unsigned long long) 
td->td_stripbytecount[s] : 0);
 #endif
        }
 }
diff --git a/dll/3rdparty/libtiff/tif_read.c b/dll/3rdparty/libtiff/tif_read.c
index 2d0e0f73b94..78c23aa5115 100644
--- a/dll/3rdparty/libtiff/tif_read.c
+++ b/dll/3rdparty/libtiff/tif_read.c
@@ -1,5 +1,3 @@
-/* $Id: tif_read.c,v 1.66 2017-11-17 20:21:00 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -58,7 +56,7 @@ static int TIFFReadAndRealloc( TIFF* tif, tmsize_t size,
                                int is_strip, uint32 strip_or_tile,
                                const char* module )
 {
-#if SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8
+#if SIZEOF_SIZE_T == 8
         tmsize_t threshold = INITIAL_THRESHOLD;
 #endif
         tmsize_t already_read = 0;
@@ -73,7 +71,7 @@ static int TIFFReadAndRealloc( TIFF* tif, tmsize_t size,
         {
             tmsize_t bytes_read;
             tmsize_t to_read = size - already_read;
-#if SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8
+#if SIZEOF_SIZE_T == 8
             if( to_read >= threshold && threshold < MAX_THRESHOLD &&
                 already_read + to_read + rawdata_offset > tif->tif_rawdatasize 
)
             {
@@ -348,6 +346,13 @@ TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
             return 0;
         whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
                 || isMapped(tif);
+        if( td->td_compression == COMPRESSION_LERC ||
+            td->td_compression == COMPRESSION_JBIG )
+        {
+            /* Ideally plugins should have a way to declare they don't support
+             * chunk strip */
+            whole_strip = 1;
+        }
 #else
         whole_strip = 1;
 #endif
diff --git a/dll/3rdparty/libtiff/tif_stream.cxx 
b/dll/3rdparty/libtiff/tif_stream.cxx
index ecca1fd54b0..7f640a9c0a4 100644
--- a/dll/3rdparty/libtiff/tif_stream.cxx
+++ b/dll/3rdparty/libtiff/tif_stream.cxx
@@ -1,5 +1,3 @@
-/* $Id: tif_stream.cxx,v 1.13 2015-05-28 01:50:22 bfriesen Exp $ */
-
 /*
  * Copyright (c) 1988-1996 Sam Leffler
  * Copyright (c) 1991-1996 Silicon Graphics, Inc.
@@ -375,6 +373,9 @@ _tiffStreamOpen(const char* name, const char* mode, void 
*fd)
                                _tiffosSizeProc,
                                _tiffDummyMapProc,
                                 _tiffDummyUnmapProc);
+               if (!tif) {
+                       delete data;
+               }
        } else {
                tiffis_data     *data = new tiffis_data;
                data->stream = reinterpret_cast<istream *>(fd);
@@ -389,6 +390,9 @@ _tiffStreamOpen(const char* name, const char* mode, void 
*fd)
                                _tiffisSizeProc,
                                _tiffDummyMapProc,
                                 _tiffDummyUnmapProc);
+               if (!tif) {
+                       delete data;
+               }
        }
 
        return (tif);
diff --git a/dll/3rdparty/libtiff/tif_strip.c b/dll/3rdparty/libtiff/tif_strip.c
index 9beaa3b18c3..d468714a48a 100644
--- a/dll/3rdparty/libtiff/tif_strip.c
+++ b/dll/3rdparty/libtiff/tif_strip.c
@@ -1,5 +1,3 @@
-/* $Id: tif_strip.c,v 1.38 2016-12-03 11:02:15 erouault Exp $ */
-
 /*
  * Copyright (c) 1991-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_swab.c b/dll/3rdparty/libtiff/tif_swab.c
index c16c6fa0561..30bf160834d 100644
--- a/dll/3rdparty/libtiff/tif_swab.c
+++ b/dll/3rdparty/libtiff/tif_swab.c
@@ -1,5 +1,3 @@
-/* $Id: tif_swab.c,v 1.15 2017-06-08 16:39:50 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_thunder.c 
b/dll/3rdparty/libtiff/tif_thunder.c
index 523fd17ea63..cec87703227 100644
--- a/dll/3rdparty/libtiff/tif_thunder.c
+++ b/dll/3rdparty/libtiff/tif_thunder.c
@@ -1,5 +1,3 @@
-/* $Id: tif_thunder.c,v 1.13 2016-09-04 21:32:56 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_tile.c b/dll/3rdparty/libtiff/tif_tile.c
index b416823fa85..36a0000dbbb 100644
--- a/dll/3rdparty/libtiff/tif_tile.c
+++ b/dll/3rdparty/libtiff/tif_tile.c
@@ -1,5 +1,3 @@
-/* $Id: tif_tile.c,v 1.24 2015-06-07 22:35:40 bfriesen Exp $ */
-
 /*
  * Copyright (c) 1991-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_unix.c b/dll/3rdparty/libtiff/tif_unix.c
index 80c437cfa37..874f1feb268 100644
--- a/dll/3rdparty/libtiff/tif_unix.c
+++ b/dll/3rdparty/libtiff/tif_unix.c
@@ -1,5 +1,3 @@
-/* $Id: tif_unix.c,v 1.28 2017-01-11 19:02:49 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_version.c 
b/dll/3rdparty/libtiff/tif_version.c
index 37e6c309e33..0139cdb4e9b 100644
--- a/dll/3rdparty/libtiff/tif_version.c
+++ b/dll/3rdparty/libtiff/tif_version.c
@@ -1,4 +1,3 @@
-/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_version.c,v 1.3 
2010-03-10 18:56:49 bfriesen Exp $ */
 /*
  * Copyright (c) 1992-1997 Sam Leffler
  * Copyright (c) 1992-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_warning.c 
b/dll/3rdparty/libtiff/tif_warning.c
index 51ce2001098..49f4d5828db 100644
--- a/dll/3rdparty/libtiff/tif_warning.c
+++ b/dll/3rdparty/libtiff/tif_warning.c
@@ -1,5 +1,3 @@
-/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_warning.c,v 1.4 
2017-07-04 12:54:42 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_webp.c b/dll/3rdparty/libtiff/tif_webp.c
new file mode 100644
index 00000000000..a002f481daa
--- /dev/null
+++ b/dll/3rdparty/libtiff/tif_webp.c
@@ -0,0 +1,684 @@
+/*
+* Copyright (c) 2018, Mapbox
+* Author: <norman.barker at mapbox.com>
+*
+* 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.
+*
+* 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
+* OF THIS SOFTWARE.
+*/
+
+#include "tiffiop.h"
+#ifdef WEBP_SUPPORT
+/*
+ * TIFF Library.
+ *
+ * WEBP Compression Support
+ *
+ */
+
+#include "webp/decode.h"
+#include "webp/encode.h"
+
+#include <stdio.h>
+
+#define LSTATE_INIT_DECODE 0x01
+#define LSTATE_INIT_ENCODE 0x02
+/*
+ * State block for each open TIFF
+ * file using WEBP compression/decompression.
+ */
+typedef struct {
+  uint16           nSamples;               /* number of samples per pixel */
+  
+  int              lossless;               /* lossy/lossless compression */
+  int              quality_level;          /* compression level */
+  WebPPicture      sPicture;               /* WebP Picture */
+  WebPConfig       sEncoderConfig;         /* WebP encoder config */
+  uint8*           pBuffer;                /* buffer to hold raw data on 
encoding */
+  unsigned int     buffer_offset;          /* current offset into the buffer */
+  unsigned int     buffer_size;
+  
+  WebPIDecoder*    psDecoder;              /* WebPIDecoder */
+  WebPDecBuffer    sDecBuffer;             /* Decoder buffer */
+  int              last_y;                 /* Last row decoded */
+  
+  int              state;                  /* state flags */
+  
+       TIFFVGetMethod   vgetparent;             /* super-class method */
+       TIFFVSetMethod   vsetparent;             /* super-class method */
+} WebPState;
+
+#define LState(tif)            ((WebPState*) (tif)->tif_data)
+#define DecoderState(tif)       LState(tif)
+#define EncoderState(tif)       LState(tif)
+
+static int TWebPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int TWebPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
+
+static
+int TWebPDatasetWriter(const uint8_t* data, size_t data_size,
+                      const WebPPicture* const picture)
+{
+  static const char module[] = "TWebPDatasetWriter";
+  TIFF* tif = (TIFF*)(picture->custom_ptr);
+  
+  if ( (tif->tif_rawcc + (tmsize_t)data_size) > tif->tif_rawdatasize ) {
+    TIFFErrorExt(tif->tif_clientdata, module,
+                 "Buffer too small by " TIFF_SIZE_FORMAT " bytes.",
+                 (size_t) (tif->tif_rawcc + data_size - tif->tif_rawdatasize));
+    return 0;
+  } else {
+    _TIFFmemcpy(tif->tif_rawcp, data, data_size);
+    tif->tif_rawcc += data_size;
+    tif->tif_rawcp += data_size;
+    return 1;    
+  }
+}
+
+/*
+ * Encode a chunk of pixels.
+ */
+static int
+TWebPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+  static const char module[] = "TWebPEncode";
+  WebPState *sp = EncoderState(tif);
+  (void) s;
+
+  assert(sp != NULL);
+  assert(sp->state == LSTATE_INIT_ENCODE);
+    
+  if( (uint64)sp->buffer_offset +
+                            (uint64)cc > sp->buffer_size )
+  {
+      TIFFErrorExt(tif->tif_clientdata, module,
+                   "Too many bytes to be written");
+      return 0;
+  }
+
+  memcpy(sp->pBuffer + sp->buffer_offset,
+         bp, cc);
+  sp->buffer_offset += (unsigned)cc;
+
+  return 1;
+  
+}
+
+static int
+TWebPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+  static const char module[] = "WebPDecode";
+  VP8StatusCode status = VP8_STATUS_OK;
+  WebPState *sp = DecoderState(tif);
+  (void) s;  
+
+  assert(sp != NULL);
+  assert(sp->state == LSTATE_INIT_DECODE);
+  
+  if (occ % sp->sDecBuffer.u.RGBA.stride)
+  {
+    TIFFErrorExt(tif->tif_clientdata, module,
+                 "Fractional scanlines cannot be read");
+    return 0;
+  }
+
+  status = WebPIAppend(sp->psDecoder, tif->tif_rawcp, tif->tif_rawcc);
+
+  if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {
+    if (status == VP8_STATUS_INVALID_PARAM) {
+       TIFFErrorExt(tif->tif_clientdata, module,
+         "Invalid parameter used.");      
+    } else if (status == VP8_STATUS_OUT_OF_MEMORY) {
+      TIFFErrorExt(tif->tif_clientdata, module,
+        "Out of memory.");         
+    } else {
+      TIFFErrorExt(tif->tif_clientdata, module,
+        "Unrecognized error.");   
+    }
+    return 0;
+  } else {
+    int current_y, stride;
+    uint8_t* buf;
+
+    /* Returns the RGB/A image decoded so far */
+    buf = WebPIDecGetRGB(sp->psDecoder, &current_y, NULL, NULL, &stride);
+    
+    if ((buf != NULL) &&
+        (occ <= stride * (current_y - sp->last_y))) {
+      memcpy(op,   
+         buf + (sp->last_y * stride),
+         occ);
+
+      tif->tif_rawcp += tif->tif_rawcc;
+      tif->tif_rawcc = 0;
+      sp->last_y += occ / sp->sDecBuffer.u.RGBA.stride;
+      return 1;
+    } else {
+      TIFFErrorExt(tif->tif_clientdata, module, "Unable to decode WebP 
data."); 
+      return 0;
+    }
+  }
+}
+
+static int
+TWebPFixupTags(TIFF* tif)
+{
+  (void) tif;
+  if (tif->tif_dir.td_planarconfig != PLANARCONFIG_CONTIG) {
+    static const char module[] = "TWebPFixupTags";
+    TIFFErrorExt(tif->tif_clientdata, module,
+      "TIFF WEBP requires data to be stored contiguously in RGB e.g. RGBRGBRGB 
"
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+      "or RGBARGBARGBA"
+#endif
+    );
+    return 0;
+  }
+  return 1;
+}
+
+static int
+TWebPSetupDecode(TIFF* tif)
+{
+  static const char module[] = "WebPSetupDecode";
+  uint16 nBitsPerSample = tif->tif_dir.td_bitspersample;
+  uint16 sampleFormat = tif->tif_dir.td_sampleformat;
+
+  WebPState* sp = DecoderState(tif);
+  assert(sp != NULL);
+
+  sp->nSamples = tif->tif_dir.td_samplesperpixel;
+
+  /* check band count */
+  if ( sp->nSamples != 3
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+    && sp->nSamples != 4
+#endif
+  )
+  {
+    TIFFErrorExt(tif->tif_clientdata, module,
+      "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
+  #if WEBP_ENCODER_ABI_VERSION >= 0x0100
+      "or 4 (RGBA) "
+  #endif
+    "bands.",
+    sp->nSamples );
+    return 0;
+  }
+
+  /* check bits per sample and data type */
+  if ((nBitsPerSample != 8) && (sampleFormat != 1)) {
+    TIFFErrorExt(tif->tif_clientdata, module,
+                "WEBP driver requires 8 bit unsigned data");
+    return 0;
+  }
+  
+  /* if we were last encoding, terminate this mode */
+  if (sp->state & LSTATE_INIT_ENCODE) {
+      WebPPictureFree(&sp->sPicture);
+      if (sp->pBuffer != NULL) {
+        _TIFFfree(sp->pBuffer);
+        sp->pBuffer = NULL;
+      }
+      sp->buffer_offset = 0;
+      sp->state = 0;
+  }
+
+  sp->state |= LSTATE_INIT_DECODE;
+
+  return 1;
+}
+
+/*
+* Setup state for decoding a strip.
+*/
+static int
+TWebPPreDecode(TIFF* tif, uint16 s)
+{
+  static const char module[] = "TWebPPreDecode";
+  uint32 segment_width, segment_height;
+  WebPState* sp = DecoderState(tif);
+  TIFFDirectory* td = &tif->tif_dir;
+  (void) s;
+  assert(sp != NULL);
+  
+  if (isTiled(tif)) {
+    segment_width = td->td_tilewidth;
+    segment_height = td->td_tilelength;
+  } else {
+    segment_width = td->td_imagewidth;
+    segment_height = td->td_imagelength - tif->tif_row;
+    if (segment_height > td->td_rowsperstrip)
+      segment_height = td->td_rowsperstrip;
+  }
+
+  if( (sp->state & LSTATE_INIT_DECODE) == 0 )
+      tif->tif_setupdecode(tif);
+      
+  if (sp->psDecoder != NULL) {
+    WebPIDelete(sp->psDecoder);
+    WebPFreeDecBuffer(&sp->sDecBuffer);
+    sp->psDecoder = NULL;
+  }
+
+  sp->last_y = 0;
+  
+  WebPInitDecBuffer(&sp->sDecBuffer);
+  
+  sp->sDecBuffer.is_external_memory = 0;
+  sp->sDecBuffer.width = segment_width;
+  sp->sDecBuffer.height = segment_height;
+  sp->sDecBuffer.u.RGBA.stride = segment_width * sp->nSamples;
+  sp->sDecBuffer.u.RGBA.size = segment_width * sp->nSamples * segment_height;
+  
+  if (sp->nSamples > 3) {
+    sp->sDecBuffer.colorspace = MODE_RGBA;
+  } else {
+    sp->sDecBuffer.colorspace = MODE_RGB;
+  }
+  
+  sp->psDecoder = WebPINewDecoder(&sp->sDecBuffer);
+  
+  if (sp->psDecoder == NULL) {
+    TIFFErrorExt(tif->tif_clientdata, module,
+                "Unable to allocate WebP decoder.");
+    return 0;
+  }
+  
+  return 1;
+}
+
+static int
+TWebPSetupEncode(TIFF* tif)
+{
+  static const char module[] = "WebPSetupEncode";
+  uint16 nBitsPerSample = tif->tif_dir.td_bitspersample;
+  uint16 sampleFormat = tif->tif_dir.td_sampleformat;
+  
+  WebPState* sp = EncoderState(tif);
+  assert(sp != NULL);
+
+  sp->nSamples = tif->tif_dir.td_samplesperpixel;
+
+  /* check band count */
+  if ( sp->nSamples != 3
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+    && sp->nSamples != 4
+#endif
+  )
+  {
+    TIFFErrorExt(tif->tif_clientdata, module,
+      "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+      "or 4 (RGBA) "
+#endif
+    "bands.",
+    sp->nSamples );
+    return 0;
+  }
+  
+  /* check bits per sample and data type */
+  if ((nBitsPerSample != 8) && (sampleFormat != 1)) {
+    TIFFErrorExt(tif->tif_clientdata, module,
+                "WEBP driver requires 8 bit unsigned data");
+    return 0;
+  }
+  
+  if (sp->state & LSTATE_INIT_DECODE) {
+    WebPIDelete(sp->psDecoder);
+    WebPFreeDecBuffer(&sp->sDecBuffer);
+    sp->psDecoder = NULL;
+    sp->last_y = 0;
+    sp->state = 0;
+  }
+
+  sp->state |= LSTATE_INIT_ENCODE;
+
+  if (!WebPConfigInitInternal(&sp->sEncoderConfig, WEBP_PRESET_DEFAULT,
+                              sp->quality_level,
+                              WEBP_ENCODER_ABI_VERSION)) {
+    TIFFErrorExt(tif->tif_clientdata, module,
+      "Error creating WebP encoder configuration.");
+    return 0;
+  }
+
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+  sp->sEncoderConfig.lossless = sp->lossless;
+#endif
+
+  if (!WebPValidateConfig(&sp->sEncoderConfig)) {
+    TIFFErrorExt(tif->tif_clientdata, module,
+      "Error with WebP encoder configuration.");
+    return 0;
+  }
+
+  if (!WebPPictureInit(&sp->sPicture)) {
+    TIFFErrorExt(tif->tif_clientdata, module,
+        "Error initializing WebP picture.");
+    return 0;
+  }
+
+  return 1;
+}
+
+/*
+* Reset encoding state at the start of a strip.
+*/
+static int
+TWebPPreEncode(TIFF* tif, uint16 s)
+{
+  static const char module[] = "TWebPPreEncode";
+  uint32 segment_width, segment_height;
+  WebPState *sp = EncoderState(tif);
+  TIFFDirectory* td = &tif->tif_dir;
+
+  (void) s;
+
+  assert(sp != NULL);
+  if( sp->state != LSTATE_INIT_ENCODE )
+    tif->tif_setupencode(tif);
+
+  /*
+   * Set encoding parameters for this strip/tile.
+   */
+  if (isTiled(tif)) {
+    segment_width = td->td_tilewidth;
+    segment_height = td->td_tilelength;
+  } else {
+    segment_width = td->td_imagewidth;
+    segment_height = td->td_imagelength - tif->tif_row;
+    if (segment_height > td->td_rowsperstrip)
+      segment_height = td->td_rowsperstrip;
+  }
+
+  if( segment_width > 16383 || segment_height > 16383 ) {
+      TIFFErrorExt(tif->tif_clientdata, module, 
+                   "WEBP maximum image dimensions are 16383 x 16383.");
+      return 0;
+  }
+
+  /* set up buffer for raw data */
+  /* given above check and that nSamples <= 4, buffer_size is <= 1 GB */
+  sp->buffer_size = segment_width * segment_height * sp->nSamples;
+  sp->pBuffer = _TIFFmalloc(sp->buffer_size);
+  if( !sp->pBuffer) {
+      TIFFErrorExt(tif->tif_clientdata, module, "Cannot allocate buffer");
+      return 0;
+  }
+  sp->buffer_offset = 0;
+
+  sp->sPicture.width = segment_width;
+  sp->sPicture.height = segment_height;
+  sp->sPicture.writer = TWebPDatasetWriter;
+  sp->sPicture.custom_ptr = tif;
+
+  return 1;
+}
+
+/*
+* Finish off an encoded strip by flushing it.
+*/
+static int
+TWebPPostEncode(TIFF* tif)
+{
+  static const char module[] = "WebPPostEncode";
+  int64_t stride;
+  WebPState *sp = EncoderState(tif);
+  assert(sp != NULL);
+
+  assert(sp->state == LSTATE_INIT_ENCODE);
+
+  stride = (int64_t)sp->sPicture.width * sp->nSamples;
+
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+  if (sp->nSamples == 4) {
+      if (!WebPPictureImportRGBA(&sp->sPicture, sp->pBuffer, (int)stride)) {
+          TIFFErrorExt(tif->tif_clientdata, module,
+                    "WebPPictureImportRGBA() failed" );
+          return 0;
+      }
+  }
+  else
+#endif
+  if (!WebPPictureImportRGB(&sp->sPicture, sp->pBuffer, (int)stride)) {
+      TIFFErrorExt(tif->tif_clientdata, module,
+                    "WebPPictureImportRGB() failed");
+      return 0;
+  }
+
+  if (!WebPEncode(&sp->sEncoderConfig, &sp->sPicture)) {
+
+#if WEBP_ENCODER_ABI_VERSION >= 0x0100
+    const char* pszErrorMsg = NULL;
+    switch(sp->sPicture.error_code) {
+    case VP8_ENC_ERROR_OUT_OF_MEMORY:
+        pszErrorMsg = "Out of memory"; break;
+    case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
+        pszErrorMsg = "Out of memory while flushing bits"; break;
+    case VP8_ENC_ERROR_NULL_PARAMETER:
+        pszErrorMsg = "A pointer parameter is NULL"; break;
+    case VP8_ENC_ERROR_INVALID_CONFIGURATION:
+        pszErrorMsg = "Configuration is invalid"; break;
+    case VP8_ENC_ERROR_BAD_DIMENSION:
+        pszErrorMsg = "Picture has invalid width/height"; break;
+    case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
+        pszErrorMsg = "Partition is bigger than 512k. Try using less "
+            "SEGMENTS, or increase PARTITION_LIMIT value";
+        break;
+    case VP8_ENC_ERROR_PARTITION_OVERFLOW:
+        pszErrorMsg = "Partition is bigger than 16M";
+        break;
+    case VP8_ENC_ERROR_BAD_WRITE:
+        pszErrorMsg = "Error while fludshing bytes"; break;
+    case VP8_ENC_ERROR_FILE_TOO_BIG:
+        pszErrorMsg = "File is bigger than 4G"; break;
+    case VP8_ENC_ERROR_USER_ABORT:
+        pszErrorMsg = "User interrupted";
+        break;
+    default:
+        TIFFErrorExt(tif->tif_clientdata, module,
+                "WebPEncode returned an unknown error code: %d",
+                sp->sPicture.error_code);
+        pszErrorMsg = "Unknown WebP error type.";
+        break;
+    }
+    TIFFErrorExt(tif->tif_clientdata, module,
+             "WebPEncode() failed : %s", pszErrorMsg);
+#else
+    TIFFErrorExt(tif->tif_clientdata, module,
+             "Error in WebPEncode()");
+#endif
+    return 0;
+  }
+
+  sp->sPicture.custom_ptr = NULL;
+
+  if (!TIFFFlushData1(tif))
+  {
+    TIFFErrorExt(tif->tif_clientdata, module,
+      "Error flushing TIFF WebP encoder.");
+    return 0;
+  }
+
+  return 1;
+}
+
+static void
+TWebPCleanup(TIFF* tif)
+{
+  WebPState* sp = LState(tif);
+
+  assert(sp != 0);
+
+  tif->tif_tagmethods.vgetfield = sp->vgetparent;
+  tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+  if (sp->state & LSTATE_INIT_ENCODE) {
+    WebPPictureFree(&sp->sPicture);
+  }
+
+  if (sp->psDecoder != NULL) {
+    WebPIDelete(sp->psDecoder);
+    WebPFreeDecBuffer(&sp->sDecBuffer);
+    sp->psDecoder = NULL;
+    sp->last_y = 0;
+  }
+  
+  if (sp->pBuffer != NULL) {
+    _TIFFfree(sp->pBuffer);
+    sp->pBuffer = NULL;
+  }
+
+  if (tif->tif_data) {
+    _TIFFfree(tif->tif_data);
+    tif->tif_data = NULL;
+  }
+  
+  _TIFFSetDefaultCompressionState(tif);
+}
+
+static int
+TWebPVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+       static const char module[] = "WebPVSetField";
+  WebPState* sp = LState(tif);
+
+  switch (tag) {
+  case TIFFTAG_WEBP_LEVEL:
+    sp->quality_level = (int) va_arg(ap, int);
+    if( sp->quality_level <= 0 ||
+        sp->quality_level > 100.0f ) {
+      TIFFWarningExt(tif->tif_clientdata, module,
+                     "WEBP_LEVEL should be between 1 and 100");
+    }
+    return 1;
+  case TIFFTAG_WEBP_LOSSLESS:
+    #if WEBP_ENCODER_ABI_VERSION >= 0x0100
+    sp->lossless = va_arg(ap, int);
+    return 1;
+    #else
+      TIFFErrorExt(tif->tif_clientdata, module,
+                  "Need to upgrade WEBP driver, this version doesn't support "
+                  "lossless compression.");
+      return 0;
+    #endif 
+  default:
+    return (*sp->vsetparent)(tif, tag, ap);
+  }
+  /*NOTREACHED*/
+}
+
+static int
+TWebPVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+  WebPState* sp = LState(tif);
+
+  switch (tag) {
+  case TIFFTAG_WEBP_LEVEL:
+    *va_arg(ap, int*) = sp->quality_level;
+    break;
+  case TIFFTAG_WEBP_LOSSLESS:
+    *va_arg(ap, int*) = sp->lossless;
+    break;
+  default:
+    return (*sp->vgetparent)(tif, tag, ap);
+  }
+  return 1;
+}
+
+static const TIFFField TWebPFields[] = {
+  { TIFFTAG_WEBP_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
+    TIFF_SETGET_UNDEFINED,
+    FIELD_PSEUDO, TRUE, FALSE, "WEBP quality", NULL },
+  { TIFFTAG_WEBP_LOSSLESS, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
+    TIFF_SETGET_UNDEFINED,
+    FIELD_PSEUDO, TRUE, FALSE, "WEBP lossless/lossy", NULL
+  },
+};
+
+int
+TIFFInitWebP(TIFF* tif, int scheme)
+{
+  static const char module[] = "TIFFInitWebP";
+  WebPState* sp;
+
+  assert( scheme == COMPRESSION_WEBP );
+
+  /*
+  * Merge codec-specific tag information.
+  */
+  if ( !_TIFFMergeFields(tif, TWebPFields, TIFFArrayCount(TWebPFields)) ) {
+    TIFFErrorExt(tif->tif_clientdata, module,
+                "Merging WebP codec-specific tags failed");
+    return 0;
+  }
+
+  /*
+  * Allocate state block so tag methods have storage to record values.
+  */
+  tif->tif_data = (uint8*) _TIFFmalloc(sizeof(WebPState));
+  if (tif->tif_data == NULL)
+    goto bad;
+  sp = LState(tif);
+
+  /*
+  * Override parent get/set field methods.
+  */
+  sp->vgetparent = tif->tif_tagmethods.vgetfield;
+  tif->tif_tagmethods.vgetfield = TWebPVGetField;      /* hook for codec tags 
*/
+  sp->vsetparent = tif->tif_tagmethods.vsetfield;
+  tif->tif_tagmethods.vsetfield = TWebPVSetField;      /* hook for codec tags 
*/
+
+  /* Default values for codec-specific fields */
+  sp->quality_level = 75.0f;           /* default comp. level */
+  sp->lossless = 0; /* default to false */
+  sp->state = 0;
+  sp->nSamples = 0;
+  sp->psDecoder = NULL;
+  sp->last_y = 0;
+  
+  sp->buffer_offset = 0;
+  sp->pBuffer = NULL;
+
+  /*
+  * Install codec methods.
+  * Notes:
+  * encoderow is not supported
+  */
+  tif->tif_fixuptags = TWebPFixupTags;
+  tif->tif_setupdecode = TWebPSetupDecode;
+  tif->tif_predecode = TWebPPreDecode;
+  tif->tif_decoderow = TWebPDecode;
+  tif->tif_decodestrip = TWebPDecode;
+  tif->tif_decodetile = TWebPDecode;
+  tif->tif_setupencode = TWebPSetupEncode;
+  tif->tif_preencode = TWebPPreEncode;
+  tif->tif_postencode = TWebPPostEncode;
+  tif->tif_encoderow = TWebPEncode;
+  tif->tif_encodestrip = TWebPEncode;
+  tif->tif_encodetile = TWebPEncode;
+  tif->tif_cleanup = TWebPCleanup;
+
+  return 1;
+bad:
+  TIFFErrorExt(tif->tif_clientdata, module,
+            "No space for WebP state block");
+  return 0;
+}
+
+#endif /* WEBP_SUPPORT */
diff --git a/dll/3rdparty/libtiff/tif_win32.c b/dll/3rdparty/libtiff/tif_win32.c
index a3ada36db32..d2dd8228ae8 100644
--- a/dll/3rdparty/libtiff/tif_win32.c
+++ b/dll/3rdparty/libtiff/tif_win32.c
@@ -1,5 +1,3 @@
-/* $Id: tif_win32.c,v 1.42 2017-01-11 19:02:49 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -411,60 +409,21 @@ _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
 static void
 Win32WarningHandler(const char* module, const char* fmt, va_list ap)
 {
-#ifndef TIF_PLATFORM_CONSOLE
-       LPTSTR szTitle;
-       LPTSTR szTmp;
-       LPCTSTR szTitleText = "%s Warning";
-       LPCTSTR szDefaultModule = "LIBTIFF";
-       LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
-        SIZE_T nBufSize = (strlen(szTmpModule) +
-                        strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
-
-       if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
-               return;
-       sprintf(szTitle, szTitleText, szTmpModule);
-       szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
-       vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
-       MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
-       LocalFree(szTitle);
-
-       return;
-#else
        if (module != NULL)
                fprintf(stderr, "%s: ", module);
        fprintf(stderr, "Warning, ");
        vfprintf(stderr, fmt, ap);
        fprintf(stderr, ".\n");
-#endif        
 }
 TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
 
 static void
 Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
 {
-#ifndef TIF_PLATFORM_CONSOLE
-       LPTSTR szTitle;
-       LPTSTR szTmp;
-       LPCTSTR szTitleText = "%s Error";
-       LPCTSTR szDefaultModule = "LIBTIFF";
-       LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
-        SIZE_T nBufSize = (strlen(szTmpModule) +
-                        strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
-
-       if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
-               return;
-       sprintf(szTitle, szTitleText, szTmpModule);
-       szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
-       vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
-       MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
-       LocalFree(szTitle);
-       return;
-#else
        if (module != NULL)
                fprintf(stderr, "%s: ", module);
        vfprintf(stderr, fmt, ap);
        fprintf(stderr, ".\n");
-#endif        
 }
 TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
 
diff --git a/dll/3rdparty/libtiff/tif_write.c b/dll/3rdparty/libtiff/tif_write.c
index d0071e2f632..32603fe5636 100644
--- a/dll/3rdparty/libtiff/tif_write.c
+++ b/dll/3rdparty/libtiff/tif_write.c
@@ -1,5 +1,3 @@
-/* $Id: tif_write.c,v 1.46 2016-12-03 21:57:44 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -541,9 +539,11 @@ TIFFSetupStrips(TIFF* tif)
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
                td->td_stripsperimage /= td->td_samplesperpixel;
        td->td_stripoffset = (uint64 *)
-           _TIFFmalloc(td->td_nstrips * sizeof (uint64));
+            _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
+                             "for \"StripOffsets\" array");
        td->td_stripbytecount = (uint64 *)
-           _TIFFmalloc(td->td_nstrips * sizeof (uint64));
+            _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
+                             "for \"StripByteCounts\" array");
        if (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)
                return (0);
        /*
diff --git a/dll/3rdparty/libtiff/tif_zip.c b/dll/3rdparty/libtiff/tif_zip.c
index 7ab02124c44..6dc64b2d034 100644
--- a/dll/3rdparty/libtiff/tif_zip.c
+++ b/dll/3rdparty/libtiff/tif_zip.c
@@ -1,5 +1,3 @@
-/* $Id: tif_zip.c,v 1.37 2017-05-10 15:21:16 erouault Exp $ */
-
 /*
  * Copyright (c) 1995-1997 Sam Leffler
  * Copyright (c) 1995-1997 Silicon Graphics, Inc.
diff --git a/dll/3rdparty/libtiff/tif_zstd.c b/dll/3rdparty/libtiff/tif_zstd.c
new file mode 100644
index 00000000000..21c935e2dad
--- /dev/null
+++ b/dll/3rdparty/libtiff/tif_zstd.c
@@ -0,0 +1,440 @@
+/*
+* Copyright (c) 2017, Planet Labs
+* Author: <even.rouault at spatialys.com>
+*
+* 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.
+*
+* 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
+* OF THIS SOFTWARE.
+*/
+
+#include "tiffiop.h"
+#ifdef ZSTD_SUPPORT
+/*
+* TIFF Library.
+*
+* ZSTD Compression Support
+*
+*/
+
+#include "tif_predict.h"
+#include "zstd.h"
+
+#include <stdio.h>
+
+/*
+* State block for each open TIFF file using ZSTD compression/decompression.
+*/
+typedef struct {
+        TIFFPredictorState predict;
+        ZSTD_DStream*   dstream;
+        ZSTD_CStream*   cstream;
+        int             compression_level;      /* compression level */
+        ZSTD_outBuffer  out_buffer;
+        int             state;                  /* state flags */
+#define LSTATE_INIT_DECODE 0x01
+#define LSTATE_INIT_ENCODE 0x02
+
+        TIFFVGetMethod  vgetparent;            /* super-class method */
+        TIFFVSetMethod  vsetparent;            /* super-class method */
+} ZSTDState;
+
+#define LState(tif)             ((ZSTDState*) (tif)->tif_data)
+#define DecoderState(tif)       LState(tif)
+#define EncoderState(tif)       LState(tif)
+
+static int ZSTDEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int ZSTDDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
+
+static int
+ZSTDFixupTags(TIFF* tif)
+{
+        (void) tif;
+        return 1;
+}
+
+static int
+ZSTDSetupDecode(TIFF* tif)
+{
+        ZSTDState* sp = DecoderState(tif);
+
+        assert(sp != NULL);
+
+        /* if we were last encoding, terminate this mode */
+        if (sp->state & LSTATE_INIT_ENCODE) {
+            ZSTD_freeCStream(sp->cstream);
+            sp->cstream = NULL;
+            sp->state = 0;
+        }
+
+        sp->state |= LSTATE_INIT_DECODE;
+        return 1;
+}
+
+/*
+* Setup state for decoding a strip.
+*/
+static int
+ZSTDPreDecode(TIFF* tif, uint16 s)
+{
+        static const char module[] = "ZSTDPreDecode";
+        ZSTDState* sp = DecoderState(tif);
+        size_t zstd_ret;
+
+        (void) s;
+        assert(sp != NULL);
+
+        if( (sp->state & LSTATE_INIT_DECODE) == 0 )
+            tif->tif_setupdecode(tif);
+
+        if( sp->dstream )
+        {
+            ZSTD_freeDStream(sp->dstream);
+            sp->dstream = NULL;
+        }
+
+        sp->dstream = ZSTD_createDStream();
+        if( sp->dstream == NULL ) {
+            TIFFErrorExt(tif->tif_clientdata, module,
+                         "Cannot allocate decompression stream");
+            return 0;
+        }
+        zstd_ret = ZSTD_initDStream(sp->dstream);
+        if( ZSTD_isError(zstd_ret) ) {
+            TIFFErrorExt(tif->tif_clientdata, module,
+                         "Error in ZSTD_initDStream(): %s",
+                         ZSTD_getErrorName(zstd_ret));
+            return 0;
+        }
+
+        return 1;
+}
+
+static int
+ZSTDDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
+{
+        static const char module[] = "ZSTDDecode";
+        ZSTDState* sp = DecoderState(tif);
+        ZSTD_inBuffer   in_buffer;
+        ZSTD_outBuffer  out_buffer;
+        size_t          zstd_ret;
+
+        (void) s;
+        assert(sp != NULL);
+        assert(sp->state == LSTATE_INIT_DECODE);
+
+        in_buffer.src = tif->tif_rawcp;
+        in_buffer.size = (size_t) tif->tif_rawcc;
+        in_buffer.pos = 0;
+
+        out_buffer.dst = op;
+        out_buffer.size = (size_t) occ;
+        out_buffer.pos = 0;
+
+        do {
+                zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer,
+                                                 &in_buffer);
+                if( ZSTD_isError(zstd_ret) ) {
+                    TIFFErrorExt(tif->tif_clientdata, module,
+                                "Error in ZSTD_decompressStream(): %s",
+                                ZSTD_getErrorName(zstd_ret));
+                    return 0;
+                }
+        } while( zstd_ret != 0 &&
+                 in_buffer.pos < in_buffer.size &&
+                 out_buffer.pos < out_buffer.size );
+
+        if (out_buffer.pos < (size_t)occ) {
+                TIFFErrorExt(tif->tif_clientdata, module,
+                    "Not enough data at scanline %lu (short %lu bytes)",
+                    (unsigned long) tif->tif_row,
+                    (unsigned long) (size_t)occ - out_buffer.pos);
+                return 0;
+        }
+
+        tif->tif_rawcp += in_buffer.pos;
+        tif->tif_rawcc -= in_buffer.pos;
+
+        return 1;
+}
+
+static int
+ZSTDSetupEncode(TIFF* tif)
+{
+        ZSTDState* sp = EncoderState(tif);
+
+        assert(sp != NULL);
+        if (sp->state & LSTATE_INIT_DECODE) {
+                ZSTD_freeDStream(sp->dstream);
+                sp->dstream = NULL;
+                sp->state = 0;
+        }
+
+        sp->state |= LSTATE_INIT_ENCODE;
+        return 1;
+}
+
+/*
+* Reset encoding state at the start of a strip.
+*/
+static int
+ZSTDPreEncode(TIFF* tif, uint16 s)
+{
+        static const char module[] = "ZSTDPreEncode";
+        ZSTDState *sp = EncoderState(tif);
+        size_t zstd_ret;
+
+        (void) s;
+        assert(sp != NULL);
+        if( sp->state != LSTATE_INIT_ENCODE )
+            tif->tif_setupencode(tif);
+
+        if (sp->cstream) {
+            ZSTD_freeCStream(sp->cstream);
+            sp->cstream = NULL;
+        }
+        sp->cstream = ZSTD_createCStream();
+        if( sp->cstream == NULL ) {
+            TIFFErrorExt(tif->tif_clientdata, module,
+                         "Cannot allocate compression stream");
+            return 0;
+        }
+
+        zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level);
+        if( ZSTD_isError(zstd_ret) ) {
+            TIFFErrorExt(tif->tif_clientdata, module,
+                         "Error in ZSTD_initCStream(): %s",
+                         ZSTD_getErrorName(zstd_ret));
+            return 0;
+        }
+
+        sp->out_buffer.dst = tif->tif_rawdata;
+        sp->out_buffer.size = (size_t)tif->tif_rawdatasize;
+        sp->out_buffer.pos = 0;
+
+        return 1;
+}
+
+/*
+* Encode a chunk of pixels.
+*/
+static int
+ZSTDEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
+{
+        static const char module[] = "ZSTDEncode";
+        ZSTDState *sp = EncoderState(tif);
+        ZSTD_inBuffer   in_buffer;
+        size_t          zstd_ret;
+
+        assert(sp != NULL);
+        assert(sp->state == LSTATE_INIT_ENCODE);
+
+        (void) s;
+
+        in_buffer.src = bp;
+        in_buffer.size = (size_t)cc;
+        in_buffer.pos = 0;
+
+        do {
+                zstd_ret = ZSTD_compressStream(sp->cstream, &sp->out_buffer,
+                                               &in_buffer);
+                if( ZSTD_isError(zstd_ret) ) {
+                    TIFFErrorExt(tif->tif_clientdata, module,
+                                "Error in ZSTD_compressStream(): %s",
+                                ZSTD_getErrorName(zstd_ret));
+                    return 0;
+                }
+                if( sp->out_buffer.pos == sp->out_buffer.size ) {
+                        tif->tif_rawcc = tif->tif_rawdatasize;
+                        TIFFFlushData1(tif);
+                        sp->out_buffer.dst = tif->tif_rawcp;
+                        sp->out_buffer.pos = 0;
+                }
+        } while( in_buffer.pos < in_buffer.size );
+
+        return 1;
+}
+
+/*
+* Finish off an encoded strip by flushing it.
+*/
+static int
+ZSTDPostEncode(TIFF* tif)
+{
+        static const char module[] = "ZSTDPostEncode";
+        ZSTDState *sp = EncoderState(tif);
+        size_t          zstd_ret;
+
+        do {
+                zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer);
+                if( ZSTD_isError(zstd_ret) ) {
+                    TIFFErrorExt(tif->tif_clientdata, module,
+                                "Error in ZSTD_endStream(): %s",
+                                ZSTD_getErrorName(zstd_ret));
+                    return 0;
+                }
+                if( sp->out_buffer.pos > 0 ) {
+                        tif->tif_rawcc = sp->out_buffer.pos;
+                        TIFFFlushData1(tif);
+                        sp->out_buffer.dst = tif->tif_rawcp;
+                        sp->out_buffer.pos = 0;
+                }
+        } while (zstd_ret != 0);
+        return 1;
+}
+
+static void
+ZSTDCleanup(TIFF* tif)
+{
+        ZSTDState* sp = LState(tif);
+
+        assert(sp != 0);
+
+        (void)TIFFPredictorCleanup(tif);
+
+        tif->tif_tagmethods.vgetfield = sp->vgetparent;
+        tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+        if (sp->dstream) {
+            ZSTD_freeDStream(sp->dstream);
+            sp->dstream = NULL;
+        }
+        if (sp->cstream) {
+            ZSTD_freeCStream(sp->cstream);
+            sp->cstream = NULL;
+        }
+        _TIFFfree(sp);
+        tif->tif_data = NULL;
+
+        _TIFFSetDefaultCompressionState(tif);
+}
+
+static int
+ZSTDVSetField(TIFF* tif, uint32 tag, va_list ap)
+{
+       static const char module[] = "ZSTDVSetField";
+        ZSTDState* sp = LState(tif);
+
+        switch (tag) {
+        case TIFFTAG_ZSTD_LEVEL:
+                sp->compression_level = (int) va_arg(ap, int);
+                if( sp->compression_level <= 0 ||
+                    sp->compression_level > ZSTD_maxCLevel() )
+                {
+                    TIFFWarningExt(tif->tif_clientdata, module,
+                                   "ZSTD_LEVEL should be between 1 and %d",
+                                   ZSTD_maxCLevel());
+                }
+                return 1;
+        default:
+                return (*sp->vsetparent)(tif, tag, ap);
+        }
+        /*NOTREACHED*/
+}
+
+static int
+ZSTDVGetField(TIFF* tif, uint32 tag, va_list ap)
+{
+        ZSTDState* sp = LState(tif);
+
+        switch (tag) {
+        case TIFFTAG_ZSTD_LEVEL:
+                *va_arg(ap, int*) = sp->compression_level;
+                break;
+        default:
+                return (*sp->vgetparent)(tif, tag, ap);
+        }
+        return 1;
+}
+
+static const TIFFField ZSTDFields[] = {
+        { TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
+          TIFF_SETGET_UNDEFINED,
+          FIELD_PSEUDO, TRUE, FALSE, "ZSTD compression_level", NULL },
+};
+
+int
+TIFFInitZSTD(TIFF* tif, int scheme)
+{
+        static const char module[] = "TIFFInitZSTD";
+        ZSTDState* sp;
+
+        assert( scheme == COMPRESSION_ZSTD );
+
+        /*
+        * Merge codec-specific tag information.
+        */
+        if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields))) {
+                TIFFErrorExt(tif->tif_clientdata, module,
+                            "Merging ZSTD codec-specific tags failed");
+                return 0;
+        }
+
+        /*
+        * Allocate state block so tag methods have storage to record values.
+        */
+        tif->tif_data = (uint8*) _TIFFmalloc(sizeof(ZSTDState));
+        if (tif->tif_data == NULL)
+                goto bad;
+        sp = LState(tif);
+
+        /*
+        * Override parent get/set field methods.
+        */
+        sp->vgetparent = tif->tif_tagmethods.vgetfield;
+        tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags 
*/
+        sp->vsetparent = tif->tif_tagmethods.vsetfield;
+        tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags 
*/
+
+        /* Default values for codec-specific fields */
+        sp->compression_level = 9;             /* default comp. level */
+        sp->state = 0;
+        sp->dstream = 0;
+        sp->cstream = 0;
+        sp->out_buffer.dst = NULL;
+        sp->out_buffer.size = 0;
+        sp->out_buffer.pos = 0;
+
+        /*
+        * Install codec methods.
+        */
+        tif->tif_fixuptags = ZSTDFixupTags;
+        tif->tif_setupdecode = ZSTDSetupDecode;
+        tif->tif_predecode = ZSTDPreDecode;
+        tif->tif_decoderow = ZSTDDecode;
+        tif->tif_decodestrip = ZSTDDecode;
+        tif->tif_decodetile = ZSTDDecode;
+        tif->tif_setupencode = ZSTDSetupEncode;
+        tif->tif_preencode = ZSTDPreEncode;
+        tif->tif_postencode = ZSTDPostEncode;
+        tif->tif_encoderow = ZSTDEncode;
+        tif->tif_encodestrip = ZSTDEncode;
+        tif->tif_encodetile = ZSTDEncode;
+        tif->tif_cleanup = ZSTDCleanup;
+        /*
+        * Setup predictor setup.
+        */
+        (void) TIFFPredictorInit(tif);
+        return 1;
+bad:
+        TIFFErrorExt(tif->tif_clientdata, module,
+                    "No space for ZSTD state block");
+        return 0;
+}
+#endif /* ZSTD_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
diff --git a/media/doc/3rd Party Files.txt b/media/doc/3rd Party Files.txt
index 8561d828029..037b9813870 100644
--- a/media/doc/3rd Party Files.txt     
+++ b/media/doc/3rd Party Files.txt     
@@ -83,7 +83,7 @@ Used Version: 9c
 Website: http://www.ijg.org/
 
 Title: libtiff
-Used Version: 4.0.9
+Used Version: 4.0.10
 Website: http://www.simplesystems.org/libtiff/
 
 Title: mbed TLS
diff --git a/sdk/include/reactos/libs/libtiff/t4.h 
b/sdk/include/reactos/libs/libtiff/t4.h
index b908f54f094..fb0951a16f7 100644
--- a/sdk/include/reactos/libs/libtiff/t4.h
+++ b/sdk/include/reactos/libs/libtiff/t4.h
@@ -1,5 +1,3 @@
-/* $Id: t4.h,v 1.3 2010-03-10 18:56:48 bfriesen Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/sdk/include/reactos/libs/libtiff/tif_config.wince.h 
b/sdk/include/reactos/libs/libtiff/tif_config.wince.h
index 94e92bbf946..e85e2e62cde 100644
--- a/sdk/include/reactos/libs/libtiff/tif_config.wince.h
+++ b/sdk/include/reactos/libs/libtiff/tif_config.wince.h
@@ -1,5 +1,3 @@
-/* $Id: tif_config.wince.h,v 1.3 2010-03-10 18:56:48 bfriesen Exp $ */
-
 /*
  * TIFF library configuration header for Windows CE platform.
  */
diff --git a/sdk/include/reactos/libs/libtiff/tif_dir.h 
b/sdk/include/reactos/libs/libtiff/tif_dir.h
index 5a38076733d..b2f5e694883 100644
--- a/sdk/include/reactos/libs/libtiff/tif_dir.h
+++ b/sdk/include/reactos/libs/libtiff/tif_dir.h
@@ -1,5 +1,3 @@
-/* $Id: tif_dir.h,v 1.55 2017-06-01 12:44:04 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -26,6 +24,10 @@
 
 #ifndef _TIFFDIR_
 #define        _TIFFDIR_
+
+#include "tiff.h"
+#include "tiffio.h"
+
 /*
  * ``Library-private'' Directory-related Definitions.
  */
diff --git a/sdk/include/reactos/libs/libtiff/tif_fax3.h 
b/sdk/include/reactos/libs/libtiff/tif_fax3.h
index 8a435059c7b..abadcd97a20 100644
--- a/sdk/include/reactos/libs/libtiff/tif_fax3.h
+++ b/sdk/include/reactos/libs/libtiff/tif_fax3.h
@@ -1,5 +1,3 @@
-/* $Id: tif_fax3.h,v 1.13 2016-12-14 18:36:27 faxguy Exp $ */
-
 /*
  * Copyright (c) 1990-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
diff --git a/sdk/include/reactos/libs/libtiff/tif_predict.h 
b/sdk/include/reactos/libs/libtiff/tif_predict.h
index 6c68e21acbb..a326b9b8f86 100644
--- a/sdk/include/reactos/libs/libtiff/tif_predict.h
+++ b/sdk/include/reactos/libs/libtiff/tif_predict.h
@@ -1,5 +1,3 @@
-/* $Id: tif_predict.h,v 1.9 2016-10-31 17:24:26 erouault Exp $ */
-
 /*
  * Copyright (c) 1995-1997 Sam Leffler
  * Copyright (c) 1995-1997 Silicon Graphics, Inc.
@@ -26,6 +24,10 @@
 
 #ifndef _TIFFPREDICT_
 #define        _TIFFPREDICT_
+
+#include "tiffio.h"
+#include "tiffiop.h"
+
 /*
  * ``Library-private'' Support for the Predictor Tag
  */
diff --git a/sdk/include/reactos/libs/libtiff/tiff.h 
b/sdk/include/reactos/libs/libtiff/tiff.h
index fb39634c4b6..5b0a0c90f67 100644
--- a/sdk/include/reactos/libs/libtiff/tiff.h
+++ b/sdk/include/reactos/libs/libtiff/tiff.h
@@ -1,5 +1,3 @@
-/* $Id: tiff.h,v 1.70 2016-01-23 21:20:34 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -189,7 +187,11 @@ typedef enum {
 #define     COMPRESSION_SGILOG         34676   /* SGI Log Luminance RLE */
 #define     COMPRESSION_SGILOG24       34677   /* SGI Log 24-bit packed */
 #define     COMPRESSION_JP2000          34712   /* Leadtools JPEG2000 */
+#define     COMPRESSION_LERC            34887   /* ESRI Lerc codec: 
https://github.com/Esri/lerc */
+/* compression codes 34887-34889 are reserved for ESRI */
 #define            COMPRESSION_LZMA            34925   /* LZMA2 */
+#define            COMPRESSION_ZSTD            50000   /* ZSTD: WARNING not 
registered in Adobe-maintained registry */
+#define            COMPRESSION_WEBP            50001   /* WEBP: WARNING not 
registered in Adobe-maintained registry */
 #define        TIFFTAG_PHOTOMETRIC             262     /* photometric 
interpretation */
 #define            PHOTOMETRIC_MINISWHITE      0       /* min value is white */
 #define            PHOTOMETRIC_MINISBLACK      1       /* min value is black */
@@ -450,6 +452,8 @@ typedef enum {
 /* tag 34929 is a private tag registered to FedEx */
 #define        TIFFTAG_FEDEX_EDR               34929   /* unknown use */
 #define TIFFTAG_INTEROPERABILITYIFD    40965   /* Pointer to Interoperability 
private directory */
+/* tags 50674 to 50677 are reserved for ESRI */
+#define TIFFTAG_LERC_PARAMETERS         50674   /* Stores LERC version and 
additional compression method */
 /* Adobe Digital Negative (DNG) format tags */
 #define TIFFTAG_DNGVERSION             50706   /* &DNG version number */
 #define TIFFTAG_DNGBACKWARDVERSION     50707   /* &DNG compatibility version */
@@ -603,6 +607,16 @@ typedef enum {
 #define TIFFTAG_PERSAMPLE       65563  /* interface for per sample tags */
 #define     PERSAMPLE_MERGED        0  /* present as a single value */
 #define     PERSAMPLE_MULTI         1  /* present as multiple values */
+#define TIFFTAG_ZSTD_LEVEL      65564    /* ZSTD compression level */
+#define TIFFTAG_LERC_VERSION            65565 /* LERC version */
+#define     LERC_VERSION_2_4            4
+#define TIFFTAG_LERC_ADD_COMPRESSION    65566 /* LERC additional compression */
+#define     LERC_ADD_COMPRESSION_NONE    0
+#define     LERC_ADD_COMPRESSION_DEFLATE 1
+#define     LERC_ADD_COMPRESSION_ZSTD    2
+#define TIFFTAG_LERC_MAXZERROR          65567    /* LERC maximum error */
+#define TIFFTAG_WEBP_LEVEL               65568 /* WebP compression level: 
WARNING not registered in Adobe-maintained registry */
+#define TIFFTAG_WEBP_LOSSLESS          65569   /* WebP lossless/lossy : 
WARNING not registered in Adobe-maintained registry */
 
 /*
  * EXIF tags
diff --git a/sdk/include/reactos/libs/libtiff/tiffconf.h 
b/sdk/include/reactos/libs/libtiff/tiffconf.h
index 799a47a5450..ce08d8a2032 100644
--- a/sdk/include/reactos/libs/libtiff/tiffconf.h
+++ b/sdk/include/reactos/libs/libtiff/tiffconf.h
@@ -7,15 +7,6 @@
 #ifndef _TIFFCONF_
 #define _TIFFCONF_
 
-/* Define to 1 if the system has the type `int16'. */
-/* #undef HAVE_INT16 */
-
-/* Define to 1 if the system has the type `int32'. */
-/* #undef HAVE_INT32 */
-
-/* Define to 1 if the system has the type `int8'. */
-/* #undef HAVE_INT8 */
-
 /* The size of a `int', as computed by sizeof. */
 #define SIZEOF_INT 4
 
diff --git a/sdk/include/reactos/libs/libtiff/tiffconf.vc.h 
b/sdk/include/reactos/libs/libtiff/tiffconf.vc.h
index c8c6c6566d8..fb37a755ff1 100644
--- a/sdk/include/reactos/libs/libtiff/tiffconf.vc.h
+++ b/sdk/include/reactos/libs/libtiff/tiffconf.vc.h
@@ -7,15 +7,6 @@
 #ifndef _TIFFCONF_
 #define _TIFFCONF_
 
-/* Define to 1 if the system has the type `int16'. */
-/* #undef HAVE_INT16 */
-
-/* Define to 1 if the system has the type `int32'. */
-/* #undef HAVE_INT32 */
-
-/* Define to 1 if the system has the type `int8'. */
-/* #undef HAVE_INT8 */
-
 /* The size of a `int', as computed by sizeof. */
 #define SIZEOF_INT 4
 
diff --git a/sdk/include/reactos/libs/libtiff/tiffconf.wince.h 
b/sdk/include/reactos/libs/libtiff/tiffconf.wince.h
index 2ef3958a075..013b0960dc0 100644
--- a/sdk/include/reactos/libs/libtiff/tiffconf.wince.h
+++ b/sdk/include/reactos/libs/libtiff/tiffconf.wince.h
@@ -1,5 +1,3 @@
-/* $Id: tiffconf.wince.h,v 1.3 2010-03-10 18:56:49 bfriesen Exp $ */
-
 /*
  * Windows CE platform tiffconf.wince.h
  * Created by Mateusz Loskot ([email protected])
@@ -27,15 +25,6 @@
 #ifndef _TIFFCONF_
 #define _TIFFCONF_
 
-/* Define to 1 if the system has the type `int16'. */
-/* #undef HAVE_INT16 */
-
-/* Define to 1 if the system has the type `int32'. */
-/* #undef HAVE_INT32 */
-
-/* Define to 1 if the system has the type `int8'. */
-/* #undef HAVE_INT8 */
-
 /* The size of a `int', as computed by sizeof. */
 #define SIZEOF_INT 4
 
diff --git a/sdk/include/reactos/libs/libtiff/tiffio.h 
b/sdk/include/reactos/libs/libtiff/tiffio.h
index c7eff6dbfd7..e8065495f12 100644
--- a/sdk/include/reactos/libs/libtiff/tiffio.h
+++ b/sdk/include/reactos/libs/libtiff/tiffio.h
@@ -1,5 +1,3 @@
-/* $Id: tiffio.h,v 1.94 2017-01-11 19:02:49 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -52,7 +50,7 @@ typedef struct tiff TIFF;
  *     promoted type (i.e. one of int, unsigned int, pointer,
  *     or double) and because we defined pseudo-tags that are
  *     outside the range of legal Aldus-assigned tags.
- * NB: tsize_t is int32 and not uint32 because some functions
+ * NB: tsize_t is signed and not unsigned because some functions
  *     return -1.
  * NB: toff_t is not off_t for many reasons; TIFFs max out at
  *     32-bit file offsets, and BigTIFF maxes out at 64-bit
diff --git a/sdk/include/reactos/libs/libtiff/tiffio.hxx 
b/sdk/include/reactos/libs/libtiff/tiffio.hxx
index ed994f11565..df2cbbceb73 100644
--- a/sdk/include/reactos/libs/libtiff/tiffio.hxx
+++ b/sdk/include/reactos/libs/libtiff/tiffio.hxx
@@ -1,5 +1,3 @@
-/* $Id: tiffio.hxx,v 1.3 2010-06-08 18:55:15 bfriesen Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -33,6 +31,7 @@
 
 #include <iostream>
 #include "tiff.h"
+#include "tiffio.h"
 
 extern TIFF* TIFFStreamOpen(const char*, std::ostream *);
 extern TIFF* TIFFStreamOpen(const char*, std::istream *);
diff --git a/sdk/include/reactos/libs/libtiff/tiffiop.h 
b/sdk/include/reactos/libs/libtiff/tiffiop.h
index daa291c0830..186c291f5d7 100644
--- a/sdk/include/reactos/libs/libtiff/tiffiop.h
+++ b/sdk/include/reactos/libs/libtiff/tiffiop.h
@@ -1,5 +1,3 @@
-/* $Id: tiffiop.h,v 1.95 2017-09-07 14:02:52 erouault Exp $ */
-
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
@@ -72,6 +70,7 @@ extern int snprintf(char* str, size_t size, const char* 
format, ...);
 #endif
 
 #define    streq(a,b)      (strcmp(a,b) == 0)
+#define    strneq(a,b,n)   (strncmp(a,b,n) == 0)
 
 #ifndef TRUE
 #define        TRUE    1
@@ -314,11 +313,15 @@ typedef size_t TIFFIOSize_t;
 #define _TIFF_off_t off_t
 #endif
 
-#if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
+#if defined(__has_attribute) && defined(__clang__)
+#if __has_attribute(no_sanitize)
 #define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW 
__attribute__((no_sanitize("unsigned-integer-overflow")))
 #else
 #define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
 #endif
+#else
+#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
+#endif
 
 
 #if defined(__cplusplus)
@@ -424,6 +427,12 @@ extern int TIFFInitSGILog(TIFF*, int);
 #ifdef LZMA_SUPPORT
 extern int TIFFInitLZMA(TIFF*, int);
 #endif
+#ifdef ZSTD_SUPPORT
+extern int TIFFInitZSTD(TIFF*, int);
+#endif
+#ifdef WEBP_SUPPORT
+extern int TIFFInitWebP(TIFF*, int);
+#endif
 #ifdef VMS
 extern const TIFFCodec _TIFFBuiltinCODECS[];
 #else
diff --git a/sdk/include/reactos/libs/libtiff/tiffvers.h 
b/sdk/include/reactos/libs/libtiff/tiffvers.h
index 7c415740f65..403d61be04b 100644
--- a/sdk/include/reactos/libs/libtiff/tiffvers.h
+++ b/sdk/include/reactos/libs/libtiff/tiffvers.h
@@ -1,4 +1,4 @@
-#define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.0.9\nCopyright (c) 1988-1996 
Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
+#define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.0.10\nCopyright (c) 1988-1996 
Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
 /*
  * This define can be used in code that requires
  * compilation-related definitions specific to a
@@ -6,4 +6,4 @@
  * version checking should be done based on the
  * string returned by TIFFGetVersion.
  */
-#define TIFFLIB_VERSION 20171118
+#define TIFFLIB_VERSION 20181110

Reply via email to