gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] 04/15: mhd_str: added MHD_hex_to_bin() internal function


From: gnunet
Subject: [libmicrohttpd] 04/15: mhd_str: added MHD_hex_to_bin() internal function
Date: Tue, 19 Jul 2022 16:51:12 +0200

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a commit to branch master
in repository libmicrohttpd.

commit ee06f77543fa335befa8159b9ffdafc35d30fb33
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Wed Jun 22 16:31:39 2022 +0300

    mhd_str: added MHD_hex_to_bin() internal function
---
 src/microhttpd/mhd_str.c | 36 ++++++++++++++++++++++++++++++++++++
 src/microhttpd/mhd_str.h | 18 ++++++++++++++++++
 2 files changed, 54 insertions(+)

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
@@ -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,
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
@@ -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.

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]