From 7bf32c2b303260230e4d73805bd142327ced2bd1 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 7 Aug 2021 17:35:09 -0400 Subject: [PATCH] Fix parsing for some loosely formatted base64 --- .../app/src/main/kotlin/com/v2ray/ang/util/Utils.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt index 9dee6fc17..0f9c84db2 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt @@ -99,6 +99,15 @@ object Utils { * base64 decode */ fun decode(text: String): String { + tryDecodeBase64(text)?.let { return it } + if (text.endsWith('=')) { + // try again for some loosely formatted base64 + tryDecodeBase64(text.trimEnd('='))?.let { return it } + } + return "" + } + + fun tryDecodeBase64(text: String): String? { try { return Base64.decode(text, Base64.NO_WRAP).toString(charset("UTF-8")) } catch (e: Exception) { @@ -109,7 +118,7 @@ object Utils { } catch (e: Exception) { Log.i(AppConfig.ANG_PACKAGE, "Parse base64 url safe failed $e") } - return "" + return null } /**