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"
31 void nghttp2_buf_init(nghttp2_buf *buf) {
39 int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem) {
40 nghttp2_buf_init(buf);
41 return nghttp2_buf_reserve(buf, initial, mem);
44 void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem) {
49 nghttp2_mem_free(mem, buf->begin);
53 int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem) {
57 cap = nghttp2_buf_cap(buf);
63 new_cap = nghttp2_max(new_cap, cap * 2);
65 ptr = nghttp2_mem_realloc(mem, buf->begin, new_cap);
67 return NGHTTP2_ERR_NOMEM;
70 buf->pos = ptr + (buf->pos - buf->begin);
71 buf->last = ptr + (buf->last - buf->begin);
72 buf->mark = ptr + (buf->mark - buf->begin);
74 buf->end = ptr + new_cap;
79 void nghttp2_buf_reset(nghttp2_buf *buf) {
80 buf->pos = buf->last = buf->mark = buf->begin;
83 void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len) {
84 buf->begin = buf->pos = buf->last = buf->mark = begin;
85 buf->end = begin + len;
88 static int buf_chain_new(nghttp2_buf_chain **chain, size_t chunk_length,
92 *chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
94 return NGHTTP2_ERR_NOMEM;
97 (*chain)->next = NULL;
99 rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length, mem);
101 nghttp2_mem_free(mem, *chain);
102 return NGHTTP2_ERR_NOMEM;
108 static void buf_chain_del(nghttp2_buf_chain *chain, nghttp2_mem *mem) {
109 nghttp2_buf_free(&chain->buf, mem);
110 nghttp2_mem_free(mem, chain);
113 int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
115 return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0, mem);
118 int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
119 size_t max_chunk, size_t offset, nghttp2_mem *mem) {
120 return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset,
124 int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
125 size_t max_chunk, size_t chunk_keep, size_t offset,
128 nghttp2_buf_chain *chain;
130 if (chunk_keep == 0 || max_chunk < chunk_keep || chunk_length < offset) {
131 return NGHTTP2_ERR_INVALID_ARGUMENT;
134 rv = buf_chain_new(&chain, chunk_length, mem);
140 bufs->offset = offset;
143 bufs->cur = bufs->head;
145 nghttp2_buf_shift_right(&bufs->cur->buf, offset);
147 bufs->chunk_length = chunk_length;
148 bufs->chunk_used = 1;
149 bufs->max_chunk = max_chunk;
150 bufs->chunk_keep = chunk_keep;
155 int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) {
157 nghttp2_buf_chain *chain;
159 if (chunk_length < bufs->offset) {
160 return NGHTTP2_ERR_INVALID_ARGUMENT;
163 rv = buf_chain_new(&chain, chunk_length, bufs->mem);
168 nghttp2_bufs_free(bufs);
171 bufs->cur = bufs->head;
173 nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
175 bufs->chunk_length = chunk_length;
176 bufs->chunk_used = 1;
181 void nghttp2_bufs_free(nghttp2_bufs *bufs) {
182 nghttp2_buf_chain *chain, *next_chain;
188 for (chain = bufs->head; chain;) {
189 next_chain = chain->next;
191 buf_chain_del(chain, bufs->mem);
199 int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
201 nghttp2_buf_chain *chain;
203 chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
205 return NGHTTP2_ERR_NOMEM;
210 nghttp2_buf_wrap_init(&chain->buf, begin, len);
216 bufs->cur = bufs->head;
218 bufs->chunk_length = len;
219 bufs->chunk_used = 1;
221 bufs->chunk_keep = 1;
226 void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) {
231 nghttp2_mem_free(bufs->mem, bufs->head);
235 void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs) {
236 nghttp2_buf_chain *ci;
238 for (ci = bufs->cur; ci; ci = ci->next) {
239 if (nghttp2_buf_len(&ci->buf) == 0) {
247 ssize_t nghttp2_bufs_len(nghttp2_bufs *bufs) {
248 nghttp2_buf_chain *ci;
252 for (ci = bufs->head; ci; ci = ci->next) {
253 len += nghttp2_buf_len(&ci->buf);
259 static ssize_t bufs_avail(nghttp2_bufs *bufs) {
260 return (ssize_t)(nghttp2_buf_avail(&bufs->cur->buf) +
261 (bufs->chunk_length - bufs->offset) *
262 (bufs->max_chunk - bufs->chunk_used));
265 static int bufs_alloc_chain(nghttp2_bufs *bufs) {
267 nghttp2_buf_chain *chain;
269 if (bufs->cur->next) {
270 bufs->cur = bufs->cur->next;
275 if (bufs->max_chunk == bufs->chunk_used) {
276 return NGHTTP2_ERR_BUFFER_ERROR;
279 rv = buf_chain_new(&chain, bufs->chunk_length, bufs->mem);
284 DEBUGF(fprintf(stderr,
285 "new buffer %zu bytes allocated for bufs %p, used %zu\n",
286 bufs->chunk_length, bufs, bufs->chunk_used));
290 bufs->cur->next = chain;
293 nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
298 int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len) {
304 if (bufs_avail(bufs) < (ssize_t)len) {
305 return NGHTTP2_ERR_BUFFER_ERROR;
311 buf = &bufs->cur->buf;
313 nwrite = nghttp2_min((size_t)nghttp2_buf_avail(buf), len);
315 rv = bufs_alloc_chain(bufs);
322 buf->last = nghttp2_cpymem(buf->last, p, nwrite);
330 static int bufs_ensure_addb(nghttp2_bufs *bufs) {
334 buf = &bufs->cur->buf;
336 if (nghttp2_buf_avail(buf) > 0) {
340 rv = bufs_alloc_chain(bufs);
348 int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b) {
351 rv = bufs_ensure_addb(bufs);
356 *bufs->cur->buf.last++ = b;
361 int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b) {
364 rv = bufs_ensure_addb(bufs);
369 *bufs->cur->buf.last = b;
374 int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b) {
377 rv = bufs_ensure_addb(bufs);
382 *bufs->cur->buf.last++ |= b;
387 int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b) {
390 rv = bufs_ensure_addb(bufs);
395 *bufs->cur->buf.last |= b;
400 ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) {
402 nghttp2_buf_chain *chain;
409 for (chain = bufs->head; chain; chain = chain->next) {
410 len += nghttp2_buf_len(&chain->buf);
416 res = nghttp2_mem_malloc(bufs->mem, len);
419 return NGHTTP2_ERR_NOMEM;
423 nghttp2_buf_wrap_init(&resbuf, res, len);
425 for (chain = bufs->head; chain; chain = chain->next) {
429 resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
432 nghttp2_buf_reset(buf);
433 nghttp2_buf_shift_right(&chain->buf, bufs->offset);
436 bufs->cur = bufs->head;
443 void nghttp2_bufs_reset(nghttp2_bufs *bufs) {
444 nghttp2_buf_chain *chain, *ci;
447 k = bufs->chunk_keep;
449 for (ci = bufs->head; ci; ci = ci->next) {
450 nghttp2_buf_reset(&ci->buf);
451 nghttp2_buf_shift_right(&ci->buf, bufs->offset);
462 for (ci = chain; ci;) {
465 buf_chain_del(ci, bufs->mem);
470 bufs->chunk_used = bufs->chunk_keep;
473 bufs->cur = bufs->head;
476 int nghttp2_bufs_advance(nghttp2_bufs *bufs) { return bufs_alloc_chain(bufs); }
478 int nghttp2_bufs_next_present(nghttp2_bufs *bufs) {
479 nghttp2_buf_chain *chain;
481 chain = bufs->cur->next;
483 return chain && nghttp2_buf_len(&chain->buf);