pcre2_convert.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Original API code Copyright (c) 1997-2012 University of Cambridge
  8. New API code Copyright (c) 2016-2017 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. #include "pcre2_internal.h"
  37. #define TYPE_OPTIONS (PCRE2_CONVERT_GLOB| \
  38. PCRE2_CONVERT_POSIX_BASIC|PCRE2_CONVERT_POSIX_EXTENDED)
  39. #define ALL_OPTIONS (PCRE2_CONVERT_UTF|PCRE2_CONVERT_NO_UTF_CHECK| \
  40. PCRE2_CONVERT_GLOB_NO_WILD_SEPARATOR| \
  41. PCRE2_CONVERT_GLOB_NO_STARSTAR| \
  42. TYPE_OPTIONS)
  43. #define DUMMY_BUFFER_SIZE 100
  44. /* Generated pattern fragments */
  45. #define STR_BACKSLASH_A STR_BACKSLASH STR_A
  46. #define STR_BACKSLASH_z STR_BACKSLASH STR_z
  47. #define STR_COLON_RIGHT_SQUARE_BRACKET STR_COLON STR_RIGHT_SQUARE_BRACKET
  48. #define STR_DOT_STAR_LOOKBEHIND STR_DOT STR_ASTERISK STR_LEFT_PARENTHESIS STR_QUESTION_MARK STR_LESS_THAN_SIGN STR_EQUALS_SIGN
  49. #define STR_LOOKAHEAD_NOT_DOT STR_LEFT_PARENTHESIS STR_QUESTION_MARK STR_EXCLAMATION_MARK STR_BACKSLASH STR_DOT STR_RIGHT_PARENTHESIS
  50. #define STR_QUERY_s STR_LEFT_PARENTHESIS STR_QUESTION_MARK STR_s STR_RIGHT_PARENTHESIS
  51. #define STR_STAR_NUL STR_LEFT_PARENTHESIS STR_ASTERISK STR_N STR_U STR_L STR_RIGHT_PARENTHESIS
  52. /* States for range and POSIX processing */
  53. enum { RANGE_NOT_STARTED, RANGE_STARTING, RANGE_STARTED };
  54. enum { POSIX_START_REGEX, POSIX_ANCHORED, POSIX_NOT_BRACKET,
  55. POSIX_CLASS_NOT_STARTED, POSIX_CLASS_STARTING, POSIX_CLASS_STARTED };
  56. /* Macro to add a character string to the output buffer, checking for overflow. */
  57. #define PUTCHARS(string) \
  58. { \
  59. for (s = (char *)(string); *s != 0; s++) \
  60. { \
  61. if (p >= endp) return PCRE2_ERROR_NOMEMORY; \
  62. *p++ = *s; \
  63. } \
  64. }
  65. /* Literals that must be escaped: \ ? * + | . ^ $ { } [ ] ( ) */
  66. static const char *pcre2_escaped_literals =
  67. STR_BACKSLASH STR_QUESTION_MARK STR_ASTERISK STR_PLUS
  68. STR_VERTICAL_LINE STR_DOT STR_CIRCUMFLEX_ACCENT STR_DOLLAR_SIGN
  69. STR_LEFT_CURLY_BRACKET STR_RIGHT_CURLY_BRACKET
  70. STR_LEFT_SQUARE_BRACKET STR_RIGHT_SQUARE_BRACKET
  71. STR_LEFT_PARENTHESIS STR_RIGHT_PARENTHESIS;
  72. /* Recognized escaped metacharacters in POSIX basic patterns. */
  73. static const char *posix_meta_escapes =
  74. STR_LEFT_PARENTHESIS STR_RIGHT_PARENTHESIS
  75. STR_LEFT_CURLY_BRACKET STR_RIGHT_CURLY_BRACKET
  76. STR_1 STR_2 STR_3 STR_4 STR_5 STR_6 STR_7 STR_8 STR_9;
  77. /*************************************************
  78. * Convert a POSIX pattern *
  79. *************************************************/
  80. /* This function handles both basic and extended POSIX patterns.
  81. Arguments:
  82. pattype the pattern type
  83. pattern the pattern
  84. plength length in code units
  85. utf TRUE if UTF
  86. use_buffer where to put the output
  87. use_length length of use_buffer
  88. bufflenptr where to put the used length
  89. dummyrun TRUE if a dummy run
  90. ccontext the convert context
  91. Returns: 0 => success
  92. !0 => error code
  93. */
  94. static int
  95. convert_posix(uint32_t pattype, PCRE2_SPTR pattern, PCRE2_SIZE plength,
  96. BOOL utf, PCRE2_UCHAR *use_buffer, PCRE2_SIZE use_length,
  97. PCRE2_SIZE *bufflenptr, BOOL dummyrun, pcre2_convert_context *ccontext)
  98. {
  99. char *s;
  100. PCRE2_SPTR posix = pattern;
  101. PCRE2_UCHAR *p = use_buffer;
  102. PCRE2_UCHAR *pp = p;
  103. PCRE2_UCHAR *endp = p + use_length - 1; /* Allow for trailing zero */
  104. PCRE2_SIZE convlength = 0;
  105. uint32_t bracount = 0;
  106. uint32_t posix_state = POSIX_START_REGEX;
  107. uint32_t lastspecial = 0;
  108. BOOL extended = (pattype & PCRE2_CONVERT_POSIX_EXTENDED) != 0;
  109. BOOL nextisliteral = FALSE;
  110. (void)utf; /* Not used when Unicode not supported */
  111. (void)ccontext; /* Not currently used */
  112. /* Initialize default for error offset as end of input. */
  113. *bufflenptr = plength;
  114. PUTCHARS(STR_STAR_NUL);
  115. /* Now scan the input. */
  116. while (plength > 0)
  117. {
  118. uint32_t c, sc;
  119. int clength = 1;
  120. /* Add in the length of the last item, then, if in the dummy run, pull the
  121. pointer back to the start of the (temporary) buffer and then remember the
  122. start of the next item. */
  123. convlength += p - pp;
  124. if (dummyrun) p = use_buffer;
  125. pp = p;
  126. /* Pick up the next character */
  127. #ifndef SUPPORT_UNICODE
  128. c = *posix;
  129. #else
  130. GETCHARLENTEST(c, posix, clength);
  131. #endif
  132. posix += clength;
  133. plength -= clength;
  134. sc = nextisliteral? 0 : c;
  135. nextisliteral = FALSE;
  136. /* Handle a character within a class. */
  137. if (posix_state >= POSIX_CLASS_NOT_STARTED)
  138. {
  139. if (c == CHAR_RIGHT_SQUARE_BRACKET)
  140. {
  141. PUTCHARS(STR_RIGHT_SQUARE_BRACKET);
  142. posix_state = POSIX_NOT_BRACKET;
  143. }
  144. /* Not the end of the class */
  145. else
  146. {
  147. switch (posix_state)
  148. {
  149. case POSIX_CLASS_STARTED:
  150. if (c <= 127 && islower(c)) break; /* Remain in started state */
  151. posix_state = POSIX_CLASS_NOT_STARTED;
  152. if (c == CHAR_COLON && plength > 0 &&
  153. *posix == CHAR_RIGHT_SQUARE_BRACKET)
  154. {
  155. PUTCHARS(STR_COLON_RIGHT_SQUARE_BRACKET);
  156. plength--;
  157. posix++;
  158. continue; /* With next character after :] */
  159. }
  160. /* Fall through */
  161. case POSIX_CLASS_NOT_STARTED:
  162. if (c == CHAR_LEFT_SQUARE_BRACKET)
  163. posix_state = POSIX_CLASS_STARTING;
  164. break;
  165. case POSIX_CLASS_STARTING:
  166. if (c == CHAR_COLON) posix_state = POSIX_CLASS_STARTED;
  167. break;
  168. }
  169. if (c == CHAR_BACKSLASH) PUTCHARS(STR_BACKSLASH);
  170. if (p + clength > endp) return PCRE2_ERROR_NOMEMORY;
  171. memcpy(p, posix - clength, CU2BYTES(clength));
  172. p += clength;
  173. }
  174. }
  175. /* Handle a character not within a class. */
  176. else switch(sc)
  177. {
  178. case CHAR_LEFT_SQUARE_BRACKET:
  179. PUTCHARS(STR_LEFT_SQUARE_BRACKET);
  180. #ifdef NEVER
  181. /* We could handle special cases [[:<:]] and [[:>:]] (which PCRE does
  182. support) but they are not part of POSIX 1003.1. */
  183. if (plength >= 6)
  184. {
  185. if (posix[0] == CHAR_LEFT_SQUARE_BRACKET &&
  186. posix[1] == CHAR_COLON &&
  187. (posix[2] == CHAR_LESS_THAN_SIGN ||
  188. posix[2] == CHAR_GREATER_THAN_SIGN) &&
  189. posix[3] == CHAR_COLON &&
  190. posix[4] == CHAR_RIGHT_SQUARE_BRACKET &&
  191. posix[5] == CHAR_RIGHT_SQUARE_BRACKET)
  192. {
  193. if (p + 6 > endp) return PCRE2_ERROR_NOMEMORY;
  194. memcpy(p, posix, CU2BYTES(6));
  195. p += 6;
  196. posix += 6;
  197. plength -= 6;
  198. continue; /* With next character */
  199. }
  200. }
  201. #endif
  202. /* Handle start of "normal" character classes */
  203. posix_state = POSIX_CLASS_NOT_STARTED;
  204. /* Handle ^ and ] as first characters */
  205. if (plength > 0)
  206. {
  207. if (*posix == CHAR_CIRCUMFLEX_ACCENT)
  208. {
  209. posix++;
  210. plength--;
  211. PUTCHARS(STR_CIRCUMFLEX_ACCENT);
  212. }
  213. if (plength > 0 && *posix == CHAR_RIGHT_SQUARE_BRACKET)
  214. {
  215. posix++;
  216. plength--;
  217. PUTCHARS(STR_RIGHT_SQUARE_BRACKET);
  218. }
  219. }
  220. break;
  221. case CHAR_BACKSLASH:
  222. if (plength <= 0) return PCRE2_ERROR_END_BACKSLASH;
  223. if (extended) nextisliteral = TRUE; else
  224. {
  225. if (*posix < 127 && strchr(posix_meta_escapes, *posix) != NULL)
  226. {
  227. if (isdigit(*posix)) PUTCHARS(STR_BACKSLASH);
  228. if (p + 1 > endp) return PCRE2_ERROR_NOMEMORY;
  229. lastspecial = *p++ = *posix++;
  230. plength--;
  231. }
  232. else nextisliteral = TRUE;
  233. }
  234. break;
  235. case CHAR_RIGHT_PARENTHESIS:
  236. if (!extended || bracount == 0) goto ESCAPE_LITERAL;
  237. bracount--;
  238. goto COPY_SPECIAL;
  239. case CHAR_LEFT_PARENTHESIS:
  240. bracount++;
  241. /* Fall through */
  242. case CHAR_QUESTION_MARK:
  243. case CHAR_PLUS:
  244. case CHAR_LEFT_CURLY_BRACKET:
  245. case CHAR_RIGHT_CURLY_BRACKET:
  246. case CHAR_VERTICAL_LINE:
  247. if (!extended) goto ESCAPE_LITERAL;
  248. /* Fall through */
  249. case CHAR_DOT:
  250. case CHAR_DOLLAR_SIGN:
  251. posix_state = POSIX_NOT_BRACKET;
  252. COPY_SPECIAL:
  253. lastspecial = c;
  254. if (p + 1 > endp) return PCRE2_ERROR_NOMEMORY;
  255. *p++ = c;
  256. break;
  257. case CHAR_ASTERISK:
  258. if (lastspecial != CHAR_ASTERISK)
  259. {
  260. if (!extended && (posix_state < POSIX_NOT_BRACKET ||
  261. lastspecial == CHAR_LEFT_PARENTHESIS))
  262. goto ESCAPE_LITERAL;
  263. goto COPY_SPECIAL;
  264. }
  265. break; /* Ignore second and subsequent asterisks */
  266. case CHAR_CIRCUMFLEX_ACCENT:
  267. if (extended) goto COPY_SPECIAL;
  268. if (posix_state == POSIX_START_REGEX ||
  269. lastspecial == CHAR_LEFT_PARENTHESIS)
  270. {
  271. posix_state = POSIX_ANCHORED;
  272. goto COPY_SPECIAL;
  273. }
  274. /* Fall through */
  275. default:
  276. if (c < 128 && strchr(pcre2_escaped_literals, c) != NULL)
  277. {
  278. ESCAPE_LITERAL:
  279. PUTCHARS(STR_BACKSLASH);
  280. }
  281. lastspecial = 0xff; /* Indicates nothing special */
  282. if (p + clength > endp) return PCRE2_ERROR_NOMEMORY;
  283. memcpy(p, posix - clength, CU2BYTES(clength));
  284. p += clength;
  285. posix_state = POSIX_NOT_BRACKET;
  286. break;
  287. }
  288. }
  289. if (posix_state >= POSIX_CLASS_NOT_STARTED)
  290. return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
  291. convlength += p - pp; /* Final segment */
  292. *bufflenptr = convlength;
  293. *p++ = 0;
  294. return 0;
  295. }
  296. /*************************************************
  297. * Convert a glob pattern *
  298. *************************************************/
  299. /* Context for writing the output into a buffer. */
  300. typedef struct pcre2_output_context {
  301. PCRE2_UCHAR *output; /* current output position */
  302. PCRE2_SPTR output_end; /* output end */
  303. PCRE2_SIZE output_size; /* size of the output */
  304. uint8_t out_str[8]; /* string copied to the output */
  305. } pcre2_output_context;
  306. /* Write a character into the output.
  307. Arguments:
  308. out output context
  309. chr the next character
  310. */
  311. static void
  312. convert_glob_write(pcre2_output_context *out, PCRE2_UCHAR chr)
  313. {
  314. out->output_size++;
  315. if (out->output < out->output_end)
  316. *out->output++ = chr;
  317. }
  318. /* Write a string into the output.
  319. Arguments:
  320. out output context
  321. length length of out->out_str
  322. */
  323. static void
  324. convert_glob_write_str(pcre2_output_context *out, PCRE2_SIZE length)
  325. {
  326. uint8_t *out_str = out->out_str;
  327. PCRE2_UCHAR *output = out->output;
  328. PCRE2_SPTR output_end = out->output_end;
  329. PCRE2_SIZE output_size = out->output_size;
  330. do
  331. {
  332. output_size++;
  333. if (output < output_end)
  334. *output++ = *out_str++;
  335. }
  336. while (--length != 0);
  337. out->output = output;
  338. out->output_size = output_size;
  339. }
  340. /* Prints the separator into the output.
  341. Arguments:
  342. out output context
  343. separator glob separator
  344. with_escape backslash is needed before separator
  345. */
  346. static void
  347. convert_glob_print_separator(pcre2_output_context *out,
  348. PCRE2_UCHAR separator, BOOL with_escape)
  349. {
  350. if (with_escape)
  351. convert_glob_write(out, CHAR_BACKSLASH);
  352. convert_glob_write(out, separator);
  353. }
  354. /* Prints a wildcard into the output.
  355. Arguments:
  356. out output context
  357. separator glob separator
  358. with_escape backslash is needed before separator
  359. */
  360. static void
  361. convert_glob_print_wildcard(pcre2_output_context *out,
  362. PCRE2_UCHAR separator, BOOL with_escape)
  363. {
  364. out->out_str[0] = CHAR_LEFT_SQUARE_BRACKET;
  365. out->out_str[1] = CHAR_CIRCUMFLEX_ACCENT;
  366. convert_glob_write_str(out, 2);
  367. convert_glob_print_separator(out, separator, with_escape);
  368. convert_glob_write(out, CHAR_RIGHT_SQUARE_BRACKET);
  369. }
  370. /* Parse a posix class.
  371. Arguments:
  372. from starting point of scanning the range
  373. pattern_end end of pattern
  374. out output context
  375. Returns: >0 => class index
  376. 0 => malformed class
  377. */
  378. static int
  379. convert_glob_parse_class(PCRE2_SPTR *from, PCRE2_SPTR pattern_end,
  380. pcre2_output_context *out)
  381. {
  382. static const char *posix_classes = "alnum:alpha:ascii:blank:cntrl:digit:"
  383. "graph:lower:print:punct:space:upper:word:xdigit:";
  384. PCRE2_SPTR start = *from + 1;
  385. PCRE2_SPTR pattern = start;
  386. const char *class_ptr;
  387. PCRE2_UCHAR c;
  388. int class_index;
  389. while (TRUE)
  390. {
  391. if (pattern >= pattern_end) return 0;
  392. c = *pattern++;
  393. if (c < CHAR_a || c > CHAR_z) break;
  394. }
  395. if (c != CHAR_COLON || pattern >= pattern_end ||
  396. *pattern != CHAR_RIGHT_SQUARE_BRACKET)
  397. return 0;
  398. class_ptr = posix_classes;
  399. class_index = 1;
  400. while (TRUE)
  401. {
  402. if (*class_ptr == CHAR_NUL) return 0;
  403. pattern = start;
  404. while (*pattern == (PCRE2_UCHAR) *class_ptr)
  405. {
  406. if (*pattern == CHAR_COLON)
  407. {
  408. pattern += 2;
  409. start -= 2;
  410. do convert_glob_write(out, *start++); while (start < pattern);
  411. *from = pattern;
  412. return class_index;
  413. }
  414. pattern++;
  415. class_ptr++;
  416. }
  417. while (*class_ptr != CHAR_COLON) class_ptr++;
  418. class_ptr++;
  419. class_index++;
  420. }
  421. }
  422. /* Checks whether the character is in the class.
  423. Arguments:
  424. class_index class index
  425. c character
  426. Returns: !0 => character is found in the class
  427. 0 => otherwise
  428. */
  429. static BOOL
  430. convert_glob_char_in_class(int class_index, PCRE2_UCHAR c)
  431. {
  432. switch (class_index)
  433. {
  434. case 1: return isalnum(c);
  435. case 2: return isalpha(c);
  436. case 3: return 1;
  437. case 4: return c == CHAR_HT || c == CHAR_SPACE;
  438. case 5: return iscntrl(c);
  439. case 6: return isdigit(c);
  440. case 7: return isgraph(c);
  441. case 8: return islower(c);
  442. case 9: return isprint(c);
  443. case 10: return ispunct(c);
  444. case 11: return isspace(c);
  445. case 12: return isupper(c);
  446. case 13: return isalnum(c) || c == CHAR_UNDERSCORE;
  447. default: return isxdigit(c);
  448. }
  449. }
  450. /* Parse a range of characters.
  451. Arguments:
  452. from starting point of scanning the range
  453. pattern_end end of pattern
  454. out output context
  455. separator glob separator
  456. with_escape backslash is needed before separator
  457. Returns: 0 => success
  458. !0 => error code
  459. */
  460. static int
  461. convert_glob_parse_range(PCRE2_SPTR *from, PCRE2_SPTR pattern_end,
  462. pcre2_output_context *out, BOOL utf, PCRE2_UCHAR separator,
  463. BOOL with_escape, PCRE2_UCHAR escape, BOOL no_wildsep)
  464. {
  465. BOOL is_negative = FALSE;
  466. BOOL separator_seen = FALSE;
  467. BOOL has_prev_c;
  468. PCRE2_SPTR pattern = *from;
  469. PCRE2_SPTR char_start = NULL;
  470. uint32_t c, prev_c;
  471. int len, class_index;
  472. (void)utf; /* Avoid compiler warning. */
  473. if (pattern >= pattern_end)
  474. {
  475. *from = pattern;
  476. return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
  477. }
  478. if (*pattern == CHAR_EXCLAMATION_MARK
  479. || *pattern == CHAR_CIRCUMFLEX_ACCENT)
  480. {
  481. pattern++;
  482. if (pattern >= pattern_end)
  483. {
  484. *from = pattern;
  485. return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
  486. }
  487. is_negative = TRUE;
  488. out->out_str[0] = CHAR_LEFT_SQUARE_BRACKET;
  489. out->out_str[1] = CHAR_CIRCUMFLEX_ACCENT;
  490. len = 2;
  491. if (!no_wildsep)
  492. {
  493. if (with_escape)
  494. {
  495. out->out_str[len] = CHAR_BACKSLASH;
  496. len++;
  497. }
  498. out->out_str[len] = (uint8_t) separator;
  499. }
  500. convert_glob_write_str(out, len + 1);
  501. }
  502. else
  503. convert_glob_write(out, CHAR_LEFT_SQUARE_BRACKET);
  504. has_prev_c = FALSE;
  505. prev_c = 0;
  506. if (*pattern == CHAR_RIGHT_SQUARE_BRACKET)
  507. {
  508. out->out_str[0] = CHAR_BACKSLASH;
  509. out->out_str[1] = CHAR_RIGHT_SQUARE_BRACKET;
  510. convert_glob_write_str(out, 2);
  511. has_prev_c = TRUE;
  512. prev_c = CHAR_RIGHT_SQUARE_BRACKET;
  513. pattern++;
  514. }
  515. while (pattern < pattern_end)
  516. {
  517. char_start = pattern;
  518. GETCHARINCTEST(c, pattern);
  519. if (c == CHAR_RIGHT_SQUARE_BRACKET)
  520. {
  521. convert_glob_write(out, c);
  522. if (!is_negative && !no_wildsep && separator_seen)
  523. {
  524. out->out_str[0] = CHAR_LEFT_PARENTHESIS;
  525. out->out_str[1] = CHAR_QUESTION_MARK;
  526. out->out_str[2] = CHAR_LESS_THAN_SIGN;
  527. out->out_str[3] = CHAR_EXCLAMATION_MARK;
  528. convert_glob_write_str(out, 4);
  529. convert_glob_print_separator(out, separator, with_escape);
  530. convert_glob_write(out, CHAR_RIGHT_PARENTHESIS);
  531. }
  532. *from = pattern;
  533. return 0;
  534. }
  535. if (pattern >= pattern_end) break;
  536. if (c == CHAR_LEFT_SQUARE_BRACKET && *pattern == CHAR_COLON)
  537. {
  538. *from = pattern;
  539. class_index = convert_glob_parse_class(from, pattern_end, out);
  540. if (class_index != 0)
  541. {
  542. pattern = *from;
  543. has_prev_c = FALSE;
  544. prev_c = 0;
  545. if (!is_negative &&
  546. convert_glob_char_in_class (class_index, separator))
  547. separator_seen = TRUE;
  548. continue;
  549. }
  550. }
  551. else if (c == CHAR_MINUS && has_prev_c &&
  552. *pattern != CHAR_RIGHT_SQUARE_BRACKET)
  553. {
  554. convert_glob_write(out, CHAR_MINUS);
  555. char_start = pattern;
  556. GETCHARINCTEST(c, pattern);
  557. if (pattern >= pattern_end) break;
  558. if (escape != 0 && c == escape)
  559. {
  560. char_start = pattern;
  561. GETCHARINCTEST(c, pattern);
  562. }
  563. else if (c == CHAR_LEFT_SQUARE_BRACKET && *pattern == CHAR_COLON)
  564. {
  565. *from = pattern;
  566. return PCRE2_ERROR_CONVERT_SYNTAX;
  567. }
  568. if (prev_c > c)
  569. {
  570. *from = pattern;
  571. return PCRE2_ERROR_CONVERT_SYNTAX;
  572. }
  573. if (prev_c < separator && separator < c) separator_seen = TRUE;
  574. has_prev_c = FALSE;
  575. prev_c = 0;
  576. }
  577. else
  578. {
  579. if (escape != 0 && c == escape)
  580. {
  581. char_start = pattern;
  582. GETCHARINCTEST(c, pattern);
  583. if (pattern >= pattern_end) break;
  584. }
  585. has_prev_c = TRUE;
  586. prev_c = c;
  587. }
  588. if (c == CHAR_LEFT_SQUARE_BRACKET || c == CHAR_RIGHT_SQUARE_BRACKET ||
  589. c == CHAR_BACKSLASH || c == CHAR_MINUS)
  590. convert_glob_write(out, CHAR_BACKSLASH);
  591. if (c == separator) separator_seen = TRUE;
  592. do convert_glob_write(out, *char_start++); while (char_start < pattern);
  593. }
  594. *from = pattern;
  595. return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
  596. }
  597. /* Prints a (*COMMIT) into the output.
  598. Arguments:
  599. out output context
  600. */
  601. static void
  602. convert_glob_print_commit(pcre2_output_context *out)
  603. {
  604. out->out_str[0] = CHAR_LEFT_PARENTHESIS;
  605. out->out_str[1] = CHAR_ASTERISK;
  606. out->out_str[2] = CHAR_C;
  607. out->out_str[3] = CHAR_O;
  608. out->out_str[4] = CHAR_M;
  609. out->out_str[5] = CHAR_M;
  610. out->out_str[6] = CHAR_I;
  611. out->out_str[7] = CHAR_T;
  612. convert_glob_write_str(out, 8);
  613. convert_glob_write(out, CHAR_RIGHT_PARENTHESIS);
  614. }
  615. /* Bash glob converter.
  616. Arguments:
  617. pattype the pattern type
  618. pattern the pattern
  619. plength length in code units
  620. utf TRUE if UTF
  621. use_buffer where to put the output
  622. use_length length of use_buffer
  623. bufflenptr where to put the used length
  624. dummyrun TRUE if a dummy run
  625. ccontext the convert context
  626. Returns: 0 => success
  627. !0 => error code
  628. */
  629. static int
  630. convert_glob(uint32_t options, PCRE2_SPTR pattern, PCRE2_SIZE plength,
  631. BOOL utf, PCRE2_UCHAR *use_buffer, PCRE2_SIZE use_length,
  632. PCRE2_SIZE *bufflenptr, BOOL dummyrun, pcre2_convert_context *ccontext)
  633. {
  634. pcre2_output_context out;
  635. PCRE2_SPTR pattern_start = pattern;
  636. PCRE2_SPTR pattern_end = pattern + plength;
  637. PCRE2_UCHAR separator = ccontext->glob_separator;
  638. PCRE2_UCHAR escape = ccontext->glob_escape;
  639. PCRE2_UCHAR c;
  640. BOOL no_wildsep = (options & PCRE2_CONVERT_GLOB_NO_WILD_SEPARATOR) != 0;
  641. BOOL no_starstar = (options & PCRE2_CONVERT_GLOB_NO_STARSTAR) != 0;
  642. BOOL in_atomic = FALSE;
  643. BOOL after_starstar = FALSE;
  644. BOOL no_slash_z = FALSE;
  645. BOOL with_escape, is_start, after_separator;
  646. int result = 0;
  647. (void)utf; /* Avoid compiler warning. */
  648. #ifdef SUPPORT_UNICODE
  649. if (utf && (separator >= 128 || escape >= 128))
  650. {
  651. /* Currently only ASCII characters are supported. */
  652. *bufflenptr = 0;
  653. return PCRE2_ERROR_CONVERT_SYNTAX;
  654. }
  655. #endif
  656. with_escape = strchr(pcre2_escaped_literals, separator) != NULL;
  657. /* Initialize default for error offset as end of input. */
  658. out.output = use_buffer;
  659. out.output_end = use_buffer + use_length;
  660. out.output_size = 0;
  661. out.out_str[0] = CHAR_LEFT_PARENTHESIS;
  662. out.out_str[1] = CHAR_QUESTION_MARK;
  663. out.out_str[2] = CHAR_s;
  664. out.out_str[3] = CHAR_RIGHT_PARENTHESIS;
  665. convert_glob_write_str(&out, 4);
  666. is_start = TRUE;
  667. if (pattern < pattern_end && pattern[0] == CHAR_ASTERISK)
  668. {
  669. if (no_wildsep)
  670. is_start = FALSE;
  671. else if (!no_starstar && pattern + 1 < pattern_end &&
  672. pattern[1] == CHAR_ASTERISK)
  673. is_start = FALSE;
  674. }
  675. if (is_start)
  676. {
  677. out.out_str[0] = CHAR_BACKSLASH;
  678. out.out_str[1] = CHAR_A;
  679. convert_glob_write_str(&out, 2);
  680. }
  681. while (pattern < pattern_end)
  682. {
  683. c = *pattern++;
  684. if (c == CHAR_ASTERISK)
  685. {
  686. is_start = pattern == pattern_start + 1;
  687. if (in_atomic)
  688. {
  689. convert_glob_write(&out, CHAR_RIGHT_PARENTHESIS);
  690. in_atomic = FALSE;
  691. }
  692. if (!no_starstar && pattern < pattern_end && *pattern == CHAR_ASTERISK)
  693. {
  694. after_separator = is_start || (pattern[-2] == separator);
  695. do pattern++; while (pattern < pattern_end &&
  696. *pattern == CHAR_ASTERISK);
  697. if (pattern >= pattern_end)
  698. {
  699. no_slash_z = TRUE;
  700. break;
  701. }
  702. after_starstar = TRUE;
  703. if (after_separator && escape != 0 && *pattern == escape &&
  704. pattern + 1 < pattern_end && pattern[1] == separator)
  705. pattern++;
  706. if (is_start)
  707. {
  708. if (*pattern != separator) continue;
  709. out.out_str[0] = CHAR_LEFT_PARENTHESIS;
  710. out.out_str[1] = CHAR_QUESTION_MARK;
  711. out.out_str[2] = CHAR_COLON;
  712. out.out_str[3] = CHAR_BACKSLASH;
  713. out.out_str[4] = CHAR_A;
  714. out.out_str[5] = CHAR_VERTICAL_LINE;
  715. convert_glob_write_str(&out, 6);
  716. convert_glob_print_separator(&out, separator, with_escape);
  717. convert_glob_write(&out, CHAR_RIGHT_PARENTHESIS);
  718. pattern++;
  719. continue;
  720. }
  721. convert_glob_print_commit(&out);
  722. if (!after_separator || *pattern != separator)
  723. {
  724. out.out_str[0] = CHAR_DOT;
  725. out.out_str[1] = CHAR_ASTERISK;
  726. out.out_str[2] = CHAR_QUESTION_MARK;
  727. convert_glob_write_str(&out, 3);
  728. continue;
  729. }
  730. out.out_str[0] = CHAR_LEFT_PARENTHESIS;
  731. out.out_str[1] = CHAR_QUESTION_MARK;
  732. out.out_str[2] = CHAR_COLON;
  733. out.out_str[3] = CHAR_DOT;
  734. out.out_str[4] = CHAR_ASTERISK;
  735. out.out_str[5] = CHAR_QUESTION_MARK;
  736. convert_glob_write_str(&out, 6);
  737. convert_glob_print_separator(&out, separator, with_escape);
  738. out.out_str[0] = CHAR_RIGHT_PARENTHESIS;
  739. out.out_str[1] = CHAR_QUESTION_MARK;
  740. out.out_str[2] = CHAR_QUESTION_MARK;
  741. convert_glob_write_str(&out, 3);
  742. pattern++;
  743. continue;
  744. }
  745. if (pattern < pattern_end && *pattern == CHAR_ASTERISK)
  746. {
  747. do pattern++; while (pattern < pattern_end &&
  748. *pattern == CHAR_ASTERISK);
  749. }
  750. if (no_wildsep)
  751. {
  752. if (pattern >= pattern_end)
  753. {
  754. no_slash_z = TRUE;
  755. break;
  756. }
  757. /* Start check must be after the end check. */
  758. if (is_start) continue;
  759. }
  760. if (!is_start)
  761. {
  762. if (after_starstar)
  763. {
  764. out.out_str[0] = CHAR_LEFT_PARENTHESIS;
  765. out.out_str[1] = CHAR_QUESTION_MARK;
  766. out.out_str[2] = CHAR_GREATER_THAN_SIGN;
  767. convert_glob_write_str(&out, 3);
  768. in_atomic = TRUE;
  769. }
  770. else
  771. convert_glob_print_commit(&out);
  772. }
  773. if (no_wildsep)
  774. convert_glob_write(&out, CHAR_DOT);
  775. else
  776. convert_glob_print_wildcard(&out, separator, with_escape);
  777. out.out_str[0] = CHAR_ASTERISK;
  778. out.out_str[1] = CHAR_QUESTION_MARK;
  779. if (pattern >= pattern_end)
  780. out.out_str[1] = CHAR_PLUS;
  781. convert_glob_write_str(&out, 2);
  782. continue;
  783. }
  784. if (c == CHAR_QUESTION_MARK)
  785. {
  786. if (no_wildsep)
  787. convert_glob_write(&out, CHAR_DOT);
  788. else
  789. convert_glob_print_wildcard(&out, separator, with_escape);
  790. continue;
  791. }
  792. if (c == CHAR_LEFT_SQUARE_BRACKET)
  793. {
  794. result = convert_glob_parse_range(&pattern, pattern_end,
  795. &out, utf, separator, with_escape, escape, no_wildsep);
  796. if (result != 0) break;
  797. continue;
  798. }
  799. if (escape != 0 && c == escape)
  800. {
  801. if (pattern >= pattern_end)
  802. {
  803. result = PCRE2_ERROR_CONVERT_SYNTAX;
  804. break;
  805. }
  806. c = *pattern++;
  807. }
  808. if (c < 128 && strchr(pcre2_escaped_literals, c) != NULL)
  809. convert_glob_write(&out, CHAR_BACKSLASH);
  810. convert_glob_write(&out, c);
  811. }
  812. if (result == 0)
  813. {
  814. if (!no_slash_z)
  815. {
  816. out.out_str[0] = CHAR_BACKSLASH;
  817. out.out_str[1] = CHAR_z;
  818. convert_glob_write_str(&out, 2);
  819. }
  820. if (in_atomic)
  821. convert_glob_write(&out, CHAR_RIGHT_PARENTHESIS);
  822. convert_glob_write(&out, CHAR_NUL);
  823. if (!dummyrun && out.output_size != (PCRE2_SIZE) (out.output - use_buffer))
  824. result = PCRE2_ERROR_NOMEMORY;
  825. }
  826. if (result != 0)
  827. {
  828. *bufflenptr = pattern - pattern_start;
  829. return result;
  830. }
  831. *bufflenptr = out.output_size - 1;
  832. return 0;
  833. }
  834. /*************************************************
  835. * Convert pattern *
  836. *************************************************/
  837. /* This is the external-facing function for converting other forms of pattern
  838. into PCRE2 regular expression patterns. On error, the bufflenptr argument is
  839. used to return an offset in the original pattern.
  840. Arguments:
  841. pattern the input pattern
  842. plength length of input, or PCRE2_ZERO_TERMINATED
  843. options options bits
  844. buffptr pointer to pointer to output buffer
  845. bufflenptr pointer to length of output buffer
  846. ccontext convert context or NULL
  847. Returns: 0 for success, else an error code (+ve or -ve)
  848. */
  849. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  850. pcre2_pattern_convert(PCRE2_SPTR pattern, PCRE2_SIZE plength, uint32_t options,
  851. PCRE2_UCHAR **buffptr, PCRE2_SIZE *bufflenptr,
  852. pcre2_convert_context *ccontext)
  853. {
  854. int i, rc;
  855. PCRE2_UCHAR dummy_buffer[DUMMY_BUFFER_SIZE];
  856. PCRE2_UCHAR *use_buffer = dummy_buffer;
  857. PCRE2_SIZE use_length = DUMMY_BUFFER_SIZE;
  858. BOOL utf = (options & PCRE2_CONVERT_UTF) != 0;
  859. uint32_t pattype = options & TYPE_OPTIONS;
  860. if (pattern == NULL || bufflenptr == NULL) return PCRE2_ERROR_NULL;
  861. if ((options & ~ALL_OPTIONS) != 0 || /* Undefined bit set */
  862. (pattype & (~pattype+1)) != pattype || /* More than one type set */
  863. pattype == 0) /* No type set */
  864. {
  865. *bufflenptr = 0; /* Error offset */
  866. return PCRE2_ERROR_BADOPTION;
  867. }
  868. if (plength == PCRE2_ZERO_TERMINATED) plength = PRIV(strlen)(pattern);
  869. if (ccontext == NULL) ccontext =
  870. (pcre2_convert_context *)(&PRIV(default_convert_context));
  871. /* Check UTF if required. */
  872. #ifndef SUPPORT_UNICODE
  873. if (utf) return PCRE2_ERROR_UNICODE_NOT_SUPPORTED;
  874. #else
  875. if (utf && (options & PCRE2_CONVERT_NO_UTF_CHECK) == 0)
  876. {
  877. PCRE2_SIZE erroroffset;
  878. rc = PRIV(valid_utf)(pattern, plength, &erroroffset);
  879. if (rc != 0)
  880. {
  881. *bufflenptr = erroroffset;
  882. return rc;
  883. }
  884. }
  885. #endif
  886. /* If buffptr is not NULL, and what it points to is not NULL, we are being
  887. provided with a buffer and a length, so set them as the buffer to use. */
  888. if (buffptr != NULL && *buffptr != NULL)
  889. {
  890. use_buffer = *buffptr;
  891. use_length = *bufflenptr;
  892. }
  893. /* Call an individual converter, either just once (if a buffer was provided or
  894. just the length is needed), or twice (if a memory allocation is required). */
  895. for (i = 0; i < 2; i++)
  896. {
  897. PCRE2_UCHAR *allocated;
  898. BOOL dummyrun = buffptr == NULL || *buffptr == NULL;
  899. switch(pattype)
  900. {
  901. case PCRE2_CONVERT_GLOB:
  902. rc = convert_glob(options & ~PCRE2_CONVERT_GLOB, pattern, plength, utf,
  903. use_buffer, use_length, bufflenptr, dummyrun, ccontext);
  904. break;
  905. case PCRE2_CONVERT_POSIX_BASIC:
  906. case PCRE2_CONVERT_POSIX_EXTENDED:
  907. rc = convert_posix(pattype, pattern, plength, utf, use_buffer, use_length,
  908. bufflenptr, dummyrun, ccontext);
  909. break;
  910. default:
  911. return PCRE2_ERROR_INTERNAL;
  912. }
  913. if (rc != 0 || /* Error */
  914. buffptr == NULL || /* Just the length is required */
  915. *buffptr != NULL) /* Buffer was provided or allocated */
  916. return rc;
  917. /* Allocate memory for the buffer, with hidden space for an allocator at
  918. the start. The next time round the loop runs the conversion for real. */
  919. allocated = PRIV(memctl_malloc)(sizeof(pcre2_memctl) +
  920. (*bufflenptr + 1)*PCRE2_CODE_UNIT_WIDTH, (pcre2_memctl *)ccontext);
  921. if (allocated == NULL) return PCRE2_ERROR_NOMEMORY;
  922. *buffptr = (PCRE2_UCHAR *)(((char *)allocated) + sizeof(pcre2_memctl));
  923. use_buffer = *buffptr;
  924. use_length = *bufflenptr + 1;
  925. }
  926. /* Control should never get here. */
  927. return PCRE2_ERROR_INTERNAL;
  928. }
  929. /*************************************************
  930. * Free converted pattern *
  931. *************************************************/
  932. /* This frees a converted pattern that was put in newly-allocated memory.
  933. Argument: the converted pattern
  934. Returns: nothing
  935. */
  936. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  937. pcre2_converted_pattern_free(PCRE2_UCHAR *converted)
  938. {
  939. if (converted != NULL)
  940. {
  941. pcre2_memctl *memctl =
  942. (pcre2_memctl *)((char *)converted - sizeof(pcre2_memctl));
  943. memctl->free(memctl, memctl->memory_data);
  944. }
  945. }
  946. /* End of pcre2_convert.c */