From da6eadc840716eb29b0175f39b2790bba166db4a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 16 Nov 2022 15:41:57 +0100 Subject: 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 --- lib/tar/internal.h | 1 + lib/tar/pax_header.c | 16 +++------------- lib/util/Makemodule.am | 1 + lib/util/hex_decode.c | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 lib/util/hex_decode.c (limited to 'lib') diff --git a/lib/tar/internal.h b/lib/tar/internal.h index cfbb938..c5483fe 100644 --- a/lib/tar/internal.h +++ b/lib/tar/internal.h @@ -11,6 +11,7 @@ #include "tar/tar.h" #include "tar/format.h" +#include "util/util.h" enum { PAX_SIZE = 0x001, diff --git a/lib/tar/pax_header.c b/lib/tar/pax_header.c index 8a1b077..cc2f2b0 100644 --- a/lib/tar/pax_header.c +++ b/lib/tar/pax_header.c @@ -26,15 +26,6 @@ static sqfs_u8 base64_convert(char in) return 0; } -static int xdigit(int x) -{ - if (isupper(x)) - return x - 'A' + 0x0A; - if (islower(x)) - return x - 'a' + 0x0A; - return x - '0'; -} - static size_t base64_decode(sqfs_u8 *out, const char *in, size_t len) { sqfs_u8 *start = out; @@ -91,14 +82,13 @@ static void urldecode(char *str) { unsigned char *out = (unsigned char *)str; char *in = str; - int x; while (*in != '\0') { - x = *(in++); + sqfs_u8 x = *(in++); if (x == '%' && isxdigit(in[0]) && isxdigit(in[1])) { - x = xdigit(*(in++)) << 4; - x |= xdigit(*(in++)); + hex_decode(in, 2, &x, 1); + in += 2; } *(out++) = x; diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index 830a338..4f1f0da 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -14,6 +14,7 @@ libutil_a_SOURCES += lib/util/canonicalize_name.c libutil_a_SOURCES += lib/util/filename_sane.c libutil_a_SOURCES += lib/util/source_date_epoch.c libutil_a_SOURCES += lib/util/file_cmp.c +libutil_a_SOURCES += lib/util/hex_decode.c libutil_a_CFLAGS = $(AM_CFLAGS) libutil_a_CPPFLAGS = $(AM_CPPFLAGS) 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 + */ +#include "util/util.h" + +#include + +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; +} -- cgit v1.2.3