Пример #1
0
        public void WriteObject(AmfObject amfObject, bool isArray = false)
        {
            if(!isArray)
            writer.Write((byte) Globals.Amf0Types.Object);
            else
            {
                writer.Write((byte) Globals.Amf0Types.Array);
                writer.Write(
                    (int)
                    (amfObject.Booleans.Count + amfObject.Numbers.Count + amfObject.Strings.Count + amfObject.Nulls));
            }
            foreach (var s in amfObject.Strings)
            {
                WriteString(s.Key, true);
                WriteString(s.Value);
            }

            foreach (var s in amfObject.Numbers)
            {
                WriteString(s.Key, true);
                WriteNumber(s.Value);
            }

            foreach (var s in amfObject.Booleans)
            {
                WriteString(s.Key, true);
                WriteBoolean(s.Value);
            }
            //objects end with 0x00,0x00, (oject end identifier [0x09 in this case])
            writer.Write((byte)0x00);
            writer.Write((byte)0x00);
            writer.Write((byte) Globals.Amf0Types.ObjectEnd);
        }
Пример #2
0
        public void Parse(EndianBinaryReader reader, uint size)
        {
            var maxReadPos = reader.BaseStream.Position + size;
            while(reader.BaseStream.Position < maxReadPos)
            {
                if (objectStack.Count != 0)
                {
                    var count = reader.ReadUInt16();
                    var propString = "";
                    for (var i = 0; i < count; i++)
                    {
                        propString += (char) reader.ReadByte();
                    }
                    objectStack.Peek().CurrentProperty = propString;
                }

                var type = (Globals.Amf0Types)reader.ReadByte();

                switch (type)
                {
                    case Globals.Amf0Types.Number:
                        {
                            var value = reader.ReadDouble();
                            if(objectStack.Count != 0)
                            {
                                objectStack.Peek().Numbers.Add(objectStack.Peek().CurrentProperty, value);
                            }
                            else
                            {
                                amfData.Numbers.Add(value);
                            }
                        }
                        break;
                    case Globals.Amf0Types.Boolean:
                        {
                            var value = reader.ReadBoolean();
                            if (objectStack.Count != 0)
                            {
                                objectStack.Peek().Booleans.Add(objectStack.Peek().CurrentProperty, value);
                            }
                            else
                            {
                                amfData.Booleans.Add(value);
                            }
                        }
                        break;
                    case Globals.Amf0Types.String:
                        {
                            var count = reader.ReadUInt16();
                            var pushString = "";
                            for (var i = 0; i < count; i++)
                            {
                                pushString += (char) reader.ReadByte();
                            }

                            if (objectStack.Count != 0)
                            {
                                objectStack.Peek().Strings.Add(objectStack.Peek().CurrentProperty, pushString);
                            }
                            else
                            {
                                amfData.Strings.Add(pushString);
                            }
                        }
                        break;
                    case Globals.Amf0Types.Null:

                        if (objectStack.Count != 0)
                        {
                            objectStack.Peek().Nulls++;
                        }
                        else
                        {
                            amfData.Nulls++;
                        }
                        break;
                    case Globals.Amf0Types.Object:
                    case Globals.Amf0Types.Array:
                        {
                            if(type == Globals.Amf0Types.Array)
                            {
                                var arrayLength = reader.ReadInt32();
                            }
                            var objectAdd = new AmfObject();
                            objectStack.Push(objectAdd);
                        }
                        break;
                    case Globals.Amf0Types.ObjectEnd:
                        {
                            if(objectStack.Count == 1)
                            {
                                amfData.Objects.Add(objectStack.Pop());
                            }
                            else if(objectStack.Count > 1)
                            {
                                var mostRecentObject = objectStack.Pop();
                                objectStack.Peek().Objects.Add(objectStack.Peek().CurrentProperty, mostRecentObject);
                            }
                        }
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
                /*
                switch (type)
                {
                    case Amf0Types.Array:
                    case Amf0Types.Object:
                        {
                            if(type == Amf0Types.Array)
                            {
                                var arrayLength = reader.ReadInt32();
                                Console.WriteLine("Array:" + arrayLength);
                            }
                            bool hasProperty = false;
                            string property = "";
                            var objectAdd = new AmfObject();
                            while (reader.BaseStream.Position < maxReadPos)
                            {
                                if(!hasProperty)
                                {
                                    property = "";
                                    var propertyStringLength = reader.ReadUInt16();
                                    for (var i = 0; i < propertyStringLength; i++)
                                    {
                                        property += (char)reader.ReadByte();
                                    }
                                    hasProperty = true;
                                }

                                if (hasProperty == true)
                                {
                                    if(property.Length == 0)
                                    {
                                        amfData.Objects.Add(objectAdd);

                                        break;
                                    }
                                    var objtype = (Amf0Types)reader.ReadByte();
                                    parseType(objtype, reader, ref objectAdd.Nulls, objectNumbers: objectAdd.Numbers,
                                              objectBooleans: objectAdd.Booleans, objectStrings: objectAdd.Strings,
                                              property: property);
                                    hasProperty = false;
                                }
                            }
                        }
                        break;
                    default:
                        parseType(type, reader, ref amfData.Nulls, amfData.Numbers, amfData.Booleans, amfData.Strings);
                        break;
                }*/
            }
        }