mirror of
https://git.gnunet.org/libmicrohttpd.git
synced 2026-09-25 04:09:31 +03:00
mhd_str: added MHD_hex_to_bin() internal function
This commit is contained in:
@@ -1387,6 +1387,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,
|
||||
|
||||
@@ -493,6 +493,24 @@ MHD_bin_to_hex (const void *bin,
|
||||
size_t size,
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user