Blame include/util_varbuf.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
/**
Packit 90a5c9
 * @file util_varbuf.h
Packit 90a5c9
 * @brief Apache resizable variable length buffer library
Packit 90a5c9
 *
Packit 90a5c9
 * @defgroup APACHE_CORE_VARBUF Variable length buffer library
Packit 90a5c9
 * @ingroup APACHE_CORE
Packit 90a5c9
 *
Packit 90a5c9
 * This set of functions provides resizable buffers. While the primary
Packit 90a5c9
 * usage is with NUL-terminated strings, most functions also work with
Packit 90a5c9
 * arbitrary binary data.
Packit 90a5c9
 * 
Packit 90a5c9
 * @{
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#ifndef AP_VARBUF_H
Packit 90a5c9
#define AP_VARBUF_H
Packit 90a5c9
Packit 90a5c9
#include "apr.h"
Packit 90a5c9
#include "apr_allocator.h"
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
extern "C" {
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#define AP_VARBUF_UNKNOWN APR_SIZE_MAX
Packit 90a5c9
struct ap_varbuf_info;
Packit 90a5c9
Packit 90a5c9
/** A resizable buffer. */
Packit 90a5c9
struct ap_varbuf {
Packit 90a5c9
    /** The actual buffer; will point to a const '\\0' if avail == 0 and
Packit 90a5c9
     *  to memory of the same lifetime as the pool otherwise. */
Packit 90a5c9
    char *buf;
Packit 90a5c9
Packit 90a5c9
    /** Allocated size of the buffer (minus one for the final \\0);
Packit 90a5c9
     *  must only be changed using ap_varbuf_grow(). */
Packit 90a5c9
    apr_size_t avail;
Packit 90a5c9
Packit 90a5c9
    /** Length of string in buffer, or AP_VARBUF_UNKNOWN. This determines how
Packit 90a5c9
     *  much memory is copied by ap_varbuf_grow() and where
Packit 90a5c9
     *  ap_varbuf_strmemcat() will append to the buffer. */
Packit 90a5c9
    apr_size_t strlen;
Packit 90a5c9
Packit 90a5c9
    /** The pool for memory allocations and for registering the cleanup;
Packit 90a5c9
     *  the buffer memory will be released when this pool is cleared. */
Packit 90a5c9
    apr_pool_t *pool;
Packit 90a5c9
Packit 90a5c9
    /** Opaque info for memory allocation. */
Packit 90a5c9
    struct ap_varbuf_info *info;
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Initialize a resizable buffer. It is safe to re-initialize a previously
Packit 90a5c9
 * used ap_varbuf. The old buffer will be released when the corresponding
Packit 90a5c9
 * pool is cleared. The buffer remains usable until the pool is cleared,
Packit 90a5c9
 * even if the ap_varbuf was located on the stack and has gone out of scope.
Packit 90a5c9
 * @param   pool        The pool to allocate small buffers from and to register
Packit 90a5c9
 *                      the cleanup with
Packit 90a5c9
 * @param   vb          Pointer to the ap_varbuf struct
Packit 90a5c9
 * @param   init_size   The initial size of the buffer (see ap_varbuf_grow() for
Packit 90a5c9
 *                      details)
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_varbuf_init(apr_pool_t *pool, struct ap_varbuf *vb,
Packit 90a5c9
                                apr_size_t init_size);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Grow a resizable buffer. If the vb->buf cannot be grown in place, it will
Packit 90a5c9
 * be reallocated and the first vb->strlen + 1 bytes of memory will be copied
Packit 90a5c9
 * to the new location. If vb->strlen == AP_VARBUF_UNKNOWN, the whole buffer
Packit 90a5c9
 * is copied.
Packit 90a5c9
 * @param   vb          Pointer to the ap_varbuf struct
Packit 90a5c9
 * @param   new_size    The minimum new size of the buffer
Packit 90a5c9
 * @note ap_varbuf_grow() will usually at least double vb->buf's size with
Packit 90a5c9
 *       every invocation in order to reduce reallocations.
Packit 90a5c9
 * @note ap_varbuf_grow() will use pool memory for small and allocator
Packit 90a5c9
 *       mem nodes for larger allocations.
Packit 90a5c9
 * @note ap_varbuf_grow() will call vb->pool's abort function if out of memory.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_varbuf_grow(struct ap_varbuf *vb, apr_size_t new_size);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Release memory from a ap_varbuf immediately, if possible.
Packit 90a5c9
 * This allows to free large buffers before the corresponding pool is
Packit 90a5c9
 * cleared. Only larger allocations using mem nodes will be freed.
Packit 90a5c9
 * @param   vb          Pointer to the ap_varbuf struct
Packit 90a5c9
 * @note After ap_varbuf_free(), vb must not be used unless ap_varbuf_init()
Packit 90a5c9
 *       is called again.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_varbuf_free(struct ap_varbuf *vb);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Concatenate a string to an ap_varbuf. vb->strlen determines where
Packit 90a5c9
 * the string is appended in the buffer. If vb->strlen == AP_VARBUF_UNKNOWN,
Packit 90a5c9
 * the string will be appended at the first NUL byte in the buffer.
Packit 90a5c9
 * If len == 0, ap_varbuf_strmemcat() does nothing.
Packit 90a5c9
 * @param   vb      Pointer to the ap_varbuf struct
Packit 90a5c9
 * @param   str     The string to append; must be at least len bytes long
Packit 90a5c9
 * @param   len     The number of characters of *str to concatenate to the buf
Packit 90a5c9
 * @note vb->strlen will be set to the length of the new string
Packit 90a5c9
 * @note if len != 0, vb->buf will always be NUL-terminated
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_varbuf_strmemcat(struct ap_varbuf *vb, const char *str,
Packit 90a5c9
                                     int len);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Duplicate an ap_varbuf's content into pool memory.
Packit 90a5c9
 * @param   p           The pool to allocate from
Packit 90a5c9
 * @param   vb          The ap_varbuf to copy from
Packit 90a5c9
 * @param   prepend     An optional buffer to prepend (may be NULL)
Packit 90a5c9
 * @param   prepend_len Length of prepend
Packit 90a5c9
 * @param   append      An optional buffer to append (may be NULL)
Packit 90a5c9
 * @param   append_len  Length of append
Packit 90a5c9
 * @param   new_len     Where to store the length of the resulting string
Packit 90a5c9
 *                      (may be NULL)
Packit 90a5c9
 * @return The new string
Packit 90a5c9
 * @note ap_varbuf_pdup() uses vb->strlen to determine how much memory to
Packit 90a5c9
 *       copy. It works even if 0-bytes are embedded in vb->buf, prepend, or
Packit 90a5c9
 *       append.
Packit 90a5c9
 * @note If vb->strlen equals AP_VARBUF_UNKNOWN, it will be set to
Packit 90a5c9
 *       strlen(vb->buf).
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(char *) ap_varbuf_pdup(apr_pool_t *p, struct ap_varbuf *vb,
Packit 90a5c9
                                  const char *prepend, apr_size_t prepend_len,
Packit 90a5c9
                                  const char *append, apr_size_t append_len,
Packit 90a5c9
                                  apr_size_t *new_len);
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Concatenate a string to an ap_varbuf.
Packit 90a5c9
 * @param   vb      Pointer to the ap_varbuf struct
Packit 90a5c9
 * @param   str     The string to append
Packit 90a5c9
 * @note vb->strlen will be set to the length of the new string
Packit 90a5c9
 */
Packit 90a5c9
#define ap_varbuf_strcat(vb, str) ap_varbuf_strmemcat(vb, str, strlen(str))
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Perform string substitutions based on regexp match, using an ap_varbuf.
Packit 90a5c9
 * This function behaves like ap_pregsub(), but appends to an ap_varbuf
Packit 90a5c9
 * instead of allocating the result from a pool.
Packit 90a5c9
 * @param   vb      The ap_varbuf to which the string will be appended
Packit 90a5c9
 * @param   input   An arbitrary string containing $1 through $9. These are
Packit 90a5c9
 *                  replaced with the corresponding matched sub-expressions
Packit 90a5c9
 * @param   source  The string that was originally matched to the regex
Packit 90a5c9
 * @param   nmatch  The nmatch returned from ap_pregex
Packit 90a5c9
 * @param   pmatch  The pmatch array returned from ap_pregex
Packit 90a5c9
 * @param   maxlen  The maximum string length to append to vb, 0 for unlimited
Packit 90a5c9
 * @return APR_SUCCESS if successful
Packit 90a5c9
 * @note Just like ap_pregsub(), this function does not copy the part of
Packit 90a5c9
 *       *source before the matching part (i.e. the first pmatch[0].rm_so
Packit 90a5c9
 *       characters).
Packit 90a5c9
 * @note If vb->strlen equals AP_VARBUF_UNKNOWN, it will be set to
Packit 90a5c9
 *       strlen(vb->buf) first.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_varbuf_regsub(struct ap_varbuf *vb,
Packit 90a5c9
                                          const char *input,
Packit 90a5c9
                                          const char *source,
Packit 90a5c9
                                          apr_size_t nmatch,
Packit 90a5c9
                                          ap_regmatch_t pmatch[],
Packit 90a5c9
                                          apr_size_t maxlen);
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Read a line from an ap_configfile_t and append it to an ap_varbuf.
Packit 90a5c9
 * @param   vb      Pointer to the ap_varbuf struct
Packit 90a5c9
 * @param   cfp     Pointer to the ap_configfile_t
Packit 90a5c9
 * @param   max_len Maximum line length, including leading/trailing whitespace
Packit 90a5c9
 * @return See ap_cfg_getline()
Packit 90a5c9
 * @note vb->strlen will be set to the length of the line
Packit 90a5c9
 * @note If vb->strlen equals AP_VARBUF_UNKNOWN, it will be set to
Packit 90a5c9
 *       strlen(vb->buf) first.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_varbuf_cfg_getline(struct ap_varbuf *vb,
Packit 90a5c9
                                               ap_configfile_t *cfp,
Packit 90a5c9
                                               apr_size_t max_len);
Packit 90a5c9
Packit 90a5c9
#ifdef __cplusplus
Packit 90a5c9
}
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#endif  /* !AP_VARBUF_H */
Packit 90a5c9
/** @} */