dynbuf.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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. #ifdef DEBUGBUILD
  33. #define DYNINIT 0xbee51da /* random pattern */
  34. #endif
  35. /*
  36. * Init a dynbuf struct.
  37. */
  38. void curlx_dyn_init(struct dynbuf *s, size_t toobig)
  39. {
  40. DEBUGASSERT(s);
  41. DEBUGASSERT(toobig);
  42. DEBUGASSERT(toobig <= MAX_DYNBUF_SIZE); /* catch crazy mistakes */
  43. s->bufr = NULL;
  44. s->leng = 0;
  45. s->allc = 0;
  46. s->toobig = toobig;
  47. #ifdef DEBUGBUILD
  48. s->init = DYNINIT;
  49. #endif
  50. }
  51. /*
  52. * free the buffer and re-init the necessary fields. It does not touch the
  53. * 'init' field and thus this buffer can be reused to add data to again.
  54. */
  55. void curlx_dyn_free(struct dynbuf *s)
  56. {
  57. DEBUGASSERT(s);
  58. DEBUGASSERT(s->init == DYNINIT);
  59. Curl_safefree(s->bufr);
  60. s->leng = s->allc = 0;
  61. }
  62. /*
  63. * Store/append an chunk of memory to the dynbuf.
  64. */
  65. static CURLcode dyn_nappend(struct dynbuf *s,
  66. const unsigned char *mem, size_t len)
  67. {
  68. size_t idx = s->leng;
  69. size_t a = s->allc;
  70. size_t fit = len + idx + 1; /* new string + old string + zero byte */
  71. /* try to detect if there is rubbish in the struct */
  72. DEBUGASSERT(s->init == DYNINIT);
  73. DEBUGASSERT(s->toobig);
  74. DEBUGASSERT(idx < s->toobig);
  75. DEBUGASSERT(!s->leng || s->bufr);
  76. DEBUGASSERT(a <= s->toobig);
  77. DEBUGASSERT(!len || mem);
  78. if(fit > s->toobig) {
  79. curlx_dyn_free(s);
  80. return CURLE_TOO_LARGE;
  81. }
  82. else if(!a) {
  83. DEBUGASSERT(!idx);
  84. /* first invoke */
  85. if(MIN_FIRST_ALLOC > s->toobig)
  86. a = s->toobig;
  87. else if(fit < MIN_FIRST_ALLOC)
  88. a = MIN_FIRST_ALLOC;
  89. else
  90. a = fit;
  91. }
  92. else {
  93. while(a < fit)
  94. a *= 2;
  95. if(a > s->toobig)
  96. /* no point in allocating a larger buffer than this is allowed to use */
  97. a = s->toobig;
  98. }
  99. if(a != s->allc) {
  100. /* this logic is not using Curl_saferealloc() to make the tool not have to
  101. include that as well when it uses this code */
  102. void *p = realloc(s->bufr, a);
  103. if(!p) {
  104. curlx_dyn_free(s);
  105. return CURLE_OUT_OF_MEMORY;
  106. }
  107. s->bufr = p;
  108. s->allc = a;
  109. }
  110. if(len)
  111. memcpy(&s->bufr[idx], mem, len);
  112. s->leng = idx + len;
  113. s->bufr[s->leng] = 0;
  114. return CURLE_OK;
  115. }
  116. /*
  117. * Clears the string, keeps the allocation. This can also be called on a
  118. * buffer that already was freed.
  119. */
  120. void curlx_dyn_reset(struct dynbuf *s)
  121. {
  122. DEBUGASSERT(s);
  123. DEBUGASSERT(s->init == DYNINIT);
  124. DEBUGASSERT(!s->leng || s->bufr);
  125. if(s->leng)
  126. s->bufr[0] = 0;
  127. s->leng = 0;
  128. }
  129. /*
  130. * Specify the size of the tail to keep (number of bytes from the end of the
  131. * buffer). The rest will be dropped.
  132. */
  133. CURLcode curlx_dyn_tail(struct dynbuf *s, size_t trail)
  134. {
  135. DEBUGASSERT(s);
  136. DEBUGASSERT(s->init == DYNINIT);
  137. DEBUGASSERT(!s->leng || s->bufr);
  138. if(trail > s->leng)
  139. return CURLE_BAD_FUNCTION_ARGUMENT;
  140. else if(trail == s->leng)
  141. return CURLE_OK;
  142. else if(!trail) {
  143. curlx_dyn_reset(s);
  144. }
  145. else {
  146. memmove(&s->bufr[0], &s->bufr[s->leng - trail], trail);
  147. s->leng = trail;
  148. s->bufr[s->leng] = 0;
  149. }
  150. return CURLE_OK;
  151. }
  152. /*
  153. * Appends a buffer with length.
  154. */
  155. CURLcode curlx_dyn_addn(struct dynbuf *s, const void *mem, size_t len)
  156. {
  157. DEBUGASSERT(s);
  158. DEBUGASSERT(s->init == DYNINIT);
  159. DEBUGASSERT(!s->leng || s->bufr);
  160. return dyn_nappend(s, mem, len);
  161. }
  162. /*
  163. * Append a null-terminated string at the end.
  164. */
  165. CURLcode curlx_dyn_add(struct dynbuf *s, const char *str)
  166. {
  167. size_t n;
  168. DEBUGASSERT(str);
  169. DEBUGASSERT(s);
  170. DEBUGASSERT(s->init == DYNINIT);
  171. DEBUGASSERT(!s->leng || s->bufr);
  172. n = strlen(str);
  173. return dyn_nappend(s, (const unsigned char *)str, n);
  174. }
  175. /*
  176. * Append a string vprintf()-style
  177. */
  178. CURLcode curlx_dyn_vaddf(struct dynbuf *s, const char *fmt, va_list ap)
  179. {
  180. #ifdef BUILDING_LIBCURL
  181. int rc;
  182. DEBUGASSERT(s);
  183. DEBUGASSERT(s->init == DYNINIT);
  184. DEBUGASSERT(!s->leng || s->bufr);
  185. DEBUGASSERT(fmt);
  186. rc = curlx_dyn_vprintf(s, fmt, ap);
  187. if(!rc)
  188. return CURLE_OK;
  189. else if(rc == MERR_TOO_LARGE)
  190. return CURLE_TOO_LARGE;
  191. return CURLE_OUT_OF_MEMORY;
  192. #else
  193. char *str;
  194. str = curl_mvaprintf(fmt, ap); /* this allocs a new string to append */
  195. if(str) {
  196. CURLcode result = dyn_nappend(s, (const unsigned char *)str, strlen(str));
  197. free(str);
  198. return result;
  199. }
  200. /* If we failed, we cleanup the whole buffer and return error */
  201. curlx_dyn_free(s);
  202. return CURLE_OUT_OF_MEMORY;
  203. #endif
  204. }
  205. /*
  206. * Append a string printf()-style
  207. */
  208. CURLcode curlx_dyn_addf(struct dynbuf *s, const char *fmt, ...)
  209. {
  210. CURLcode result;
  211. va_list ap;
  212. DEBUGASSERT(s);
  213. DEBUGASSERT(s->init == DYNINIT);
  214. DEBUGASSERT(!s->leng || s->bufr);
  215. DEBUGASSERT(strcmp(fmt, "%s")); /* use curlx_dyn_add instead */
  216. va_start(ap, fmt);
  217. result = curlx_dyn_vaddf(s, fmt, ap);
  218. va_end(ap);
  219. return result;
  220. }
  221. /*
  222. * Returns a pointer to the buffer.
  223. */
  224. char *curlx_dyn_ptr(const struct dynbuf *s)
  225. {
  226. DEBUGASSERT(s);
  227. DEBUGASSERT(s->init == DYNINIT);
  228. DEBUGASSERT(!s->leng || s->bufr);
  229. return s->bufr;
  230. }
  231. char *curlx_dyn_take(struct dynbuf *s, size_t *plen)
  232. {
  233. char *ptr = s->bufr;
  234. DEBUGASSERT(s);
  235. DEBUGASSERT(s->init == DYNINIT);
  236. *plen = s->leng;
  237. s->bufr = NULL;
  238. s->leng = 0;
  239. s->allc = 0;
  240. return ptr;
  241. }
  242. /*
  243. * Returns an unsigned pointer to the buffer.
  244. */
  245. unsigned char *curlx_dyn_uptr(const struct dynbuf *s)
  246. {
  247. DEBUGASSERT(s);
  248. DEBUGASSERT(s->init == DYNINIT);
  249. DEBUGASSERT(!s->leng || s->bufr);
  250. return (unsigned char *)s->bufr;
  251. }
  252. /*
  253. * Returns the length of the buffer.
  254. */
  255. size_t curlx_dyn_len(const struct dynbuf *s)
  256. {
  257. DEBUGASSERT(s);
  258. DEBUGASSERT(s->init == DYNINIT);
  259. DEBUGASSERT(!s->leng || s->bufr);
  260. return s->leng;
  261. }
  262. /*
  263. * Set a new (smaller) length.
  264. */
  265. CURLcode curlx_dyn_setlen(struct dynbuf *s, size_t set)
  266. {
  267. DEBUGASSERT(s);
  268. DEBUGASSERT(s->init == DYNINIT);
  269. DEBUGASSERT(!s->leng || s->bufr);
  270. if(set > s->leng)
  271. return CURLE_BAD_FUNCTION_ARGUMENT;
  272. s->leng = set;
  273. s->bufr[s->leng] = 0;
  274. return CURLE_OK;
  275. }