Blame include/ap_regex.h

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/* This code is based on pcreposix.h from the PCRE Library distribution,
Packit 90a5c9
 * as originally written by Philip Hazel <ph10@cam.ac.uk>, and forked by
Packit 90a5c9
 * the Apache HTTP Server project to provide POSIX-style regex function
Packit 90a5c9
 * wrappers around underlying PCRE library functions for httpd.
Packit 90a5c9
 * 
Packit 90a5c9
 * The original source file pcreposix.h is copyright and licensed as follows;
Packit 90a5c9
Packit 90a5c9
            Copyright (c) 1997-2004 University of Cambridge
Packit 90a5c9
Packit 90a5c9
-----------------------------------------------------------------------------
Packit 90a5c9
Redistribution and use in source and binary forms, with or without
Packit 90a5c9
modification, are permitted provided that the following conditions are met:
Packit 90a5c9
Packit 90a5c9
    * Redistributions of source code must retain the above copyright notice,
Packit 90a5c9
      this list of conditions and the following disclaimer.
Packit 90a5c9
Packit 90a5c9
    * Redistributions in binary form must reproduce the above copyright
Packit 90a5c9
      notice, this list of conditions and the following disclaimer in the
Packit 90a5c9
      documentation and/or other materials provided with the distribution.
Packit 90a5c9
Packit 90a5c9
    * Neither the name of the University of Cambridge nor the names of its
Packit 90a5c9
      contributors may be used to endorse or promote products derived from
Packit 90a5c9
      this software without specific prior written permission.
Packit 90a5c9
Packit 90a5c9
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 90a5c9
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 90a5c9
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 90a5c9
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit 90a5c9
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit 90a5c9
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit 90a5c9
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 90a5c9
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit 90a5c9
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit 90a5c9
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit 90a5c9
POSSIBILITY OF SUCH DAMAGE.
Packit 90a5c9
-----------------------------------------------------------------------------
Packit 90a5c9
*/
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * @file ap_regex.h
Packit 90a5c9
 * @brief Apache Regex defines
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#ifndef AP_REGEX_H
Packit 90a5c9
#define AP_REGEX_H
Packit 90a5c9
Packit 90a5c9
#include "apr.h"
Packit 90a5c9
Packit 90a5c9
/* Allow for C++ users */
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
extern "C" {
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
/* Options for ap_regcomp, ap_regexec, and ap_rxplus versions: */
Packit 90a5c9
Packit 90a5c9
#define AP_REG_ICASE    0x01 /** use a case-insensitive match */
Packit 90a5c9
#define AP_REG_NEWLINE  0x02 /** don't match newlines against '.' etc */
Packit 90a5c9
#define AP_REG_NOTBOL   0x04 /** ^ will not match against start-of-string */
Packit 90a5c9
#define AP_REG_NOTEOL   0x08 /** $ will not match against end-of-string */
Packit 90a5c9
Packit 90a5c9
#define AP_REG_EXTENDED (0)  /** unused */
Packit 90a5c9
#define AP_REG_NOSUB    (0)  /** unused */
Packit 90a5c9
Packit 90a5c9
#define AP_REG_MULTI 0x10    /* perl's /g (needs fixing) */
Packit 90a5c9
#define AP_REG_NOMEM 0x20    /* nomem in our code */
Packit 90a5c9
#define AP_REG_DOTALL 0x40   /* perl's /s flag */
Packit 90a5c9
Packit 90a5c9
#define AP_REG_DOLLAR_ENDONLY 0x200 /* '$' matches at end of subject string only */
Packit 90a5c9
Packit 90a5c9
#define AP_REG_MATCH "MATCH_" /** suggested prefix for ap_regname */
Packit 90a5c9
Packit 90a5c9
/* Error values: */
Packit 90a5c9
enum {
Packit 90a5c9
  AP_REG_ASSERT = 1,  /** internal error ? */
Packit 90a5c9
  AP_REG_ESPACE,      /** failed to get memory */
Packit 90a5c9
  AP_REG_INVARG,      /** invalid argument */
Packit 90a5c9
  AP_REG_NOMATCH      /** match failed */
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
/* The structure representing a compiled regular expression. */
Packit 90a5c9
typedef struct {
Packit 90a5c9
    void *re_pcre;
Packit 90a5c9
    int re_nsub;
Packit 90a5c9
    apr_size_t re_erroffset;
Packit 90a5c9
} ap_regex_t;
Packit 90a5c9
Packit 90a5c9
/* The structure in which a captured offset is returned. */
Packit 90a5c9
typedef struct {
Packit 90a5c9
    int rm_so;
Packit 90a5c9
    int rm_eo;
Packit 90a5c9
} ap_regmatch_t;
Packit 90a5c9
Packit 90a5c9
/* The functions */
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Get default compile flags
Packit 90a5c9
 * @return Bitwise OR of AP_REG_* flags
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_regcomp_get_default_cflags(void);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Set default compile flags
Packit 90a5c9
 * @param cflags Bitwise OR of AP_REG_* flags
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_regcomp_set_default_cflags(int cflags);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Get the AP_REG_* corresponding to the string.
Packit 90a5c9
 * @param name The name (i.e. AP_REG_<name>)
Packit 90a5c9
 * @return The AP_REG_*, or zero if the string is unknown
Packit 90a5c9
 *
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_regcomp_default_cflag_by_name(const char *name);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Compile a regular expression.
Packit 90a5c9
 * @param preg Returned compiled regex
Packit 90a5c9
 * @param regex The regular expression string
Packit 90a5c9
 * @param cflags Bitwise OR of AP_REG_* flags (ICASE and NEWLINE supported,
Packit 90a5c9
 *                                             other flags are ignored)
Packit 90a5c9
 * @return Zero on success or non-zero on error
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_regcomp(ap_regex_t *preg, const char *regex, int cflags);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Match a NUL-terminated string against a pre-compiled regex.
Packit 90a5c9
 * @param preg The pre-compiled regex
Packit 90a5c9
 * @param string The string to match
Packit 90a5c9
 * @param nmatch Provide information regarding the location of any matches
Packit 90a5c9
 * @param pmatch Provide information regarding the location of any matches
Packit 90a5c9
 * @param eflags Bitwise OR of AP_REG_* flags (NOTBOL and NOTEOL supported,
Packit 90a5c9
 *                                             other flags are ignored)
Packit 90a5c9
 * @return 0 for successful match, \p AP_REG_NOMATCH otherwise
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_regexec(const ap_regex_t *preg, const char *string,
Packit 90a5c9
                           apr_size_t nmatch, ap_regmatch_t *pmatch, int eflags);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Match a string with given length against a pre-compiled regex. The string
Packit 90a5c9
 * does not need to be NUL-terminated.
Packit 90a5c9
 * @param preg The pre-compiled regex
Packit 90a5c9
 * @param buff The string to match
Packit 90a5c9
 * @param len Length of the string to match
Packit 90a5c9
 * @param nmatch Provide information regarding the location of any matches
Packit 90a5c9
 * @param pmatch Provide information regarding the location of any matches
Packit 90a5c9
 * @param eflags Bitwise OR of AP_REG_* flags (NOTBOL and NOTEOL supported,
Packit 90a5c9
 *                                             other flags are ignored)
Packit 90a5c9
 * @return 0 for successful match, AP_REG_NOMATCH otherwise
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_regexec_len(const ap_regex_t *preg, const char *buff,
Packit 90a5c9
                               apr_size_t len, apr_size_t nmatch,
Packit 90a5c9
                               ap_regmatch_t *pmatch, int eflags);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Return the error code returned by regcomp or regexec into error messages
Packit 90a5c9
 * @param errcode the error code returned by regexec or regcomp
Packit 90a5c9
 * @param preg The precompiled regex
Packit 90a5c9
 * @param errbuf A buffer to store the error in
Packit 90a5c9
 * @param errbuf_size The size of the buffer
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(apr_size_t) ap_regerror(int errcode, const ap_regex_t *preg,
Packit 90a5c9
                                   char *errbuf, apr_size_t errbuf_size);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Return an array of named regex backreferences
Packit 90a5c9
 * @param preg The precompiled regex
Packit 90a5c9
 * @param names The array to which the names will be added
Packit 90a5c9
 * @param upper If non zero, uppercase the names
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_regname(const ap_regex_t *preg,
Packit 90a5c9
                           apr_array_header_t *names, const char *prefix,
Packit 90a5c9
                           int upper);
Packit 90a5c9
Packit 90a5c9
/** Destroy a pre-compiled regex.
Packit 90a5c9
 * @param preg The pre-compiled regex to free.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_regfree(ap_regex_t *preg);
Packit 90a5c9
Packit 90a5c9
/* ap_rxplus: higher-level regexps */
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    ap_regex_t rx;
Packit 90a5c9
    apr_uint32_t flags;
Packit 90a5c9
    const char *subs;
Packit 90a5c9
    const char *match;
Packit 90a5c9
    apr_size_t nmatch;
Packit 90a5c9
    ap_regmatch_t *pmatch;
Packit 90a5c9
} ap_rxplus_t;
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Compile a pattern into a regexp.
Packit 90a5c9
 * supports perl-like formats
Packit 90a5c9
 *    match-string
Packit 90a5c9
 *    /match-string/flags
Packit 90a5c9
 *    s/match-string/replacement-string/flags
Packit 90a5c9
 *    Intended to support more perl-like stuff as and when round tuits happen
Packit 90a5c9
 * match-string is anything supported by ap_regcomp
Packit 90a5c9
 * replacement-string is a substitution string as supported in ap_pregsub
Packit 90a5c9
 * flags should correspond with perl syntax: treat failure to do so as a bug
Packit 90a5c9
 *                                           (documentation TBD)
Packit 90a5c9
 * @param pool Pool to allocate from
Packit 90a5c9
 * @param pattern Pattern to compile
Packit 90a5c9
 * @return Compiled regexp, or NULL in case of compile/syntax error
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(ap_rxplus_t*) ap_rxplus_compile(apr_pool_t *pool, const char *pattern);
Packit 90a5c9
/**
Packit 90a5c9
 * Apply a regexp operation to a string.
Packit 90a5c9
 * @param pool Pool to allocate from
Packit 90a5c9
 * @param rx The regex match to apply
Packit 90a5c9
 * @param pattern The string to apply it to
Packit 90a5c9
 *                NOTE: This MUST be kept in scope to use regexp memory
Packit 90a5c9
 * @param newpattern The modified string (ignored if the operation doesn't
Packit 90a5c9
 *                                        modify the string)
Packit 90a5c9
 * @return Number of times a match happens.  Normally 0 (no match) or 1
Packit 90a5c9
 *         (match found), but may be greater if a transforming pattern
Packit 90a5c9
 *         is applied with the 'g' flag.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_rxplus_exec(apr_pool_t *pool, ap_rxplus_t *rx,
Packit 90a5c9
                               const char *pattern, char **newpattern);
Packit 90a5c9
#ifdef DOXYGEN
Packit 90a5c9
/**
Packit 90a5c9
 * Number of matches in the regexp operation's memory
Packit 90a5c9
 * This may be 0 if no match is in memory, or up to nmatch from compilation
Packit 90a5c9
 * @param rx The regexp
Packit 90a5c9
 * @return Number of matches in memory
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_rxplus_nmatch(ap_rxplus_t *rx);
Packit 90a5c9
#else
Packit 90a5c9
#define ap_rxplus_nmatch(rx) (((rx)->match != NULL) ? (rx)->nmatch : 0)
Packit 90a5c9
#endif
Packit 90a5c9
/**
Packit 90a5c9
 * Get a pointer to a match from regex memory
Packit 90a5c9
 * NOTE: this relies on the match pattern from the last call to
Packit 90a5c9
 *       ap_rxplus_exec still being valid (i.e. not freed or out-of-scope)
Packit 90a5c9
 * @param rx The regexp
Packit 90a5c9
 * @param n The match number to retrieve (must be between 0 and nmatch)
Packit 90a5c9
 * @param len Returns the length of the match.
Packit 90a5c9
 * @param match Returns the match pattern
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_rxplus_match(ap_rxplus_t *rx, int n, int *len,
Packit 90a5c9
                                 const char **match);
Packit 90a5c9
/**
Packit 90a5c9
 * Get a match from regex memory in a string copy
Packit 90a5c9
 * NOTE: this relies on the match pattern from the last call to
Packit 90a5c9
 *       ap_rxplus_exec still being valid (i.e. not freed or out-of-scope)
Packit 90a5c9
 * @param pool Pool to allocate from
Packit 90a5c9
 * @param rx The regexp
Packit 90a5c9
 * @param n The match number to retrieve (must be between 0 and nmatch)
Packit 90a5c9
 * @return The matched string
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(char*) ap_rxplus_pmatch(apr_pool_t *pool, ap_rxplus_t *rx, int n);
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
}   /* extern "C" */
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#endif /* AP_REGEX_T */
Packit 90a5c9