dynbuf.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2020, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "dynbuf.h"
  24. #include "curl_printf.h"
  25. #ifdef BUILDING_LIBCURL
  26. #include "curl_memory.h"
  27. #endif
  28. #include "memdebug.h"
  29. #define MIN_FIRST_ALLOC 32
  30. #define DYNINIT 0xbee51da /* random pattern */
  31. /*
  32. * Init a dynbuf struct.
  33. */
  34. void Curl_dyn_init(struct dynbuf *s, size_t toobig)
  35. {
  36. DEBUGASSERT(s);
  37. DEBUGASSERT(toobig);
  38. s->bufr = NULL;
  39. s->leng = 0;
  40. s->allc = 0;
  41. s->toobig = toobig;
  42. #ifdef DEBUGBUILD
  43. s->init = DYNINIT;
  44. #endif
  45. }
  46. /*
  47. * free the buffer and re-init the necessary fields. It doesn't touch the
  48. * 'init' field and thus this buffer can be reused to add data to again.
  49. */
  50. void Curl_dyn_free(struct dynbuf *s)
  51. {
  52. DEBUGASSERT(s);
  53. Curl_safefree(s->bufr);
  54. s->leng = s->allc = 0;
  55. }
  56. /*
  57. * Store/append an chunk of memory to the dynbuf.
  58. */
  59. static CURLcode dyn_nappend(struct dynbuf *s,
  60. const unsigned char *mem, size_t len)
  61. {
  62. size_t indx = s->leng;
  63. size_t a = s->allc;
  64. size_t fit = len + indx + 1; /* new string + old string + zero byte */
  65. /* try to detect if there's rubbish in the struct */
  66. DEBUGASSERT(s->init == DYNINIT);
  67. DEBUGASSERT(s->toobig);
  68. DEBUGASSERT(indx < s->toobig);
  69. DEBUGASSERT(!s->leng || s->bufr);
  70. if(fit > s->toobig) {
  71. Curl_dyn_free(s);
  72. return CURLE_OUT_OF_MEMORY;
  73. }
  74. else if(!a) {
  75. DEBUGASSERT(!indx);
  76. /* first invoke */
  77. if(fit < MIN_FIRST_ALLOC)
  78. a = MIN_FIRST_ALLOC;
  79. else
  80. a = fit;
  81. }
  82. else {
  83. while(a < fit)
  84. a *= 2;
  85. }
  86. if(a != s->allc) {
  87. /* this logic is not using Curl_saferealloc() to make the tool not have to
  88. include that as well when it uses this code */
  89. void *p = realloc(s->bufr, a);
  90. if(!p) {
  91. Curl_safefree(s->bufr);
  92. s->leng = s->allc = 0;
  93. return CURLE_OUT_OF_MEMORY;
  94. }
  95. s->bufr = p;
  96. s->allc = a;
  97. }
  98. if(len)
  99. memcpy(&s->bufr[indx], mem, len);
  100. s->leng = indx + len;
  101. s->bufr[s->leng] = 0;
  102. return CURLE_OK;
  103. }
  104. /*
  105. * Clears the string, keeps the allocation. This can also be called on a
  106. * buffer that already was freed.
  107. */
  108. void Curl_dyn_reset(struct dynbuf *s)
  109. {
  110. DEBUGASSERT(s);
  111. DEBUGASSERT(s->init == DYNINIT);
  112. DEBUGASSERT(!s->leng || s->bufr);
  113. if(s->leng)
  114. s->bufr[0] = 0;
  115. s->leng = 0;
  116. }
  117. #ifdef USE_NGTCP2
  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. #endif
  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. }