Submission d6864d48...

ChallengeHex decoder
Submitterzac.creditmint.eth
Submitted at2018-29-10
Gas used36828
pragma solidity ^0.4.23;

contract HexDecoder {
    /**
     * @dev Decodes a hex-encoded input string, returning it in binary.
     *
     * Input strings may be of any length, but will always be a multiple of two
     * bytes long, and will not contain any non-hexadecimal characters.
     *
     * @param input The hex-encoded input.
     * @return The decoded output.
     */
    function decode(string input) external view returns(bytes output) {
        assembly {
            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
            0xEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEF
            0x9000000000000000000000000000000000000000000000000000000000000000
            0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F

            2 0x24 calldataload div
            dup1 iszero no_bytes jumpi

            0x44
            0x40
            
            dup3 4 eq four_byte jumpi
            // dup3 1 eq one_byte jumpi
            loop_start:

                dup2 calldataload
                dup8
                dup2 dup9 or not
                dup8 mulmod add
                dup5 and
                16 dup2 div or
                0x20 mstore
                4 mload 6 mload 8 mload 10 mload 12 mload 14 mload 16 mload 18 mload
                20 mload 22 mload 24 mload 26 mload 28 mload 30 mload 32 mload
                48 mstore8 47 mstore8 46 mstore8 45 mstore8 44 mstore8 43 mstore8 42 mstore8 41 mstore8
                40 mstore8 39 mstore8 38 mstore8 37 mstore8 36 mstore8 35 mstore8 34 mstore8
                33 mload dup2 mstore

                dup2 0x20 add calldataload
                dup8
                dup2 dup9 or not
                dup8 mulmod add
                dup5 and
                16 dup2 div or
                0x20 mstore
                4 mload 6 mload 8 mload 10 mload 12 mload 14 mload 16 mload 18 mload
                20 mload 22 mload 24 mload 26 mload 28 mload 30 mload 32 mload
                48 mstore8 47 mstore8 46 mstore8 45 mstore8 44 mstore8 43 mstore8 42 mstore8 41 mstore8
                40 mstore8 39 mstore8 38 mstore8 37 mstore8 36 mstore8 35 mstore8 34 mstore8
                33 mload dup2 0x10 add mstore
    
                0x20 add
                swap1 0x40 add
                swap1
                dup2 calldatasize gt
                loop_start jumpi

            loop_end:
            pop pop
            0x20 0x00 mstore
            dup1 0x20 mstore
            0x40 add 0x0
            dup2 mstore

            dup1 0x1f and iszero
            skip jumpi
            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0 and 0x20 add
            skip:
            0
            return
            pop pop pop pop      
            
        no_bytes:
            0x40 0x00 return

        // one_byte:
        //     calldataload(0x44)
        //     dup8
        //     dup2 dup9 or not
        //     dup8 mulmod add dup5 and
            
        //     dup1 1 byte
        //     swap1 0 byte
        //     16 mul or
        //     0x21 mstore
        //     0x00 0x41 mstore
        //     0x01 0x20 mstore
        //     0x20 0x00 mstore
        //     0x60 0x00 return

        four_byte:
                dup2 calldataload
                dup8
                dup2 dup9 or not
                dup8 mulmod add
                dup5 and
                16 dup2 div or
                0x20 mstore
                4 mload 6 mload 8 mload
                36 mstore8 35 mstore8 34 mstore8
                33 mload dup2 mstore

            0x20 0x00 mstore
            4 0x20 mstore
            0x0 0x44 mstore
            0x60
            0
            return
        }
    }
}