<commit_before>//
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.
// OpenNMS(R) is a derivative work, containing both original code, included code and modified
// code that was published under the GNU General Public License. Copyrights for modified 
// and included code are below.
//
// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
// 
// Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// For more information contact:
//      OpenNMS Licensing       <<EMAIL>>
//      http://www.opennms.org/
//      http://www.opennms.com/
//
//
// Tab Size = 8
//
// Base64.java,v 1.1.1.1 2001/11/11 17:23:18 ben Exp
//

package org.opennms.core.utils;

/**
 * Performs base 64 encoding and decoding on byte arrays.
 * 
 * @author <A HREF="mailto:<EMAIL>"><NAME> </A>
 * @author <A HREF="http://www.opennms.org/">OpenNMS </A>
 * 
 * @version CVS 1.1.1.1
 */
public final class Base64 extends Object {
    /**
     * <P>
     * The base64 encoding map. Using 6-bit values it is possible to map 24-bits
     * into 4 characters. If there are not sufficent amount of bits to makeup
     * six then it is padded with BASE64_PAD.
     * </P>
     */
    private static final char BASE64_CHARS[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

    // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    private static final byte BASE64_VALUES[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    /* 16 - 31 */-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    /* 32 - 47 */-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    /* 48 - 63 */52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1,
    /* 64 - 79 */-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    /* 80 - 95 */15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    /* 96 - 111 */-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    /* 112 - 127 */41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };

    /**
     * The base64 padding character
     */
    private static final char BASE64_PAD = '=';

    /**
     * <P>
     * Encodes the passed byte array using the base64 rules. The base64 encoding
     * schema is performed by grouping the bytes in to 6-bit quantities and then
     * encoding them.
     * </P>
     * 
     * <P>
     * For more information see RFC1341 for the format used for base64 encoding.
     * </P>
     * 
     * @param data
     *            The input byte array
     * 
     * @return The converted data in a character stream.
     * 
     */
    public static char[] encodeBase64(byte[] data) {
        int destlen = ((data.length + 2) / 3) * 4;
        char[] dest = new char[destlen];
        int destndx = 0;

        for (int i = 0; i < data.length; i += 3) {
            int quantum = 0;
            int pad = 0;
            int bytes = data.length - i;

            if (bytes >= 1) {
                quantum = (data[i] < 0 ? (256 + (int) data[i]) : (int) data[i]);
                pad = 2;
            }

            quantum <<= 8;
            if (bytes >= 2) {
                quantum |= (data[i + 1] < 0 ? (256 + (int) data[i + 1]) : (int) data[i + 1]);
                pad = 1;
            }

            quantum <<= 8;
            if (bytes > 2) {
                quantum |= (data[i + 2] < 0 ? (256 + (int) data[i + 2]) : (int) data[i + 2]);
                pad = 0;
            }

            for (int j = 3; j >= pad; j--) {
                int ndx = (quantum >> (j * 6)) & 0x3f;
                dest[destndx++] = BASE64_CHARS[ndx];
            }

            for (int j = pad; j > 0; j--) {
                dest[destndx++] = BASE64_PAD;
            }
        }
        return dest;
    }

