001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.coder;
020
021import lucee.commons.io.CharsetUtil;
022
023
024public final class Base64Util {
025        
026        private static byte base64Alphabet[];
027        private static byte lookUpBase64Alphabet[];
028        
029        
030        /**
031         * @param arrayOctect byte array to check
032         * @return true if base64
033         */
034        public static boolean isBase64(byte arrayOctect[]) {
035                int length = arrayOctect.length;
036                if(length == 0)
037                        return true;
038                for(int i = 0; i < length; i++) {
039                        if(!isBase64(arrayOctect[i]))
040                                return false;
041                }
042                return true;
043        }
044        /**
045         * @param octect byte to check
046         * @return true if base64
047         */
048        public static boolean isBase64(byte octect) {
049                return octect == 61 || base64Alphabet[octect] != -1;
050        }
051        /**
052         * @param isValidString string to check
053         * @return true if base64
054         */
055        public static boolean isBase64(String isValidString) {
056                return isBase64(isValidString.getBytes(CharsetUtil.UTF8));
057        }
058        /** Initializations */
059        static {
060                base64Alphabet = new byte[255];
061                lookUpBase64Alphabet = new byte[64];
062                for(int i = 0; i < 255; i++)
063                        base64Alphabet[i] = -1;
064                for(int i = 90; i >= 65; i--)
065                        base64Alphabet[i] = (byte)(i - 65);
066                for(int i = 122; i >= 97; i--)
067                        base64Alphabet[i] = (byte)((i - 97) + 26);
068                for(int i = 57; i >= 48; i--)
069                        base64Alphabet[i] = (byte)((i - 48) + 52);
070                base64Alphabet[43] = 62;
071                base64Alphabet[47] = 63;
072                for(int i = 0; i <= 25; i++)
073                        lookUpBase64Alphabet[i] = (byte)(65 + i);
074                int i = 26;
075                for(int j = 0; i <= 51; j++) {
076                        lookUpBase64Alphabet[i] = (byte)(97 + j);
077                        i++;
078                }
079                i = 52;
080                for(int j = 0; i <= 61; j++) {
081                        lookUpBase64Alphabet[i] = (byte)(48 + j);
082                        i++;
083                }
084                lookUpBase64Alphabet[62] = 43;
085                lookUpBase64Alphabet[63] = 47;
086        }
087}
088