113498266Sopenharmony_ci#ifndef HEADER_CURL_BUFQ_H
213498266Sopenharmony_ci#define HEADER_CURL_BUFQ_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci *
1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1513498266Sopenharmony_ci *
1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1913498266Sopenharmony_ci *
2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2113498266Sopenharmony_ci * KIND, either express or implied.
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * SPDX-License-Identifier: curl
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci#include "curl_setup.h"
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#include <curl/curl.h>
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci/**
3113498266Sopenharmony_ci * A chunk of bytes for reading and writing.
3213498266Sopenharmony_ci * The size is fixed a creation with read and write offset
3313498266Sopenharmony_ci * for where unread content is.
3413498266Sopenharmony_ci */
3513498266Sopenharmony_cistruct buf_chunk {
3613498266Sopenharmony_ci  struct buf_chunk *next;  /* to keep it in a list */
3713498266Sopenharmony_ci  size_t dlen;             /* the amount of allocated x.data[] */
3813498266Sopenharmony_ci  size_t r_offset;         /* first unread bytes */
3913498266Sopenharmony_ci  size_t w_offset;         /* one after last written byte */
4013498266Sopenharmony_ci  union {
4113498266Sopenharmony_ci    unsigned char data[1]; /* the buffer for `dlen` bytes */
4213498266Sopenharmony_ci    void *dummy;           /* alignment */
4313498266Sopenharmony_ci  } x;
4413498266Sopenharmony_ci};
4513498266Sopenharmony_ci
4613498266Sopenharmony_ci/**
4713498266Sopenharmony_ci * A pool for providing/keeping a number of chunks of the same size
4813498266Sopenharmony_ci *
4913498266Sopenharmony_ci * The same pool can be shared by many `bufq` instances. However, a pool
5013498266Sopenharmony_ci * is not thread safe. All bufqs using it are supposed to operate in the
5113498266Sopenharmony_ci * same thread.
5213498266Sopenharmony_ci */
5313498266Sopenharmony_cistruct bufc_pool {
5413498266Sopenharmony_ci  struct buf_chunk *spare;  /* list of available spare chunks */
5513498266Sopenharmony_ci  size_t chunk_size;        /* the size of chunks in this pool */
5613498266Sopenharmony_ci  size_t spare_count;       /* current number of spare chunks in list */
5713498266Sopenharmony_ci  size_t spare_max;         /* max number of spares to keep */
5813498266Sopenharmony_ci};
5913498266Sopenharmony_ci
6013498266Sopenharmony_civoid Curl_bufcp_init(struct bufc_pool *pool,
6113498266Sopenharmony_ci                     size_t chunk_size, size_t spare_max);
6213498266Sopenharmony_ci
6313498266Sopenharmony_civoid Curl_bufcp_free(struct bufc_pool *pool);
6413498266Sopenharmony_ci
6513498266Sopenharmony_ci/**
6613498266Sopenharmony_ci * A queue of byte chunks for reading and writing.
6713498266Sopenharmony_ci * Reading is done from `head`, writing is done to `tail`.
6813498266Sopenharmony_ci *
6913498266Sopenharmony_ci * `bufq`s can be empty or full or neither. Its `len` is the number
7013498266Sopenharmony_ci * of bytes that can be read. For an empty bufq, `len` will be 0.
7113498266Sopenharmony_ci *
7213498266Sopenharmony_ci * By default, a bufq can hold up to `max_chunks * chunk_size` number
7313498266Sopenharmony_ci * of bytes. When `max_chunks` are used (in the `head` list) and the
7413498266Sopenharmony_ci * `tail` chunk is full, the bufq will report that it is full.
7513498266Sopenharmony_ci *
7613498266Sopenharmony_ci * On a full bufq, `len` may be less than the maximum number of bytes,
7713498266Sopenharmony_ci * e.g. when the head chunk is partially read. `len` may also become
7813498266Sopenharmony_ci * larger than the max when option `BUFQ_OPT_SOFT_LIMIT` is used.
7913498266Sopenharmony_ci *
8013498266Sopenharmony_ci * By default, writing to a full bufq will return (-1, CURLE_AGAIN). Same
8113498266Sopenharmony_ci * as reading from an empty bufq.
8213498266Sopenharmony_ci * With `BUFQ_OPT_SOFT_LIMIT` set, a bufq will allow writing becond this
8313498266Sopenharmony_ci * limit and use more than `max_chunks`. However it will report that it
8413498266Sopenharmony_ci * is full nevertheless. This is provided for situation where writes
8513498266Sopenharmony_ci * preferably never fail (except for memory exhaustion).
8613498266Sopenharmony_ci *
8713498266Sopenharmony_ci * By default and without a pool, a bufq will keep chunks that read
8813498266Sopenharmony_ci * read empty in its `spare` list. Option `BUFQ_OPT_NO_SPARES` will
8913498266Sopenharmony_ci * disable that and free chunks once they become empty.
9013498266Sopenharmony_ci *
9113498266Sopenharmony_ci * When providing a pool to a bufq, all chunk creation and spare handling
9213498266Sopenharmony_ci * will be delegated to that pool.
9313498266Sopenharmony_ci */
9413498266Sopenharmony_cistruct bufq {
9513498266Sopenharmony_ci  struct buf_chunk *head;       /* chunk with bytes to read from */
9613498266Sopenharmony_ci  struct buf_chunk *tail;       /* chunk to write to */
9713498266Sopenharmony_ci  struct buf_chunk *spare;      /* list of free chunks, unless `pool` */
9813498266Sopenharmony_ci  struct bufc_pool *pool;       /* optional pool for free chunks */
9913498266Sopenharmony_ci  size_t chunk_count;           /* current number of chunks in `head+spare` */
10013498266Sopenharmony_ci  size_t max_chunks;            /* max `head` chunks to use */
10113498266Sopenharmony_ci  size_t chunk_size;            /* size of chunks to manage */
10213498266Sopenharmony_ci  int opts;                     /* options for handling queue, see below */
10313498266Sopenharmony_ci};
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci/**
10613498266Sopenharmony_ci * Default behaviour: chunk limit is "hard", meaning attempts to write
10713498266Sopenharmony_ci * more bytes than can be hold in `max_chunks` is refused and will return
10813498266Sopenharmony_ci * -1, CURLE_AGAIN. */
10913498266Sopenharmony_ci#define BUFQ_OPT_NONE        (0)
11013498266Sopenharmony_ci/**
11113498266Sopenharmony_ci * Make `max_chunks` a "soft" limit. A bufq will report that it is "full"
11213498266Sopenharmony_ci * when `max_chunks` are used, but allows writing beyond this limit.
11313498266Sopenharmony_ci */
11413498266Sopenharmony_ci#define BUFQ_OPT_SOFT_LIMIT  (1 << 0)
11513498266Sopenharmony_ci/**
11613498266Sopenharmony_ci * Do not keep spare chunks.
11713498266Sopenharmony_ci */
11813498266Sopenharmony_ci#define BUFQ_OPT_NO_SPARES   (1 << 1)
11913498266Sopenharmony_ci
12013498266Sopenharmony_ci/**
12113498266Sopenharmony_ci * Initialize a buffer queue that can hold up to `max_chunks` buffers
12213498266Sopenharmony_ci * each of size `chunk_size`. The bufq will not allow writing of
12313498266Sopenharmony_ci * more bytes than can be held in `max_chunks`.
12413498266Sopenharmony_ci */
12513498266Sopenharmony_civoid Curl_bufq_init(struct bufq *q, size_t chunk_size, size_t max_chunks);
12613498266Sopenharmony_ci
12713498266Sopenharmony_ci/**
12813498266Sopenharmony_ci * Initialize a buffer queue that can hold up to `max_chunks` buffers
12913498266Sopenharmony_ci * each of size `chunk_size` with the given options. See `BUFQ_OPT_*`.
13013498266Sopenharmony_ci */
13113498266Sopenharmony_civoid Curl_bufq_init2(struct bufq *q, size_t chunk_size,
13213498266Sopenharmony_ci                     size_t max_chunks, int opts);
13313498266Sopenharmony_ci
13413498266Sopenharmony_civoid Curl_bufq_initp(struct bufq *q, struct bufc_pool *pool,
13513498266Sopenharmony_ci                     size_t max_chunks, int opts);
13613498266Sopenharmony_ci
13713498266Sopenharmony_ci/**
13813498266Sopenharmony_ci * Reset the buffer queue to be empty. Will keep any allocated buffer
13913498266Sopenharmony_ci * chunks around.
14013498266Sopenharmony_ci */
14113498266Sopenharmony_civoid Curl_bufq_reset(struct bufq *q);
14213498266Sopenharmony_ci
14313498266Sopenharmony_ci/**
14413498266Sopenharmony_ci * Free all resources held by the buffer queue.
14513498266Sopenharmony_ci */
14613498266Sopenharmony_civoid Curl_bufq_free(struct bufq *q);
14713498266Sopenharmony_ci
14813498266Sopenharmony_ci/**
14913498266Sopenharmony_ci * Return the total amount of data in the queue.
15013498266Sopenharmony_ci */
15113498266Sopenharmony_cisize_t Curl_bufq_len(const struct bufq *q);
15213498266Sopenharmony_ci
15313498266Sopenharmony_ci/**
15413498266Sopenharmony_ci * Return the total amount of free space in the queue.
15513498266Sopenharmony_ci * The returned length is the number of bytes that can
15613498266Sopenharmony_ci * be expected to be written successfully to the bufq,
15713498266Sopenharmony_ci * providing no memory allocations fail.
15813498266Sopenharmony_ci */
15913498266Sopenharmony_cisize_t Curl_bufq_space(const struct bufq *q);
16013498266Sopenharmony_ci
16113498266Sopenharmony_ci/**
16213498266Sopenharmony_ci * Returns TRUE iff there is no data in the buffer queue.
16313498266Sopenharmony_ci */
16413498266Sopenharmony_cibool Curl_bufq_is_empty(const struct bufq *q);
16513498266Sopenharmony_ci
16613498266Sopenharmony_ci/**
16713498266Sopenharmony_ci * Returns TRUE iff there is no space left in the buffer queue.
16813498266Sopenharmony_ci */
16913498266Sopenharmony_cibool Curl_bufq_is_full(const struct bufq *q);
17013498266Sopenharmony_ci
17113498266Sopenharmony_ci/**
17213498266Sopenharmony_ci * Write buf to the end of the buffer queue. The buf is copied
17313498266Sopenharmony_ci * and the amount of copied bytes is returned.
17413498266Sopenharmony_ci * A return code of -1 indicates an error, setting `err` to the
17513498266Sopenharmony_ci * cause. An err of CURLE_AGAIN is returned if the buffer queue is full.
17613498266Sopenharmony_ci */
17713498266Sopenharmony_cissize_t Curl_bufq_write(struct bufq *q,
17813498266Sopenharmony_ci                        const unsigned char *buf, size_t len,
17913498266Sopenharmony_ci                        CURLcode *err);
18013498266Sopenharmony_ci
18113498266Sopenharmony_ci/**
18213498266Sopenharmony_ci * Read buf from the start of the buffer queue. The buf is copied
18313498266Sopenharmony_ci * and the amount of copied bytes is returned.
18413498266Sopenharmony_ci * A return code of -1 indicates an error, setting `err` to the
18513498266Sopenharmony_ci * cause. An err of CURLE_AGAIN is returned if the buffer queue is empty.
18613498266Sopenharmony_ci */
18713498266Sopenharmony_cissize_t Curl_bufq_read(struct bufq *q, unsigned char *buf, size_t len,
18813498266Sopenharmony_ci                        CURLcode *err);
18913498266Sopenharmony_ci
19013498266Sopenharmony_ci/**
19113498266Sopenharmony_ci * Peek at the head chunk in the buffer queue. Returns a pointer to
19213498266Sopenharmony_ci * the chunk buf (at the current offset) and its length. Does not
19313498266Sopenharmony_ci * modify the buffer queue.
19413498266Sopenharmony_ci * Returns TRUE iff bytes are available. Sets `pbuf` to NULL and `plen`
19513498266Sopenharmony_ci * to 0 when no bytes are available.
19613498266Sopenharmony_ci * Repeated calls return the same information until the buffer queue
19713498266Sopenharmony_ci * is modified, see `Curl_bufq_skip()``
19813498266Sopenharmony_ci */
19913498266Sopenharmony_cibool Curl_bufq_peek(struct bufq *q,
20013498266Sopenharmony_ci                    const unsigned char **pbuf, size_t *plen);
20113498266Sopenharmony_ci
20213498266Sopenharmony_cibool Curl_bufq_peek_at(struct bufq *q, size_t offset,
20313498266Sopenharmony_ci                       const unsigned char **pbuf, size_t *plen);
20413498266Sopenharmony_ci
20513498266Sopenharmony_ci/**
20613498266Sopenharmony_ci * Tell the buffer queue to discard `amount` buf bytes at the head
20713498266Sopenharmony_ci * of the queue. Skipping more buf than is currently buffered will
20813498266Sopenharmony_ci * just empty the queue.
20913498266Sopenharmony_ci */
21013498266Sopenharmony_civoid Curl_bufq_skip(struct bufq *q, size_t amount);
21113498266Sopenharmony_ci
21213498266Sopenharmony_citypedef ssize_t Curl_bufq_writer(void *writer_ctx,
21313498266Sopenharmony_ci                                 const unsigned char *buf, size_t len,
21413498266Sopenharmony_ci                                 CURLcode *err);
21513498266Sopenharmony_ci/**
21613498266Sopenharmony_ci * Passes the chunks in the buffer queue to the writer and returns
21713498266Sopenharmony_ci * the amount of buf written. A writer may return -1 and CURLE_AGAIN
21813498266Sopenharmony_ci * to indicate blocking at which point the queue will stop and return
21913498266Sopenharmony_ci * the amount of buf passed so far.
22013498266Sopenharmony_ci * -1 is returned on any other errors reported by the writer.
22113498266Sopenharmony_ci * Note that in case of a -1 chunks may have been written and
22213498266Sopenharmony_ci * the buffer queue will have different length than before.
22313498266Sopenharmony_ci */
22413498266Sopenharmony_cissize_t Curl_bufq_pass(struct bufq *q, Curl_bufq_writer *writer,
22513498266Sopenharmony_ci                       void *writer_ctx, CURLcode *err);
22613498266Sopenharmony_ci
22713498266Sopenharmony_citypedef ssize_t Curl_bufq_reader(void *reader_ctx,
22813498266Sopenharmony_ci                                 unsigned char *buf, size_t len,
22913498266Sopenharmony_ci                                 CURLcode *err);
23013498266Sopenharmony_ci
23113498266Sopenharmony_ci/**
23213498266Sopenharmony_ci * Read date and append it to the end of the buffer queue until the
23313498266Sopenharmony_ci * reader returns blocking or the queue is full. A reader returns
23413498266Sopenharmony_ci * -1 and CURLE_AGAIN to indicate blocking.
23513498266Sopenharmony_ci * Returns the total amount of buf read (may be 0) or -1 on other
23613498266Sopenharmony_ci * reader errors.
23713498266Sopenharmony_ci * Note that in case of a -1 chunks may have been read and
23813498266Sopenharmony_ci * the buffer queue will have different length than before.
23913498266Sopenharmony_ci */
24013498266Sopenharmony_cissize_t Curl_bufq_slurp(struct bufq *q, Curl_bufq_reader *reader,
24113498266Sopenharmony_ci                        void *reader_ctx, CURLcode *err);
24213498266Sopenharmony_ci
24313498266Sopenharmony_ci/**
24413498266Sopenharmony_ci * Read *once* up to `max_len` bytes and append it to the buffer.
24513498266Sopenharmony_ci * if `max_len` is 0, no limit is imposed besides the chunk space.
24613498266Sopenharmony_ci * Returns the total amount of buf read (may be 0) or -1 on other
24713498266Sopenharmony_ci * reader errors.
24813498266Sopenharmony_ci */
24913498266Sopenharmony_cissize_t Curl_bufq_sipn(struct bufq *q, size_t max_len,
25013498266Sopenharmony_ci                       Curl_bufq_reader *reader, void *reader_ctx,
25113498266Sopenharmony_ci                       CURLcode *err);
25213498266Sopenharmony_ci
25313498266Sopenharmony_ci/**
25413498266Sopenharmony_ci * Write buf to the end of the buffer queue.
25513498266Sopenharmony_ci * Will write bufq content or passed `buf` directly using the `writer`
25613498266Sopenharmony_ci * callback when it sees fit. 'buf' might get passed directly
25713498266Sopenharmony_ci * on or is placed into the buffer, depending on `len` and current
25813498266Sopenharmony_ci * amount buffered, chunk size, etc.
25913498266Sopenharmony_ci */
26013498266Sopenharmony_cissize_t Curl_bufq_write_pass(struct bufq *q,
26113498266Sopenharmony_ci                             const unsigned char *buf, size_t len,
26213498266Sopenharmony_ci                             Curl_bufq_writer *writer, void *writer_ctx,
26313498266Sopenharmony_ci                             CURLcode *err);
26413498266Sopenharmony_ci
26513498266Sopenharmony_ci#endif /* HEADER_CURL_BUFQ_H */
266