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