d599e3
From: Giovanni Scafora <giovanni.archlinux.org>
d599e3
Subject: unzip files encoded with non-latin, non-unicode file names
d599e3
Last-Update: 2015-02-11
d599e3
d599e3
Updated 2015-02-11 by Marc Deslauriers <marc.deslauriers@canonical.com>
d599e3
to fix buffer overflow in charset_to_intern()
d599e3
d599e3
Index: unzip-6.0/unix/unix.c
d599e3
===================================================================
d599e3
--- unzip-6.0.orig/unix/unix.c	2015-02-11 08:46:43.675324290 -0500
d599e3
+++ unzip-6.0/unix/unix.c	2015-02-11 09:18:04.902081319 -0500
d599e3
@@ -30,6 +30,9 @@
d599e3
 #define UNZIP_INTERNAL
d599e3
 #include "unzip.h"
d599e3
 
d599e3
+#include <iconv.h>
d599e3
+#include <langinfo.h>
d599e3
+
d599e3
 #ifdef SCO_XENIX
d599e3
 #  define SYSNDIR
d599e3
 #else  /* SCO Unix, AIX, DNIX, TI SysV, Coherent 4.x, ... */
d599e3
@@ -1874,3 +1877,102 @@
d599e3
     }
d599e3
 }
d599e3
 #endif /* QLZIP */
d599e3
+
d599e3
+
d599e3
+typedef struct {
d599e3
+    char *local_charset;
d599e3
+    char *archive_charset;
d599e3
+} CHARSET_MAP;
d599e3
+
d599e3
+/* A mapping of local <-> archive charsets used by default to convert filenames
d599e3
+ * of DOS/Windows Zip archives. Currently very basic. */
d599e3
+static CHARSET_MAP dos_charset_map[] = {
d599e3
+    { "ANSI_X3.4-1968", "CP850" },
d599e3
+    { "ISO-8859-1", "CP850" },
d599e3
+    { "CP1252", "CP850" },
d599e3
+    { "UTF-8", "CP866" },
d599e3
+    { "KOI8-R", "CP866" },
d599e3
+    { "KOI8-U", "CP866" },
d599e3
+    { "ISO-8859-5", "CP866" }
d599e3
+};
d599e3
+
d599e3
+char OEM_CP[MAX_CP_NAME] = "";
d599e3
+char ISO_CP[MAX_CP_NAME] = "";
d599e3
+
d599e3
+/* Try to guess the default value of OEM_CP based on the current locale.
d599e3
+ * ISO_CP is left alone for now. */
d599e3
+void init_conversion_charsets()
d599e3
+{
d599e3
+    const char *local_charset;
d599e3
+    int i;
d599e3
+
d599e3
+    /* Make a guess only if OEM_CP not already set. */ 
d599e3
+    if(*OEM_CP == '\0') {
d599e3
+    	local_charset = nl_langinfo(CODESET);
d599e3
+    	for(i = 0; i < sizeof(dos_charset_map)/sizeof(CHARSET_MAP); i++)
d599e3
+    		if(!strcasecmp(local_charset, dos_charset_map[i].local_charset)) {
d599e3
+    			strncpy(OEM_CP, dos_charset_map[i].archive_charset,
d599e3
+    					sizeof(OEM_CP));
d599e3
+    			break;
d599e3
+    		}
d599e3
+    }
d599e3
+}
d599e3
+
d599e3
+/* Convert a string from one encoding to the current locale using iconv().
d599e3
+ * Be as non-intrusive as possible. If error is encountered during covertion
d599e3
+ * just leave the string intact. */
d599e3
+static void charset_to_intern(char *string, char *from_charset)
d599e3
+{
d599e3
+    iconv_t cd;
d599e3
+    char *s,*d, *buf;
d599e3
+    size_t slen, dlen, buflen;
d599e3
+    const char *local_charset;
d599e3
+
d599e3
+    if(*from_charset == '\0')
d599e3
+    	return;
d599e3
+
d599e3
+    buf = NULL;
d599e3
+    local_charset = nl_langinfo(CODESET);
d599e3
+
d599e3
+    if((cd = iconv_open(local_charset, from_charset)) == (iconv_t)-1)
d599e3
+        return;
d599e3
+
d599e3
+    slen = strlen(string);
d599e3
+    s = string;
d599e3
+
d599e3
+    /*  Make sure OUTBUFSIZ + 1 never ends up smaller than FILNAMSIZ
d599e3
+     *  as this function also gets called with G.outbuf in fileio.c
d599e3
+     */
d599e3
+    buflen = FILNAMSIZ;
d599e3
+    if (OUTBUFSIZ + 1 < FILNAMSIZ)
d599e3
+    {
d599e3
+        buflen = OUTBUFSIZ + 1;
d599e3
+    }
d599e3
+
d599e3
+    d = buf = malloc(buflen);
d599e3
+    if(!d)
d599e3
+    	goto cleanup;
d599e3
+
d599e3
+    bzero(buf,buflen);
d599e3
+    dlen = buflen - 1;
d599e3
+
d599e3
+    if(iconv(cd, &s, &slen, &d, &dlen) == (size_t)-1)
d599e3
+    	goto cleanup;
d599e3
+    strncpy(string, buf, buflen);
d599e3
+
d599e3
+    cleanup:
d599e3
+    free(buf);
d599e3
+    iconv_close(cd);
d599e3
+}
d599e3
+
d599e3
+/* Convert a string from OEM_CP to the current locale charset. */
d599e3
+inline void oem_intern(char *string)
d599e3
+{
d599e3
+    charset_to_intern(string, OEM_CP);
d599e3
+}
d599e3
+
d599e3
+/* Convert a string from ISO_CP to the current locale charset. */
d599e3
+inline void iso_intern(char *string)
d599e3
+{
d599e3
+    charset_to_intern(string, ISO_CP);
d599e3
+}
d599e3
Index: unzip-6.0/unix/unxcfg.h
d599e3
===================================================================
d599e3
--- unzip-6.0.orig/unix/unxcfg.h	2015-02-11 08:46:43.675324290 -0500
d599e3
+++ unzip-6.0/unix/unxcfg.h	2015-02-11 08:46:43.671324260 -0500
d599e3
@@ -228,4 +228,30 @@
d599e3
 /* wild_dir, dirname, wildname, matchname[], dirnamelen, have_dirname, */
