Avoid setting the private tif_fd field in struct tiff

CTiffContext::InitDecoder() calls a helper function which wraps
TIFFClientOpen() and sets the tif_fd field in the returned struct. This
is a private field that the libtiff caller should not be reaching into.
The field also appears to be of no use to CTiffContext. Stop setting
this field and simplify the code. Then the code can just include the
public tiffio.h instead of the private tiffiop.h.

After removing the tiffiop.h include, adjust the bundled libtiff to
set error handlers itself.

Clean up C-style casts along the way.

Bug: chromium:1501495
Change-Id: Ic42e7c7e1af68483814f72333fd8a32547fb900f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/113594
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcodec/tiff/DEPS b/core/fxcodec/tiff/DEPS
index 83b566a..6e85120 100644
--- a/core/fxcodec/tiff/DEPS
+++ b/core/fxcodec/tiff/DEPS
@@ -1,3 +1,3 @@
 include_rules = [
-  '+third_party/libtiff/tiffiop.h',
+  '+third_party/libtiff/tiffio.h',
 ]
diff --git a/core/fxcodec/tiff/tiff_decoder.cpp b/core/fxcodec/tiff/tiff_decoder.cpp
index 518dd0b..928b7eb 100644
--- a/core/fxcodec/tiff/tiff_decoder.cpp
+++ b/core/fxcodec/tiff/tiff_decoder.cpp
@@ -23,7 +23,7 @@
 #include "third_party/base/numerics/safe_conversions.h"
 
 extern "C" {
-#include "third_party/libtiff/tiffiop.h"
+#include "third_party/libtiff/tiffio.h"
 }  // extern C
 
 namespace {
@@ -106,9 +106,6 @@
   return memcmp(ptr1, ptr2, static_cast<size_t>(size));
 }
 
