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.
25 #include "nghttp2_buf.h"
29 #include "nghttp2_helper.h"
30 #include "nghttp2_debug.h"
32 void nghttp2_buf_init(nghttp2_buf *buf) {
40 int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem) {
41 nghttp2_buf_init(buf);
42 return nghttp2_buf_reserve(buf, initial, mem);
45 void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem) {
50 nghttp2_mem_free(mem, buf->begin);
54 int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem) {
58 cap = nghttp2_buf_cap(buf);
64 new_cap = nghttp2_max(new_cap, cap * 2);
66 ptr = nghttp2_mem_realloc(mem, buf->begin, new_cap);
68 return NGHTTP2_ERR_NOMEM;
71 buf->pos = ptr + (buf->pos - buf->begin);
72 buf->last = ptr + (buf->last - buf->begin);
73 buf->mark = ptr + (buf->mark - buf->begin);
75 buf->end = ptr + new_cap;
80 void nghttp2_buf_reset(nghttp2_buf *buf) {
81 buf->pos = buf->last = buf->mark = buf->begin;
84 void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len) {
85 buf->begin = buf->pos = buf->last = buf->mark = begin;
86 buf->end = begin + len;
89 static int buf_chain_new(nghttp2_buf_chain **chain, size_t chunk_length,
93 *chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
95 return NGHTTP2_ERR_NOMEM;
98 (*chain)->next = NULL;
100 rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length, mem);
102 nghttp2_mem_free(mem, *chain);
103 return NGHTTP2_ERR_NOMEM;
109 static void buf_chain_del(nghttp2_buf_chain *chain, nghttp2_mem *mem) {
110 nghttp2_buf_free(&chain->buf, mem);
111 nghttp2_mem_free(mem, chain);
114 int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
116 return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0, mem);
119 int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
120 size_t max_chunk, size_t offset, nghttp2_mem *mem) {
121 return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset,
125 int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
126 size_t max_chunk, size_t chunk_keep, size_t offset,
129 nghttp2_buf_chain *chain;
131 if (chunk_keep == 0 || max_chunk < chunk_keep || chunk_length < offset) {
132 return NGHTTP2_ERR_INVALID_ARGUMENT;
135 rv = buf_chain_new(&chain, chunk_length, mem);
141 bufs->offset = offset;
144 bufs->cur = bufs->head;
146 nghttp2_buf_shift_right(&bufs->cur->buf, offset);
148 bufs->chunk_length = chunk_length;
149 bufs->chunk_used = 1;
150 bufs->max_chunk = max_chunk;
151 bufs->chunk_keep = chunk_keep;
156 int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) {
158 nghttp2_buf_chain *chain;
160 if (chunk_length < bufs->offset) {
161 return NGHTTP2_ERR_INVALID_ARGUMENT;
164 rv = buf_chain_new(&chain, chunk_length, bufs->mem);
169 nghttp2_bufs_free(bufs);
172 bufs->cur = bufs->head;
174 nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
176 bufs->chunk_length = chunk_length;
177 bufs->chunk_used = 1;
182 void nghttp2_bufs_free(nghttp2_bufs *bufs) {
183 nghttp2_buf_chain *chain, *next_chain;
189 for (chain = bufs->head; chain;) {
190 next_chain = chain->next;
192 buf_chain_del(chain, bufs->mem);
200 int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
202 nghttp2_buf_chain *chain;
204 chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
206 return NGHTTP2_ERR_NOMEM;
211 nghttp2_buf_wrap_init(&chain->buf, begin, len);
217 bufs->cur = bufs->head;
219 bufs->chunk_length = len;
220 bufs->chunk_used = 1;
222 bufs->chunk_keep = 1;
227 int nghttp2_bufs_wrap_init2(nghttp2_bufs *bufs, const nghttp2_vec *vec,
228 size_t veclen, nghttp2_mem *mem) {
230 nghttp2_buf_chain *cur_chain;
231 nghttp2_buf_chain *head_chain;
232 nghttp2_buf_chain **dst_chain = &head_chain;
235 return nghttp2_bufs_wrap_init(bufs, NULL, 0, mem);
238 head_chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain) * veclen);
239 if (head_chain == NULL) {
240 return NGHTTP2_ERR_NOMEM;
243 for (i = 0; i < veclen; ++i) {
244 cur_chain = &head_chain[i];
245 cur_chain->next = NULL;
246 nghttp2_buf_wrap_init(&cur_chain->buf, vec[i].base, vec[i].len);
248 *dst_chain = cur_chain;
249 dst_chain = &cur_chain->next;
255 bufs->head = head_chain;
256 bufs->cur = bufs->head;
258 /* We don't use chunk_length since no allocation is expected. */
259 bufs->chunk_length = 0;
260 bufs->chunk_used = veclen;
261 bufs->max_chunk = veclen;
262 bufs->chunk_keep = veclen;
267 void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) {
273 nghttp2_mem_free(bufs->mem, bufs->head);
277 void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs) {
278 nghttp2_buf_chain *ci;
280 for (ci = bufs->cur; ci; ci = ci->next) {
281 if (nghttp2_buf_len(&ci->buf) == 0) {
289 size_t nghttp2_bufs_len(nghttp2_bufs *bufs) {
290 nghttp2_buf_chain *ci;
294 for (ci = bufs->head; ci; ci = ci->next) {
295 len += nghttp2_buf_len(&ci->buf);
301 static int bufs_alloc_chain(nghttp2_bufs *bufs) {
303 nghttp2_buf_chain *chain;
305 if (bufs->cur->next) {
306 bufs->cur = bufs->cur->next;
311 if (bufs->max_chunk == bufs->chunk_used) {
312 return NGHTTP2_ERR_BUFFER_ERROR;
315 rv = buf_chain_new(&chain, bufs->chunk_length, bufs->mem);
320 DEBUGF("new buffer %zu bytes allocated for bufs %p, used %zu\n",
321 bufs->chunk_length, bufs, bufs->chunk_used);
325 bufs->cur->next = chain;
328 nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
333 int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len) {
342 buf = &bufs->cur->buf;
344 nwrite = nghttp2_min(nghttp2_buf_avail(buf), len);
346 rv = bufs_alloc_chain(bufs);
353 buf->last = nghttp2_cpymem(buf->last, p, nwrite);
361 static int bufs_ensure_addb(nghttp2_bufs *bufs) {
365 buf = &bufs->cur->buf;
367 if (nghttp2_buf_avail(buf) > 0) {
371 rv = bufs_alloc_chain(bufs);
379 int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b) {
382 rv = bufs_ensure_addb(bufs);
387 *bufs->cur->buf.last++ = b;
392 int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b) {
395 rv = bufs_ensure_addb(bufs);
400 *bufs->cur->buf.last = b;
405 int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b) {
408 rv = bufs_ensure_addb(bufs);
413 *bufs->cur->buf.last++ |= b;
418 int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b) {
421 rv = bufs_ensure_addb(bufs);
426 *bufs->cur->buf.last |= b;
431 ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) {
433 nghttp2_buf_chain *chain;
440 for (chain = bufs->head; chain; chain = chain->next) {
441 len += nghttp2_buf_len(&chain->buf);
449 res = nghttp2_mem_malloc(bufs->mem, len);
451 return NGHTTP2_ERR_NOMEM;
454 nghttp2_buf_wrap_init(&resbuf, res, len);
456 for (chain = bufs->head; chain; chain = chain->next) {
458 resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
466 size_t nghttp2_bufs_remove_copy(nghttp2_bufs *bufs, uint8_t *out) {
468 nghttp2_buf_chain *chain;
472 len = nghttp2_bufs_len(bufs);
474 nghttp2_buf_wrap_init(&resbuf, out, len);
476 for (chain = bufs->head; chain; chain = chain->next) {
478 resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
484 void nghttp2_bufs_reset(nghttp2_bufs *bufs) {
485 nghttp2_buf_chain *chain, *ci;
488 k = bufs->chunk_keep;
490 for (ci = bufs->head; ci; ci = ci->next) {
491 nghttp2_buf_reset(&ci->buf);
492 nghttp2_buf_shift_right(&ci->buf, bufs->offset);
503 for (ci = chain; ci;) {
506 buf_chain_del(ci, bufs->mem);
511 bufs->chunk_used = bufs->chunk_keep;
514 bufs->cur = bufs->head;
517 int nghttp2_bufs_advance(nghttp2_bufs *bufs) { return bufs_alloc_chain(bufs); }
519 int nghttp2_bufs_next_present(nghttp2_bufs *bufs) {
520 nghttp2_buf_chain *chain;
522 chain = bufs->cur->next;
524 return chain && nghttp2_buf_len(&chain->buf);