Submission 176b95ef...

ChallengeHex decoder
Submitterzac.creditmint.eth
Submitted at2018-40-16
Gas used33963
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.
     *
     * @return The decoded output.
     */
    function decode(string) external view returns(bytes) {
        assembly {
            mstore(0x40, 0x00)
            0xEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEF
            0x9000000000000000000000000000000000000000000000000000000000000000
            0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F

            0x40
            msize
            2 0x24 calldataload div
            dup1 iszero no_bytes jumpi

            dup3 4 eq four_byte jumpi

            0x44
            dup1 calldataload

            loop_start:

                dup1 dup9 or not
                153 mul swap1
                dup7 and
                272 mul
                add
                0x20 mstore
                0x03 mload 0x05 mload 0x07 mload 0x09 mload 0x0b mload
                0x0d mload 0x0f mload 0x11 mload 0x13 mload 0x15 mload
                0x17 mload 0x19 mload 0x1b mload 0x1d mload 0x1f mload
                0x2f mstore8 0x2e mstore8 0x2d mstore8 0x2c mstore8 0x2b mstore8
                0x2a mstore8 0x29 mstore8 0x28 mstore8 0x27 mstore8 0x26 mstore8
                0x25 mstore8 0x24 mstore8 0x23 mstore8 0x22 mstore8 0x21 mstore8

                0x20 dup2 add calldataload
                dup1 dup9 or not
                153 mul swap1
                dup7 and
                272 mul
                add
                0x30 mstore
                0x13 mload 0x15 mload 0x17 mload 0x19 mload 0x1b mload
                0x1d mload 0x1f mload 0x21 mload 0x23 mload 0x25 mload
                0x27 mload 0x29 mload 0x2b mload 0x2d mload 0x2f mload
                0x3f mstore8 0x3e mstore8 0x3d mstore8 0x3c mstore8 0x3b mstore8
                0x3a mstore8 0x39 mstore8 0x38 mstore8 0x37 mstore8 0x36 mstore8
                0x35 mstore8 0x34 mstore8 0x33 mstore8 0x32 mstore8 0x31 mstore8
                0x20 mload msize mstore
    
                0x40 add
                dup1 calldataload dup1
                loop_start jumpi

            pop pop                // s m'
            0x0 dup2 dup4 add mstore
            0x20 dup3 sub mstore // store length
            
                                // m' 0x40
            sub
            0x20 dup2 mstore

            dup1 0x20 msize sub sub // size start
            swap1
            return
            
            pop pop
            
        no_bytes:
            0x40 0x00 return

        four_byte:
            pop pop 0x44
            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

        four_words:
        }
    }
}