/[classpath]/inetlib/source/gnu/inet/util/BASE64.java
ViewVC logotype

Diff of /inetlib/source/gnu/inet/util/BASE64.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by dog, Sun Oct 19 08:51:37 2003 UTC revision 1.2 by dog, Sun Oct 19 16:16:50 2003 UTC
# Line 36  package gnu.inet.util; Line 36  package gnu.inet.util;
36  public final class BASE64  public final class BASE64
37  {  {
38    
39    private static final byte[] src =    private static final byte[] src = {
   {  
40      0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,      0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
41      0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54,      0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54,
42      0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,      0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
# Line 46  public final class BASE64 Line 45  public final class BASE64
45      0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,      0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
46      0x38, 0x39, 0x2b, 0x2f      0x38, 0x39, 0x2b, 0x2f
47    };    };
48    
49    private static final byte[] dst;    private static final byte[] dst;
50    static    static
51    {    {
52      dst = new byte[0x100];      dst = new byte[0x100];
53      for (int i = 0x0; i<0xff; i++)      for (int i = 0x0; i < 0xff; i++)
54        dst[i] = -1;        dst[i] = -1;
55      for (int i = 0; i<src.length; i++)      for (int i = 0; i < src.length; i++)
56        dst[src[i]] = (byte)i;        dst[src[i]] = (byte) i;
57        
58    }    }
59      
60    private BASE64()    private BASE64()
61    {    {
62    }    }
# Line 67  public final class BASE64 Line 66  public final class BASE64
66     *     *
67     * @param bs the source byte array     * @param bs the source byte array
68     */     */
69    public static byte[] encode(byte[] bs)    public static byte[] encode(byte[]bs)
70    {    {
71      int si = 0, ti = 0; // source/target array indices      int si = 0, ti = 0;         // source/target array indices
72      byte[] bt = new byte[((bs.length+2)*4)/3]; // target byte array      byte[]bt = new byte[((bs.length + 2) * 4) / 3];     // target byte array
73      for (; si<bs.length; si+=3)      for (; si < bs.length; si += 3)
74      {      {
75        int buflen = bs.length-si;        int buflen = bs.length - si;
76        if (buflen==1)        if (buflen == 1)
77        {        {
78          byte b = bs[si];          byte b = bs[si];
79          int i = 0;          int i = 0;
80          boolean flag = false;          boolean flag = false;
81          bt[ti++] = src[b>>>2 & 0x3f];          bt[ti++] = src[b >>> 2 & 0x3f];
82          bt[ti++] = src[(b<<4 & 0x30) + (i>>>4 & 0xf)];          bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)];
83        }        }
84        else if (buflen==2)        else if (buflen == 2)
85        {        {
86          byte b1 = bs[si], b2 = bs[si+1];          byte b1 = bs[si], b2 = bs[si + 1];
87          int i = 0;          int i = 0;
88          bt[ti++] = src[b1>>>2 & 0x3f];          bt[ti++] = src[b1 >>> 2 & 0x3f];
89          bt[ti++] = src[(b1<<4 & 0x30) + (b2>>>4 & 0xf)];          bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
90          bt[ti++] = src[(b2<<2 & 0x3c) + (i>>>6 & 0x3)];          bt[ti++] = src[(b2 << 2 & 0x3c) + (i >>> 6 & 0x3)];
91        }        }
92        else        else
93        {        {
94          byte b1 = bs[si], b2 = bs[si+1], b3 = bs[si+2];          byte b1 = bs[si], b2 = bs[si + 1], b3 = bs[si + 2];
95          bt[ti++] = src[b1>>>2 & 0x3f];          bt[ti++] = src[b1 >>> 2 & 0x3f];
96          bt[ti++] = src[(b1<<4 & 0x30) + (b2>>>4 & 0xf)];          bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
97          bt[ti++] = src[(b2<<2 & 0x3c) + (b3>>>6 & 0x3)];          bt[ti++] = src[(b2 << 2 & 0x3c) + (b3 >>> 6 & 0x3)];
98          bt[ti++] = src[b3 & 0x3f];          bt[ti++] = src[b3 & 0x3f];
99        }        }
100      }      }
# Line 107  public final class BASE64 Line 106  public final class BASE64
106     *     *
107     * @param bs the source byte array     * @param bs the source byte array
108     */     */
109    public static byte[] decode(byte[] bs)    public static byte[] decode(byte[]bs)
110    {    {
111      byte[] buffer = new byte[bs.length];      byte[]buffer = new byte[bs.length];
112      int buflen = 0;      int buflen = 0;
113      int si = 0;      int si = 0;
114      int len = bs.length-si;      int len = bs.length - si;
115      while (len>0)      while (len > 0)
116      {      {
117        byte b0 = dst[bs[si++] & 0xff];        byte b0 = dst[bs[si++] & 0xff];
118        byte b2 = dst[bs[si++] & 0xff];        byte b2 = dst[bs[si++] & 0xff];
119        buffer[buflen++] = (byte)(b0<<2 & 0xfc | b2>>>4 & 0x3);        buffer[buflen++] = (byte) (b0 << 2 & 0xfc | b2 >>> 4 & 0x3);
120        if (len>2)        if (len > 2)
121        {        {
122          b0 = b2;          b0 = b2;
123          b2 = dst[bs[si++] & 0xff];          b2 = dst[bs[si++] & 0xff];
124          buffer[buflen++] = (byte)(b0<<4 & 0xf0 | b2>>>2 & 0xf);          buffer[buflen++] = (byte) (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf);
125          if (len>3)          if (len > 3)
126          {          {
127            b0 = b2;            b0 = b2;
128            b2 = dst[bs[si++] & 0xff];            b2 = dst[bs[si++] & 0xff];
129            buffer[buflen++] = (byte)(b0<<6 & 0xc0 | b2 & 0x3f);            buffer[buflen++] = (byte) (b0 << 6 & 0xc0 | b2 & 0x3f);
130          }          }
131        }        }
132        len = bs.length-si;        len = bs.length - si;
133      }      }
134      byte[] bt = new byte[buflen];      byte[]bt = new byte[buflen];
135      System.arraycopy(buffer, 0, bt, 0, buflen);      System.arraycopy(buffer, 0, bt, 0, buflen);
136      return bt;      return bt;
137    }    }
138    
139    public static void main(String[] args)    public static void main(String[]args)
140    {    {
141      boolean decode = false;      boolean decode = false;
142      for (int i=0; i<args.length; i++)      for (int i = 0; i < args.length; i++)
143      {      {
144        if (args[i].equals("-d"))        if (args[i].equals("-d"))
145          decode = true;          decode = true;
# Line 148  public final class BASE64 Line 147  public final class BASE64
147        {        {
148          try          try
149          {          {
150            byte[] in = args[i].getBytes("US-ASCII");            byte[]in = args[i].getBytes("US-ASCII");
151            byte[] out = decode ? decode(in) : encode(in);            byte[]out = decode ? decode(in) : encode(in);
152            System.out.println(args[i]+" = "+new String(out, "US-ASCII"));            System.out.println(args[i] + " = " + new String(out, "US-ASCII"));
153          }          }
154          catch (java.io.UnsupportedEncodingException e)          catch(java.io.UnsupportedEncodingException e)
155          {          {
156            e.printStackTrace(System.err);            e.printStackTrace(System.err);
157          }          }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26