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