#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#include <curl/curl.h>2829/**30* A chunk of bytes for reading and writing.31* The size is fixed a creation with read and write offset32* for where unread content is.33*/34struct buf_chunk {35struct buf_chunk *next; /* to keep it in a list */36size_t dlen; /* the amount of allocated x.data[] */37size_t r_offset; /* first unread bytes */38size_t w_offset; /* one after last written byte */39union {40unsigned char data[1]; /* the buffer for `dlen` bytes */41void *dummy; /* alignment */42} x;43};4445/**46* A pool for providing/keeping a number of chunks of the same size47*48* The same pool can be shared by many `bufq` instances. However, a pool49* is not thread safe. All bufqs using it are supposed to operate in the50* same thread.51*/52struct bufc_pool {53struct buf_chunk *spare; /* list of available spare chunks */54size_t chunk_size; /* the size of chunks in this pool */55size_t spare_count; /* current number of spare chunks in list */56size_t spare_max; /* max number of spares to keep */57};5859void Curl_bufcp_init(struct bufc_pool *pool,60size_t chunk_size, size_t spare_max);6162void Curl_bufcp_free(struct bufc_pool *pool);6364/**65* A queue of byte chunks for reading and writing.66* Reading is done from `head`, writing is done to `tail`.67*68* `bufq`s can be empty or full or neither. Its `len` is the number69* of bytes that can be read. For an empty bufq, `len` will be 0.70*71* By default, a bufq can hold up to `max_chunks * chunk_size` number72* of bytes. When `max_chunks` are used (in the `head` list) and the73* `tail` chunk is full, the bufq will report that it is full.74*75* On a full bufq, `len` may be less than the maximum number of bytes,76* e.g. when the head chunk is partially read. `len` may also become77* larger than the max when option `BUFQ_OPT_SOFT_LIMIT` is used.78*79* By default, writing to a full bufq will return (-1, CURLE_AGAIN). Same80* as reading from an empty bufq.81* With `BUFQ_OPT_SOFT_LIMIT` set, a bufq will allow writing becond this82* limit and use more than `max_chunks`. However it will report that it83* is full nevertheless. This is provided for situation where writes84* preferably never fail (except for memory exhaustion).85*86* By default and without a pool, a bufq will keep chunks that read87* empty in its `spare` list. Option `BUFQ_OPT_NO_SPARES` will88* disable that and free chunks once they become empty.89*90* When providing a pool to a bufq, all chunk creation and spare handling91* will be delegated to that pool.92*/93struct bufq {94struct buf_chunk *head; /* chunk with bytes to read from */95struct buf_chunk *tail; /* chunk to write to */96struct buf_chunk *spare; /* list of free chunks, unless `pool` */97struct bufc_pool *pool; /* optional pool for free chunks */98size_t chunk_count; /* current number of chunks in `head+spare` */99size_t max_chunks; /* max `head` chunks to use */100size_t chunk_size; /* size of chunks to manage */101int opts; /* options for handling queue, see below */102};103104/**105* Default behaviour: chunk limit is "hard", meaning attempts to write106* more bytes than can be hold in `max_chunks` is refused and will return107* -1, CURLE_AGAIN. */108#define BUFQ_OPT_NONE (0)109/**110* Make `max_chunks` a "soft" limit. A bufq will report that it is "full"111* when `max_chunks` are used, but allows writing beyond this limit.112*/113#define BUFQ_OPT_SOFT_LIMIT (1 << 0)114/**115* Do not keep spare chunks.116*/117#define BUFQ_OPT_NO_SPARES (1 << 1)118119/**120* Initialize a buffer queue that can hold up to `max_chunks` buffers121* each of size `chunk_size`. The bufq will not allow writing of122* more bytes than can be held in `max_chunks`.123*/124void Curl_bufq_init(struct bufq *q, size_t chunk_size, size_t max_chunks);125126/**127* Initialize a buffer queue that can hold up to `max_chunks` buffers128* each of size `chunk_size` with the given options. See `BUFQ_OPT_*`.129*/130void Curl_bufq_init2(struct bufq *q, size_t chunk_size,131size_t max_chunks, int opts);132133void Curl_bufq_initp(struct bufq *q, struct bufc_pool *pool,134size_t max_chunks, int opts);135136/**137* Reset the buffer queue to be empty. Will keep any allocated buffer138* chunks around.139*/140void Curl_bufq_reset(struct bufq *q);141142/**143* Free all resources held by the buffer queue.144*/145void Curl_bufq_free(struct bufq *q);146147/**148* Return the total amount of data in the queue.149*/150size_t Curl_bufq_len(const struct bufq *q);151152/**153* Returns TRUE iff there is no data in the buffer queue.154*/155bool Curl_bufq_is_empty(const struct bufq *q);156157/**158* Returns TRUE iff there is no space left in the buffer queue.159*/160bool Curl_bufq_is_full(const struct bufq *q);161162/**163* Write buf to the end of the buffer queue. The buf is copied164* and the amount of copied bytes is returned.165* A return code of -1 indicates an error, setting `err` to the166* cause. An err of CURLE_AGAIN is returned if the buffer queue is full.167*/168ssize_t Curl_bufq_write(struct bufq *q,169const unsigned char *buf, size_t len,170CURLcode *err);171172CURLcode Curl_bufq_cwrite(struct bufq *q,173const char *buf, size_t len,174size_t *pnwritten);175176/**177* Remove `len` bytes from the end of the buffer queue again.178* Returns CURLE_AGAIN if less than `len` bytes were in the queue.179*/180CURLcode Curl_bufq_unwrite(struct bufq *q, size_t len);181182/**183* Read buf from the start of the buffer queue. The buf is copied184* and the amount of copied bytes is returned.185* A return code of -1 indicates an error, setting `err` to the186* cause. An err of CURLE_AGAIN is returned if the buffer queue is empty.187*/188ssize_t Curl_bufq_read(struct bufq *q, unsigned char *buf, size_t len,189CURLcode *err);190191CURLcode Curl_bufq_cread(struct bufq *q, char *buf, size_t len,192size_t *pnread);193194/**195* Peek at the head chunk in the buffer queue. Returns a pointer to196* the chunk buf (at the current offset) and its length. Does not197* modify the buffer queue.198* Returns TRUE iff bytes are available. Sets `pbuf` to NULL and `plen`199* to 0 when no bytes are available.200* Repeated calls return the same information until the buffer queue201* is modified, see `Curl_bufq_skip()``202*/203bool Curl_bufq_peek(struct bufq *q,204const unsigned char **pbuf, size_t *plen);205206bool Curl_bufq_peek_at(struct bufq *q, size_t offset,207const unsigned char **pbuf, size_t *plen);208209/**210* Tell the buffer queue to discard `amount` buf bytes at the head211* of the queue. Skipping more buf than is currently buffered will212* just empty the queue.213*/214void Curl_bufq_skip(struct bufq *q, size_t amount);215216typedef ssize_t Curl_bufq_writer(void *writer_ctx,217const unsigned char *buf, size_t len,218CURLcode *err);219/**220* Passes the chunks in the buffer queue to the writer and returns221* the amount of buf written. A writer may return -1 and CURLE_AGAIN222* to indicate blocking at which point the queue will stop and return223* the amount of buf passed so far.224* -1 is returned on any other errors reported by the writer.225* Note that in case of a -1 chunks may have been written and226* the buffer queue will have different length than before.227*/228ssize_t Curl_bufq_pass(struct bufq *q, Curl_bufq_writer *writer,229void *writer_ctx, CURLcode *err);230231typedef ssize_t Curl_bufq_reader(void *reader_ctx,232unsigned char *buf, size_t len,233CURLcode *err);234235/**236* Read date and append it to the end of the buffer queue until the237* reader returns blocking or the queue is full. A reader returns238* -1 and CURLE_AGAIN to indicate blocking.239* Returns the total amount of buf read (may be 0) or -1 on other240* reader errors.241* Note that in case of a -1 chunks may have been read and242* the buffer queue will have different length than before.243*/244ssize_t Curl_bufq_slurp(struct bufq *q, Curl_bufq_reader *reader,245void *reader_ctx, CURLcode *err);246247/**248* Read *once* up to `max_len` bytes and append it to the buffer.249* if `max_len` is 0, no limit is imposed besides the chunk space.250* Returns the total amount of buf read (may be 0) or -1 on other251* reader errors.252*/253ssize_t Curl_bufq_sipn(struct bufq *q, size_t max_len,254Curl_bufq_reader *reader, void *reader_ctx,255CURLcode *err);256257/**258* Write buf to the end of the buffer queue.259* Will write bufq content or passed `buf` directly using the `writer`260* callback when it sees fit. 'buf' might get passed directly261* on or is placed into the buffer, depending on `len` and current262* amount buffered, chunk size, etc.263*/264ssize_t Curl_bufq_write_pass(struct bufq *q,265const unsigned char *buf, size_t len,266Curl_bufq_writer *writer, void *writer_ctx,267CURLcode *err);268269#endif /* HEADER_CURL_BUFQ_H */270271272