dynbuf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2020 - 2022, 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. if(fit > s->toobig) {
  73. Curl_dyn_free(s);
  74. return CURLE_OUT_OF_MEMORY;
  75. }
  76. else if(!a) {
  77. DEBUGASSERT(!indx);
  78. /* first invoke */
  79. if(fit < MIN_FIRST_ALLOC)
  80. a = MIN_FIRST_ALLOC;
  81. else
  82. a = fit;
  83. }
  84. else {
  85. while(a < fit)
  86. a *= 2;
  87. }
  88. if(a != s->allc) {
  89. /* this logic is not using Curl_saferealloc() to make the tool not have to
  90. include that as well when it uses this code */
  91. void *p = realloc(s->bufr, a);
  92. if(!p) {
  93. Curl_safefree(s->bufr);
  94. s->leng = s->allc = 0;
  95. return CURLE_OUT_OF_MEMORY;
  96. }
  97. s->bufr = p;
  98. s->allc = a;
  99. }
  100. if(len)
  101. memcpy(&s->bufr[indx], mem, len);
  102. s->leng = indx + len;
  103. s->bufr[s->leng] = 0;
  104. return CURLE_OK;
  105. }
  106. /*
  107. * Clears the string, keeps the allocation. This can also be called on a
  108. * buffer that already was freed.
  109. */
  110. void Curl_dyn_reset(struct dynbuf *s)
  111. {
  112. DEBUGASSERT(s);
  113. DEBUGASSERT(s->init == DYNINIT);
  114. DEBUGASSERT(!s->leng || s->bufr);
  115. if(s->leng)
  116. s->bufr[0] = 0;
  117. s->leng = 0;
  118. }
  119. /*
  120. * Specify the size of the tail to keep (number of bytes from the end of the
  121. * buffer). The rest will be dropped.
  122. */
  123. CURLcode Curl_dyn_tail(struct dynbuf *s, size_t trail)
  124. {
  125. DEBUGASSERT(s);
  126. DEBUGASSERT(s->init == DYNINIT);
  127. DEBUGASSERT(!s->leng || s->bufr);
  128. if(trail > s->leng)
  129. return CURLE_BAD_FUNCTION_ARGUMENT;
  130. else if(trail == s->leng)
  131. return CURLE_OK;
  132. else if(!trail) {
  133. Curl_dyn_reset(s);
  134. }
  135. else {
  136. memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail);
  137. s->leng = trail;
  138. s->bufr[s->leng] = 0;
  139. }
  140. return CURLE_OK;
  141. }
  142. /*
  143. * Appends a buffer with length.
  144. */
  145. CURLcode Curl_dyn_addn(struct dynbuf *s, const void *mem, size_t len)
  146. {
  147. DEBUGASSERT(s);
  148. DEBUGASSERT(s->init == DYNINIT);
  149. DEBUGASSERT(!s->leng || s->bufr);
  150. return dyn_nappend(s, mem, len);
  151. }
  152. /*
  153. * Append a null-terminated string at the end.
  154. */
  155. CURLcode Curl_dyn_add(struct dynbuf *s, const char *str)
  156. {
  157. size_t n = strlen(str);
  158. DEBUGASSERT(s);
  159. DEBUGASSERT(s->init == DYNINIT);
  160. DEBUGASSERT(!s->leng || s->bufr);
  161. return dyn_nappend(s, (unsigned char *)str, n);
  162. }
  163. /*
  164. * Append a string vprintf()-style
  165. */
  166. CURLcode Curl_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap)
  167. {
  168. #ifdef BUILDING_LIBCURL
  169. int rc;
  170. DEBUGASSERT(s);
  171. DEBUGASSERT(s->init == DYNINIT);
  172. DEBUGASSERT(!s->leng || s->bufr);
  173. rc = Curl_dyn_vprintf(s, fmt, ap);
  174. if(!rc)
  175. return CURLE_OK;
  176. #else
  177. char *str;
  178. str = vaprintf(fmt, ap); /* this allocs a new string to append */
  179. if(str) {
  180. CURLcode result = dyn_nappend(s, (unsigned char *)str, strlen(str));
  181. free(str);
  182. return result;
  183. }
  184. /* If we failed, we cleanup the whole buffer and return error */
  185. Curl_dyn_free(s);
  186. #endif
  187. return CURLE_OUT_OF_MEMORY;
  188. }
  189. /*
  190. * Append a string printf()-style
  191. */
  192. CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...)
  193. {
  194. CURLcode result;
  195. va_list ap;
  196. DEBUGASSERT(s);
  197. DEBUGASSERT(s->init == DYNINIT);
  198. DEBUGASSERT(!s->leng || s->bufr);
  199. va_start(ap, fmt);
  200. result = Curl_dyn_vaddf(s, fmt, ap);
  201. va_end(ap);
  202. return result;
  203. }
  204. /*
  205. * Returns a pointer to the buffer.
  206. */
  207. char *Curl_dyn_ptr(const struct dynbuf *s)
  208. {
  209. DEBUGASSERT(s);
  210. DEBUGASSERT(s->init == DYNINIT);
  211. DEBUGASSERT(!s->leng || s->bufr);
  212. return s->bufr;
  213. }
  214. /*
  215. * Returns an unsigned pointer to the buffer.
  216. */
  217. unsigned char *Curl_dyn_uptr(const struct dynbuf *s)
  218. {
  219. DEBUGASSERT(s);
  220. DEBUGASSERT(s->init == DYNINIT);
  221. DEBUGASSERT(!s->leng || s->bufr);
  222. return (unsigned char *)s->bufr;
  223. }
  224. /*
  225. * Returns the length of the buffer.
  226. */
  227. size_t Curl_dyn_len(const struct dynbuf *s)
  228. {
  229. DEBUGASSERT(s);
  230. DEBUGASSERT(s->init == DYNINIT);
  231. DEBUGASSERT(!s->leng || s->bufr);
  232. return s->leng;
  233. }
  234. /*
  235. * Set a new (smaller) length.
  236. */
  237. CURLcode Curl_dyn_setlen(struct dynbuf *s, size_t set)
  238. {
  239. DEBUGASSERT(s);
  240. DEBUGASSERT(s->init == DYNINIT);
  241. DEBUGASSERT(!s->leng || s->bufr);
  242. if(set > s->leng)
  243. return CURLE_BAD_FUNCTION_ARGUMENT;
  244. s->leng = set;
  245. s->bufr[s->leng] = 0;
  246. return CURLE_OK;
  247. }