d599e3
 /*    and notfirstcall are used by do_wild().                          */
d599e3
 
d599e3
+
d599e3
+#define MAX_CP_NAME 25 
d599e3
+   
d599e3
+#ifdef SETLOCALE
d599e3
+#  undef SETLOCALE
d599e3
+#endif
d599e3
+#define SETLOCALE(category, locale) setlocale(category, locale)
d599e3
+#include <locale.h>
d599e3
+   
d599e3
+#ifdef _ISO_INTERN
d599e3
+#  undef _ISO_INTERN
d599e3
+#endif
d599e3
+#define _ISO_INTERN(str1) iso_intern(str1)
d599e3
+
d599e3
+#ifdef _OEM_INTERN
d599e3
+#  undef _OEM_INTERN
d599e3
+#endif
d599e3
+#ifndef IZ_OEM2ISO_ARRAY
d599e3
+#  define IZ_OEM2ISO_ARRAY
d599e3
+#endif
d599e3
+#define _OEM_INTERN(str1) oem_intern(str1)
d599e3
+
d599e3
+void iso_intern(char *);
d599e3
+void oem_intern(char *);
d599e3
+void init_conversion_charsets(void);
d599e3
+   
d599e3
 #endif /* !__unxcfg_h */
d599e3
Index: unzip-6.0/unzip.c
d599e3
===================================================================
d599e3
--- unzip-6.0.orig/unzip.c	2015-02-11 08:46:43.675324290 -0500
d599e3
+++ unzip-6.0/unzip.c	2015-02-11 08:46:43.675324290 -0500
d599e3
@@ -327,11 +327,21 @@
d599e3
   -2  just filenames but allow -h/-t/-z  -l  long Unix \"ls -l\" format\n\
d599e3
                                          -v  verbose, multi-page format\n";
d599e3
 
d599e3
+#ifndef UNIX
d599e3
 static ZCONST char Far ZipInfoUsageLine3[] = "miscellaneous options:\n\
d599e3
   -h  print header line       -t  print totals for listed files or for all\n\
d599e3
   -z  print zipfile comment   -T  print file times in sortable decimal format\
d599e3
 \n  -C  be case-insensitive   %s\
