953429
diff -up unzip60/match.c.recmatch unzip60/match.c
953429
--- unzip60/match.c.recmatch	2005-08-14 13:00:36.000000000 -0400
953429
+++ unzip60/match.c	2013-05-28 10:29:57.949077543 -0400
953429
@@ -27,16 +27,14 @@
953429
 
953429
   ---------------------------------------------------------------------------
953429
 
953429
-  Copyright on recmatch() from Zip's util.c (although recmatch() was almost
953429
-  certainly written by Mark Adler...ask me how I can tell :-) ):
953429
+  Copyright on recmatch() from Zip's util.c
953429
+	 Copyright (c) 1990-2005 Info-ZIP.  All rights reserved.
953429
 
953429
-     Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
953429
-     Kai Uwe Rommel and Igor Mandrichenko.
953429
+	 See the accompanying file LICENSE, version 2004-May-22 or later
953429
+	 for terms of use.
953429
+	 If, for some reason, both of these files are missing, the Info-ZIP license
953429
+	 also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html  
953429
 
953429
-     Permission is granted to any individual or institution to use, copy,
953429
-     or redistribute this software so long as all of the original files are
953429
-     included unmodified, that it is not sold for profit, and that this copy-
953429
-     right notice is retained.
953429
 
953429
   ---------------------------------------------------------------------------
953429
 
953429
@@ -53,7 +51,7 @@
953429
 
953429
   A set is composed of characters or ranges; a range looks like ``character
953429
   hyphen character'' (as in 0-9 or A-Z).  [0-9a-zA-Z_] is the minimal set of
953429
-  characters allowed in the [..] pattern construct.  Other characters are
953429
+  characters ALlowed in the [..] pattern construct.  Other characters are
953429
   allowed (i.e., 8-bit characters) if your system will support them.
953429
 
953429
   To suppress the special syntactic significance of any of ``[]*?!^-\'', in-
953429
@@ -101,8 +99,32 @@
953429
 #  define WILDCHAR   '?'
953429
 #  define BEG_RANGE  '['
953429
 #  define END_RANGE  ']'
953429
+#  define WILDCHR_SINGLE '?'
953429
+#  define DIRSEP_CHR '/'
953429
+#  define WILDCHR_MULTI '*'
953429
 #endif
953429
 
953429
+#ifdef WILD_STOP_AT_DIR
953429
+   int wild_stop_at_dir = 1; /* default wildcards do not include / in matches */
953429
+#else
953429
+   int wild_stop_at_dir = 0; /* default wildcards do include / in matches */
953429
+#endif
953429
+
953429
+
953429
+
953429
+/*
953429
+ * case mapping functions. case_map is used to ignore case in comparisons,
953429
+ * to_up is used to force upper case even on Unix (for dosify option).
953429
+ */
953429
+#ifdef USE_CASE_MAP
953429
+#  define case_map(c) upper[(c) & 0xff]
953429
+#  define to_up(c)    upper[(c) & 0xff]
953429
+#else
953429
+#  define case_map(c) (c)
953429
+#  define to_up(c)    ((c) >= 'a' && (c) <= 'z' ? (c)-'a'+'A' : (c))
953429
+#endif /* USE_CASE_MAP */
953429
+
953429
+
953429
 #if 0                /* GRR:  add this to unzip.h someday... */
953429
 #if !(defined(MSDOS) && defined(DOSWILD))
953429
 #ifdef WILD_STOP_AT_DIR
953429
@@ -114,8 +136,8 @@ int recmatch OF((ZCONST uch *pattern, ZC
953429
                  int ignore_case __WDLPRO));
953429
 #endif
953429
 #endif /* 0 */
953429
-static int recmatch OF((ZCONST uch *pattern, ZCONST uch *string,
953429
-                        int ignore_case __WDLPRO));
953429
+static int recmatch OF((ZCONST char *, ZCONST char *, 
953429
+                        int));
953429
 static char *isshexp OF((ZCONST char *p));
