diff options
Diffstat (limited to 'src/microhttpd')
-rw-r--r-- | src/microhttpd/mhd_str.c | 36 | ||||
-rw-r--r-- | src/microhttpd/mhd_str.h | 18 |
2 files changed, 54 insertions, 0 deletions
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c index ef870e61..cf2e43c3 100644 --- a/src/microhttpd/mhd_str.c +++ b/src/microhttpd/mhd_str.c | |||
@@ -1388,6 +1388,42 @@ MHD_bin_to_hex (const void *bin, | |||
1388 | 1388 | ||
1389 | 1389 | ||
1390 | size_t | 1390 | size_t |
1391 | MHD_hex_to_bin (const char *hex, | ||
1392 | size_t len, | ||
1393 | void *bin) | ||
1394 | { | ||
1395 | uint8_t *const out = (uint8_t *) bin; | ||
1396 | size_t r; | ||
1397 | size_t w; | ||
1398 | |||
1399 | if (0 == len) | ||
1400 | return 0; | ||
1401 | r = 0; | ||
1402 | w = 0; | ||
1403 | if (0 != len % 2) | ||
1404 | { | ||
1405 | /* Assume the first byte is encoded with single digit */ | ||
1406 | const int l = toxdigitvalue (hex[r++]); | ||
1407 | if (0 > l) | ||
1408 | return 0; | ||
1409 | out[w++] = (uint8_t) ((unsigned int) l); | ||
1410 | } | ||
1411 | while (r < len) | ||
1412 | { | ||
1413 | const int h = toxdigitvalue (hex[r++]); | ||
1414 | const int l = toxdigitvalue (hex[r++]); | ||
1415 | if ((0 > h) || (0 > l)) | ||
1416 | return 0; | ||
1417 | out[w++] = ( (((uint8_t) ((unsigned int) h)) << 4) | ||
1418 | | ((uint8_t) ((unsigned int) l)) ); | ||
1419 | } | ||
1420 | mhd_assert (len == r); | ||
1421 | mhd_assert ((len + 1) / 2 == w); | ||
1422 | return w; | ||
1423 | } | ||
1424 | |||
1425 | |||
1426 | size_t | ||
1391 | MHD_str_pct_decode_strict_n_ (const char *pct_encoded, | 1427 | MHD_str_pct_decode_strict_n_ (const char *pct_encoded, |
1392 | size_t pct_encoded_len, | 1428 | size_t pct_encoded_len, |
1393 | char *decoded, | 1429 | char *decoded, |
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h index ec8b5cf4..91219cba 100644 --- a/src/microhttpd/mhd_str.h +++ b/src/microhttpd/mhd_str.h | |||
@@ -494,6 +494,24 @@ MHD_bin_to_hex (const void *bin, | |||
494 | char *hex); | 494 | char *hex); |
495 | 495 | ||
496 | /** | 496 | /** |
497 | * Convert hexadecimal digits to binary data. | ||
498 | * | ||
499 | * The input decoded byte-by-byte (each byte is two hexadecimal digits). | ||
500 | * If length is an odd number, extra leading zero is assumed. | ||
501 | * | ||
502 | * @param hex the input string with hexadecimal digits | ||
503 | * @param len the length of the input string | ||
504 | * @param[out] bin the output buffer, must be at least len/2 bytes long (or | ||
505 | * len/2 + 1 if @a len is not even number) | ||
506 | * @return the number of bytes written to the output buffer, | ||
507 | * zero if found any character which is not hexadecimal digits | ||
508 | */ | ||
509 | size_t | ||
510 | MHD_hex_to_bin (const char *hex, | ||
511 | size_t len, | ||
512 | void *bin); | ||
513 | |||
514 | /** | ||
497 | * Decode string with percent-encoded characters as defined by | 515 | * Decode string with percent-encoded characters as defined by |
498 | * RFC 3986 #section-2.1. | 516 | * RFC 3986 #section-2.1. |
499 | * | 517 | * |