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