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 Copyright (c) 1997-2010 University of Cambridge
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
22 * Neither the name of the University of Cambridge nor the names of its
23 contributors may be used to endorse or promote products derived from
24 this software without specific prior written permission.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37 -----------------------------------------------------------------------------
41 /* This module contains a PCRE private debugging function for printing out the
42 internal form of a compiled regular expression, along with some supporting
43 local functions. This source file is used in two places:
45 (1) It is #included by pcre_compile.c when it is compiled in debugging mode
46 (PCRE_DEBUG defined in pcre_internal.h). It is not included in production
49 (2) It is always #included by pcretest.c, which can be asked to print out a
50 compiled regex for debugging purposes. */
53 /* Macro that decides whether a character should be output as a literal or in
54 hexadecimal. We don't use isprint() because that can vary from system to system
55 (even without the use of locales) and we want the output always to be the same,
56 for testing purposes. This macro is used in pcretest as well as in this file. */
59 #define PRINTABLE(c) ((c) >= 64 && (c) < 255)
61 #define PRINTABLE(c) ((c) >= 32 && (c) < 127)
64 /* The table of operator names. */
66 static const char *OP_names[] = { OP_NAME_LIST };
70 /*************************************************
71 * Print single- or multi-byte character *
72 *************************************************/
75 print_char(FILE *f, uschar *ptr, BOOL utf8)
80 utf8 = utf8; /* Avoid compiler warning */
81 if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c);
85 if (!utf8 || (c & 0xc0) != 0xc0)
87 if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c);
93 int a = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */
95 c = (c & _pcre_utf8_table3[a]) << s;
96 for (i = 1; i <= a; i++)
98 /* This is a check for malformed UTF-8; it should only occur if the sanity
99 check has been turned off. Rather than swallow random bytes, just stop if
100 we hit a bad one. Print it with \X instead of \x as an indication. */
102 if ((ptr[i] & 0xc0) != 0x80)
104 fprintf(f, "\\X{%x}", c);
111 c |= (ptr[i] & 0x3f) << s;
113 if (c < 128) fprintf(f, "\\x%02x", c); else fprintf(f, "\\x{%x}", c);
121 /*************************************************
122 * Find Unicode property name *
123 *************************************************/
126 get_ucpname(int ptype, int pvalue)
130 for (i = _pcre_utt_size - 1; i >= 0; i--)
132 if (ptype == _pcre_utt[i].type && pvalue == _pcre_utt[i].value) break;
134 return (i >= 0)? _pcre_utt_names + _pcre_utt[i].name_offset : "??";
136 /* It gets harder and harder to shut off unwanted compiler warnings. */
137 ptype = ptype * pvalue;
138 return (ptype == pvalue)? "??" : "??";
144 /*************************************************
145 * Print compiled regex *
146 *************************************************/
148 /* Make this function work for a regex with integers either byte order.
149 However, we assume that what we are passed is a compiled regex. The
150 print_lengths flag controls whether offsets and lengths of items are printed.
151 They can be turned off from pcretest so that automatic tests on bytecode can be
152 written that do not depend on the value of LINK_SIZE. */
155 pcre_printint(pcre *external_re, FILE *f, BOOL print_lengths)
157 real_pcre *re = (real_pcre *)external_re;
158 uschar *codestart, *code;
161 unsigned int options = re->options;
162 int offset = re->name_table_offset;
163 int count = re->name_count;
164 int size = re->name_entry_size;
166 if (re->magic_number != MAGIC_NUMBER)
168 offset = ((offset << 8) & 0xff00) | ((offset >> 8) & 0xff);
169 count = ((count << 8) & 0xff00) | ((count >> 8) & 0xff);
170 size = ((size << 8) & 0xff00) | ((size >> 8) & 0xff);
171 options = ((options << 24) & 0xff000000) |
172 ((options << 8) & 0x00ff0000) |
173 ((options >> 8) & 0x0000ff00) |
174 ((options >> 24) & 0x000000ff);
177 code = codestart = (uschar *)re + offset + count * size;
178 utf8 = (options & PCRE_UTF8) != 0;
187 fprintf(f, "%3d ", (int)(code - codestart));
193 /* ========================================================================== */
194 /* These cases are never obeyed. This is a fudge that causes a compile-
195 time error if the vectors OP_names or _pcre_OP_lengths, which are indexed
196 by opcode, are not the correct length. It seems to be the only way to do
197 such a check at compile time, as the sizeof() operator does not work in
198 the C preprocessor. We do this while compiling pcretest, because that
199 #includes pcre_tables.c, which holds _pcre_OP_lengths. We can't do this
200 when building pcre_compile.c with PCRE_DEBUG set, because it doesn't then
201 know the size of _pcre_OP_lengths. */
203 #ifdef COMPILING_PCRETEST
204 case OP_TABLE_LENGTH:
205 case OP_TABLE_LENGTH +
206 ((sizeof(OP_names)/sizeof(const char *) == OP_TABLE_LENGTH) &&
207 (sizeof(_pcre_OP_lengths) == OP_TABLE_LENGTH)):
210 /* ========================================================================== */
213 fprintf(f, " %s\n", OP_names[*code]);
214 fprintf(f, "------------------------------------------------------------------\n");
218 fprintf(f, " %.2x %s", code[1], OP_names[*code]);
226 code += 1 + print_char(f, code, utf8);
228 while (*code == OP_CHAR);
237 code += 1 + print_char(f, code, utf8);
239 while (*code == OP_CHARNC);
245 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
246 else fprintf(f, " ");
247 fprintf(f, "%s %d", OP_names[*code], GET2(code, 1+LINK_SIZE));
259 case OP_ASSERTBACK_NOT:
264 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
265 else fprintf(f, " ");
266 fprintf(f, "%s", OP_names[*code]);
270 fprintf(f, " %s %d", OP_names[*code], GET2(code, 1));
275 fprintf(f, "%3d %s", GET2(code,1), OP_names[*code]);
281 fprintf(f, " Cond recurse any");
283 fprintf(f, " Cond recurse %d", c);
289 fprintf(f, " Cond nrecurse any");
291 fprintf(f, " Cond nrecurse %d", c);
295 fprintf(f, " Cond def");
314 case OP_TYPEMINQUERY:
315 case OP_TYPEPOSQUERY:
317 if (*code >= OP_TYPESTAR)
319 fprintf(f, "%s", OP_names[code[1]]);
320 if (code[1] == OP_PROP || code[1] == OP_NOTPROP)
322 fprintf(f, " %s ", get_ucpname(code[2], code[3]));
326 else extra = print_char(f, code+1, utf8);
327 fprintf(f, "%s", OP_names[*code]);
335 extra = print_char(f, code+3, utf8);
337 if (*code != OP_EXACT) fprintf(f, "0,");
338 fprintf(f, "%d}", GET2(code,1));
339 if (*code == OP_MINUPTO) fprintf(f, "?");
340 else if (*code == OP_POSUPTO) fprintf(f, "+");
347 fprintf(f, " %s", OP_names[code[3]]);
348 if (code[3] == OP_PROP || code[3] == OP_NOTPROP)
350 fprintf(f, " %s ", get_ucpname(code[4], code[5]));
354 if (*code != OP_TYPEEXACT) fprintf(f, "0,");
355 fprintf(f, "%d}", GET2(code,1));
356 if (*code == OP_TYPEMINUPTO) fprintf(f, "?");
357 else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+");
362 if (PRINTABLE(c)) fprintf(f, " [^%c]", c);
363 else fprintf(f, " [^\\x%02x]", c);
376 if (PRINTABLE(c)) fprintf(f, " [^%c]", c);
377 else fprintf(f, " [^\\x%02x]", c);
378 fprintf(f, "%s", OP_names[*code]);
386 if (PRINTABLE(c)) fprintf(f, " [^%c]{", c);
387 else fprintf(f, " [^\\x%02x]{", c);
388 if (*code != OP_NOTEXACT) fprintf(f, "0,");
389 fprintf(f, "%d}", GET2(code,1));
390 if (*code == OP_NOTMINUPTO) fprintf(f, "?");
391 else if (*code == OP_NOTPOSUPTO) fprintf(f, "+");
395 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
396 else fprintf(f, " ");
397 fprintf(f, "%s", OP_names[*code]);
401 fprintf(f, " \\%d", GET2(code,1));
402 ccode = code + _pcre_OP_lengths[*code];
403 goto CLASS_REF_REPEAT;
406 fprintf(f, " %s %d %d %d", OP_names[*code], code[1], GET(code,2),
407 GET(code, 2 + LINK_SIZE));
412 fprintf(f, " %s %s", OP_names[*code], get_ucpname(code[1], code[2]));
415 /* OP_XCLASS can only occur in UTF-8 mode. However, there's no harm in
416 having this code always here, and it makes it less messy without all those
428 if (*code == OP_XCLASS)
430 extra = GET(code, 1);
431 ccode = code + LINK_SIZE + 1;
432 printmap = (*ccode & XCL_MAP) != 0;
433 if ((*ccode++ & XCL_NOT) != 0) fprintf(f, "^");
441 /* Print a bit map */
445 for (i = 0; i < 256; i++)
447 if ((ccode[i/8] & (1 << (i&7))) != 0)
450 for (j = i+1; j < 256; j++)
451 if ((ccode[j/8] & (1 << (j&7))) == 0) break;
452 if (i == '-' || i == ']') fprintf(f, "\\");
453 if (PRINTABLE(i)) fprintf(f, "%c", i);
454 else fprintf(f, "\\x%02x", i);
457 if (j != i + 1) fprintf(f, "-");
458 if (j == '-' || j == ']') fprintf(f, "\\");
459 if (PRINTABLE(j)) fprintf(f, "%c", j);
460 else fprintf(f, "\\x%02x", j);
468 /* For an XCLASS there is always some additional data */
470 if (*code == OP_XCLASS)
473 while ((ch = *ccode++) != XCL_END)
477 int ptype = *ccode++;
478 int pvalue = *ccode++;
479 fprintf(f, "\\p{%s}", get_ucpname(ptype, pvalue));
481 else if (ch == XCL_NOTPROP)
483 int ptype = *ccode++;
484 int pvalue = *ccode++;
485 fprintf(f, "\\P{%s}", get_ucpname(ptype, pvalue));
489 ccode += 1 + print_char(f, ccode, TRUE);
493 ccode += 1 + print_char(f, ccode, TRUE);
499 /* Indicate a non-UTF8 class which was created by negation */
501 fprintf(f, "]%s", (*code == OP_NCLASS)? " (neg)" : "");
503 /* Handle repeats after a class or a back reference */
514 fprintf(f, "%s", OP_names[*ccode]);
515 extra += _pcre_OP_lengths[*ccode];
522 if (max == 0) fprintf(f, "{%d,}", min);
523 else fprintf(f, "{%d,%d}", min, max);
524 if (*ccode == OP_CRMINRANGE) fprintf(f, "?");
525 extra += _pcre_OP_lengths[*ccode];
528 /* Do nothing if it's not a repeat; this code stops picky compilers
529 warning about the lack of a default code path. */
540 fprintf(f, " %s %s", OP_names[*code], code + 2);
546 fprintf(f, " %s %d", OP_names[*code], GET(code, 1));
548 fprintf(f, " %s", OP_names[*code]);
553 fprintf(f, " %s %d %s", OP_names[*code], GET(code, 1),
554 code + 2 + LINK_SIZE);
556 fprintf(f, " %s %s", OP_names[*code], code + 2 + LINK_SIZE);
557 extra += code[1+LINK_SIZE];
560 /* Anything else is just an item with no data*/
563 fprintf(f, " %s", OP_names[*code]);
567 code += _pcre_OP_lengths[*code] + extra;
572 /* End of pcre_printint.src */