summaryrefslogtreecommitdiff
path: root/apt-pkg
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2024-11-23 19:32:06 +0100
committerJulian Andres Klode <jak@debian.org>2024-11-28 10:17:21 +0100
commit62cc071f88cb33c1f6213c9dbd54a10135c5ad34 (patch)
treeca58e0a3f52f11ac592154452458445b8f8648a8 /apt-pkg
parent0211fd6451bd67e5c590ac89c8696310ac8dd78c (diff)
strutl: Add Base64Decode
Lifted from the dpkg mtree code that mjg59 submitted in https://lists.debian.org/debian-dpkg/2018/05/msg00005.html Which as dpkg is GPL-2.0+ also is GPL-2.0+
Diffstat (limited to 'apt-pkg')
-rw-r--r--apt-pkg/contrib/strutl.cc67
-rw-r--r--apt-pkg/contrib/strutl.h1
2 files changed, 68 insertions, 0 deletions
diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc
index ec61c7e2b..69a67188e 100644
--- a/apt-pkg/contrib/strutl.cc
+++ b/apt-pkg/contrib/strutl.cc
@@ -625,6 +625,73 @@ string Base64Encode(const string &S)
return Final;
}
/*}}}*/
+// Base64Decode - Base64 decoding routine for short strings /*{{{*/
+// ---------------------------------------------------------------------
+// Taken from mjg59's dpkg code: https://lists.debian.org/debian-dpkg/2018/05/msg00002.html
+std::string Base64Decode(const std::string_view in)
+{
+ constexpr std::array<unsigned char, 256> mime_base64_rank{
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 0, 255, 255,
+ 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
+ 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
+ std::string out;
+ unsigned char rank;
+ unsigned char last[2];
+ unsigned int v;
+ int i;
+
+ /* convert 4 base64 bytes to 3 normal bytes */
+ v = 0;
+ i = 0;
+
+ last[0] = last[1] = 0;
+
+ /* we use the sign in the state to determine if we got a padding character
+ in the previous sequence */
+ if (i < 0)
+ {
+ i = -i;
+ last[0] = '=';
+ }
+
+ for (const unsigned char c : in)
+ {
+ rank = mime_base64_rank[c];
+ if (rank != 0xff)
+ {
+ last[1] = last[0];
+ last[0] = c;
+ v = (v << 6) | rank;
+ i++;
+ if (i == 4)
+ {
+ out += v >> 16;
+ if (last[1] != '=')
+ out += v >> 8;
+ if (last[0] != '=')
+ out += v;
+ i = 0;
+ }
+ }
+ }
+
+ return out;
+}
+
+ /*}}}*/
// stringcmp - Arbitrary string compare /*{{{*/
// ---------------------------------------------------------------------
/* This safely compares two non-null terminated strings of arbitrary
diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h
index 90d9674fd..91f5fe44b 100644
--- a/apt-pkg/contrib/strutl.h
+++ b/apt-pkg/contrib/strutl.h
@@ -69,6 +69,7 @@ APT_PUBLIC std::string DeEscapeString(const std::string &input);
APT_PUBLIC std::string SizeToStr(double Bytes);
APT_PUBLIC std::string TimeToStr(unsigned long Sec);
APT_PUBLIC std::string Base64Encode(const std::string &Str);
+APT_PUBLIC std::string Base64Decode(const std::string_view in);
APT_PUBLIC std::string OutputInDepth(const unsigned long Depth, const char* Separator=" ");
APT_PUBLIC std::string URItoFileName(const std::string &URI);
/** returns a datetime string as needed by HTTP/1.1 and Debian files.