Source for gnu.inet.util.BASE64

   1: /*
   2:  * BASE64.java
   3:  * Copyright (C) 2003 The Free Software Foundation
   4:  * 
   5:  * This file is part of GNU inetlib, a library.
   6:  * 
   7:  * GNU inetlib is free software; you can redistribute it and/or modify
   8:  * it under the terms of the GNU General Public License as published by
   9:  * the Free Software Foundation; either version 2 of the License, or
  10:  * (at your option) any later version.
  11:  * 
  12:  * GNU inetlib is distributed in the hope that it will be useful,
  13:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15:  * GNU General Public License for more details.
  16:  * 
  17:  * You should have received a copy of the GNU General Public License
  18:  * along with this library; if not, write to the Free Software
  19:  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20:  *
  21:  * Linking this library statically or dynamically with other modules is
  22:  * making a combined work based on this library.  Thus, the terms and
  23:  * conditions of the GNU General Public License cover the whole
  24:  * combination.
  25:  *
  26:  * As a special exception, the copyright holders of this library give you
  27:  * permission to link this library with independent modules to produce an
  28:  * executable, regardless of the license terms of these independent
  29:  * modules, and to copy and distribute the resulting executable under
  30:  * terms of your choice, provided that you also meet, for each linked
  31:  * independent module, the terms and conditions of the license of that
  32:  * module.  An independent module is a module which is not derived from
  33:  * or based on this library.  If you modify this library, you may extend
  34:  * this exception to your version of the library, but you are not
  35:  * obliged to do so.  If you do not wish to do so, delete this
  36:  * exception statement from your version.
  37:  */
  38: 
  39: package gnu.inet.util;
  40: 
  41: /**
  42:  * Encodes and decodes text according to the BASE64 encoding.
  43:  *
  44:  * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
  45:  */
  46: public final class BASE64
  47: {
  48: 
  49:   private static final byte[] src = {
  50:     0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
  51:     0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54,
  52:     0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
  53:     0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
  54:     0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  55:     0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  56:     0x38, 0x39, 0x2b, 0x2f
  57:   };
  58: 
  59:   private static final byte[] dst;
  60:   static
  61:   {
  62:     dst = new byte[0x100];
  63:     for (int i = 0x0; i < 0xff; i++)
  64:       {
  65:         dst[i] = -1;
  66:       }
  67:     for (int i = 0; i < src.length; i++)
  68:       {
  69:         dst[src[i]] = (byte) i;
  70:       }
  71:   }
  72: 
  73:   private BASE64()
  74:   {
  75:   }
  76: 
  77:   /**
  78:    * Encode the specified byte array using the BASE64 algorithm.
  79:    *
  80:    * @param bs the source byte array
  81:    */
  82:   public static byte[] encode(byte[] bs)
  83:   {
  84:     int si = 0, ti = 0;         // source/target array indices
  85:     byte[] bt = new byte[((bs.length + 2) * 4) / 3];     // target byte array
  86:     for (; si < bs.length; si += 3)
  87:       {
  88:         int buflen = bs.length - si;
  89:         if (buflen == 1)
  90:           {
  91:             byte b = bs[si];
  92:             int i = 0;
  93:             boolean flag = false;
  94:             bt[ti++] = src[b >>> 2 & 0x3f];
  95:             bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)];
  96:           }
  97:         else if (buflen == 2)
  98:           {
  99:             byte b1 = bs[si], b2 = bs[si + 1];
 100:             int i = 0;
 101:             bt[ti++] = src[b1 >>> 2 & 0x3f];
 102:             bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
 103:             bt[ti++] = src[(b2 << 2 & 0x3c) + (i >>> 6 & 0x3)];
 104:           }
 105:         else
 106:           {
 107:             byte b1 = bs[si], b2 = bs[si + 1], b3 = bs[si + 2];
 108:             bt[ti++] = src[b1 >>> 2 & 0x3f];
 109:             bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
 110:             bt[ti++] = src[(b2 << 2 & 0x3c) + (b3 >>> 6 & 0x3)];
 111:             bt[ti++] = src[b3 & 0x3f];
 112:           }
 113:       }
 114:     if (ti < bt.length)
 115:       {
 116:         byte[] tmp = new byte[ti];
 117:         System.arraycopy(bt, 0, tmp, 0, ti);
 118:         bt = tmp;
 119:       }
 120:     /*while (ti < bt.length)
 121:       {
 122:         bt[ti++] = 0x3d;
 123:       }*/
 124:     return bt;
 125:   }
 126: 
 127:   /**
 128:    * Decode the specified byte array using the BASE64 algorithm.
 129:    *
 130:    * @param bs the source byte array
 131:    */
 132:   public static byte[] decode(byte[] bs)
 133:   {
 134:     int srclen = bs.length;
 135:     while (srclen > 0 && bs[srclen - 1] == 0x3d)
 136:       {
 137:         srclen--; /* strip padding character */
 138:       }
 139:     byte[] buffer = new byte[srclen];
 140:     int buflen = 0;
 141:     int si = 0;
 142:     int len = srclen - si;
 143:     while (len > 0)
 144:       {
 145:         byte b0 = dst[bs[si++] & 0xff];
 146:         byte b2 = dst[bs[si++] & 0xff];
 147:         buffer[buflen++] = (byte) (b0 << 2 & 0xfc | b2 >>> 4 & 0x3);
 148:         if (len > 2)
 149:           {
 150:             b0 = b2;
 151:             b2 = dst[bs[si++] & 0xff];
 152:             buffer[buflen++] = (byte) (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf);
 153:             if (len > 3)
 154:               {
 155:                 b0 = b2;
 156:                 b2 = dst[bs[si++] & 0xff];
 157:                 buffer[buflen++] = (byte) (b0 << 6 & 0xc0 | b2 & 0x3f);
 158:               }
 159:           }
 160:         len = srclen - si;
 161:       }
 162:     byte[] bt = new byte[buflen];
 163:     System.arraycopy(buffer, 0, bt, 0, buflen);
 164:     return bt;
 165:   }
 166:   
 167:   public static void main(String[] args)
 168:   {
 169:     boolean decode = false;
 170:     for (int i = 0; i < args.length; i++)
 171:       {
 172:         if (args[i].equals("-d"))
 173:           {
 174:             decode = true;
 175:           }
 176:         else
 177:           {
 178:             try
 179:               {
 180:                 byte[] in = args[i].getBytes("US-ASCII");
 181:                 byte[] out = decode ? decode(in) : encode(in);
 182:                 System.out.println(args[i] + " = " +
 183:                                    new String(out, "US-ASCII"));
 184:               }
 185:             catch (java.io.UnsupportedEncodingException e)
 186:               {
 187:                 e.printStackTrace(System.err);
 188:               }
 189:           }
 190:       }
 191:   }
 192:   
 193: }