From 844fdd42f03a633f1dbce5d90b2ecf44698cf8b0 Mon Sep 17 00:00:00 2001
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Date: Fri, 18 Nov 2022 00:57:01 +0100
Subject: Add a single, central base64 decoder

Similar to the hex blob decoder, we need this once for tar and
once for the filemap xattr parser.

Simply add a single, central implementation to libutil, with a
simple unit test, and then use it in both libtar and gensquashfs.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 lib/tar/pax_header.c     |  62 +++++-----------------------
 lib/util/Makemodule.am   |   1 +
 lib/util/base64_decode.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+), 53 deletions(-)
 create mode 100644 lib/util/base64_decode.c

(limited to 'lib')

diff --git a/lib/tar/pax_header.c b/lib/tar/pax_header.c
index cc2f2b0..b61aab6 100644
--- a/lib/tar/pax_header.c
+++ b/lib/tar/pax_header.c
@@ -11,56 +11,6 @@
 #include <string.h>
 #include <stdlib.h>
 
-static sqfs_u8 base64_convert(char in)
-{
-	if (isupper(in))
-		return in - 'A';
-	if (islower(in))
-		return in - 'a' + 26;
-	if (isdigit(in))
-		return in - '0' + 52;
-	if (in == '+')
-		return 62;
-	if (in == '/' || in == '-')
-		return 63;
-	return 0;
-}
-
-static size_t base64_decode(sqfs_u8 *out, const char *in, size_t len)
-{
-	sqfs_u8 *start = out;
-
-	while (len > 0) {
-		unsigned int diff = 0, value = 0;
-
-		while (diff < 4 && len > 0) {
-			if (*in == '=' || *in == '_' || *in == '\0') {
-				len = 0;
-			} else {
-				value = (value << 6) | base64_convert(*(in++));
-				--len;
-				++diff;
-			}
-		}
-
-		if (diff < 2)
-			break;
-
-		value <<= 6 * (4 - diff);
-
-		switch (diff) {
-		case 4:  out[2] = value & 0xff; /* fall-through */
-		case 3:  out[1] = (value >> 8) & 0xff; /* fall-through */
-		default: out[0] = (value >> 16) & 0xff;
-		}
-
-		out += (diff * 3) / 4;
-	}
-
-	*out = '\0';
-	return out - start;
-}
-
 static int pax_read_decimal(const char *str, sqfs_u64 *out)
 {
 	sqfs_u64 result = 0;
@@ -201,10 +151,16 @@ static int pax_xattr_schily(tar_header_decoded_t *out,
 static int pax_xattr_libarchive(tar_header_decoded_t *out,
 				tar_xattr_t *xattr)
 {
+	int ret;
+
+	ret = base64_decode((const char *)xattr->value, xattr->value_len,
+			    xattr->value, &xattr->value_len);
+	if (ret)
+		return -1;
+
 	urldecode(xattr->key);
-	xattr->value_len = base64_decode(xattr->value,
-					 (const char *)xattr->value,
-					 xattr->value_len);
+
+	xattr->value[xattr->value_len] = '\0';
 	xattr->next = out->xattr;
 	out->xattr = xattr;
 	return 0;
diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am
index 4f1f0da..ec38b7a 100644
--- a/lib/util/Makemodule.am
+++ b/lib/util/Makemodule.am
@@ -15,6 +15,7 @@ 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_SOURCES += lib/util/base64_decode.c
 libutil_a_CFLAGS = $(AM_CFLAGS)
 libutil_a_CPPFLAGS = $(AM_CPPFLAGS)
 
diff --git a/lib/util/base64_decode.c b/lib/util/base64_decode.c
new file mode 100644
index 0000000..b1cf5b6
--- /dev/null
+++ b/lib/util/base64_decode.c
@@ -0,0 +1,103 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * base64_decode.c
+ *
+ * Copyright (C) 2022 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+#include "util/util.h"
+#include "util/test.h"
+
+#include <ctype.h>
+
+static int base64_digit(int c)
+{
+	if (isupper(c))
+		return c - 'A';
+	if (islower(c))
+		return c - 'a' + 26;
+	if (isdigit(c))
+		return c - '0' + 52;
+	if (c == '+')
+		return 62;
+	if (c == '/' || c == '-')
+		return 63;
+	return -1;
+}
+
+int base64_decode(const char *in, size_t in_len, sqfs_u8 *out, size_t *out_len)
+{
+	int i1, i2, i3, i4;
+	size_t count = 0;
+
+	while (in_len >= 4) {
+		i1 = base64_digit(*(in++));
+		i2 = base64_digit(*(in++));
+		i3 = *(in++);
+		i4 = *(in++);
+		in_len -= 4;
+
+		if (i1 < 0 || i2 < 0 || count >= *out_len)
+			goto fail;
+
+		out[count++] = (i1 << 2) | (i2 >> 4);
+
+		if (i3 == '=' || i3 == '_') {
+			if ((i4 != '=' && i4 != '_') || in_len > 0)
+				goto fail;
+			break;
+		}
+
+		i3 = base64_digit(i3);
+		if (i3 < 0 || count >= *out_len)
+			goto fail;
+
+		out[count++] = ((i2 & 0x0F) << 4) | (i3 >> 2);
+
+		if (i4 == '=' || i4 == '_') {
+			if (in_len > 0)
+				goto fail;
+			break;
+		}
+
+		i4 = base64_digit(i4);
+		if (i4 < 0 || count >= *out_len)
+			goto fail;
+
+		out[count++] = ((i3 & 0x3) << 6) | i4;
+	}
+
+	/* libarchive has this bizarre bastardization of truncated base64 */
+	if (in_len > 0) {
+		if (in_len == 1)
+			goto fail;
+
+		i1 = base64_digit(*(in++));
+		i2 = base64_digit(*(in++));
+		in_len -= 2;
+
+		if (i1 < 0 || i2 < 0 || count >= *out_len)
+			goto fail;
+
+		out[count++] = (i1 << 2) | (i2 >> 4);
+
+		if (in_len > 0) {
+			i3 = *(in++);
+			--in_len;
+
+			if (i3 != '=' && i3 != '_') {
+				i3 = base64_digit(i3);
+				if (i3 < 0 || count >= *out_len)
+					goto fail;
+
+				out[count++] = ((i2 & 0x0F) << 4) | (i3 >> 2);
+			}
+		}
+	}
+
+	*out_len = count;
+	return 0;
+fail:
+	*out_len = 0;
+	return -1;
+}
-- 
cgit v1.2.3