/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.mime.ContentTypeHeader
 *  com.l7tech.common.mime.NoSuchPartException
 *  com.l7tech.common.mime.PartInfo
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.MessageTargetable
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.Regex
 *  com.l7tech.policy.assertion.TargetMessageType
 *  com.l7tech.policy.variable.NoSuchVariableException
 *  com.l7tech.util.Charsets
 *  com.l7tech.util.Either
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.Pair
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.common.mime.ContentTypeHeader;
import com.l7tech.common.mime.NoSuchPartException;
import com.l7tech.common.mime.PartInfo;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.MessageTargetable;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.Regex;
import com.l7tech.policy.assertion.TargetMessageType;
import com.l7tech.policy.variable.NoSuchVariableException;
import com.l7tech.server.message.ContextVariableBackedMessageUtils;
import com.l7tech.server.message.ContextVariableKnob;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.assertion.af;
import com.l7tech.server.policy.assertion.ag;
import com.l7tech.server.policy.assertion.ah;
import com.l7tech.server.policy.assertion.ai;
import com.l7tech.server.policy.assertion.aj;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.util.Charsets;
import com.l7tech.util.Either;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.Pair;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class ServerRegex
extends AbstractServerAssertion<Regex> {
    private Exception d;
    private final Either<Pattern, String> e;
    private final String[] f;
    private final boolean g;
    private final int h;
    private final boolean i;
    private final boolean j;
    private final String k;
    private final boolean l;
    private static final String[] m;

    public ServerRegex(Regex regex) {
        super(regex);
        Either either;
        String string = regex.getRegex();
        this.l = ((Regex)this.assertion).isCaseInsensitive();
        this.g = ((Regex)this.assertion).isReplace();
        this.f = regex.getVariablesUsed();
        this.h = Math.abs(regex.getReplaceRepeatCount());
        this.i = regex.isIncludeEntireExpressionCapture();
        this.k = ((Regex)this.assertion).getCaptureVar();
        this.j = ((Regex)this.assertion).isFindAll();
        try {
            either = ((Regex)this.assertion).isPatternContainsVariables() ? Either.right((Object)string) : Either.left((Object)Pattern.compile(string, this.a()));
        }
        catch (Exception exception) {
            this.d = exception;
            either = null;
        }
        this.e = either;
    }

    private int a() {
        int n2 = 40;
        if (this.l) {
            n2 |= 2;
        }
        return n2;
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        AssertionStatus assertionStatus;
        Pair<ai, aj> pair = this.a(policyEnforcementContext);
        Pattern pattern = this.d(policyEnforcementContext);
        try {
            if (((Regex)this.assertion).getRegexVar() != null) {
                policyEnforcementContext.setVariable(((Regex)this.assertion).getRegexVar(), pattern.pattern());
            }
        }
        catch (IOException iOException) {
            throw ServerRegex.a(iOException);
        }
        try {
            assertionStatus = this.g ? this.a(policyEnforcementContext, pattern, (ai)pair.left, (aj)pair.right) : this.a(policyEnforcementContext, pattern.matcher(((ai)pair.left).getInput()));
        }
        catch (IOException iOException) {
            throw ServerRegex.a(iOException);
        }
        return assertionStatus;
    }

    private Pair<ai, aj> a(PolicyEnforcementContext policyEnforcementContext) throws IOException {
        ai ai2;
        aj aj2 = null;
        Message message = this.b(policyEnforcementContext);
        int n2 = ((Regex)this.assertion).getMimePart();
        try {
            PartInfo partInfo = message.getMimeKnob().getPart(n2);
            Charset charset = this.a(partInfo);
            ContextVariableKnob contextVariableKnob = (ContextVariableKnob)message.getKnob(ContextVariableKnob.class);
            try {
                if (contextVariableKnob != null) {
                    contextVariableKnob.setOverrideEncoding(charset);
                }
            }
            catch (NoSuchPartException noSuchPartException) {
                throw ServerRegex.a((Exception)((Object)noSuchPartException));
            }
            ai2 = this.a(partInfo, charset);
            if (this.g) {
                aj2 = this.b(partInfo, charset);
            }
        }
        catch (NoSuchPartException noSuchPartException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_NO_SUCH_PART, Integer.toString(n2));
            throw new AssertionStatusException(noSuchPartException);
        }
        return new Pair((Object)ai2, (Object)aj2);
    }

    private ai a(PartInfo partInfo, Charset charset) {
        return new af(this, partInfo, charset);
    }

    private aj b(PartInfo partInfo, Charset charset) {
        return new ag(this, partInfo, charset);
    }

    private Charset a(PartInfo partInfo) {
        Charset charset;
        block6: {
            String string;
            block5: {
                string = ((Regex)this.assertion).getEncoding();
                try {
                    if (string != null && string.length() >= 1) break block5;
                }
                catch (UnsupportedCharsetException unsupportedCharsetException) {
                    throw ServerRegex.a(unsupportedCharsetException);
                }
                charset = partInfo.getContentType().getEncoding();
                break block6;
            }
            try {
                charset = Charset.forName(string);
                this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_ENCODING_OVERRIDE, string);
            }
            catch (UnsupportedCharsetException unsupportedCharsetException) {
                charset = null;
            }
        }
        if (charset == null) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_NO_ENCODING, Charsets.UTF8.name());
            charset = Charsets.UTF8;
        }
        return charset;
    }

    /*
     * Loose catch block
     */
    private Message b(PolicyEnforcementContext policyEnforcementContext) {
        try {
            Message message;
            block8: {
                Message message2;
                block10: {
                    block9: {
                        if (!((Regex)this.assertion).isAutoTarget()) break block8;
                        try {
                            block11: {
                                if (!policyEnforcementContext.isPostRouting()) break block9;
                                break block11;
                                catch (NoSuchVariableException noSuchVariableException) {
                                    throw ServerRegex.a((Exception)((Object)noSuchVariableException));
                                }
                            }
                            message2 = policyEnforcementContext.getResponse();
                            break block10;
                        }
                        catch (NoSuchVariableException noSuchVariableException) {
                            throw ServerRegex.a((Exception)((Object)noSuchVariableException));
                        }
                    }
                    message2 = policyEnforcementContext.getRequest();
                }
                return message2;
            }
            try {
                message = TargetMessageType.OTHER.equals((Object)((Regex)this.assertion).getTarget()) ? this.c(policyEnforcementContext) : policyEnforcementContext.getTargetMessage((MessageTargetable)this.assertion, true);
            }
            catch (NoSuchVariableException noSuchVariableException) {
                throw ServerRegex.a((Exception)((Object)noSuchVariableException));
            }
            return message;
        }
        catch (NoSuchVariableException noSuchVariableException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.NO_SUCH_VARIABLE, noSuchVariableException.getVariable());
            throw new AssertionStatusException(AssertionStatus.SERVER_ERROR);
        }
    }

    private Message c(PolicyEnforcementContext policyEnforcementContext) throws NoSuchVariableException {
        String string;
        String string2 = ((Regex)this.assertion).getOtherTargetMessageVariable();
        try {
            if (string2 == null) {
                throw new IllegalArgumentException(m[2]);
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRegex.a((Exception)((Object)noSuchVariableException));
        }
        Object object = policyEnforcementContext.getVariable(string2);
        try {
            if (object == null) {
                throw new NoSuchVariableException(string2, m[5]);
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRegex.a((Exception)((Object)noSuchVariableException));
        }
        try {
            if (object instanceof Message) {
                return (Message)object;
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRegex.a((Exception)((Object)noSuchVariableException));
        }
        if (object instanceof CharSequence) {
            string = object.toString();
        } else if (object instanceof AbstractCollection) {
            string = object.toString();
        } else if (object instanceof Collection) {
            string = new ArrayList((Collection)object).toString();
        } else if (object instanceof Object[]) {
            Object[] objectArray = (Object[])object;
            string = Arrays.asList(objectArray).toString();
        } else {
            string = object.toString();
        }
        return ContextVariableBackedMessageUtils.createContextVariableBackedMessage(policyEnforcementContext, string2, ContentTypeHeader.TEXT_DEFAULT, string);
    }

    private AssertionStatus a(PolicyEnforcementContext policyEnforcementContext, Matcher matcher) {
        boolean bl2;
        block20: {
            block21: {
                block22: {
                    block19: {
                        bl2 = matcher.find();
                        try {
                            if (!bl2 || this.k == null) break block19;
                        }
                        catch (AssertionStatusException assertionStatusException) {
                            throw ServerRegex.a(assertionStatusException);
                        }
                        ArrayList<String> arrayList = new ArrayList<String>();
                        this.a(arrayList, matcher);
                        if (this.j) {
                            StringBuffer stringBuffer = new StringBuffer();
                            while (true) {
                                boolean bl3 = matcher.find();
                                try {
                                    if (!bl3) {
                                        break;
                                    }
                                }
                                catch (AssertionStatusException assertionStatusException) {
                                    throw ServerRegex.a(assertionStatusException);
                                }
                                this.a(arrayList, matcher);
                            }
                            matcher.appendTail(stringBuffer);
                        }
                        policyEnforcementContext.setVariable(this.k, arrayList.toArray(new String[arrayList.size()]));
                    }
                    try {
                        try {
                            try {
                                if (!((Regex)this.assertion).isProceedIfPatternMatches()) break block20;
                                if (!bl2) break block21;
                            }
                            catch (AssertionStatusException assertionStatusException) {
                                throw ServerRegex.a(assertionStatusException);
                            }
                            if (!this.logger.isLoggable(Level.FINE)) break block22;
                        }
                        catch (AssertionStatusException assertionStatusException) {
                            throw ServerRegex.a(assertionStatusException);
                        }
                        this.logger.fine(m[6] + ((Regex)this.assertion).getRegex());
                    }
                    catch (AssertionStatusException assertionStatusException) {
                        throw ServerRegex.a(assertionStatusException);
                    }
                }
                return AssertionStatus.NONE;
            }
            this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_NO_MATCH_FAILURE, ((Regex)this.assertion).getRegex());
            return AssertionStatus.FALSIFIED;
        }
        try {
            if (bl2) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_MATCH_FAILURE, ((Regex)this.assertion).getRegex());
                return AssertionStatus.FALSIFIED;
            }
        }
        catch (AssertionStatusException assertionStatusException) {
            throw ServerRegex.a(assertionStatusException);
        }
        try {
            if (this.logger.isLoggable(Level.FINE)) {
                this.logger.fine(m[3] + ((Regex)this.assertion).getRegex());
            }
        }
        catch (AssertionStatusException assertionStatusException) {
            throw ServerRegex.a(assertionStatusException);
        }
        return AssertionStatus.NONE;
    }

    private void a(List<String> list, Matcher matcher) {
        try {
            if (this.i) {
                list.add(matcher.group(0));
            }
        }
        catch (AssertionStatusException assertionStatusException) {
            throw ServerRegex.a(assertionStatusException);
        }
        try {
            for (int i2 = 1; i2 <= matcher.groupCount(); ++i2) {
                list.add(matcher.group(i2));
            }
        }
        catch (AssertionStatusException assertionStatusException) {
            throw ServerRegex.a(assertionStatusException);
        }
    }

    /*
     * Loose catch block
     */
    private AssertionStatus a(PolicyEnforcementContext policyEnforcementContext, Pattern pattern, ai ai2, aj aj2) throws IOException {
        CharSequence charSequence;
        String string = ((Regex)this.assertion).getReplacement();
        try {
            if (this.logger.isLoggable(Level.FINE)) {
                this.logger.log(Level.FINE, m[4], new Object[]{((Regex)this.assertion).getRegex(), string});
            }
        }
        catch (IndexOutOfBoundsException indexOutOfBoundsException) {
            throw ServerRegex.a(indexOutOfBoundsException);
        }
        string = ExpandVariables.process(string, policyEnforcementContext.getVariableMap(this.f, this.getAudit()), this.getAudit());
        ArrayList<String> arrayList = new ArrayList<String>();
        CharSequence charSequence2 = charSequence = ai2.getInput();
        for (int i2 = 0; i2 <= this.h; ++i2) {
            boolean bl2;
            Matcher matcher;
            block20: {
                matcher = pattern.matcher(charSequence2);
                bl2 = matcher.find();
                if (!bl2) break;
                try {
                    block21: {
                        if (this.k == null) break block20;
                        break block21;
                        catch (IndexOutOfBoundsException indexOutOfBoundsException) {
                            throw ServerRegex.a(indexOutOfBoundsException);
                        }
                    }
                    this.a(arrayList, matcher);
                }
                catch (IndexOutOfBoundsException indexOutOfBoundsException) {
                    throw ServerRegex.a(indexOutOfBoundsException);
                }
            }
            StringBuffer stringBuffer = new StringBuffer();
            while (true) {
                try {
                    matcher.appendReplacement(stringBuffer, string);
                }
                catch (IllegalArgumentException | IndexOutOfBoundsException runtimeException) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_REPLACEMENT_INVALID, new String[]{m[0] + runtimeException.getMessage()}, ExceptionUtils.getDebugException((Throwable)runtimeException));
                    throw new AssertionStatusException(AssertionStatus.SERVER_ERROR);
                }
                bl2 = matcher.find();
                try {
                    if (!bl2) {
                        break;
                    }
                }
                catch (IndexOutOfBoundsException indexOutOfBoundsException) {
                    throw ServerRegex.a(indexOutOfBoundsException);
                }
                if (this.k == null) continue;
                this.a(arrayList, matcher);
            }
            matcher.appendTail(stringBuffer);
            charSequence2 = stringBuffer.toString();
        }
        try {
            if (charSequence2 != charSequence) {
                aj2.setOutput(charSequence2);
            }
        }
        catch (IndexOutOfBoundsException indexOutOfBoundsException) {
            throw ServerRegex.a(indexOutOfBoundsException);
        }
        try {
            if (this.k != null) {
                policyEnforcementContext.setVariable(this.k, arrayList.toArray(new String[arrayList.size()]));
            }
        }
        catch (IndexOutOfBoundsException indexOutOfBoundsException) {
            throw ServerRegex.a(indexOutOfBoundsException);
        }
        return AssertionStatus.NONE;
    }

    private Pattern d(PolicyEnforcementContext policyEnforcementContext) throws PolicyAssertionException {
        try {
            this.b();
            if (this.e.isLeft()) {
                return (Pattern)this.e.left();
            }
        }
        catch (PatternSyntaxException patternSyntaxException) {
            throw ServerRegex.a(patternSyntaxException);
        }
        String string = ExpandVariables.process((String)this.e.right(), policyEnforcementContext.getVariableMap(this.f, this.getAudit()), this.getAudit(), new ah(this));
        try {
            return Pattern.compile(string, this.a());
        }
        catch (PatternSyntaxException patternSyntaxException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_PATTERN_INVALID, new String[]{string, ExceptionUtils.getMessage((Throwable)patternSyntaxException)}, ExceptionUtils.getDebugException((Throwable)patternSyntaxException));
            throw new AssertionStatusException(AssertionStatus.SERVER_ERROR, (Throwable)patternSyntaxException);
        }
    }

    private void b() throws AssertionStatusException, PolicyAssertionException {
        block11: {
            block8: {
                block10: {
                    block9: {
                        try {
                            try {
                                if (this.e != null) break block8;
                                if (this.d == null) break block9;
                            }
                            catch (AssertionStatusException assertionStatusException) {
                                throw ServerRegex.a(assertionStatusException);
                            }
                            this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_PATTERN_INVALID, new String[]{((Regex)this.assertion).getRegex(), this.d.getMessage()}, this.d);
                            break block10;
                        }
                        catch (AssertionStatusException assertionStatusException) {
                            throw ServerRegex.a(assertionStatusException);
                        }
                    }
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_PATTERN_INVALID, ((Regex)this.assertion).getRegex(), m[1]);
                }
                throw new AssertionStatusException(AssertionStatus.SERVER_ERROR);
            }
            try {
                try {
                    if (!this.g || ((Regex)this.assertion).getReplacement() != null) break block11;
                }
                catch (AssertionStatusException assertionStatusException) {
                    throw ServerRegex.a(assertionStatusException);
                }
                this.logAndAudit((AuditDetailMessage)AssertionMessages.REGEX_NO_REPLACEMENT);
                throw new PolicyAssertionException(this.assertion, AssertionMessages.REGEX_NO_REPLACEMENT.getMessage());
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerRegex.a(assertionStatusException);
            }
        }
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[7];
                var3_1 = 0;
                var2_2 = "{\u0002H<K@\u0012z\u0003\t?WUW`\b\t,BU^o\u000fL3BKF.\tQ.U@A}\u0005F0\u0007\rBa\u001fZ7EIW.\u0002F0B][}\u0018L0S\u0005Po\u000fB~U@Tk\u001eL0D@\u0012a\u001e\t7KIWi\rE~@W]{\u001c\t,BCW|\tG=B\f\u001c.)[,HW\b.\r{\u0002B0HR\\.\t[,HW,Z\r[9BQ\u0012g\u001f\t\u0011smw\\LK+S\u0005\\aL_?ULSl\u0000L~ID_kL^?T\u0005Ak\u0018;^\u001eF=B@Vg\u0002N~\u001d\u0005|a\u0018\t3FQQf\tM~FKV.\u001c[1D@WjL@8\u0007K].\u0001H*DM\u0012|\tX+BVFk\b\tA\\\tY2FFW.\u001eL/R@Az\tMd\u0007hSz\u000fA~WDFz\t[0\u0007\u0002\u0015u\\Ty\u0000\t\u0012|\tY2FFW.\u001cH*S@@`L\u000ey\\\u0014O)K";
                var4_3 = "{\u0002H<K@\u0012z\u0003\t?WUW`\b\t,BU^o\u000fL3BKF.\tQ.U@A}\u0005F0\u0007\rBa\u001fZ7EIW.\u0002F0B][}\u0018L0S\u0005Po\u000fB~U@Tk\u001eL0D@\u0012a\u001e\t7KIWi\rE~@W]{\u001c\t,BCW|\tG=B\f\u001c.)[,HW\b.\r{\u0002B0HR\\.\t[,HW,Z\r[9BQ\u0012g\u001f\t\u0011smw\\LK+S\u0005\\aL_?ULSl\u0000L~ID_kL^?T\u0005Ak\u0018;^\u001eF=B@Vg\u0002N~\u001d\u0005|a\u0018\t3FQQf\tM~FKV.\u001c[1D@WjL@8\u0007K].\u0001H*DM\u0012|\tX+BVFk\b\tA\\\tY2FFW.\u001eL/R@Az\tMd\u0007hSz\u000fA~WDFz\t[0\u0007\u0002\u0015u\\Ty\u0000\t\u0012|\tY2FFW.\u001cH*S@@`L\u000ey\\\u0014O)K".length();
                var1_4 = 113;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 7;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = ServerRegex.b(v0, ServerRegex.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "b5c\u0001zi*_'1)KUOdts\u0013k=|W&x\u0007}qo\u0016\"p\njx*_'1\bjqf\u0015f&~\u0005zxn_:vF%=GW r\u000ezy*";
                    var4_3 = "b5c\u0001zi*_'1)KUOdts\u0013k=|W&x\u0007}qo\u0016\"p\njx*_'1\bjqf\u0015f&~\u0005zxn_:vF%=GW r\u000ezy*".length();
                    var1_4 = 42;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 63;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = ServerRegex.b(v0, ServerRegex.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        ServerRegex.m = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x35);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 9;
                    case 1 -> 107;
                    case 2 -> 46;
                    case 3 -> 89;
                    case 4 -> 32;
                    case 5 -> 34;
                    default -> 53;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

