aboutsummaryrefslogtreecommitdiffstats
path: root/tools/card-tool-misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/card-tool-misc.c')
-rw-r--r--tools/card-tool-misc.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/tools/card-tool-misc.c b/tools/card-tool-misc.c
index 0f5fcc0a0..06fcb6705 100644
--- a/tools/card-tool-misc.c
+++ b/tools/card-tool-misc.c
@@ -42,3 +42,38 @@ find_kinfo (card_info_t info, const char *keyref)
return kinfo;
return NULL;
}
+
+
+/* Convert STRING into a newly allocated buffer while translating the
+ * hex numbers. Blanks and colons are allowed to separate pairs of
+ * hex digits. Returns NULL on error or a newly malloced buffer and
+ * its length in LENGTH. */
+void *
+hex_to_buffer (const char *string, size_t *r_length)
+{
+ unsigned char *buffer;
+ const char *s;
+ size_t n;
+
+ buffer = xtrymalloc (strlen (string)+1);
+ if (!buffer)
+ return NULL;
+ for (s=string, n=0; *s; s++)
+ {
+ if (ascii_isspace (*s) || *s == ':')
+ continue;
+ if (hexdigitp (s) && hexdigitp (s+1))
+ {
+ buffer[n++] = xtoi_2 (s);
+ s++;
+ }
+ else
+ {
+ xfree (buffer);
+ gpg_err_set_errno (EINVAL);
+ return NULL;
+ }
+ }
+ *r_length = n;
+ return buffer;
+}