2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #endif /* HAVE_CONFIG_H */
32 #include <nghttp2/nghttp2.h>
34 #include "nghttp2_int.h"
35 #include "nghttp2_mem.h"
38 /* This points to the beginning of the buffer. The effective range
39 of buffer is [begin, end). */
41 /* This points to the memory one byte beyond the end of the
44 /* The position indicator for effective start of the buffer. pos <=
47 /* The position indicator for effective one beyond of the end of the
48 buffer. last <= end must be hold. */
50 /* Mark arbitrary position in buffer [begin, end) */
54 #define nghttp2_buf_len(BUF) ((ssize_t)((BUF)->last - (BUF)->pos))
55 #define nghttp2_buf_avail(BUF) ((ssize_t)((BUF)->end - (BUF)->last))
56 #define nghttp2_buf_mark_avail(BUF) ((ssize_t)((BUF)->mark - (BUF)->last))
57 #define nghttp2_buf_cap(BUF) ((ssize_t)((BUF)->end - (BUF)->begin))
59 #define nghttp2_buf_pos_offset(BUF) ((ssize_t)((BUF)->pos - (BUF)->begin))
60 #define nghttp2_buf_last_offset(BUF) ((ssize_t)((BUF)->last - (BUF)->begin))
62 #define nghttp2_buf_shift_right(BUF, AMT) \
68 #define nghttp2_buf_shift_left(BUF, AMT) \
75 * Initializes the |buf|. No memory is allocated in this function. Use
76 * nghttp2_buf_reserve() or nghttp2_buf_reserve2() to allocate memory.
78 void nghttp2_buf_init(nghttp2_buf *buf);
81 * Initializes the |buf| and allocates at least |initial| bytes of
84 * This function returns 0 if it succeeds, or one of the following
85 * negative error codes:
90 int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem);
93 * Frees buffer in |buf|.
95 void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem);
98 * Extends buffer so that nghttp2_buf_cap() returns at least
99 * |new_cap|. If extensions took place, buffer pointers in |buf| will
102 * This function returns 0 if it succeeds, or one of the followings
103 * negative error codes:
108 int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem);
111 * Resets pos, last, mark member of |buf| to buf->begin.
113 void nghttp2_buf_reset(nghttp2_buf *buf);
116 * Initializes |buf| using supplied buffer |begin| of length
117 * |len|. Semantically, the application should not call *_reserve() or
118 * nghttp2_free() functions for |buf|.
120 void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len);
122 struct nghttp2_buf_chain;
124 typedef struct nghttp2_buf_chain nghttp2_buf_chain;
126 /* Chains 2 buffers */
127 struct nghttp2_buf_chain {
128 /* Points to the subsequent buffer. NULL if there is no such
130 nghttp2_buf_chain *next;
135 /* Points to the first buffer */
136 nghttp2_buf_chain *head;
137 /* Buffer pointer where write occurs. */
138 nghttp2_buf_chain *cur;
139 /* Memory allocator */
141 /* The buffer capacity of each buf */
143 /* The maximum number of nghttp2_buf_chain */
145 /* The number of nghttp2_buf_chain allocated */
147 /* The number of nghttp2_buf_chain to keep on reset */
149 /* pos offset from begin in each buffers. On initialization and
150 reset, buf->pos and buf->last are positioned at buf->begin +
156 * This is the same as calling nghttp2_bufs_init2 with the given
157 * arguments and offset = 0.
159 int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
163 * This is the same as calling nghttp2_bufs_init3 with the given
164 * arguments and chunk_keep = max_chunk.
166 int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
167 size_t max_chunk, size_t offset, nghttp2_mem *mem);
170 * Initializes |bufs|. Each buffer size is given in the
171 * |chunk_length|. The maximum number of buffers is given in the
172 * |max_chunk|. On reset, first |chunk_keep| buffers are kept and
173 * remaining buffers are deleted. Each buffer will have bufs->pos and
174 * bufs->last shifted to left by |offset| bytes on creation and reset.
176 * This function allocates first buffer. bufs->head and bufs->cur
177 * will point to the first buffer after this call.
179 * This function returns 0 if it succeeds, or one of the following
180 * negative error codes:
184 * NGHTTP2_ERR_INVALID_ARGUMENT
185 * chunk_keep is 0; or max_chunk < chunk_keep; or offset is too
188 int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
189 size_t max_chunk, size_t chunk_keep, size_t offset,
193 * Frees any related resources to the |bufs|.
195 void nghttp2_bufs_free(nghttp2_bufs *bufs);
198 * Initializes |bufs| using supplied buffer |begin| of length |len|.
199 * The first buffer bufs->head uses buffer |begin|. The buffer size
200 * is fixed and no allocate extra chunk buffer is allocated. In other
201 * words, max_chunk = chunk_keep = 1. To free the resource allocated
202 * for |bufs|, use nghttp2_bufs_wrap_free().
204 * This function returns 0 if it succeeds, or one of the following
205 * negative error codes:
210 int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
214 * Frees any related resource to the |bufs|. This function does not
215 * free supplied buffer provided in nghttp2_bufs_wrap_init().
217 void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs);
220 * Reallocates internal buffer using |chunk_length|. The max_chunk,
221 * chunk_keep and offset do not change. After successful allocation
222 * of new buffer, previous buffers are deallocated without copying
223 * anything into new buffers. chunk_used is reset to 1.
225 * This function returns 0 if it succeeds, or one of the following
226 * negative error codes:
230 * NGHTTP2_ERR_INVALID_ARGUMENT
231 * chunk_length < offset
233 int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length);
236 * Appends the |data| of length |len| to the |bufs|. The write starts
237 * at bufs->cur->buf.last. A new buffers will be allocated to store
240 * This function returns 0 if it succeeds, or one of the following
241 * negative error codes:
245 * NGHTTP2_ERR_BUFFER_ERROR
246 * Out of buffer space.
248 int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len);
251 * Appends a single byte |b| to the |bufs|. The write starts at
252 * bufs->cur->buf.last. A new buffers will be allocated to store all
255 * This function returns 0 if it succeeds, or one of the following
256 * negative error codes:
260 * NGHTTP2_ERR_BUFFER_ERROR
261 * Out of buffer space.
263 int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b);
266 * Behaves like nghttp2_bufs_addb(), but this does not update
269 int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b);
271 #define nghttp2_bufs_fast_addb(BUFS, B) \
273 *(BUFS)->cur->buf.last++ = B; \
276 #define nghttp2_bufs_fast_addb_hold(BUFS, B) \
278 *(BUFS)->cur->buf.last = B; \
282 * Performs bitwise-OR of |b| at bufs->cur->buf.last. A new buffers
283 * will be allocated if necessary.
285 * This function returns 0 if it succeeds, or one of the following
286 * negative error codes:
290 * NGHTTP2_ERR_BUFFER_ERROR
291 * Out of buffer space.
293 int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b);
296 * Behaves like nghttp2_bufs_orb(), but does not update buf->last
299 int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b);
301 #define nghttp2_bufs_fast_orb(BUFS, B) \
303 *(BUFS)->cur->buf.last++ |= B; \
306 #define nghttp2_bufs_fast_orb_hold(BUFS, B) \
308 *(BUFS)->cur->buf.last |= B; \
312 * Copies all data stored in |bufs| to the contagious buffer. This
313 * function allocates the contagious memory to store all data in
314 * |bufs| and assigns it to |*out|.
316 * On successful return, nghttp2_bufs_len(bufs) returns 0, just like
317 * after calling nghttp2_bufs_reset().
319 * This function returns the length of copied data and assigns the
320 * pointer to copied data to |*out| if it succeeds, or one of the
321 * following negative error codes:
326 ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out);
329 * Resets |bufs| and makes the buffers empty.
331 void nghttp2_bufs_reset(nghttp2_bufs *bufs);
334 * Moves bufs->cur to bufs->cur->next. If resulting bufs->cur is
335 * NULL, this function allocates new buffers and bufs->cur points to
338 * This function returns 0 if it succeeds, or one of the following
339 * negative error codes:
343 * NGHTTP2_ERR_BUFFER_ERROR
344 * Out of buffer space.
346 int nghttp2_bufs_advance(nghttp2_bufs *bufs);
348 /* Sets bufs->cur to bufs->head */
349 #define nghttp2_bufs_rewind(BUFS) \
351 (BUFS)->cur = (BUFS)->head; \
355 * Move bufs->cur, from the current position, using next member, to
356 * the last buf which has nghttp2_buf_len(buf) > 0 without seeing buf
357 * which satisfies nghttp2_buf_len(buf) == 0. If
358 * nghttp2_buf_len(&bufs->cur->buf) == 0 or bufs->cur->next is NULL,
359 * bufs->cur is unchanged.
361 void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs);
364 * Returns nonzero if bufs->cur->next is not emtpy.
366 int nghttp2_bufs_next_present(nghttp2_bufs *bufs);
368 #define nghttp2_bufs_cur_avail(BUFS) nghttp2_buf_avail(&(BUFS)->cur->buf)
371 * Returns the buffer length of |bufs|.
373 ssize_t nghttp2_bufs_len(nghttp2_bufs *bufs);
375 #endif /* NGHTTP2_BUF_H */