summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-08-28 01:20:44 -0400
committerPaul Duncan <pabs@pablotron.org>2016-08-28 01:20:44 -0400
commit1367fe1bafa1b877f4f92361294eab3a1dd3c11c (patch)
tree46ed75be5dcd420f6151888275a42176bd29b45e
parent56b49c7258f6c597da56ac615c7387b655485c47 (diff)
downloadlibfhp-1367fe1bafa1b877f4f92361294eab3a1dd3c11c.tar.bz2
libfhp-1367fe1bafa1b877f4f92361294eab3a1dd3c11c.zip
fix te-parser.c
-rw-r--r--error.c2
-rw-r--r--include/fhp/fhp.h7
-rw-r--r--te-parser.c80
-rw-r--r--test.c8
4 files changed, 74 insertions, 23 deletions
diff --git a/error.c b/error.c
index 3a5ea9c..43ec598 100644
--- a/error.c
+++ b/error.c
@@ -24,6 +24,8 @@ fhp_errors[] = {
"invalid character in transfer encoding",
"bad transfer encoding state",
"transfer encoding parser already done",
+ "NULL transfer encoding hash output buffer",
+ "transfer encoding hash output buffer is too small",
};
fhp_err_t
diff --git a/include/fhp/fhp.h b/include/fhp/fhp.h
index d537a6e..5ca48b7 100644
--- a/include/fhp/fhp.h
+++ b/include/fhp/fhp.h
@@ -39,6 +39,8 @@ typedef enum {
FHP_ERR_INVALID_CHAR_IN_TE,
FHP_ERR_BAD_TE_STATE,
FHP_ERR_TE_PARSER_DONE,
+ FHP_ERR_NULL_TE_BUF,
+ FHP_ERR_SMALL_TE_BUF,
FHP_ERR_LAST
} fhp_err_t;
@@ -146,7 +148,10 @@ fhp_err_t
fhp_te_parser_push(fhp_te_parser_t *, uint8_t * const, size_t);
fhp_err_t
-fhp_te_parser_done(fhp_te_parser_t * const);
+fhp_te_parser_done(fhp_te_parser_t * const, size_t * const);
+
+fhp_err_t
+fhp_te_parser_get_tes(fhp_te_parser_t * const, uint32_t * const, size_t);
//
// context functions
diff --git a/te-parser.c b/te-parser.c
index 507038b..53b690f 100644
--- a/te-parser.c
+++ b/te-parser.c
@@ -45,10 +45,29 @@ fhp_te_parser_init(fhp_te_parser_t * const te) {
}
static fhp_err_t
+fhp_te_parser_push_te(
+ fhp_te_parser_t * const te,
+ uint32_t hash
+) {
+ // check number of transfer encodings
+ if (te->num_tes + 1 >= FHP_TE_MAX_TES)
+ return FHP_ERR_TOO_MANY_TES;
+
+ // add to list of transfer encodings
+ te->tes[te->num_tes] = hash;
+ te->num_tes++;
+
+ // return success
+ return FHP_OK;
+}
+
+static fhp_err_t
fhp_te_parser_push_byte(
fhp_te_parser_t * const te,
uint8_t byte
) {
+ fhp_err_t err;
+
retry:
switch (te->state) {
case FHP_TE_STATE_INIT:
@@ -69,14 +88,9 @@ retry:
if (te->len > 0)
fhp_te_parser_buf_flush(te);
- // check number of encodings
- if (te->num_tes + 1 >= FHP_TE_MAX_TES)
- return FHP_ERR_TOO_MANY_TES;
-
- // add to list of transfer encodings
- // FIXME: check length
- te->tes[te->num_tes] = te->buf_hash;
- te->num_tes++;
+ // push te
+ if ((err = fhp_te_parser_push_te(te, te->buf_hash)) != FHP_OK)
+ return err;
// set state
te->state = FHP_TE_STATE_IGNORE_UNTIL_COMMA;
@@ -87,14 +101,9 @@ retry:
if (te->len > 0)
fhp_te_parser_buf_flush(te);
- // check number of encodings
- if (te->num_tes + 1 >= FHP_TE_MAX_TES)
- return FHP_ERR_TOO_MANY_TES;
-
- // add to list of transfer encodings
- // FIXME: check length
- te->tes[te->num_tes] = te->buf_hash;
- te->num_tes++;
+ // push te
+ if ((err = fhp_te_parser_push_te(te, te->buf_hash)) != FHP_OK)
+ return err;
// set state
te->state = FHP_TE_STATE_SPACE;
@@ -115,7 +124,7 @@ retry:
break;
default:
// do nothing (ignore)
- NULL;
+ break;
}
break;
@@ -166,8 +175,11 @@ fhp_te_parser_push(
size_t len
) {
for (size_t i = 0; i < len; i++) {
+ // debug
+ // fprintf(stderr, "fhp_te_parser_push(): i = %lu, byte = %u\n", i, buf[i]);
+
// push byte
- fhp_err_t err = fhp_te_parser_push_byte(te, buf[len]);
+ fhp_err_t err = fhp_te_parser_push_byte(te, buf[i]);
// check error
if (err != FHP_OK) {
@@ -187,8 +199,11 @@ fhp_te_parser_push(
}
fhp_err_t
-fhp_te_parser_done(fhp_te_parser_t * const te) {
- uint8_t buf[1] = { 20 };
+fhp_te_parser_done(
+ fhp_te_parser_t * const te,
+ size_t * const ret_num_tes
+) {
+ uint8_t buf[1] = { ' ' };
// flush data, check for error
fhp_err_t err = fhp_te_parser_push(te, buf, 1);
@@ -198,6 +213,31 @@ fhp_te_parser_done(fhp_te_parser_t * const te) {
// set state
te->state = FHP_TE_STATE_DONE;
+ // save number of tes
+ if (ret_num_tes)
+ *ret_num_tes = te->num_tes;
+
+ // return success
+ return FHP_OK;
+}
+
+fhp_err_t
+fhp_te_parser_get_tes(
+ fhp_te_parser_t * const te,
+ uint32_t * const hashes,
+ size_t hashes_len
+) {
+ // check buffer
+ if (!hashes)
+ return FHP_ERR_NULL_TE_BUF;
+
+ // check size
+ if (hashes_len < te->num_tes)
+ return FHP_ERR_SMALL_TE_BUF;
+
+ // copy data
+ memcpy(hashes, te->tes, te->num_tes * sizeof(uint32_t));
+
// return success
return FHP_OK;
}
diff --git a/test.c b/test.c
index a371bc6..b1ca674 100644
--- a/test.c
+++ b/test.c
@@ -151,6 +151,7 @@ test_percent(void) {
static char *
te_parser_tests[] = {
+ " ",
"gzip",
"deflate",
"gzip,deflate,chunked",
@@ -168,6 +169,8 @@ test_te_parser(void) {
// get test string
char * const s = te_parser_tests[i];
+ fprintf(stderr, "te testing: \"%s\"\n", s);
+
// init parser
fhp_te_parser_t p;
fhp_te_parser_init(&p);
@@ -178,11 +181,12 @@ test_te_parser(void) {
}
// finish parsing
- if ((err = fhp_te_parser_done(&p)) != FHP_OK) {
+ size_t num_tes;
+ if ((err = fhp_te_parser_done(&p, &num_tes)) != FHP_OK) {
die("test_te_parser", "fhp_te_parser_push", err);
}
- fprintf(stderr, "te test: \"%s\", p->num_tes = %lu\n", s, p.num_tes);
+ fprintf(stderr, "te test: \"%s\", num_tes = %lu\n", s, num_tes);
}
}