summaryrefslogtreecommitdiff
path: root/lib/util/hex_decode.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-11-16 15:41:57 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-11-18 00:58:07 +0100
commitda6eadc840716eb29b0175f39b2790bba166db4a (patch)
tree2da0a94126432e7af409ed809ea3476a1e44597c /lib/util/hex_decode.c
parent0f68ca18f491b4e53cec788b327a752cbeb43377 (diff)
Add a single, central hex blob decoder
Since we need it twice (once for tar, once for the filemap xattr parser), add a single, central implementation to libutil, add a unit test for that implementation and then use it in both libtar and gensquashfs. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/util/hex_decode.c')
-rw-r--r--lib/util/hex_decode.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/util/hex_decode.c b/lib/util/hex_decode.c
new file mode 100644
index 0000000..ee4b21c
--- /dev/null
+++ b/lib/util/hex_decode.c
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * hex_decode.h
+ *
+ * Copyright (C) 2022 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "util/util.h"
+
+#include <ctype.h>
+
+static sqfs_u8 xdigit(int in)
+{
+ if (isupper(in))
+ return in - 'A' + 10;
+ if (islower(in))
+ return in - 'a' + 10;
+ return in - '0';
+}
+
+int hex_decode(const char *in, size_t in_sz, sqfs_u8 *out, size_t out_sz)
+{
+ while (out_sz > 0 && in_sz >= 2 &&
+ isxdigit(in[0]) && isxdigit(in[1])) {
+ sqfs_u8 hi = xdigit(*(in++));
+ sqfs_u8 lo = xdigit(*(in++));
+
+ *(out++) = (hi << 4) | lo;
+
+ in_sz -= 2;
+ --out_sz;
+ }
+
+ return (in_sz > 0) ? -1 : 0;
+}