membuffer.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2000-2003 Intel Corporation
  4. * All rights reserved.
  5. * Copyright (c) 2012 France Telecom All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * - Neither name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  27. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************/
  32. #ifndef GENLIB_UTIL_MEMBUFFER_H
  33. #define GENLIB_UTIL_MEMBUFFER_H
  34. /*!
  35. * \file
  36. */
  37. #include <stdlib.h>
  38. #include "upnputil.h"
  39. #define MINVAL( a, b ) ( (a) < (b) ? (a) : (b) )
  40. #define MAXVAL( a, b ) ( (a) > (b) ? (a) : (b) )
  41. /*! pointer to a chunk of memory. */
  42. typedef struct {
  43. /*! start of memory (read/write). */
  44. char *buf;
  45. /*! length of memory (read-only). */
  46. size_t length;
  47. } memptr;
  48. /*! Maintains a block of dynamically allocated memory
  49. * note: Total length/capacity should not exceed MAX_INT */
  50. typedef struct {
  51. /*! mem buffer; must not write beyond buf[length-1] (read/write). */
  52. char *buf;
  53. /*! length of buffer (read-only). */
  54. size_t length;
  55. /*! total allocated memory (read-only). */
  56. size_t capacity;
  57. /*! used to increase size; MUST be > 0; (read/write). */
  58. size_t size_inc;
  59. /*! default value of size_inc. */
  60. #define MEMBUF_DEF_SIZE_INC (size_t)5
  61. } membuffer;
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif /* __cplusplus */
  65. /*!
  66. * \brief Allocate memory and copy information from the input string to the
  67. * newly allocated memory.
  68. *
  69. * \return Pointer to the newly allocated memory.
  70. * NULL if memory cannot be allocated.
  71. */
  72. char *str_alloc(
  73. /*! [in] Input string object. */
  74. const char *str,
  75. /*! [in] Input string length. */
  76. size_t str_len);
  77. /*!
  78. * \brief Compares characters of strings passed for number of bytes.
  79. * If equal for the number of bytes, the length of the bytes determines
  80. * which buffer is shorter.
  81. *
  82. * \return
  83. * \li < 0 string1 substring less than string2 substring
  84. * \li == 0 string1 substring identical to string2 substring
  85. * \li > 0 string1 substring greater than string2 substring
  86. */
  87. int memptr_cmp(
  88. /*! [in] Input memory object. */
  89. memptr *m,
  90. /*! [in] Constatnt string for the memory object to be compared with. */
  91. const char *s);
  92. /*!
  93. * \brief Compares characters of 2 strings irrespective of the case for a
  94. * specific count of bytes.
  95. *
  96. * If the character comparison is the same the length of the 2 srings
  97. * determines the shorter of the 2 strings.
  98. *
  99. * \return
  100. * \li < 0 string1 substring less than string2 substring
  101. * \li == 0 string1 substring identical to string2 substring
  102. * \li > 0 string1 substring greater than string2 substring
  103. */
  104. int memptr_cmp_nocase(
  105. /*! [in] Input memory object. */
  106. memptr *m,
  107. /*! [in] Constatnt string for the memory object to be compared with. */
  108. const char *s);
  109. /*!
  110. * \brief Increases or decreases buffer cap so that at least 'new_length'
  111. * bytes can be stored.
  112. *
  113. * \return
  114. * \li UPNP_E_SUCCESS - On Success
  115. * \li UPNP_E_OUTOF_MEMORY - On failure to allocate memory.
  116. */
  117. int membuffer_set_size(
  118. /*! [in,out] buffer whose size is to be modified. */
  119. membuffer *m,
  120. /*! [in] new size to which the buffer will be modified. */
  121. size_t new_length);
  122. /*!
  123. * \brief Wrapper to membuffer_initialize().
  124. *
  125. * Set the size of the buffer to MEMBUF_DEF_SIZE_INC and Initializes
  126. * m->buf to NULL, length = 0.
  127. */
  128. void membuffer_init(
  129. /*! [in,out] Buffer to be initialized. */
  130. membuffer *m);
  131. /*!
  132. * \brief Free's memory allocated for membuffer* m.
  133. */
  134. void membuffer_destroy(
  135. /*! [in,out] Buffer to be destroyed. */
  136. membuffer *m);
  137. /*!
  138. * \brief Allocate memory to membuffer *m and copy the contents of the in
  139. * parameter const void *buf.
  140. *
  141. * \return
  142. * \li UPNP_E_SUCCESS
  143. * \li UPNP_E_OUTOF_MEMORY
  144. */
  145. int membuffer_assign(
  146. /*! [in,out] Buffer whose memory is to be allocated and assigned. */
  147. membuffer *m,
  148. /*! [in] Source buffer whose contents will be copied. */
  149. const void *buf,
  150. /*! [in] Length of the source buffer. */
  151. size_t buf_len);
  152. /*!
  153. * \brief Wrapper function for membuffer_assign().
  154. *
  155. * \return
  156. * \li UPNP_E_SUCCESS
  157. * \li UPNP_E_OUTOF_MEMORY
  158. */
  159. int membuffer_assign_str(
  160. /*! [in,out] Buffer to be allocated and assigned. */
  161. membuffer *m,
  162. /*! [in] Source buffer whose contents will be copied. */
  163. const char *c_str);
  164. /*!
  165. * \brief Invokes function to appends data from a constant buffer to the buffer.
  166. *
  167. * \return int.
  168. */
  169. int membuffer_append(
  170. /*! [in,out] Buffer whose memory is to be appended. */
  171. membuffer *m,
  172. /*! [in] Source buffer whose contents will be copied. */
  173. const void *buf,
  174. /*! [in] Length of the source buffer. */
  175. size_t buf_len);
  176. /*!
  177. * \brief Invokes function to appends data from a constant string to the buffer.
  178. *
  179. * \return int.
  180. */
  181. int membuffer_append_str(
  182. /*! [in,out] Buffer whose memory is to be appended. */
  183. membuffer *m,
  184. /*! [in] Source buffer whose contents will be copied. */
  185. const char *c_str);
  186. /*!
  187. * \brief Allocates memory for the new data to be inserted. Does
  188. * memory management by moving the data from the existing memory to
  189. * the newly allocated memory and then appending the new data.
  190. *
  191. * \return 0 if successful, error code if error.
  192. */
  193. int membuffer_insert(
  194. /*! [in,out] Buffer whose memory size is to be increased and appended. */
  195. membuffer * m,
  196. /*! [in] source buffer whose contents will be copied. */
  197. const void *buf,
  198. /*! [in] size of the source buffer. */
  199. size_t buf_len,
  200. /*! [in] index to determine the bounds while movinf the data. */
  201. size_t index);
  202. /*!
  203. * \brief Shrink the size of the buffer depending on the current size of the
  204. * bufer and te input parameters. Move contents from the old buffer to the
  205. * new sized buffer.
  206. */
  207. void membuffer_delete(
  208. /*! [in,out] Buffer whose memory size is to be decreased and copied
  209. * to the modified location. */
  210. membuffer * m,
  211. /*! [in] Index to determine bounds while moving data. */
  212. size_t index,
  213. /*! [in] Number of bytes that the data needs to shrink by. */
  214. size_t num_bytes);
  215. /*
  216. * \brief Detaches current buffer and returns it. The caller must free the
  217. * returned buffer using free(). After this call, length becomes 0.
  218. *
  219. * \return A pointer to the current buffer.
  220. */
  221. char *membuffer_detach(
  222. /*! [in,out] Buffer to be returned and updated. */
  223. membuffer *m);
  224. /*
  225. * \brief Free existing memory in membuffer and assign the new buffer in its
  226. * place.
  227. *
  228. * \note 'new_buf' must be allocted using malloc or realloc so that it can be
  229. * freed using free().
  230. */
  231. void membuffer_attach(
  232. /*! [in,out] Buffer to be updated. */
  233. membuffer *m,
  234. /*! [in] Source buffer which will be assigned to the buffer to be
  235. * updated. */
  236. char *new_buf,
  237. /*! [in] Length of the source buffer. */
  238. size_t buf_len);
  239. #ifdef __cplusplus
  240. } /* extern "C" */
  241. #endif /* __cplusplus */
  242. #endif /* GENLIB_UTIL_MEMBUFFER_H */