From 0c88f8bf89a89ec061460cec3467a99ed2bcf323 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Mon, 30 Dec 2013 14:35:55 +1030 Subject: [PATCH] Replace str_to_uint() with str_to_uint32() Also str_to_int() with str_to_int32() --- http_server.c | 10 +++++----- str.c | 44 ++++++++++++++++++++++++++++++++++++++++---- str.h | 4 ++-- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/http_server.c b/http_server.c index 5fe5a45f..87f38b31 100644 --- a/http_server.c +++ b/http_server.c @@ -541,9 +541,9 @@ static inline int _parse_http_size_t(struct http_request *r, http_size_t *szp) return !_run_out(r) && isdigit(*r->cursor) && str_to_uint64(r->cursor, 10, szp, &r->cursor); } -static inline int _parse_uint(struct http_request *r, unsigned int *uintp) +static inline int _parse_uint32(struct http_request *r, uint32_t *uint32p) { - return !_run_out(r) && isdigit(*r->cursor) && str_to_uint(r->cursor, 10, uintp, &r->cursor); + return !_run_out(r) && isdigit(*r->cursor) && str_to_uint32(r->cursor, 10, uint32p, &r->cursor); } static unsigned _parse_ranges(struct http_request *r, struct http_range *range, unsigned nrange) @@ -793,12 +793,12 @@ static int http_request_parse_http_version(struct http_request *r) // Parse HTTP version: HTTP/m.n followed by CRLF. assert(r->version_major == 0); assert(r->version_minor == 0); - unsigned major, minor; + uint32_t major, minor; if (!( _skip_literal(r, "HTTP/") - && _parse_uint(r, &major) + && _parse_uint32(r, &major) && major > 0 && major < UINT8_MAX && _skip_literal(r, ".") - && _parse_uint(r, &minor) + && _parse_uint32(r, &minor) && minor < UINT8_MAX && _skip_eol(r) ) diff --git a/str.c b/str.c index c4c16d85..c673e721 100644 --- a/str.c +++ b/str.c @@ -645,7 +645,7 @@ char *str_str(char *haystack, const char *needle, size_t haystack_len) return NULL; } -int str_to_int(const char *str, int base, int *result, const char **afterp) +int str_to_int32(const char *str, int base, int32_t *result, const char **afterp) { if (isspace(*str)) return 0; @@ -654,14 +654,14 @@ int str_to_int(const char *str, int base, int *result, const char **afterp) long value = strtol(str, (char**)&end, base); if (afterp) *afterp = end; - if (errno == ERANGE || end == str || value > INT_MAX || value < INT_MIN || isdigit(*end) || (!afterp && *end)) + if (errno == ERANGE || end == str || value > INT32_MAX || value < INT32_MIN || isdigit(*end) || (!afterp && *end)) return 0; if (result) *result = value; return 1; } -int str_to_uint(const char *str, int base, unsigned *result, const char **afterp) +int str_to_uint32(const char *str, int base, uint32_t *result, const char **afterp) { if (isspace(*str)) return 0; @@ -670,7 +670,7 @@ int str_to_uint(const char *str, int base, unsigned *result, const char **afterp unsigned long value = strtoul(str, (char**)&end, base); if (afterp) *afterp = end; - if (errno == ERANGE || end == str || value > UINT_MAX || isdigit(*end) || (!afterp && *end)) + if (errno == ERANGE || end == str || value > UINT32_MAX || isdigit(*end) || (!afterp && *end)) return 0; if (result) *result = value; @@ -758,6 +758,42 @@ int str_to_int64_scaled(const char *str, int base, int64_t *result, const char * return 1; } +int str_to_uint32_scaled(const char *str, int base, uint32_t *result, const char **afterp) +{ + uint32_t value; + const char *end = str; + if (!str_to_uint32(str, base, &value, &end)) { + if (afterp) + *afterp = end; + return 0; + } + value *= scale_factor(end, &end); + if (afterp) + *afterp = end; + else if (*end) + return 0; + if (result) + *result = value; + return 1; +} + +int uint32_scaled_to_str(char *str, size_t len, uint32_t value) +{ + char symbol = '\0'; + int i; + for (i = 0; i != NELS(scale_factors); ++i) + if (value % scale_factors[i].factor == 0) { + value /= scale_factors[i].factor; + symbol = scale_factors[i].symbol; + break; + } + strbuf b = strbuf_local(str, len); + strbuf_sprintf(b, "%lu", (unsigned long) value); + if (symbol) + strbuf_putc(b, symbol); + return strbuf_overrun(b) ? 0 : 1; +} + int str_to_uint64_scaled(const char *str, int base, uint64_t *result, const char **afterp) { uint64_t value; diff --git a/str.h b/str.h index b2cb34f1..15d5f2b3 100644 --- a/str.h +++ b/str.h @@ -391,8 +391,8 @@ char *str_str(char *haystack, const char *needle, size_t haystack_len); * * @author Andrew Bettison */ -int str_to_int(const char *str, int base, int *result, const char **afterp); -int str_to_uint(const char *str, int base, unsigned *result, const char **afterp); +int str_to_int32(const char *str, int base, int32_t *result, const char **afterp); +int str_to_uint32(const char *str, int base, uint32_t *result, const char **afterp); int str_to_int64(const char *str, int base, int64_t *result, const char **afterp); int str_to_uint64(const char *str, int base, uint64_t *result, const char **afterp);