]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
shift read_hex_bytes() and parse_guid_string() into lib/util
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Fri, 16 Feb 2018 04:53:15 +0000 (17:53 +1300)
committerDouglas Bagnall <dbagnall@samba.org>
Thu, 22 Feb 2018 00:04:18 +0000 (01:04 +0100)
read_hex_bytes() is going to be used in lib/util/rfc1738.c.

parse_guid_string() is shifted for two reasons: Firstly, it is called
very often in some operations, sometimes constituting a few percent of
the CPU load, and it makes several calls to read_hex_bytes(). We want
the compiler to be able to inline those calls if it thinks that is
wise. Secondly, there are other places that could do with fast GUID
parsing.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/util/util_str_hex.c [new file with mode: 0644]
lib/util/util_str_hex.h [new file with mode: 0644]
lib/util/wscript_build
librpc/ndr/uuid.c
librpc/wscript_build

diff --git a/lib/util/util_str_hex.c b/lib/util/util_str_hex.c
new file mode 100644 (file)
index 0000000..148735b
--- /dev/null
@@ -0,0 +1,100 @@
+#include "replace.h"
+#include "libcli/util/ntstatus.h"
+#include "util_str_hex.h"
+
+NTSTATUS read_hex_bytes(const char *s, uint hexchars, uint64_t *dest)
+{
+       uint64_t x = 0;
+       uint i;
+       char c;
+
+       if ((hexchars & 1) || hexchars > 16) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       for (i = 0; i < hexchars; i++) {
+               x <<= 4;
+               c = s[i];
+               if (c >= '0' && c <= '9') {
+                       x += c - '0';
+               }
+               else if (c >= 'a' && c <= 'f') {
+                       x += c - 'a' + 10;
+               }
+               else if (c >= 'A' && c <= 'F') {
+                       x += c - 'A' + 10;
+               }
+               else {
+                       /* BAD character (including '\0') */
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+       }
+       *dest = x;
+       return NT_STATUS_OK;
+}
+
+
+NTSTATUS parse_guid_string(const char *s,
+                          uint32_t *time_low,
+                          uint32_t *time_mid,
+                          uint32_t *time_hi_and_version,
+                          uint32_t *clock_seq,
+                          uint32_t *node)
+{
+       uint64_t tmp;
+       NTSTATUS status;
+       int i;
+       /* "e12b56b6-0a95-11d1-adbb-00c04fd8d5cd"
+                |     |    |    |    |
+                |     |    |    |    \ node[6]
+                |     |    |    \_____ clock_seq[2]
+                |     |    \__________ time_hi_and_version
+               |     \_______________ time_mid
+               \_____________________ time_low
+       */
+       status = read_hex_bytes(s, 8, &tmp);
+       if (!NT_STATUS_IS_OK(status) || s[8] != '-') {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+       *time_low = tmp;
+       s += 9;
+
+       status = read_hex_bytes(s, 4, &tmp);
+       if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+       *time_mid = tmp;
+       s += 5;
+
+       status = read_hex_bytes(s, 4, &tmp);
+       if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+       *time_hi_and_version = tmp;
+       s += 5;
+
+       for (i = 0; i < 2; i++) {
+               status = read_hex_bytes(s, 2, &tmp);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               clock_seq[i] = tmp;
+               s += 2;
+       }
+       if (s[0] != '-') {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       s++;
+
+       for (i = 0; i < 6; i++) {
+               status = read_hex_bytes(s, 2, &tmp);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               node[i] = tmp;
+               s += 2;
+       }
+
+       return NT_STATUS_OK;
+}
diff --git a/lib/util/util_str_hex.h b/lib/util/util_str_hex.h
new file mode 100644 (file)
index 0000000..2d0ba65
--- /dev/null
@@ -0,0 +1,10 @@
+#include "../libcli/util/ntstatus.h"
+
+NTSTATUS read_hex_bytes(const char *s, uint hexchars, uint64_t *dest);
+
+NTSTATUS parse_guid_string(const char *s,
+                          uint32_t *time_low,
+                          uint32_t *time_mid,
+                          uint32_t *time_hi_and_version,
+                          uint32_t *clock_seq,
+                          uint32_t *node);
index 30face34381a5384b560a601dfcf2c426e6f57c3..8dbe1636e3bfd1f0886c08148e59dea88ba44b85 100644 (file)
@@ -123,7 +123,7 @@ else:
                     util_str.c util_str_common.c ms_fnmatch.c
                     server_id.c dprintf.c
                     tevent_debug.c memcache.c unix_match.c tfork.c''',
-                  deps='samba-util-core DYNCONFIG close-low-fd tini tiniparser genrand',
+                  deps='samba-util-core DYNCONFIG close-low-fd tini tiniparser genrand util_str_hex',
                   public_deps='talloc tevent execinfo pthread LIBCRYPTO charset util_setid systemd systemd-daemon',
                   public_headers='debug.h attr.h byteorder.h data_blob.h memory.h safe_string.h time.h talloc_stack.h string_wrappers.h idtree.h idtree_random.h blocking.h signal.h substitute.h fault.h genrand.h tfork.h',
                   header_path= [ ('dlinklist.h samba_util.h', '.'), ('*', 'util') ],
@@ -211,3 +211,8 @@ else:
                         source='util_str_escape.c',
                         deps='talloc',
                         local_include=False)
+
+    bld.SAMBA_SUBSYSTEM('util_str_hex',
+                        source='util_str_hex.c',
+                        deps='talloc',
+                        local_include=False)
index 037dd909fce3e83c8de92334f433ed152a4b8889..53540c7ddc8ba4fef2e0a5ba783a6036b21fb1df 100644 (file)
@@ -24,7 +24,7 @@
 #include "includes.h"
 #include "librpc/ndr/libndr.h"
 #include "librpc/gen_ndr/ndr_misc.h"
-
+#include "lib/util/util_str_hex.h"
 /**
   build a NDR blob from a GUID
 */
@@ -52,102 +52,6 @@ _PUBLIC_ NTSTATUS GUID_from_ndr_blob(const DATA_BLOB *b, struct GUID *guid)
        return ndr_map_error2ntstatus(ndr_err);
 }
 
-static NTSTATUS read_hex_bytes(const char *s, uint hexchars, uint64_t *dest)
-{
-       uint64_t x = 0;
-       uint i;
-       char c;
-
-       if ((hexchars & 1) || hexchars > 16) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       for (i = 0; i < hexchars; i++) {
-               x <<= 4;
-               c = s[i];
-               if (c >= '0' && c <= '9') {
-                       x += c - '0';
-               }
-               else if (c >= 'a' && c <= 'f') {
-                       x += c - 'a' + 10;
-               }
-               else if (c >= 'A' && c <= 'F') {
-                       x += c - 'A' + 10;
-               }
-               else {
-                       /* BAD character (including '\0') */
-                       return NT_STATUS_INVALID_PARAMETER;
-               }
-       }
-       *dest = x;
-       return NT_STATUS_OK;
-}
-
-
-static NTSTATUS parse_guid_string(const char *s,
-                                 uint32_t *time_low,
-                                 uint32_t *time_mid,
-                                 uint32_t *time_hi_and_version,
-                                 uint32_t *clock_seq,
-                                 uint32_t *node)
-{
-       uint64_t tmp;
-       NTSTATUS status;
-       int i;
-       /* "e12b56b6-0a95-11d1-adbb-00c04fd8d5cd"
-                |     |    |    |    |
-                |     |    |    |    \ node[6]
-                |     |    |    \_____ clock_seq[2]
-                |     |    \__________ time_hi_and_version
-               |     \_______________ time_mid
-               \_____________________ time_low
-       */
-       status = read_hex_bytes(s, 8, &tmp);
-       if (!NT_STATUS_IS_OK(status) || s[8] != '-') {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-       *time_low = tmp;
-       s += 9;
-
-       status = read_hex_bytes(s, 4, &tmp);
-       if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-       *time_mid = tmp;
-       s += 5;
-
-       status = read_hex_bytes(s, 4, &tmp);
-       if (!NT_STATUS_IS_OK(status) || s[4] != '-') {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-       *time_hi_and_version = tmp;
-       s += 5;
-
-       for (i = 0; i < 2; i++) {
-               status = read_hex_bytes(s, 2, &tmp);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return NT_STATUS_INVALID_PARAMETER;
-               }
-               clock_seq[i] = tmp;
-               s += 2;
-       }
-       if (s[0] != '-') {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       s++;
-
-       for (i = 0; i < 6; i++) {
-               status = read_hex_bytes(s, 2, &tmp);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return NT_STATUS_INVALID_PARAMETER;
-               }
-               node[i] = tmp;
-               s += 2;
-       }
-
-       return NT_STATUS_OK;
-}
 
 /**
   build a GUID from a string
index c4b647c242de9ecafb360adc678de5be00415cb4..36414fbddf41673804da75659681cec574b818da 100644 (file)
@@ -717,7 +717,7 @@ bld.SAMBA_LIBRARY('dcerpc-samba',
 bld.SAMBA_LIBRARY('ndr',
     source='ndr/ndr_string.c ndr/ndr_basic.c ndr/uuid.c ndr/ndr.c ndr/ndr_misc.c gen_ndr/ndr_misc.c ndr/util.c',
     pc_files='ndr.pc',
-    public_deps='samba-errors talloc samba-util',
+    public_deps='samba-errors talloc samba-util util_str_hex',
     public_headers='gen_ndr/misc.h gen_ndr/ndr_misc.h ndr/libndr.h:ndr.h',
     header_path= [('*gen_ndr*', 'gen_ndr')],
     vnum='0.1.0',