package mt.util; import java.io.*; import java.util.*; // Copyright 2000 Jonathan White // Use/modify this however you want, just keep this and the previous line // This class provides two methods: encode and decode; it performs base64 // encoding and decoding respectively // Derived from the Base64 RFC final public class Base64 extends Object { private Base64(){} final private static char[] tr = {// look up table for encoding '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', '+', '/', '=' }; final private static int[] lu = { // look up table for decoding 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; final public static String encode(String line){ //Compute this to make input length a multiple of three int padding = line.length() % 3; // line length if(padding == 1) padding = 2; else if(padding == 2) padding = 1; //The output must be a multiple of 4 byte[] input = new byte[line.length() + padding]; char[] output = new char[(input.length/3)*4]; System.arraycopy(line.getBytes(), 0, input, 0, line.length()); //Encode: /* [7654 3210] [7654 3210] [7654 3210] to -> */ // /* [xx76 5432] [xx10 7654] [xx32 1076] [xx54 321] */ int loc = 0; for(int i = 0; i < input.length; i+=3){ //Do three blocks at a time output[loc++] = tr[input[i] >>> 2 & 0x3f]; output[loc++] = tr[((input[i] << 4) | (input[i+1] >>> 4)) & 0x3f]; output[loc++] = tr[((input[i+1] << 2) | (input[i+2] >>> 6)) & 0x3f]; output[loc++] = tr[input[i+2] & 0x3f]; } //Add the padding if(padding == 1) output[output.length-1] = '='; else if(padding == 2){ output[output.length-1] = '='; output[output.length-2] = '='; } //We are done! return new String(output); } final public static String decode(String line){ int length = line.length(); //Check to see if length is a multiple of four, if not then it is wrong //Should throw exception, oh well if(length % 4 != 0) return "INVALID"; char[] input = line.toCharArray(); byte[] output= new byte[(input.length/4) * 3]; //Decode: rearrange the bits-- reverse of the encode int loc = 0; for(int i=0; i< input.length; i+=4){ output[loc++] = (byte)(((lu[input[i]] << 2) | (lu[input[i+1]] >>> 4)) & 0xff); output[loc++] = (byte)(((lu[input[i+1]] << 4) | (lu[input[i+2]] >>> 2)) & 0xff); output[loc++] = (byte)(((lu[input[i+2]] << 6) | (lu[input[i+3]])) & 0xff); } //Remove the trailing ='s if (input[length-2] == '=') loc -= 2; else if (input[length-1] == '=') loc--; return new String(output,0,loc); } //Helper Method public static void main(String[] args){ try{ BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); String line = in.readLine(); while(line != null){ if(line.startsWith("e ")){ System.out.println(Base64.encode(line.substring(2))); }else if(line.startsWith("d ")){ System.out.println("<"+Base64.decode(line.substring(2))+">"); } line = in.readLine(); } }catch(IOException e){ e.printStackTrace(); } } }