ofp-util: Refactor decoding of OpenFlow 1.1 group mod messages.
[cascardo/ovs.git] / lib / util.c
index 01ba6bc..fb2ff51 100644 (file)
@@ -43,7 +43,7 @@ VLOG_DEFINE_THIS_MODULE(util);
 COVERAGE_DEFINE(util_xalloc);
 
 /* argv[0] without directory names. */
-const char *program_name;
+char *program_name;
 
 /* Name for the currently running thread or process, for log messages, process
  * listings, and debuggers. */
@@ -455,26 +455,23 @@ void
 set_program_name__(const char *argv0, const char *version, const char *date,
                    const char *time)
 {
-#ifdef _WIN32
     char *basename;
+#ifdef _WIN32
     size_t max_len = strlen(argv0) + 1;
 
     SetErrorMode(GetErrorMode() | SEM_NOGPFAULTERRORBOX);
     _set_output_format(_TWO_DIGIT_EXPONENT);
 
-    if (program_name) {
-        free(program_name);
-    }
     basename = xmalloc(max_len);
     _splitpath_s(argv0, NULL, 0, NULL, 0, basename, max_len, NULL, 0);
-    assert_single_threaded();
-    program_name = basename;
 #else
     const char *slash = strrchr(argv0, '/');
-    assert_single_threaded();
-    program_name = slash ? slash + 1 : argv0;
+    basename = xstrdup(slash ? slash + 1 : argv0);
 #endif
 
+    assert_single_threaded();
+    free(program_name);
+    program_name = basename;
     free(program_version);
 
     if (!strcmp(version, VERSION)) {
@@ -704,29 +701,25 @@ hexit_value(int c)
 }
 
 /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or
- * UINT_MAX if one of those "digits" is not really a hex digit.  If 'ok' is
- * nonnull, '*ok' is set to true if the conversion succeeds or to false if a
- * non-hex digit is detected. */
-unsigned int
+ * UINTMAX_MAX if one of those "digits" is not really a hex digit.  Sets '*ok'
+ * to true if the conversion succeeds or to false if a non-hex digit is
+ * detected. */
+uintmax_t
 hexits_value(const char *s, size_t n, bool *ok)
 {
-    unsigned int value;
+    uintmax_t value;
     size_t i;
 
     value = 0;
     for (i = 0; i < n; i++) {
         int hexit = hexit_value(s[i]);
         if (hexit < 0) {
-            if (ok) {
-                *ok = false;
-            }
-            return UINT_MAX;
+            *ok = false;
+            return UINTMAX_MAX;
         }
         value = (value << 4) + hexit;
     }
-    if (ok) {
-        *ok = true;
-    }
+    *ok = true;
     return value;
 }
 
@@ -952,7 +945,7 @@ english_list_delimiter(size_t index, size_t total)
 }
 
 /* Returns the number of trailing 0-bits in 'n'.  Undefined if 'n' == 0. */
-#if __GNUC__ >= 4
+#if __GNUC__ >= 4 || _MSC_VER
 /* Defined inline in util.h. */
 #else
 /* Returns the number of trailing 0-bits in 'n'.  Undefined if 'n' == 0. */
@@ -1028,8 +1021,9 @@ const uint8_t count_1bits_8[256] = {
 
 /* Returns true if the 'n' bytes starting at 'p' are zeros. */
 bool
-is_all_zeros(const uint8_t *p, size_t n)
+is_all_zeros(const void *p_, size_t n)
 {
+    const uint8_t *p = p_;
     size_t i;
 
     for (i = 0; i < n; i++) {
@@ -1042,8 +1036,9 @@ is_all_zeros(const uint8_t *p, size_t n)
 
 /* Returns true if the 'n' bytes starting at 'p' are 0xff. */
 bool
-is_all_ones(const uint8_t *p, size_t n)
+is_all_ones(const void *p_, size_t n)
 {
+    const uint8_t *p = p_;
     size_t i;
 
     for (i = 0; i < n; i++) {
@@ -1274,6 +1269,35 @@ bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs,
     return true;
 }
 
+/* Scans the bits in 'p' that have bit offsets 'start' through 'end'
+ * (inclusive) for the first bit with value 'target'.  If one is found, returns
+ * its offset, otherwise 'end'.  'p' is 'len' bytes long.
+ *
+ * If you consider all of 'p' to be a single unsigned integer in network byte
+ * order, then bit N is the bit with value 2**N.  That is, bit 0 is the bit
+ * with value 1 in p[len - 1], bit 1 is the bit with value 2, bit 2 is the bit
+ * with value 4, ..., bit 8 is the bit with value 1 in p[len - 2], and so on.
+ *
+ * Required invariant:
+ *   start <= end
+ */
+unsigned int
+bitwise_scan(const void *p_, unsigned int len, bool target, unsigned int start,
+             unsigned int end)
+{
+    const uint8_t *p = p_;
+    unsigned int ofs;
+
+    for (ofs = start; ofs < end; ofs++) {
+        bool bit = (p[len - (ofs / 8 + 1)] & (1u << (ofs % 8))) != 0;
+        if (bit == target) {
+            break;
+        }
+    }
+    return ofs;
+}
+
+
 /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits
  * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long.
  *