Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/recover #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions inc/hpack.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
# include <unistd.h>
#endif

/* hpack_config */

enum hpack_config_e {
#define HPC(f, v, l) \
HPACK_CFG_##f = v,
#include "tbl/hpack_tbl.h"
#undef HPC
};

/* hpack_result */

enum hpack_result_e {
Expand All @@ -59,8 +68,8 @@ struct hpack_alloc {

extern const struct hpack_alloc *hpack_default_alloc;

struct hpack * hpack_decoder(size_t, ssize_t, const struct hpack_alloc *);
struct hpack * hpack_encoder(size_t, ssize_t, const struct hpack_alloc *);
struct hpack * hpack_decoder(size_t, ssize_t, const struct hpack_alloc *, uint32_t);
struct hpack * hpack_encoder(size_t, ssize_t, const struct hpack_alloc *, uint32_t);
void hpack_free(struct hpack **);

enum hpack_result_e hpack_resize(struct hpack **, size_t);
Expand All @@ -69,6 +78,9 @@ enum hpack_result_e hpack_trim(struct hpack **);

/* hpack_error */

extern const char *hpack_unknown_name;
extern const char *hpack_unknown_value;

typedef void hpack_dump_f(void *, const char *, ...);

const char * hpack_strerror(enum hpack_result_e);
Expand Down
4 changes: 4 additions & 0 deletions inc/hpack_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#define HPACK_LIMIT(hp) \
(((hp)->sz.lim >= 0 ? (size_t)(hp)->sz.lim : (hp)->sz.max))

#define HPC_DEGRADED() \
(ctx->hp->flags & HPACK_CFG_DEGRADED)

#define CALL(func, ...) \
do { \
if ((func)(__VA_ARGS__) != 0) \
Expand Down Expand Up @@ -197,6 +200,7 @@ struct hpack {
#define ENCODER_MAGIC 0x8ab1fb4c
#define DECODER_MAGIC 0xab0e3218
#define DEFUNCT_MAGIC 0xdffadae9
uint32_t flags;
struct hpack_alloc alloc;
struct hpack_size sz;
struct hpack_state state;
Expand Down
8 changes: 8 additions & 0 deletions inc/tbl/hpack_tbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,16 @@ HPF(AUT_IDX, 0x80,
"\tname and value) is found, either ``TYP_DYN`` or ``TYP_LIT`` are\n"
"\tturned into ``TYP_IDX``. If a match is found, *idx* or *nam_idx*\n"
"\tis set to non-zero, zero otherwise.\n\n")

#endif /* HPF */

#ifdef HPC
/* configuration flags */
HPC(DEGRADED, 0x01,
"\tTODO: write documentation.\n\n")

#endif /* HPC */

#ifdef HPP
HPP(STR, 7, 0x00) /* Section 5.2 */
HPP(HUF, 7, 0x80) /* Section 5.2 */
Expand Down
2 changes: 2 additions & 0 deletions lib/cashpack.map
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ CASHPACK_0.4 {

# variables
hpack_default_alloc;
hpack_unknown_name;
hpack_unknown_value;

local:
*;
Expand Down
15 changes: 9 additions & 6 deletions lib/hpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const struct hpack_alloc *hpack_default_alloc = &hpack_libc_alloc;

static struct hpack *
hpack_new(uint32_t magic, size_t mem, size_t max,
const struct hpack_alloc *ha)
const struct hpack_alloc *ha, uint32_t flags)
{
struct hpack *hp;

Expand All @@ -104,6 +104,7 @@ hpack_new(uint32_t magic, size_t mem, size_t max,

(void)memset(hp, 0, sizeof *hp);
hp->magic = magic;
hp->flags = flags;
hp->ctx.hp = hp;
(void)memcpy(&hp->alloc, ha, sizeof *ha);
hp->sz.mem = mem;
Expand All @@ -116,14 +117,14 @@ hpack_new(uint32_t magic, size_t mem, size_t max,
}

struct hpack *
hpack_encoder(size_t max, ssize_t lim, const struct hpack_alloc *ha)
hpack_encoder(size_t max, ssize_t lim, const struct hpack_alloc *ha, uint32_t flags)
{
enum hpack_result_e res;
struct hpack *hp, *tmp;
size_t mem;

mem = lim >= 0 ? (size_t)lim : max;
hp = hpack_new(ENCODER_MAGIC, mem, max, ha);
hp = hpack_new(ENCODER_MAGIC, mem, max, ha, flags);
if (lim >= 0 && hp != NULL) {
tmp = hp;
res = hpack_limit(&tmp, (size_t)lim);
Expand All @@ -136,14 +137,14 @@ hpack_encoder(size_t max, ssize_t lim, const struct hpack_alloc *ha)
}

struct hpack *
hpack_decoder(size_t max, ssize_t rsz, const struct hpack_alloc *ha)
hpack_decoder(size_t max, ssize_t rsz, const struct hpack_alloc *ha, uint32_t flags)
{
size_t mem;

mem = rsz >= 0 ? (size_t)rsz : max;
if (mem < max)
mem = max;
return (hpack_new(DECODER_MAGIC, mem, max, ha));
return (hpack_new(DECODER_MAGIC, mem, max, ha, flags));
}

static enum hpack_result_e
Expand Down Expand Up @@ -624,7 +625,9 @@ hpack_decode_field(HPACK_CTX)
else
CALL(HPT_decode_name, ctx);
assert(ctx->buf > ctx->fld.nam);
ctx->fld.nam_sz = (size_t)(ctx->buf - ctx->fld.nam - 1);
if(ctx->fld.nam != hpack_unknown_name) {
ctx->fld.nam_sz = (size_t)(ctx->buf - ctx->fld.nam - 1);
}
CALL(HPV_token, ctx, ctx->fld.nam, ctx->fld.nam_sz);
ctx->fld.val = ctx->buf;
ctx->hp->state.stp = HPACK_STP_VAL_LEN;
Expand Down
33 changes: 29 additions & 4 deletions lib/hpack_tbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ static const struct hpt_field hpt_static[] = {
* Tables lookups
*/

const char *hpack_unknown_name = "unknown_name";
const char *hpack_unknown_value = "unknown_value";

static size_t HPT_index_silent(HPACK_CTX);

static struct hpt_entry *
hpt_dynamic(struct hpack *hp, size_t idx)
{
Expand Down Expand Up @@ -99,6 +104,18 @@ HPT_field(HPACK_CTX, size_t idx, struct hpt_field *hf)
}

idx -= HPACK_STATIC;
if(HPC_DEGRADED() && idx > ctx->hp->cnt) {
ctx->fld.nam = hpack_unknown_name;
ctx->fld.val = hpack_unknown_value;
ctx->fld.nam_sz = strlen(ctx->fld.nam);
ctx->fld.val_sz = strlen(ctx->fld.val);
while(idx > ctx->hp->cnt) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this loop, but don't know how to get rid of it.
Comments welcome!

HPT_index_silent(ctx);
}
while(idx >= ctx->hp->cnt) {
HPT_index(ctx);
}
}
EXPECT(ctx, IDX, idx <= ctx->hp->cnt);

he = hpt_dynamic(ctx->hp, idx);
Expand Down Expand Up @@ -366,8 +383,8 @@ hpt_move_evicted(HPACK_CTX, const char *nam, size_t nam_sz, size_t len)
(void)memmove(MOVE(tbl_ptr, len), tbl_ptr, hp->sz.len);
}

void
HPT_index(HPACK_CTX)
static size_t
HPT_index_silent(HPACK_CTX)
{
struct hpack *hp;
void *nam_ptr, *val_ptr;
Expand All @@ -391,7 +408,7 @@ HPT_index(HPACK_CTX)

len = HPACK_OVERHEAD + nam_sz + val_sz;
if (!hpt_fit(ctx, len))
return;
return 0;

nam_ptr = JUMP(hp->tbl, 0);
val_ptr = JUMP(hp->tbl, nam_sz + 1);
Expand All @@ -412,8 +429,16 @@ HPT_index(HPACK_CTX)
hp->tbl->val_sz = (uint16_t)val_sz;
hp->sz.len += len;
hp->cnt++;
return len;
}

HPC_notify(ctx, HPACK_EVT_INDEX, NULL, len);
void
HPT_index(HPACK_CTX)
{
size_t len = HPT_index_silent(ctx);
if(len > 0) {
HPC_notify(ctx, HPACK_EVT_INDEX, NULL, len);
}
}

/**********************************************************************
Expand Down
2 changes: 1 addition & 1 deletion man/cashdumb.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ main(void)
size_t linelen;

/* initialization */
hp = hpack_encoder(TABLE_SIZE, -1, hpack_default_alloc);
hp = hpack_encoder(TABLE_SIZE, -1, hpack_default_alloc, 0);
(void)memset(&stt, 0, sizeof stt);
stt.fld = static_fld;
stt.dmb = static_dmb;
Expand Down
2 changes: 1 addition & 1 deletion man/cashdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ main(int argc, char **argv)

/* initialization */
first = 1;
hp = hpack_decoder(4096, -1, hpack_default_alloc);
hp = hpack_decoder(4096, -1, hpack_default_alloc, 0);
dec.blk = blk;
dec.blk_len = 0;
dec.buf = buf;
Expand Down
2 changes: 1 addition & 1 deletion man/hpack_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ main(void)
{
struct hpack *hp;

hp = hpack_encoder(4096, -1, hpack_default_alloc);
hp = hpack_encoder(4096, -1, hpack_default_alloc, 0);
if (hp == NULL)
return (EXIT_FAILURE);

Expand Down
2 changes: 1 addition & 1 deletion tst/fdecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ main(int argc, char **argv)

ctx.blk = blk;

hp = hpack_decoder(tbl_sz, -1, hpack_default_alloc);
hp = hpack_decoder(tbl_sz, -1, hpack_default_alloc, 0);
assert(hp != NULL);

priv.hp = hp;
Expand Down
2 changes: 1 addition & 1 deletion tst/hdecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ main(int argc, char **argv)

ctx.blk = blk;

hp = hpack_decoder(tbl_sz, -1, hpack_default_alloc);
hp = hpack_decoder(tbl_sz, -1, hpack_default_alloc, 0);
assert(hp != NULL);

priv.hp = hp;
Expand Down
2 changes: 1 addition & 1 deletion tst/hencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ main(int argc, char **argv)
return (EXIT_FAILURE);
}

hp = hpack_encoder(tbl_sz, tbl_mem, hpack_default_alloc);
hp = hpack_encoder(tbl_sz, tbl_mem, hpack_default_alloc, 0);
assert(hp != NULL);

ctx.res = HPACK_RES_OK;
Expand Down
20 changes: 10 additions & 10 deletions tst/hpack_arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ make_decoder(size_t max, ssize_t rsz, const struct hpack_alloc *ha)
{
struct hpack *dec;

CHECK_NOTNULL(dec, hpack_decoder, max, rsz, ha);
CHECK_NOTNULL(dec, hpack_decoder, max, rsz, ha, 0);
return (dec);
}

Expand All @@ -119,7 +119,7 @@ make_encoder(size_t max, ssize_t lim, const struct hpack_alloc *ha)
{
struct hpack *enc;

CHECK_NOTNULL(enc, hpack_encoder, max, lim, ha);
CHECK_NOTNULL(enc, hpack_encoder, max, lim, ha, 0);
return (enc);
}

Expand Down Expand Up @@ -244,13 +244,13 @@ static struct hpack_encoding unknown_encoding = {
static void
test_null_alloc(void)
{
CHECK_NULL(hp, hpack_decoder, 0, -1, NULL);
CHECK_NULL(hp, hpack_decoder, 0, -1, NULL, 0);
}

static void
test_null_malloc(void)
{
CHECK_NULL(hp, hpack_decoder, 0, -1, &null_alloc);
CHECK_NULL(hp, hpack_decoder, 0, -1, &null_alloc, 0);
}

static void
Expand All @@ -264,25 +264,25 @@ static void
test_alloc_overflow(void)
{
CHECK_NULL(hp, hpack_decoder, UINT16_MAX + 1, -1,
hpack_default_alloc);
hpack_default_alloc, 0);
CHECK_NULL(hp, hpack_decoder, 4096, UINT16_MAX + 1,
hpack_default_alloc);
hpack_default_alloc, 0);
}

static void
test_alloc_underflow(void)
{
CHECK_NOTNULL(hp, hpack_decoder, 4096, 2048, hpack_default_alloc);
CHECK_NOTNULL(hp, hpack_decoder, 4096, 2048, hpack_default_alloc, 0);
hpack_free(&hp);
CHECK_NOTNULL(hp, hpack_encoder, 4096, 2048, hpack_default_alloc);
CHECK_NOTNULL(hp, hpack_encoder, 4096, 2048, hpack_default_alloc, 0);
hpack_free(&hp);
}

static void
test_malloc_failure(void)
{
CHECK_NULL(hp, hpack_decoder, 4096, -1, &static_alloc);
CHECK_NULL(hp, hpack_encoder, 0, 4096, &static_alloc);
CHECK_NULL(hp, hpack_decoder, 4096, -1, &static_alloc, 0);
CHECK_NULL(hp, hpack_encoder, 0, 4096, &static_alloc, 0);
}

static void
Expand Down
2 changes: 1 addition & 1 deletion tst/hpack_mbm.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ mbm_setup(void)
struct hpack_encoding he;
char buf[64];

hp = hpack_encoder(4096, -1, hpack_default_alloc);
hp = hpack_encoder(4096, -1, hpack_default_alloc, 0);
if (hp == NULL)
WRONG("hpack_encoder");

Expand Down