From a4099970069e8517dcad586ca63b370026f9f89e Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Sat, 6 Dec 2025 01:03:10 +0100 Subject: [PATCH] Add url_decode_field --- url.c | 31 +++++++++++++++++++++++++++++++ url.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/url.c b/url.c index 1307440..98fc93d 100644 --- a/url.c +++ b/url.c @@ -901,3 +901,34 @@ int url_remove_white_space(char *src, int len, char *dst, int cap) return copied; } + +int url_decode_field(URL_String field, char *dst, int cap) +{ + char *src = field.ptr; + int len = field.len; + int rd = 0; + int wr = 0; + + while (rd < len) { + + int ret = is_percent_encoded(src, len, rd); + if (ret < 0) + return -1; + + char c; + if (ret == 1) { + int h = hex_digit_to_int(src[rd+1]); + int l = hex_digit_to_int(src[rd+2]); + c = (char) ((h << 4) | l); + rd += 3; + } else { + c = src[rd]; + rd++; + } + + if (wr < cap) + dst[wr] = c; + wr++; + } + return wr; +} diff --git a/url.h b/url.h index acaa2be..4021074 100644 --- a/url.h +++ b/url.h @@ -110,4 +110,6 @@ int url_resolve_reference(char *src, int len, int *pcur, int url_remove_white_space(char *src, int len, char *dst, int cap); +int url_decode_field(URL_String field, char *dst, int cap); + #endif // URL_INCLUDED