StringUtils.java 11.6 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.fd.forms.internal.utils;

import com.adobe.fd.forms.internal.logging.FormsServiceLogger;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public final class StringUtils {
    private static FormsServiceLogger logger = new FormsServiceLogger(StringUtils.class);

    private StringUtils() {
    }

    public static byte[] replaceArraySegment(byte[] srcArray, int srcPos, int lengthToBeReplaced, byte[] newSegment, int offset, int length) {
        byte[] newArray = new byte[srcArray.length - lengthToBeReplaced + length];
        System.arraycopy(srcArray, 0, newArray, 0, srcPos);
        System.arraycopy(newSegment, offset, newArray, srcPos, length);
        System.arraycopy(srcArray, srcPos + lengthToBeReplaced, newArray, srcPos + length, srcArray.length - (srcPos + lengthToBeReplaced));
        return newArray;
    }

    public static byte[] replaceArraySegment(byte[] origArray, byte[] oldSegment, byte[] newSegment) {
        int nStartOffset = StringUtils.findArraySegment(origArray, 0, oldSegment);
        if (nStartOffset < 0) {
            return origArray;
        }
        byte[] newArray = new byte[origArray.length + newSegment.length - oldSegment.length];
        System.arraycopy(origArray, 0, newArray, 0, nStartOffset);
        System.arraycopy(newSegment, 0, newArray, nStartOffset, newSegment.length);
        System.arraycopy(origArray, nStartOffset + oldSegment.length, newArray, nStartOffset + newSegment.length, origArray.length - (nStartOffset + oldSegment.length));
        return newArray;
    }

    public static int findArraySegmentLimited(byte[] origArray, int start, byte[] findSegment, int origArrayLimit) {
        int origArrayLen;
        int i = start;
        int n = origArrayLen = origArrayLimit <= 0 ? origArray.length : origArrayLimit;
        if (origArrayLen > origArray.length) {
            origArrayLen = origArray.length;
        }
        while (i + findSegment.length <= origArrayLen) {
            int j;
            for (j = 0; j < findSegment.length && findSegment[j] == origArray[i + j]; ++j) {
            }
            if (j == findSegment.length) {
                return i;
            }
            ++i;
        }
        return -1;
    }

    public static int findArraySegment(byte[] origArray, int start, byte[] findSegment) {
        return StringUtils.findArraySegmentLimited(origArray, start, findSegment, 0);
    }

    public static int findArraySegmentReverse(byte[] origArray, byte[] findSegment) {
        return StringUtils.findArraySegmentReverse(origArray, -1, findSegment);
    }

    public static int findArraySegmentReverse(byte[] origArray, int start, byte[] findSegment) {
        if (origArray == null || findSegment == null || origArray.length == 0 || findSegment.length == 0) {
            return -1;
        }
        int origArrayLen = origArray.length;
        int findSegmentLen = findSegment.length;
        int i = start = start == -1 ? origArrayLen - 1 : start;
        while (i - findSegmentLen >= 0) {
            int j;
            for (j = 0; j < findSegmentLen && findSegment[findSegmentLen - 1 - j] == origArray[i - j]; ++j) {
            }
            if (j == findSegmentLen) {
                return i - j + 1;
            }
            --i;
        }
        return -1;
    }

    public static byte[] joinArray(byte[] cOne, byte[] cTwo) {
        byte[] cTemp = new byte[cOne.length + cTwo.length];
        System.arraycopy(cOne, 0, cTemp, 0, cOne.length);
        System.arraycopy(cTwo, 0, cTemp, cOne.length, cTwo.length);
        return cTemp;
    }

    public static byte[] joinArrayOfByteArrays(byte[][] arrayOfByteArrays) {
        int totalLength = 0;
        for (int i = 0; i < arrayOfByteArrays.length; ++i) {
            totalLength += arrayOfByteArrays[i].length;
        }
        byte[] joinedArray = new byte[totalLength];
        int destPosition = 0;
        for (int i2 = 0; i2 < arrayOfByteArrays.length; ++i2) {
            System.arraycopy(arrayOfByteArrays[i2], 0, joinedArray, destPosition, arrayOfByteArrays[i2].length);
            destPosition += arrayOfByteArrays[i2].length;
        }
        return joinedArray;
    }

    public static byte[] extractArraySegment(byte[] oldArray, int start, int newSize) {
        if (start < 0) {
            start = 0;
        }
        int oldSize = oldArray.length - start;
        if (newSize == 0) {
            newSize = oldSize;
        }
        byte[] newArray = new byte[newSize];
        int preserveLength = Math.min(oldSize, newSize);
        if (preserveLength > 0) {
            System.arraycopy(oldArray, start, newArray, 0, preserveLength);
        }
        return newArray;
    }

    public static byte[] stripXMLDescriptor(byte[] oriArray) {
        int xmlDescriptorEndLoc;
        byte[] resultBytes = oriArray;
        if (StringUtils.findArraySegmentLimited(oriArray, 0, "<?xml".getBytes(), 10) != -1 && (xmlDescriptorEndLoc = StringUtils.findArraySegment(oriArray, 0, "?>".getBytes())) != -1) {
            resultBytes = StringUtils.extractArraySegment(oriArray, xmlDescriptorEndLoc + 2, oriArray.length - (xmlDescriptorEndLoc + 2));
        }
        return resultBytes;
    }

    public static byte[] getXMLBlockBytes(byte[] content, String sTagName) {
        return StringUtils.getXMLBlockBytes(content, sTagName, 0);
    }

    public static byte[] getXMLBlockBytes(byte[] content, String sTagName, int nStart) {
        if (content == null) {
            return null;
        }
        int iBegin = StringUtils.getStartBeginTagIndex(content, sTagName, nStart);
        if (iBegin < 0) {
            return null;
        }
        String sEndTag = ">";
        int iEnd = StringUtils.findArraySegment(content, iBegin, sEndTag.getBytes());
        String sTerminatedTag = "/>";
        int iTerminated = StringUtils.findArraySegment(content, iBegin, sTerminatedTag.getBytes());
        if (iTerminated < 0 || iTerminated > iEnd) {
            sEndTag = "</" + sTagName;
            iEnd = StringUtils.findArraySegment(content, iBegin, sEndTag.getBytes());
            if (iEnd < 0) {
                return null;
            }
            sEndTag = ">";
            iEnd = StringUtils.findArraySegment(content, iEnd, sEndTag.getBytes());
        }
        return StringUtils.extractArraySegment(content, iBegin, iEnd - iBegin + sEndTag.length());
    }

    public static int getStartBeginTagIndex(byte[] content, String sTagName, int nStart) {
        String sStartTag = "<" + sTagName;
        int iBegin = StringUtils.findArraySegment(content, nStart, sStartTag.getBytes());
        return iBegin;
    }

    public static Map<String, String> getNamespacesDeclaredInTag(byte[] content, String sTagName, int nStart) {
        HashMap<String, String> nameSpaceMap = new HashMap<String, String>();
        int startTagIndex = StringUtils.getStartBeginTagIndex(content, sTagName, nStart);
        if (startTagIndex >= 0) {
            int endTagIndex = StringUtils.findArraySegment(content, startTagIndex, ">".getBytes());
            byte[] nameSpaceByteArray = "xmlns".getBytes();
            byte[] singleQuoteBytes = "'".getBytes();
            byte[] doubleQuoteBytes = "\"".getBytes();
            int xmlNameSpaceIndx = StringUtils.findArraySegmentLimited(content, startTagIndex, nameSpaceByteArray, endTagIndex);
            while (xmlNameSpaceIndx >= 0) {
                int singleQuoteIndx = StringUtils.findArraySegmentLimited(content, xmlNameSpaceIndx, "'".getBytes(), endTagIndex);
                int doubleQuoteIndx = StringUtils.findArraySegmentLimited(content, xmlNameSpaceIndx, "\"".getBytes(), endTagIndex);
                int nameSpaceEndIndex = 0;
                int startQuoteIndex = 0;
                byte[] quotesToUse = null;
                if (singleQuoteIndx > 0 && (singleQuoteIndx < doubleQuoteIndx || doubleQuoteIndx < 0)) {
                    startQuoteIndex = singleQuoteIndx;
                    quotesToUse = singleQuoteBytes;
                } else {
                    if (doubleQuoteIndx <= 0 || doubleQuoteIndx >= singleQuoteIndx && singleQuoteIndx >= 0) break;
                    startQuoteIndex = doubleQuoteIndx;
                    quotesToUse = doubleQuoteBytes;
                }
                nameSpaceEndIndex = StringUtils.findArraySegmentLimited(content, startQuoteIndex + 1, quotesToUse, endTagIndex);
                String nameSpaceStr = new String(StringUtils.extractArraySegment(content, xmlNameSpaceIndx, nameSpaceEndIndex - xmlNameSpaceIndx + 1));
                String prefixStr = StringUtils.extractPrefixFrmNameSpaceStr(nameSpaceStr);
                nameSpaceMap.put(prefixStr, nameSpaceStr);
                xmlNameSpaceIndx = StringUtils.findArraySegmentLimited(content, nameSpaceEndIndex, nameSpaceByteArray, endTagIndex);
            }
        }
        return nameSpaceMap;
    }

    public static String extractPrefixFrmNameSpaceStr(String nameSpaceStr) {
        int indexOfAssign;
        int indexOfColon = nameSpaceStr.indexOf(58);
        if (indexOfColon > 0 && (indexOfAssign = (nameSpaceStr = nameSpaceStr.replaceAll("\\s", "")).indexOf(61)) > indexOfColon) {
            return nameSpaceStr.substring(indexOfColon + 1, indexOfAssign);
        }
        return "";
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    public static byte[] inputStreamToBytes(InputStream oInputStream, int kPerRead) {
        byte[] result = new byte[]{};
        BufferedInputStream oStream = new BufferedInputStream(oInputStream);
        ByteArrayOutputStream oBAOS = new ByteArrayOutputStream();
        int nRead = 0;
        int nBlkSize = 1024 * kPerRead;
        byte[] buffer = new byte[nBlkSize];
        while ((nRead = oStream.read(buffer, 0, nBlkSize)) != -1) {
            oBAOS.write(buffer, 0, nRead);
        }
        result = oBAOS.toByteArray();
        try {
            if (oStream != null) {
                oStream.close();
            }
            if (oBAOS == null) return result;
            oBAOS.close();
            return result;
        }
        catch (IOException e) {
            logger.trace("AEM_FRM_001_004", new String[]{e.getMessage()}, e);
            return result;
        }
        catch (IOException e) {
            try {
                logger.debug("AEM_FRM_001_004", new String[]{e.getMessage()}, e);
            }
            catch (Throwable var9_11) {
                try {
                    if (oStream != null) {
                        oStream.close();
                    }
                    if (oBAOS == null) throw var9_11;
                    oBAOS.close();
                    throw var9_11;
                }
                catch (IOException e) {
                    logger.trace("AEM_FRM_001_004", new String[]{e.getMessage()}, e);
                }
                throw var9_11;
            }
            try {
                if (oStream != null) {
                    oStream.close();
                }
                if (oBAOS == null) return result;
                oBAOS.close();
                return result;
            }
            catch (IOException e) {
                logger.trace("AEM_FRM_001_004", new String[]{e.getMessage()}, e);
                return result;
            }
        }
    }

    public static boolean exists(byte[] bytes, int nStart, String sFind) {
        return StringUtils.findArraySegment(bytes, nStart, sFind.getBytes()) >= 0;
    }
}