aboutsummaryrefslogtreecommitdiff
path: root/lib/util
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-11-18 00:57:01 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-11-18 00:58:13 +0100
commit844fdd42f03a633f1dbce5d90b2ecf44698cf8b0 (patch)
tree1255d42327bb297b12dfd4e7adfd81e3fbea6762 /lib/util
parentda6eadc840716eb29b0175f39b2790bba166db4a (diff)
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>
Diffstat (limited to 'lib/util')
-rw-r--r--lib/util/Makemodule.am1
-rw-r--r--lib/util/base64_decode.c103
2 files changed, 104 insertions, 0 deletions
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;
+}