[Pkg-libvirt-commits] [libguestfs] 75/87: utils: add a function to validate a GUID string

Hilko Bengen bengen at moszumanska.debian.org
Wed Feb 19 21:10:16 UTC 2014


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

bengen pushed a commit to branch debian
in repository libguestfs.

commit de3f9ce91eea110d9aa4ec6c25ecfc57b4d5efc8
Author: Pino Toscano <ptoscano at redhat.com>
Date:   Thu Feb 6 15:57:09 2014 +0100

    utils: add a function to validate a GUID string
    
    (cherry picked from commit beef77403cd9d634b6ff6daa9f33d292e2d320a7)
---
 src/guestfs-internal.h |  3 +++
 src/test-utils.c       | 14 ++++++++++++++
 src/utils.c            | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)

diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index 8888603..3501ed5 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -812,4 +812,7 @@ extern void guestfs___cmd_close (struct command *);
 #endif
 extern void guestfs___cleanup_cmd_close (struct command **);
 
+/* utils.c */
+extern int guestfs___validate_guid (const char *);
+
 #endif /* GUESTFS_INTERNAL_H_ */
diff --git a/src/test-utils.c b/src/test-utils.c
index 8ee2873..089d6b8 100644
--- a/src/test-utils.c
+++ b/src/test-utils.c
@@ -24,6 +24,7 @@
 #include <assert.h>
 
 #include "guestfs.h"
+#include "guestfs-internal.h"
 #include "guestfs-internal-frontend.h"
 
 /* Test guestfs___split_string. */
@@ -145,12 +146,25 @@ test_join (void)
   free (ret);
 }
 
+/* Test guestfs___validate_guid. */
+static void
+test_validate_guid (void)
+{
+  assert (guestfs___validate_guid ("") == 0);
+  assert (guestfs___validate_guid ("1") == 0);
+  assert (guestfs___validate_guid ("21EC20203AEA1069A2DD08002B30309D") == 0);
+
+  assert (guestfs___validate_guid ("{21EC2020-3AEA-1069-A2DD-08002B30309D}") == 1);
+  assert (guestfs___validate_guid ("21EC2020-3AEA-1069-A2DD-08002B30309D") == 1);
+}
+
 int
 main (int argc, char *argv[])
 {
   test_split ();
   test_concat ();
   test_join ();
+  test_validate_guid ();
 
   exit (EXIT_SUCCESS);
 }
diff --git a/src/utils.c b/src/utils.c
index 5eebdf6..45adc3d 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -28,7 +28,10 @@
 #include <sys/wait.h>
 #include <libintl.h>
 
+#include "c-ctype.h"
+
 #include "guestfs.h"
+#include "guestfs-internal.h"
 #include "guestfs-internal-frontend.h"
 
 /* Note that functions in libutils are used by the tools and language
@@ -268,3 +271,44 @@ guestfs___drive_name (size_t index, char *ret)
   *ret = '\0';
   return ret;
 }
+
+/* Check whether a string supposed to contain a GUID actually contains it.
+ * It can recognize strings either as '{21EC2020-3AEA-1069-A2DD-08002B30309D}'
+ * or '21EC2020-3AEA-1069-A2DD-08002B30309D'.
+ */
+int
+guestfs___validate_guid (const char *str)
+{
+  int len = strlen (str);
+  switch (len) {
+  case 36:
+    break;
+  case 38:
+    if (str[0] == '{' && str[len -1] == '}') {
+      ++str;
+      len -= 2;
+      break;
+    }
+    return 0;
+  default:
+    return 0;
+  }
+
+  for (int i = 0; i < len; ++i) {
+    switch (i) {
+    case 8:
+    case 13:
+    case 18:
+    case 23:
+      if (str[i] != '-')
+        return 0;
+      break;
+    default:
+      if (!c_isalnum (str[i]))
+        return 0;
+      break;
+    }
+  }
+
+  return 1;
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list