Search in sources :

Example 6 with UnsafeByteArrayOutputStream

use of io.nuls.core.tools.crypto.UnsafeByteArrayOutputStream in project nuls by nuls-io.

the class Script method removeAllInstancesOf.

/**
 * Returns the script bytes of inputScript with all instances of the specified script object removed
 */
public static byte[] removeAllInstancesOf(byte[] inputScript, byte[] chunkToRemove) {
    // We usually don't end up removing anything
    UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(inputScript.length);
    int cursor = 0;
    while (cursor < inputScript.length) {
        boolean skip = equalsRange(inputScript, cursor, chunkToRemove);
        int opcode = inputScript[cursor++] & 0xFF;
        int additionalBytes = 0;
        if (opcode >= 0 && opcode < OP_PUSHDATA1) {
            additionalBytes = opcode;
        } else if (opcode == OP_PUSHDATA1) {
            additionalBytes = (0xFF & inputScript[cursor]) + 1;
        } else if (opcode == OP_PUSHDATA2) {
            additionalBytes = ((0xFF & inputScript[cursor]) | ((0xFF & inputScript[cursor + 1]) << 8)) + 2;
        } else if (opcode == OP_PUSHDATA4) {
            additionalBytes = ((0xFF & inputScript[cursor]) | ((0xFF & inputScript[cursor + 1]) << 8) | ((0xFF & inputScript[cursor + 1]) << 16) | ((0xFF & inputScript[cursor + 1]) << 24)) + 4;
        }
        if (!skip) {
            try {
                bos.write(opcode);
                bos.write(Arrays.copyOfRange(inputScript, cursor, cursor + additionalBytes));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        cursor += additionalBytes;
    }
    return bos.toByteArray();
}
Also used : UnsafeByteArrayOutputStream(io.nuls.core.tools.crypto.UnsafeByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

UnsafeByteArrayOutputStream (io.nuls.core.tools.crypto.UnsafeByteArrayOutputStream)6 IOException (java.io.IOException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 NulsOutputStreamBuffer (io.nuls.kernel.utils.NulsOutputStreamBuffer)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)1