bes  Updated for version 3.20.8
BESRegex.cc
1 // BESRegex.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 // and James Gallagher <jgallagher@gso.uri.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact University Corporation for Atmospheric Research at
25 // 3080 Center Green Drive, Boulder, CO 80301
26 
27 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
28 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
29 //
30 // Authors:
31 // pwest Patrick West <pwest@ucar.edu>
32 // jgarcia Jose Garcia <jgarcia@ucar.edu>
33 // jimg James Gallagher <jgallagher@gso.uri.edu>
34 
35 #include <config.h>
36 
37 #ifndef WIN32
38 #include <alloca.h>
39 #endif
40 
41 #include <sys/types.h>
42 #include <regex.h>
43 
44 #include <cstdlib>
45 #include <new>
46 #include <string>
47 #include <stdexcept>
48 
49 #include "BESRegex.h"
50 #include "BESInternalError.h"
51 #include "BESScrub.h"
52 
53 using namespace std;
54 
55 void
56 BESRegex::init(const char *t)
57 {
58  d_pattern = t;
59  d_preg = static_cast<void*>(new regex_t);
60  int result = regcomp(static_cast<regex_t*>(d_preg), t, REG_EXTENDED);
61 
62  if (result != 0) {
63  size_t msg_len = regerror(result, static_cast<regex_t*>(d_preg), static_cast<char*>(NULL),
64  static_cast<size_t>(0));
65  char *msg = new char[msg_len + 1];
66  regerror(result, static_cast<regex_t*>(d_preg), msg, msg_len);
67  string err = string("BESRegex error: ") + string(msg);
68  BESInternalError e(err, __FILE__, __LINE__);
69  delete[] msg;
70  throw e;
71  }
72 }
73 
74 BESRegex::~BESRegex()
75 {
76  regfree(static_cast<regex_t*>(d_preg));
77  delete static_cast<regex_t*>(d_preg); d_preg = 0;
78 
79 }
80 
84 BESRegex::BESRegex(const char* t)
85 {
86  init(t);
87 }
88 
91 BESRegex::BESRegex(const char* t, int)
92 {
93  init(t);
94 }
95 
106 int
107 BESRegex::match(const char* s, int len, int pos)
108 {
109  // TODO re-implement using auto_ptr or unique_ptr. jhrg 7/27/18
110  regmatch_t *pmatch = new regmatch_t[len+1];
111  string ss = s;
112 
113  int result = regexec(static_cast<regex_t*>(d_preg),
114  ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
115  int matchnum;
116  if (result == REG_NOMATCH)
117  matchnum = -1; //returns -1 due to function being able to match strings of 0 length
118  else
119  matchnum = pmatch[0].rm_eo - pmatch[0].rm_so;
120 
121  delete[] pmatch; pmatch = 0;
122 
123  return matchnum;
124 }
125 
136 int
137 BESRegex::search(const char* s, int len, int& matchlen, int pos)
138 {
139  // sanitize allocation
140  if (!BESScrub::size_ok(sizeof(regmatch_t), len+1))
141  return -1;
142 
143  // alloc space for len matches, which is theoretical max.
144  // Problem: If somehow 'len' is very large - say the size of a 32-bit int,
145  // then len+1 is a an integer overflow and this might be exploited by
146  // an attacker. It's not likely there will be more than a handful of
147  // matches, so I am going to limit this value to 32766. jhrg 3/4/09
148  if (len > 32766)
149  return -1;
150 
151  regmatch_t *pmatch = new regmatch_t[len+1];
152  string ss = s;
153 
154  int result = regexec(static_cast<regex_t*>(d_preg),
155  ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
156  if (result == REG_NOMATCH) {
157  delete[] pmatch; pmatch = 0;
158  return -1;
159  }
160 
161  // Match found, find the first one (pmatch lists the longest first)
162  int m = 0;
163  for (int i = 1; i < len; ++i)
164  if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so)
165  m = i;
166 
167  matchlen = pmatch[m].rm_eo - pmatch[m].rm_so;
168  int matchpos = pmatch[m].rm_so;
169 
170  delete[] pmatch; pmatch = 0;
171  return matchpos;
172 }
173 
exception thrown if internal error encountered
int search(const char *s, int len, int &matchlen, int pos=0)
How much of the string does the pattern matche.
Definition: BESRegex.cc:137
BESRegex(const char *t)
Definition: BESRegex.cc:84
int match(const char *s, int len, int pos=0)
Does the pattern match.
Definition: BESRegex.cc:107
static bool size_ok(unsigned int sz, unsigned int nelem)
sanitize the size of an array. Test for integer overflow when dynamically allocating an array.
Definition: BESScrub.cc:68