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 */ 083 public static char[] encode( byte[] data ) 084 { 085 char[] out = new char[( ( data.length + 2 ) / 3 ) * 4]; 086 087 // 088 // 3 bytes encode to 4 chars. Output is always an even 089 // multiple of 4 characters. 090 // 091 for ( int i = 0, index = 0; i < data.length; i += 3, index += 4 ) 092 { 093 boolean isQuadrupel = false; 094 boolean isTripel = false; 095 096 int val = 0xFF & data[i]; 097 val <<= 8; 098 if ( ( i + 1 ) < data.length ) 099 { 100 val |= ( 0xFF & data[i + 1] ); 101 isTripel = true; 102 } 103 104 val <<= 8; 105 if ( ( i + 2 ) < data.length ) 106 { 107 val |= ( 0xFF & data[i + 2] ); 108 isQuadrupel = true; 109 } 110 111 out[index + 3] = ALPHABET[ isQuadrupel ? ( val & 0x3F ) : 64 ]; 112 val >>= 6; 113 out[index + 2] = ALPHABET[ isTripel ? ( val & 0x3F ) : 64 ]; 114 val >>= 6; 115 out[index + 1] = ALPHABET[val & 0x3F]; 116 val >>= 6; 117 out[index + 0] = ALPHABET[val & 0x3F]; 118 } 119 return out; 120 } 121 122 123 /** 124 * Decodes a BASE-64 encoded stream to recover the original data. White 125 * space before and after will be trimmed away, but no other manipulation of 126 * the input will be performed. As of version 1.2 this method will properly 127 * handle input containing junk characters (newlines and the like) rather 128 * than throwing an error. It does this by pre-parsing the input and 129 * generating from that a count of VALID input characters. 130 * 131 * @param data 132 * data to decode. 133 * @return the decoded binary data. 134 */ 135 public static byte[] decode( char[] data ) 136 { 137 // as our input could contain non-BASE64 data (newlines, 138 // whitespace of any sort, whatever) we must first adjust 139 // our count of USABLE data so that... 140 // (a) we don't misallocate the output array, and 141 // (b) think that we miscalculated our data length 142 // just because of extraneous throw-away junk 143 144 int tempLen = data.length; 145 146 for ( char c : data ) 147 { 148 if ( ( c > 255 ) || CODES[c] < 0 ) 149 { 150 // ignore non-valid chars and padding 151 --tempLen; 152 } 153 } 154 // calculate required length: 155 // -- 3 bytes for every 4 valid base64 chars 156 // -- plus 2 bytes if there are 3 extra base64 chars, 157 // or plus 1 byte if there are 2 extra. 158 159 int len = ( tempLen / 4 ) * 3; 160 161 if ( ( tempLen % 4 ) == 3 ) 162 { 163 len += 2; 164 } 165 166 if ( ( tempLen % 4 ) == 2 ) 167 { 168 len += 1; 169 } 170 171 byte[] out = new byte[len]; 172 173 // # of excess bits stored in accum excess bits 174 int shift = 0; 175 int accum = 0; 176 int index = 0; 177 178 // we now go through the entire array (NOT using the 'tempLen' value) 179 for ( char c : data ) 180 { 181 int value = ( c > 255 ) ? -1 : CODES[c]; 182 183 // skip over non-code bits 184 if ( value >= 0 ) 185 { 186 // shift up by 6 each time thru 187 // loop, with new bits being put in 188 // at the bottom. whenever there 189 // are 8 or more shifted in, write them 190 // out (from the top, leaving any excess 191 // at the bottom for next iteration. 192 accum <<= 6; 193 shift += 6; 194 accum |= value; 195 196 if ( shift >= 8 ) 197 { 198 shift -= 8; 199 out[index++] = ( byte ) ( ( accum >> shift ) & 0xff ); 200 } 201 } 202 // we will also have skipped processing a padding null byte ('=') here; 203 // these are used ONLY for padding to an even length and do not legally 204 // occur as encoded data. for this reason we can ignore the fact 205 // that no index++ operation occurs in that special case: the out[] array 206 // is initialized to all-zero bytes to start with and that works to our 207 // advantage in this combination. 208 } 209 210 // if there is STILL something wrong we just have to throw up now! 211 if ( index != out.length ) 212 { 213 throw new Error( I18n.err( I18n.ERR_17027_WRONG_DATA_LENGTH, index, out.length ) ); 214 } 215 216 return out; 217 } 218}