From daa8a0670ae2bf577718dd15cccfd7b389d94be6 Mon Sep 17 00:00:00 2001
From: Pino Toscano <ptoscano@redhat.com>
Date: Thu, 6 Feb 2014 15:57:09 +0100
Subject: [PATCH] utils: add a function to validate a GUID string
(cherry picked from commit beef77403cd9d634b6ff6daa9f33d292e2d320a7)
---
src/guestfs-internal.h | 3 +++
src/utils.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 47 insertions(+)
diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index d789dcc..61236eb 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -767,4 +767,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/utils.c b/src/utils.c
index f8d2509..3d0eb8e 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
@@ -187,3 +190,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;
+}
--
1.8.3.1