jackass110 发表于 2010-7-16 11:59:32

java版变异base64 请高人转换成AU3

java版变异base64 请高人转换成AU3


public   class   Base64
{

      public   Base64()
      {
      }

      private   static   int   decode0(char   ibuf[],   byte   obuf[],   int   wp)
      {
                int   outlen   =   3;
                if(ibuf   ==   '= ')
                        outlen   =   2;
                if(ibuf   ==   '= ')
                        outlen   =   1;
                int   b0   =   S_DECODETABLE];
                int   b1   =   S_DECODETABLE];
                int   b2   =   S_DECODETABLE];
                int   b3   =   S_DECODETABLE];
                switch(outlen)
                {
                case   1:   //   '\001 '
                        obuf   =   (byte)(b0   < <   2   &   0xfc   |   b1   > >   4   &   3);
                        return   1;

                case   2:   //   '\002 '
                        obuf   =   (byte)(b0   < <   2   &   0xfc   |   b1   > >   4   &   3);
                        obuf   =   (byte)(b1   < <   4   &   0xf0   |   b2   > >   2   &   0xf);
                        return   2;

                case   3:   //   '\003 '
                        obuf   =   (byte)(b0   < <   2   &   0xfc   |   b1   > >   4   &   3);
                        obuf   =   (byte)(b1   < <   4   &   0xf0   |   b2   > >   2   &   0xf);
                        obuf   =   (byte)(b2   < <   6   &   0xc0   |   b3   &   0x3f);
                        return   3;
                }
                throw   new   RuntimeException( " ");
      }

      public   static   byte[]   decode(char   data[],   int   off,   int   len)
      {
                char   ibuf[]   =   new   char;
                int   ibufcount   =   0;
                byte   obuf[]   =   new   byte[(len   /   4)   *   3   +   3];
                int   obufcount   =   0;
                for(int   i   =   off;   i   <   off   +   len;   i++)
                {
                        char   ch   =   data;
                        if(ch   ==   '= '   ||   ch   <   S_DECODETABLE.length   &&   S_DECODETABLE   !=   127)
                        {
                              ibuf   =   ch;
                              if(ibufcount   ==   ibuf.length)
                              {
                                        ibufcount   =   0;
                                        obufcount   +=   decode0(ibuf,   obuf,   obufcount);
                              }
                        }
                }

                if(obufcount   ==   obuf.length)
                {
                        return   obuf;
                }   else
                {
                        byte   ret[]   =   new   byte;
                        System.arraycopy(obuf,   0,   ret,   0,   obufcount);
                        return   ret;
                }
      }

      public   static   byte[]   decode(String   data)
      {
                char   ibuf[]   =   new   char;
                int   ibufcount   =   0;
                byte   obuf[]   =   new   byte[(data.length()   /   4)   *   3   +   3];
                int   obufcount   =   0;
                for(int   i   =   0;   i   <   data.length();   i++)
                {
                        char   ch   =   data.charAt(i);
                        if(ch   ==   '= '   ||   ch   <   S_DECODETABLE.length   &&   S_DECODETABLE   !=   127)
                        {
                              ibuf   =   ch;
                              if(ibufcount   ==   ibuf.length)
                              {
                                        ibufcount   =   0;
                                        obufcount   +=   decode0(ibuf,   obuf,   obufcount);
                              }
                        }
                }

                if(obufcount   ==   obuf.length)
                {
                        return   obuf;
                }   else
                {
                        byte   ret[]   =   new   byte;
                        System.arraycopy(obuf,   0,   ret,   0,   obufcount);
                        return   ret;
                }
      }

      public   static   String   encode(byte   data[])
      {
                return   encode(data,   0,   data.length);
      }

      public   static   String   encode(byte   data[],   int   off,   int   len)
      {
                if(len   <=   0)
                        return   " ";
                char   out[]   =   new   char[(len   /   3)   *   4   +   4];
                int   rindex   =   off;
                int   windex   =   0;
                int   rest;
                for(rest   =   len   -   off;   rest   > =   3;   rest   -=   3)
                {
                        int   i   =   ((data   &   0xff)   < <   16)   +   ((data   &   0xff)   < <   8)   +   (data   &   0xff);
                        out   =   S_BASE64CHAR;
                        out   =   S_BASE64CHAR;
                        out   =   S_BASE64CHAR;
                        out   =   S_BASE64CHAR;
                        rindex   +=   3;
                }

                if(rest   ==   1)
                {
                        int   i   =   data   &   0xff;
                        out   =   S_BASE64CHAR;
                        out   =   S_BASE64CHAR;
                        out   =   '= ';
                        out   =   '= ';
                }   else
                if(rest   ==   2)
                {
                        int   i   =   ((data   &   0xff)   < <   8)   +   (data   &   0xff);
                        out   =   S_BASE64CHAR;
                        out   =   S_BASE64CHAR;
                        out   =   S_BASE64CHAR;
                        out   =   '= ';
                }
                return   new   String(out,   0,   windex);
      }

      private   static   final   char   S_BASE64CHAR[]   =   {
                '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 ',   '+ ',   '/ '
      };
      private   static   final   char   S_BASE64PAD   =   61;
      private   static   final   byte   S_DECODETABLE[];

      static   
      {
                S_DECODETABLE   =   new   byte;
                for(int   i   =   0;   i   <   S_DECODETABLE.length;   i++)
                        S_DECODETABLE   =   127;

                for(int   i   =   0;   i   <   S_BASE64CHAR.length;   i++)
                        S_DECODETABLE]   =   (byte)i;

      }
}
页: [1]
查看完整版本: java版变异base64 请高人转换成AU3