Blame standalone/regex.c

Packit 284210
/*
Packit 284210
* ModSecurity for Apache 2.x, http://www.modsecurity.org/
Packit 284210
* Copyright (c) 2004-2013 Trustwave Holdings, Inc. (http://www.trustwave.com/)
Packit 284210
*
Packit 284210
* You may not use this file except in compliance with
Packit 284210
* the License.  You may obtain a copy of the License at
Packit 284210
*
Packit 284210
*     http://www.apache.org/licenses/LICENSE-2.0
Packit 284210
*
Packit 284210
* If any of the files related to licensing are missing or if you have any
Packit 284210
* other questions related to licensing please contact Trustwave Holdings, Inc.
Packit 284210
* directly using the email address security@modsecurity.org.
Packit 284210
*/
Packit 284210

Packit 284210
#include <limits.h>
Packit 284210

Packit 284210
#include "http_core.h"
Packit 284210
#include "http_request.h"
Packit 284210

Packit 284210
#include "modsecurity.h"
Packit 284210
#include "apache2.h"
Packit 284210
#include "http_main.h"
Packit 284210
#include "http_connection.h"
Packit 284210

Packit 284210
#include "apr_optional.h"
Packit 284210
#include "mod_log_config.h"
Packit 284210

Packit 284210
#include "msc_logging.h"
Packit 284210
#include "msc_util.h"
Packit 284210

Packit 284210
#include "ap_mpm.h"
Packit 284210
#include "scoreboard.h"
Packit 284210

Packit 284210
#include "apr_version.h"
Packit 284210

Packit 284210
#include "apr_lib.h"
Packit 284210
#include "ap_config.h"
Packit 284210
#include "http_config.h"
Packit 284210

Packit 284210

Packit 284210
static apr_status_t regex_cleanup(void *preg)
Packit 284210
{
Packit 284210
    ap_regfree((ap_regex_t *) preg);
Packit 284210
    return APR_SUCCESS;
Packit 284210
}
Packit 284210

Packit 284210
AP_DECLARE(ap_regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
Packit 284210
                                     int cflags)
Packit 284210
{
Packit 284210
    ap_regex_t *preg = apr_palloc(p, sizeof *preg);
Packit 284210

Packit 284210
    if (ap_regcomp(preg, pattern, cflags)) {
Packit 284210
        return NULL;
Packit 284210
    }
Packit 284210

Packit 284210
    apr_pool_cleanup_register(p, (void *) preg, regex_cleanup,
Packit 284210
                              apr_pool_cleanup_null);
Packit 284210

Packit 284210
    return preg;
Packit 284210
}
Packit 284210

Packit 284210
AP_DECLARE(void) ap_regfree(ap_regex_t *preg)
Packit 284210
{
Packit 284210
(pcre_free)(preg->re_pcre);
Packit 284210
}
Packit 284210

Packit 284210
AP_DECLARE(int) ap_regcomp(ap_regex_t *preg, const char *pattern, int cflags)
Packit 284210
{
Packit 284210
const char *errorptr;
Packit 284210
int erroffset;
Packit 284210
int options = 0;
Packit 284210
int nsub = 0;
Packit 284210

Packit 284210
if ((cflags & AP_REG_ICASE) != 0) options |= PCRE_CASELESS;
Packit 284210
if ((cflags & AP_REG_NEWLINE) != 0) options |= PCRE_MULTILINE;
Packit 284210

Packit 284210
preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset, NULL);
Packit 284210
preg->re_erroffset = erroffset;
Packit 284210

Packit 284210
if (preg->re_pcre == NULL) return AP_REG_INVARG;
Packit 284210

Packit 284210
pcre_fullinfo((const pcre *)preg->re_pcre, NULL, PCRE_INFO_CAPTURECOUNT, &nsub);
Packit 284210
preg->re_nsub = nsub;
Packit 284210
return 0;
Packit 284210
}
Packit 284210

Packit 284210
#ifndef POSIX_MALLOC_THRESHOLD
Packit 284210
#define POSIX_MALLOC_THRESHOLD (10)
Packit 284210
#endif
Packit 284210

