commit ee06f77543fa335befa8159b9ffdafc35d30fb33
parent 93a449edfbfe0f4e0f0608791a8b54882dab76bf
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Wed, 22 Jun 2022 16:31:39 +0300
mhd_str: added MHD_hex_to_bin() internal function
Diffstat:
2 files changed, 54 insertions(+), 0 deletions(-)
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
@@ -1388,6 +1388,42 @@ MHD_bin_to_hex (const void *bin,
size_t
+MHD_hex_to_bin (const char *hex,
+ size_t len,
+ void *bin)
+{
+ uint8_t *const out = (uint8_t *) bin;
+ size_t r;
+ size_t w;
+
+ if (0 == len)
+ return 0;
+ r = 0;
+ w = 0;
+ if (0 != len % 2)
+ {
+ /* Assume the first byte is encoded with single digit */
+ const int l = toxdigitvalue (hex[r++]);
+ if (0 > l)
+ return 0;
+ out[w++] = (uint8_t) ((unsigned int) l);
+ }
+ while (r < len)
+ {
+ const int h = toxdigitvalue (hex[r++]);
+ const int l = toxdigitvalue (hex[r++]);
+ if ((0 > h) || (0 > l))
+ return 0;
+ out[w++] = ( (((uint8_t) ((unsigned int) h)) << 4)
+ | ((uint8_t) ((unsigned int) l)) );
+ }
+ mhd_assert (len == r);
+ mhd_assert ((len + 1) / 2 == w);
+ return w;
+}
+
+
+size_t
MHD_str_pct_decode_strict_n_ (const char *pct_encoded,
size_t pct_encoded_len,
char *decoded,
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h
@@ -494,6 +494,24 @@ MHD_bin_to_hex (const void *bin,
char *hex);
/**
+ * Convert hexadecimal digits to binary data.
+ *
+ * The input decoded byte-by-byte (each byte is two hexadecimal digits).
+ * If length is an odd number, extra leading zero is assumed.
+ *
+ * @param hex the input string with hexadecimal digits
+ * @param len the length of the input string
+ * @param[out] bin the output buffer, must be at least len/2 bytes long (or
+ * len/2 + 1 if @a len is not even number)
+ * @return the number of bytes written to the output buffer,
+ * zero if found any character which is not hexadecimal digits
+ */
+size_t
+MHD_hex_to_bin (const char *hex,
+ size_t len,
+ void *bin);
+
+/**
* Decode string with percent-encoded characters as defined by
* RFC 3986 #section-2.1.
*