gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r37025 - libmicrohttpd/src/microhttpd


From: gnunet
Subject: [GNUnet-SVN] r37025 - libmicrohttpd/src/microhttpd
Date: Tue, 12 Apr 2016 13:01:36 +0200

Author: Karlson2k
Date: 2016-04-12 13:01:36 +0200 (Tue, 12 Apr 2016)
New Revision: 37025

Modified:
   libmicrohttpd/src/microhttpd/connection.c
   libmicrohttpd/src/microhttpd/mhd_str.c
   libmicrohttpd/src/microhttpd/mhd_str.h
Log:
mhd_str: refactored MHD_str_to_uint64() and MHD_str_to_uint64_n_() for 
simplification, more
error checking and optimization

Modified: libmicrohttpd/src/microhttpd/connection.c
===================================================================
--- libmicrohttpd/src/microhttpd/connection.c   2016-04-11 20:09:39 UTC (rev 
37024)
+++ libmicrohttpd/src/microhttpd/connection.c   2016-04-12 11:01:36 UTC (rev 
37025)
@@ -2100,9 +2100,9 @@
                                          MHD_HTTP_HEADER_CONTENT_LENGTH);
       if (NULL != clen)
         {
-          if (!MHD_str_to_uint64_ (clen, &connection->remaining_upload_size,
-                                   &end) ||
-              ('\0' != *end) )
+          end = clen + MHD_str_to_uint64_ (clen,
+                                           &connection->remaining_upload_size);
+          if ( (clen == end) || ('\0' != *end) )
             {
               connection->remaining_upload_size = 0;
 #ifdef HAVE_MESSAGES

Modified: libmicrohttpd/src/microhttpd/mhd_str.c
===================================================================
--- libmicrohttpd/src/microhttpd/mhd_str.c      2016-04-11 20:09:39 UTC (rev 
37024)
+++ libmicrohttpd/src/microhttpd/mhd_str.c      2016-04-12 11:01:36 UTC (rev 
37025)
@@ -237,47 +237,33 @@
  * Conversion stopped at first non-digit character.
  * @param str string to convert
  * @param out_val pointer to uint64_t to store result of conversion
- * @param next_char pointer to store pointer to character next to last
- *                  converted digit, ignored if NULL
- * @return non-zero if conversion succeed; zero if no digit is found,
- *         value is larger then possible to store in uint64_t or
- *         @a out_val or @a str is NULL
+ * @return non-zero number of characters processed on succeed,
+ *         zero if no digit is found, resulting value is larger
+ *         then possible to store in uint64_t or @a out_val is NULL
  */
-int 
-MHD_str_to_uint64_ (const char * str, uint64_t * out_val, const char ** 
next_char)
+size_t 
+MHD_str_to_uint64_ (const char * str, uint64_t * out_val)
 {
+  const char * const start = str;
   uint64_t res;
-  if (!str || !isasciidigit(str[0]))
-    {
-      if (next_char)
-        *next_char = str;
-      return 0;
-    }
+  if (!str || !out_val || !isasciidigit(str[0]))
+    return 0;
 
   res = 0;
   do
     {
-      const int digit = str[0] - '0';
-      if ( (res < (UINT64_MAX / 10)) ||
-           (res == (UINT64_MAX / 10) && digit <= (UINT64_MAX % 10)) )
-        {
-          res *= 10;
-          res += digit;
-        }
-      else
-        {
-          if (next_char)
-            *next_char = str;
-          return 0;
-        }
+      const int digit = (unsigned char)(*str) - '0';
+      if ( (res > (UINT64_MAX / 10)) ||
+           (res == (UINT64_MAX / 10) && digit > (UINT64_MAX % 10)) )
+        return 0;
+
+      res *= 10;
+      res += digit;
       str++;
-    } while (isasciidigit (str[0]));
+    } while (isasciidigit (*str));
 
   *out_val = res;
-  if (next_char)
-    *next_char = str;
-
-  return !0;
+  return str - start;
 }
 
 
@@ -287,49 +273,34 @@
  * Conversion stopped at first non-digit character or after @a maxlen 
  * digits.
  * @param str string to convert
+ * @param maxlen maximum number of characters to process
  * @param out_val pointer to uint64_t to store result of conversion
- * @param next_char pointer to store pointer to character next to last
- *                  converted digit, ignored if NULL
- * @return non-zero if conversion succeed; zero if no digit is found,
- *         value is larger then possible to store in uint64_t or
- *         @a out_val is NULL
+ * @return non-zero number of characters processed on succeed,
+ *         zero if no digit is found, resulting value is larger
+ *         then possible to store in uint64_t or @a out_val is NULL
  */
-int
-MHD_str_to_uint64_n_ (const char * str, size_t maxlen, uint64_t * out_val,
-                      const char ** next_char)
+size_t
+MHD_str_to_uint64_n_ (const char * str, size_t maxlen, uint64_t * out_val)
 {
   uint64_t res;
   size_t i;
-  if (!str || !maxlen || !isasciidigit (str[0]))
-    {
-      if (next_char)
-        *next_char = str;
-      return 0;
-    }
+  if (!str || !maxlen || !out_val || !isasciidigit (str[0]))
+    return 0;
 
   res = 0;
   i = 0;
   do
     {
-      const int digit = str[i] - '0';
-      if ( (res < (UINT64_MAX / 10)) ||
-           (res == (UINT64_MAX / 10) && digit <= (UINT64_MAX % 10)) )
-        {
-          res *= 10;
-          res += digit;
-        }
-      else
-        {
-          if (next_char)
-            *next_char = str + i;
-          return 0;
-        }
-      
+      const int digit = (unsigned char)str[i] - '0';
+      if ( (res > (UINT64_MAX / 10)) ||
+           (res == (UINT64_MAX / 10) && digit > (UINT64_MAX % 10)) )
+        return 0;
+
+      res *= 10;
+      res += digit;
+      i++;      
     } while(i < maxlen && isasciidigit(str[i]));
 
-  *out_val = res;
-  if (next_char)
-    *next_char = str + i;
-
-  return !0;
+  *out_val= res;
+  return i;
 }

Modified: libmicrohttpd/src/microhttpd/mhd_str.h
===================================================================
--- libmicrohttpd/src/microhttpd/mhd_str.h      2016-04-11 20:09:39 UTC (rev 
37024)
+++ libmicrohttpd/src/microhttpd/mhd_str.h      2016-04-12 11:01:36 UTC (rev 
37025)
@@ -65,16 +65,13 @@
  * Conversion stopped at first non-digit character.
  * @param str string to convert
  * @param out_val pointer to uint64_t to store result of conversion
- * @param next_char pointer to store pointer to character next to last
- *                  converted digit, ignored if NULL
- * @return non-zero if conversion succeed; zero if no digit is found,
- *         value is larger then possible to store in uint64_t or
- *         @a out_val is NULL
+ * @return non-zero number of characters processed on succeed,
+ *         zero if no digit is found, resulting value is larger
+ *         then possible to store in uint64_t or @a out_val is NULL
  */
-int
+size_t
 MHD_str_to_uint64_ (const char * str,
-                    uint64_t * out_val,
-                    const char ** next_char);
+                    uint64_t * out_val);
 
 /**
  * Convert not more then @a maxlen decimal US-ASCII digits in string to
@@ -82,17 +79,15 @@
  * Conversion stopped at first non-digit character or after @a maxlen 
  * digits.
  * @param str string to convert
+ * @param maxlen maximum number of characters to process
  * @param out_val pointer to uint64_t to store result of conversion
- * @param next_char pointer to store pointer to character next to last
- *                  converted digit, ignored if NULL
- * @return non-zero if conversion succeed; zero if no digit is found,
- *         value is larger then possible to store in uint64_t or
- *         @a out_val is NULL
+ * @return non-zero number of characters processed on succeed,
+ *         zero if no digit is found, resulting value is larger
+ *         then possible to store in uint64_t or @a out_val is NULL
  */
-int
+size_t
 MHD_str_to_uint64_n_ (const char * str,
                       size_t maxlen,
-                      uint64_t * out_val,
-                      const char ** next_char);
+                      uint64_t * out_val);
 
 #endif /* MHD_STR_H */




reply via email to

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