github.com/krum110487/go-htaccess@v0.0.0-20240316004156-60641c8e7598/tests/data/apache_2_4_58/include/util_varbuf.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 /** 18 * @file util_varbuf.h 19 * @brief Apache resizable variable length buffer library 20 * 21 * @defgroup APACHE_CORE_VARBUF Variable length buffer library 22 * @ingroup APACHE_CORE 23 * 24 * This set of functions provides resizable buffers. While the primary 25 * usage is with NUL-terminated strings, most functions also work with 26 * arbitrary binary data. 27 * 28 * @{ 29 */ 30 31 #ifndef AP_VARBUF_H 32 #define AP_VARBUF_H 33 34 #include "apr.h" 35 #include "apr_allocator.h" 36 37 #include "httpd.h" 38 39 #ifdef __cplusplus 40 extern "C" { 41 #endif 42 43 #define AP_VARBUF_UNKNOWN APR_SIZE_MAX 44 struct ap_varbuf_info; 45 46 /** A resizable buffer. */ 47 struct ap_varbuf { 48 /** The actual buffer; will point to a const '\\0' if avail == 0 and 49 * to memory of the same lifetime as the pool otherwise. */ 50 char *buf; 51 52 /** Allocated size of the buffer (minus one for the final \\0); 53 * must only be changed using ap_varbuf_grow(). */ 54 apr_size_t avail; 55 56 /** Length of string in buffer, or AP_VARBUF_UNKNOWN. This determines how 57 * much memory is copied by ap_varbuf_grow() and where 58 * ap_varbuf_strmemcat() will append to the buffer. */ 59 apr_size_t strlen; 60 61 /** The pool for memory allocations and for registering the cleanup; 62 * the buffer memory will be released when this pool is cleared. */ 63 apr_pool_t *pool; 64 65 /** Opaque info for memory allocation. */ 66 struct ap_varbuf_info *info; 67 }; 68 69 /** 70 * Initialize a resizable buffer. It is safe to re-initialize a previously 71 * used ap_varbuf. The old buffer will be released when the corresponding 72 * pool is cleared. The buffer remains usable until the pool is cleared, 73 * even if the ap_varbuf was located on the stack and has gone out of scope. 74 * @param pool The pool to allocate small buffers from and to register 75 * the cleanup with 76 * @param vb Pointer to the ap_varbuf struct 77 * @param init_size The initial size of the buffer (see ap_varbuf_grow() for 78 * details) 79 */ 80 AP_DECLARE(void) ap_varbuf_init(apr_pool_t *pool, struct ap_varbuf *vb, 81 apr_size_t init_size); 82 83 /** 84 * Grow a resizable buffer. If the vb->buf cannot be grown in place, it will 85 * be reallocated and the first vb->strlen + 1 bytes of memory will be copied 86 * to the new location. If vb->strlen == AP_VARBUF_UNKNOWN, the whole buffer 87 * is copied. 88 * @param vb Pointer to the ap_varbuf struct 89 * @param new_size The minimum new size of the buffer 90 * @note ap_varbuf_grow() will usually at least double vb->buf's size with 91 * every invocation in order to reduce reallocations. 92 * @note ap_varbuf_grow() will use pool memory for small and allocator 93 * mem nodes for larger allocations. 94 * @note ap_varbuf_grow() will call vb->pool's abort function if out of memory. 95 */ 96 AP_DECLARE(void) ap_varbuf_grow(struct ap_varbuf *vb, apr_size_t new_size); 97 98 /** 99 * Release memory from a ap_varbuf immediately, if possible. 100 * This allows to free large buffers before the corresponding pool is 101 * cleared. Only larger allocations using mem nodes will be freed. 102 * @param vb Pointer to the ap_varbuf struct 103 * @note After ap_varbuf_free(), vb must not be used unless ap_varbuf_init() 104 * is called again. 105 */ 106 AP_DECLARE(void) ap_varbuf_free(struct ap_varbuf *vb); 107 108 /** 109 * Concatenate a string to an ap_varbuf. vb->strlen determines where 110 * the string is appended in the buffer. If vb->strlen == AP_VARBUF_UNKNOWN, 111 * the string will be appended at the first NUL byte in the buffer. 112 * If len == 0, ap_varbuf_strmemcat() does nothing. 113 * @param vb Pointer to the ap_varbuf struct 114 * @param str The string to append; must be at least len bytes long 115 * @param len The number of characters of *str to concatenate to the buf 116 * @note vb->strlen will be set to the length of the new string 117 * @note if len != 0, vb->buf will always be NUL-terminated 118 */ 119 AP_DECLARE(void) ap_varbuf_strmemcat(struct ap_varbuf *vb, const char *str, 120 int len); 121 122 /** 123 * Duplicate an ap_varbuf's content into pool memory. 124 * @param p The pool to allocate from 125 * @param vb The ap_varbuf to copy from 126 * @param prepend An optional buffer to prepend (may be NULL) 127 * @param prepend_len Length of prepend 128 * @param append An optional buffer to append (may be NULL) 129 * @param append_len Length of append 130 * @param new_len Where to store the length of the resulting string 131 * (may be NULL) 132 * @return The new string 133 * @note ap_varbuf_pdup() uses vb->strlen to determine how much memory to 134 * copy. It works even if 0-bytes are embedded in vb->buf, prepend, or 135 * append. 136 * @note If vb->strlen equals AP_VARBUF_UNKNOWN, it will be set to 137 * strlen(vb->buf). 138 */ 139 AP_DECLARE(char *) ap_varbuf_pdup(apr_pool_t *p, struct ap_varbuf *vb, 140 const char *prepend, apr_size_t prepend_len, 141 const char *append, apr_size_t append_len, 142 apr_size_t *new_len); 143 144 145 /** 146 * Concatenate a string to an ap_varbuf. 147 * @param vb Pointer to the ap_varbuf struct 148 * @param str The string to append 149 * @note vb->strlen will be set to the length of the new string 150 */ 151 #define ap_varbuf_strcat(vb, str) ap_varbuf_strmemcat(vb, str, strlen(str)) 152 153 /** 154 * Perform string substitutions based on regexp match, using an ap_varbuf. 155 * This function behaves like ap_pregsub(), but appends to an ap_varbuf 156 * instead of allocating the result from a pool. 157 * @param vb The ap_varbuf to which the string will be appended 158 * @param input An arbitrary string containing $1 through $9. These are 159 * replaced with the corresponding matched sub-expressions 160 * @param source The string that was originally matched to the regex 161 * @param nmatch The nmatch returned from ap_pregex 162 * @param pmatch The pmatch array returned from ap_pregex 163 * @param maxlen The maximum string length to append to vb, 0 for unlimited 164 * @return APR_SUCCESS if successful 165 * @note Just like ap_pregsub(), this function does not copy the part of 166 * *source before the matching part (i.e. the first pmatch[0].rm_so 167 * characters). 168 * @note If vb->strlen equals AP_VARBUF_UNKNOWN, it will be set to 169 * strlen(vb->buf) first. 170 */ 171 AP_DECLARE(apr_status_t) ap_varbuf_regsub(struct ap_varbuf *vb, 172 const char *input, 173 const char *source, 174 apr_size_t nmatch, 175 ap_regmatch_t pmatch[], 176 apr_size_t maxlen); 177 178 /** 179 * Read a line from an ap_configfile_t and append it to an ap_varbuf. 180 * @param vb Pointer to the ap_varbuf struct 181 * @param cfp Pointer to the ap_configfile_t 182 * @param max_len Maximum line length, including leading/trailing whitespace 183 * @return See ap_cfg_getline() 184 * @note vb->strlen will be set to the length of the line 185 * @note If vb->strlen equals AP_VARBUF_UNKNOWN, it will be set to 186 * strlen(vb->buf) first. 187 */ 188 AP_DECLARE(apr_status_t) ap_varbuf_cfg_getline(struct ap_varbuf *vb, 189 ap_configfile_t *cfp, 190 apr_size_t max_len); 191 192 #ifdef __cplusplus 193 } 194 #endif 195 196 #endif /* !AP_VARBUF_H */ 197 /** @} */