From 1367fe1bafa1b877f4f92361294eab3a1dd3c11c Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 28 Aug 2016 01:20:44 -0400 Subject: fix te-parser.c --- te-parser.c | 80 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 20 deletions(-) (limited to 'te-parser.c') diff --git a/te-parser.c b/te-parser.c index 507038b..53b690f 100644 --- a/te-parser.c +++ b/te-parser.c @@ -44,11 +44,30 @@ fhp_te_parser_init(fhp_te_parser_t * const te) { return FHP_OK; } +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; } -- cgit v1.2.3