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

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