001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 021package org.apache.directory.api.util; 022 023 024import org.apache.directory.api.i18n.I18n; 025 026 027/** 028 * Encoding and decoding of Base64 characters to and from raw bytes. 029 * 030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 031 */ 032public final class Base64 033{ 034 035 /** code characters for values 0..63 */ 036 private static final char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" 037 .toCharArray(); 038 039 /** lookup table for converting base64 characters to value in range 0..63 */ 040 private static final byte[] CODES = new byte[256]; 041 042 static 043 { 044 for ( int ii = 0; ii < 256; ii++ ) 045 { 046 CODES[ii] = -1; 047 } 048 049 for ( int ii = 'A'; ii <= 'Z'; ii++ ) 050 { 051 CODES[ii] = ( byte ) ( ii - 'A' ); 052 } 053 054 for ( int ii = 'a'; ii <= 'z'; ii++ ) 055 { 056 CODES[ii] = ( byte ) ( 26 + ii - 'a' ); 057 } 058 059 for ( int ii = '0'; ii <= '9'; ii++ ) 060 { 061 CODES[ii] = ( byte ) ( 52 + ii - '0' ); 062 } 063 064 CODES['+'] = 62; 065 CODES['/'] = 63; 066 } 067 068 /** 069 * Private constructor. 070 */ 071 private Base64() 072 { 073 } 074 075 076 /** 077 * Encodes binary data to a Base64 encoded characters. 078 * 079 * @param data 080 * the array of bytes to encode 081 * @return base64-coded character array. 082 * @deprecated Use the java.util.Base64.getEncoder().encode(byte[]) method instead 083 */ 084 public static char[] encode( byte[] data ) 085 { 086 char[] out = new char[( ( data.length + 2 ) / 3 ) * 4]; 087 088 // 089 // 3 bytes encode to 4 chars. Output is always an even 090 // multiple of 4 characters. 091 // 092 for ( int i = 0, index = 0; i < data.length; i += 3, index += 4 ) 093 { 094 boolean isQuadrupel = false; 095 boolean isTripel = false; 096 097 int val = 0xFF & data[i]; 098 val <<= 8; 099 if ( ( i + 1 ) < data.length ) 100 { 101 val |= ( 0xFF & data[i + 1] ); 102 isTripel = true; 103 } 104 105 val <<= 8; 106 if ( ( i + 2 ) < data.length ) 107 { 108 val |= ( 0xFF & data[i + 2] ); 109 isQuadrupel = true; 110 } 111 112 out[index + 3] = ALPHABET[ isQuadrupel ? ( val & 0x3F ) : 64 ]; 113 val >>= 6; 114 out[index + 2] = ALPHABET[ isTripel ? ( val & 0x3F ) : 64 ]; 115 val >>= 6; 116 out[index + 1] = ALPHABET[val & 0x3F]; 117 val >>= 6; 118 out[index + 0] = ALPHABET[val & 0x3F]; 119 } 120 return out; 121 } 122 123 124 /** 125 * Decodes a BASE-64 encoded stream to recover the original data. White 126 * space before and after will be trimmed away, but no other manipulation of 127 * the input will be performed. As of version 1.2 this method will properly 128 * handle input containing junk characters (newlines and the like) rather 129 * than throwing an error. It does this by pre-parsing the input and 130 * generating from that a count of VALID input characters. 131 * 132 * @param data 133 * data to decode. 134 * @return the decoded binary data. 135 * @deprecated Use the java.util.Base64.geDecoder().decode(String) method instead 136 */ 137 public static byte[] decode( char[] data ) 138 { 139 // as our input could contain non-BASE64 data (newlines, 140 // whitespace of any sort, whatever) we must first adjust 141 // our count of USABLE data so that... 142 // (a) we don't misallocate the output array, and 143 // (b) think that we miscalculated our data length 144 // just because of extraneous throw-away junk 145 146 int tempLen = data.length; 147 148 for ( char c : data ) 149 { 150 if ( ( c > 255 ) || CODES[c] < 0 ) 151 { 152 // ignore non-valid chars and padding 153 --tempLen; 154 } 155 } 156 // calculate required length: 157 // -- 3 bytes for every 4 valid base64 chars 158 // -- plus 2 bytes if there are 3 extra base64 chars, 159 // or plus 1 byte if there are 2 extra. 160 161 int len = ( tempLen / 4 ) * 3; 162 163 if ( ( tempLen % 4 ) == 3 ) 164 { 165 len += 2; 166 } 167 168 if ( ( tempLen % 4 ) == 2 ) 169 { 170 len += 1; 171 } 172 173 byte[] out = new byte[len]; 174 175 // # of excess bits stored in accum excess bits 176 int shift = 0; 177 int accum = 0; 178 int index = 0; 179 180 // we now go through the entire array (NOT using the 'tempLen' value) 181 for ( char c : data ) 182 { 183 int value = ( c > 255 ) ? -1 : CODES[c]; 184 185 // skip over non-code bits 186 if ( value >= 0 ) 187 { 188 // shift up by 6 each time thru 189 // loop, with new bits being put in 190 // at the bottom. whenever there 191 // are 8 or more shifted in, write them 192 // out (from the top, leaving any excess 193 // at the bottom for next iteration. 194 accum <<= 6; 195 shift += 6; 196 accum |= value; 197 198 if ( shift >= 8 ) 199 { 200 shift -= 8; 201 out[index++] = ( byte ) ( ( accum >> shift ) & 0xff ); 202 } 203 } 204 // we will also have skipped processing a padding null byte ('=') here; 205 // these are used ONLY for padding to an even length and do not legally 206 // occur as encoded data. for this reason we can ignore the fact 207 // that no index++ operation occurs in that special case: the out[] array 208 // is initialized to all-zero bytes to start with and that works to our 209 // advantage in this combination. 210 } 211 212 // if there is STILL something wrong we just have to throw up now! 213 if ( index != out.length ) 214 { 215 throw new Error( I18n.err( I18n.ERR_17027_WRONG_DATA_LENGTH, index, out.length ) ); 216 } 217 218 return out; 219 } 220}