From c649f447bfce701040336dd021fdd80cf262ef77 Mon Sep 17 00:00:00 2001
From: Akira Kakuto <kakuto@fuk.kindai.ac.jp>
Date: Sat, 28 Apr 2018 06:09:56 +0000
Subject: [PATCH 1/7] dvipdfm-x: Fix a bug which causes a segfault with
 1/2/4-bit transparent indexed PNGs (Stefun Br\"uns).

git-svn-id: svn://tug.org/texlive/trunk/Build/source@47469 c570f23f-e606-0410-a88d-b1316a301751
---
 texk/dvipdfm-x/ChangeLog    |  6 ++++++
 texk/dvipdfm-x/configure    | 22 +++++++++++-----------
 texk/dvipdfm-x/configure.ac |  2 +-
 texk/dvipdfm-x/pngimage.c   |  7 ++++++-
 4 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/texk/dvipdfm-x/ChangeLog b/texk/dvipdfm-x/ChangeLog
index 51326a845..66924f693 100644
--- a/texk/dvipdfm-x/ChangeLog
+++ b/texk/dvipdfm-x/ChangeLog
@@ -1,3 +1,9 @@
+2018-04-28  Stefun Br\"uns  <stefan.bruens@rwth-aachen.de>
+
+	* pngimage.c: Fix a bug which causes a segfault with 1/2/4-bit
+	transparent indexed PNGs.
+	* configure.ac: Version 20180428.
+
 2018-03-03  Akira Kakuto  <kakuto@fuk.kindai.ac.jp>
 
 	* xbb.c: Remove unused lines.
diff --git a/texk/dvipdfm-x/configure b/texk/dvipdfm-x/configure
index 157cbb3bb..036c643ec 100755
--- a/texk/dvipdfm-x/configure
+++ b/texk/dvipdfm-x/configure
@@ -1,6 +1,6 @@
 #! /bin/sh
 # Guess values for system-dependent variables and create Makefiles.
-# Generated by GNU Autoconf 2.69 for dvipdfm-x (TeX Live) 20180217.
+# Generated by GNU Autoconf 2.69 for dvipdfm-x (TeX Live) 20180428.
 #
 # Report bugs to <tex-k@tug.org>.
 #
@@ -590,8 +590,8 @@ MAKEFLAGS=
 # Identity of this package.
 PACKAGE_NAME='dvipdfm-x (TeX Live)'
 PACKAGE_TARNAME='dvipdfm-x--tex-live-'
-PACKAGE_VERSION='20180217'
-PACKAGE_STRING='dvipdfm-x (TeX Live) 20180217'
+PACKAGE_VERSION='20180428'
+PACKAGE_STRING='dvipdfm-x (TeX Live) 20180428'
 PACKAGE_BUGREPORT='tex-k@tug.org'
 PACKAGE_URL=''
 
@@ -1350,7 +1350,7 @@ if test "$ac_init_help" = "long"; then
   # Omit some internal or obsolete options to make the list less imposing.
   # This message is too long to be a string in the A/UX 3.1 sh.
   cat <<_ACEOF
-\`configure' configures dvipdfm-x (TeX Live) 20180217 to adapt to many kinds of systems.
+\`configure' configures dvipdfm-x (TeX Live) 20180428 to adapt to many kinds of systems.
 
 Usage: $0 [OPTION]... [VAR=VALUE]...
 
@@ -1421,7 +1421,7 @@ fi
 
 if test -n "$ac_init_help"; then
   case $ac_init_help in
-     short | recursive ) echo "Configuration of dvipdfm-x (TeX Live) 20180217:";;
+     short | recursive ) echo "Configuration of dvipdfm-x (TeX Live) 20180428:";;
    esac
   cat <<\_ACEOF
 
@@ -1551,7 +1551,7 @@ fi
 test -n "$ac_init_help" && exit $ac_status
 if $ac_init_version; then
   cat <<\_ACEOF
-dvipdfm-x (TeX Live) configure 20180217
+dvipdfm-x (TeX Live) configure 20180428
 generated by GNU Autoconf 2.69
 
 Copyright (C) 2012 Free Software Foundation, Inc.
@@ -2390,7 +2390,7 @@ cat >config.log <<_ACEOF
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
 
-It was created by dvipdfm-x (TeX Live) $as_me 20180217, which was
+It was created by dvipdfm-x (TeX Live) $as_me 20180428, which was
 generated by GNU Autoconf 2.69.  Invocation command line was
 
   $ $0 $@
@@ -8075,7 +8075,7 @@ fi
 
 # Define the identity of the package.
  PACKAGE='dvipdfm-x--tex-live-'
- VERSION='20180217'
+ VERSION='20180428'
 
 
 cat >>confdefs.h <<_ACEOF
@@ -14744,7 +14744,7 @@ Usage: $0 [OPTIONS]
 Report bugs to <bug-libtool@gnu.org>."
 
 lt_cl_version="\
-dvipdfm-x (TeX Live) config.lt 20180217
+dvipdfm-x (TeX Live) config.lt 20180428
 configured by $0, generated by GNU Autoconf 2.69.
 
 Copyright (C) 2011 Free Software Foundation, Inc.
@@ -16624,7 +16624,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 # report actual input values of CONFIG_FILES etc. instead of their
 # values after options handling.
 ac_log="
-This file was extended by dvipdfm-x (TeX Live) $as_me 20180217, which was
+This file was extended by dvipdfm-x (TeX Live) $as_me 20180428, which was
 generated by GNU Autoconf 2.69.  Invocation command line was
 
   CONFIG_FILES    = $CONFIG_FILES
@@ -16694,7 +16694,7 @@ _ACEOF
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 ac_cs_version="\\
-dvipdfm-x (TeX Live) config.status 20180217
+dvipdfm-x (TeX Live) config.status 20180428
 configured by $0, generated by GNU Autoconf 2.69,
   with options \\"\$ac_cs_config\\"
 
diff --git a/texk/dvipdfm-x/configure.ac b/texk/dvipdfm-x/configure.ac
index f3b899ad2..81c02a96b 100644
--- a/texk/dvipdfm-x/configure.ac
+++ b/texk/dvipdfm-x/configure.ac
@@ -7,7 +7,7 @@ dnl   This file is free software; the copyright holder
 dnl   gives unlimited permission to copy and/or distribute it,
 dnl   with or without modifications, as long as this notice is preserved.
 dnl
-AC_INIT([dvipdfm-x (TeX Live)], [20180217], [tex-k@tug.org])
+AC_INIT([dvipdfm-x (TeX Live)], [20180428], [tex-k@tug.org])
 AC_PREREQ([2.65])
 AC_CONFIG_SRCDIR([agl.c])
 AC_CONFIG_AUX_DIR([../../build-aux])
diff --git a/texk/dvipdfm-x/pngimage.c b/texk/dvipdfm-x/pngimage.c
index e4152177e..e985b0fa8 100644
--- a/texk/dvipdfm-x/pngimage.c
+++ b/texk/dvipdfm-x/pngimage.c
@@ -964,12 +964,16 @@ create_soft_mask (png_structp png_ptr, png_infop info_ptr,
   png_bytep   trans;
   int         num_trans;
   png_uint_32 i;
+  png_byte    bpc, mask, shift;
 
   if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ||
       !png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL)) {
     WARN("%s: PNG does not have valid tRNS chunk but tRNS is requested.", PNG_DEBUG_STR);
     return NULL;
   }
+  bpc   = png_get_bit_depth(png_ptr, info_ptr);
+  mask  = 0xff >> (8 - bpc);
+  shift = 8 - bpc;
 
   smask = pdf_new_stream(STREAM_COMPRESS);
   dict  = pdf_stream_dict(smask);
@@ -981,7 +985,8 @@ create_soft_mask (png_structp png_ptr, png_infop info_ptr,
   pdf_add_dict(dict, pdf_new_name("ColorSpace"), pdf_new_name("DeviceGray"));
   pdf_add_dict(dict, pdf_new_name("BitsPerComponent"), pdf_new_number(8));
   for (i = 0; i < width*height; i++) {
-    png_byte idx = image_data_ptr[i];
+    /* data is packed for 1/2/4 bpc formats, msb first */
+    png_byte idx = (image_data_ptr[bpc * i / 8] >> (shift - bpc * i % 8)) & mask;
     smask_data_ptr[i] = (idx < num_trans) ? trans[idx] : 0xff;
   }
   pdf_add_stream(smask, (char *)smask_data_ptr, width*height);
-- 
2.17.0

