Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6330099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:48:25+00:00 2026-05-24T17:48:25+00:00

I have an actionscript class that serializes and unserializes data compatible with php’s serialization

  • 0

I have an actionscript class that serializes and unserializes data compatible with php’s serialization functions. I expanded it to support binary data but now the unserialization does not seem to work.

For example, this data is not unserialized correctly:

a:2:{i:0;s:1:"õ";i:1;a:2:{i:0;s:32:"mÎiyl·T=doÁ°ýNd_¤ÁÝ`:AåÁˆ@";i:1;s:32:"ÿ^ò`d^|“T¶&JÐÞG[±iÏ*Ÿ!–Ü’IÍ";}}

Here is the class:

package pack
{
    import flash.utils.ByteArray;
    import flash.utils.Dictionary;

    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import mx.core.*;

    use namespace mx_internal;

    public class Serializer extends Object
    {
        public static const version:String = "3.0.0";

        mx_internal static var c:uint;
        mx_internal static var pattern:RegExp = /[A-Z][a-z]{2}, \d{2} [A-Z][a-z]{2} \d{4} \d{2}:\d{2}:\d{2} \+|\-\d{4}/g

        public static function serialize(data:*):ByteArray
        {
            var bas:ByteArray = new ByteArray();
            var tmp:ByteArray = new ByteArray();
            var i:int = 0;
            var key:String;

            if(data is Boolean){
                bas.writeUTFBytes('b:');
                bas.writeUnsignedInt(data);
                bas.writeUTFBytes(';');
            } else if(data is int){
                bas.writeUTFBytes('i:');
                bas.writeUTFBytes(data);
                bas.writeUTFBytes(';');
            } else if(data is Number){
                bas.writeUTFBytes('d:');
                bas.writeUTFBytes(data);
                bas.writeUTFBytes(';');
            } else if(data is ByteArray){
                bas.writeUTFBytes('s:');
                bas.writeUTFBytes(data.length.toString());
                bas.writeUTFBytes(':"');
                bas.writeBytes(data);
                bas.writeUTFBytes('";');
            } else if(data is String){
                bas.writeUTFBytes('s:');
                bas.writeUTFBytes(data.length.toString());
                bas.writeUTFBytes(':"');
                bas.writeUTFBytes(data);
                bas.writeUTFBytes('";');
            } else if(data is Date){
                bas.writeUTFBytes('s:');
                bas.writeUTFBytes(data.toString().length.toString());
                bas.writeUTFBytes(':"');
                bas.writeUTFBytes(data);
                bas.writeUTFBytes('";');
            } else if(data is ArrayCollection){
                for(key in data){
                    tmp.writeBytes(Serializer.serialize(i));
                    tmp.writeBytes(Serializer.serialize(data[key]));
                    i += 1;
                }
                bas.writeUTFBytes('a:');
                bas.writeUTFBytes(i.toString());
                bas.writeUTFBytes(':{');
                bas.writeBytes(tmp);
                bas.writeUTFBytes('}');
            } else if(data is Array){
                for(key in data){
                    tmp.writeBytes(Serializer.serialize(i));
                    tmp.writeBytes(Serializer.serialize(data[key]));
                    i += 1;
                }
                bas.writeUTFBytes('a:');
                bas.writeUTFBytes(i.toString());
                bas.writeUTFBytes(':{');
                bas.writeBytes(tmp);
                bas.writeUTFBytes('}');
            } else if(data is Object){
                for(key in data){
                    tmp.writeBytes(Serializer.serialize(key));
                    tmp.writeBytes(Serializer.serialize(data[key]));
                    i += 1;
                }
                bas.writeUTFBytes('O:8:"stdClass":');
                bas.writeUTFBytes(i.toString());
                bas.writeUTFBytes(':{');
                bas.writeBytes(tmp);
                bas.writeUTFBytes('}');
            } else if(data == null || data == undefined){
                bas.writeUTFBytes('N;');
            } else {
                bas.writeUTFBytes('i:0;');
            }
            return bas;
        }

        public static function unserialize(data:ByteArray):*
        {
            Serializer.c = 0;
            return Serializer.unserialize_internal(data);
        }


        mx_internal static function unserialize_internal(data:ByteArray):*
        {
            var result:*;
            var tmpvar:*;
            var tmp:Array = new Array();
            var type:String = Serializer.charAt(data, Serializer.c);
            var pos:uint = 0;
            var islist:Boolean = true;
            var i:uint;

            switch(type){
                case "N":
                    Serializer.c += 2;
                    break;
                case "b":
                    result = Serializer.substr(data, Serializer.c+2, 1).toString() == '1'
                    //result = data.substr(Serializer.c+2, 1) == "1"
                    Serializer.c += 4
                    break;
                case "i":
                    tmp.push(Serializer.indexOf(data, ';', Serializer.c));
                    //tmp.push(data.indexOf(";", Serializer.c))
                    pos = Serializer.c+2
                    Serializer.c = tmp[0]+1
                    result = int(Serializer.substring(data, pos, tmp[0]));
                    //result = int(data.substring(pos,tmp[0]))
                    break;
                case "d":
                    tmp.push(Serializer.indexOf(data, ';', Serializer.c));
                    //tmp.push(data.indexOf(";", Serializer.c))
                    pos = Serializer.c + 2
                    Serializer.c = tmp[0]+1
                    result = Number(Serializer.substring(data, pos, tmp[0]));
                    //result = Number(data.substring(pos,tmp[0]))
                    break;
                case "s":
                    tmp.push(int(Serializer.indexOf(data, ':', Serializer.c+2)));
                    //tmp.push(int(data.indexOf(":", Serializer.c+2)))
                    tmp.push(tmp[0]+2)
                    pos = Serializer.c+2
                    tmp.push(0)
                    tmp.push(int(Serializer.substring(data, pos, tmp[0])));
                    //tmp.push(int(data.substring(pos, tmp[0])));
                    if(tmp[3] == 0)
                    {
                        result = "";
                        Serializer.c = pos+5
                    } else {
                        var lenc:uint = Serializer.stringBCLenght(data, Serializer.c, tmp[3]);
                        if(lenc != tmp[3])
                        {
                            result = Serializer.substr(data, tmp[0]+2, lenc);
                            //result = data.substr(tmp[0]+2, lenc);
                            Serializer.c = tmp[0]+4+lenc;
                        } else {
                            result = Serializer.substr(data, tmp[0]+2, tmp[3]);
                            //result = data.substr(tmp[0]+2, tmp[3]);
                            Serializer.c = tmp[0]+4+tmp[3];
                        }
                    }
                    if(Serializer.pattern.test(result))
                    {
                        result = new Date(result)
                    }
                    break;
                case "a":
                    //result:ByteArray;
                    pos = Serializer.c+2
                    tmp.push(int(Serializer.indexOf(data, ":", pos)))
                    tmp.push(int(Serializer.substring(data, pos, tmp[0])))          
                    //tmp.push(int(data.indexOf(":", pos)))
                    //tmp.push(int(data.substring(pos, tmp[0])))
                    Serializer.c = tmp[0]+2
                    result = []
                    for(i = 0; i < tmp[1]; i++){
                        tmpvar = Serializer.unserialize_internal(data)
                        result[tmpvar] = Serializer.unserialize_internal(data)
                        if(!(tmpvar is int) || tmpvar < 0){
                            islist = false
                        }
                    }
                    if(islist){
                        tmp.push([])
                        for(var key:uint = 0; key < result.length; key++){
                            pos = tmp[2].length
                            while(key > pos){
                                tmp[2].push(null)
                                pos +=1
                            }
                            tmp[2].push(result[key])
                        }
                        result = tmp[2]
                    }
                    Serializer.c += 1
                    break;
                case "O":
                    pos = Serializer.indexOf(data, "\"", Serializer.c)+1;
                    Serializer.c =  Serializer.indexOf(data, "\"", pos);
                    tmp.push(Serializer.substring(data, pos, Serializer.c))
                    //pos = data.indexOf("\"", Serializer.c)+1;
                    //Serializer.c =  data.indexOf("\"", pos);
                    //tmp.push(data.substring(pos, Serializer.c))
                    Serializer.c += 2
                    i = Serializer.c
                    Serializer.c = Serializer.indexOf(data, ":", i)
                    i = int(Serializer.substring(data, i, Serializer.c))
                    //Serializer.c = data.indexOf(":", i)
                    //i = int(data.substring(i, Serializer.c))
                    Serializer.c +=2;
                    result = {};
                    var tmps:*;
                    while(i > 0){
                        tmps = Serializer.unserialize_internal(data)
                        result[tmps] = Serializer.unserialize_internal(data)
                        i -= 1
                    }
                    break;
            }
            return result;
        }


        mx_internal static function stringCLenght(data:String, from:uint = 0, len:uint = 0):int
        {
            var i:uint;
            var j:uint = len;
            var startIndex:uint = from + 4 + len.toString().length;
            for (i = 0; i < j; i++){
                if (data.charCodeAt(i+startIndex) > 128)
                {
                    j = j - 1
                }
            }
            return j;
        }

        mx_internal static function stringBCLenght(data:ByteArray, from:uint = 0, len:uint = 0):int
        {
            var i:uint;
            var j:uint = len;
            var startIndex:uint = from + 4 + len.toString().length;
            for (i = 0; i < j; i++){
                if (Serializer.charCodeAt(data, i+startIndex) > 128)
                {
                    j = j - 1
                }
            }
            return j;
        }

        mx_internal static function stringLength(data:String):uint
        {
            var code:int   = 0
            var result:int = 0
            var slen:int   = data.length;
            while(slen){
                slen = slen - 1
                try
                {
                    code = data.charCodeAt(slen)
                } catch(e:Error){
                    code = 65536
                }
                if(code < 128){
                    result = result + 1
                } else if(code < 2048){
                    result = result + 2
                } else if(code < 65536){
                    result = result + 3
                } else {
                    result = result + 4
                }
            }
            return result
        }

        public static function charAt(bytes:ByteArray, index:int):String {
            if (bytes.length <= index) return null;
            return String.fromCharCode(bytes[index]);
        }

        public static function charCodeAt(bytes:ByteArray, index:int):int {
            if (bytes.length <= index) return -1;
            return bytes[index];
        }

        public static function substr(bytes:ByteArray, start:int, length:int=0):ByteArray {
            var res:ByteArray = new ByteArray();
            bytes.position = start;
            bytes.readBytes(res, 0, length);
            return res;
        }

        public static function substring(bytes:ByteArray, start:int, end:int=0):ByteArray {
            return substr(bytes, start, end-start);
        }

        public static function indexOf(bytes:ByteArray, str:String, startIndex:int):int {
            var num:int = 0;
            for (var i:int=0; i<bytes.length; i++) {
                var strPos:int = 0;
                while (bytes[i+strPos] == str.charCodeAt(strPos)) {
                    strPos++;
                    if (strPos == str.length) {
                        num++;
                        if(num == startIndex) {
                            return i;
                        }
                    }
                }
            }
            return -1;
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:48:25+00:00Added an answer on May 24, 2026 at 5:48 pm

    I resolved the issue, no need for answers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an ActionScript class: MyClass and that class has data in
I have an actionscript file that defines a class that I would like to
In the library, right-click on a movieclip that you have written an ActionScript class
I have an actionscript class in my flex app that looks like this: package
I have a dynamic ActionScript Class that is used to send parameters to a
In my main Actionscript file i have a instance of a body class that
I have a class member in an ActionScript 3 class that looks something like
I have an actionscript class MyClass that extens NavigatorContent. I instantiate the class as
I have a CircleButton class in Actionscript. I want to know when someone externally
I have an ActionScript 2.0 file that contains a small library. This library parses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.