d599e3
   -x  exclude filenames that follow from listing\n";
d599e3
+#else /* UNIX */
d599e3
+static ZCONST char Far ZipInfoUsageLine3[] = "miscellaneous options:\n\
d599e3
+  -h  print header line       -t  print totals for listed files or for all\n\
d599e3
+  -z  print zipfile comment  %c-T%c print file times in sortable decimal format\
d599e3
+\n %c-C%c be case-insensitive   %s\
d599e3
+  -x  exclude filenames that follow from listing\n\
d599e3
+  -O CHARSET  specify a character encoding for DOS, Windows and OS/2 archives\n\
d599e3
+  -I CHARSET  specify a character encoding for UNIX and other archives\n";
d599e3
+#endif /* !UNIX */
d599e3
 #ifdef MORE
d599e3
    static ZCONST char Far ZipInfoUsageLine4[] =
d599e3
      "  -M  page output through built-in \"more\"\n";
d599e3
@@ -664,6 +674,17 @@
d599e3
   -U  use escapes for all non-ASCII Unicode  -UU ignore any Unicode fields\n\
d599e3
   -C  match filenames case-insensitively     -L  make (some) names \
d599e3
 lowercase\n %-42s  -V  retain VMS version numbers\n%s";
d599e3
+#elif (defined UNIX)
d599e3
+static ZCONST char Far UnzipUsageLine4[] = "\
d599e3
+modifiers:\n\
d599e3
+  -n  never overwrite existing files         -q  quiet mode (-qq => quieter)\n\
d599e3
+  -o  overwrite files WITHOUT prompting      -a  auto-convert any text files\n\
d599e3
+  -j  junk paths (do not make directories)   -aa treat ALL files as text\n\
d599e3
+  -U  use escapes for all non-ASCII Unicode  -UU ignore any Unicode fields\n\
d599e3
+  -C  match filenames case-insensitively     -L  make (some) names \
d599e3
+lowercase\n %-42s  -V  retain VMS version numbers\n%s\
d599e3
+  -O CHARSET  specify a character encoding for DOS, Windows and OS/2 archives\n\
d599e3
+  -I CHARSET  specify a character encoding for UNIX and other archives\n\n";
d599e3
 #else /* !VMS */
d599e3
 static ZCONST char Far UnzipUsageLine4[] = "\
d599e3
 modifiers:\n\
d599e3
@@ -802,6 +823,10 @@
d599e3
 #endif /* UNICODE_SUPPORT */
d599e3
 
d599e3
 
d599e3
+#ifdef UNIX
d599e3
+    init_conversion_charsets();
d599e3
+#endif
d599e3
+
d599e3
 #if (defined(__IBMC__) && defined(__DEBUG_ALLOC__))
d599e3
     extern void DebugMalloc(void);
d599e3
 
d599e3
@@ -1335,6 +1360,11 @@
d599e3
     argc = *pargc;
d599e3
     argv = *pargv;
d599e3
 
d599e3
+#ifdef UNIX
d599e3
+    extern char OEM_CP[MAX_CP_NAME];
d599e3
+    extern char ISO_CP[MAX_CP_NAME];
d599e3
+#endif
d599e3
+    
d599e3
     while (++argv, (--argc > 0 && *argv != NULL && **argv == '-')) {
d599e3
         s = *argv + 1;
d599e3
         while ((c = *s++) != 0) {    /* "!= 0":  prevent Turbo C warning */
d599e3
@@ -1516,6 +1546,35 @@
d599e3
                     }
d599e3
                     break;
d599e3
 #endif  /* MACOS */
d599e3
+#ifdef UNIX
d599e3
+    			case ('I'):
d599e3
+                    if (negative) {
d599e3
+                        Info(slide, 0x401, ((char *)slide,
d599e3
+                          "error:  encodings can't be negated"));
d599e3
+                        return(PK_PARAM);
d599e3
+    				} else {
d599e3
+    					if(*s) { /* Handle the -Icharset case */
d599e3
+    						/* Assume that charsets can't start with a dash to spot arguments misuse */
d599e3
+    						if(*s == '-') { 
d599e3
+    	                        Info(slide, 0x401, ((char *)slide,
d599e3
+        		                  "error:  a valid character encoding should follow the -I argument"));
d599e3
+    	                        return(PK_PARAM); 
d599e3
+    						}
d599e3
+    						strncpy(ISO_CP, s, sizeof(ISO_CP));
d599e3
+    					} else { /* -I charset */
d599e3
+    						++argv;
d599e3
+    						if(!(--argc > 0 && *argv != NULL && **argv != '-')) {
d599e3
+    	                        Info(slide, 0x401, ((char *)slide,
d599e3
+        		                  "error:  a valid character encoding should follow the -I argument"));
d599e3
+    	                        return(PK_PARAM); 
d599e3
+    						}
d599e3
+    						s = *argv;
d599e3
+    						strncpy(ISO_CP, s, sizeof(ISO_CP));
d599e3
+    					}
d599e3
+    					while(*(++s)); /* No params straight after charset name */
d599e3
+    				}
d599e3
+    				break;
d599e3
+#endif /* ?UNIX */
d599e3
                 case ('j'):    /* junk pathnames/directory structure */
d599e3
                     if (negative)
d599e3
                         uO.jflag = FALSE, negative = 0;
d599e3
@@ -1591,6 +1650,35 @@
d599e3
                     } else
