Submission 2f718f6b...

ChallengeHex decoder
Submitterzac.creditmint.eth
Submitted at2018-49-10
Gas used38520
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 {
            2 0x24 calldataload div
            dup1 iszero no_bytes jumpi
            0x44
            0x40
            loop_start:

                dup2 calldataload
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
                dup2 0xEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEF or not
                0x9000000000000000000000000000000000000000000000000000000000000000 mulmod add
                0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F 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

                0x10 add
                swap1 0x20 add
                dup1 calldatasize eq
                loop_end jumpi

                dup1 calldataload
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
                dup2 0xEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEF or not
                0x9000000000000000000000000000000000000000000000000000000000000000 mulmod add
                0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F 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 dup3 mstore

                /* dup1 0x20 add calldataload
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
                dup2 0xEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEF or not
                0x9000000000000000000000000000000000000000000000000000000000000000 mulmod add
                0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F 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 dup3 0x10 add mstore */

                0x20 add
                swap1 0x10 add
                dup2 calldatasize gt
                loop_start jumpi

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

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