Submission 593f813c...

ChallengeHex decoder
Submitter0x888888417655103...
Submitted at2018-25-29
Gas used1208435
pragma solidity 0.4.24;

contract HexDecoder {

    function decode(
        string input
    ) public pure returns(bytes outBytes) {
        bytes memory inBytes = bytes(input);
        uint outLen = inBytes.length / 2;
        if (outLen == 0) {
            return;
        }
        outBytes = new bytes(outLen);
        if (outLen <= 128) {
            decodeFew(inBytes, outBytes);
            return;
        }
        if (outLen <= 512) {
            decodeMore(inBytes, outBytes);
            return;
        }
        decodeMost(inBytes, outBytes);
    }

    function decodeFew(
        bytes inBytes,
        bytes outBytes
    ) private pure {
        int outLen = int(outBytes.length);
        while (outLen-- != 0) {
            uint tmp = uint(inBytes[uint(outLen)+uint(outLen)+1]);
            if (tmp < 58) {
                tmp -= 48;
            } else if (tmp > 96) {
                tmp -= 87;
            } else {
                tmp -= 55;
            }
            tmp += uint(inBytes[uint(outLen)+uint(outLen)]) * 16;
            if (tmp < 928) {
                tmp -= 768;
            } else if (tmp > 1536) {
                tmp -= 1392;
            } else {
                tmp -= 880;
            }
            outBytes[uint(outLen)] = byte(tmp);
        }
    }

    function decodeMore(
        bytes inBytes,
        bytes outBytes
    ) private pure {
        int j = int(outBytes.length);
        uint[103] memory dict = [
            uint(0),
             0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
             0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
             0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
             1,  2,  3,  4,  5,  6,  7,  8,  9,  0,  0,  0,  0,  0,  0,  0,
            10, 11, 12, 13, 14, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
             0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
            10, 11, 12, 13, 14, 15];
        while (j-- != 0) {
            outBytes[uint(j)] = byte(
                dict[uint(inBytes[uint(j)+uint(j)])] * 16 +
                dict[uint(inBytes[uint(j)+uint(j)+1])]
            );
        }
    }

    function decodeMost(
        bytes inBytes,
        bytes outBytes
    ) private pure {
        int j = int(outBytes.length);
        uint[103] memory dict1 = [
            uint(0),
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
           0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9,   0,   0,   0,   0,   0,   0,   0,
           0xa, 0xb, 0xc, 0xd, 0xe, 0xf,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
           0xa, 0xb, 0xc, 0xd, 0xe, 0xf];
        uint[103] memory dict2 = [
            uint(0),
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,   0,   0,   0,   0,   0,   0,   0,
          0xa0,0xb0,0xc0,0xd0,0xe0,0xf0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
             0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
          0xa0,0xb0,0xc0,0xd0,0xe0,0xf0];
        while (j-- != 0) {
            outBytes[uint(j)] = byte(
                dict2[uint(inBytes[uint(j)+uint(j)])] |
                dict1[uint(inBytes[uint(j)+uint(j)+1])]);
        }
    }
}