From 62cc071f88cb33c1f6213c9dbd54a10135c5ad34 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 23 Nov 2024 19:32:06 +0100 Subject: 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+ --- apt-pkg/contrib/strutl.cc | 67 +++++++++++++++++++++++++++++++++++++++++++++++ apt-pkg/contrib/strutl.h | 1 + 2 files changed, 68 insertions(+) (limited to 'apt-pkg') 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 @@ -624,6 +624,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 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 /*{{{*/ // --------------------------------------------------------------------- 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. -- cgit v1.2.3-70-g09d2