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

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