summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2011-07-05 12:02:49 -0700
committerJon Loeliger <jdl@jdl.com>2011-07-17 07:42:26 -0500
commit492f9d5de7db74aeb3a905246c4efd7cb29227a8 (patch)
treeb745e6929817e98d47f1e53e4260040570fa3670 /util.c
parentd5b3165023b1cc3914e9943b91964ec9ad4be8b2 (diff)
downloaddevice-tree-compiler-492f9d5de7db74aeb3a905246c4efd7cb29227a8.tar.gz
Split out is_printable_string() into util.c
This useful function is split out so it will be available to programs other than ftdump. Signed-off-by: Simon Glass <sjg@chromium.org> Acked-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'util.c')
-rw-r--r--util.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/util.c b/util.c
index d7ac27d..994436f 100644
--- a/util.c
+++ b/util.c
@@ -1,6 +1,9 @@
/*
* Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
*
+ * util_is_printable_string contributed by
+ * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
@@ -17,6 +20,7 @@
* USA
*/
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
@@ -57,3 +61,27 @@ char *join_path(const char *path, const char *name)
memcpy(str+lenp, name, lenn+1);
return str;
}
+
+int util_is_printable_string(const void *data, int len)
+{
+ const char *s = data;
+ const char *ss;
+
+ /* zero length is not */
+ if (len == 0)
+ return 0;
+
+ /* must terminate with zero */
+ if (s[len - 1] != '\0')
+ return 0;
+
+ ss = s;
+ while (*s && isprint(*s))
+ s++;
+
+ /* not zero, or not done yet */
+ if (*s != '\0' || (s + 1 - ss) < len)
+ return 0;
+
+ return 1;
+}