util_varbuf.h 8.3 KB

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