github.com/krum110487/go-htaccess@v0.0.0-20240316004156-60641c8e7598/tests/data/apache_2_4_58/include/ap_regex.h (about)

     1  /* Licensed to the Apache Software Foundation (ASF) under one or more
     2   * contributor license agreements.  See the NOTICE file distributed with
     3   * this work for additional information regarding copyright ownership.
     4   * The ASF licenses this file to You under the Apache License, Version 2.0
     5   * (the "License"); you may not use this file except in compliance with
     6   * the License.  You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  /* This code is based on pcreposix.h from the PCRE Library distribution,
    18   * as originally written by Philip Hazel <ph10@cam.ac.uk>, and forked by
    19   * the Apache HTTP Server project to provide POSIX-style regex function
    20   * wrappers around underlying PCRE library functions for httpd.
    21   * 
    22   * The original source file pcreposix.h is copyright and licensed as follows;
    23  
    24              Copyright (c) 1997-2004 University of Cambridge
    25  
    26  -----------------------------------------------------------------------------
    27  Redistribution and use in source and binary forms, with or without
    28  modification, are permitted provided that the following conditions are met:
    29  
    30      * Redistributions of source code must retain the above copyright notice,
    31        this list of conditions and the following disclaimer.
    32  
    33      * Redistributions in binary form must reproduce the above copyright
    34        notice, this list of conditions and the following disclaimer in the
    35        documentation and/or other materials provided with the distribution.
    36  
    37      * Neither the name of the University of Cambridge nor the names of its
    38        contributors may be used to endorse or promote products derived from
    39        this software without specific prior written permission.
    40  
    41  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    42  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    43  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    44  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    45  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    46  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    47  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    48  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    49  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    50  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    51  POSSIBILITY OF SUCH DAMAGE.
    52  -----------------------------------------------------------------------------
    53  */
    54  
    55  /**
    56   * @file ap_regex.h
    57   * @brief Apache Regex defines
    58   */
    59  
    60  #ifndef AP_REGEX_H
    61  #define AP_REGEX_H
    62  
    63  #include "apr.h"
    64  
    65  /* Allow for C++ users */
    66  
    67  #ifdef __cplusplus
    68  extern "C" {
    69  #endif
    70  
    71  /* Options for ap_regcomp, ap_regexec, and ap_rxplus versions: */
    72  
    73  #define AP_REG_ICASE    0x01 /** use a case-insensitive match */
    74  #define AP_REG_NEWLINE  0x02 /** don't match newlines against '.' etc */
    75  #define AP_REG_NOTBOL   0x04 /** ^ will not match against start-of-string */
    76  #define AP_REG_NOTEOL   0x08 /** $ will not match against end-of-string */
    77  
    78  #define AP_REG_EXTENDED (0)  /** unused */
    79  #define AP_REG_NOSUB    (0)  /** unused */
    80  
    81  #define AP_REG_MULTI 0x10    /* perl's /g (needs fixing) */
    82  #define AP_REG_NOMEM 0x20    /* nomem in our code */
    83  #define AP_REG_DOTALL 0x40   /* perl's /s flag */
    84  
    85  #define AP_REG_DOLLAR_ENDONLY 0x200 /* '$' matches at end of subject string only */
    86  
    87  #define AP_REG_NO_DEFAULT 0x400 /**< Don't implicitely add AP_REG_DEFAULT options */
    88  
    89  #define AP_REG_MATCH "MATCH_" /**< suggested prefix for ap_regname */
    90  
    91  #define AP_REG_DEFAULT (AP_REG_DOTALL|AP_REG_DOLLAR_ENDONLY)
    92  
    93  /* Arguments for ap_pcre_version_string */
    94  enum {
    95    AP_REG_PCRE_COMPILED = 0, /** PCRE version used during program compilation */
    96    AP_REG_PCRE_LOADED        /** PCRE version loaded at runtime */
    97  };
    98  
    99  /* Error values: */
   100  enum {
   101    AP_REG_ASSERT = 1,  /** internal error ? */
   102    AP_REG_ESPACE,      /** failed to get memory */
   103    AP_REG_INVARG,      /** invalid argument */
   104    AP_REG_NOMATCH      /** match failed */
   105  };
   106  
   107  /* The structure representing a compiled regular expression. */
   108  typedef struct {
   109      void *re_pcre;
   110      int re_nsub;
   111      apr_size_t re_erroffset;
   112  } ap_regex_t;
   113  
   114  /* The structure in which a captured offset is returned. */
   115  typedef struct {
   116      int rm_so;
   117      int rm_eo;
   118  } ap_regmatch_t;
   119  
   120  /* The functions */
   121  
   122  /**
   123   * Return PCRE version string.
   124   * @param which Either AP_REG_PCRE_COMPILED (PCRE version used
   125   *              during program compilation) or AP_REG_PCRE_LOADED
   126   *              (PCRE version used at runtime)
   127   * @return The PCRE version string
   128   */
   129  AP_DECLARE(const char *) ap_pcre_version_string(int which);
   130  
   131  /**
   132   * Get default compile flags
   133   * @return Bitwise OR of AP_REG_* flags
   134   */
   135  AP_DECLARE(int) ap_regcomp_get_default_cflags(void);
   136  
   137  /**
   138   * Set default compile flags
   139   * @param cflags Bitwise OR of AP_REG_* flags
   140   */
   141  AP_DECLARE(void) ap_regcomp_set_default_cflags(int cflags);
   142  
   143  /**
   144   * Get the AP_REG_* corresponding to the string.
   145   * @param name The name (i.e. AP_REG_<name>)
   146   * @return The AP_REG_*, or zero if the string is unknown
   147   *
   148   */
   149  AP_DECLARE(int) ap_regcomp_default_cflag_by_name(const char *name);
   150  
   151  /**
   152   * Compile a regular expression.
   153   * @param preg Returned compiled regex
   154   * @param regex The regular expression string
   155   * @param cflags Bitwise OR of AP_REG_* flags (ICASE and NEWLINE supported,
   156   *                                             other flags are ignored)
   157   * @return Zero on success or non-zero on error
   158   */
   159  AP_DECLARE(int) ap_regcomp(ap_regex_t *preg, const char *regex, int cflags);
   160  
   161  /**
   162   * Match a NUL-terminated string against a pre-compiled regex.
   163   * @param preg The pre-compiled regex
   164   * @param string The string to match
   165   * @param nmatch Provide information regarding the location of any matches
   166   * @param pmatch Provide information regarding the location of any matches
   167   * @param eflags Bitwise OR of AP_REG_* flags (NOTBOL and NOTEOL supported,
   168   *                                             other flags are ignored)
   169   * @return 0 for successful match, \p AP_REG_NOMATCH otherwise
   170   */
   171  AP_DECLARE(int) ap_regexec(const ap_regex_t *preg, const char *string,
   172                             apr_size_t nmatch, ap_regmatch_t *pmatch, int eflags);
   173  
   174  /**
   175   * Match a string with given length against a pre-compiled regex. The string
   176   * does not need to be NUL-terminated.
   177   * @param preg The pre-compiled regex
   178   * @param buff The string to match
   179   * @param len Length of the string to match
   180   * @param nmatch Provide information regarding the location of any matches
   181   * @param pmatch Provide information regarding the location of any matches
   182   * @param eflags Bitwise OR of AP_REG_* flags (NOTBOL and NOTEOL supported,
   183   *                                             other flags are ignored)
   184   * @return 0 for successful match, AP_REG_NOMATCH otherwise
   185   */
   186  AP_DECLARE(int) ap_regexec_len(const ap_regex_t *preg, const char *buff,
   187                                 apr_size_t len, apr_size_t nmatch,
   188                                 ap_regmatch_t *pmatch, int eflags);
   189  
   190  /**
   191   * Return the error code returned by regcomp or regexec into error messages
   192   * @param errcode the error code returned by regexec or regcomp
   193   * @param preg The precompiled regex
   194   * @param errbuf A buffer to store the error in
   195   * @param errbuf_size The size of the buffer
   196   */
   197  AP_DECLARE(apr_size_t) ap_regerror(int errcode, const ap_regex_t *preg,
   198                                     char *errbuf, apr_size_t errbuf_size);
   199  
   200  /**
   201   * Return an array of named regex backreferences
   202   * @param preg The precompiled regex
   203   * @param names The array to which the names will be added
   204   * @param prefix An optional prefix to add to the returned names.  AP_REG_MATCH
   205   * is the recommended prefix.
   206   * @param upper If non zero, uppercase the names
   207   */
   208  AP_DECLARE(int) ap_regname(const ap_regex_t *preg,
   209                             apr_array_header_t *names, const char *prefix,
   210                             int upper);
   211  
   212  /** Destroy a pre-compiled regex.
   213   * @param preg The pre-compiled regex to free.
   214   */
   215  AP_DECLARE(void) ap_regfree(ap_regex_t *preg);
   216  
   217  /* ap_rxplus: higher-level regexps */
   218  
   219  typedef struct {
   220      ap_regex_t rx;
   221      apr_uint32_t flags;
   222      const char *subs;
   223      const char *match;
   224      apr_size_t nmatch;
   225      ap_regmatch_t *pmatch;
   226  } ap_rxplus_t;
   227  
   228  /**
   229   * Compile a pattern into a regexp.
   230   * supports perl-like formats
   231   *    match-string
   232   *    /match-string/flags
   233   *    s/match-string/replacement-string/flags
   234   *    Intended to support more perl-like stuff as and when round tuits happen
   235   * match-string is anything supported by ap_regcomp
   236   * replacement-string is a substitution string as supported in ap_pregsub
   237   * flags should correspond with perl syntax: treat failure to do so as a bug
   238   *                                           (documentation TBD)
   239   * @param pool Pool to allocate from
   240   * @param pattern Pattern to compile
   241   * @return Compiled regexp, or NULL in case of compile/syntax error
   242   */
   243  AP_DECLARE(ap_rxplus_t*) ap_rxplus_compile(apr_pool_t *pool, const char *pattern);
   244  /**
   245   * Apply a regexp operation to a string.
   246   * @param pool Pool to allocate from
   247   * @param rx The regex match to apply
   248   * @param pattern The string to apply it to
   249   *                NOTE: This MUST be kept in scope to use regexp memory
   250   * @param newpattern The modified string (ignored if the operation doesn't
   251   *                                        modify the string)
   252   * @return Number of times a match happens.  Normally 0 (no match) or 1
   253   *         (match found), but may be greater if a transforming pattern
   254   *         is applied with the 'g' flag.
   255   */
   256  AP_DECLARE(int) ap_rxplus_exec(apr_pool_t *pool, ap_rxplus_t *rx,
   257                                 const char *pattern, char **newpattern);
   258  #ifdef DOXYGEN
   259  /**
   260   * Number of matches in the regexp operation's memory
   261   * This may be 0 if no match is in memory, or up to nmatch from compilation
   262   * @param rx The regexp
   263   * @return Number of matches in memory
   264   */
   265  AP_DECLARE(int) ap_rxplus_nmatch(ap_rxplus_t *rx);
   266  #else
   267  #define ap_rxplus_nmatch(rx) (((rx)->match != NULL) ? (rx)->nmatch : 0)
   268  #endif
   269  /**
   270   * Get a pointer to a match from regex memory
   271   * NOTE: this relies on the match pattern from the last call to
   272   *       ap_rxplus_exec still being valid (i.e. not freed or out-of-scope)
   273   * @param rx The regexp
   274   * @param n The match number to retrieve (must be between 0 and nmatch)
   275   * @param len Returns the length of the match.
   276   * @param match Returns the match pattern
   277   */
   278  AP_DECLARE(void) ap_rxplus_match(ap_rxplus_t *rx, int n, int *len,
   279                                   const char **match);
   280  /**
   281   * Get a match from regex memory in a string copy
   282   * NOTE: this relies on the match pattern from the last call to
   283   *       ap_rxplus_exec still being valid (i.e. not freed or out-of-scope)
   284   * @param pool Pool to allocate from
   285   * @param rx The regexp
   286   * @param n The match number to retrieve (must be between 0 and nmatch)
   287   * @return The matched string
   288   */
   289  AP_DECLARE(char*) ap_rxplus_pmatch(apr_pool_t *pool, ap_rxplus_t *rx, int n);
   290  
   291  #ifdef __cplusplus
   292  }   /* extern "C" */
   293  #endif
   294  
   295  #endif /* AP_REGEX_T */
   296