001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.io.ProtectedObjectInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
019    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    
023    import java.io.ObjectInputStream;
024    import java.io.ObjectOutputStream;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class Base64 {
030    
031            public static byte[] decode(String base64) {
032                    if (Validator.isNull(base64)) {
033                            return new byte[0];
034                    }
035    
036                    int pad = 0;
037    
038                    for (int i = base64.length() - 1; base64.charAt(i) == CharPool.EQUAL;
039                                    i--) {
040    
041                            pad++;
042                    }
043    
044                    int length = (base64.length() * 6) / 8 - pad;
045                    byte[] raw = new byte[length];
046                    int rawindex = 0;
047    
048                    for (int i = 0; i < base64.length(); i += 4) {
049                            int block = getValue(base64.charAt(i)) << 18;
050    
051                            block += getValue(base64.charAt(i + 1)) << 12;
052                            block += getValue(base64.charAt(i + 2)) << 6;
053                            block += getValue(base64.charAt(i + 3));
054    
055                            for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
056                                    raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff);
057                            }
058    
059                            rawindex += 3;
060                    }
061    
062                    return raw;
063            }
064    
065            public static String encode(byte[] raw) {
066                    return encode(raw, 0, raw.length);
067            }
068    
069            public static String encode(byte[] raw, int offset, int length) {
070                    int lastIndex = Math.min(raw.length, offset + length);
071    
072                    StringBuilder sb = new StringBuilder(
073                            ((lastIndex - offset) / 3 + 1) * 4);
074    
075                    for (int i = offset; i < lastIndex; i += 3) {
076                            sb.append(encodeBlock(raw, i, lastIndex));
077                    }
078    
079                    return sb.toString();
080            }
081    
082            public static String fromURLSafe(String base64) {
083                    return StringUtil.replace(
084                            base64,
085                            new String[] {
086                                    StringPool.MINUS, StringPool.STAR, StringPool.UNDERLINE
087                            },
088                            new String[] {StringPool.PLUS, StringPool.EQUAL, StringPool.SLASH});
089            }
090    
091            public static String objectToString(Object o) {
092                    if (o == null) {
093                            return null;
094                    }
095    
096                    UnsyncByteArrayOutputStream ubaos = new UnsyncByteArrayOutputStream(
097                            32000);
098    
099                    try (ObjectOutputStream os = new ObjectOutputStream(ubaos)) {
100                            os.writeObject(o);
101                    }
102                    catch (Exception e) {
103                            _log.error(e, e);
104                    }
105    
106                    return encode(ubaos.unsafeGetByteArray(), 0, ubaos.size());
107            }
108    
109            public static Object stringToObject(String s) {
110                    return _stringToObject(s, null, false);
111            }
112    
113            public static Object stringToObject(String s, ClassLoader classLoader) {
114                    return _stringToObject(s, classLoader, false);
115            }
116    
117            public static Object stringToObjectSilent(String s) {
118                    return _stringToObject(s, null, true);
119            }
120    
121            public static Object stringToObjectSilent(
122                    String s, ClassLoader classLoader) {
123    
124                    return _stringToObject(s, classLoader, true);
125            }
126    
127            public static String toURLSafe(String base64) {
128                    return StringUtil.replace(
129                            base64,
130                            new String[] {StringPool.PLUS, StringPool.EQUAL, StringPool.SLASH},
131                            new String[] {
132                                    StringPool.MINUS, StringPool.STAR, StringPool.UNDERLINE
133                            });
134            }
135    
136            protected static char[] encodeBlock(byte[] raw, int offset, int lastIndex) {
137                    int block = 0;
138                    int slack = lastIndex - offset - 1;
139                    int end = slack < 2 ? slack : 2;
140    
141                    for (int i = 0; i <= end; i++) {
142                            byte b = raw[offset + i];
143    
144                            int neuter = b >= 0 ? ((int) (b)) : b + 256;
145                            block += neuter << 8 * (2 - i);
146                    }
147    
148                    char[] base64 = new char[4];
149    
150                    for (int i = 0; i < 4; i++) {
151                            int sixbit = block >>> 6 * (3 - i) & 0x3f;
152                            base64[i] = getChar(sixbit);
153                    }
154    
155                    if (slack < 1) {
156                            base64[2] = CharPool.EQUAL;
157                    }
158    
159                    if (slack < 2) {
160                            base64[3] = CharPool.EQUAL;
161                    }
162    
163                    return base64;
164            }
165    
166            protected static char getChar(int sixbit) {
167                    if ((sixbit >= 0) && (sixbit <= 25)) {
168                            return (char)(65 + sixbit);
169                    }
170    
171                    if ((sixbit >= 26) && (sixbit <= 51)) {
172                            return (char)(97 + (sixbit - 26));
173                    }
174    
175                    if ((sixbit >= 52) && (sixbit <= 61)) {
176                            return (char)(48 + (sixbit - 52));
177                    }
178    
179                    if (sixbit == 62) {
180                            return CharPool.PLUS;
181                    }
182    
183                    return sixbit != 63 ? CharPool.QUESTION : CharPool.SLASH;
184            }
185    
186            protected static int getValue(char c) {
187                    if ((c >= CharPool.UPPER_CASE_A) && (c <= CharPool.UPPER_CASE_Z)) {
188                            return c - 65;
189                    }
190    
191                    if ((c >= CharPool.LOWER_CASE_A) && (c <= CharPool.LOWER_CASE_Z)) {
192                            return (c - 97) + 26;
193                    }
194    
195                    if ((c >= CharPool.NUMBER_0) && (c <= CharPool.NUMBER_9)) {
196                            return (c - 48) + 52;
197                    }
198    
199                    if (c == CharPool.PLUS) {
200                            return 62;
201                    }
202    
203                    if (c == CharPool.SLASH) {
204                            return 63;
205                    }
206    
207                    return c != CharPool.EQUAL ? -1 : 0;
208            }
209    
210            private static Object _stringToObject(
211                    String s, ClassLoader classLoader, boolean silent) {
212    
213                    if (s == null) {
214                            return null;
215                    }
216    
217                    byte[] bytes = decode(s);
218    
219                    UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
220                            bytes);
221    
222                    try {
223                            ObjectInputStream is = null;
224    
225                            if (classLoader == null) {
226                                    is = new ProtectedObjectInputStream(ubais);
227                            }
228                            else {
229                                    is = new ProtectedClassLoaderObjectInputStream(
230                                            ubais, classLoader);
231                            }
232    
233                            return is.readObject();
234                    }
235                    catch (Exception e) {
236                            if (!silent) {
237                                    _log.error(e, e);
238                            }
239                    }
240    
241                    return null;
242            }
243    
244            private static final Log _log = LogFactoryUtil.getLog(Base64.class);
245    
246    }