diff options
| author | Julian Andres Klode <jak@debian.org> | 2024-11-23 19:32:06 +0100 |
|---|---|---|
| committer | Julian Andres Klode <jak@debian.org> | 2024-11-28 10:17:21 +0100 |
| commit | 62cc071f88cb33c1f6213c9dbd54a10135c5ad34 (patch) | |
| tree | ca58e0a3f52f11ac592154452458445b8f8648a8 | |
| parent | 0211fd6451bd67e5c590ac89c8696310ac8dd78c (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+
| -rw-r--r-- | apt-pkg/contrib/strutl.cc | 67 | ||||
| -rw-r--r-- | apt-pkg/contrib/strutl.h | 1 | ||||
| -rw-r--r-- | test/libapt/strutil_test.cc | 21 |
3 files changed, 88 insertions, 1 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. diff --git a/test/libapt/strutil_test.cc b/test/libapt/strutil_test.cc index 500ba98da..9320f8fa9 100644 --- a/test/libapt/strutil_test.cc +++ b/test/libapt/strutil_test.cc @@ -179,6 +179,24 @@ TEST(StrUtilTest,Base64Encode) EXPECT_EQ("/A==", Base64Encode("\xfc")); EXPECT_EQ("//8=", Base64Encode("\xff\xff")); } +TEST(StrUtilTest,Base64Decode) +{ + EXPECT_EQ(Base64Decode("QWxhZGRpbjpvcGVuIHNlc2FtZQ=="), "Aladdin:open sesame"); + EXPECT_EQ(Base64Decode("cGxlYXN1cmUu"), "pleasure."); + EXPECT_EQ(Base64Decode("bGVhc3VyZS4="), "leasure."); + EXPECT_EQ(Base64Decode("ZWFzdXJlLg=="), "easure."); + EXPECT_EQ(Base64Decode("YXN1cmUu"), "asure."); + EXPECT_EQ(Base64Decode("c3VyZS4="), "sure."); + EXPECT_EQ(Base64Decode("dXJlLg=="), "ure."); + EXPECT_EQ(Base64Decode("cmUu"), "re."); + EXPECT_EQ(Base64Decode("ZS4="), "e."); + EXPECT_EQ(Base64Decode("Lg=="), "."); + EXPECT_EQ(Base64Decode(""), ""); + EXPECT_EQ(Base64Decode("IA=="), "\x20"); + EXPECT_EQ(Base64Decode("/w=="), "\xff"); + EXPECT_EQ(Base64Decode("/A=="), "\xfc"); + EXPECT_EQ(Base64Decode("//8="), "\xff\xff"); +} static void ReadMessagesTestWithNewLine(char const * const nl, char const * const ab) { SCOPED_TRACE(SubstVar(SubstVar(nl, "\n", "n"), "\r", "r") + " # " + ab); @@ -318,7 +336,8 @@ TEST(StrUtilTest,RFC1123StrToTime) time_t t; EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 GMT", t)); EXPECT_EQ(784111777, t); - } { + } + { time_t t; EXPECT_TRUE(RFC1123StrToTime("Sun, 6 Nov 1994 08:49:37 UTC", t)); EXPECT_EQ(784111777, t); |
