1// Copyright 2003-2009 Google Inc. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// This is a variant of PCRE's pcrecpp.cc, originally written at Google.
6// The main changes are the addition of the HitLimit method and
7// compilation as PCRE in namespace re2.
8
9#include <assert.h>
10#include <ctype.h>
11#include <errno.h>
12#include <stdlib.h>
13#include <string.h>
14#include <limits>
15#include <string>
16#include <utility>
17
18#include "util/util.h"
19#include "util/flags.h"
20#include "util/logging.h"
21#include "util/pcre.h"
22#include "util/strutil.h"
23
24// Silence warnings about the wacky formatting in the operator() functions.
25// Note that we test for Clang first because it defines __GNUC__ as well.
26#if defined(__clang__)
27#elif defined(__GNUC__) && __GNUC__ >= 6
28#pragma GCC diagnostic ignored "-Wmisleading-indentation"
29#endif
30
31#define PCREPORT(level) LOG(level)
32
33// Default PCRE limits.
34// Defaults chosen to allow a plausible amount of CPU and
35// not exceed main thread stacks. Note that other threads
36// often have smaller stacks, and therefore tightening
37// regexp_stack_limit may frequently be necessary.
38DEFINE_FLAG(int, regexp_stack_limit, 256 << 10,
39 "default PCRE stack limit (bytes)");
40DEFINE_FLAG(int, regexp_match_limit, 1000000,
41 "default PCRE match limit (function calls)");
42
43#ifndef USEPCRE
44
45// Fake just enough of the PCRE API to allow this file to build. :)
46
47struct pcre_extra {
48 int flags;
49 int match_limit;
50 int match_limit_recursion;
51};
52
53#define PCRE_EXTRA_MATCH_LIMIT 0
54#define PCRE_EXTRA_MATCH_LIMIT_RECURSION 0
55#define PCRE_ANCHORED 0
56#define PCRE_NOTEMPTY 0
57#define PCRE_ERROR_NOMATCH 1
58#define PCRE_ERROR_MATCHLIMIT 2
59#define PCRE_ERROR_RECURSIONLIMIT 3
60#define PCRE_INFO_CAPTURECOUNT 0
61
62void pcre_free(void*) {
63}
64
65pcre* pcre_compile(const char*, int, const char**, int*, const unsigned char*) {
66 return NULL;
67}
68
69int pcre_exec(const pcre*, const pcre_extra*, const char*, int, int, int, int*, int) {
70 return 0;
71}
72
73int pcre_fullinfo(const pcre*, const pcre_extra*, int, void*) {
74 return 0;
75}
76
77#endif
78
79namespace re2 {
80
81// Maximum number of args we can set
82static const int kMaxArgs = 16;
83static const int kVecSize = (1 + kMaxArgs) * 3; // results + PCRE workspace
84
85// Approximate size of a recursive invocation of PCRE's
86// internal "match()" frame. This varies depending on the
87// compiler and architecture, of course, so the constant is
88// just a conservative estimate. To find the exact number,
89// run regexp_unittest with --regexp_stack_limit=0 under
90// a debugger and look at the frames when it crashes.
91// The exact frame size was 656 in production on 2008/02/03.
92static const int kPCREFrameSize = 700;
93
94// Special name for missing C++ arguments.
95PCRE::Arg PCRE::no_more_args((void*)NULL);
96
97const PCRE::PartialMatchFunctor PCRE::PartialMatch = { };
98const PCRE::FullMatchFunctor PCRE::FullMatch = { } ;
99const PCRE::ConsumeFunctor PCRE::Consume = { };
100const PCRE::FindAndConsumeFunctor PCRE::FindAndConsume = { };
101
102// If a regular expression has no error, its error_ field points here
103static const std::string empty_string;
104
105void PCRE::Init(const char* pattern, Option options, int match_limit,
106 int stack_limit, bool report_errors) {
107 pattern_ = pattern;
108 options_ = options;
109 match_limit_ = match_limit;
110 stack_limit_ = stack_limit;
111 hit_limit_ = false;
112 error_ = &empty_string;
113 report_errors_ = report_errors;
114 re_full_ = NULL;
115 re_partial_ = NULL;
116
117 if (options & ~(EnabledCompileOptions | EnabledExecOptions)) {
118 error_ = new std::string("illegal regexp option");
119 PCREPORT(ERROR)
120 << "Error compiling '" << pattern << "': illegal regexp option";
121 } else {
122 re_partial_ = Compile(UNANCHORED);
123 if (re_partial_ != NULL) {
124 re_full_ = Compile(ANCHOR_BOTH);
125 }
126 }
127}
128
129PCRE::PCRE(const char* pattern) {
130 Init(pattern, None, 0, 0, true);
131}
132PCRE::PCRE(const char* pattern, Option option) {
133 Init(pattern, option, 0, 0, true);
134}
135PCRE::PCRE(const std::string& pattern) {
136 Init(pattern.c_str(), None, 0, 0, true);
137}
138PCRE::PCRE(const std::string& pattern, Option option) {
139 Init(pattern.c_str(), option, 0, 0, true);
140}
141PCRE::PCRE(const std::string& pattern, const PCRE_Options& re_option) {
142 Init(pattern.c_str(), re_option.option(), re_option.match_limit(),
143 re_option.stack_limit(), re_option.report_errors());
144}
145
146PCRE::PCRE(const char *pattern, const PCRE_Options& re_option) {
147 Init(pattern, re_option.option(), re_option.match_limit(),
148 re_option.stack_limit(), re_option.report_errors());
149}
150
151PCRE::~PCRE() {
152 if (re_full_ != NULL) pcre_free(re_full_);
153 if (re_partial_ != NULL) pcre_free(re_partial_);
154 if (error_ != &empty_string) delete error_;
155}
156
157pcre* PCRE::Compile(Anchor anchor) {
158 // Special treatment for anchoring. This is needed because at
159 // runtime pcre only provides an option for anchoring at the
160 // beginning of a string.
161 //
162 // There are three types of anchoring we want:
163 // UNANCHORED Compile the original pattern, and use
164 // a pcre unanchored match.
165 // ANCHOR_START Compile the original pattern, and use
166 // a pcre anchored match.
167 // ANCHOR_BOTH Tack a "\z" to the end of the original pattern
168 // and use a pcre anchored match.
169
170 const char* error = "";
171 int eoffset;
172 pcre* re;
173 if (anchor != ANCHOR_BOTH) {
174 re = pcre_compile(pattern_.c_str(),
175 (options_ & EnabledCompileOptions),
176 &error, &eoffset, NULL);
177 } else {
178 // Tack a '\z' at the end of PCRE. Parenthesize it first so that
179 // the '\z' applies to all top-level alternatives in the regexp.
180 std::string wrapped = "(?:"; // A non-counting grouping operator
181 wrapped += pattern_;
182 wrapped += ")\\z";
183 re = pcre_compile(wrapped.c_str(),
184 (options_ & EnabledCompileOptions),
185 &error, &eoffset, NULL);
186 }
187 if (re == NULL) {
188 if (error_ == &empty_string) error_ = new std::string(error);
189 PCREPORT(ERROR) << "Error compiling '" << pattern_ << "': " << error;
190 }
191 return re;
192}
193
194/***** Convenience interfaces *****/
195
196bool PCRE::FullMatchFunctor::operator ()(const StringPiece& text,
197 const PCRE& re,
198 const Arg& a0,
199 const Arg& a1,
200 const Arg& a2,
201 const Arg& a3,
202 const Arg& a4,
203 const Arg& a5,
204 const Arg& a6,
205 const Arg& a7,
206 const Arg& a8,
207 const Arg& a9,
208 const Arg& a10,
209 const Arg& a11,
210 const Arg& a12,
211 const Arg& a13,
212 const Arg& a14,
213 const Arg& a15) const {
214 const Arg* args[kMaxArgs];
215 int n = 0;
216 if (&a0 == &no_more_args) goto done; args[n++] = &a0;
217 if (&a1 == &no_more_args) goto done; args[n++] = &a1;
218 if (&a2 == &no_more_args) goto done; args[n++] = &a2;
219 if (&a3 == &no_more_args) goto done; args[n++] = &a3;
220 if (&a4 == &no_more_args) goto done; args[n++] = &a4;
221 if (&a5 == &no_more_args) goto done; args[n++] = &a5;
222 if (&a6 == &no_more_args) goto done; args[n++] = &a6;
223 if (&a7 == &no_more_args) goto done; args[n++] = &a7;
224 if (&a8 == &no_more_args) goto done; args[n++] = &a8;
225 if (&a9 == &no_more_args) goto done; args[n++] = &a9;
226 if (&a10 == &no_more_args) goto done; args[n++] = &a10;
227 if (&a11 == &no_more_args) goto done; args[n++] = &a11;
228 if (&a12 == &no_more_args) goto done; args[n++] = &a12;
229 if (&a13 == &no_more_args) goto done; args[n++] = &a13;
230 if (&a14 == &no_more_args) goto done; args[n++] = &a14;
231 if (&a15 == &no_more_args) goto done; args[n++] = &a15;
232done:
233
234 size_t consumed;
235 int vec[kVecSize] = {};
236 return re.DoMatchImpl(text, ANCHOR_BOTH, &consumed, args, n, vec, kVecSize);
237}
238
239bool PCRE::PartialMatchFunctor::operator ()(const StringPiece& text,
240 const PCRE& re,
241 const Arg& a0,
242 const Arg& a1,
243 const Arg& a2,
244 const Arg& a3,
245 const Arg& a4,
246 const Arg& a5,
247 const Arg& a6,
248 const Arg& a7,
249 const Arg& a8,
250 const Arg& a9,
251 const Arg& a10,
252 const Arg& a11,
253 const Arg& a12,
254 const Arg& a13,
255 const Arg& a14,
256 const Arg& a15) const {
257 const Arg* args[kMaxArgs];
258 int n = 0;
259 if (&a0 == &no_more_args) goto done; args[n++] = &a0;
260 if (&a1 == &no_more_args) goto done; args[n++] = &a1;
261 if (&a2 == &no_more_args) goto done; args[n++] = &a2;
262 if (&a3 == &no_more_args) goto done; args[n++] = &a3;
263 if (&a4 == &no_more_args) goto done; args[n++] = &a4;
264 if (&a5 == &no_more_args) goto done; args[n++] = &a5;
265 if (&a6 == &no_more_args) goto done; args[n++] = &a6;
266 if (&a7 == &no_more_args) goto done; args[n++] = &a7;
267 if (&a8 == &no_more_args) goto done; args[n++] = &a8;
268 if (&a9 == &no_more_args) goto done; args[n++] = &a9;
269 if (&a10 == &no_more_args) goto done; args[n++] = &a10;
270 if (&a11 == &no_more_args) goto done; args[n++] = &a11;
271 if (&a12 == &no_more_args) goto done; args[n++] = &a12;
272 if (&a13 == &no_more_args) goto done; args[n++] = &a13;
273 if (&a14 == &no_more_args) goto done; args[n++] = &a14;
274 if (&a15 == &no_more_args) goto done; args[n++] = &a15;
275done:
276
277 size_t consumed;
278 int vec[kVecSize] = {};
279 return re.DoMatchImpl(text, UNANCHORED, &consumed, args, n, vec, kVecSize);
280}
281
282bool PCRE::ConsumeFunctor::operator ()(StringPiece* input,
283 const PCRE& pattern,
284 const Arg& a0,
285 const Arg& a1,
286 const Arg& a2,
287 const Arg& a3,
288 const Arg& a4,
289 const Arg& a5,
290 const Arg& a6,
291 const Arg& a7,
292 const Arg& a8,
293 const Arg& a9,
294 const Arg& a10,
295 const Arg& a11,
296 const Arg& a12,
297 const Arg& a13,
298 const Arg& a14,
299 const Arg& a15) const {
300 const Arg* args[kMaxArgs];
301 int n = 0;
302 if (&a0 == &no_more_args) goto done; args[n++] = &a0;
303 if (&a1 == &no_more_args) goto done; args[n++] = &a1;
304 if (&a2 == &no_more_args) goto done; args[n++] = &a2;
305 if (&a3 == &no_more_args) goto done; args[n++] = &a3;
306 if (&a4 == &no_more_args) goto done; args[n++] = &a4;
307 if (&a5 == &no_more_args) goto done; args[n++] = &a5;
308 if (&a6 == &no_more_args) goto done; args[n++] = &a6;
309 if (&a7 == &no_more_args) goto done; args[n++] = &a7;
310 if (&a8 == &no_more_args) goto done; args[n++] = &a8;
311 if (&a9 == &no_more_args) goto done; args[n++] = &a9;
312 if (&a10 == &no_more_args) goto done; args[n++] = &a10;
313 if (&a11 == &no_more_args) goto done; args[n++] = &a11;
314 if (&a12 == &no_more_args) goto done; args[n++] = &a12;
315 if (&a13 == &no_more_args) goto done; args[n++] = &a13;
316 if (&a14 == &no_more_args) goto done; args[n++] = &a14;
317 if (&a15 == &no_more_args) goto done; args[n++] = &a15;
318done:
319
320 size_t consumed;
321 int vec[kVecSize] = {};
322 if (pattern.DoMatchImpl(*input, ANCHOR_START, &consumed,
323 args, n, vec, kVecSize)) {
324 input->remove_prefix(consumed);
325 return true;
326 } else {
327 return false;
328 }
329}
330
331bool PCRE::FindAndConsumeFunctor::operator ()(StringPiece* input,
332 const PCRE& pattern,
333 const Arg& a0,
334 const Arg& a1,
335 const Arg& a2,
336 const Arg& a3,
337 const Arg& a4,
338 const Arg& a5,
339 const Arg& a6,
340 const Arg& a7,
341 const Arg& a8,
342 const Arg& a9,
343 const Arg& a10,
344 const Arg& a11,
345 const Arg& a12,
346 const Arg& a13,
347 const Arg& a14,
348 const Arg& a15) const {
349 const Arg* args[kMaxArgs];
350 int n = 0;
351 if (&a0 == &no_more_args) goto done; args[n++] = &a0;
352 if (&a1 == &no_more_args) goto done; args[n++] = &a1;
353 if (&a2 == &no_more_args) goto done; args[n++] = &a2;
354 if (&a3 == &no_more_args) goto done; args[n++] = &a3;
355 if (&a4 == &no_more_args) goto done; args[n++] = &a4;
356 if (&a5 == &no_more_args) goto done; args[n++] = &a5;
357 if (&a6 == &no_more_args) goto done; args[n++] = &a6;
358 if (&a7 == &no_more_args) goto done; args[n++] = &a7;
359 if (&a8 == &no_more_args) goto done; args[n++] = &a8;
360 if (&a9 == &no_more_args) goto done; args[n++] = &a9;
361 if (&a10 == &no_more_args) goto done; args[n++] = &a10;
362 if (&a11 == &no_more_args) goto done; args[n++] = &a11;
363 if (&a12 == &no_more_args) goto done; args[n++] = &a12;
364 if (&a13 == &no_more_args) goto done; args[n++] = &a13;
365 if (&a14 == &no_more_args) goto done; args[n++] = &a14;
366 if (&a15 == &no_more_args) goto done; args[n++] = &a15;
367done:
368
369 size_t consumed;
370 int vec[kVecSize] = {};
371 if (pattern.DoMatchImpl(*input, UNANCHORED, &consumed,
372 args, n, vec, kVecSize)) {
373 input->remove_prefix(consumed);
374 return true;
375 } else {
376 return false;
377 }
378}
379
380bool PCRE::Replace(std::string *str,
381 const PCRE& pattern,
382 const StringPiece& rewrite) {
383 int vec[kVecSize] = {};
384 int matches = pattern.TryMatch(*str, 0, UNANCHORED, true, vec, kVecSize);
385 if (matches == 0)
386 return false;
387
388 std::string s;
389 if (!pattern.Rewrite(&s, rewrite, *str, vec, matches))
390 return false;
391
392 assert(vec[0] >= 0);
393 assert(vec[1] >= 0);
394 str->replace(vec[0], vec[1] - vec[0], s);
395 return true;
396}
397
398int PCRE::GlobalReplace(std::string *str,
399 const PCRE& pattern,
400 const StringPiece& rewrite) {
401 int count = 0;
402 int vec[kVecSize] = {};
403 std::string out;
404 size_t start = 0;
405 bool last_match_was_empty_string = false;
406
407 while (start <= str->size()) {
408 // If the previous match was for the empty string, we shouldn't
409 // just match again: we'll match in the same way and get an
410 // infinite loop. Instead, we do the match in a special way:
411 // anchored -- to force another try at the same position --
412 // and with a flag saying that this time, ignore empty matches.
413 // If this special match returns, that means there's a non-empty
414 // match at this position as well, and we can continue. If not,
415 // we do what perl does, and just advance by one.
416 // Notice that perl prints '@@@' for this;
417 // perl -le '$_ = "aa"; s/b*|aa/@/g; print'
418 int matches;
419 if (last_match_was_empty_string) {
420 matches = pattern.TryMatch(*str, start, ANCHOR_START, false,
421 vec, kVecSize);
422 if (matches <= 0) {
423 if (start < str->size())
424 out.push_back((*str)[start]);
425 start++;
426 last_match_was_empty_string = false;
427 continue;
428 }
429 } else {
430 matches = pattern.TryMatch(*str, start, UNANCHORED, true,
431 vec, kVecSize);
432 if (matches <= 0)
433 break;
434 }
435 size_t matchstart = vec[0], matchend = vec[1];
436 assert(matchstart >= start);
437 assert(matchend >= matchstart);
438
439 out.append(*str, start, matchstart - start);
440 pattern.Rewrite(&out, rewrite, *str, vec, matches);
441 start = matchend;
442 count++;
443 last_match_was_empty_string = (matchstart == matchend);
444 }
445
446 if (count == 0)
447 return 0;
448
449 if (start < str->size())
450 out.append(*str, start, str->size() - start);
451 using std::swap;
452 swap(out, *str);
453 return count;
454}
455
456bool PCRE::Extract(const StringPiece &text,
457 const PCRE& pattern,
458 const StringPiece &rewrite,
459 std::string *out) {
460 int vec[kVecSize] = {};
461 int matches = pattern.TryMatch(text, 0, UNANCHORED, true, vec, kVecSize);
462 if (matches == 0)
463 return false;
464 out->clear();
465 return pattern.Rewrite(out, rewrite, text, vec, matches);
466}
467
468std::string PCRE::QuoteMeta(const StringPiece& unquoted) {
469 std::string result;
470 result.reserve(unquoted.size() << 1);
471
472 // Escape any ascii character not in [A-Za-z_0-9].
473 //
474 // Note that it's legal to escape a character even if it has no
475 // special meaning in a regular expression -- so this function does
476 // that. (This also makes it identical to the perl function of the
477 // same name except for the null-character special case;
478 // see `perldoc -f quotemeta`.)
479 for (size_t ii = 0; ii < unquoted.size(); ++ii) {
480 // Note that using 'isalnum' here raises the benchmark time from
481 // 32ns to 58ns:
482 if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
483 (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
484 (unquoted[ii] < '0' || unquoted[ii] > '9') &&
485 unquoted[ii] != '_' &&
486 // If this is the part of a UTF8 or Latin1 character, we need
487 // to copy this byte without escaping. Experimentally this is
488 // what works correctly with the regexp library.
489 !(unquoted[ii] & 128)) {
490 if (unquoted[ii] == '\0') { // Special handling for null chars.
491 // Can't use "\\0" since the next character might be a digit.
492 result += "\\x00";
493 continue;
494 }
495 result += '\\';
496 }
497 result += unquoted[ii];
498 }
499
500 return result;
501}
502
503/***** Actual matching and rewriting code *****/
504
505bool PCRE::HitLimit() {
506 return hit_limit_ != 0;
507}
508
509void PCRE::ClearHitLimit() {
510 hit_limit_ = 0;
511}
512
513int PCRE::TryMatch(const StringPiece& text,
514 size_t startpos,
515 Anchor anchor,
516 bool empty_ok,
517 int *vec,
518 int vecsize) const {
519 pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
520 if (re == NULL) {
521 PCREPORT(ERROR) << "Matching against invalid re: " << *error_;
522 return 0;
523 }
524
525 int match_limit = match_limit_;
526 if (match_limit <= 0) {
527 match_limit = GetFlag(FLAGS_regexp_match_limit);
528 }
529
530 int stack_limit = stack_limit_;
531 if (stack_limit <= 0) {
532 stack_limit = GetFlag(FLAGS_regexp_stack_limit);
533 }
534
535 pcre_extra extra = { 0 };
536 if (match_limit > 0) {
537 extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
538 extra.match_limit = match_limit;
539 }
540 if (stack_limit > 0) {
541 extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
542 extra.match_limit_recursion = stack_limit / kPCREFrameSize;
543 }
544
545 int options = 0;
546 if (anchor != UNANCHORED)
547 options |= PCRE_ANCHORED;
548 if (!empty_ok)
549 options |= PCRE_NOTEMPTY;
550
551 int rc = pcre_exec(re, // The regular expression object
552 &extra,
553 (text.data() == NULL) ? "" : text.data(),
554 static_cast<int>(text.size()),
555 static_cast<int>(startpos),
556 options,
557 vec,
558 vecsize);
559
560 // Handle errors
561 if (rc == 0) {
562 // pcre_exec() returns 0 as a special case when the number of
563 // capturing subpatterns exceeds the size of the vector.
564 // When this happens, there is a match and the output vector
565 // is filled, but we miss out on the positions of the extra subpatterns.
566 rc = vecsize / 2;
567 } else if (rc < 0) {
568 switch (rc) {
569 case PCRE_ERROR_NOMATCH:
570 return 0;
571 case PCRE_ERROR_MATCHLIMIT:
572 // Writing to hit_limit is not safe if multiple threads
573 // are using the PCRE, but the flag is only intended
574 // for use by unit tests anyway, so we let it go.
575 hit_limit_ = true;
576 PCREPORT(WARNING) << "Exceeded match limit of " << match_limit
577 << " when matching '" << pattern_ << "'"
578 << " against text that is " << text.size() << " bytes.";
579 return 0;
580 case PCRE_ERROR_RECURSIONLIMIT:
581 // See comment about hit_limit above.
582 hit_limit_ = true;
583 PCREPORT(WARNING) << "Exceeded stack limit of " << stack_limit
584 << " when matching '" << pattern_ << "'"
585 << " against text that is " << text.size() << " bytes.";
586 return 0;
587 default:
588 // There are other return codes from pcre.h :
589 // PCRE_ERROR_NULL (-2)
590 // PCRE_ERROR_BADOPTION (-3)
591 // PCRE_ERROR_BADMAGIC (-4)
592 // PCRE_ERROR_UNKNOWN_NODE (-5)
593 // PCRE_ERROR_NOMEMORY (-6)
594 // PCRE_ERROR_NOSUBSTRING (-7)
595 // ...
596 PCREPORT(ERROR) << "Unexpected return code: " << rc
597 << " when matching '" << pattern_ << "'"
598 << ", re=" << re
599 << ", text=" << text
600 << ", vec=" << vec
601 << ", vecsize=" << vecsize;
602 return 0;
603 }
604 }
605
606 return rc;
607}
608
609bool PCRE::DoMatchImpl(const StringPiece& text,
610 Anchor anchor,
611 size_t* consumed,
612 const Arg* const* args,
613 int n,
614 int* vec,
615 int vecsize) const {
616 assert((1 + n) * 3 <= vecsize); // results + PCRE workspace
617 if (NumberOfCapturingGroups() < n) {
618 // RE has fewer capturing groups than number of Arg pointers passed in.
619 return false;
620 }
621
622 int matches = TryMatch(text, 0, anchor, true, vec, vecsize);
623 assert(matches >= 0); // TryMatch never returns negatives
624 if (matches == 0)
625 return false;
626
627 *consumed = vec[1];
628
629 if (n == 0 || args == NULL) {
630 // We are not interested in results
631 return true;
632 }
633
634 // If we got here, we must have matched the whole pattern.
635 // We do not need (can not do) any more checks on the value of 'matches' here
636 // -- see the comment for TryMatch.
637 for (int i = 0; i < n; i++) {
638 const int start = vec[2*(i+1)];
639 const int limit = vec[2*(i+1)+1];
640
641 // Avoid invoking undefined behavior when text.data() happens
642 // to be null and start happens to be -1, the latter being the
643 // case for an unmatched subexpression. Even if text.data() is
644 // not null, pointing one byte before was a longstanding bug.
645 const char* addr = NULL;
646 if (start != -1) {
647 addr = text.data() + start;
648 }
649
650 if (!args[i]->Parse(addr, limit-start)) {
651 // TODO: Should we indicate what the error was?
652 return false;
653 }
654 }
655
656 return true;
657}
658
659bool PCRE::DoMatch(const StringPiece& text,
660 Anchor anchor,
661 size_t* consumed,
662 const Arg* const args[],
663 int n) const {
664 assert(n >= 0);
665 const int vecsize = (1 + n) * 3; // results + PCRE workspace
666 // (as for kVecSize)
667 int* vec = new int[vecsize];
668 bool b = DoMatchImpl(text, anchor, consumed, args, n, vec, vecsize);
669 delete[] vec;
670 return b;
671}
672
673bool PCRE::Rewrite(std::string *out, const StringPiece &rewrite,
674 const StringPiece &text, int *vec, int veclen) const {
675 int number_of_capturing_groups = NumberOfCapturingGroups();
676 for (const char *s = rewrite.data(), *end = s + rewrite.size();
677 s < end; s++) {
678 int c = *s;
679 if (c == '\\') {
680 c = *++s;
681 if (isdigit(c)) {
682 int n = (c - '0');
683 if (n >= veclen) {
684 if (n <= number_of_capturing_groups) {
685 // unmatched optional capturing group. treat
686 // its value as empty string; i.e., nothing to append.
687 } else {
688 PCREPORT(ERROR) << "requested group " << n
689 << " in regexp " << rewrite.data();
690 return false;
691 }
692 }
693 int start = vec[2 * n];
694 if (start >= 0)
695 out->append(text.data() + start, vec[2 * n + 1] - start);
696 } else if (c == '\\') {
697 out->push_back('\\');
698 } else {
699 PCREPORT(ERROR) << "invalid rewrite pattern: " << rewrite.data();
700 return false;
701 }
702 } else {
703 out->push_back(c);
704 }
705 }
706 return true;
707}
708
709bool PCRE::CheckRewriteString(const StringPiece& rewrite,
710 std::string* error) const {
711 int max_token = -1;
712 for (const char *s = rewrite.data(), *end = s + rewrite.size();
713 s < end; s++) {
714 int c = *s;
715 if (c != '\\') {
716 continue;
717 }
718 if (++s == end) {
719 *error = "Rewrite schema error: '\\' not allowed at end.";
720 return false;
721 }
722 c = *s;
723 if (c == '\\') {
724 continue;
725 }
726 if (!isdigit(c)) {
727 *error = "Rewrite schema error: "
728 "'\\' must be followed by a digit or '\\'.";
729 return false;
730 }
731 int n = (c - '0');
732 if (max_token < n) {
733 max_token = n;
734 }
735 }
736
737 if (max_token > NumberOfCapturingGroups()) {
738 *error = StringPrintf(
739 "Rewrite schema requests %d matches, but the regexp only has %d "
740 "parenthesized subexpressions.",
741 max_token, NumberOfCapturingGroups());
742 return false;
743 }
744 return true;
745}
746
747
748// Return the number of capturing subpatterns, or -1 if the
749// regexp wasn't valid on construction.
750int PCRE::NumberOfCapturingGroups() const {
751 if (re_partial_ == NULL) return -1;
752
753 int result;
754 int rc = pcre_fullinfo(re_partial_, // The regular expression object
755 NULL, // We did not study the pattern
756 PCRE_INFO_CAPTURECOUNT,
757 &result);
758 if (rc != 0) {
759 PCREPORT(ERROR) << "Unexpected return code: " << rc;
760 return -1;
761 }
762 return result;
763}
764
765
766/***** Parsers for various types *****/
767
768bool PCRE::Arg::parse_null(const char* str, size_t n, void* dest) {
769 // We fail if somebody asked us to store into a non-NULL void* pointer
770 return (dest == NULL);
771}
772
773bool PCRE::Arg::parse_string(const char* str, size_t n, void* dest) {
774 if (dest == NULL) return true;
775 reinterpret_cast<std::string*>(dest)->assign(str, n);
776 return true;
777}
778
779bool PCRE::Arg::parse_stringpiece(const char* str, size_t n, void* dest) {
780 if (dest == NULL) return true;
781 *(reinterpret_cast<StringPiece*>(dest)) = StringPiece(str, n);
782 return true;
783}
784
785bool PCRE::Arg::parse_char(const char* str, size_t n, void* dest) {
786 if (n != 1) return false;
787 if (dest == NULL) return true;
788 *(reinterpret_cast<char*>(dest)) = str[0];
789 return true;
790}
791
792bool PCRE::Arg::parse_schar(const char* str, size_t n, void* dest) {
793 if (n != 1) return false;
794 if (dest == NULL) return true;
795 *(reinterpret_cast<signed char*>(dest)) = str[0];
796 return true;
797}
798
799bool PCRE::Arg::parse_uchar(const char* str, size_t n, void* dest) {
800 if (n != 1) return false;
801 if (dest == NULL) return true;
802 *(reinterpret_cast<unsigned char*>(dest)) = str[0];
803 return true;
804}
805
806// Largest number spec that we are willing to parse
807static const int kMaxNumberLength = 32;
808
809// PCREQUIPCRES "buf" must have length at least kMaxNumberLength+1
810// PCREQUIPCRES "n > 0"
811// Copies "str" into "buf" and null-terminates if necessary.
812// Returns one of:
813// a. "str" if no termination is needed
814// b. "buf" if the string was copied and null-terminated
815// c. "" if the input was invalid and has no hope of being parsed
816static const char* TerminateNumber(char* buf, const char* str, size_t n) {
817 if ((n > 0) && isspace(*str)) {
818 // We are less forgiving than the strtoxxx() routines and do not
819 // allow leading spaces.
820 return "";
821 }
822
823 // See if the character right after the input text may potentially
824 // look like a digit.
825 if (isdigit(str[n]) ||
826 ((str[n] >= 'a') && (str[n] <= 'f')) ||
827 ((str[n] >= 'A') && (str[n] <= 'F'))) {
828 if (n > kMaxNumberLength) return ""; // Input too big to be a valid number
829 memcpy(buf, str, n);
830 buf[n] = '\0';
831 return buf;
832 } else {
833 // We can parse right out of the supplied string, so return it.
834 return str;
835 }
836}
837
838bool PCRE::Arg::parse_long_radix(const char* str,
839 size_t n,
840 void* dest,
841 int radix) {
842 if (n == 0) return false;
843 char buf[kMaxNumberLength+1];
844 str = TerminateNumber(buf, str, n);
845 char* end;
846 errno = 0;
847 long r = strtol(str, &end, radix);
848 if (end != str + n) return false; // Leftover junk
849 if (errno) return false;
850 if (dest == NULL) return true;
851 *(reinterpret_cast<long*>(dest)) = r;
852 return true;
853}
854
855bool PCRE::Arg::parse_ulong_radix(const char* str,
856 size_t n,
857 void* dest,
858 int radix) {
859 if (n == 0) return false;
860 char buf[kMaxNumberLength+1];
861 str = TerminateNumber(buf, str, n);
862 if (str[0] == '-') {
863 // strtoul() will silently accept negative numbers and parse
864 // them. This module is more strict and treats them as errors.
865 return false;
866 }
867
868 char* end;
869 errno = 0;
870 unsigned long r = strtoul(str, &end, radix);
871 if (end != str + n) return false; // Leftover junk
872 if (errno) return false;
873 if (dest == NULL) return true;
874 *(reinterpret_cast<unsigned long*>(dest)) = r;
875 return true;
876}
877
878bool PCRE::Arg::parse_short_radix(const char* str,
879 size_t n,
880 void* dest,
881 int radix) {
882 long r;
883 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
884 if ((short)r != r) return false; // Out of range
885 if (dest == NULL) return true;
886 *(reinterpret_cast<short*>(dest)) = (short)r;
887 return true;
888}
889
890bool PCRE::Arg::parse_ushort_radix(const char* str,
891 size_t n,
892 void* dest,
893 int radix) {
894 unsigned long r;
895 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
896 if ((unsigned short)r != r) return false; // Out of range
897 if (dest == NULL) return true;
898 *(reinterpret_cast<unsigned short*>(dest)) = (unsigned short)r;
899 return true;
900}
901
902bool PCRE::Arg::parse_int_radix(const char* str,
903 size_t n,
904 void* dest,
905 int radix) {
906 long r;
907 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
908 if ((int)r != r) return false; // Out of range
909 if (dest == NULL) return true;
910 *(reinterpret_cast<int*>(dest)) = (int)r;
911 return true;
912}
913
914bool PCRE::Arg::parse_uint_radix(const char* str,
915 size_t n,
916 void* dest,
917 int radix) {
918 unsigned long r;
919 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
920 if ((unsigned int)r != r) return false; // Out of range
921 if (dest == NULL) return true;
922 *(reinterpret_cast<unsigned int*>(dest)) = (unsigned int)r;
923 return true;
924}
925
926bool PCRE::Arg::parse_longlong_radix(const char* str,
927 size_t n,
928 void* dest,
929 int radix) {
930 if (n == 0) return false;
931 char buf[kMaxNumberLength+1];
932 str = TerminateNumber(buf, str, n);
933 char* end;
934 errno = 0;
935 long long r = strtoll(str, &end, radix);
936 if (end != str + n) return false; // Leftover junk
937 if (errno) return false;
938 if (dest == NULL) return true;
939 *(reinterpret_cast<long long*>(dest)) = r;
940 return true;
941}
942
943bool PCRE::Arg::parse_ulonglong_radix(const char* str,
944 size_t n,
945 void* dest,
946 int radix) {
947 if (n == 0) return false;
948 char buf[kMaxNumberLength+1];
949 str = TerminateNumber(buf, str, n);
950 if (str[0] == '-') {
951 // strtoull() will silently accept negative numbers and parse
952 // them. This module is more strict and treats them as errors.
953 return false;
954 }
955 char* end;
956 errno = 0;
957 unsigned long long r = strtoull(str, &end, radix);
958 if (end != str + n) return false; // Leftover junk
959 if (errno) return false;
960 if (dest == NULL) return true;
961 *(reinterpret_cast<unsigned long long*>(dest)) = r;
962 return true;
963}
964
965static bool parse_double_float(const char* str, size_t n, bool isfloat,
966 void* dest) {
967 if (n == 0) return false;
968 static const int kMaxLength = 200;
969 char buf[kMaxLength];
970 if (n >= kMaxLength) return false;
971 memcpy(buf, str, n);
972 buf[n] = '\0';
973 char* end;
974 errno = 0;
975 double r;
976 if (isfloat) {
977 r = strtof(buf, &end);
978 } else {
979 r = strtod(buf, &end);
980 }
981 if (end != buf + n) return false; // Leftover junk
982 if (errno) return false;
983 if (dest == NULL) return true;
984 if (isfloat) {
985 *(reinterpret_cast<float*>(dest)) = (float)r;
986 } else {
987 *(reinterpret_cast<double*>(dest)) = r;
988 }
989 return true;
990}
991
992bool PCRE::Arg::parse_double(const char* str, size_t n, void* dest) {
993 return parse_double_float(str, n, false, dest);
994}
995
996bool PCRE::Arg::parse_float(const char* str, size_t n, void* dest) {
997 return parse_double_float(str, n, true, dest);
998}
999
1000#define DEFINE_INTEGER_PARSER(name) \
1001 bool PCRE::Arg::parse_##name(const char* str, size_t n, void* dest) { \
1002 return parse_##name##_radix(str, n, dest, 10); \
1003 } \
1004 bool PCRE::Arg::parse_##name##_hex(const char* str, size_t n, void* dest) { \
1005 return parse_##name##_radix(str, n, dest, 16); \
1006 } \
1007 bool PCRE::Arg::parse_##name##_octal(const char* str, size_t n, \
1008 void* dest) { \
1009 return parse_##name##_radix(str, n, dest, 8); \
1010 } \
1011 bool PCRE::Arg::parse_##name##_cradix(const char* str, size_t n, \
1012 void* dest) { \
1013 return parse_##name##_radix(str, n, dest, 0); \
1014 }
1015
1016DEFINE_INTEGER_PARSER(short);
1017DEFINE_INTEGER_PARSER(ushort);
1018DEFINE_INTEGER_PARSER(int);
1019DEFINE_INTEGER_PARSER(uint);
1020DEFINE_INTEGER_PARSER(long);
1021DEFINE_INTEGER_PARSER(ulong);
1022DEFINE_INTEGER_PARSER(longlong);
1023DEFINE_INTEGER_PARSER(ulonglong);
1024
1025#undef DEFINE_INTEGER_PARSER
1026
1027} // namespace re2
1028