ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) { size_t len; nghttp2_buf_chain *chain; nghttp2_buf *buf; uint8_t *res; nghttp2_buf resbuf; len = 0; for (chain = bufs->head; chain; chain = chain->next) { len += nghttp2_buf_len(&chain->buf); } if (len == 0) { res = NULL; return 0; } res = nghttp2_mem_malloc(bufs->mem, len); if (res == NULL) { return NGHTTP2_ERR_NOMEM; } nghttp2_buf_wrap_init(&resbuf, res, len); for (chain = bufs->head; chain; chain = chain->next) { buf = &chain->buf; resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf)); } *out = res; return (ssize_t)len; }
void test_nghttp2_bufs_add(void) { int rv; nghttp2_bufs bufs; uint8_t data[2048]; rv = nghttp2_bufs_init(&bufs, 1000, 3); CU_ASSERT(0 == rv); CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.last); rv = nghttp2_bufs_add(&bufs, data, 493); CU_ASSERT(0 == rv); CU_ASSERT(493 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(493 == nghttp2_bufs_len(&bufs)); CU_ASSERT(507 == nghttp2_bufs_cur_avail(&bufs)); rv = nghttp2_bufs_add(&bufs, data, 507); CU_ASSERT(0 == rv); CU_ASSERT(1000 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1000 == nghttp2_bufs_len(&bufs)); CU_ASSERT(bufs.cur == bufs.head); rv = nghttp2_bufs_add(&bufs, data, 1); CU_ASSERT(0 == rv); CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1001 == nghttp2_bufs_len(&bufs)); CU_ASSERT(bufs.cur == bufs.head->next); nghttp2_bufs_free(&bufs); }
void test_nghttp2_bufs_orb(void) { int rv; nghttp2_bufs bufs; rv = nghttp2_bufs_init(&bufs, 1000, 3); CU_ASSERT(0 == rv); *(bufs.cur->buf.last) = 0; rv = nghttp2_bufs_orb_hold(&bufs, 15); CU_ASSERT(0 == rv); CU_ASSERT(0 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(0 == nghttp2_bufs_len(&bufs)); CU_ASSERT(15 == *(bufs.cur->buf.last)); rv = nghttp2_bufs_orb(&bufs, 240); CU_ASSERT(0 == rv); CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1 == nghttp2_bufs_len(&bufs)); CU_ASSERT(255 == *(bufs.cur->buf.last - 1)); *(bufs.cur->buf.last) = 0; nghttp2_bufs_fast_orb_hold(&bufs, 240); CU_ASSERT(240 == *(bufs.cur->buf.last)); nghttp2_bufs_fast_orb(&bufs, 15); CU_ASSERT(255 == *(bufs.cur->buf.last - 1)); nghttp2_bufs_free(&bufs); }
int nghttp2_bufs_next_present(nghttp2_bufs *bufs) { nghttp2_buf_chain *chain; chain = bufs->cur->next; return chain && nghttp2_buf_len(&chain->buf); }
ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) { size_t len; nghttp2_buf_chain *chain; nghttp2_buf *buf; uint8_t *res; nghttp2_buf resbuf; len = 0; for(chain = bufs->head; chain; chain = chain->next) { len += nghttp2_buf_len(&chain->buf); } if(!len) { res = NULL; } else { res = (uint8_t *)malloc(len); if(res == NULL) { return NGHTTP2_ERR_NOMEM; } } nghttp2_buf_wrap_init(&resbuf, res, len); for(chain = bufs->head; chain; chain = chain->next) { buf = &chain->buf; if(resbuf.last) { resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf)); } nghttp2_buf_reset(buf); nghttp2_buf_shift_right(&chain->buf, bufs->offset); } bufs->cur = bufs->head; *out = res; return (ssize_t)len; }
size_t nghttp2_bufs_len(nghttp2_bufs *bufs) { nghttp2_buf_chain *ci; size_t len; len = 0; for (ci = bufs->head; ci; ci = ci->next) { len += nghttp2_buf_len(&ci->buf); } return len; }
void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs) { nghttp2_buf_chain *ci; for (ci = bufs->cur; ci; ci = ci->next) { if (nghttp2_buf_len(&ci->buf) == 0) { return; } else { bufs->cur = ci; } } }
static void data_feed_init(data_feed *df, nghttp2_bufs *bufs) { nghttp2_buf *buf; size_t data_length; buf = &bufs->head->buf; data_length = nghttp2_buf_len(buf); assert(data_length <= sizeof(df->data)); memcpy(df->data, buf->pos, data_length); df->datamark = df->data; df->datalimit = df->data + data_length; }
static void run_nghttp2_frame_pack_settings(void) { nghttp2_frame frame, oframe; nghttp2_bufs bufs; nghttp2_buf *buf; nghttp2_settings_entry iv[2], *iv_copy; int rv; rv = frame_pack_bufs_init(&bufs); if (rv != 0) { return; } iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE; iv[0].value = 4096; iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; iv[1].value = 100; iv_copy = nghttp2_frame_iv_copy(iv, 2, nghttp2_mem_fm()); if (iv_copy == NULL) { goto iv_copy_fail; } nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE, iv_copy, 2); rv = nghttp2_frame_pack_settings(&bufs, &frame.settings); if (rv != 0) { goto fail; } buf = &bufs.head->buf; rv = nghttp2_frame_unpack_settings_payload2( &oframe.settings.iv, &oframe.settings.niv, buf->pos + NGHTTP2_FRAME_HDLEN, nghttp2_buf_len(buf) - NGHTTP2_FRAME_HDLEN, nghttp2_mem_fm()); if (rv != 0) { goto fail; } nghttp2_frame_settings_free(&oframe.settings, nghttp2_mem_fm()); fail: nghttp2_frame_settings_free(&frame.settings, nghttp2_mem_fm()); iv_copy_fail: nghttp2_bufs_free(&bufs); }
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; }
void test_nghttp2_bufs_addb(void) { int rv; nghttp2_bufs bufs; ssize_t i; rv = nghttp2_bufs_init(&bufs, 1000, 3); CU_ASSERT(0 == rv); rv = nghttp2_bufs_addb(&bufs, 14); CU_ASSERT(0 == rv); CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1 == nghttp2_bufs_len(&bufs)); CU_ASSERT(14 == *bufs.cur->buf.pos); for(i = 0; i < 999; ++i) { rv = nghttp2_bufs_addb(&bufs, 254); CU_ASSERT(0 == rv); CU_ASSERT(i + 2 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(i + 2 == nghttp2_bufs_len(&bufs)); CU_ASSERT(254 == *(bufs.cur->buf.last - 1)); CU_ASSERT(bufs.cur == bufs.head); } rv = nghttp2_bufs_addb(&bufs, 253); CU_ASSERT(0 == rv); CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1001 == nghttp2_bufs_len(&bufs)); CU_ASSERT(253 == *(bufs.cur->buf.last - 1)); CU_ASSERT(bufs.cur == bufs.head->next); rv = nghttp2_bufs_addb_hold(&bufs, 15); CU_ASSERT(0 == rv); CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1001 == nghttp2_bufs_len(&bufs)); CU_ASSERT(15 == *(bufs.cur->buf.last)); /* test fast version */ nghttp2_bufs_fast_addb(&bufs, 240); CU_ASSERT(2 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1002 == nghttp2_bufs_len(&bufs)); CU_ASSERT(240 == *(bufs.cur->buf.last - 1)); nghttp2_bufs_fast_addb_hold(&bufs, 113); CU_ASSERT(2 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1002 == nghttp2_bufs_len(&bufs)); CU_ASSERT(113 == *(bufs.cur->buf.last)); /* addb_hold when last == end */ bufs.cur->buf.last = bufs.cur->buf.end; rv = nghttp2_bufs_addb_hold(&bufs, 19); CU_ASSERT(0 == rv); CU_ASSERT(0 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(2000 == nghttp2_bufs_len(&bufs)); CU_ASSERT(19 == *(bufs.cur->buf.last)); nghttp2_bufs_free(&bufs); }