Blame SOURCES/unzip-6.0-alt-iconv-utf8-print.patch

623fdc
From ca0212ba19b64488b9e8459a762c11ecd6e7d0bd Mon Sep 17 00:00:00 2001
623fdc
From: Petr Stodulka <pstodulk@redhat.com>
623fdc
Date: Tue, 24 Nov 2015 17:56:11 +0100
623fdc
Subject: [PATCH] print correctly non-ascii filenames
623fdc
623fdc
---
623fdc
 extract.c | 289 ++++++++++++++++++++++++++++++++++++++++++++++++--------------
623fdc
 unzpriv.h |   7 ++
623fdc
 2 files changed, 233 insertions(+), 63 deletions(-)
623fdc
623fdc
diff --git a/extract.c b/extract.c
623fdc
index 0ee4e93..741b7e0 100644
623fdc
--- a/extract.c
623fdc
+++ b/extract.c
623fdc
@@ -2648,8 +2648,21 @@ static void set_deferred_symlink(__G__ slnk_entry)
623fdc
 } /* end function set_deferred_symlink() */
623fdc
 #endif /* SYMLINKS */
623fdc
 
623fdc
+/*
623fdc
+ * If Unicode is supported, assume we have what we need to do this
623fdc
+ * check using wide characters, avoiding MBCS issues.
623fdc
+ */
623fdc
 
623fdc
-
623fdc
+#ifndef UZ_FNFILTER_REPLACECHAR
623fdc
+        /* A convenient choice for the replacement of unprintable char codes is
623fdc
+         * the "single char wildcard", as this character is quite unlikely to
623fdc
+         * appear in filenames by itself.  The following default definition
623fdc
+         * sets the replacement char to a question mark as the most common
623fdc
+         * "single char wildcard"; this setting should be overridden in the
623fdc
+         * appropiate system-specific configuration header when needed.
623fdc
+         */
623fdc
+# define UZ_FNFILTER_REPLACECHAR      '?'
623fdc
+#endif
623fdc
 
623fdc
 /*************************/
623fdc
 /*  Function fnfilter()  */        /* here instead of in list.c for SFX */
623fdc
@@ -2661,48 +2674,168 @@ char *fnfilter(raw, space, size)   /* convert name to safely printable form */
623fdc
     extent size;
