From 418eee6c4f5c52a8b6b7fc1cff4f47940cc6d077 Mon Sep 17 00:00:00 2001 From: tyrolyean Date: Fri, 1 May 2020 01:45:51 +0200 Subject: [PATCH] Improved boundary checking Signed-off-by: tyrolyean --- src/tools.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/tools.c b/src/tools.c index 914f9ed..72d7751 100644 --- a/src/tools.c +++ b/src/tools.c @@ -158,16 +158,37 @@ char* get_value_equals(char* content_type, size_t content_len, *boundary_len = 0; return NULL; } +#define STATE_INIT 0 +#define STATE_EQUALS 1 +#define STATE_ALNUM 2 +#define STATE_QUOTE 3 + + size_t state = STATE_INIT; for(size_t i = boundary_offset; i < content_len;i++){ - if(content_type[i] == '"' && boundary_begin == NULL){ - boundary_begin = &content_type[i+1]; - }else if(content_type[i] == '"'){ - *boundary_len = &content_type[i] - boundary_begin ; - return boundary_begin; + if(state == STATE_INIT && content_type[i] == '='){ + state = STATE_EQUALS; + }else if(state == STATE_EQUALS && !isspace(content_type[i])){ + if(content_type[i] == '"' && boundary_begin == NULL){ + boundary_begin = &content_type[++i]; + state = STATE_QUOTE; + }else { + boundary_begin = &content_type[i]; + state = STATE_ALNUM; + } + }else if(state == STATE_ALNUM){ + if(isspace(content_type[i])){ + *boundary_len = &content_type[i] - + boundary_begin ; + return boundary_begin; + } + }else if(state == STATE_QUOTE && content_type[i] == '"'){ + *boundary_len = &content_type[i] - + boundary_begin ; + return boundary_begin; + } } - *boundary_len = 0; return NULL; }