Packit 284210
AP_DECLARE(int) ap_regexec(const ap_regex_t *preg, const char *string,
Packit 284210
                           apr_size_t nmatch, ap_regmatch_t pmatch[],
Packit 284210
                           int eflags)
Packit 284210
{
Packit 284210
int rc;
Packit 284210
int options = 0;
Packit 284210
int *ovector = NULL;
Packit 284210
int small_ovector[POSIX_MALLOC_THRESHOLD * 3];
Packit 284210
int allocated_ovector = 0;
Packit 284210

Packit 284210
if ((eflags & AP_REG_NOTBOL) != 0) options |= PCRE_NOTBOL;
Packit 284210
if ((eflags & AP_REG_NOTEOL) != 0) options |= PCRE_NOTEOL;
Packit 284210

Packit 284210
((ap_regex_t *)preg)->re_erroffset = (apr_size_t)(-1);  /* Only has meaning after compile */
Packit 284210

Packit 284210
if (nmatch > 0)
Packit 284210
  {
Packit 284210
  if (nmatch <= POSIX_MALLOC_THRESHOLD)
Packit 284210
    {
Packit 284210
    ovector = &(small_ovector[0]);
Packit 284210
    }
Packit 284210
  else
Packit 284210
    {
Packit 284210
    ovector = (int *)malloc(sizeof(int) * nmatch * 3);
Packit 284210
    if (ovector == NULL) return AP_REG_ESPACE;
Packit 284210
    allocated_ovector = 1;
Packit 284210
    }
Packit 284210
  }
Packit 284210

Packit 284210
rc = pcre_exec((const pcre *)preg->re_pcre, NULL, string, (int)strlen(string),
Packit 284210
  0, options, ovector, nmatch * 3);
Packit 284210

Packit 284210
if (rc == 0) rc = nmatch;    /* All captured slots were filled in */
Packit 284210

Packit 284210
if (rc >= 0)
Packit 284210
  {
Packit 284210
  apr_size_t i;
Packit 284210
  for (i = 0; i < (apr_size_t)rc; i++)
Packit 284210
    {
Packit 284210
    pmatch[i].rm_so = ovector[i*2];
Packit 284210
    pmatch[i].rm_eo = ovector[i*2+1];
Packit 284210
    }
Packit 284210
  if (allocated_ovector) free(ovector);
Packit 284210
  for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
Packit 284210
  return 0;
Packit 284210
  }
Packit 284210

Packit 284210
else
Packit 284210
  {
Packit 284210
  if (allocated_ovector) free(ovector);
Packit 284210
  switch(rc)
Packit 284210
    {
Packit 284210
    case PCRE_ERROR_NOMATCH: return AP_REG_NOMATCH;
Packit 284210
    case PCRE_ERROR_NULL: return AP_REG_INVARG;
Packit 284210
    case PCRE_ERROR_BADOPTION: return AP_REG_INVARG;
Packit 284210
    case PCRE_ERROR_BADMAGIC: return AP_REG_INVARG;
Packit 284210
    case PCRE_ERROR_UNKNOWN_NODE: return AP_REG_ASSERT;
Packit 284210
    case PCRE_ERROR_NOMEMORY: return AP_REG_ESPACE;
Packit 284210
#ifdef PCRE_ERROR_MATCHLIMIT
Packit 284210
    case PCRE_ERROR_MATCHLIMIT: return AP_REG_ESPACE;
Packit 284210
#endif
Packit 284210
#ifdef PCRE_ERROR_BADUTF8
Packit 284210
    case PCRE_ERROR_BADUTF8: return AP_REG_INVARG;
Packit 284210
#endif
Packit 284210
#ifdef PCRE_ERROR_BADUTF8_OFFSET
Packit 284210
    case PCRE_ERROR_BADUTF8_OFFSET: return AP_REG_INVARG;
Packit 284210
#endif
Packit 284210
    default: return AP_REG_ASSERT;
Packit 284210
    }
Packit 284210
  }
Packit 284210
}
Packit 284210