summaryrefslogtreecommitdiff
path: root/te-parser.c
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 /te-parser.c
parent56b49c7258f6c597da56ac615c7387b655485c47 (diff)
downloadlibfhp-1367fe1bafa1b877f4f92361294eab3a1dd3c11c.tar.bz2
libfhp-1367fe1bafa1b877f4f92361294eab3a1dd3c11c.zip
fix te-parser.c
Diffstat (limited to 'te-parser.c')
-rw-r--r--te-parser.c80
1 files changed, 60 insertions, 20 deletions
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;
}