Base64.java 4.34 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.internal.xmp.impl;

public class Base64 {
    private static final byte INVALID = -1;
    private static final byte WHITESPACE = -2;
    private static final byte EQUAL = -3;
    private static byte[] base64;
    private static byte[] ascii;

    public static final byte[] encode(byte[] src) {
        return Base64.encode(src, 0);
    }

    public static final byte[] encode(byte[] src, int lineFeed) {
        int bits24;
        int bits6;
        if ((lineFeed = lineFeed / 4 * 4) < 0) {
            lineFeed = 0;
        }
        int codeLength = (src.length + 2) / 3 * 4;
        if (lineFeed > 0) {
            codeLength += (codeLength - 1) / lineFeed;
        }
        byte[] dst = new byte[codeLength];
        int didx = 0;
        int sidx = 0;
        int lf = 0;
        while (sidx + 3 <= src.length) {
            bits24 = (src[sidx++] & 255) << 16;
            bits24 |= (src[sidx++] & 255) << 8;
            bits6 = ((bits24 |= (src[sidx++] & 255) << 0) & 16515072) >> 18;
            dst[didx++] = base64[bits6];
            bits6 = (bits24 & 258048) >> 12;
            dst[didx++] = base64[bits6];
            bits6 = (bits24 & 4032) >> 6;
            dst[didx++] = base64[bits6];
            bits6 = bits24 & 63;
            dst[didx++] = base64[bits6];
            if (didx >= codeLength || lineFeed <= 0 || (lf += 4) % lineFeed != 0) continue;
            dst[didx++] = 10;
        }
        if (src.length - sidx == 2) {
            bits24 = (src[sidx] & 255) << 16;
            bits6 = ((bits24 |= (src[sidx + 1] & 255) << 8) & 16515072) >> 18;
            dst[didx++] = base64[bits6];
            bits6 = (bits24 & 258048) >> 12;
            dst[didx++] = base64[bits6];
            bits6 = (bits24 & 4032) >> 6;
            dst[didx++] = base64[bits6];
            dst[didx++] = 61;
        } else if (src.length - sidx == 1) {
            bits24 = (src[sidx] & 255) << 16;
            bits6 = (bits24 & 16515072) >> 18;
            dst[didx++] = base64[bits6];
            bits6 = (bits24 & 258048) >> 12;
            dst[didx++] = base64[bits6];
            dst[didx++] = 61;
            dst[didx++] = 61;
        }
        return dst;
    }

    public static final String encode(String src) {
        return new String(Base64.encode(src.getBytes()));
    }

    public static final byte[] decode(byte[] src) throws IllegalArgumentException {
        int didx;
        int sidx;
        int srcLen = 0;
        for (sidx = 0; sidx < src.length; ++sidx) {
            byte val = ascii[src[sidx]];
            if (val >= 0) {
                src[srcLen++] = val;
                continue;
            }
            if (val != -1) continue;
            throw new IllegalArgumentException("Invalid base 64 string");
        }
        while (srcLen > 0 && src[srcLen - 1] == -3) {
            --srcLen;
        }
        byte[] dst = new byte[srcLen * 3 / 4];
        sidx = 0;
        for (didx = 0; didx < dst.length - 2; didx += 3) {
            dst[didx] = (byte)(src[sidx] << 2 & 255 | src[sidx + 1] >>> 4 & 3);
            dst[didx + 1] = (byte)(src[sidx + 1] << 4 & 255 | src[sidx + 2] >>> 2 & 15);
            dst[didx + 2] = (byte)(src[sidx + 2] << 6 & 255 | src[sidx + 3] & 63);
            sidx += 4;
        }
        if (didx < dst.length) {
            dst[didx] = (byte)(src[sidx] << 2 & 255 | src[sidx + 1] >>> 4 & 3);
        }
        if (++didx < dst.length) {
            dst[didx] = (byte)(src[sidx + 1] << 4 & 255 | src[sidx + 2] >>> 2 & 15);
        }
        return dst;
    }

    public static final String decode(String src) {
        return new String(Base64.decode(src.getBytes()));
    }

    static {
        int idx;
        base64 = new byte[]{65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47};
        ascii = new byte[255];
        for (idx = 0; idx < 255; ++idx) {
            Base64.ascii[idx] = -1;
        }
        for (idx = 0; idx < base64.length; ++idx) {
            Base64.ascii[Base64.base64[idx]] = (byte)idx;
        }
        Base64.ascii[9] = -2;
        Base64.ascii[10] = -2;
        Base64.ascii[13] = -2;
        Base64.ascii[32] = -2;
        Base64.ascii[61] = -3;
    }
}