util_varbuf.h revision eaf6669b6ce9b9714e451d5cc81ba30c802c89fc
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/* Licensed to the Apache Software Foundation (ASF) under one or more
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * contributor license agreements. See the NOTICE file distributed with
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * this work for additional information regarding copyright ownership.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * The ASF licenses this file to You under the Apache License, Version 2.0
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * (the "License"); you may not use this file except in compliance with
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * the License. You may obtain a copy of the License at
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias *
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * http://www.apache.org/licenses/LICENSE-2.0
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias *
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * Unless required by applicable law or agreed to in writing, software
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * distributed under the License is distributed on an "AS IS" BASIS,
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * See the License for the specific language governing permissions and
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * limitations under the License.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/**
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @file util_varbuf.h
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias * @brief Apache resizable variable length buffer library
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#ifndef AP_VARBUF_H
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#define AP_VARBUF_H
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#include "apr.h"
990d9f14e81ad0ffb0fbec4224725b631f5fae27Jonathan von Schroeder#include "apr_allocator.h"
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#include "httpd.h"
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#ifdef __cplusplus
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogiasextern "C" {
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#endif
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#define AP_VARBUF_UNKNOWN APR_SIZE_MAX
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogiasstruct ap_varbuf_info;
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/** A resizable buffer */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogiasstruct ap_varbuf {
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias /** the actual buffer */
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias char *buf;
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias /** allocated size of the buffer (minus one for the final \0);
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias * must only be changed using ap_varbuf_grow() */
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias apr_size_t avail;
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias /** length of string in buffer, or AP_VARBUF_UNKNOWN. This determines how
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias * much memory is copied by ap_varbuf_grow() and where
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias * ap_varbuf_strmemcat() will append to the buffer. */
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias apr_size_t strlen;
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias /** the pool for memory allocations and for registering the cleanup;
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias * the buffer memory will be released when this pool is destroyed */
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias apr_pool_t *pool;
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias /** opaque info for memory allocation */
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias struct ap_varbuf_info *info;
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias};
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias/** initialize a resizable buffer. It is safe to re-initialize a prevously
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias * used ap_varbuf. The old buffer will be released when the corresponding
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * pool is destroyed.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @param pool the pool to allocate small buffers from and to register the
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * cleanup with
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @param vb pointer to the ap_varbuf struct
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @init_size the initial size of the buffer (see ap_varbuf_grow() for details)
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias */
658187feb755694eb5ff29561bda7109c22c743cAlexis TsogiasAP_DECLARE(void) ap_varbuf_init(apr_pool_t *pool, struct ap_varbuf *vb,
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias apr_size_t init_size);
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/** grow a resizable buffer. If the vb->buf cannot be grown in place, it will
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * be reallocated and up to vb->strlen + 1 bytes of memory will be copied to
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * the new location. If vb->strlen == AP_VARBUF_UNKNOWN, the whole buffer
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * is copied.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @param vb pointer to the ap_varbuf struct
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @param new_size the minimum new size of the buffer
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @note ap_varbuf_grow() will usually at least double vb->buf's size with
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * every invocation in order to reduce reallications.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @note ap_varbuf_grow() will use pool memory for small and allocator
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * mem nodes for larger allocations.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @note ap_varbuf_grow() will call vb->pool's abort function if out of memory.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias */
658187feb755694eb5ff29561bda7109c22c743cAlexis TsogiasAP_DECLARE(void) ap_varbuf_grow(struct ap_varbuf *vb, apr_size_t new_size);
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/** Release memory from a ap_varbuf immediately, if possible.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * This allows to free large buffers before the corresponding pool is
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * destroyed. Only larger allocations using mem nodes will be freed.
8fb127028cb7dd361e348a3252e33487f73428bcJonathan von Schroeder * @param vb pointer to the ap_varbuf struct
8fb127028cb7dd361e348a3252e33487f73428bcJonathan von Schroeder * @note After ap_varbuf_free(), vb must not be used unless ap_varbuf_init()
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * is called again.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias */
658187feb755694eb5ff29561bda7109c22c743cAlexis TsogiasAP_DECLARE(void) ap_varbuf_free(struct ap_varbuf *vb);
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/** Concatenate a string to an ap_varbuf
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @param vb pointer to the ap_varbuf struct
8fb127028cb7dd361e348a3252e33487f73428bcJonathan von Schroeder * @param str the string to append; must be at least len bytes long
8fb127028cb7dd361e348a3252e33487f73428bcJonathan von Schroeder * @param len the number of characters of *str to concatenate to the buf
8fb127028cb7dd361e348a3252e33487f73428bcJonathan von Schroeder * @note vb->strlen will be set to the length of the new string
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @note vb->buf will be null-terminated
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias */
658187feb755694eb5ff29561bda7109c22c743cAlexis TsogiasAP_DECLARE(void) ap_varbuf_strmemcat(struct ap_varbuf *vb, const char *str,
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias int len);
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/** Concatenate a string to an ap_varbuf
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @param vb pointer to the ap_varbuf struct
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @param str the string to append
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @note vb->strlen will be set to the length of the new string
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#define ap_varbuf_strcat(vb, str) ap_varbuf_strmemcat(vb, str, strlen(str))
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/** Read a line from an ap_configfile_t into an ap_varbuf.
8fb127028cb7dd361e348a3252e33487f73428bcJonathan von Schroeder * @param vb pointer to the ap_varbuf struct
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @param cfg pointer to the ap_configfile_t
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @param max_len (soft) limit for the size of the buffer
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @return see ap_cfg_getline()
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @note The buffer will not be grown once it has reached at least max_len
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * bytes. This means that the returned line can be longer than max_len.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * @note vb->strlen will be set to the length of the line
8394b397aadaf0c2bfc19c0628f17f83f031a759Jonathan von Schroeder */
c95de7451b35950f21196b610dab702730221a98Alexis TsogiasAP_DECLARE(apr_status_t) ap_varbuf_cfg_getline(struct ap_varbuf *vb,
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias ap_configfile_t *cfp,
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias apr_size_t max_len);
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias#ifdef __cplusplus
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias}
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias#endif
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias#endif /* !AP_VARBUF_H */
c95de7451b35950f21196b610dab702730221a98Alexis Tsogias