-2

Now, I do Flash Actionscript 3.0 development, I have a Base64bitEncoder class with the encodeByteArray(...) and decodeToByteArray(...) function.

but when I write the below testing code, but the byteArray.length at before Encoding and after decoding are different, could you help to see see where I wrong?

Test Sample Code as below:

        var _loc_2:XML = new XML("<formXML>aaaaa</formXML>");;

        var _loc_3:ByteArray;   
        _loc_3 = new ByteArray();
        //_loc_3.endian = Endian.LITTLE_ENDIAN;
        _loc_3.writeUTFBytes(_loc_2);
        trace("Before Encoding Length:" + _loc_3.length);

        var baseString:String = Base64bitEncoder.encodeByteArray(_loc_3);
        trace(baseString);


        var strArray:ByteArray = Base64bitEncoder.decodeToByteArray(baseString);
        trace("After Decoding length:" + strArray.length);

The Output log as below:

Before Encoding Length:5

GFhAGFhA

After Decoding length:**6**

Why the after is 6, where I do wrong?

Base64bitEncoder source code as below:

package
{
    import flash.utils.*;

    public class Base64bitEncoder extends Object
    {
        private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        private static const BASE64_URL_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!";

        public function Base64bitEncoder()
        {
            return;
        }// end function

        public static function encode(param1:String, param2:Boolean = false) : String
        {
            var _loc_3:ByteArray;
            _loc_3 = new ByteArray();
            _loc_3.writeUTFBytes(param1);
            return encodeByteArray(_loc_3, param2);
        }// end function

        public static function decodeToByteArray(param1:String, param2:Boolean = false) : ByteArray
        {
            var _loc_3:ByteArray;
            var _loc_4:Array;
            var _loc_5:Array;
            var _loc_6:uint;
            var _loc_7:uint;
            var _loc_8:uint;
            _loc_3 = new ByteArray();
            _loc_4 = new Array(4);
            _loc_5 = new Array(3);
            _loc_6 = 0;
            while (_loc_6 < param1.length)
            {
                // label
                _loc_7 = 0;
                while (_loc_7++ < 4 && _loc_6 + _loc_7 < param1.length)
                {
                    // label
                    _loc_4[_loc_7] = param2 == true ? (BASE64_URL_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7))) : (BASE64_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7)));
                }// end while
                _loc_5[0] = (_loc_4[0] << 2) + ((_loc_4[1] & 48) >> 4);
                _loc_5[1] = ((_loc_4[1] & 15) << 4) + ((_loc_4[2] & 60) >> 2);
                _loc_5[2] = ((_loc_4[2] & 3) << 6) + _loc_4[3];
                _loc_8 = 0;
                while (_loc_8++ < _loc_5.length)
                {
                    // label
                    if (_loc_4[_loc_8 + 1] == 64)
                    {
                        break;
                    }// end if
                    _loc_3.writeByte(_loc_5[_loc_8]);
                }// end while
                _loc_6 = _loc_6 + 4;
            }// end while
            _loc_3.position = 0;
            return _loc_3;
        }// end function

        public static function encodeByteArray(param1:ByteArray, param2:Boolean = false) : String
        {
            var _loc_3:String;
            var _loc_4:Array;
            var _loc_5:Array;
            var _loc_6:uint;
            var _loc_7:uint;
            var _loc_8:uint;
            _loc_3 = "";
            _loc_5 = new Array(4);
            param1.position = 0;
            while (param1.bytesAvailable > 0)
            {
                // label
                _loc_4 = [];
                _loc_6 = 0;
                while (_loc_6++ < 3 && param1.bytesAvailable > 0)
                {
                    // label
                    _loc_4[_loc_6] = param1.readUnsignedByte();
                }// end while
                _loc_5[0] = (_loc_4[0] & 252) >> 2;
                _loc_5[1] = (_loc_4[0] & 3) << 4 | _loc_4[1] >> 4;
                _loc_5[2] = (_loc_4[1] & 15) << 2 | _loc_4[2] >> 6;
                _loc_5[3] = _loc_4[2] & 63;
                _loc_7 = _loc_4.length;
                while (_loc_7++ < 3)
                {
                    // label
                    _loc_5[_loc_7 + 1] = 64;
                }// end while
                _loc_8 = 0;
                while (_loc_8++ < _loc_5.length)
                {
                    // label
                    _loc_3 = _loc_3 + (param2 == true ? (BASE64_URL_CHARS.charAt(_loc_5[_loc_8])) : (BASE64_CHARS.charAt(_loc_5[_loc_8])));
                }// end while
            }// end while
            return _loc_3;
        }// end function

        public static function decode(param1:String, param2:Boolean = false) : String
        {
            var _loc_3:ByteArray;
            _loc_3 = decodeToByteArray(param1, param2);
            return _loc_3.readUTFBytes(_loc_3.length);
        }// end function

    }
}
Ian.Y
  • 1
  • 1
  • 2
    That's a decompiled code and it's not yours, so why you told us : "I do Flash AS3 ... I have a Base64bitEncoder class ... I write the below testing code ... I wrong ... I do wrong " ... but as you said it, you do really wrong ! Try to write your own code then if you have problems, post them here and you surely will get help, but not when using the code of others ! – akmozo Apr 09 '16 at 16:09
  • @akmozo, Yes, your are right, the Base64bitEncoder class code is not developed by me, I just refer to it. But I found that it may has some issue in decodeByteArray method, as I am not familiar with the Base64, so I past the code here to see whether the Master can point out what /where 's the issue . thanks. – Ian.Y Apr 10 '16 at 03:41
  • Your problem is with using Base64 in AS3 or just with that code ? There are many [AS3 libraries](http://stackoverflow.com/questions/6625347/best-class-for-base64-encoding-decoding-action-script) to do that (and many of them are open source) ... – akmozo Apr 10 '16 at 08:19

1 Answers1

0

@akmozo, today, I spend some time to study the base64 theory, I fix the issue, thanks.

update code:

    public static function encodeByteArray(param1:ByteArray, param2:Boolean = false) : String
    {
        var _loc_3:String;
        var _loc_4:Array;
        var _loc_5:Array;
        var _loc_6:uint;
        var _loc_7:uint;
        var _loc_8:uint;
        _loc_3 = "";
        _loc_5 = new Array(4);
        param1.position = 0;
        while (param1.bytesAvailable > 0)
        {
            // label
            _loc_4 = [];
            _loc_6 = 0;
            while (_loc_6 < 3 && param1.bytesAvailable > 0)
            {
                // label
                _loc_4[_loc_6] = param1.readUnsignedByte();
                _loc_6 = _loc_6 + 1;

            }// end while
            _loc_5[0] = (_loc_4[0] & 252) >> 2;
            _loc_5[1] = (_loc_4[0] & 3) << 4 | _loc_4[1] >> 4;
            _loc_5[2] = (_loc_4[1] & 15) << 2 | _loc_4[2] >> 6;
            _loc_5[3] = _loc_4[2] & 63;

            /*
            trace("--------------------------------");
            trace("0:" + _loc_4[0]);
            trace("1:" + _loc_4[1]);
            trace("3:" + _loc_4[2]);

            trace("len:"+_loc_4.length);

            trace("++++++++++++++++++++++++++++++++");
            */

            _loc_7 = _loc_4.length;

            while (_loc_7 < 3)
            {
                // label
                _loc_5[_loc_7 + 1] = 64;
                _loc_7 = _loc_7 + 1;
            }// end while
            _loc_8 = 0;
            while (_loc_8 < _loc_5.length)
            {
                // label
                _loc_3 = _loc_3 + (param2 == true ? (BASE64_URL_CHARS.charAt(_loc_5[_loc_8])) : (BASE64_CHARS.charAt(_loc_5[_loc_8])));
                _loc_8 = _loc_8 + 1;
            }// end while
        }// end while
        return _loc_3;
    }// end function

    public static function decodeToByteArray(param1:String, param2:Boolean = false) : ByteArray
    {
        var _loc_3:ByteArray;
        var _loc_4:Array;
        var _loc_5:Array;
        var _loc_6:uint;
        var _loc_7:uint;
        var _loc_8:uint;
        _loc_3 = new ByteArray();
        _loc_4 = new Array(4);
        _loc_5 = new Array(3);
        _loc_6 = 0;
        while (_loc_6 < param1.length)
        {
            // label
            _loc_7 = 0;
            while (_loc_7 < 4 && _loc_6 + _loc_7 < param1.length)
            {
                // label
                _loc_4[_loc_7] = param2 == true ? (BASE64_URL_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7))) : (BASE64_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7)));
                _loc_7 = _loc_7 + 1;
            }// end while
            _loc_5[0] = (_loc_4[0] << 2) + ((_loc_4[1] & 48) >> 4);
            _loc_5[1] = ((_loc_4[1] & 15) << 4) + ((_loc_4[2] & 60) >> 2);
            _loc_5[2] = ((_loc_4[2] & 3) << 6) + _loc_4[3];
            _loc_8 = 0;
            while (_loc_8 < _loc_5.length)
            {
                // label
                if (_loc_4[_loc_8 + 1] == 64)
                {
                    break;
                }// end if
                _loc_3.writeByte(_loc_5[_loc_8]);
                _loc_8 = _loc_8 + 1;
            }// end while
            _loc_6 = _loc_6 + 4;
        }// end while
        _loc_3.position = 0;
        return _loc_3;
    }// end function
Ian.Y
  • 1
  • 1