Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

match.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. Copyright (c) 1990-2005 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2000-Apr-09 or later
  4. (the contents of which are also included in unzip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /*---------------------------------------------------------------------------
  9. match.c
  10. The match() routine recursively compares a string to a "pattern" (regular
  11. expression), returning TRUE if a match is found or FALSE if not. This
  12. version is specifically for use with unzip.c: as did the previous match()
  13. routines from SEA and J. Kercheval, it leaves the case (upper, lower, or
  14. mixed) of the string alone, but converts any uppercase characters in the
  15. pattern to lowercase if indicated by the global var pInfo->lcflag (which
  16. is to say, string is assumed to have been converted to lowercase already,
  17. if such was necessary).
  18. GRR: reversed order of text, pattern in matche() (now same as match());
  19. added ignore_case/ic flags, Case() macro.
  20. PaulK: replaced matche() with recmatch() from Zip, modified to have an
  21. ignore_case argument; replaced test frame with simpler one.
  22. ---------------------------------------------------------------------------
  23. Copyright on recmatch() from Zip's util.c (although recmatch() was almost
  24. certainly written by Mark Adler...ask me how I can tell :-) ):
  25. Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  26. Kai Uwe Rommel and Igor Mandrichenko.
  27. Permission is granted to any individual or institution to use, copy,
  28. or redistribute this software so long as all of the original files are
  29. included unmodified, that it is not sold for profit, and that this copy-
  30. right notice is retained.
  31. ---------------------------------------------------------------------------
  32. Match the pattern (wildcard) against the string (fixed):
  33. match(string, pattern, ignore_case, sepc);
  34. returns TRUE if string matches pattern, FALSE otherwise. In the pattern:
  35. `*' matches any sequence of characters (zero or more)
  36. `?' matches any single character
  37. [SET] matches any character in the specified set,
  38. [!SET] or [^SET] matches any character not in the specified set.
  39. A set is composed of characters or ranges; a range looks like ``character
  40. hyphen character'' (as in 0-9 or A-Z). [0-9a-zA-Z_] is the minimal set of
  41. characters allowed in the [..] pattern construct. Other characters are
  42. allowed (i.e., 8-bit characters) if your system will support them.
  43. To suppress the special syntactic significance of any of ``[]*?!^-\'', in-
  44. side or outside a [..] construct, and match the character exactly, precede
  45. it with a ``\'' (backslash).
  46. Note that "*.*" and "*." are treated specially under MS-DOS if DOSWILD is
  47. defined. See the DOSWILD section below for an explanation. Note also
  48. that with VMSWILD defined, '%' is used instead of '?', and sets (ranges)
  49. are delimited by () instead of [].
  50. ---------------------------------------------------------------------------*/
  51. #define __MATCH_C /* identifies this source module */
  52. /* define ToLower() in here (for Unix, define ToLower to be macro (using
  53. * isupper()); otherwise just use tolower() */
  54. #define UNZIP_INTERNAL
  55. #include "unzip.h"
  56. #ifndef THEOS /* the Theos port defines its own variant of match() */
  57. #if 0 /* this is not useful until it matches Amiga names insensitively */
  58. #ifdef AMIGA /* some other platforms might also want to use this */
  59. # define ANSI_CHARSET /* MOVE INTO UNZIP.H EVENTUALLY */
  60. #endif
  61. #endif /* 0 */
  62. #ifdef ANSI_CHARSET
  63. # ifdef ToLower
  64. # undef ToLower
  65. # endif
  66. /* uppercase letters are values 41 thru 5A, C0 thru D6, and D8 thru DE */
  67. # define IsUpper(c) (c>=0xC0 ? c<=0xDE && c!=0xD7 : c>=0x41 && c<=0x5A)
  68. # define ToLower(c) (IsUpper((uch) c) ? (unsigned) c | 0x20 : (unsigned) c)
  69. #endif
  70. #define Case(x) (ic? ToLower(x) : (x))
  71. #ifdef VMSWILD
  72. # define WILDCHAR '%'
  73. # define BEG_RANGE '('
  74. # define END_RANGE ')'
  75. #else
  76. # define WILDCHAR '?'
  77. # define BEG_RANGE '['
  78. # define END_RANGE ']'
  79. #endif
  80. #if 0 /* GRR: add this to unzip.h someday... */
  81. #if !(defined(MSDOS) && defined(DOSWILD))
  82. #ifdef WILD_STOP_AT_DIR
  83. #define match(s,p,ic,sc) (recmatch((ZCONST uch *)p,(ZCONST uch *)s,ic,sc) == 1)
  84. #else
  85. #define match(s,p,ic) (recmatch((ZCONST uch *)p,(ZCONST uch *)s,ic) == 1)
  86. #endif
  87. int recmatch OF((ZCONST uch *pattern, ZCONST uch *string,
  88. int ignore_case __WDLPRO));
  89. #endif
  90. #endif /* 0 */
  91. static int recmatch OF((ZCONST uch *pattern, ZCONST uch *string,
  92. int ignore_case __WDLPRO));
  93. static char *isshexp OF((ZCONST char *p));
  94. static int namecmp OF((ZCONST char *s1, ZCONST char *s2));
  95. /* match() is a shell to recmatch() to return only Boolean values. */
  96. int match(string, pattern, ignore_case __WDL)
  97. ZCONST char *string, *pattern;
  98. int ignore_case;
  99. __WDLDEF
  100. {
  101. #if (defined(MSDOS) && defined(DOSWILD))
  102. char *dospattern;
  103. int j = strlen(pattern);
  104. /*---------------------------------------------------------------------------
  105. Optional MS-DOS preprocessing section: compare last three chars of the
  106. wildcard to "*.*" and translate to "*" if found; else compare the last
  107. two characters to "*." and, if found, scan the non-wild string for dots.
  108. If in the latter case a dot is found, return failure; else translate the
  109. "*." to "*". In either case, continue with the normal (Unix-like) match
  110. procedure after translation. (If not enough memory, default to normal
  111. match.) This causes "a*.*" and "a*." to behave as MS-DOS users expect.
  112. ---------------------------------------------------------------------------*/
  113. if ((dospattern = (char *)malloc(j+1)) != NULL) {
  114. strcpy(dospattern, pattern);
  115. if (!strcmp(dospattern+j-3, "*.*")) {
  116. dospattern[j-2] = '\0'; /* nuke the ".*" */
  117. } else if (!strcmp(dospattern+j-2, "*.")) {
  118. char *p = MBSCHR(string, '.');
  119. if (p) { /* found a dot: match fails */
  120. free(dospattern);
  121. return 0;
  122. }
  123. dospattern[j-1] = '\0'; /* nuke the end "." */
  124. }
  125. j = recmatch((uch *)dospattern, (uch *)string, ignore_case __WDL);
  126. free(dospattern);
  127. return j == 1;
  128. } else
  129. #endif /* MSDOS && DOSWILD */
  130. return recmatch((uch *)pattern, (uch *)string, ignore_case __WDL) == 1;
  131. }
  132. static int recmatch(p, s, ic __WDL)
  133. ZCONST uch *p; /* sh pattern to match */
  134. ZCONST uch *s; /* string to which to match it */
  135. int ic; /* true for case insensitivity */
  136. __WDLDEF /* directory sepchar for WildStopAtDir mode, or 0 */
  137. /* Recursively compare the sh pattern p with the string s and return 1 if
  138. * they match, and 0 or 2 if they don't or if there is a syntax error in the
  139. * pattern. This routine recurses on itself no more deeply than the number
  140. * of characters in the pattern. */
  141. {
  142. unsigned int c; /* pattern char or start of range in [-] loop */
  143. /* Get first character, the pattern for new recmatch calls follows */
  144. c = *p; INCSTR(p);
  145. /* If that was the end of the pattern, match if string empty too */
  146. if (c == 0)
  147. return *s == 0;
  148. /* '?' (or '%') matches any character (but not an empty string). */
  149. if (c == WILDCHAR)
  150. #ifdef WILD_STOP_AT_DIR
  151. /* If uO.W_flag is non-zero, it won't match '/' */
  152. return (*s && (!sepc || *s != (uch)sepc))
  153. ? recmatch(p, s + CLEN(s), ic, sepc) : 0;
  154. #else
  155. return *s ? recmatch(p, s + CLEN(s), ic) : 0;
  156. #endif
  157. /* '*' matches any number of characters, including zero */
  158. #ifdef AMIGA
  159. if (c == '#' && *p == '?') /* "#?" is Amiga-ese for "*" */
  160. c = '*', p++;
  161. #endif /* AMIGA */
  162. if (c == '*') {
  163. #ifdef WILD_STOP_AT_DIR
  164. if (sepc) {
  165. /* check for single "*" or double "**" */
  166. # ifdef AMIGA
  167. if ((c = p[0]) == '#' && p[1] == '?') /* "#?" is Amiga-ese for "*" */
  168. c = '*', p++;
  169. if (c != '*') {
  170. # else /* !AMIGA */
  171. if (*p != '*') {
  172. # endif /* ?AMIGA */
  173. /* single "*": this doesn't match the dirsep character */
  174. for (; *s && *s != (uch)sepc; INCSTR(s))
  175. if ((c = recmatch(p, s, ic, sepc)) != 0)
  176. return (int)c;
  177. /* end of pattern: matched if at end of string, else continue */
  178. if (*p == '\0')
  179. return (*s == 0);
  180. /* continue to match if at sepc in pattern, else give up */
  181. return (*p == (uch)sepc || (*p == '\\' && p[1] == (uch)sepc))
  182. ? recmatch(p, s, ic, sepc) : 2;
  183. }
  184. /* "**": this matches slashes */
  185. ++p; /* move p behind the second '*' */
  186. /* and continue with the non-W_flag code variant */
  187. }
  188. #endif /* WILD_STOP_AT_DIR */
  189. if (*p == 0)
  190. return 1;
  191. if (isshexp((ZCONST char *)p) == NULL) {
  192. /* Optimization for rest of pattern being a literal string:
  193. * If there are no other shell expression chars in the rest
  194. * of the pattern behind the multi-char wildcard, then just
  195. * compare the literal string tail.
  196. */
  197. ZCONST uch *srest;
  198. srest = s + (strlen((ZCONST char *)s) - strlen((ZCONST char *)p));
  199. if (srest - s < 0)
  200. /* remaining literal string from pattern is longer than rest
  201. * of test string, there can't be a match
  202. */
  203. return 0;
  204. else
  205. /* compare the remaining literal pattern string with the last
  206. * bytes of the test string to check for a match
  207. */
  208. #ifdef _MBCS
  209. {
  210. ZCONST uch *q = s;
  211. /* MBCS-aware code must not scan backwards into a string from
  212. * the end.
  213. * So, we have to move forward by character from our well-known
  214. * character position s in the test string until we have
  215. * advanced to the srest position.
  216. */
  217. while (q < srest)
  218. INCSTR(q);
  219. /* In case the byte *srest is a trailing byte of a multibyte
  220. * character in the test string s, we have actually advanced
  221. * past the position (srest).
  222. * For this case, the match has failed!
  223. */
  224. if (q != srest)
  225. return 0;
  226. return ((ic
  227. ? namecmp((ZCONST char *)p, (ZCONST char *)q)
  228. : strcmp((ZCONST char *)p, (ZCONST char *)q)
  229. ) == 0);
  230. }
  231. #else /* !_MBCS */
  232. return ((ic
  233. ? namecmp((ZCONST char *)p, (ZCONST char *)srest)
  234. : strcmp((ZCONST char *)p, (ZCONST char *)srest)
  235. ) == 0);
  236. #endif /* ?_MBCS */
  237. } else {
  238. /* pattern contains more wildcards, continue with recursion... */
  239. for (; *s; INCSTR(s))
  240. if ((c = recmatch(p, s, ic __WDL)) != 0)
  241. return (int)c;
  242. return 2; /* 2 means give up--match will return false */
  243. }
  244. }
  245. /* Parse and process the list of characters and ranges in brackets */
  246. if (c == BEG_RANGE) {
  247. int e; /* flag true if next char to be taken literally */
  248. ZCONST uch *q; /* pointer to end of [-] group */
  249. int r; /* flag true to match anything but the range */
  250. if (*s == 0) /* need a character to match */
  251. return 0;
  252. p += (r = (*p == '!' || *p == '^')); /* see if reverse */
  253. for (q = p, e = 0; *q; INCSTR(q)) /* find closing bracket */
  254. if (e)
  255. e = 0;
  256. else
  257. if (*q == '\\') /* GRR: change to ^ for MS-DOS, OS/2? */
  258. e = 1;
  259. else if (*q == END_RANGE)
  260. break;
  261. if (*q != END_RANGE) /* nothing matches if bad syntax */
  262. return 0;
  263. for (c = 0, e = (*p == '-'); p < q; INCSTR(p)) {
  264. /* go through the list */
  265. if (!e && *p == '\\') /* set escape flag if \ */
  266. e = 1;
  267. else if (!e && *p == '-') /* set start of range if - */
  268. c = *(p-1);
  269. else {
  270. unsigned int cc = Case(*s);
  271. if (*(p+1) != '-')
  272. for (c = c ? c : *p; c <= *p; c++) /* compare range */
  273. if ((unsigned)Case(c) == cc) /* typecast for MSC bug */
  274. return r ? 0 : recmatch(q + 1, s + 1, ic __WDL);
  275. c = e = 0; /* clear range, escape flags */
  276. }
  277. }
  278. return r ? recmatch(q + CLEN(q), s + CLEN(s), ic __WDL) : 0;
  279. /* bracket match failed */
  280. }
  281. /* if escape ('\\'), just compare next character */
  282. if (c == '\\' && (c = *p++) == 0) /* if \ at end, then syntax error */
  283. return 0;
  284. /* just a character--compare it */
  285. #ifdef QDOS
  286. return QMatch(Case((uch)c), Case(*s)) ?
  287. recmatch(p, s + CLEN(s), ic __WDL) : 0;
  288. #else
  289. return Case((uch)c) == Case(*s) ?
  290. recmatch(p, s + CLEN(s), ic __WDL) : 0;
  291. #endif
  292. } /* end function recmatch() */
  293. static char *isshexp(p)
  294. ZCONST char *p;
  295. /* If p is a sh expression, a pointer to the first special character is
  296. returned. Otherwise, NULL is returned. */
  297. {
  298. for (; *p; INCSTR(p))
  299. if (*p == '\\' && *(p+1))
  300. p++;
  301. else if (*p == WILDCHAR || *p == '*' || *p == BEG_RANGE)
  302. return (char *)p;
  303. return NULL;
  304. } /* end function isshexp() */
  305. static int namecmp(s1, s2)
  306. ZCONST char *s1, *s2;
  307. {
  308. int d;
  309. for (;;) {
  310. d = (int)ToLower((uch)*s1)
  311. - (int)ToLower((uch)*s2);
  312. if (d || *s1 == 0 || *s2 == 0)
  313. return d;
  314. s1++;
  315. s2++;
  316. }
  317. } /* end function namecmp() */
  318. #endif /* !THEOS */
  319. int iswild(p) /* originally only used for stat()-bug workaround in */
  320. ZCONST char *p; /* VAX C, Turbo/Borland C, Watcom C, Atari MiNT libs; */
  321. { /* now used in process_zipfiles() as well */
  322. for (; *p; INCSTR(p))
  323. if (*p == '\\' && *(p+1))
  324. ++p;
  325. #ifdef THEOS
  326. else if (*p == '?' || *p == '*' || *p=='#'|| *p == '@')
  327. #else /* !THEOS */
  328. #ifdef VMS
  329. else if (*p == '%' || *p == '*')
  330. #else /* !VMS */
  331. #ifdef AMIGA
  332. else if (*p == '?' || *p == '*' || (*p=='#' && p[1]=='?') || *p == '[')
  333. #else /* !AMIGA */
  334. else if (*p == '?' || *p == '*' || *p == '[')
  335. #endif /* ?AMIGA */
  336. #endif /* ?VMS */
  337. #endif /* ?THEOS */
  338. #ifdef QDOS
  339. return (int)p;
  340. #else
  341. return TRUE;
  342. #endif
  343. return FALSE;
  344. } /* end function iswild() */
  345. #ifdef TEST_MATCH
  346. #define put(s) {fputs(s,stdout); fflush(stdout);}
  347. #ifdef main
  348. # undef main
  349. #endif
  350. int main(int argc, char **argv)
  351. {
  352. char pat[256], str[256];
  353. for (;;) {
  354. put("Pattern (return to exit): ");
  355. gets(pat);
  356. if (!pat[0])
  357. break;
  358. for (;;) {
  359. put("String (return for new pattern): ");
  360. gets(str);
  361. if (!str[0])
  362. break;
  363. printf("Case sensitive: %s insensitive: %s\n",
  364. match(str, pat, 0) ? "YES" : "NO",
  365. match(str, pat, 1) ? "YES" : "NO");
  366. }
  367. }
  368. EXIT(0);
  369. }
  370. #endif /* TEST_MATCH */