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 --- bin/gensquashfs/filemap_xattr.c | 43 ++++++----------------------------------- 1 file changed, 6 insertions(+), 37 deletions(-) (limited to 'bin') diff --git a/bin/gensquashfs/filemap_xattr.c b/bin/gensquashfs/filemap_xattr.c index 44ed9de..059ce81 100644 --- a/bin/gensquashfs/filemap_xattr.c +++ b/bin/gensquashfs/filemap_xattr.c @@ -10,19 +10,6 @@ #define NEW_FILE_START "# file: " -// Taken from attr-2.5.1/tools/setfattr.c -static int -hex_digit(char c) { - if (c >= '0' && c <= '9') - return c - '0'; - else if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - else if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - else - return -1; -} - // Taken from attr-2.5.1/tools/setfattr.c static int base64_digit(char c) { @@ -50,36 +37,18 @@ decode(const char *value, size_t *size) { if (*size == 0) return strdup(""); if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) { - const char *v = value + 2, *end = value + *size; - char *d; + *size = ((*size) - 2) / 2; - decoded = realloc(decoded, *size / 2); + decoded = realloc(decoded, *size); if (decoded == NULL) { return NULL; } - d = decoded; - while (v < end) { - int d1, d0; - while (v < end && isspace(*v)) - v++; - if (v == end) - break; - d1 = hex_digit(*v++); - while (v < end && isspace(*v)) - v++; - if (v == end) { - bad_hex_encoding: - fprintf(stderr, "bad input encoding\n"); - free(decoded); - return NULL; - } - d0 = hex_digit(*v++); - if (d1 < 0 || d0 < 0) - goto bad_hex_encoding; - *d++ = ((d1 << 4) | d0); + if (hex_decode(value + 2, (*size) * 2, decoded, *size)) { + fprintf(stderr, "bad input encoding\n"); + free(decoded); + return NULL; } - *size = d - decoded; } else if (value[0] == '0' && (value[1] == 's' || value[1] == 'S')) { const char *v = value + 2, *end = value + *size; int d0, d1, d2, d3; -- cgit v1.2.3