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