623fdc
 {
623fdc
 #ifndef NATIVE   /* ASCII:  filter ANSI escape codes, etc. */
623fdc
-    ZCONST uch *r=(ZCONST uch *)raw;
623fdc
+    ZCONST uch *r; // =(ZCONST uch *)raw;
623fdc
     uch *s=space;
623fdc
     uch *slim=NULL;
623fdc
     uch *se=NULL;
623fdc
     int have_overflow = FALSE;
623fdc
 
623fdc
-    if (size > 0) {
623fdc
-        slim = space + size
623fdc
-#ifdef _MBCS
623fdc
-                     - (MB_CUR_MAX - 1)
623fdc
-#endif
623fdc
-                     - 4;
623fdc
+# if defined( UNICODE_SUPPORT) && defined( _MBCS)
623fdc
+/* If Unicode support is enabled, and we have multi-byte characters,
623fdc
+ * then do the isprint() checks by first converting to wide characters
623fdc
+ * and checking those.  This avoids our having to parse multi-byte
623fdc
+ * characters for ourselves.  After the wide-char replacements have been
623fdc
+ * made, the wide string is converted back to the local character set.
623fdc
+ */
623fdc
+    wchar_t *wstring;    /* wchar_t version of raw */
623fdc
+    size_t wslen;        /* length of wstring */
623fdc
+    wchar_t *wostring;   /* wchar_t version of output string */
623fdc
+    size_t woslen;       /* length of wostring */
623fdc
+    char *newraw;        /* new raw */
623fdc
+
623fdc
+    /* 2012-11-06 SMS.
623fdc
+     * Changed to check the value returned by mbstowcs(), and bypass the
623fdc
+     * Unicode processing if it fails.  This seems to fix a problem
623fdc
+     * reported in the SourceForge forum, but it's not clear that we
623fdc
+     * should be doing any Unicode processing without some evidence that
623fdc
+     * the name actually is Unicode.  (Check bit 11 in the flags before
623fdc
+     * coming here?)
623fdc
+     * http://sourceforge.net/p/infozip/bugs/40/
623fdc
+     */
623fdc
+
623fdc
+    if (MB_CUR_MAX <= 1)
623fdc
+    {
623fdc
+        /* There's no point to converting multi-byte chars if there are
623fdc
+         * no multi-byte chars.
623fdc
+         */
623fdc
+        wslen = (size_t)-1;
623fdc
     }
623fdc
-    while (*r) {
623fdc
-        if (size > 0 && s >= slim && se == NULL) {
623fdc
-            se = s;
623fdc
+    else
623fdc
+    {
623fdc
+        /* Get Unicode wide character count (for storage allocation). */
623fdc
+        wslen = mbstowcs( NULL, raw, 0);
623fdc
+    }
623fdc
+
623fdc
+    if (wslen != (size_t)-1)
623fdc
+    {
623fdc
+        /* Apparently valid Unicode.  Allocate wide-char storage. */
623fdc
+        wstring = (wchar_t *)malloc((wslen + 1) * sizeof(wchar_t));
623fdc
+        if (wstring == NULL) {
623fdc
+            strcpy( (char *)space, raw);
623fdc
+            return (char *)space;
623fdc
         }
623fdc
-#ifdef QDOS
623fdc
-        if (qlflag & 2) {
623fdc
-            if (*r == '/' || *r == '.') {
623fdc
+        wostring = (wchar_t *)malloc(2 * (wslen + 1) * sizeof(wchar_t));
623fdc
+        if (wostring == NULL) {
623fdc
+            free(wstring);
623fdc
+            strcpy( (char *)space, raw);
623fdc
+            return (char *)space;
623fdc
+        }
623fdc
+
623fdc
+        /* Convert the multi-byte Unicode to wide chars. */
623fdc
+        wslen = mbstowcs(wstring, raw, wslen + 1);
623fdc
+
623fdc
+        /* Filter the wide-character string. */
623fdc
+        fnfilterw( wstring, wostring, (2 * (wslen + 1) * sizeof(wchar_t)));
623fdc
+
623fdc
+        /* Convert filtered wide chars back to multi-byte. */
623fdc
+        woslen = wcstombs( NULL, wostring, 0);
623fdc
+        if ((newraw = malloc(woslen + 1)) == NULL) {
623fdc
+            free(wstring);
623fdc
+            free(wostring);
623fdc
+            strcpy( (char *)space, raw);
623fdc
+            return (char *)space;
623fdc
+        }
623fdc
+        woslen = wcstombs( newraw, wostring, (woslen * MB_CUR_MAX) + 1);
623fdc
+
623fdc
+        if (size > 0) {
623fdc
+            slim = space + size - 4;
623fdc
+        }
623fdc
+        r = (ZCONST uch *)newraw;
623fdc
+        while (*r) {
623fdc
+            if (size > 0 && s >= slim && se == NULL) {
623fdc
+                se = s;
623fdc
+            }
623fdc
+#  ifdef QDOS
623fdc
+            if (qlflag & 2) {
623fdc
+                if (*r == '/' || *r == '.') {
623fdc
+                    if (se != NULL && (s > (space + (size-3)))) {
623fdc
+                        have_overflow = TRUE;
623fdc
+                        break;
623fdc
+                    }
623fdc
+                    ++r;
623fdc
+                    *s++ = '_';
623fdc
+                    continue;
623fdc
+                }
623fdc
+            } else
623fdc
+#  endif
623fdc
+            {
623fdc
                 if (se != NULL && (s > (space + (size-3)))) {
623fdc
                     have_overflow = TRUE;
623fdc
                     break;
623fdc
                 }
623fdc
-                ++r;
623fdc
-                *s++ = '_';
623fdc
-                continue;
623fdc
+                *s++ = *r++;
623fdc
             }
623fdc
-        } else
623fdc
+        }
623fdc
+        if (have_overflow) {
623fdc
+            strcpy((char *)se, "...");
623fdc
+        } else {
623fdc
+            *s = '\0';
623fdc
+        }
623fdc
+
623fdc
+        free(wstring);
623fdc
+        free(wostring);
623fdc
+        free(newraw);
623fdc
+    }
623fdc
+    else
623fdc
+# endif /* defined( UNICODE_SUPPORT) && defined( _MBCS) */
623fdc
+    {
623fdc
+        /* No Unicode support, or apparently invalid Unicode. */
623fdc
+        r = (ZCONST uch *)raw;
623fdc
+
623fdc
+        if (size > 0) {
623fdc
+            slim = space + size
623fdc
+#ifdef _MBCS
623fdc
+                         - (MB_CUR_MAX - 1)
623fdc
+#endif
623fdc
+                         - 4;
623fdc
+        }
623fdc
+        while (*r) {
623fdc
+            if (size > 0 && s >= slim && se == NULL) {
623fdc
+                se = s;
623fdc
+            }
623fdc
+#ifdef QDOS
623fdc
+            if (qlflag & 2) {
623fdc
+                if (*r == '/' || *r == '.') {
623fdc
+                    if (se != NULL && (s > (space + (size-3)))) {
623fdc
+                        have_overflow = TRUE;
623fdc
+                        break;
623fdc
+                    }
623fdc
+                    ++r;
623fdc
+                    *s++ = '_';
623fdc
+                    continue;
623fdc
+                }
623fdc
+            } else
623fdc
 #endif
623fdc
 #ifdef HAVE_WORKING_ISPRINT
623fdc
-# ifndef UZ_FNFILTER_REPLACECHAR
623fdc
-    /* A convenient choice for the replacement of unprintable char codes is
623fdc
-     * the "single char wildcard", as this character is quite unlikely to
623fdc
-     * appear in filenames by itself.  The following default definition
623fdc
-     * sets the replacement char to a question mark as the most common
623fdc
-     * "single char wildcard"; this setting should be overridden in the
623fdc
-     * appropiate system-specific configuration header when needed.
623fdc
-     */
623fdc
-#   define UZ_FNFILTER_REPLACECHAR      '?'
623fdc
-# endif
623fdc
-        if (!isprint(*r)) {
623fdc
+            if (!isprint(*r)) {
623fdc
+                if (*r < 32) {
623fdc
+                    /* ASCII control codes are escaped as "^{letter}". */
623fdc
+                    if (se != NULL && (s > (space + (size-4)))) {
623fdc
+                        have_overflow = TRUE;
623fdc
+                        break;
623fdc
+                    }
623fdc
+                    *s++ = '^', *s++ = (uch)(64 + *r++);
623fdc
+                } else {
623fdc
+                    /* Other unprintable codes are replaced by the
623fdc
+                     * placeholder character. */
623fdc
+                    if (se != NULL && (s > (space + (size-3)))) {
623fdc
+                        have_overflow = TRUE;
623fdc
+                        break;
623fdc
+                    }
623fdc
+                    *s++ = UZ_FNFILTER_REPLACECHAR;
623fdc
+                    INCSTR(r);
623fdc
+                }
623fdc
+#else /* !HAVE_WORKING_ISPRINT */
623fdc
             if (*r < 32) {
623fdc
                 /* ASCII control codes are escaped as "^{letter}". */
623fdc
                 if (se != NULL && (s > (space + (size-4)))) {
623fdc
@@ -2710,47 +2843,30 @@ char *fnfilter(raw, space, size)   /* convert name to safely printable form */
623fdc
                     break;
623fdc
                 }
623fdc
                 *s++ = '^', *s++ = (uch)(64 + *r++);
623fdc
+#endif /* ?HAVE_WORKING_ISPRINT */
623fdc
             } else {
623fdc
-                /* Other unprintable codes are replaced by the
623fdc
-                 * placeholder character. */
623fdc
+#ifdef _MBCS
623fdc
+                unsigned i = CLEN(r);
623fdc
+                if (se != NULL && (s > (space + (size-i-2)))) {
623fdc
+                    have_overflow = TRUE;
623fdc
+                    break;
623fdc
+                }
623fdc
+                for (; i > 0; i--)
623fdc
+                    *s++ = *r++;
623fdc
+#else
623fdc
                 if (se != NULL && (s > (space + (size-3)))) {
623fdc
                     have_overflow = TRUE;
623fdc
                     break;
623fdc
                 }
623fdc
-                *s++ = UZ_FNFILTER_REPLACECHAR;
623fdc
-                INCSTR(r);
623fdc
-            }
623fdc
-#else /* !HAVE_WORKING_ISPRINT */
623fdc
-        if (*r < 32) {
623fdc
-            /* ASCII control codes are escaped as "^{letter}". */
623fdc
-            if (se != NULL && (s > (space + (size-4)))) {
623fdc
-                have_overflow = TRUE;
623fdc
-                break;
623fdc
-            }
623fdc
-            *s++ = '^', *s++ = (uch)(64 + *r++);
623fdc
-#endif /* ?HAVE_WORKING_ISPRINT */
623fdc
-        } else {
623fdc
-#ifdef _MBCS
623fdc
-            unsigned i = CLEN(r);
623fdc
-            if (se != NULL && (s > (space + (size-i-2)))) {
623fdc
-                have_overflow = TRUE;
623fdc
-                break;
623fdc
-            }
623fdc
-            for (; i > 0; i--)
623fdc
                 *s++ = *r++;
623fdc
-#else
623fdc
-            if (se != NULL && (s > (space + (size-3)))) {
623fdc
-                have_overflow = TRUE;
623fdc
-                break;
623fdc
-            }
623fdc
-            *s++ = *r++;
623fdc
 #endif
623fdc
-         }
623fdc
-    }
623fdc
-    if (have_overflow) {
623fdc
-        strcpy((char *)se, "...");
623fdc
-    } else {
623fdc
-        *s = '\0';
623fdc
+             }
623fdc
+        }
623fdc
+        if (have_overflow) {
623fdc
+            strcpy((char *)se, "...");
623fdc
+        } else {
623fdc
+            *s = '\0';
623fdc
+        }
623fdc
     }
623fdc
 
623fdc
 #ifdef WINDLL
623fdc
@@ -2772,6 +2888,53 @@ char *fnfilter(raw, space, size)   /* convert name to safely printable form */
623fdc
 } /* end function fnfilter() */
623fdc
 
623fdc
 
623fdc
+#if defined( UNICODE_SUPPORT) && defined( _MBCS)
623fdc
+
623fdc
+/****************************/
623fdc
+/*  Function fnfilter[w]()  */  /* (Here instead of in list.c for SFX.) */
623fdc
+/****************************/
623fdc
+
623fdc
+/* fnfilterw() - Convert wide name to safely printable form. */
623fdc
+
623fdc
+/* fnfilterw() - Convert wide-character name to safely printable form. */
623fdc
+
623fdc
+wchar_t *fnfilterw( src, dst, siz)
623fdc
+    ZCONST wchar_t *src;        /* Pointer to source char (string). */
623fdc
+    wchar_t *dst;               /* Pointer to destination char (string). */
623fdc
+    extent siz;                 /* Not used (!). */
623fdc
+{
623fdc
+    wchar_t *dsx = dst;
623fdc
+
623fdc
+    /* Filter the wide chars. */
623fdc
+    while (*src)
623fdc
+    {
623fdc
+        if (iswprint( *src))
623fdc
+        {
623fdc
+            /* Printable code.  Copy it. */
623fdc
+            *dst++ = *src;
623fdc
+        }
623fdc
+        else
623fdc
+        {
623fdc
+            /* Unprintable code.  Substitute something printable for it. */
623fdc
+            if (*src < 32)
623fdc
+            {
623fdc
+                /* Replace ASCII control code with "^{letter}". */
623fdc
+                *dst++ = (wchar_t)'^';
623fdc
+                *dst++ = (wchar_t)(64 + *src);
623fdc
+            }
623fdc
+            else
623fdc
+            {
623fdc
+                /* Replace other unprintable code with the placeholder. */
623fdc
+                *dst++ = (wchar_t)UZ_FNFILTER_REPLACECHAR;
623fdc
+            }
623fdc
+        }
623fdc
+        src++;
623fdc
+    }
623fdc
+    *dst = (wchar_t)0;  /* NUL-terminate the destination string. */
623fdc
+    return dsx;
623fdc
+} /* fnfilterw(). */
623fdc
+
623fdc
+#endif /* defined( UNICODE_SUPPORT) && defined( _MBCS) */
623fdc
 
623fdc
 
623fdc
 #ifdef SET_DIR_ATTRIB
623fdc
diff --git a/unzpriv.h b/unzpriv.h
623fdc
index 22d3923..e48a652 100644
623fdc
--- a/unzpriv.h
623fdc
+++ b/unzpriv.h
623fdc
@@ -1212,6 +1212,7 @@
623fdc
 # ifdef UNICODE_WCHAR
623fdc
 #  if !(defined(_WIN32_WCE) || defined(POCKET_UNZIP))
623fdc
 #   include <wchar.h>
623fdc
+#   include <wctype.h>
623fdc
 #  endif
623fdc
 # endif
623fdc
 # ifndef _MBCS  /* no need to include <locale.h> twice, see below */
623fdc
@@ -2410,6 +2411,12 @@ int    memflush                  OF((__GPRO__ ZCONST uch *rawbuf, ulg size));
623fdc
 char  *fnfilter                  OF((ZCONST char *raw, uch *space,
623fdc
                                      extent size));
623fdc
 
623fdc
+# if defined( UNICODE_SUPPORT) && defined( _MBCS)
623fdc
+wchar_t *fnfilterw               OF((ZCONST wchar_t *src, wchar_t *dst,
623fdc
+                                     extent siz));
623fdc
+#endif
623fdc
+
623fdc
+
623fdc
 /*---------------------------------------------------------------------------
623fdc
     Decompression functions:
623fdc
   ---------------------------------------------------------------------------*/
623fdc
-- 
623fdc
2.4.3
623fdc