Submission b849aec3...

ChallengeHex decoder
Submitterzac.creditmint.eth
Submitted at2018-14-10
Gas used37375
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

            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
            0xEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEF
            0x9000000000000000000000000000000000000000000000000000000000000000
            0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F

            dup5 1 eq one_byte jumpi
            0x44
            0x40
            loop_start:

                dup2 calldataload
                dup7
                dup2 dup8 or not
                dup7 mulmod add
                dup4 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
                dup7
                dup2 dup8 or not
                dup7 mulmod add
                dup4 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 pop pop 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
                        
            
        no_bytes:
            0x20 0x00 mstore
            0x00 0x20 mstore
            0x40 0x00 return

        one_byte:

            calldataload(0x44)
            dup5
            dup2 dup5 or not
                // multiply by 2**255 + 2**252, trigger integer overflow so that
                // every 5th bit becomes every (4th + 1st) bit
            dup5
            mulmod
                // combine back into w
            add
            // mask out the top 4 bits of every byte
            dup2 and
            
            dup1 1 byte // 0b
            swap1 0 byte      // 0a
            16 mul or   // ab
            0x40 mstore8
            0x00 0x41 mstore
            0x01 0x20 mstore
            0x20 0x00 mstore
            0x60 0x00 return
        }
    }
}