#ifndef HEADER_CURL_BUFQ_H1#define HEADER_CURL_BUFQ_H2/***************************************************************************3* _ _ ____ _4* Project ___| | | | _ \| |5* / __| | | | |_) | |6* | (__| |_| | _ <| |___7* \___|\___/|_| \_\_____|8*9* Copyright (C) Daniel Stenberg, <[email protected]>, et al.10*11* This software is licensed as described in the file COPYING, which12* you should have received as part of this distribution. The terms13* are also available at https://curl.se/docs/copyright.html.14*15* You may opt to use, copy, modify, merge, publish, distribute and/or sell16* copies of the Software, and permit persons to whom the Software is17* furnished to do so, under the terms of the COPYING file.18*19* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY20* KIND, either express or implied.21*22* SPDX-License-Identifier: curl23*24***************************************************************************/25#include "curl_setup.h"2627/**28* A chunk of bytes for reading and writing.29* The size is fixed a creation with read and write offset30* for where unread content is.31*/32struct buf_chunk {33struct buf_chunk *next; /* to keep it in a list */34size_t dlen; /* the amount of allocated x.data[] */35size_t r_offset; /* first unread bytes */36size_t w_offset; /* one after last written byte */37union {38uint8_t data[1]; /* the buffer for `dlen` bytes */39void *dummy; /* alignment */40} x;41};4243/**44* A pool for providing/keeping a number of chunks of the same size45*46* The same pool can be shared by many `bufq` instances. However, a pool47* is not thread safe. All bufqs using it are supposed to operate in the48* same thread.49*/50struct bufc_pool {51struct buf_chunk *spare; /* list of available spare chunks */52size_t chunk_size; /* the size of chunks in this pool */53size_t spare_count; /* current number of spare chunks in list */54size_t spare_max; /* max number of spares to keep */55};5657void Curl_bufcp_init(struct bufc_pool *pool,58size_t chunk_size, size_t spare_max);5960void Curl_bufcp_free(struct bufc_pool *pool);6162/**63* A queue of byte chunks for reading and writing.64* Reading is done from `head`, writing is done to `tail`.65*66* `bufq`s can be empty or full or neither. Its `len` is the number67* of bytes that can be read. For an empty bufq, `len` will be 0.68*69* By default, a bufq can hold up to `max_chunks * chunk_size` number70* of bytes. When `max_chunks` are used (in the `head` list) and the71* `tail` chunk is full, the bufq will report that it is full.72*73* On a full bufq, `len` may be less than the maximum number of bytes,74* e.g. when the head chunk is partially read. `len` may also become75* larger than the max when option `BUFQ_OPT_SOFT_LIMIT` is used.76*77* By default, writing to a full bufq will return (-1, CURLE_AGAIN). Same78* as reading from an empty bufq.79* With `BUFQ_OPT_SOFT_LIMIT` set, a bufq will allow writing becond this80* limit and use more than `max_chunks`. However it will report that it81* is full nevertheless. This is provided for situation where writes82* preferably never fail (except for memory exhaustion).83*84* By default and without a pool, a bufq will keep chunks that read85* empty in its `spare` list. Option `BUFQ_OPT_NO_SPARES` will86* disable that and free chunks once they become empty.87*88* When providing a pool to a bufq, all chunk creation and spare handling89* will be delegated to that pool.90*/91struct bufq {92struct buf_chunk *head; /* chunk with bytes to read from */93struct buf_chunk *tail; /* chunk to write to */94struct buf_chunk *spare; /* list of free chunks, unless `pool` */95struct bufc_pool *pool; /* optional pool for free chunks */96size_t chunk_count; /* current number of chunks in `head+spare` */97size_t max_chunks; /* max `head` chunks to use */98size_t chunk_size; /* size of chunks to manage */99int opts; /* options for handling queue, see below */100};101102/**103* Default behaviour: chunk limit is "hard", meaning attempts to write104* more bytes than can be hold in `max_chunks` is refused and will return105* -1, CURLE_AGAIN. */106#define BUFQ_OPT_NONE (0)107/**108* Make `max_chunks` a "soft" limit. A bufq will report that it is "full"109* when `max_chunks` are used, but allows writing beyond this limit.110*/111#define BUFQ_OPT_SOFT_LIMIT (1 << 0)112/**113* Do not keep spare chunks.114*/115#define BUFQ_OPT_NO_SPARES (1 << 1)116117/**118* Initialize a buffer queue that can hold up to `max_chunks` buffers119* each of size `chunk_size`. The bufq will not allow writing of120* more bytes than can be held in `max_chunks`.121*/122void Curl_bufq_init(struct bufq *q, size_t chunk_size, size_t max_chunks);123124/**125* Initialize a buffer queue that can hold up to `max_chunks` buffers126* each of size `chunk_size` with the given options. See `BUFQ_OPT_*`.127*/128void Curl_bufq_init2(struct bufq *q, size_t chunk_size,129size_t max_chunks, int opts);130131void Curl_bufq_initp(struct bufq *q, struct bufc_pool *pool,132size_t max_chunks, int opts);133134/**135* Reset the buffer queue to be empty. Will keep any allocated buffer136* chunks around.137*/138void Curl_bufq_reset(struct bufq *q);139140/**141* Free all resources held by the buffer queue.142*/143void Curl_bufq_free(struct bufq *q);144145/**146* Return the total amount of data in the queue.147*/148size_t Curl_bufq_len(const struct bufq *q);149150/**151* Returns TRUE iff there is no data in the buffer queue.152*/153bool Curl_bufq_is_empty(const struct bufq *q);154155/**156* Returns TRUE iff there is no space left in the buffer queue.157*/158bool Curl_bufq_is_full(const struct bufq *q);159160/**161* Write buf to the end of the buffer queue. The buf is copied162* and the amount of copied bytes is returned.163* CURLE_AGAIN is returned if the buffer queue is full.164*/165CURLcode Curl_bufq_write(struct bufq *q,166const uint8_t *buf, size_t len,167size_t *pnwritten);168169CURLcode Curl_bufq_cwrite(struct bufq *q,170const char *buf, size_t len,171size_t *pnwritten);172173/**174* Read buf from the start of the buffer queue. The buf is copied175* and the amount of copied bytes is returned.176*/177CURLcode Curl_bufq_read(struct bufq *q, uint8_t *buf, size_t len,178size_t *pnread);179180CURLcode Curl_bufq_cread(struct bufq *q, char *buf, size_t len,181size_t *pnread);182183/**184* Peek at the head chunk in the buffer queue. Returns a pointer to185* the chunk buf (at the current offset) and its length. Does not186* modify the buffer queue.187* Returns TRUE iff bytes are available. Sets `pbuf` to NULL and `plen`188* to 0 when no bytes are available.189* Repeated calls return the same information until the buffer queue190* is modified, see `Curl_bufq_skip()``191*/192bool Curl_bufq_peek(struct bufq *q,193const uint8_t **pbuf, size_t *plen);194195bool Curl_bufq_peek_at(struct bufq *q, size_t offset,196const uint8_t **pbuf, size_t *plen);197198/**199* Tell the buffer queue to discard `amount` buf bytes at the head200* of the queue. Skipping more buf than is currently buffered will201* just empty the queue.202*/203void Curl_bufq_skip(struct bufq *q, size_t amount);204205typedef CURLcode Curl_bufq_writer(void *writer_ctx,206const uint8_t *buf, size_t len,207size_t *pwritten);208/**209* Passes the chunks in the buffer queue to the writer and returns210* the amount of buf written. A writer may return -1 and CURLE_AGAIN211* to indicate blocking at which point the queue will stop and return212* the amount of buf passed so far.213* -1 is returned on any other errors reported by the writer.214* Note that in case of a -1 chunks may have been written and215* the buffer queue will have different length than before.216*/217CURLcode Curl_bufq_pass(struct bufq *q, Curl_bufq_writer *writer,218void *writer_ctx, size_t *pwritten);219220typedef CURLcode Curl_bufq_reader(void *reader_ctx,221uint8_t *buf, size_t len,222size_t *pnread);223224/**225* Read bytes and append them to the end of the buffer queue until the226* reader returns blocking or the queue is full. A reader returns227* CURLE_AGAIN to indicate blocking.228* Returns the total amount of buf read (may be 0) in `pnread` on success.229* Note that in case of an error chunks may have been read and230* the buffer queue will have different length than before.231*/232CURLcode Curl_bufq_slurp(struct bufq *q, Curl_bufq_reader *reader,233void *reader_ctx, size_t *pnread);234235/**236* Read *once* up to `max_len` bytes and append it to the buffer.237* if `max_len` is 0, no limit is imposed besides the chunk space.238* Returns the total amount of buf read (may be 0) or -1 on other239* reader errors.240*/241CURLcode Curl_bufq_sipn(struct bufq *q, size_t max_len,242Curl_bufq_reader *reader, void *reader_ctx,243size_t *pnread);244245/**246* Write buf to the end of the buffer queue.247* Will write bufq content or passed `buf` directly using the `writer`248* callback when it sees fit. 'buf' might get passed directly249* on or is placed into the buffer, depending on `len` and current250* amount buffered, chunk size, etc.251*/252CURLcode Curl_bufq_write_pass(struct bufq *q,253const uint8_t *buf, size_t len,254Curl_bufq_writer *writer, void *writer_ctx,255size_t *pwritten);256257#endif /* HEADER_CURL_BUFQ_H */258259260