1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
8 Written by Philip Hazel
9 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016-2018 University of Cambridge
12 -----------------------------------------------------------------------------
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
19 * Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
23 * Neither the name of the University of Cambridge nor the names of its
24 contributors may be used to endorse or promote products derived from
25 this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38 -----------------------------------------------------------------------------
46 #include "pcre2_internal.h"
48 #define TYPE_OPTIONS (PCRE2_CONVERT_GLOB| \
49 PCRE2_CONVERT_POSIX_BASIC|PCRE2_CONVERT_POSIX_EXTENDED)
51 #define ALL_OPTIONS (PCRE2_CONVERT_UTF|PCRE2_CONVERT_NO_UTF_CHECK| \
52 PCRE2_CONVERT_GLOB_NO_WILD_SEPARATOR| \
53 PCRE2_CONVERT_GLOB_NO_STARSTAR| \
56 #define DUMMY_BUFFER_SIZE 100
58 /* Generated pattern fragments */
60 #define STR_BACKSLASH_A STR_BACKSLASH STR_A
61 #define STR_BACKSLASH_z STR_BACKSLASH STR_z
62 #define STR_COLON_RIGHT_SQUARE_BRACKET STR_COLON STR_RIGHT_SQUARE_BRACKET
63 #define STR_DOT_STAR_LOOKBEHIND STR_DOT STR_ASTERISK STR_LEFT_PARENTHESIS STR_QUESTION_MARK STR_LESS_THAN_SIGN STR_EQUALS_SIGN
64 #define STR_LOOKAHEAD_NOT_DOT STR_LEFT_PARENTHESIS STR_QUESTION_MARK STR_EXCLAMATION_MARK STR_BACKSLASH STR_DOT STR_RIGHT_PARENTHESIS
65 #define STR_QUERY_s STR_LEFT_PARENTHESIS STR_QUESTION_MARK STR_s STR_RIGHT_PARENTHESIS
66 #define STR_STAR_NUL STR_LEFT_PARENTHESIS STR_ASTERISK STR_N STR_U STR_L STR_RIGHT_PARENTHESIS
68 /* States for range and POSIX processing */
70 enum { RANGE_NOT_STARTED, RANGE_STARTING, RANGE_STARTED };
71 enum { POSIX_START_REGEX, POSIX_ANCHORED, POSIX_NOT_BRACKET,
72 POSIX_CLASS_NOT_STARTED, POSIX_CLASS_STARTING, POSIX_CLASS_STARTED };
74 /* Macro to add a character string to the output buffer, checking for overflow. */
76 #define PUTCHARS(string) \
78 for (s = (char *)(string); *s != 0; s++) \
80 if (p >= endp) return PCRE2_ERROR_NOMEMORY; \
85 /* Literals that must be escaped: \ ? * + | . ^ $ { } [ ] ( ) */
87 static const char *pcre2_escaped_literals =
88 STR_BACKSLASH STR_QUESTION_MARK STR_ASTERISK STR_PLUS
89 STR_VERTICAL_LINE STR_DOT STR_CIRCUMFLEX_ACCENT STR_DOLLAR_SIGN
90 STR_LEFT_CURLY_BRACKET STR_RIGHT_CURLY_BRACKET
91 STR_LEFT_SQUARE_BRACKET STR_RIGHT_SQUARE_BRACKET
92 STR_LEFT_PARENTHESIS STR_RIGHT_PARENTHESIS;
94 /* Recognized escaped metacharacters in POSIX basic patterns. */
96 static const char *posix_meta_escapes =
97 STR_LEFT_PARENTHESIS STR_RIGHT_PARENTHESIS
98 STR_LEFT_CURLY_BRACKET STR_RIGHT_CURLY_BRACKET
99 STR_1 STR_2 STR_3 STR_4 STR_5 STR_6 STR_7 STR_8 STR_9;
103 /*************************************************
104 * Convert a POSIX pattern *
105 *************************************************/
107 /* This function handles both basic and extended POSIX patterns.
110 pattype the pattern type
112 plength length in code units
114 use_buffer where to put the output
115 use_length length of use_buffer
116 bufflenptr where to put the used length
117 dummyrun TRUE if a dummy run
118 ccontext the convert context
120 Returns: 0 => success
125 convert_posix(uint32_t pattype, PCRE2_SPTR pattern, PCRE2_SIZE plength,
126 BOOL utf, PCRE2_UCHAR *use_buffer, PCRE2_SIZE use_length,
127 PCRE2_SIZE *bufflenptr, BOOL dummyrun, pcre2_convert_context *ccontext)
130 PCRE2_SPTR posix = pattern;
131 PCRE2_UCHAR *p = use_buffer;
133 PCRE2_UCHAR *endp = p + use_length - 1; /* Allow for trailing zero */
134 PCRE2_SIZE convlength = 0;
136 uint32_t bracount = 0;
137 uint32_t posix_state = POSIX_START_REGEX;
138 uint32_t lastspecial = 0;
139 BOOL extended = (pattype & PCRE2_CONVERT_POSIX_EXTENDED) != 0;
140 BOOL nextisliteral = FALSE;
142 (void)utf; /* Not used when Unicode not supported */
143 (void)ccontext; /* Not currently used */
145 /* Initialize default for error offset as end of input. */
147 *bufflenptr = plength;
148 PUTCHARS(STR_STAR_NUL);
150 /* Now scan the input. */
157 /* Add in the length of the last item, then, if in the dummy run, pull the
158 pointer back to the start of the (temporary) buffer and then remember the
159 start of the next item. */
161 convlength += p - pp;
162 if (dummyrun) p = use_buffer;
165 /* Pick up the next character */
167 #ifndef SUPPORT_UNICODE
170 GETCHARLENTEST(c, posix, clength);
175 sc = nextisliteral? 0 : c;
176 nextisliteral = FALSE;
178 /* Handle a character within a class. */
180 if (posix_state >= POSIX_CLASS_NOT_STARTED)
182 if (c == CHAR_RIGHT_SQUARE_BRACKET)
184 PUTCHARS(STR_RIGHT_SQUARE_BRACKET);
185 posix_state = POSIX_NOT_BRACKET;
188 /* Not the end of the class */
194 case POSIX_CLASS_STARTED:
195 if (c <= 127 && islower(c)) break; /* Remain in started state */
196 posix_state = POSIX_CLASS_NOT_STARTED;
197 if (c == CHAR_COLON && plength > 0 &&
198 *posix == CHAR_RIGHT_SQUARE_BRACKET)
200 PUTCHARS(STR_COLON_RIGHT_SQUARE_BRACKET);
203 continue; /* With next character after :] */
207 case POSIX_CLASS_NOT_STARTED:
208 if (c == CHAR_LEFT_SQUARE_BRACKET)
209 posix_state = POSIX_CLASS_STARTING;
212 case POSIX_CLASS_STARTING:
213 if (c == CHAR_COLON) posix_state = POSIX_CLASS_STARTED;
217 if (c == CHAR_BACKSLASH) PUTCHARS(STR_BACKSLASH);
218 if (p + clength > endp) return PCRE2_ERROR_NOMEMORY;
219 memcpy(p, posix - clength, CU2BYTES(clength));
224 /* Handle a character not within a class. */
228 case CHAR_LEFT_SQUARE_BRACKET:
229 PUTCHARS(STR_LEFT_SQUARE_BRACKET);
232 /* We could handle special cases [[:<:]] and [[:>:]] (which PCRE does
233 support) but they are not part of POSIX 1003.1. */
237 if (posix[0] == CHAR_LEFT_SQUARE_BRACKET &&
238 posix[1] == CHAR_COLON &&
239 (posix[2] == CHAR_LESS_THAN_SIGN ||
240 posix[2] == CHAR_GREATER_THAN_SIGN) &&
241 posix[3] == CHAR_COLON &&
242 posix[4] == CHAR_RIGHT_SQUARE_BRACKET &&
243 posix[5] == CHAR_RIGHT_SQUARE_BRACKET)
245 if (p + 6 > endp) return PCRE2_ERROR_NOMEMORY;
246 memcpy(p, posix, CU2BYTES(6));
250 continue; /* With next character */
255 /* Handle start of "normal" character classes */
257 posix_state = POSIX_CLASS_NOT_STARTED;
259 /* Handle ^ and ] as first characters */
263 if (*posix == CHAR_CIRCUMFLEX_ACCENT)
267 PUTCHARS(STR_CIRCUMFLEX_ACCENT);
269 if (plength > 0 && *posix == CHAR_RIGHT_SQUARE_BRACKET)
273 PUTCHARS(STR_RIGHT_SQUARE_BRACKET);
279 if (plength <= 0) return PCRE2_ERROR_END_BACKSLASH;
280 if (extended) nextisliteral = TRUE; else
282 if (*posix < 127 && strchr(posix_meta_escapes, *posix) != NULL)
284 if (isdigit(*posix)) PUTCHARS(STR_BACKSLASH);
285 if (p + 1 > endp) return PCRE2_ERROR_NOMEMORY;
286 lastspecial = *p++ = *posix++;
289 else nextisliteral = TRUE;
293 case CHAR_RIGHT_PARENTHESIS:
294 if (!extended || bracount == 0) goto ESCAPE_LITERAL;
298 case CHAR_LEFT_PARENTHESIS:
302 case CHAR_QUESTION_MARK:
304 case CHAR_LEFT_CURLY_BRACKET:
305 case CHAR_RIGHT_CURLY_BRACKET:
306 case CHAR_VERTICAL_LINE:
307 if (!extended) goto ESCAPE_LITERAL;
311 case CHAR_DOLLAR_SIGN:
312 posix_state = POSIX_NOT_BRACKET;
315 if (p + 1 > endp) return PCRE2_ERROR_NOMEMORY;
320 if (lastspecial != CHAR_ASTERISK)
322 if (!extended && (posix_state < POSIX_NOT_BRACKET ||
323 lastspecial == CHAR_LEFT_PARENTHESIS))
327 break; /* Ignore second and subsequent asterisks */
329 case CHAR_CIRCUMFLEX_ACCENT:
330 if (extended) goto COPY_SPECIAL;
331 if (posix_state == POSIX_START_REGEX ||
332 lastspecial == CHAR_LEFT_PARENTHESIS)
334 posix_state = POSIX_ANCHORED;
340 if (c < 128 && strchr(pcre2_escaped_literals, c) != NULL)
343 PUTCHARS(STR_BACKSLASH);
345 lastspecial = 0xff; /* Indicates nothing special */
346 if (p + clength > endp) return PCRE2_ERROR_NOMEMORY;
347 memcpy(p, posix - clength, CU2BYTES(clength));
349 posix_state = POSIX_NOT_BRACKET;
354 if (posix_state >= POSIX_CLASS_NOT_STARTED)
355 return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
356 convlength += p - pp; /* Final segment */
357 *bufflenptr = convlength;
363 /*************************************************
364 * Convert a glob pattern *
365 *************************************************/
367 /* Context for writing the output into a buffer. */
369 typedef struct pcre2_output_context {
370 PCRE2_UCHAR *output; /* current output position */
371 PCRE2_SPTR output_end; /* output end */
372 PCRE2_SIZE output_size; /* size of the output */
373 uint8_t out_str[8]; /* string copied to the output */
374 } pcre2_output_context;
377 /* Write a character into the output.
381 chr the next character
385 convert_glob_write(pcre2_output_context *out, PCRE2_UCHAR chr)
389 if (out->output < out->output_end)
390 *out->output++ = chr;
394 /* Write a string into the output.
398 length length of out->out_str
402 convert_glob_write_str(pcre2_output_context *out, PCRE2_SIZE length)
404 uint8_t *out_str = out->out_str;
405 PCRE2_UCHAR *output = out->output;
406 PCRE2_SPTR output_end = out->output_end;
407 PCRE2_SIZE output_size = out->output_size;
413 if (output < output_end)
414 *output++ = *out_str++;
416 while (--length != 0);
418 out->output = output;
419 out->output_size = output_size;
423 /* Prints the separator into the output.
427 separator glob separator
428 with_escape backslash is needed before separator
432 convert_glob_print_separator(pcre2_output_context *out,
433 PCRE2_UCHAR separator, BOOL with_escape)
436 convert_glob_write(out, CHAR_BACKSLASH);
438 convert_glob_write(out, separator);
442 /* Prints a wildcard into the output.
446 separator glob separator
447 with_escape backslash is needed before separator
451 convert_glob_print_wildcard(pcre2_output_context *out,
452 PCRE2_UCHAR separator, BOOL with_escape)
454 out->out_str[0] = CHAR_LEFT_SQUARE_BRACKET;
455 out->out_str[1] = CHAR_CIRCUMFLEX_ACCENT;
456 convert_glob_write_str(out, 2);
458 convert_glob_print_separator(out, separator, with_escape);
460 convert_glob_write(out, CHAR_RIGHT_SQUARE_BRACKET);
464 /* Parse a posix class.
467 from starting point of scanning the range
468 pattern_end end of pattern
471 Returns: >0 => class index
476 convert_glob_parse_class(PCRE2_SPTR *from, PCRE2_SPTR pattern_end,
477 pcre2_output_context *out)
479 static const char *posix_classes = "alnum:alpha:ascii:blank:cntrl:digit:"
480 "graph:lower:print:punct:space:upper:word:xdigit:";
481 PCRE2_SPTR start = *from + 1;
482 PCRE2_SPTR pattern = start;
483 const char *class_ptr;
489 if (pattern >= pattern_end) return 0;
493 if (c < CHAR_a || c > CHAR_z) break;
496 if (c != CHAR_COLON || pattern >= pattern_end ||
497 *pattern != CHAR_RIGHT_SQUARE_BRACKET)
500 class_ptr = posix_classes;
505 if (*class_ptr == CHAR_NUL) return 0;
509 while (*pattern == (PCRE2_UCHAR) *class_ptr)
511 if (*pattern == CHAR_COLON)
516 do convert_glob_write(out, *start++); while (start < pattern);
525 while (*class_ptr != CHAR_COLON) class_ptr++;
531 /* Checks whether the character is in the class.
534 class_index class index
537 Returns: !0 => character is found in the class
542 convert_glob_char_in_class(int class_index, PCRE2_UCHAR c)
546 case 1: return isalnum(c);
547 case 2: return isalpha(c);
549 case 4: return c == CHAR_HT || c == CHAR_SPACE;
550 case 5: return iscntrl(c);
551 case 6: return isdigit(c);
552 case 7: return isgraph(c);
553 case 8: return islower(c);
554 case 9: return isprint(c);
555 case 10: return ispunct(c);
556 case 11: return isspace(c);
557 case 12: return isupper(c);
558 case 13: return isalnum(c) || c == CHAR_UNDERSCORE;
559 default: return isxdigit(c);
563 /* Parse a range of characters.
566 from starting point of scanning the range
567 pattern_end end of pattern
569 separator glob separator
570 with_escape backslash is needed before separator
572 Returns: 0 => success
577 convert_glob_parse_range(PCRE2_SPTR *from, PCRE2_SPTR pattern_end,
578 pcre2_output_context *out, BOOL utf, PCRE2_UCHAR separator,
579 BOOL with_escape, PCRE2_UCHAR escape, BOOL no_wildsep)
581 BOOL is_negative = FALSE;
582 BOOL separator_seen = FALSE;
584 PCRE2_SPTR pattern = *from;
585 PCRE2_SPTR char_start = NULL;
587 int len, class_index;
589 (void)utf; /* Avoid compiler warning. */
591 if (pattern >= pattern_end)
594 return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
597 if (*pattern == CHAR_EXCLAMATION_MARK
598 || *pattern == CHAR_CIRCUMFLEX_ACCENT)
602 if (pattern >= pattern_end)
605 return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
610 out->out_str[0] = CHAR_LEFT_SQUARE_BRACKET;
611 out->out_str[1] = CHAR_CIRCUMFLEX_ACCENT;
618 out->out_str[len] = CHAR_BACKSLASH;
621 out->out_str[len] = (uint8_t) separator;
624 convert_glob_write_str(out, len + 1);
627 convert_glob_write(out, CHAR_LEFT_SQUARE_BRACKET);
632 if (*pattern == CHAR_RIGHT_SQUARE_BRACKET)
634 out->out_str[0] = CHAR_BACKSLASH;
635 out->out_str[1] = CHAR_RIGHT_SQUARE_BRACKET;
636 convert_glob_write_str(out, 2);
638 prev_c = CHAR_RIGHT_SQUARE_BRACKET;
642 while (pattern < pattern_end)
644 char_start = pattern;
645 GETCHARINCTEST(c, pattern);
647 if (c == CHAR_RIGHT_SQUARE_BRACKET)
649 convert_glob_write(out, c);
651 if (!is_negative && !no_wildsep && separator_seen)
653 out->out_str[0] = CHAR_LEFT_PARENTHESIS;
654 out->out_str[1] = CHAR_QUESTION_MARK;
655 out->out_str[2] = CHAR_LESS_THAN_SIGN;
656 out->out_str[3] = CHAR_EXCLAMATION_MARK;
657 convert_glob_write_str(out, 4);
659 convert_glob_print_separator(out, separator, with_escape);
660 convert_glob_write(out, CHAR_RIGHT_PARENTHESIS);
667 if (pattern >= pattern_end) break;
669 if (c == CHAR_LEFT_SQUARE_BRACKET && *pattern == CHAR_COLON)
672 class_index = convert_glob_parse_class(from, pattern_end, out);
674 if (class_index != 0)
682 convert_glob_char_in_class (class_index, separator))
683 separator_seen = TRUE;
687 else if (c == CHAR_MINUS && has_prev_c &&
688 *pattern != CHAR_RIGHT_SQUARE_BRACKET)
690 convert_glob_write(out, CHAR_MINUS);
692 char_start = pattern;
693 GETCHARINCTEST(c, pattern);
695 if (pattern >= pattern_end) break;
697 if (escape != 0 && c == escape)
699 char_start = pattern;
700 GETCHARINCTEST(c, pattern);
702 else if (c == CHAR_LEFT_SQUARE_BRACKET && *pattern == CHAR_COLON)
705 return PCRE2_ERROR_CONVERT_SYNTAX;
711 return PCRE2_ERROR_CONVERT_SYNTAX;
714 if (prev_c < separator && separator < c) separator_seen = TRUE;
721 if (escape != 0 && c == escape)
723 char_start = pattern;
724 GETCHARINCTEST(c, pattern);
726 if (pattern >= pattern_end) break;
733 if (c == CHAR_LEFT_SQUARE_BRACKET || c == CHAR_RIGHT_SQUARE_BRACKET ||
734 c == CHAR_BACKSLASH || c == CHAR_MINUS)
735 convert_glob_write(out, CHAR_BACKSLASH);
737 if (c == separator) separator_seen = TRUE;
739 do convert_glob_write(out, *char_start++); while (char_start < pattern);
743 return PCRE2_ERROR_MISSING_SQUARE_BRACKET;
747 /* Prints a (*COMMIT) into the output.
754 convert_glob_print_commit(pcre2_output_context *out)
756 out->out_str[0] = CHAR_LEFT_PARENTHESIS;
757 out->out_str[1] = CHAR_ASTERISK;
758 out->out_str[2] = CHAR_C;
759 out->out_str[3] = CHAR_O;
760 out->out_str[4] = CHAR_M;
761 out->out_str[5] = CHAR_M;
762 out->out_str[6] = CHAR_I;
763 out->out_str[7] = CHAR_T;
764 convert_glob_write_str(out, 8);
765 convert_glob_write(out, CHAR_RIGHT_PARENTHESIS);
769 /* Bash glob converter.
772 pattype the pattern type
774 plength length in code units
776 use_buffer where to put the output
777 use_length length of use_buffer
778 bufflenptr where to put the used length
779 dummyrun TRUE if a dummy run
780 ccontext the convert context
782 Returns: 0 => success
787 convert_glob(uint32_t options, PCRE2_SPTR pattern, PCRE2_SIZE plength,
788 BOOL utf, PCRE2_UCHAR *use_buffer, PCRE2_SIZE use_length,
789 PCRE2_SIZE *bufflenptr, BOOL dummyrun, pcre2_convert_context *ccontext)
791 pcre2_output_context out;
792 PCRE2_SPTR pattern_start = pattern;
793 PCRE2_SPTR pattern_end = pattern + plength;
794 PCRE2_UCHAR separator = ccontext->glob_separator;
795 PCRE2_UCHAR escape = ccontext->glob_escape;
797 BOOL no_wildsep = (options & PCRE2_CONVERT_GLOB_NO_WILD_SEPARATOR) != 0;
798 BOOL no_starstar = (options & PCRE2_CONVERT_GLOB_NO_STARSTAR) != 0;
799 BOOL in_atomic = FALSE;
800 BOOL after_starstar = FALSE;
801 BOOL no_slash_z = FALSE;
802 BOOL with_escape, is_start, after_separator;
805 (void)utf; /* Avoid compiler warning. */
807 #ifdef SUPPORT_UNICODE
808 if (utf && (separator >= 128 || escape >= 128))
810 /* Currently only ASCII characters are supported. */
812 return PCRE2_ERROR_CONVERT_SYNTAX;
816 with_escape = strchr(pcre2_escaped_literals, separator) != NULL;
818 /* Initialize default for error offset as end of input. */
819 out.output = use_buffer;
820 out.output_end = use_buffer + use_length;
823 out.out_str[0] = CHAR_LEFT_PARENTHESIS;
824 out.out_str[1] = CHAR_QUESTION_MARK;
825 out.out_str[2] = CHAR_s;
826 out.out_str[3] = CHAR_RIGHT_PARENTHESIS;
827 convert_glob_write_str(&out, 4);
831 if (pattern < pattern_end && pattern[0] == CHAR_ASTERISK)
835 else if (!no_starstar && pattern + 1 < pattern_end &&
836 pattern[1] == CHAR_ASTERISK)
842 out.out_str[0] = CHAR_BACKSLASH;
843 out.out_str[1] = CHAR_A;
844 convert_glob_write_str(&out, 2);
847 while (pattern < pattern_end)
851 if (c == CHAR_ASTERISK)
853 is_start = pattern == pattern_start + 1;
857 convert_glob_write(&out, CHAR_RIGHT_PARENTHESIS);
861 if (!no_starstar && pattern < pattern_end && *pattern == CHAR_ASTERISK)
863 after_separator = is_start || (pattern[-2] == separator);
865 do pattern++; while (pattern < pattern_end &&
866 *pattern == CHAR_ASTERISK);
868 if (pattern >= pattern_end)
874 after_starstar = TRUE;
876 if (after_separator && escape != 0 && *pattern == escape &&
877 pattern + 1 < pattern_end && pattern[1] == separator)
882 if (*pattern != separator) continue;
884 out.out_str[0] = CHAR_LEFT_PARENTHESIS;
885 out.out_str[1] = CHAR_QUESTION_MARK;
886 out.out_str[2] = CHAR_COLON;
887 out.out_str[3] = CHAR_BACKSLASH;
888 out.out_str[4] = CHAR_A;
889 out.out_str[5] = CHAR_VERTICAL_LINE;
890 convert_glob_write_str(&out, 6);
892 convert_glob_print_separator(&out, separator, with_escape);
893 convert_glob_write(&out, CHAR_RIGHT_PARENTHESIS);
899 convert_glob_print_commit(&out);
901 if (!after_separator || *pattern != separator)
903 out.out_str[0] = CHAR_DOT;
904 out.out_str[1] = CHAR_ASTERISK;
905 out.out_str[2] = CHAR_QUESTION_MARK;
906 convert_glob_write_str(&out, 3);
910 out.out_str[0] = CHAR_LEFT_PARENTHESIS;
911 out.out_str[1] = CHAR_QUESTION_MARK;
912 out.out_str[2] = CHAR_COLON;
913 out.out_str[3] = CHAR_DOT;
914 out.out_str[4] = CHAR_ASTERISK;
915 out.out_str[5] = CHAR_QUESTION_MARK;
917 convert_glob_write_str(&out, 6);
919 convert_glob_print_separator(&out, separator, with_escape);
921 out.out_str[0] = CHAR_RIGHT_PARENTHESIS;
922 out.out_str[1] = CHAR_QUESTION_MARK;
923 out.out_str[2] = CHAR_QUESTION_MARK;
924 convert_glob_write_str(&out, 3);
930 if (pattern < pattern_end && *pattern == CHAR_ASTERISK)
932 do pattern++; while (pattern < pattern_end &&
933 *pattern == CHAR_ASTERISK);
938 if (pattern >= pattern_end)
944 /* Start check must be after the end check. */
945 if (is_start) continue;
952 out.out_str[0] = CHAR_LEFT_PARENTHESIS;
953 out.out_str[1] = CHAR_QUESTION_MARK;
954 out.out_str[2] = CHAR_GREATER_THAN_SIGN;
955 convert_glob_write_str(&out, 3);
959 convert_glob_print_commit(&out);
963 convert_glob_write(&out, CHAR_DOT);
965 convert_glob_print_wildcard(&out, separator, with_escape);
967 out.out_str[0] = CHAR_ASTERISK;
968 out.out_str[1] = CHAR_QUESTION_MARK;
969 if (pattern >= pattern_end)
970 out.out_str[1] = CHAR_PLUS;
971 convert_glob_write_str(&out, 2);
975 if (c == CHAR_QUESTION_MARK)
978 convert_glob_write(&out, CHAR_DOT);
980 convert_glob_print_wildcard(&out, separator, with_escape);
984 if (c == CHAR_LEFT_SQUARE_BRACKET)
986 result = convert_glob_parse_range(&pattern, pattern_end,
987 &out, utf, separator, with_escape, escape, no_wildsep);
988 if (result != 0) break;
992 if (escape != 0 && c == escape)
994 if (pattern >= pattern_end)
996 result = PCRE2_ERROR_CONVERT_SYNTAX;
1002 if (c < 128 && strchr(pcre2_escaped_literals, c) != NULL)
1003 convert_glob_write(&out, CHAR_BACKSLASH);
1005 convert_glob_write(&out, c);
1012 out.out_str[0] = CHAR_BACKSLASH;
1013 out.out_str[1] = CHAR_z;
1014 convert_glob_write_str(&out, 2);
1018 convert_glob_write(&out, CHAR_RIGHT_PARENTHESIS);
1020 convert_glob_write(&out, CHAR_NUL);
1022 if (!dummyrun && out.output_size != (PCRE2_SIZE) (out.output - use_buffer))
1023 result = PCRE2_ERROR_NOMEMORY;
1028 *bufflenptr = pattern - pattern_start;
1032 *bufflenptr = out.output_size - 1;
1037 /*************************************************
1039 *************************************************/
1041 /* This is the external-facing function for converting other forms of pattern
1042 into PCRE2 regular expression patterns. On error, the bufflenptr argument is
1043 used to return an offset in the original pattern.
1046 pattern the input pattern
1047 plength length of input, or PCRE2_ZERO_TERMINATED
1048 options options bits
1049 buffptr pointer to pointer to output buffer
1050 bufflenptr pointer to length of output buffer
1051 ccontext convert context or NULL
1053 Returns: 0 for success, else an error code (+ve or -ve)
1056 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
1057 pcre2_pattern_convert(PCRE2_SPTR pattern, PCRE2_SIZE plength, uint32_t options,
1058 PCRE2_UCHAR **buffptr, PCRE2_SIZE *bufflenptr,
1059 pcre2_convert_context *ccontext)
1062 PCRE2_UCHAR dummy_buffer[DUMMY_BUFFER_SIZE];
1063 PCRE2_UCHAR *use_buffer = dummy_buffer;
1064 PCRE2_SIZE use_length = DUMMY_BUFFER_SIZE;
1065 BOOL utf = (options & PCRE2_CONVERT_UTF) != 0;
1066 uint32_t pattype = options & TYPE_OPTIONS;
1068 if (pattern == NULL || bufflenptr == NULL) return PCRE2_ERROR_NULL;
1070 if ((options & ~ALL_OPTIONS) != 0 || /* Undefined bit set */
1071 (pattype & (~pattype+1)) != pattype || /* More than one type set */
1072 pattype == 0) /* No type set */
1074 *bufflenptr = 0; /* Error offset */
1075 return PCRE2_ERROR_BADOPTION;
1078 if (plength == PCRE2_ZERO_TERMINATED) plength = PRIV(strlen)(pattern);
1079 if (ccontext == NULL) ccontext =
1080 (pcre2_convert_context *)(&PRIV(default_convert_context));
1082 /* Check UTF if required. */
1084 #ifndef SUPPORT_UNICODE
1087 *bufflenptr = 0; /* Error offset */
1088 return PCRE2_ERROR_UNICODE_NOT_SUPPORTED;
1091 if (utf && (options & PCRE2_CONVERT_NO_UTF_CHECK) == 0)
1093 PCRE2_SIZE erroroffset;
1094 rc = PRIV(valid_utf)(pattern, plength, &erroroffset);
1097 *bufflenptr = erroroffset;
1103 /* If buffptr is not NULL, and what it points to is not NULL, we are being
1104 provided with a buffer and a length, so set them as the buffer to use. */
1106 if (buffptr != NULL && *buffptr != NULL)
1108 use_buffer = *buffptr;
1109 use_length = *bufflenptr;
1112 /* Call an individual converter, either just once (if a buffer was provided or
1113 just the length is needed), or twice (if a memory allocation is required). */
1115 for (i = 0; i < 2; i++)
1117 PCRE2_UCHAR *allocated;
1118 BOOL dummyrun = buffptr == NULL || *buffptr == NULL;
1122 case PCRE2_CONVERT_GLOB:
1123 rc = convert_glob(options & ~PCRE2_CONVERT_GLOB, pattern, plength, utf,
1124 use_buffer, use_length, bufflenptr, dummyrun, ccontext);
1127 case PCRE2_CONVERT_POSIX_BASIC:
1128 case PCRE2_CONVERT_POSIX_EXTENDED:
1129 rc = convert_posix(pattype, pattern, plength, utf, use_buffer, use_length,
1130 bufflenptr, dummyrun, ccontext);
1134 *bufflenptr = 0; /* Error offset */
1135 return PCRE2_ERROR_INTERNAL;
1138 if (rc != 0 || /* Error */
1139 buffptr == NULL || /* Just the length is required */
1140 *buffptr != NULL) /* Buffer was provided or allocated */
1143 /* Allocate memory for the buffer, with hidden space for an allocator at
1144 the start. The next time round the loop runs the conversion for real. */
1146 allocated = PRIV(memctl_malloc)(sizeof(pcre2_memctl) +
1147 (*bufflenptr + 1)*PCRE2_CODE_UNIT_WIDTH, (pcre2_memctl *)ccontext);
1148 if (allocated == NULL) return PCRE2_ERROR_NOMEMORY;
1149 *buffptr = (PCRE2_UCHAR *)(((char *)allocated) + sizeof(pcre2_memctl));
1151 use_buffer = *buffptr;
1152 use_length = *bufflenptr + 1;
1155 /* Control should never get here. */
1157 return PCRE2_ERROR_INTERNAL;
1161 /*************************************************
1162 * Free converted pattern *
1163 *************************************************/
1165 /* This frees a converted pattern that was put in newly-allocated memory.
1167 Argument: the converted pattern
1171 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
1172 pcre2_converted_pattern_free(PCRE2_UCHAR *converted)
1174 if (converted != NULL)
1176 pcre2_memctl *memctl =
1177 (pcre2_memctl *)((char *)converted - sizeof(pcre2_memctl));
1178 memctl->free(memctl, memctl->memory_data);
1182 /* End of pcre2_convert.c */