11package org .schabi .newpipe .extractor .services .youtube ;
22
3- import static org .schabi .newpipe .extractor .utils .Parser .matchGroup1MultiplePatterns ;
3+ import static org .schabi .newpipe .extractor .utils .Parser .matchMultiplePatterns ;
44
55import org .schabi .newpipe .extractor .exceptions .ParsingException ;
66import org .schabi .newpipe .extractor .utils .JavaScript ;
7+ import org .schabi .newpipe .extractor .utils .Pair ;
78import org .schabi .newpipe .extractor .utils .Parser ;
89import org .schabi .newpipe .extractor .utils .jsextractor .JavaScriptExtractor ;
910
1011import javax .annotation .Nonnull ;
12+
13+ import java .util .regex .Matcher ;
1114import java .util .regex .Pattern ;
1215
1316/**
@@ -24,6 +27,7 @@ final class YoutubeSignatureUtils {
2427
2528 private static final Pattern [] FUNCTION_REGEXES = {
2629 // CHECKSTYLE:OFF
30+ Pattern .compile ("\\ b(?:[a-zA-Z0-9_$]+)&&\\ ((?:[a-zA-Z0-9_$]+)=([a-zA-Z0-9_$]{2,})\\ ((\\ d+,)decodeURIComponent\\ ((?:[a-zA-Z0-9_$]+)\\ )\\ )" ),
2731 Pattern .compile ("\\ b(?:[a-zA-Z0-9_$]+)&&\\ ((?:[a-zA-Z0-9_$]+)=([a-zA-Z0-9_$]{2,})\\ (decodeURIComponent\\ ((?:[a-zA-Z0-9_$]+)\\ )\\ )" ),
2832 Pattern .compile ("\\ bm=([a-zA-Z0-9$]{2,})\\ (decodeURIComponent\\ (h\\ .s\\ )\\ )" ),
2933 Pattern .compile ("\\ bc&&\\ (c=([a-zA-Z0-9$]{2,})\\ (decodeURIComponent\\ (c\\ )\\ )" ),
@@ -38,8 +42,10 @@ final class YoutubeSignatureUtils {
3842 private static final String DEOBF_FUNC_REGEX_END = "=function\\ ([a-zA-Z0-9_]+\\ )\\ {.+?\\ })" ;
3943
4044 // CHECKSTYLE:OFF
41- private static final String SIG_DEOBF_GLOBAL_ARRAY_REGEX = "(var [A-z]=['\" ].*['\" ].split\\ (\" ;\" \\ ))" ;
42- private static final String SIG_DEOBF_HELPER_OBJ_NAME_REGEX = ";([A-Za-z0-9_\\ $]{2,})\\ [.." ;
45+ private static final Pattern SIG_DEOBF_GLOBAL_ARRAY_REGEX =
46+ Pattern .compile ("(var [A-z]=['\" ].*['\" ].split\\ (\" [;{]\" \\ ))" );
47+ private static final Pattern SIG_DEOBF_HELPER_OBJ_NAME_REGEX =
48+ Pattern .compile ("[;,]([A-Za-z0-9_$]{2,})\\ [.." );
4349 private static final String SIG_DEOBF_HELPER_OBJ_REGEX_START = "(var " ;
4450 private static final String SIG_DEOBF_HELPER_OBJ_REGEX_END = "=\\ {(?>.|\\ n)+?\\ }\\ };)" ;
4551 // CHECKSTYLE:ON
@@ -76,8 +82,10 @@ static String getSignatureTimestamp(@Nonnull final String javaScriptPlayerCode)
7682 static String getDeobfuscationCode (@ Nonnull final String javaScriptPlayerCode )
7783 throws ParsingException {
7884 try {
79- final String deobfuscationFunctionName = getDeobfuscationFunctionName (
80- javaScriptPlayerCode );
85+ final Pair <String , String > deobfuscationFunctionNameAndParams =
86+ getDeobfuscationFunctionNameAndParams (javaScriptPlayerCode );
87+ final String deobfuscationFunctionName = deobfuscationFunctionNameAndParams .getFirst ();
88+ final String functionAdditionalParams = deobfuscationFunctionNameAndParams .getSecond ();
8189
8290 String deobfuscationFunction ;
8391 try {
@@ -102,7 +110,7 @@ static String getDeobfuscationCode(@Nonnull final String javaScriptPlayerCode)
102110 final String callerFunction = "function " + DEOBFUSCATION_FUNCTION_NAME
103111 + "(a){return "
104112 + deobfuscationFunctionName
105- + "(a);}" ;
113+ + "(" + functionAdditionalParams + " a);}" ;
106114
107115 return globalVar + ";" + helperObject + deobfuscationFunction + ";" + callerFunction ;
108116 } catch (final Exception e ) {
@@ -111,10 +119,18 @@ static String getDeobfuscationCode(@Nonnull final String javaScriptPlayerCode)
111119 }
112120
113121 @ Nonnull
114- private static String getDeobfuscationFunctionName ( @ Nonnull final String javaScriptPlayerCode )
115- throws ParsingException {
122+ private static Pair < String , String > getDeobfuscationFunctionNameAndParams (
123+ @ Nonnull final String javaScriptPlayerCode ) throws ParsingException {
116124 try {
117- return matchGroup1MultiplePatterns (FUNCTION_REGEXES , javaScriptPlayerCode );
125+ final Matcher m = matchMultiplePatterns (FUNCTION_REGEXES , javaScriptPlayerCode );
126+ final String functionName = m .group (1 );
127+ final String functionAdditionalParams ;
128+ if (m .groupCount () > 1 ) {
129+ functionAdditionalParams = m .group (2 );
130+ } else {
131+ functionAdditionalParams = "" ;
132+ }
133+ return new Pair <>(functionName , functionAdditionalParams );
118134 } catch (final Parser .RegexException e ) {
119135 throw new ParsingException (
120136 "Could not find deobfuscation function with any of the known patterns" , e );
0 commit comments