libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit b115e254b32f58dea98a0f30ad1510c3223b8a1b
parent 7d2bf243107fb53a36f345bfd933af207c2e7823
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Sat, 13 Aug 2022 11:44:50 +0300

mhd_str: added function for bin to hex without zero-termination

Diffstat:
Msrc/microhttpd/digestauth.c | 1-
Msrc/microhttpd/mhd_str.c | 15++++++++++++++-
Msrc/microhttpd/mhd_str.h | 21++++++++++++++++++---
Msrc/microhttpd/test_str_base64.c | 6+++---
Msrc/microhttpd/test_str_bin_hex.c | 2+-
5 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c @@ -1603,7 +1603,6 @@ calculate_add_nonce_with_retry (struct MHD_Connection *const connection, return false; } memcpy (nonce, nonce2, NONCE_STD_LEN (digest_size)); - mhd_assert (0 == nonce[NONCE_STD_LEN (digest_size)]); } return true; } diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c @@ -1382,12 +1382,25 @@ MHD_bin_to_hex (const void *bin, j = b & 0x0f; hex[i * 2 + 1] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a')); } - hex[i * 2] = 0; return i * 2; } size_t +MHD_bin_to_hex_z (const void *bin, + size_t size, + char *hex) +{ + size_t res; + + res = MHD_bin_to_hex (bin, size, hex); + hex[res] = 0; + + return res; +} + + +size_t MHD_hex_to_bin (const char *hex, size_t len, void *bin) diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h @@ -497,6 +497,21 @@ MHD_uint8_to_str_pad (uint8_t val, char *buf, size_t buf_size); + +/** + * Convert @a size bytes from input binary data to lower case + * hexadecimal digits. + * Result is NOT zero-terminated + * @param bin the pointer to the binary data to convert + * @param size the size in bytes of the binary data to convert + * @param[out] hex the output buffer, should be at least 2 * @a size + * @return The number of characters written to the output buffer. + */ +size_t +MHD_bin_to_hex (const void *bin, + size_t size, + char *hex); + /** * Convert @a size bytes from input binary data to lower case * hexadecimal digits, zero-terminate the result. @@ -507,9 +522,9 @@ MHD_uint8_to_str_pad (uint8_t val, * not including terminating zero. */ size_t -MHD_bin_to_hex (const void *bin, - size_t size, - char *hex); +MHD_bin_to_hex_z (const void *bin, + size_t size, + char *hex); /** * Convert hexadecimal digits to binary data. diff --git a/src/microhttpd/test_str_base64.c b/src/microhttpd/test_str_base64.c @@ -86,7 +86,7 @@ expect_decoded_n (const char *const encoded, const size_t encoded_len, } else { - prnt_size = MHD_bin_to_hex (buf, res_size, prnt); + prnt_size = MHD_bin_to_hex_z (buf, res_size, prnt); mhd_assert (2 * res_size == prnt_size); fprintf (stderr, @@ -96,7 +96,7 @@ expect_decoded_n (const char *const encoded, const size_t encoded_len, (int) prnt_size, prnt, (unsigned) decoded_size, (unsigned) res_size); } - prnt_size = MHD_bin_to_hex (decoded, decoded_size, prnt); + prnt_size = MHD_bin_to_hex_z (decoded, decoded_size, prnt); mhd_assert (2 * decoded_size == prnt_size); fprintf (stderr, "\tEXPECTED: MHD_base64_to_bin_n ('%.*s', %u, ->%.*sh, %u)" @@ -620,7 +620,7 @@ expect_fail_n (const char *const encoded, const size_t encoded_len, } else { - prnt_size = MHD_bin_to_hex (buf, res_size, prnt); + prnt_size = MHD_bin_to_hex_z (buf, res_size, prnt); mhd_assert (2 * res_size == prnt_size); fprintf (stderr, diff --git a/src/microhttpd/test_str_bin_hex.c b/src/microhttpd/test_str_bin_hex.c @@ -168,7 +168,7 @@ expect_decoded_n (const char *const hex, const size_t hex_len, unsigned int check_res = 0; memset (buf, fill_chr, sizeof(buf)); /* Fill buffer with some character */ - res_size = MHD_bin_to_hex (bin, bin_size, buf); + res_size = MHD_bin_to_hex_z (bin, bin_size, buf); if (res_size != hex_len) {