dynbuf.c 6.1 KB

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