d599e3
                         ++uO.overwrite_all;
d599e3
                     break;
d599e3
+#ifdef UNIX
d599e3
+    			case ('O'):
d599e3
+                    if (negative) {
d599e3
+                        Info(slide, 0x401, ((char *)slide,
d599e3
+                          "error:  encodings can't be negated"));
d599e3
+                        return(PK_PARAM);
d599e3
+    				} else {
d599e3
+    					if(*s) { /* Handle the -Ocharset case */
d599e3
+    						/* Assume that charsets can't start with a dash to spot arguments misuse */
d599e3
+    						if(*s == '-') { 
d599e3
+    	                        Info(slide, 0x401, ((char *)slide,
d599e3
+        		                  "error:  a valid character encoding should follow the -I argument"));
d599e3
+    	                        return(PK_PARAM); 
d599e3
+    						}
d599e3
+    						strncpy(OEM_CP, s, sizeof(OEM_CP));
d599e3
+    					} else { /* -O charset */
d599e3
+    						++argv;
d599e3
+    						if(!(--argc > 0 && *argv != NULL && **argv != '-')) {
d599e3
+    	                        Info(slide, 0x401, ((char *)slide,
d599e3
+        		                  "error:  a valid character encoding should follow the -O argument"));
d599e3
+    	                        return(PK_PARAM); 
d599e3
+    						}
d599e3
+    						s = *argv;
d599e3
+    						strncpy(OEM_CP, s, sizeof(OEM_CP));
d599e3
+    					}
d599e3
+    					while(*(++s)); /* No params straight after charset name */
d599e3
+    				}
d599e3
+    				break;
d599e3
+#endif /* ?UNIX */
d599e3
                 case ('p'):    /* pipes:  extract to stdout, no messages */
d599e3
                     if (negative) {
d599e3
                         uO.cflag = FALSE;
d599e3
Index: unzip-6.0/unzpriv.h
d599e3
===================================================================
d599e3
--- unzip-6.0.orig/unzpriv.h	2015-02-11 08:46:43.675324290 -0500
d599e3
+++ unzip-6.0/unzpriv.h	2015-02-11 08:46:43.675324290 -0500
d599e3
@@ -3008,7 +3008,7 @@
d599e3
          !(((islochdr) || (isuxatt)) && \
d599e3
            ((hostver) == 25 || (hostver) == 26 || (hostver) == 40))) || \
d599e3
         (hostnum) == FS_HPFS_ || \
d599e3
-        ((hostnum) == FS_NTFS_ && (hostver) == 50)) { \
d599e3
+        ((hostnum) == FS_NTFS_ /* && (hostver) == 50 */ )) { \
d599e3
         _OEM_INTERN((string)); \
d599e3
     } else { \
d599e3
         _ISO_INTERN((string)); \
d599e3
Index: unzip-6.0/zipinfo.c
d599e3
===================================================================
d599e3
--- unzip-6.0.orig/zipinfo.c	2015-02-11 08:46:43.675324290 -0500
d599e3
+++ unzip-6.0/zipinfo.c	2015-02-11 08:46:43.675324290 -0500
d599e3
@@ -457,6 +457,10 @@
d599e3
     int    tflag_slm=TRUE, tflag_2v=FALSE;
d599e3
     int    explicit_h=FALSE, explicit_t=FALSE;
d599e3
 
d599e3
+#ifdef UNIX
d599e3
+    extern char OEM_CP[MAX_CP_NAME];
d599e3
+    extern char ISO_CP[MAX_CP_NAME];
d599e3
+#endif
d599e3
 
d599e3
 #ifdef MACOS
d599e3
     uO.lflag = LFLAG;         /* reset default on each call */
d599e3
@@ -501,6 +505,35 @@
d599e3
                             uO.lflag = 0;
d599e3
                     }
