bufq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 "bufq.h"
  26. /* The last 2 #include files should be in this order */
  27. #include "curl_memory.h"
  28. #include "memdebug.h"
  29. static bool chunk_is_empty(const struct buf_chunk *chunk)
  30. {
  31. return chunk->r_offset >= chunk->w_offset;
  32. }
  33. static bool chunk_is_full(const struct buf_chunk *chunk)
  34. {
  35. return chunk->w_offset >= chunk->dlen;
  36. }
  37. static size_t chunk_len(const struct buf_chunk *chunk)
  38. {
  39. return chunk->w_offset - chunk->r_offset;
  40. }
  41. static void chunk_reset(struct buf_chunk *chunk)
  42. {
  43. chunk->next = NULL;
  44. chunk->r_offset = chunk->w_offset = 0;
  45. }
  46. static size_t chunk_append(struct buf_chunk *chunk,
  47. const unsigned char *buf, size_t len)
  48. {
  49. unsigned char *p = &chunk->x.data[chunk->w_offset];
  50. size_t n = chunk->dlen - chunk->w_offset;
  51. DEBUGASSERT(chunk->dlen >= chunk->w_offset);
  52. if(n) {
  53. n = CURLMIN(n, len);
  54. memcpy(p, buf, n);
  55. chunk->w_offset += n;
  56. }
  57. return n;
  58. }
  59. static size_t chunk_read(struct buf_chunk *chunk,
  60. unsigned char *buf, size_t len)
  61. {
  62. unsigned char *p = &chunk->x.data[chunk->r_offset];
  63. size_t n = chunk->w_offset - chunk->r_offset;
  64. DEBUGASSERT(chunk->w_offset >= chunk->r_offset);
  65. if(!n) {
  66. return 0;
  67. }
  68. else if(n <= len) {
  69. memcpy(buf, p, n);
  70. chunk->r_offset = chunk->w_offset = 0;
  71. return n;
  72. }
  73. else {
  74. memcpy(buf, p, len);
  75. chunk->r_offset += len;
  76. return len;
  77. }
  78. }
  79. static CURLcode chunk_slurpn(struct buf_chunk *chunk, size_t max_len,
  80. Curl_bufq_reader *reader,
  81. void *reader_ctx, size_t *pnread)
  82. {
  83. unsigned char *p = &chunk->x.data[chunk->w_offset];
  84. size_t n = chunk->dlen - chunk->w_offset; /* free amount */
  85. CURLcode result;
  86. *pnread = 0;
  87. DEBUGASSERT(chunk->dlen >= chunk->w_offset);
  88. if(!n)
  89. return CURLE_AGAIN;
  90. if(max_len && n > max_len)
  91. n = max_len;
  92. result = reader(reader_ctx, p, n, pnread);
  93. if(!result) {
  94. DEBUGASSERT(*pnread <= n);
  95. chunk->w_offset += *pnread;
  96. }
  97. return result;
  98. }
  99. static void chunk_peek(const struct buf_chunk *chunk,
  100. const unsigned char **pbuf, size_t *plen)
  101. {
  102. DEBUGASSERT(chunk->w_offset >= chunk->r_offset);
  103. *pbuf = &chunk->x.data[chunk->r_offset];
  104. *plen = chunk->w_offset - chunk->r_offset;
  105. }
  106. static void chunk_peek_at(const struct buf_chunk *chunk, size_t offset,
  107. const unsigned char **pbuf, size_t *plen)
  108. {
  109. offset += chunk->r_offset;
  110. DEBUGASSERT(chunk->w_offset >= offset);
  111. *pbuf = &chunk->x.data[offset];
  112. *plen = chunk->w_offset - offset;
  113. }
  114. static size_t chunk_skip(struct buf_chunk *chunk, size_t amount)
  115. {
  116. size_t n = chunk->w_offset - chunk->r_offset;
  117. DEBUGASSERT(chunk->w_offset >= chunk->r_offset);
  118. if(n) {
  119. n = CURLMIN(n, amount);
  120. chunk->r_offset += n;
  121. if(chunk->r_offset == chunk->w_offset)
  122. chunk->r_offset = chunk->w_offset = 0;
  123. }
  124. return n;
  125. }
  126. static void chunk_list_free(struct buf_chunk **anchor)
  127. {
  128. struct buf_chunk *chunk;
  129. while(*anchor) {
  130. chunk = *anchor;
  131. *anchor = chunk->next;
  132. free(chunk);
  133. }
  134. }
  135. void Curl_bufcp_init(struct bufc_pool *pool,
  136. size_t chunk_size, size_t spare_max)
  137. {
  138. DEBUGASSERT(chunk_size > 0);
  139. DEBUGASSERT(spare_max > 0);
  140. memset(pool, 0, sizeof(*pool));
  141. pool->chunk_size = chunk_size;
  142. pool->spare_max = spare_max;
  143. }
  144. static CURLcode bufcp_take(struct bufc_pool *pool,
  145. struct buf_chunk **pchunk)
  146. {
  147. struct buf_chunk *chunk = NULL;
  148. if(pool->spare) {
  149. chunk = pool->spare;
  150. pool->spare = chunk->next;
  151. --pool->spare_count;
  152. chunk_reset(chunk);
  153. *pchunk = chunk;
  154. return CURLE_OK;
  155. }
  156. /* Check for integer overflow before allocation */
  157. if(pool->chunk_size > SIZE_MAX - sizeof(*chunk)) {
  158. *pchunk = NULL;
  159. return CURLE_OUT_OF_MEMORY;
  160. }
  161. chunk = calloc(1, sizeof(*chunk) + pool->chunk_size);
  162. if(!chunk) {
  163. *pchunk = NULL;
  164. return CURLE_OUT_OF_MEMORY;
  165. }
  166. chunk->dlen = pool->chunk_size;
  167. *pchunk = chunk;
  168. return CURLE_OK;
  169. }
  170. static void bufcp_put(struct bufc_pool *pool,
  171. struct buf_chunk *chunk)
  172. {
  173. if(pool->spare_count >= pool->spare_max) {
  174. free(chunk);
  175. }
  176. else {
  177. chunk_reset(chunk);
  178. chunk->next = pool->spare;
  179. pool->spare = chunk;
  180. ++pool->spare_count;
  181. }
  182. }
  183. void Curl_bufcp_free(struct bufc_pool *pool)
  184. {
  185. chunk_list_free(&pool->spare);
  186. pool->spare_count = 0;
  187. }
  188. static void bufq_init(struct bufq *q, struct bufc_pool *pool,
  189. size_t chunk_size, size_t max_chunks, int opts)
  190. {
  191. DEBUGASSERT(chunk_size > 0);
  192. DEBUGASSERT(max_chunks > 0);
  193. memset(q, 0, sizeof(*q));
  194. q->chunk_size = chunk_size;
  195. q->max_chunks = max_chunks;
  196. q->pool = pool;
  197. q->opts = opts;
  198. }
  199. void Curl_bufq_init2(struct bufq *q, size_t chunk_size, size_t max_chunks,
  200. int opts)
  201. {
  202. bufq_init(q, NULL, chunk_size, max_chunks, opts);
  203. }
  204. void Curl_bufq_init(struct bufq *q, size_t chunk_size, size_t max_chunks)
  205. {
  206. bufq_init(q, NULL, chunk_size, max_chunks, BUFQ_OPT_NONE);
  207. }
  208. void Curl_bufq_initp(struct bufq *q, struct bufc_pool *pool,
  209. size_t max_chunks, int opts)
  210. {
  211. bufq_init(q, pool, pool->chunk_size, max_chunks, opts);
  212. }
  213. void Curl_bufq_free(struct bufq *q)
  214. {
  215. chunk_list_free(&q->head);
  216. chunk_list_free(&q->spare);
  217. q->tail = NULL;
  218. q->chunk_count = 0;
  219. }
  220. void Curl_bufq_reset(struct bufq *q)
  221. {
  222. struct buf_chunk *chunk;
  223. while(q->head) {
  224. chunk = q->head;
  225. q->head = chunk->next;
  226. chunk->next = q->spare;
  227. q->spare = chunk;
  228. }
  229. q->tail = NULL;
  230. }
  231. size_t Curl_bufq_len(const struct bufq *q)
  232. {
  233. const struct buf_chunk *chunk = q->head;
  234. size_t len = 0;
  235. while(chunk) {
  236. len += chunk_len(chunk);
  237. chunk = chunk->next;
  238. }
  239. return len;
  240. }
  241. bool Curl_bufq_is_empty(const struct bufq *q)
  242. {
  243. return !q->head || chunk_is_empty(q->head);
  244. }
  245. bool Curl_bufq_is_full(const struct bufq *q)
  246. {
  247. if(!q->tail || q->spare)
  248. return FALSE;
  249. if(q->chunk_count < q->max_chunks)
  250. return FALSE;
  251. if(q->chunk_count > q->max_chunks)
  252. return TRUE;
  253. /* we have no spares and cannot make more, is the tail full? */
  254. return chunk_is_full(q->tail);
  255. }
  256. static struct buf_chunk *get_spare(struct bufq *q)
  257. {
  258. struct buf_chunk *chunk = NULL;
  259. if(q->spare) {
  260. chunk = q->spare;
  261. q->spare = chunk->next;
  262. chunk_reset(chunk);
  263. return chunk;
  264. }
  265. if(q->chunk_count >= q->max_chunks && (!(q->opts & BUFQ_OPT_SOFT_LIMIT)))
  266. return NULL;
  267. if(q->pool) {
  268. if(bufcp_take(q->pool, &chunk))
  269. return NULL;
  270. ++q->chunk_count;
  271. return chunk;
  272. }
  273. else {
  274. /* Check for integer overflow before allocation */
  275. if(q->chunk_size > SIZE_MAX - sizeof(*chunk)) {
  276. return NULL;
  277. }
  278. chunk = calloc(1, sizeof(*chunk) + q->chunk_size);
  279. if(!chunk)
  280. return NULL;
  281. chunk->dlen = q->chunk_size;
  282. ++q->chunk_count;
  283. return chunk;
  284. }
  285. }
  286. static void prune_head(struct bufq *q)
  287. {
  288. struct buf_chunk *chunk;
  289. while(q->head && chunk_is_empty(q->head)) {
  290. chunk = q->head;
  291. q->head = chunk->next;
  292. if(q->tail == chunk)
  293. q->tail = q->head;
  294. if(q->pool) {
  295. bufcp_put(q->pool, chunk);
  296. --q->chunk_count;
  297. }
  298. else if((q->chunk_count > q->max_chunks) ||
  299. (q->opts & BUFQ_OPT_NO_SPARES)) {
  300. /* SOFT_LIMIT allowed us more than max. free spares until
  301. * we are at max again. Or free them if we are configured
  302. * to not use spares. */
  303. free(chunk);
  304. --q->chunk_count;
  305. }
  306. else {
  307. chunk->next = q->spare;
  308. q->spare = chunk;
  309. }
  310. }
  311. }
  312. static struct buf_chunk *get_non_full_tail(struct bufq *q)
  313. {
  314. struct buf_chunk *chunk;
  315. if(q->tail && !chunk_is_full(q->tail))
  316. return q->tail;
  317. chunk = get_spare(q);
  318. if(chunk) {
  319. /* new tail, and possibly new head */
  320. if(q->tail) {
  321. q->tail->next = chunk;
  322. q->tail = chunk;
  323. }
  324. else {
  325. DEBUGASSERT(!q->head);
  326. q->head = q->tail = chunk;
  327. }
  328. }
  329. return chunk;
  330. }
  331. CURLcode Curl_bufq_write(struct bufq *q,
  332. const unsigned char *buf, size_t len,
  333. size_t *pnwritten)
  334. {
  335. struct buf_chunk *tail;
  336. size_t n;
  337. DEBUGASSERT(q->max_chunks > 0);
  338. *pnwritten = 0;
  339. while(len) {
  340. tail = get_non_full_tail(q);
  341. if(!tail) {
  342. if((q->chunk_count < q->max_chunks) || (q->opts & BUFQ_OPT_SOFT_LIMIT))
  343. /* should have gotten a tail, but did not */
  344. return CURLE_OUT_OF_MEMORY;
  345. break;
  346. }
  347. n = chunk_append(tail, buf, len);
  348. if(!n)
  349. break;
  350. *pnwritten += n;
  351. buf += n;
  352. len -= n;
  353. }
  354. return (!*pnwritten && len) ? CURLE_AGAIN : CURLE_OK;
  355. }
  356. CURLcode Curl_bufq_cwrite(struct bufq *q,
  357. const char *buf, size_t len,
  358. size_t *pnwritten)
  359. {
  360. return Curl_bufq_write(q, (const unsigned char *)buf, len, pnwritten);
  361. }
  362. CURLcode Curl_bufq_read(struct bufq *q, unsigned char *buf, size_t len,
  363. size_t *pnread)
  364. {
  365. *pnread = 0;
  366. while(len && q->head) {
  367. size_t n = chunk_read(q->head, buf, len);
  368. if(n) {
  369. *pnread += n;
  370. buf += n;
  371. len -= n;
  372. }
  373. prune_head(q);
  374. }
  375. return (!*pnread) ? CURLE_AGAIN : CURLE_OK;
  376. }
  377. CURLcode Curl_bufq_cread(struct bufq *q, char *buf, size_t len,
  378. size_t *pnread)
  379. {
  380. return Curl_bufq_read(q, (unsigned char *)buf, len, pnread);
  381. }
  382. bool Curl_bufq_peek(struct bufq *q,
  383. const unsigned char **pbuf, size_t *plen)
  384. {
  385. if(q->head && chunk_is_empty(q->head)) {
  386. prune_head(q);
  387. }
  388. if(q->head && !chunk_is_empty(q->head)) {
  389. chunk_peek(q->head, pbuf, plen);
  390. return TRUE;
  391. }
  392. *pbuf = NULL;
  393. *plen = 0;
  394. return FALSE;
  395. }
  396. bool Curl_bufq_peek_at(struct bufq *q, size_t offset,
  397. const unsigned char **pbuf, size_t *plen)
  398. {
  399. struct buf_chunk *c = q->head;
  400. size_t clen;
  401. while(c) {
  402. clen = chunk_len(c);
  403. if(!clen)
  404. break;
  405. if(offset >= clen) {
  406. offset -= clen;
  407. c = c->next;
  408. continue;
  409. }
  410. chunk_peek_at(c, offset, pbuf, plen);
  411. return TRUE;
  412. }
  413. *pbuf = NULL;
  414. *plen = 0;
  415. return FALSE;
  416. }
  417. void Curl_bufq_skip(struct bufq *q, size_t amount)
  418. {
  419. size_t n;
  420. while(amount && q->head) {
  421. n = chunk_skip(q->head, amount);
  422. amount -= n;
  423. prune_head(q);
  424. }
  425. }
  426. CURLcode Curl_bufq_pass(struct bufq *q, Curl_bufq_writer *writer,
  427. void *writer_ctx, size_t *pwritten)
  428. {
  429. const unsigned char *buf;
  430. size_t blen;
  431. CURLcode result = CURLE_OK;
  432. *pwritten = 0;
  433. while(Curl_bufq_peek(q, &buf, &blen)) {
  434. size_t chunk_written;
  435. result = writer(writer_ctx, buf, blen, &chunk_written);
  436. if(result) {
  437. if((result == CURLE_AGAIN) && *pwritten) {
  438. /* blocked on subsequent write, report success */
  439. result = CURLE_OK;
  440. }
  441. break;
  442. }
  443. if(!chunk_written) {
  444. if(!*pwritten) {
  445. /* treat as blocked */
  446. result = CURLE_AGAIN;
  447. }
  448. break;
  449. }
  450. *pwritten += chunk_written;
  451. Curl_bufq_skip(q, chunk_written);
  452. }
  453. return result;
  454. }
  455. CURLcode Curl_bufq_write_pass(struct bufq *q,
  456. const unsigned char *buf, size_t len,
  457. Curl_bufq_writer *writer, void *writer_ctx,
  458. size_t *pwritten)
  459. {
  460. CURLcode result = CURLE_OK;
  461. size_t n;
  462. *pwritten = 0;
  463. while(len) {
  464. if(Curl_bufq_is_full(q)) {
  465. /* try to make room in case we are full */
  466. result = Curl_bufq_pass(q, writer, writer_ctx, &n);
  467. if(result) {
  468. if(result != CURLE_AGAIN) {
  469. /* real error, fail */
  470. return result;
  471. }
  472. /* would block, bufq is full, give up */
  473. break;
  474. }
  475. }
  476. /* Add to bufq as much as there is room for */
  477. result = Curl_bufq_write(q, buf, len, &n);
  478. if(result) {
  479. if(result != CURLE_AGAIN)
  480. /* real error, fail */
  481. return result;
  482. /* result == CURLE_AGAIN */
  483. if(*pwritten)
  484. /* we did write successfully before */
  485. result = CURLE_OK;
  486. return result;
  487. }
  488. else if(n == 0)
  489. /* edge case of writer returning 0 (and len is >0)
  490. * break or we might enter an infinite loop here */
  491. break;
  492. /* Track what we added to bufq */
  493. buf += n;
  494. len -= n;
  495. *pwritten += n;
  496. }
  497. return (!*pwritten && len) ? CURLE_AGAIN : CURLE_OK;
  498. }
  499. CURLcode Curl_bufq_sipn(struct bufq *q, size_t max_len,
  500. Curl_bufq_reader *reader, void *reader_ctx,
  501. size_t *pnread)
  502. {
  503. struct buf_chunk *tail = NULL;
  504. *pnread = 0;
  505. tail = get_non_full_tail(q);
  506. if(!tail) {
  507. if(q->chunk_count < q->max_chunks)
  508. return CURLE_OUT_OF_MEMORY;
  509. /* full, blocked */
  510. return CURLE_AGAIN;
  511. }
  512. return chunk_slurpn(tail, max_len, reader, reader_ctx, pnread);
  513. }
  514. /**
  515. * Read up to `max_len` bytes and append it to the end of the buffer queue.
  516. * if `max_len` is 0, no limit is imposed and the call behaves exactly
  517. * the same as `Curl_bufq_slurp()`.
  518. * Returns the total amount of buf read (may be 0) in `pnread` or error
  519. * Note that even in case of an error chunks may have been read and
  520. * the buffer queue will have different length than before.
  521. */
  522. static CURLcode bufq_slurpn(struct bufq *q, size_t max_len,
  523. Curl_bufq_reader *reader, void *reader_ctx,
  524. size_t *pnread)
  525. {
  526. CURLcode result;
  527. *pnread = 0;
  528. while(1) {
  529. size_t n;
  530. result = Curl_bufq_sipn(q, max_len, reader, reader_ctx, &n);
  531. if(result) {
  532. if(!*pnread || result != CURLE_AGAIN) {
  533. /* blocked on first read or real error, fail */
  534. return result;
  535. }
  536. result = CURLE_OK;
  537. break;
  538. }
  539. else if(n == 0) {
  540. /* eof, result remains CURLE_OK */
  541. break;
  542. }
  543. *pnread += n;
  544. if(max_len) {
  545. DEBUGASSERT(n <= max_len);
  546. max_len -= n;
  547. if(!max_len)
  548. break;
  549. }
  550. /* give up slurping when we get less bytes than we asked for */
  551. if(q->tail && !chunk_is_full(q->tail))
  552. break;
  553. }
  554. return result;
  555. }
  556. CURLcode Curl_bufq_slurp(struct bufq *q, Curl_bufq_reader *reader,
  557. void *reader_ctx, size_t *pnread)
  558. {
  559. return bufq_slurpn(q, 0, reader, reader_ctx, pnread);
  560. }