diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-08-13 11:44:50 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-08-15 16:21:45 +0300 |
commit | b115e254b32f58dea98a0f30ad1510c3223b8a1b (patch) | |
tree | adc8b48e05dcda5d728d931b8cff80e26448abe9 | |
parent | 7d2bf243107fb53a36f345bfd933af207c2e7823 (diff) | |
download | libmicrohttpd-b115e254b32f58dea98a0f30ad1510c3223b8a1b.tar.gz libmicrohttpd-b115e254b32f58dea98a0f30ad1510c3223b8a1b.zip |
mhd_str: added function for bin to hex without zero-termination
-rw-r--r-- | src/microhttpd/digestauth.c | 1 | ||||
-rw-r--r-- | src/microhttpd/mhd_str.c | 15 | ||||
-rw-r--r-- | src/microhttpd/mhd_str.h | 21 | ||||
-rw-r--r-- | src/microhttpd/test_str_base64.c | 6 | ||||
-rw-r--r-- | src/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 index 7d0719ba..4b4eac7c 100644 --- a/src/microhttpd/digestauth.c +++ b/src/microhttpd/digestauth.c | |||
@@ -1603,7 +1603,6 @@ calculate_add_nonce_with_retry (struct MHD_Connection *const connection, | |||
1603 | return false; | 1603 | return false; |
1604 | } | 1604 | } |
1605 | memcpy (nonce, nonce2, NONCE_STD_LEN (digest_size)); | 1605 | memcpy (nonce, nonce2, NONCE_STD_LEN (digest_size)); |
1606 | mhd_assert (0 == nonce[NONCE_STD_LEN (digest_size)]); | ||
1607 | } | 1606 | } |
1608 | return true; | 1607 | return true; |
1609 | } | 1608 | } |
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c index cf2e43c3..6099f93c 100644 --- a/src/microhttpd/mhd_str.c +++ b/src/microhttpd/mhd_str.c | |||
@@ -1382,12 +1382,25 @@ MHD_bin_to_hex (const void *bin, | |||
1382 | j = b & 0x0f; | 1382 | j = b & 0x0f; |
1383 | hex[i * 2 + 1] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a')); | 1383 | hex[i * 2 + 1] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a')); |
1384 | } | 1384 | } |
1385 | hex[i * 2] = 0; | ||
1386 | return i * 2; | 1385 | return i * 2; |
1387 | } | 1386 | } |
1388 | 1387 | ||
1389 | 1388 | ||
1390 | size_t | 1389 | size_t |
1390 | MHD_bin_to_hex_z (const void *bin, | ||
1391 | size_t size, | ||
1392 | char *hex) | ||
1393 | { | ||
1394 | size_t res; | ||
1395 | |||
1396 | res = MHD_bin_to_hex (bin, size, hex); | ||
1397 | hex[res] = 0; | ||
1398 | |||
1399 | return res; | ||
1400 | } | ||
1401 | |||
1402 | |||
1403 | size_t | ||
1391 | MHD_hex_to_bin (const char *hex, | 1404 | MHD_hex_to_bin (const char *hex, |
1392 | size_t len, | 1405 | size_t len, |
1393 | void *bin) | 1406 | void *bin) |
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h index dd74e602..284ad853 100644 --- a/src/microhttpd/mhd_str.h +++ b/src/microhttpd/mhd_str.h | |||
@@ -497,6 +497,21 @@ MHD_uint8_to_str_pad (uint8_t val, | |||
497 | char *buf, | 497 | char *buf, |
498 | size_t buf_size); | 498 | size_t buf_size); |
499 | 499 | ||
500 | |||
501 | /** | ||
502 | * Convert @a size bytes from input binary data to lower case | ||
503 | * hexadecimal digits. | ||
504 | * Result is NOT zero-terminated | ||
505 | * @param bin the pointer to the binary data to convert | ||
506 | * @param size the size in bytes of the binary data to convert | ||
507 | * @param[out] hex the output buffer, should be at least 2 * @a size | ||
508 | * @return The number of characters written to the output buffer. | ||
509 | */ | ||
510 | size_t | ||
511 | MHD_bin_to_hex (const void *bin, | ||
512 | size_t size, | ||
513 | char *hex); | ||
514 | |||
500 | /** | 515 | /** |
501 | * Convert @a size bytes from input binary data to lower case | 516 | * Convert @a size bytes from input binary data to lower case |
502 | * hexadecimal digits, zero-terminate the result. | 517 | * hexadecimal digits, zero-terminate the result. |
@@ -507,9 +522,9 @@ MHD_uint8_to_str_pad (uint8_t val, | |||
507 | * not including terminating zero. | 522 | * not including terminating zero. |
508 | */ | 523 | */ |
509 | size_t | 524 | size_t |
510 | MHD_bin_to_hex (const void *bin, | 525 | MHD_bin_to_hex_z (const void *bin, |
511 | size_t size, | 526 | size_t size, |
512 | char *hex); | 527 | char *hex); |
513 | 528 | ||
514 | /** | 529 | /** |
515 | * Convert hexadecimal digits to binary data. | 530 | * Convert hexadecimal digits to binary data. |
diff --git a/src/microhttpd/test_str_base64.c b/src/microhttpd/test_str_base64.c index 9daa6cb7..1821d116 100644 --- 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, | |||
86 | } | 86 | } |
87 | else | 87 | else |
88 | { | 88 | { |
89 | prnt_size = MHD_bin_to_hex (buf, res_size, prnt); | 89 | prnt_size = MHD_bin_to_hex_z (buf, res_size, prnt); |
90 | mhd_assert (2 * res_size == prnt_size); | 90 | mhd_assert (2 * res_size == prnt_size); |
91 | 91 | ||
92 | fprintf (stderr, | 92 | fprintf (stderr, |
@@ -96,7 +96,7 @@ expect_decoded_n (const char *const encoded, const size_t encoded_len, | |||
96 | (int) prnt_size, prnt, (unsigned) decoded_size, | 96 | (int) prnt_size, prnt, (unsigned) decoded_size, |
97 | (unsigned) res_size); | 97 | (unsigned) res_size); |
98 | } | 98 | } |
99 | prnt_size = MHD_bin_to_hex (decoded, decoded_size, prnt); | 99 | prnt_size = MHD_bin_to_hex_z (decoded, decoded_size, prnt); |
100 | mhd_assert (2 * decoded_size == prnt_size); | 100 | mhd_assert (2 * decoded_size == prnt_size); |
101 | fprintf (stderr, | 101 | fprintf (stderr, |
102 | "\tEXPECTED: MHD_base64_to_bin_n ('%.*s', %u, ->%.*sh, %u)" | 102 | "\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, | |||
620 | } | 620 | } |
621 | else | 621 | else |
622 | { | 622 | { |
623 | prnt_size = MHD_bin_to_hex (buf, res_size, prnt); | 623 | prnt_size = MHD_bin_to_hex_z (buf, res_size, prnt); |
624 | mhd_assert (2 * res_size == prnt_size); | 624 | mhd_assert (2 * res_size == prnt_size); |
625 | 625 | ||
626 | fprintf (stderr, | 626 | fprintf (stderr, |
diff --git a/src/microhttpd/test_str_bin_hex.c b/src/microhttpd/test_str_bin_hex.c index 2b45040a..557146eb 100644 --- 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, | |||
168 | unsigned int check_res = 0; | 168 | unsigned int check_res = 0; |
169 | 169 | ||
170 | memset (buf, fill_chr, sizeof(buf)); /* Fill buffer with some character */ | 170 | memset (buf, fill_chr, sizeof(buf)); /* Fill buffer with some character */ |
171 | res_size = MHD_bin_to_hex (bin, bin_size, buf); | 171 | res_size = MHD_bin_to_hex_z (bin, bin_size, buf); |
172 | 172 | ||
173 | if (res_size != hex_len) | 173 | if (res_size != hex_len) |
174 | { | 174 | { |