From e10f2bf08d1b8b2cc0d2d9450676b8327fb64828 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 27 Sep 2016 23:47:58 +0700 Subject: [PATCH] reformatting: parse_hex4 --- cJSON.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 9 deletions(-) diff --git a/cJSON.c b/cJSON.c index 5eced55..706ac76 100644 --- a/cJSON.c +++ b/cJSON.c @@ -344,17 +344,90 @@ static char *print_number(cJSON *item, printbuffer *p) return str; } +/* parse 4 digit hexadecimal number */ static unsigned parse_hex4(const char *str) { - unsigned h=0; - if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; - h=h<<4;str++; - if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; - h=h<<4;str++; - if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; - h=h<<4;str++; - if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; - return h; + unsigned h = 0; + /* first digit */ + if ((*str >= '0') && (*str <= '9')) + { + h += (*str) - '0'; + } + else if ((*str >= 'A') && (*str <= 'F')) + { + h += 10 + (*str) - 'A'; + } + else if ((*str >= 'a') && (*str <= 'f')) + { + h += 10 + (*str) - 'a'; + } + else /* invalid */ + { + return 0; + } + + + /* second digit */ + h = h << 4; + str++; + if ((*str >= '0') && (*str <= '9')) + { + h += (*str) - '0'; + } + else if ((*str >= 'A') && (*str <= 'F')) + { + h += 10 + (*str) - 'A'; + } + else if ((*str >= 'a') && (*str <= 'f')) + { + h += 10 + (*str) - 'a'; + } + else /* invalid */ + { + return 0; + } + + /* third digit */ + h = h << 4; + str++; + if ((*str >= '0') && (*str <= '9')) + { + h += (*str) - '0'; + } + else if ((*str >= 'A') && (*str <= 'F')) + { + h += 10 + (*str) - 'A'; + } + else if ((*str >= 'a') && (*str <= 'f')) + { + h += 10 + (*str) - 'a'; + } + else /* invalid */ + { + return 0; + } + + /* fourth digit */ + h = h << 4; + str++; + if ((*str >= '0') && (*str <= '9')) + { + h += (*str) - '0'; + } + else if ((*str >= 'A') && (*str <= 'F')) + { + h += 10 + (*str) - 'A'; + } + else if ((*str >= 'a') && (*str <= 'f')) + { + h += 10 + (*str) - 'a'; + } + else /* invalid */ + { + return 0; + } + + return h; } /* Parse the input text into an unescaped cstring, and populate item. */