2
0

dynbuf.c 6.5 KB

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