d599e3
                     break;
d599e3
+#ifdef UNIX
d599e3
+    			case ('I'):
d599e3
+                    if (negative) {
d599e3
+                        Info(slide, 0x401, ((char *)slide,
d599e3
+                          "error:  encodings can't be negated"));
d599e3
+                        return(PK_PARAM);
d599e3
+    				} else {
d599e3
+    					if(*s) { /* Handle the -Icharset case */
d599e3
+    						/* Assume that charsets can't start with a dash to spot arguments misuse */
d599e3
+    						if(*s == '-') { 
d599e3
+    	                        Info(slide, 0x401, ((char *)slide,
d599e3
+        		                  "error:  a valid character encoding should follow the -I argument"));
d599e3
+    	                        return(PK_PARAM); 
d599e3
+    						}
d599e3
+    						strncpy(ISO_CP, s, sizeof(ISO_CP));
d599e3
+    					} else { /* -I charset */
d599e3
+    						++argv;
d599e3
+    						if(!(--argc > 0 && *argv != NULL && **argv != '-')) {
d599e3
+    	                        Info(slide, 0x401, ((char *)slide,
d599e3
+        		                  "error:  a valid character encoding should follow the -I argument"));
d599e3
+    	                        return(PK_PARAM); 
d599e3
+    						}
d599e3
+    						s = *argv;
d599e3
+    						strncpy(ISO_CP, s, sizeof(ISO_CP));
d599e3
+    					}
d599e3
+    					while(*(++s)); /* No params straight after charset name */
d599e3
+    				}
d599e3
+    				break;
d599e3
+#endif /* ?UNIX */
d599e3
                 case 'l':      /* longer form of "ls -l" type listing */
d599e3
                     if (negative)
d599e3
                         uO.lflag = -2, negative = 0;
d599e3
@@ -521,6 +554,35 @@
d599e3
                         G.M_flag = TRUE;
d599e3
                     break;
d599e3
 #endif
d599e3
+#ifdef UNIX
d599e3
+    			case ('O'):
d599e3
+                    if (negative) {
d599e3
+                        Info(slide, 0x401, ((char *)slide,
d599e3
+                          "error:  encodings can't be negated"));
d599e3
+                        return(PK_PARAM);
d599e3
+    				} else {
d599e3
+    					if(*s) { /* Handle the -Ocharset case */
d599e3
+    						/* Assume that charsets can't start with a dash to spot arguments misuse */
d599e3
+    						if(*s == '-') { 
d599e3
+    	                        Info(slide, 0x401, ((char *)slide,
d599e3
+        		                  "error:  a valid character encoding should follow the -I argument"));
d599e3
+    	                        return(PK_PARAM); 
d599e3
+    						}
d599e3
+    						strncpy(OEM_CP, s, sizeof(OEM_CP));
d599e3
+    					} else { /* -O charset */
d599e3
+    						++argv;
d599e3
+    						if(!(--argc > 0 && *argv != NULL && **argv != '-')) {
d599e3
+    	                        Info(slide, 0x401, ((char *)slide,
d599e3
+        		                  "error:  a valid character encoding should follow the -O argument"));
d599e3
+    	                        return(PK_PARAM); 
d599e3
+    						}
d599e3
+    						s = *argv;
d599e3
+    						strncpy(OEM_CP, s, sizeof(OEM_CP));
d599e3
+    					}
d599e3
+    					while(*(++s)); /* No params straight after charset name */
d599e3
+    				}
d599e3
+    				break;
d599e3
+#endif /* ?UNIX */
d599e3
                 case 's':      /* default:  shorter "ls -l" type listing */
d599e3
                     if (negative)
d599e3
                         uO.lflag = -2, negative = 0;