-TIFFErrorHandler _TIFFwarningHandler = nullptr;
-TIFFErrorHandler _TIFFerrorHandler = nullptr;
-
 namespace {
 
 tsize_t tiff_read(thandle_t context, tdata_t buf, tsize_t length) {
@@ -185,16 +182,6 @@
 
 void tiff_unmap(thandle_t context, tdata_t, toff_t) {}
 
-TIFF* tiff_open(void* context, const char* mode) {
-  TIFF* tif = TIFFClientOpen("Tiff Image", mode, (thandle_t)context, tiff_read,
-                             tiff_write, tiff_seek, tiff_close, tiff_get_size,
-                             tiff_map, tiff_unmap);
-  if (tif) {
-    tif->tif_fd = (int)(intptr_t)context;
-  }
-  return tif;
-}
-
 void TiffBGRA2RGBA(uint8_t* pBuf, int32_t pixel, int32_t spp) {
   for (int32_t n = 0; n < pixel; n++) {
     uint8_t tmp = pBuf[0];
@@ -209,7 +196,9 @@
 bool CTiffContext::InitDecoder(
     const RetainPtr<IFX_SeekableReadStream>& file_ptr) {
   m_io_in = file_ptr;
-  m_tif_ctx.reset(tiff_open(this, "r"));
+  m_tif_ctx.reset(TIFFClientOpen(
+      /*name=*/"Tiff Image", /*mode=*/"r", /*clientdata=*/this, tiff_read,
+      tiff_write, tiff_seek, tiff_close, tiff_get_size, tiff_map, tiff_unmap));
   return !!m_tif_ctx;
 }
 
@@ -335,7 +324,7 @@
   }
   SetPalette(pDIBitmap, bps);
   int32_t size = static_cast<int32_t>(TIFFScanlineSize(m_tif_ctx.get()));
-  uint8_t* buf = (uint8_t*)_TIFFmalloc(size);
+  uint8_t* buf = reinterpret_cast<uint8_t*>(_TIFFmalloc(size));
   if (!buf) {
     TIFFError(TIFFFileName(m_tif_ctx.get()), "No space for scanline buffer");
     return false;
@@ -362,7 +351,7 @@
   }
   SetPalette(pDIBitmap, bps);
   int32_t size = static_cast<int32_t>(TIFFScanlineSize(m_tif_ctx.get()));
-  uint8_t* buf = (uint8_t*)_TIFFmalloc(size);
+  uint8_t* buf = reinterpret_cast<uint8_t*>(_TIFFmalloc(size));
   if (!buf) {
     TIFFError(TIFFFileName(m_tif_ctx.get()), "No space for scanline buffer");
     return false;
@@ -395,7 +384,7 @@
     return false;
 
   int32_t size = static_cast<int32_t>(TIFFScanlineSize(m_tif_ctx.get()));
-  uint8_t* buf = (uint8_t*)_TIFFmalloc(size);
+  uint8_t* buf = reinterpret_cast<uint8_t*>(_TIFFmalloc(size));
   if (!buf) {
     TIFFError(TIFFFileName(m_tif_ctx.get()), "No space for scanline buffer");
     return false;
@@ -426,8 +415,9 @@
   if (pDIBitmap->GetBPP() == 32) {
     uint16_t rotation = ORIENTATION_TOPLEFT;
     TIFFGetField(m_tif_ctx.get(), TIFFTAG_ORIENTATION, &rotation);
-    if (TIFFReadRGBAImageOriented(m_tif_ctx.get(), img_width, img_height,
-                                  (uint32_t*)pDIBitmap->GetBuffer().data(),
+    uint32_t* data = const_cast<uint32_t*>(
+        reinterpret_cast<const uint32_t*>(pDIBitmap->GetBuffer().data()));
+    if (TIFFReadRGBAImageOriented(m_tif_ctx.get(), img_width, img_height, data,
                                   rotation, 1)) {
       for (uint32_t row = 0; row < img_height; row++) {
         uint8_t* row_buf = pDIBitmap->GetWritableScanline(row).data();
diff --git a/third_party/libtiff/0001-no-error-handlers.patch b/third_party/libtiff/0001-no-error-handlers.patch
new file mode 100644
index 0000000..846783d
--- /dev/null
+++ b/third_party/libtiff/0001-no-error-handlers.patch
@@ -0,0 +1,14 @@
+diff --git a/third_party/libtiff/tif_error.c b/third_party/libtiff/tif_error.c
+index ac0b9c373..3d320eeff 100644
+--- a/third_party/libtiff/tif_error.c
++++ b/third_party/libtiff/tif_error.c
+@@ -29,6 +29,9 @@
+ 
+ TIFFErrorHandlerExt _TIFFerrorHandlerExt = NULL;
+ 
++TIFFErrorHandler _TIFFwarningHandler = NULL;
++TIFFErrorHandler _TIFFerrorHandler = NULL;
++
+ TIFFErrorHandler TIFFSetErrorHandler(TIFFErrorHandler handler)
+ {
+     TIFFErrorHandler prev = _TIFFerrorHandler;
diff --git a/third_party/libtiff/README.pdfium b/third_party/libtiff/README.pdfium
index fd575c7..9f3319d 100644
--- a/third_party/libtiff/README.pdfium
+++ b/third_party/libtiff/README.pdfium
@@ -12,6 +12,7 @@
 Local Modifications:
 
 0000-build-config.patch: Local build configuration changes.
+0001-no-error-handlers.patch: Set default error handlers.
 0017-safe_skews_in_gtTileContig.patch: return error if to/from skews overflow from int32.
 0028-nstrips-OOM.patch: return error for excess number of tiles/strips.
 0031-safe_size_ingtStripContig.patch: return error if the size to read overflow from int32.
diff --git a/third_party/libtiff/tif_error.c b/third_party/libtiff/tif_error.c
index ac0b9c3..3d320ee 100644
--- a/third_party/libtiff/tif_error.c
+++ b/third_party/libtiff/tif_error.c
@@ -29,6 +29,9 @@
 
 TIFFErrorHandlerExt _TIFFerrorHandlerExt = NULL;
 
+TIFFErrorHandler _TIFFwarningHandler = NULL;
+TIFFErrorHandler _TIFFerrorHandler = NULL;
+
 TIFFErrorHandler TIFFSetErrorHandler(TIFFErrorHandler handler)
 {
     TIFFErrorHandler prev = _TIFFerrorHandler;