OPeNDAP Hyrax Back End Server (BES)
Updated for version 3.8.3
|
00001 // BESRegex.cc 00002 00003 // This file is part of bes, A C++ back-end server implementation framework 00004 // for the OPeNDAP Data Access Protocol. 00005 00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research 00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu> 00008 // and James Gallagher <jgallagher@gso.uri.edu> 00009 // 00010 // This library is free software; you can redistribute it and/or 00011 // modify it under the terms of the GNU Lesser General Public 00012 // License as published by the Free Software Foundation; either 00013 // version 2.1 of the License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 // 00024 // You can contact University Corporation for Atmospheric Research at 00025 // 3080 Center Green Drive, Boulder, CO 80301 00026 00027 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005 00028 // Please read the full copyright statement in the file COPYRIGHT_UCAR. 00029 // 00030 // Authors: 00031 // pwest Patrick West <pwest@ucar.edu> 00032 // jgarcia Jose Garcia <jgarcia@ucar.edu> 00033 // jimg James Gallagher <jgallagher@gso.uri.edu> 00034 00035 #include <config.h> 00036 00037 #ifndef WIN32 00038 #include <alloca.h> 00039 #endif 00040 00041 #include <sys/types.h> 00042 #include <regex.h> 00043 00044 #include <cstdlib> 00045 #include <new> 00046 #include <string> 00047 #include <stdexcept> 00048 00049 #include "BESRegex.h" 00050 #include "BESInternalError.h" 00051 #include "BESScrub.h" 00052 00053 using namespace std; 00054 00055 void 00056 BESRegex::init(const char *t) 00057 { 00058 d_preg = static_cast<void*>(new regex_t); 00059 int result = regcomp(static_cast<regex_t*>(d_preg), t, REG_EXTENDED); 00060 00061 if (result != 0) { 00062 size_t msg_len = regerror(result, static_cast<regex_t*>(d_preg), 00063 static_cast<char*>(NULL), 00064 static_cast<size_t>(0)); 00065 char *msg = new char[msg_len+1]; 00066 regerror(result, static_cast<regex_t*>(d_preg), msg, msg_len); 00067 string err = string( "BESRegex error: " ) + string( msg ) ; 00068 BESInternalError e( err, __FILE__, __LINE__ ) ; 00069 delete[] msg; 00070 throw e; 00071 } 00072 } 00073 00074 BESRegex::~BESRegex() 00075 { 00076 regfree(static_cast<regex_t*>(d_preg)); 00077 delete static_cast<regex_t*>(d_preg); d_preg = 0; 00078 00079 } 00080 00084 BESRegex::BESRegex(const char* t) 00085 { 00086 init(t); 00087 } 00088 00091 BESRegex::BESRegex(const char* t, int) 00092 { 00093 init(t); 00094 } 00095 00102 int 00103 BESRegex::match(const char* s, int len, int pos) 00104 { 00105 regmatch_t *pmatch = new regmatch_t[len+1]; 00106 string ss = s; 00107 00108 int result = regexec(static_cast<regex_t*>(d_preg), 00109 ss.substr(pos, len-pos).c_str(), len, pmatch, 0); 00110 int matchnum; 00111 if (result == REG_NOMATCH) 00112 matchnum = -1; 00113 else 00114 matchnum = pmatch[0].rm_eo - pmatch[0].rm_so; 00115 00116 delete[] pmatch; pmatch = 0; 00117 00118 return matchnum; 00119 } 00120 00131 int 00132 BESRegex::search(const char* s, int len, int& matchlen, int pos) 00133 { 00134 // sanitize allocation 00135 if (!BESScrub::size_ok(sizeof(regmatch_t), len+1)) 00136 return -1; 00137 00138 // alloc space for len matches, which is theoretical max. 00139 // Problem: If somehow 'len' is very large - say the size of a 32-bit int, 00140 // then len+1 is a an integer overflow and this might be exploited by 00141 // an attacker. It's not likely there will be more than a handful of 00142 // matches, so I am going to limit this value to 32766. jhrg 3/4/09 00143 if (len > 32766) 00144 return -1; 00145 00146 regmatch_t *pmatch = new regmatch_t[len+1]; 00147 string ss = s; 00148 00149 int result = regexec(static_cast<regex_t*>(d_preg), 00150 ss.substr(pos, len-pos).c_str(), len, pmatch, 0); 00151 if (result == REG_NOMATCH) { 00152 delete[] pmatch; pmatch = 0; 00153 return -1; 00154 } 00155 00156 // Match found, find the first one (pmatch lists the longest first) 00157 int m = 0; 00158 for (int i = 1; i < len; ++i) 00159 if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so) 00160 m = i; 00161 00162 matchlen = pmatch[m].rm_eo - pmatch[m].rm_so; 00163 int matchpos = pmatch[m].rm_so; 00164 00165 delete[] pmatch; pmatch = 0; 00166 return matchpos; 00167 } 00168