From b19761081148f0d6d57fad2eafefc7021285b1bb Mon Sep 17 00:00:00 2001 From: Jens Steube Date: Wed, 2 Oct 2019 14:18:26 +0200 Subject: [PATCH] Add tokenizer support for TOKEN_ATTR_VERIFY_FLOAT --- include/convert.h | 2 ++ include/types.h | 9 +++++---- src/convert.c | 21 +++++++++++++++++++++ src/shared.c | 10 ++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/include/convert.h b/include/convert.h index 7a11466ca..968c93e1b 100644 --- a/include/convert.h +++ b/include/convert.h @@ -24,6 +24,8 @@ bool is_valid_hex_string (const u8 *s, const size_t len); bool is_valid_hex_char (const u8 c); bool is_valid_digit_string (const u8 *s, const size_t len); bool is_valid_digit_char (const u8 c); +bool is_valid_float_string (const u8 *s, const size_t len); +bool is_valid_float_char (const u8 c); u8 hex_convert (const u8 c); diff --git a/include/types.h b/include/types.h index 0f754aa71..e56326117 100644 --- a/include/types.h +++ b/include/types.h @@ -758,10 +758,11 @@ typedef enum token_attr TOKEN_ATTR_VERIFY_SIGNATURE = 1 << 2, TOKEN_ATTR_VERIFY_LENGTH = 1 << 3, TOKEN_ATTR_VERIFY_DIGIT = 1 << 4, - TOKEN_ATTR_VERIFY_HEX = 1 << 5, - TOKEN_ATTR_VERIFY_BASE64A = 1 << 6, - TOKEN_ATTR_VERIFY_BASE64B = 1 << 7, - TOKEN_ATTR_VERIFY_BASE64C = 1 << 8 + TOKEN_ATTR_VERIFY_FLOAT = 1 << 5, + TOKEN_ATTR_VERIFY_HEX = 1 << 6, + TOKEN_ATTR_VERIFY_BASE64A = 1 << 7, + TOKEN_ATTR_VERIFY_BASE64B = 1 << 8, + TOKEN_ATTR_VERIFY_BASE64C = 1 << 9 } token_attr_t; diff --git a/src/convert.c b/src/convert.c index befca7fb9..3a26d6a28 100644 --- a/src/convert.c +++ b/src/convert.c @@ -311,6 +311,27 @@ bool is_valid_hex_char (const u8 c) return false; } +bool is_valid_float_string (const u8 *s, const size_t len) +{ + for (size_t i = 0; i < len; i++) + { + const u8 c = s[i]; + + if (is_valid_float_char (c) == false) return false; + } + + return true; +} + +bool is_valid_float_char (const u8 c) +{ + if ((c >= '0') && (c <= '9')) return true; + + if (c == '.') return true; + + return false; +} + bool is_valid_digit_string (const u8 *s, const size_t len) { for (size_t i = 0; i < len; i++) diff --git a/src/shared.c b/src/shared.c index 009322c48..8d4181375 100644 --- a/src/shared.c +++ b/src/shared.c @@ -1092,6 +1092,16 @@ int input_tokenizer (const u8 *input_buf, const int input_len, token_t *token) if (token->len[token_idx] > token->len_max[token_idx]) return (PARSER_TOKEN_LENGTH); } + if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_DIGIT) + { + if (is_valid_digit_string (token->buf[token_idx], token->len[token_idx]) == false) return (PARSER_TOKEN_ENCODING); + } + + if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_FLOAT) + { + if (is_valid_float_string (token->buf[token_idx], token->len[token_idx]) == false) return (PARSER_TOKEN_ENCODING); + } + if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_HEX) { if (is_valid_hex_string (token->buf[token_idx], token->len[token_idx]) == false) return (PARSER_TOKEN_ENCODING);