2014-03-10 16:47:38 +00:00
|
|
|
/*
|
2014-03-30 10:09:21 +00:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2014-03-10 16:47:38 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "nghttp2_buf.h"
|
|
|
|
|
2014-03-13 15:24:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-03-10 16:47:38 +00:00
|
|
|
#include "nghttp2_helper.h"
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void nghttp2_buf_init(nghttp2_buf *buf) {
|
2014-03-10 16:47:38 +00:00
|
|
|
buf->begin = NULL;
|
|
|
|
buf->end = NULL;
|
|
|
|
buf->pos = NULL;
|
|
|
|
buf->last = NULL;
|
|
|
|
buf->mark = NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem) {
|
2014-03-10 16:47:38 +00:00
|
|
|
nghttp2_buf_init(buf);
|
2014-12-07 14:07:13 +00:00
|
|
|
return nghttp2_buf_reserve(buf, initial, mem);
|
2014-03-10 16:47:38 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem) {
|
2014-11-27 14:39:04 +00:00
|
|
|
if (buf == NULL) {
|
2014-08-23 02:16:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
nghttp2_mem_free(mem, buf->begin);
|
2014-08-23 02:16:49 +00:00
|
|
|
buf->begin = NULL;
|
2014-03-10 16:47:38 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem) {
|
2014-03-10 16:47:38 +00:00
|
|
|
uint8_t *ptr;
|
|
|
|
size_t cap;
|
|
|
|
|
|
|
|
cap = nghttp2_buf_cap(buf);
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
if (cap >= new_cap) {
|
2014-03-10 16:47:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_cap = nghttp2_max(new_cap, cap * 2);
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
ptr = nghttp2_mem_realloc(mem, buf->begin, new_cap);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (ptr == NULL) {
|
2014-03-10 16:47:38 +00:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->pos = ptr + (buf->pos - buf->begin);
|
|
|
|
buf->last = ptr + (buf->last - buf->begin);
|
|
|
|
buf->mark = ptr + (buf->mark - buf->begin);
|
|
|
|
buf->begin = ptr;
|
|
|
|
buf->end = ptr + new_cap;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void nghttp2_buf_reset(nghttp2_buf *buf) {
|
2014-03-10 16:47:38 +00:00
|
|
|
buf->pos = buf->last = buf->mark = buf->begin;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len) {
|
2014-03-10 16:47:38 +00:00
|
|
|
buf->begin = buf->pos = buf->last = buf->mark = begin;
|
|
|
|
buf->end = begin + len;
|
|
|
|
}
|
2014-03-11 12:18:28 +00:00
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
static int buf_chain_new(nghttp2_buf_chain **chain, size_t chunk_length,
|
|
|
|
nghttp2_mem *mem) {
|
2014-03-11 12:18:28 +00:00
|
|
|
int rv;
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
*chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
|
2014-11-27 14:39:04 +00:00
|
|
|
if (*chain == NULL) {
|
2014-03-11 12:18:28 +00:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*chain)->next = NULL;
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length, mem);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-12-07 14:07:13 +00:00
|
|
|
nghttp2_mem_free(mem, *chain);
|
2014-03-11 12:18:28 +00:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
static void buf_chain_del(nghttp2_buf_chain *chain, nghttp2_mem *mem) {
|
|
|
|
nghttp2_buf_free(&chain->buf, mem);
|
|
|
|
nghttp2_mem_free(mem, chain);
|
2014-03-11 12:18:28 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
|
|
|
|
nghttp2_mem *mem) {
|
|
|
|
return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0, mem);
|
2014-03-13 13:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
|
2014-12-07 14:07:13 +00:00
|
|
|
size_t max_chunk, size_t offset, nghttp2_mem *mem) {
|
|
|
|
return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset,
|
|
|
|
mem);
|
2014-03-16 06:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
|
2014-12-07 14:07:13 +00:00
|
|
|
size_t max_chunk, size_t chunk_keep, size_t offset,
|
|
|
|
nghttp2_mem *mem) {
|
2014-03-11 12:18:28 +00:00
|
|
|
int rv;
|
|
|
|
nghttp2_buf_chain *chain;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
if (chunk_keep == 0 || max_chunk < chunk_keep || chunk_length < offset) {
|
2014-03-11 12:18:28 +00:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
rv = buf_chain_new(&chain, chunk_length, mem);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-03-11 12:18:28 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
bufs->mem = mem;
|
2014-03-13 13:11:02 +00:00
|
|
|
bufs->offset = offset;
|
|
|
|
|
2014-03-11 12:18:28 +00:00
|
|
|
bufs->head = chain;
|
|
|
|
bufs->cur = bufs->head;
|
|
|
|
|
2014-03-13 13:11:02 +00:00
|
|
|
nghttp2_buf_shift_right(&bufs->cur->buf, offset);
|
|
|
|
|
2014-03-11 12:18:28 +00:00
|
|
|
bufs->chunk_length = chunk_length;
|
2014-03-16 06:58:21 +00:00
|
|
|
bufs->chunk_used = 1;
|
|
|
|
bufs->max_chunk = max_chunk;
|
|
|
|
bufs->chunk_keep = chunk_keep;
|
2014-03-11 12:18:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) {
|
2014-08-24 06:34:55 +00:00
|
|
|
int rv;
|
|
|
|
nghttp2_buf_chain *chain;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
if (chunk_length < bufs->offset) {
|
2014-08-24 06:34:55 +00:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
rv = buf_chain_new(&chain, chunk_length, bufs->mem);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-08-24 06:34:55 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_bufs_free(bufs);
|
|
|
|
|
|
|
|
bufs->head = chain;
|
|
|
|
bufs->cur = bufs->head;
|
|
|
|
|
|
|
|
nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
|
|
|
|
|
|
|
|
bufs->chunk_length = chunk_length;
|
|
|
|
bufs->chunk_used = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void nghttp2_bufs_free(nghttp2_bufs *bufs) {
|
2014-03-11 12:18:28 +00:00
|
|
|
nghttp2_buf_chain *chain, *next_chain;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
if (bufs == NULL) {
|
2014-08-23 02:16:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
for (chain = bufs->head; chain;) {
|
2014-03-11 12:18:28 +00:00
|
|
|
next_chain = chain->next;
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
buf_chain_del(chain, bufs->mem);
|
2014-03-11 12:18:28 +00:00
|
|
|
|
|
|
|
chain = next_chain;
|
|
|
|
}
|
2014-08-23 02:16:49 +00:00
|
|
|
|
|
|
|
bufs->head = NULL;
|
2014-03-11 12:18:28 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
|
|
|
|
nghttp2_mem *mem) {
|
2014-05-13 14:42:55 +00:00
|
|
|
nghttp2_buf_chain *chain;
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
|
2014-11-27 14:39:04 +00:00
|
|
|
if (chain == NULL) {
|
2014-05-13 14:42:55 +00:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
chain->next = NULL;
|
|
|
|
|
|
|
|
nghttp2_buf_wrap_init(&chain->buf, begin, len);
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
bufs->mem = mem;
|
2014-05-13 14:42:55 +00:00
|
|
|
bufs->offset = 0;
|
|
|
|
|
|
|
|
bufs->head = chain;
|
|
|
|
bufs->cur = bufs->head;
|
|
|
|
|
|
|
|
bufs->chunk_length = len;
|
|
|
|
bufs->chunk_used = 1;
|
|
|
|
bufs->max_chunk = 1;
|
|
|
|
bufs->chunk_keep = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) {
|
|
|
|
if (bufs == NULL) {
|
2014-08-23 02:16:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
nghttp2_mem_free(bufs->mem, bufs->head);
|
2014-08-23 02:16:49 +00:00
|
|
|
bufs->head = NULL;
|
2014-05-13 14:42:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs) {
|
2014-03-13 13:11:02 +00:00
|
|
|
nghttp2_buf_chain *ci;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
for (ci = bufs->cur; ci; ci = ci->next) {
|
|
|
|
if (nghttp2_buf_len(&ci->buf) == 0) {
|
2014-03-13 13:11:02 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
bufs->cur = ci;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 05:41:53 +00:00
|
|
|
size_t nghttp2_bufs_len(nghttp2_bufs *bufs) {
|
2014-03-13 13:11:02 +00:00
|
|
|
nghttp2_buf_chain *ci;
|
2015-09-23 05:41:53 +00:00
|
|
|
size_t len;
|
2014-03-13 13:11:02 +00:00
|
|
|
|
|
|
|
len = 0;
|
2014-11-27 14:39:04 +00:00
|
|
|
for (ci = bufs->head; ci; ci = ci->next) {
|
2014-03-13 13:11:02 +00:00
|
|
|
len += nghttp2_buf_len(&ci->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-09-23 05:41:53 +00:00
|
|
|
static size_t bufs_avail(nghttp2_bufs *bufs) {
|
|
|
|
return nghttp2_buf_avail(&bufs->cur->buf) +
|
|
|
|
(bufs->chunk_length - bufs->offset) *
|
|
|
|
(bufs->max_chunk - bufs->chunk_used);
|
2014-03-11 12:18:28 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
static int bufs_alloc_chain(nghttp2_bufs *bufs) {
|
2014-03-11 12:18:28 +00:00
|
|
|
int rv;
|
|
|
|
nghttp2_buf_chain *chain;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
if (bufs->cur->next) {
|
2014-03-11 12:18:28 +00:00
|
|
|
bufs->cur = bufs->cur->next;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
if (bufs->max_chunk == bufs->chunk_used) {
|
2014-03-11 12:18:28 +00:00
|
|
|
return NGHTTP2_ERR_BUFFER_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
rv = buf_chain_new(&chain, bufs->chunk_length, bufs->mem);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-03-11 12:18:28 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-03-13 15:24:34 +00:00
|
|
|
DEBUGF(fprintf(stderr,
|
2014-03-19 14:24:46 +00:00
|
|
|
"new buffer %zu bytes allocated for bufs %p, used %zu\n",
|
|
|
|
bufs->chunk_length, bufs, bufs->chunk_used));
|
2014-03-13 15:24:34 +00:00
|
|
|
|
2014-03-16 06:58:21 +00:00
|
|
|
++bufs->chunk_used;
|
2014-03-11 12:18:28 +00:00
|
|
|
|
|
|
|
bufs->cur->next = chain;
|
|
|
|
bufs->cur = chain;
|
|
|
|
|
2014-03-13 13:11:02 +00:00
|
|
|
nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
|
|
|
|
|
2014-03-11 12:18:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len) {
|
2014-03-11 12:18:28 +00:00
|
|
|
int rv;
|
|
|
|
size_t nwrite;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
const uint8_t *p;
|
|
|
|
|
2015-09-23 05:41:53 +00:00
|
|
|
if (bufs_avail(bufs) < len) {
|
2014-03-11 12:18:28 +00:00
|
|
|
return NGHTTP2_ERR_BUFFER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = data;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
while (len) {
|
2014-03-11 12:18:28 +00:00
|
|
|
buf = &bufs->cur->buf;
|
|
|
|
|
2015-09-23 05:41:53 +00:00
|
|
|
nwrite = nghttp2_min(nghttp2_buf_avail(buf), len);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (nwrite == 0) {
|
2014-05-08 14:54:07 +00:00
|
|
|
rv = bufs_alloc_chain(bufs);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-03-11 12:18:28 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-13 15:24:34 +00:00
|
|
|
|
2014-03-11 12:18:28 +00:00
|
|
|
buf->last = nghttp2_cpymem(buf->last, p, nwrite);
|
2015-05-14 14:45:17 +00:00
|
|
|
p += nwrite;
|
2014-03-11 12:18:28 +00:00
|
|
|
len -= nwrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
static int bufs_ensure_addb(nghttp2_bufs *bufs) {
|
2014-03-11 12:18:28 +00:00
|
|
|
int rv;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
|
|
|
|
buf = &bufs->cur->buf;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
if (nghttp2_buf_avail(buf) > 0) {
|
2014-03-11 12:18:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-08 14:54:07 +00:00
|
|
|
rv = bufs_alloc_chain(bufs);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-03-11 12:18:28 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-03-13 13:11:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b) {
|
2014-03-13 13:11:02 +00:00
|
|
|
int rv;
|
|
|
|
|
2014-05-08 14:54:07 +00:00
|
|
|
rv = bufs_ensure_addb(bufs);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-03-13 13:11:02 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bufs->cur->buf.last++ = b;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b) {
|
2014-03-13 13:11:02 +00:00
|
|
|
int rv;
|
2014-03-11 12:18:28 +00:00
|
|
|
|
2014-05-08 14:54:07 +00:00
|
|
|
rv = bufs_ensure_addb(bufs);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-03-13 13:11:02 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bufs->cur->buf.last = b;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b) {
|
2014-03-13 13:11:02 +00:00
|
|
|
int rv;
|
|
|
|
|
2014-05-08 14:54:07 +00:00
|
|
|
rv = bufs_ensure_addb(bufs);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-03-13 13:11:02 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bufs->cur->buf.last++ |= b;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b) {
|
2014-03-13 13:11:02 +00:00
|
|
|
int rv;
|
|
|
|
|
2014-05-08 14:54:07 +00:00
|
|
|
rv = bufs_ensure_addb(bufs);
|
2014-11-27 14:39:04 +00:00
|
|
|
if (rv != 0) {
|
2014-03-13 13:11:02 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bufs->cur->buf.last |= b;
|
2014-03-11 12:18:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) {
|
2014-03-11 12:18:28 +00:00
|
|
|
size_t len;
|
|
|
|
nghttp2_buf_chain *chain;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
uint8_t *res;
|
|
|
|
nghttp2_buf resbuf;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
for (chain = bufs->head; chain; chain = chain->next) {
|
2014-03-11 12:18:28 +00:00
|
|
|
len += nghttp2_buf_len(&chain->buf);
|
|
|
|
}
|
|
|
|
|
2015-03-24 12:54:05 +00:00
|
|
|
if (len == 0) {
|
2014-04-30 13:09:02 +00:00
|
|
|
res = NULL;
|
2015-03-24 12:54:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-30 13:09:02 +00:00
|
|
|
|
2015-03-24 12:54:05 +00:00
|
|
|
res = nghttp2_mem_malloc(bufs->mem, len);
|
|
|
|
if (res == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2014-03-11 12:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_buf_wrap_init(&resbuf, res, len);
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
for (chain = bufs->head; chain; chain = chain->next) {
|
2014-03-11 12:18:28 +00:00
|
|
|
buf = &chain->buf;
|
2015-03-24 12:54:05 +00:00
|
|
|
resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
|
2014-03-11 12:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*out = res;
|
|
|
|
|
2014-06-16 17:17:49 +00:00
|
|
|
return (ssize_t)len;
|
2014-03-11 12:18:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 09:43:24 +00:00
|
|
|
size_t nghttp2_bufs_remove_copy(nghttp2_bufs *bufs, uint8_t *out) {
|
|
|
|
size_t len;
|
|
|
|
nghttp2_buf_chain *chain;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
nghttp2_buf resbuf;
|
|
|
|
|
|
|
|
len = nghttp2_bufs_len(bufs);
|
|
|
|
|
|
|
|
nghttp2_buf_wrap_init(&resbuf, out, len);
|
|
|
|
|
|
|
|
for (chain = bufs->head; chain; chain = chain->next) {
|
|
|
|
buf = &chain->buf;
|
|
|
|
resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
void nghttp2_bufs_reset(nghttp2_bufs *bufs) {
|
2014-03-16 06:58:21 +00:00
|
|
|
nghttp2_buf_chain *chain, *ci;
|
|
|
|
size_t k;
|
2014-03-11 12:18:28 +00:00
|
|
|
|
2014-03-16 06:58:21 +00:00
|
|
|
k = bufs->chunk_keep;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
for (ci = bufs->head; ci; ci = ci->next) {
|
2014-03-16 06:58:21 +00:00
|
|
|
nghttp2_buf_reset(&ci->buf);
|
|
|
|
nghttp2_buf_shift_right(&ci->buf, bufs->offset);
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
if (--k == 0) {
|
2014-03-16 06:58:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
if (ci) {
|
2014-03-16 06:58:21 +00:00
|
|
|
chain = ci->next;
|
|
|
|
ci->next = NULL;
|
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
for (ci = chain; ci;) {
|
2014-03-16 06:58:21 +00:00
|
|
|
chain = ci->next;
|
|
|
|
|
2014-12-07 14:07:13 +00:00
|
|
|
buf_chain_del(ci, bufs->mem);
|
2014-03-16 06:58:21 +00:00
|
|
|
|
|
|
|
ci = chain;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufs->chunk_used = bufs->chunk_keep;
|
2014-03-11 12:18:28 +00:00
|
|
|
}
|
2014-03-13 13:11:02 +00:00
|
|
|
|
|
|
|
bufs->cur = bufs->head;
|
2014-03-11 12:18:28 +00:00
|
|
|
}
|
2014-03-13 13:11:02 +00:00
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
int nghttp2_bufs_advance(nghttp2_bufs *bufs) { return bufs_alloc_chain(bufs); }
|
2014-03-13 13:11:02 +00:00
|
|
|
|
2014-11-27 14:39:04 +00:00
|
|
|
int nghttp2_bufs_next_present(nghttp2_bufs *bufs) {
|
2014-03-13 13:11:02 +00:00
|
|
|
nghttp2_buf_chain *chain;
|
|
|
|
|
|
|
|
chain = bufs->cur->next;
|
|
|
|
|
|
|
|
return chain && nghttp2_buf_len(&chain->buf);
|
|
|
|
}
|