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