953429
 static int namecmp OF((ZCONST char *s1, ZCONST char *s2));
953429
 
953429
@@ -154,192 +176,240 @@ int match(string, pattern, ignore_case _
953429
             }
953429
             dospattern[j-1] = '\0';                    /* nuke the end "." */
953429
         }
953429
-        j = recmatch((uch *)dospattern, (uch *)string, ignore_case __WDL);
953429
+        j = recmatch(dospattern, string, ignore_case);
953429
         free(dospattern);
953429
         return j == 1;
953429
     } else
953429
 #endif /* MSDOS && DOSWILD */
953429
-    return recmatch((uch *)pattern, (uch *)string, ignore_case __WDL) == 1;
953429
+    return recmatch(pattern, string, ignore_case) == 1;
953429
 }
953429
 
953429
+#ifdef _MBCS
953429
+
953429
+char *___tmp_ptr;
953429
 
953429
+#endif
953429
 
953429
-static int recmatch(p, s, ic __WDL)
953429
-    ZCONST uch *p;        /* sh pattern to match */
953429
-    ZCONST uch *s;        /* string to which to match it */
953429
-    int ic;               /* true for case insensitivity */
953429
-    __WDLDEF              /* directory sepchar for WildStopAtDir mode, or 0 */
953429
+static int recmatch(p, s, cs)
953429
+ZCONST char *p;         /* sh pattern to match */
953429
+ZCONST char *s;         /* string to match it to */
953429
+int cs;                 /* flag: force case-sensitive matching */
953429
 /* Recursively compare the sh pattern p with the string s and return 1 if
953429
- * they match, and 0 or 2 if they don't or if there is a syntax error in the
953429
- * pattern.  This routine recurses on itself no more deeply than the number
953429
- * of characters in the pattern. */
953429
+   they match, and 0 or 2 if they don't or if there is a syntax error in the
953429
+   pattern.  This routine recurses on itself no deeper than the number of
953429
+   characters in the pattern. */
953429
 {
953429
-    unsigned int c;       /* pattern char or start of range in [-] loop */
953429
+  int c;                /* pattern char or start of range in [-] loop */
953429
+  /* Get first character, the pattern for new recmatch calls follows */
953429
+ /* borrowed from Zip's global.c */
953429
+ int no_wild = 0; 
953429
+ int allow_regex=1;
953429
+  /* This fix provided by akt@m5.dion.ne.jp for Japanese.
953429
+     See 21 July 2006 mail.
953429
+     It only applies when p is pointing to a doublebyte character and
953429
+     things like / and wildcards are not doublebyte.  This probably
953429
+     should not be needed. */
953429
 
953429
-    /* Get first character, the pattern for new recmatch calls follows */
953429
-    c = *p; INCSTR(p);
953429
+#ifdef _MBCS
953429
+  if (CLEN(p) == 2) {
953429
+    if (CLEN(s) == 2) {
953429
+      return (*p == *s && *(p+1) == *(s+1)) ?
953429
+        recmatch(p + 2, s + 2, cs) : 0;
953429
+    } else {
953429
+      return 0;
953429
+    }
953429
+  }
953429
+#endif /* ?_MBCS */
953429
 
953429
-    /* If that was the end of the pattern, match if string empty too */
953429
-    if (c == 0)
953429
-        return *s == 0;
953429
+  c = *POSTINCSTR(p);
953429
 
953429
-    /* '?' (or '%') matches any character (but not an empty string). */
953429
-    if (c == WILDCHAR)
953429
-#ifdef WILD_STOP_AT_DIR
953429
-        /* If uO.W_flag is non-zero, it won't match '/' */
953429
-        return (*s && (!sepc || *s != (uch)sepc))
953429
-               ? recmatch(p, s + CLEN(s), ic, sepc) : 0;
953429
-#else
953429
-        return *s ? recmatch(p, s + CLEN(s), ic) : 0;
953429
-#endif
953429
+  /* If that was the end of the pattern, match if string empty too */
953429
+  if (c == 0)
953429
+    return *s == 0;
953429
+
953429
+  /* '?' (or '%' or '#') matches any character (but not an empty string) */
953429
+  if (c == WILDCHR_SINGLE) {
953429
+    if (wild_stop_at_dir)
953429
+      return (*s && *s != DIRSEP_CHR) ? recmatch(p, s + CLEN(s), cs) : 0;
953429
+    else
953429
+      return *s ? recmatch(p, s + CLEN(s), cs) : 0;
953429
+  }
953429
 
953429
-    /* '*' matches any number of characters, including zero */
953429
+  /* WILDCHR_MULTI ('*') matches any number of characters, including zero */
953429
 #ifdef AMIGA
953429
-    if (c == '#' && *p == '?')     /* "#?" is Amiga-ese for "*" */
953429
-        c = '*', p++;
953429
+  if (!no_wild && c == '#' && *p == '?')            /* "#?" is Amiga-ese for "*" */
953429
+    c = WILDCHR_MULTI, p++;
953429
 #endif /* AMIGA */
953429
-    if (c == '*') {
953429
-#ifdef WILD_STOP_AT_DIR
953429
-        if (sepc) {
953429
-          /* check for single "*" or double "**" */
953429
-#  ifdef AMIGA
953429
-          if ((c = p[0]) == '#' && p[1] == '?') /* "#?" is Amiga-ese for "*" */
953429
-            c = '*', p++;
953429
-          if (c != '*') {
953429
-#  else /* !AMIGA */
953429
-          if (*p != '*') {
953429
-#  endif /* ?AMIGA */
953429
-            /* single "*": this doesn't match the dirsep character */
953429
-            for (; *s && *s != (uch)sepc; INCSTR(s))
953429
-                if ((c = recmatch(p, s, ic, sepc)) != 0)
953429
-                    return (int)c;
953429
-            /* end of pattern: matched if at end of string, else continue */
953429
-            if (*p == '\0')
953429
-                return (*s == 0);
953429
-            /* continue to match if at sepc in pattern, else give up */
953429
-            return (*p == (uch)sepc || (*p == '\\' && p[1] == (uch)sepc))
953429
-                   ? recmatch(p, s, ic, sepc) : 2;
953429
-          }
953429
-          /* "**": this matches slashes */
953429
-          ++p;        /* move p behind the second '*' */
953429
-          /* and continue with the non-W_flag code variant */
953429
-        }
953429
-#endif /* WILD_STOP_AT_DIR */
953429
+  if (!no_wild && c == WILDCHR_MULTI)
953429
+  {
953429
+    if (wild_stop_at_dir) {
953429
+      /* Check for an immediately following WILDCHR_MULTI */
953429
+# ifdef AMIGA
953429
+      if ((c = p[0]) == '#' && p[1] == '?') /* "#?" is Amiga-ese for "*" */
953429
+        c = WILDCHR_MULTI, p++;
953429
+      if (c != WILDCHR_MULTI) {
953429
+# else /* !AMIGA */
953429
+      if (*p != WILDCHR_MULTI) {
953429
+# endif /* ?AMIGA */
953429
+        /* Single WILDCHR_MULTI ('*'): this doesn't match slashes */
953429
+        for (; *s && *s != DIRSEP_CHR; INCSTR(s))
953429
+          if ((c = recmatch(p, s, cs)) != 0)
953429
+            return c;
953429
+        /* end of pattern: matched if at end of string, else continue */
953429
         if (*p == 0)
953429
-            return 1;
953429
-        if (isshexp((ZCONST char *)p) == NULL) {
953429
-            /* Optimization for rest of pattern being a literal string:
953429
-             * If there are no other shell expression chars in the rest
953429
-             * of the pattern behind the multi-char wildcard, then just
953429
-             * compare the literal string tail.
953429
-             */
953429
-            ZCONST uch *srest;
953429
-
953429
-            srest = s + (strlen((ZCONST char *)s) - strlen((ZCONST char *)p));
953429
-            if (srest - s < 0)
953429
-                /* remaining literal string from pattern is longer than rest
953429
-                 * of test string, there can't be a match
953429
-                 */
953429
-                return 0;
953429
-            else
953429
-              /* compare the remaining literal pattern string with the last
953429
-               * bytes of the test string to check for a match
953429
-               */
953429
+          return (*s == 0);
953429
+        /* continue to match if at DIRSEP_CHR in pattern, else give up */
953429
+        return (*p == DIRSEP_CHR || (*p == '\\' && p[1] == DIRSEP_CHR))
953429
+               ? recmatch(p, s, cs) : 2;
953429
+      }
953429
+      /* Two consecutive WILDCHR_MULTI ("**"): this matches DIRSEP_CHR ('/') */
953429
+      p++;        /* move p past the second WILDCHR_MULTI */
953429
+      /* continue with the normal non-WILD_STOP_AT_DIR code */
953429
+    } /* wild_stop_at_dir */
953429
+
953429
+    /* Not wild_stop_at_dir */
953429
+    if (*p == 0)
953429
+      return 1;
953429
+    if (!isshexp((char *)p))
953429
+    {
953429
+      /* optimization for rest of pattern being a literal string */
953429
+
953429
+      /* optimization to handle patterns like *.txt */
953429
+      /* if the first char in the pattern is '*' and there */
953429
+      /* are no other shell expression chars, i.e. a literal string */
953429
+      /* then just compare the literal string at the end */
953429
+
953429
+      ZCONST char *srest;
953429
+
953429
+      srest = s + (strlen(s) - strlen(p));
953429
+      if (srest - s < 0)
953429
+        /* remaining literal string from pattern is longer than rest of
953429
+           test string, there can't be a match
953429
+         */
953429
+        return 0;
953429
+      else
953429
+        /* compare the remaining literal pattern string with the last bytes
953429
+           of the test string to check for a match */
953429
 #ifdef _MBCS
953429
-            {
953429
-                ZCONST uch *q = s;
953429
+      {
953429
+        ZCONST char *q = s;
953429
 
953429
-                /* MBCS-aware code must not scan backwards into a string from
953429
-                 * the end.
953429
-                 * So, we have to move forward by character from our well-known
953429
-                 * character position s in the test string until we have
953429
-                 * advanced to the srest position.
953429
-                 */
953429
-                while (q < srest)
953429
-                  INCSTR(q);
953429
-                /* In case the byte *srest is a trailing byte of a multibyte
953429
-                 * character in the test string s, we have actually advanced
953429
-                 * past the position (srest).
953429
-                 * For this case, the match has failed!
953429
-                 */
953429
-                if (q != srest)
953429
-                    return 0;
953429
-                return ((ic
953429
-                         ? namecmp((ZCONST char *)p, (ZCONST char *)q)
953429
-                         : strcmp((ZCONST char *)p, (ZCONST char *)q)
953429
-                        ) == 0);
953429
-            }
953429
+        /* MBCS-aware code must not scan backwards into a string from
953429
+         * the end.
953429
+         * So, we have to move forward by character from our well-known
953429
+         * character position s in the test string until we have advanced
953429
+         * to the srest position.
953429
+         */
953429
+        while (q < srest)
953429
+          INCSTR(q);
953429
+        /* In case the byte *srest is a trailing byte of a multibyte
953429
+         * character, we have actually advanced past the position (srest).
953429
+         * For this case, the match has failed!
953429
+         */
953429
+        if (q != srest)
953429
+          return 0;
953429
+        return ((cs ? strcmp(p, q) : namecmp(p, q)) == 0);
953429
+      }
953429
 #else /* !_MBCS */
953429
-                return ((ic
953429
-                         ? namecmp((ZCONST char *)p, (ZCONST char *)srest)
953429
-                         : strcmp((ZCONST char *)p, (ZCONST char *)srest)
953429
-                        ) == 0);
953429
+        return ((cs ? strcmp(p, srest) : namecmp(p, srest)) == 0);
953429
 #endif /* ?_MBCS */
953429
-        } else {
953429
-            /* pattern contains more wildcards, continue with recursion... */
953429
-            for (; *s; INCSTR(s))
953429
-                if ((c = recmatch(p, s, ic __WDL)) != 0)
953429
-                    return (int)c;
953429
-            return 2;  /* 2 means give up--match will return false */
953429
-        }
953429
     }
953429
-
953429
-    /* Parse and process the list of characters and ranges in brackets */
953429
-    if (c == BEG_RANGE) {
953429
-        int e;          /* flag true if next char to be taken literally */
953429
-        ZCONST uch *q;  /* pointer to end of [-] group */
953429
-        int r;          /* flag true to match anything but the range */
953429
-
953429
-        if (*s == 0)                            /* need a character to match */
953429
-            return 0;
953429
-        p += (r = (*p == '!' || *p == '^'));    /* see if reverse */
953429
-        for (q = p, e = 0; *q; INCSTR(q))       /* find closing bracket */
953429
-            if (e)
953429
-                e = 0;
953429
-            else
953429
-                if (*q == '\\')      /* GRR:  change to ^ for MS-DOS, OS/2? */
953429
-                    e = 1;
953429
-                else if (*q == END_RANGE)
953429
-                    break;
953429
-        if (*q != END_RANGE)         /* nothing matches if bad syntax */
953429
-            return 0;
953429
-        for (c = 0, e = (*p == '-'); p < q; INCSTR(p)) {
953429
-            /* go through the list */
953429
-            if (!e && *p == '\\')               /* set escape flag if \ */
953429
-                e = 1;
953429
-            else if (!e && *p == '-')           /* set start of range if - */
953429
-                c = *(p-1);
953429
-            else {
953429
-                unsigned int cc = Case(*s);
953429
-
953429
-                if (*(p+1) != '-')
953429
-                    for (c = c ? c : *p; c <= *p; c++)  /* compare range */
953429
-                        if ((unsigned)Case(c) == cc) /* typecast for MSC bug */
953429
-                            return r ? 0 : recmatch(q + 1, s + 1, ic __WDL);
953429
-                c = e = 0;   /* clear range, escape flags */
953429
-            }
953429
-        }
953429
-        return r ? recmatch(q + CLEN(q), s + CLEN(s), ic __WDL) : 0;
953429
-                                        /* bracket match failed */
953429
+    else
953429
+    {
953429
+      /* pattern contains more wildcards, continue with recursion... */
953429
+      for (; *s; INCSTR(s))
953429
+        if ((c = recmatch(p, s, cs)) != 0)
953429
+          return c;
953429
+      return 2;           /* 2 means give up--shmatch will return false */
953429
     }
953429
+  }
953429
 
953429
-    /* if escape ('\\'), just compare next character */
953429
-    if (c == '\\' && (c = *p++) == 0)     /* if \ at end, then syntax error */
953429
-        return 0;
953429
+#ifndef VMS             /* No bracket matching in VMS */
953429
+  /* Parse and process the list of characters and ranges in brackets */
953429
+  if (!no_wild && allow_regex && c == '[')
953429
+  {
953429
+    int e;              /* flag true if next char to be taken literally */
953429
+    ZCONST char *q;     /* pointer to end of [-] group */
953429
+    int r;              /* flag true to match anything but the range */
953429
+
953429
+    if (*s == 0)                        /* need a character to match */
953429
+      return 0;
953429
+    p += (r = (*p == '!' || *p == '^')); /* see if reverse */
953429
+    for (q = p, e = 0; *q; q++)         /* find closing bracket */
953429
+      if (e)
953429
+        e = 0;
953429
+      else
953429
+        if (*q == '\\')
953429
+          e = 1;
953429
+        else if (*q == ']')
953429
+          break;
953429
+    if (*q != ']')                      /* nothing matches if bad syntax */
953429
+      return 0;
953429
+    for (c = 0, e = *p == '-'; p < q; p++)      /* go through the list */
953429
+    {
953429
+      if (e == 0 && *p == '\\')         /* set escape flag if \ */
953429
+        e = 1;
953429
+      else if (e == 0 && *p == '-')     /* set start of range if - */
953429
+        c = *(p-1);
953429
+      else
953429
+      {
953429
+        uch cc = (cs ? (uch)*s : case_map((uch)*s));
953429
+        uch uc = (uch) c;
953429
+        if (*(p+1) != '-')
953429
+          for (uc = uc ? uc : (uch)*p; uc <= (uch)*p; uc++)
953429
+            /* compare range */
953429
+            if ((cs ? uc : case_map(uc)) == cc)
953429
+              return r ? 0 : recmatch(q + CLEN(q), s + CLEN(s), cs);
953429
+        c = e = 0;                      /* clear range, escape flags */
953429
+      }
953429
+    }
953429
+    return r ? recmatch(q + CLEN(q), s + CLEN(s), cs) : 0;
953429
+                                        /* bracket match failed */
953429
+  }
953429
+#endif /* !VMS */
953429
 
953429
-    /* just a character--compare it */
953429
-#ifdef QDOS
953429
-    return QMatch(Case((uch)c), Case(*s)) ?
953429
-           recmatch(p, s + CLEN(s), ic __WDL) : 0;
953429
-#else
953429
-    return Case((uch)c) == Case(*s) ?
953429
-           recmatch(p, s + CLEN(s), ic __WDL) : 0;
953429
-#endif
953429
+  /* If escape ('\'), just compare next character */
953429
+  if (!no_wild && c == '\\')
953429
+    if ((c = *p++) == '\0')             /* if \ at end, then syntax error */
953429
+      return 0;
953429
+
953429
+#ifdef VMS
953429
+  /* 2005-11-06 SMS.
953429
+     Handle "..." wildcard in p with "." or "]" in s.
953429
+  */
953429
+  if ((c == '.') && (*p == '.') && (*(p+ CLEN( p)) == '.') &&
953429
+   ((*s == '.') || (*s == ']')))
953429
+  {
953429
+    /* Match "...]" with "]".  Continue after "]" in both. */
953429
+    if ((*(p+ 2* CLEN( p)) == ']') && (*s == ']'))
953429
+      return recmatch( (p+ 3* CLEN( p)), (s+ CLEN( s)), cs);
953429
+
953429
+    /* Else, look for a reduced match in s, until "]" in or end of s. */
953429
+    for (; *s && (*s != ']'); INCSTR(s))
953429
+      if (*s == '.')
953429
+        /* If reduced match, then continue after "..." in p, "." in s. */
953429
+        if ((c = recmatch( (p+ CLEN( p)), s, cs)) != 0)
953429
+          return (int)c;
953429
+
953429
+    /* Match "...]" with "]".  Continue after "]" in both. */
953429
+    if ((*(p+ 2* CLEN( p)) == ']') && (*s == ']'))
953429
+      return recmatch( (p+ 3* CLEN( p)), (s+ CLEN( s)), cs);
953429
+
953429
+    /* No reduced match.  Quit. */
953429
+    return 2;
953429
+  }
953429
+
953429
+#endif /* def VMS */
953429
+
953429
+  /* Just a character--compare it */
953429
+  return (cs ? c == *s : case_map((uch)c) == case_map((uch)*s)) ?
953429
+          recmatch(p, s + CLEN(s), cs) : 0;
953429
+}
953429
 
953429
-} /* end function recmatch() */
953429
 
953429
 
953429
 
953429
+/*************************************************************************************************/
953429
 static char *isshexp(p)
953429
 ZCONST char *p;
953429
 /* If p is a sh expression, a pointer to the first special character is