dynbuf.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "dynbuf.h"
  26. #include "curl_printf.h"
  27. #ifdef BUILDING_LIBCURL
  28. #include "curl_memory.h"
  29. #endif
  30. #include "memdebug.h"
  31. #define MIN_FIRST_ALLOC 32
  32. #define DYNINIT 0xbee51da /* random pattern */
  33. /*
  34. * Init a dynbuf struct.
  35. */
  36. void Curl_dyn_init(struct dynbuf *s, size_t toobig)
  37. {
  38. DEBUGASSERT(s);
  39. DEBUGASSERT(toobig);
  40. s->bufr = NULL;
  41. s->leng = 0;
  42. s->allc = 0;
  43. s->toobig = toobig;
  44. #ifdef DEBUGBUILD
  45. s->init = DYNINIT;
  46. #endif
  47. }
  48. /*
  49. * free the buffer and re-init the necessary fields. It doesn't touch the
  50. * 'init' field and thus this buffer can be reused to add data to again.
  51. */
  52. void Curl_dyn_free(struct dynbuf *s)
  53. {
  54. DEBUGASSERT(s);
  55. Curl_safefree(s->bufr);
  56. s->leng = s->allc = 0;
  57. }
  58. /*
  59. * Store/append an chunk of memory to the dynbuf.
  60. */
  61. static CURLcode dyn_nappend(struct dynbuf *s,
  62. const unsigned char *mem, size_t len)
  63. {
  64. size_t indx = s->leng;
  65. size_t a = s->allc;
  66. size_t fit = len + indx + 1; /* new string + old string + zero byte */
  67. /* try to detect if there's rubbish in the struct */
  68. DEBUGASSERT(s->init == DYNINIT);
  69. DEBUGASSERT(s->toobig);
  70. DEBUGASSERT(indx < s->toobig);
  71. DEBUGASSERT(!s->leng || s->bufr);
  72. DEBUGASSERT(a <= s->toobig);
  73. if(fit > s->toobig) {
  74. Curl_dyn_free(s);
  75. return CURLE_OUT_OF_MEMORY;
  76. }
  77. else if(!a) {
  78. DEBUGASSERT(!indx);
  79. /* first invoke */
  80. if(MIN_FIRST_ALLOC > s->toobig)
  81. a = s->toobig;
  82. else if(fit < MIN_FIRST_ALLOC)
  83. a = MIN_FIRST_ALLOC;
  84. else
  85. a = fit;
  86. }
  87. else {
  88. while(a < fit)
  89. a *= 2;
  90. if(a > s->toobig)
  91. /* no point in allocating a larger buffer than this is allowed to use */
  92. a = s->toobig;
  93. }
  94. if(a != s->allc) {
  95. /* this logic is not using Curl_saferealloc() to make the tool not have to
  96. include that as well when it uses this code */
  97. void *p = realloc(s->bufr, a);
  98. if(!p) {
  99. Curl_dyn_free(s);
  100. return CURLE_OUT_OF_MEMORY;
  101. }
  102. s->bufr = p;
  103. s->allc = a;
  104. }
  105. if(len)
  106. memcpy(&s->bufr[indx], mem, len);
  107. s->leng = indx + len;
  108. s->bufr[s->leng] = 0;
  109. return CURLE_OK;
  110. }
  111. /*
  112. * Clears the string, keeps the allocation. This can also be called on a
  113. * buffer that already was freed.
  114. */
  115. void Curl_dyn_reset(struct dynbuf *s)
  116. {
  117. DEBUGASSERT(s);
  118. DEBUGASSERT(s->init == DYNINIT);
  119. DEBUGASSERT(!s->leng || s->bufr);
  120. if(s->leng)
  121. s->bufr[0] = 0;
  122. s->leng = 0;
  123. }
  124. /*
  125. * Specify the size of the tail to keep (number of bytes from the end of the
  126. * buffer). The rest will be dropped.
  127. */
  128. CURLcode Curl_dyn_tail(struct dynbuf *s, size_t trail)
  129. {
  130. DEBUGASSERT(s);
  131. DEBUGASSERT(s->init == DYNINIT);
  132. DEBUGASSERT(!s->leng || s->bufr);
  133. if(trail > s->leng)
  134. return CURLE_BAD_FUNCTION_ARGUMENT;
  135. else if(trail == s->leng)
  136. return CURLE_OK;
  137. else if(!trail) {
  138. Curl_dyn_reset(s);
  139. }
  140. else {
  141. memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail);
  142. s->leng = trail;
  143. s->bufr[s->leng] = 0;
  144. }
  145. return CURLE_OK;
  146. }
  147. /*
  148. * Appends a buffer with length.
  149. */
  150. CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len)
  151. {
  152. DEBUGASSERT(s);
  153. DEBUGASSERT(s->init == DYNINIT);
  154. DEBUGASSERT(!s->leng || s->bufr);
  155. return dyn_nappend(s, mem, len);
  156. }
  157. /*
  158. * Append a null-terminated string at the end.
  159. */
  160. CURLcode Curl_dyn_add(struct dynbuf *s, const char *str)
  161. {
  162. size_t n = strlen(str);
  163. DEBUGASSERT(s);
  164. DEBUGASSERT(s->init == DYNINIT);
  165. DEBUGASSERT(!s->leng || s->bufr);
  166. return dyn_nappend(s, (unsigned char *)str, n);
  167. }
  168. /*
  169. * Append a string vprintf()-style
  170. */
  171. CURLcode Curl_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap)
  172. {
  173. #ifdef BUILDING_LIBCURL
  174. int rc;
  175. DEBUGASSERT(s);
  176. DEBUGASSERT(s->init == DYNINIT);
  177. DEBUGASSERT(!s->leng || s->bufr);
  178. rc = Curl_dyn_vprintf(s, fmt, ap);
  179. if(!rc)
  180. return CURLE_OK;
  181. #else
  182. char *str;
  183. str = vaprintf(fmt, ap); /* this allocs a new string to append */
  184. if(str) {
  185. CURLcode result = dyn_nappend(s, (unsigned char *)str, strlen(str));
  186. free(str);
  187. return result;
  188. }
  189. /* If we failed, we cleanup the whole buffer and return error */
  190. Curl_dyn_free(s);
  191. #endif
  192. return CURLE_OUT_OF_MEMORY;
  193. }
  194. /*
  195. * Append a string printf()-style
  196. */
  197. CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...)
  198. {
  199. CURLcode result;
  200. va_list ap;
  201. DEBUGASSERT(s);
  202. DEBUGASSERT(s->init == DYNINIT);
  203. DEBUGASSERT(!s->leng || s->bufr);
  204. va_start(ap, fmt);
  205. result = Curl_dyn_vaddf(s, fmt, ap);
  206. va_end(ap);
  207. return result;
  208. }
  209. /*
  210. * Returns a pointer to the buffer.
  211. */
  212. char *Curl_dyn_ptr(const struct dynbuf *s)
  213. {
  214. DEBUGASSERT(s);
  215. DEBUGASSERT(s->init == DYNINIT);
  216. DEBUGASSERT(!s->leng || s->bufr);
  217. return s->bufr;
  218. }
  219. /*
  220. * Returns an unsigned pointer to the buffer.
  221. */
  222. unsigned char *Curl_dyn_uptr(const struct dynbuf *s)
  223. {
  224. DEBUGASSERT(s);
  225. DEBUGASSERT(s->init == DYNINIT);
  226. DEBUGASSERT(!s->leng || s->bufr);
  227. return (unsigned char *)s->bufr;
  228. }
  229. /*
  230. * Returns the length of the buffer.
  231. */
  232. size_t Curl_dyn_len(const struct dynbuf *s)
  233. {
  234. DEBUGASSERT(s);
  235. DEBUGASSERT(s->init == DYNINIT);
  236. DEBUGASSERT(!s->leng || s->bufr);
  237. return s->leng;
  238. }
  239. /*
  240. * Set a new (smaller) length.
  241. */
  242. CURLcode Curl_dyn_setlen(struct dynbuf *s, size_t set)
  243. {
  244. DEBUGASSERT(s);
  245. DEBUGASSERT(s->init == DYNINIT);
  246. DEBUGASSERT(!s->leng || s->bufr);
  247. if(set > s->leng)
  248. return CURLE_BAD_FUNCTION_ARGUMENT;
  249. s->leng = set;
  250. s->bufr[s->leng] = 0;
  251. return CURLE_OK;
  252. }