    /**
     * <P>
     * Decodes a character array into the corresponding byte array. The buffer
     * must be an intergral number of 4 character. I.E. size mod 4 is equal to
     * zero or an exception will be thrown. Likewise, if there is an invalid
     * character in the input array then an exception will be thrown.
     * </P>
     * 
     * 
     * @param data
     *            The data stream to be filtered.
     * 
     * @return The coverted array of bytes.
     * 
     * @exception java.lang.IllegalArgumentException
     *                Thrown if an invalid buffer that cannot be decoded is
     *                passed.
     * 
     */
    public static byte[] decodeBase64(char[] data) {
        //
        // check the length, it must be an integral number of 4 characters.
        //
        if ((data.length % 4) != 0)
            throw new IllegalArgumentException("Invalid base64 encoding, improper length");

        //
        // get the raw length and check for
        // the appended padding characters
        // if any.
        //
        int rawlen = (data.length / 4) * 3;
        for (int i = 1; i <= 2; i++) {
            if (data[data.length - i] == BASE64_PAD)
                --rawlen;<commit_msg>hit an exception trying to decode what looks to be an empty data array add a check for that<commit_after>//
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.
// OpenNMS(R) is a derivative work, containing both original code, included code and modified
// code that was published under the GNU General Public License. Copyrights for modified 
// and included code are below.
//
// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
// 
// Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// For more information contact:
//      OpenNMS Licensing       <<EMAIL>>
//      http://www.opennms.org/
//      http://www.opennms.com/
//
//
// Tab Size = 8
//
// Base64.java,v 1.1.1.1 2001/11/11 17:23:18 ben Exp
//

package org.opennms.core.utils;

/**
 * Performs base 64 encoding and decoding on byte arrays.
 * 
 * @author <A HREF="mailto:<EMAIL>"><NAME> </A>
 * @author <A HREF="http://www.opennms.org/">OpenNMS </A>
 * 
 * @version CVS 1.1.1.1
 */
public final class Base64 extends Object {
    /**
     * <P>
     * The base64 encoding map. Using 6-bit values it is possible to map 24-bits
     * into 4 characters. If there are not sufficent amount of bits to makeup
     * six then it is padded with BASE64_PAD.
     * </P>
     */
    private static final char BASE64_CHARS[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

    // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    private static final byte BASE64_VALUES[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    /* 16 - 31 */-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    /* 32 - 47 */-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    /* 48 - 63 */52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1,
    /* 64 - 79 */-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    /* 80 - 95 */15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    /* 96 - 111 */-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    /* 112 - 127 */41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };

    /**
     * The base64 padding character
     */
    private static final char BASE64_PAD = '=';

    /**
     * <P>
     * Encodes the passed byte array using the base64 rules. The base64 encoding
     * schema is performed by grouping the bytes in to 6-bit quantities and then
     * encoding them.
     * </P>
     * 
     * <P>
     * For more information see RFC1341 for the format used for base64 encoding.
     * </P>
     * 
     * @param data
     *            The input byte array
     * 
     * @return The converted data in a character stream.
     * 
     */
    public static char[] encodeBase64(byte[] data) {
        int destlen = ((data.length + 2) / 3) * 4;
        char[] dest = new char[destlen];
        int destndx = 0;

        for (int i = 0; i < data.length; i += 3) {
            int quantum = 0;
            int pad = 0;
            int bytes = data.length - i;

            if (bytes >= 1) {
                quantum = (data[i] < 0 ? (256 + (int) data[i]) : (int) data[i]);
                pad = 2;
            }

            quantum <<= 8;
            if (bytes >= 2) {
                quantum |= (data[i + 1] < 0 ? (256 + (int) data[i + 1]) : (int) data[i + 1]);
                pad = 1;
            }

            quantum <<= 8;
            if (bytes > 2) {
                quantum |= (data[i + 2] < 0 ? (256 + (int) data[i + 2]) : (int) data[i + 2]);
                pad = 0;
            }

            for (int j = 3; j >= pad; j--) {
                int ndx = (quantum >> (j * 6)) & 0x3f;
                dest[destndx++] = BASE64_CHARS[ndx];
            }

            for (int j = pad; j > 0; j--) {
                dest[destndx++] = BASE64_PAD;
            }
        }
        return dest;
    }

    /**
     * <P>
     * Decodes a character array into the corresponding byte array. The buffer
     * must be an intergral number of 4 character. I.E. size mod 4 is equal to
     * zero or an exception will be thrown. Likewise, if there is an invalid
     * character in the input array then an exception will be thrown.
     * </P>
     * 
     * 
     * @param data
     *            The data stream to be filtered.
     * 
     * @return The coverted array of bytes.
     * 
     * @exception java.lang.IllegalArgumentException
     *                Thrown if an invalid buffer that cannot be decoded is
     *                passed.
     * 
     */
    public static byte[] decodeBase64(char[] data) {
        //. If the data is zero length just return a zero length byte array
        if (data.length == 0) {
            return new byte[0];
        }
        //
        // check the length, it must be an integral number of 4 characters.
        //
        if ((data.length % 4) != 0)
            throw new IllegalArgumentException("Invalid base64 encoding, improper length");

        //
        // get the raw length and check for
        // the appended padding characters
        // if any.
        //
        int rawlen = (data.length / 4) * 3;
        for (int i = 1; i <= 2; i++) {
            if (data[data.length - i] == BASE64_PAD)
                --rawlen;
{'fraction_non_alphanumeric': 0.16349902534113062, 'fraction_numerical': 0.0638401559454191, 'mean_word_length': 1.7769508344609832, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 72, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2141364', 'n_tokens_mistral': 4807, 'n_tokens_neox': 4146, 'n_words': 1654}