Submission b9141f64...

ChallengeHex decoder
Submitter0x1dba1131000664b...
Submitted at2018-18-27
Gas used36849
/**
 * This file is part of the 1st Solidity Gas Golfing Contest.
 *
 * This work is licensed under Creative Commons Attribution ShareAlike 3.0.
 * https://creativecommons.org/licenses/by-sa/3.0/
 *
 * Author: Jordi Baylina
 */

pragma solidity 0.4.24;

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 pure returns(bytes ) {
        assembly {
            let lout
            for {
                let l := calldataload(36)
//                if and(l, 1) { revert(0,0) }
                let p := 68
                let pend := add(p, l)
                let outp := 64
                lout := div(l,2)
                mstore(32, lout)
            } lt(p, pend) {
            } {
                let v := calldataload(p)
                let d := and(v, 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f)
                let d1 := div(and(v, 0x4040404040404040404040404040404040404040404040404040404040404040),8)
                let d2 := div(d1,8)
                d := add(d, add(d1,d2))

                d := and(add(div(d, 0x10), d),0x00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff)
                d := and(add(div(d, 0x100), d), 0x0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff)
                d := and(add(div(d, 0x10000), d), 0x00000000ffffffff00000000ffffffff00000000ffffffff00000000ffffffff)
                d := and(add(div(d, 0x100000000), d), 0x0000000000000000ffffffffffffffff0000000000000000ffffffffffffffff)
                d := add(div(d, 0x10000000000000000), d)
                d := mul(d, 0x100000000000000000000000000000000)

                mstore(outp, d)
                outp := add(outp, 16)
                p := add(p,32)

            }
            lout:= and(add(lout, 95),0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0)
            mstore(0, 0x20)
            return(0, lout)
        }

    }

}