Submission b095ac7d...

ChallengeHex decoder
Submitter0x53b21be84eece2e...
Submitted at2018-10-25
Gas used71314
/**
 * 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/
 */

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.
     */

// 384586
    function decode(string input) public pure returns(bytes output) {
        output = bytes(input);
        uint len = output.length;

        for (uint i = 0; i < len; i += 0x20) {
            // uint j = i;
            assembly {
                let d := mload(add(output, add(i, 0x20)))
                d := add(
                    and(0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f, d),
                    mul(9, and(0x0101010101010101010101010101010101010101010101010101010101010101,
                        div(d, 64)
                    ))
                )
                d := add(
                    and(0x000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f, d),
                    div(and(0x0f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f00, d), 16)
                )

                let save := add(mul(and(d, 0x00ff000000000000000000000000000000000000000000000000000000000000), 0x0000000000000000000000000000000000000000000000000000000000000100),
                            add(mul(and(d, 0x000000ff00000000000000000000000000000000000000000000000000000000), 0x0000000000000000000000000000000000000000000000000000000000010000),
                            add(mul(and(d, 0x0000000000ff0000000000000000000000000000000000000000000000000000), 0x0000000000000000000000000000000000000000000000000000000001000000),
                            add(mul(and(d, 0x00000000000000ff000000000000000000000000000000000000000000000000), 0x0000000000000000000000000000000000000000000000000000000100000000),
                            add(mul(and(d, 0x000000000000000000ff00000000000000000000000000000000000000000000), 0x0000000000000000000000000000000000000000000000000000010000000000),
                            add(mul(and(d, 0x0000000000000000000000ff0000000000000000000000000000000000000000), 0x0000000000000000000000000000000000000000000000000001000000000000),
                            add(mul(and(d, 0x00000000000000000000000000ff000000000000000000000000000000000000), 0x0000000000000000000000000000000000000000000000000100000000000000),
                            add(mul(and(d, 0x000000000000000000000000000000ff00000000000000000000000000000000), 0x0000000000000000000000000000000000000000000000010000000000000000),
                            add(mul(and(d, 0x0000000000000000000000000000000000ff0000000000000000000000000000), 0x0000000000000000000000000000000000000000000001000000000000000000),
                            add(mul(and(d, 0x00000000000000000000000000000000000000ff000000000000000000000000), 0x0000000000000000000000000000000000000000000100000000000000000000),
                            add(mul(and(d, 0x000000000000000000000000000000000000000000ff00000000000000000000), 0x0000000000000000000000000000000000000000010000000000000000000000),
                            add(mul(and(d, 0x0000000000000000000000000000000000000000000000ff0000000000000000), 0x0000000000000000000000000000000000000001000000000000000000000000),
                            add(mul(and(d, 0x00000000000000000000000000000000000000000000000000ff000000000000), 0x0000000000000000000000000000000000000100000000000000000000000000),
                            add(mul(and(d, 0x000000000000000000000000000000000000000000000000000000ff00000000), 0x0000000000000000000000000000000000010000000000000000000000000000),
                            add(mul(and(d, 0x0000000000000000000000000000000000000000000000000000000000ff0000), 0x0000000000000000000000000000000001000000000000000000000000000000),
                                mul(and(d, 0x00000000000000000000000000000000000000000000000000000000000000ff), 0x0000000000000000000000000000000100000000000000000000000000000000))))))))))))))))
                mstore(add(output, add(div(i, 2), 0x20)), save)
            }
        }
        assembly {
            mstore(output, div(len, 2))
        }
    }
}