gnunet-svn
[Top][All Lists]
Advanced

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

[libmicrohttpd] branch master updated (a940c3ba -> 33ee100c)


From: gnunet
Subject: [libmicrohttpd] branch master updated (a940c3ba -> 33ee100c)
Date: Tue, 07 Jun 2022 08:52:34 +0200

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

karlson2k pushed a change to branch master
in repository libmicrohttpd.

    from a940c3ba -fix warning
     new 4ec916c0 MHD_str_quote(): added new internal function
     new 33ee100c test_str_quote: added testing of the new function

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/microhttpd/mhd_str.c        |  41 ++++++++++++++
 src/microhttpd/mhd_str.h        |  27 ++++++++-
 src/microhttpd/test_str_quote.c | 121 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 186 insertions(+), 3 deletions(-)

diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
index 5829daa5..f8e1965f 100644
--- a/src/microhttpd/mhd_str.c
+++ b/src/microhttpd/mhd_str.c
@@ -1474,3 +1474,44 @@ MHD_str_unquote (const char *quoted,
 
 
 #endif /* DAUTH_SUPPORT */
+
+#if defined(DAUTH_SUPPORT) || defined(BAUTH_SUPPORT)
+
+size_t
+MHD_str_quote (const char *unquoted,
+               size_t unquoted_len,
+               char *result,
+               size_t buf_size)
+{
+  size_t r;
+  size_t w;
+
+  r = 0;
+  w = 0;
+
+  if (unquoted_len > buf_size)
+    return 0; /* The output buffer is too small */
+
+  while (unquoted_len > r)
+  {
+    if (buf_size <= w)
+      return 0; /* The output buffer is too small */
+    else
+    {
+      const char chr = unquoted[r++];
+      if (('\\' == chr) || ('\"' == chr))
+      {
+        result[w++] = '\\'; /* Escape current char */
+        if (buf_size <= w)
+          return 0; /* The output buffer is too small */
+      }
+      result[w++] = chr;
+    }
+  }
+  mhd_assert (w >= r);
+  mhd_assert (w <= r * 2);
+  return w;
+}
+
+
+#endif /* DAUTH_SUPPORT || BAUTH_SUPPORT */
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h
index 67b2d044..dbe97352 100644
--- a/src/microhttpd/mhd_str.h
+++ b/src/microhttpd/mhd_str.h
@@ -544,7 +544,7 @@ MHD_str_equal_caseless_quoted_bin_n (const char *quoted,
  *               DQUOTE chars, does not need to be zero-terminated
  * @param quoted_len the length in chars of the @a quoted string
  * @param[out] result the pointer to the buffer to put the result, must
- *                    be at least @a size character long.May be modified even
+ *                    be at least @a size character long. May be modified even
  *                    if @a quoted is invalid sequence. The result is NOT
  *                    zero-terminated.
  * @return The number of characters written to the output buffer,
@@ -558,4 +558,29 @@ MHD_str_unquote (const char *quoted,
 
 #endif /* DAUTH_SUPPORT */
 
+#if defined(DAUTH_SUPPORT) || defined(BAUTH_SUPPORT)
+
+/**
+ * Convert string from unquoted to quoted form as specified by
+ * RFC7230#section-3.2.6 and RFC7694#quoted.strings.
+ *
+ * @param unquoted the unquoted string, does not need to be zero-terminated
+ * @param unquoted_len the length in chars of the @a unquoted string
+ * @param[out] result the pointer to the buffer to put the result. May be
+ *                    modified even if function failed due to insufficient
+ *                    space. The result is NOT zero-terminated and does not
+ *                    have opening and closing DQUOTE chars.
+ * @param buf_size the size of the allocated memory for @a result
+ * @return The number of copied characters, can be up to two times more than
+ *         @a unquoted_len, zero if @a unquoted_len is zero or if quoted
+ *         string is larger than @a buf_size.
+ */
+size_t
+MHD_str_quote (const char *unquoted,
+               size_t unquoted_len,
+               char *result,
+               size_t buf_size);
+
+#endif /* DAUTH_SUPPORT || BAUTH_SUPPORT */
+
 #endif /* MHD_STR_H */
diff --git a/src/microhttpd/test_str_quote.c b/src/microhttpd/test_str_quote.c
index 2c3d942f..1ad66975 100644
--- a/src/microhttpd/test_str_quote.c
+++ b/src/microhttpd/test_str_quote.c
@@ -50,10 +50,12 @@ expect_result_unquote_n (const char *const quoted, const 
size_t quoted_len,
   unsigned int ret1;
   unsigned int ret2;
   unsigned int ret3;
+  unsigned int ret4;
 
   mhd_assert (NULL != quoted);
   mhd_assert (NULL != unquoted);
   mhd_assert (TEST_STR_MAX_LEN > quoted_len);
+  mhd_assert (quoted_len >= unquoted_len);
 
   /* First check: MHD_str_unquote () */
   ret1 = 0;
@@ -66,7 +68,7 @@ expect_result_unquote_n (const char *const quoted, const 
size_t quoted_len,
     fprintf (stderr,
              "'MHD_str_unquote ()' FAILED: Wrong result size:\n");
   }
-  else if (0 != memcmp (buf, unquoted, unquoted_len))
+  else if ((0 != unquoted_len) && (0 != memcmp (buf, unquoted, unquoted_len)))
   {
     ret1 = 1;
     fprintf (stderr,
@@ -129,7 +131,38 @@ expect_result_unquote_n (const char *const quoted, const 
size_t quoted_len,
     ret3 = 1;
   }
 
-  return ret1 + ret2 + ret3;
+  /* Fourth check: MHD_str_unquote () */
+  ret4 = 0;
+  memset (buf, '#', sizeof(buf)); /* Fill buffer with character unused in the 
check */
+  res_len = MHD_str_quote (unquoted, unquoted_len, buf, quoted_len);
+  if (res_len != quoted_len)
+  {
+    ret4 = 1;
+    fprintf (stderr,
+             "'MHD_str_quote ()' FAILED: Wrong result size:\n");
+  }
+  else if ((0 != quoted_len) && (0 != memcmp (buf, quoted, quoted_len)))
+  {
+    ret4 = 1;
+    fprintf (stderr,
+             "'MHD_str_quote ()' FAILED: Wrong result string:\n");
+  }
+  if (0 != ret4)
+  {
+    /* This does NOT print part of the string after binary zero */
+    fprintf (stderr,
+             "\tRESULT  : MHD_str_quote('%.*s', %u, ->'%.*s', %u) -> %u\n"
+             "\tEXPECTED: MHD_str_quote('%.*s', %u, ->'%.*s', %u) -> %u\n",
+             (int) unquoted_len, unquoted, (unsigned) unquoted_len,
+             (int) res_len, buf, (unsigned) quoted_len, (unsigned) res_len,
+             (int) unquoted_len, unquoted, (unsigned) unquoted_len,
+             (int) quoted_len, quoted, (unsigned) quoted_len,
+             (unsigned) unquoted_len);
+    fprintf (stderr,
+             "The check is at line: %u\n\n", line_num);
+  }
+
+  return ret1 + ret2 + ret3 + ret4;
 }
 
 
@@ -165,6 +198,89 @@ check_match (void)
 }
 
 
+/* return zero if succeed, non-zero otherwise */
+static unsigned int
+expect_result_quote_failed_n (const char *const unquoted,
+                              const size_t unquoted_len,
+                              const size_t buf_size,
+                              const unsigned int line_num)
+{
+  static char buf[TEST_STR_MAX_LEN];
+  size_t res_len;
+  unsigned int ret4;
+
+  mhd_assert (TEST_STR_MAX_LEN > buf_size);
+
+  /* The check: MHD_str_unquote () */
+  ret4 = 0;
+  memset (buf, '#', sizeof(buf)); /* Fill buffer with character unused in the 
check */
+  res_len = MHD_str_quote (unquoted, unquoted_len, buf, buf_size);
+  if (0 != res_len)
+  {
+    ret4 = 1;
+    fprintf (stderr,
+             "'MHD_str_quote ()' FAILED: Wrong result size:\n");
+  }
+  if (0 != ret4)
+  {
+    /* This does NOT print part of the string after binary zero */
+    fprintf (stderr,
+             "\tRESULT  : MHD_str_quote('%.*s', %u, ->'%.*s', %u) -> %u\n"
+             "\tEXPECTED: MHD_str_quote('%.*s', %u, (not checked), %u) -> 0\n",
+             (int) unquoted_len, unquoted, (unsigned) unquoted_len,
+             (int) res_len, buf,
+             (unsigned) buf_size, (unsigned) res_len,
+             (int) unquoted_len, unquoted, (unsigned) unquoted_len,
+             (unsigned) buf_size);
+    fprintf (stderr,
+             "The check is at line: %u\n\n", line_num);
+  }
+
+  return ret4;
+}
+
+
+#define expect_result_quote_failed(q,s) \
+    expect_result_quote_failed_n(q,MHD_STATICSTR_LEN_(q),\
+                            s,__LINE__)
+
+
+static unsigned int
+check_quote_failed (void)
+{
+  unsigned int r = 0; /**< The number of errors */
+
+  r += expect_result_quote_failed ("a", 0);
+  r += expect_result_quote_failed ("aa", 1);
+  r += expect_result_quote_failed ("abc\\", 4);
+  r += expect_result_quote_failed ("abc\"", 4);
+  r += expect_result_quote_failed ("abc\"\"\"\"", 6);
+  r += expect_result_quote_failed ("abc\"\"\"\"", 7);
+  r += expect_result_quote_failed ("abc\"\"\"\"", 8);
+  r += expect_result_quote_failed ("abc\"\"\"\"", 9);
+  r += expect_result_quote_failed ("abc\"\"\"\"", 10);
+  r += expect_result_quote_failed ("abc\\\\\\\\", 9);
+  r += expect_result_quote_failed ("abc\\\\\\\\", 10);
+  r += expect_result_quote_failed ("abc\"\"\"\"", 9);
+  r += expect_result_quote_failed ("abc\"\"\"\"", 10);
+  r += expect_result_quote_failed ("abc\"\\\"\\", 9);
+  r += expect_result_quote_failed ("abc\\\"\\\"", 10);
+  r += expect_result_quote_failed ("\"\"\"\"abc", 6);
+  r += expect_result_quote_failed ("\"\"\"\"abc", 7);
+  r += expect_result_quote_failed ("\"\"\"\"abc", 8);
+  r += expect_result_quote_failed ("\"\"\"\"abc", 9);
+  r += expect_result_quote_failed ("\"\"\"\"abc", 10);
+  r += expect_result_quote_failed ("\\\\\\\\abc", 9);
+  r += expect_result_quote_failed ("\\\\\\\\abc", 10);
+  r += expect_result_quote_failed ("\"\"\"\"abc", 9);
+  r += expect_result_quote_failed ("\"\"\"\"abc", 10);
+  r += expect_result_quote_failed ("\"\\\"\\abc", 9);
+  r += expect_result_quote_failed ("\\\"\\\"abc", 10);
+
+  return r;
+}
+
+
 /* return zero if succeed, one otherwise */
 static unsigned int
 expect_match_caseless_n (const char *const quoted, const size_t quoted_len,
@@ -670,6 +786,7 @@ main (int argc, char *argv[])
   unsigned int errcount = 0;
   (void) argc; (void) argv; /* Unused. Silent compiler warning. */
   errcount += check_match ();
+  errcount += check_quote_failed ();
   errcount += check_match_caseless ();
   errcount += check_invalid ();
   errcount += check_unmatch ();

-- 
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]