diff --git a/src/ChunkedEncoding.h b/src/ChunkedEncoding.h index a83b216a6..5b959ad30 100644 --- a/src/ChunkedEncoding.h +++ b/src/ChunkedEncoding.h @@ -44,28 +44,26 @@ namespace uWS { inline void consumeHexNumber(std::string_view &data, uint64_t &state) { /* Consume everything higher than 32 */ while (data.length() && data.data()[0] > 32) { - + unsigned char digit = (unsigned char)data.data()[0]; - unsigned int number; - - if (digit >= '0' && digit <= '9') { - number = (unsigned int) (digit - '0'); - } else if (digit >= 'a' && digit <= 'f') { - number = (unsigned int) (digit - 'a') + 10; - } else if (digit >= 'A' && digit <= 'F') { - number = (unsigned int) (digit - 'A') + 10; - } else { - /* Not a hex digit: chunk-size token ends here (chunk-ext follows) */ - break; + if (digit >= 'a') { + digit = (unsigned char) (digit - ('a' - ':')); + } else if (digit >= 'A') { + digit = (unsigned char) (digit - ('A' - ':')); } - - if (chunkSize(state) & STATE_SIZE_OVERFLOW) { + + unsigned int number = ((unsigned int) digit - (unsigned int) '0'); + + if (number > 16 || (chunkSize(state) & STATE_SIZE_OVERFLOW)) { state = STATE_IS_ERROR; return; } - - uint64_t bits = STATE_IS_CHUNKED; + + // extract state bits + uint64_t bits = /*state &*/ STATE_IS_CHUNKED; + state = (state & STATE_SIZE_MASK) * 16ull + number; + state |= bits; data.remove_prefix(1); }