Ejemplo n.º 1
0
        /// <summary>
        /// Reads the value of a Property inside a object instance.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="binary"></param>
        /// <returns>A NdfValueWrapper Instance.</returns>
        protected NdfValueWrapper ReadValue(Stream ms, NdfBinary binary)
        {
            uint            contBufferlen;
            NdfValueWrapper value;
            var             buffer = new byte[4];

            ms.Read(buffer, 0, buffer.Length);
            NdfType type = NdfTypeManager.GetType(buffer);

            if (type == NdfType.Unknown)
            {
                throw new InvalidDataException("Unknown datatypes are not supported!");
            }



            if (type == NdfType.Reference)
            {
                ms.Read(buffer, 0, buffer.Length);
                type = NdfTypeManager.GetType(buffer);
            }

            switch (type)
            {
            case NdfType.Unknown:
            case NdfType.WideString:
            case NdfType.List:
            case NdfType.MapList:
            case NdfType.Blob:
            case NdfType.ZipBlob:
                ms.Read(buffer, 0, buffer.Length);
                contBufferlen = BitConverter.ToUInt32(buffer, 0);

                if (type == NdfType.ZipBlob)
                {
                    if (ms.ReadByte() != 1)
                    {
                        throw new InvalidDataException("has to be checked.");
                    }
                }
                break;

            default:
                contBufferlen = NdfTypeManager.SizeofType(type);
                break;
            }

            switch (type)
            {
            case NdfType.MapList:
            case NdfType.List:
                NdfCollection lstValue = type == NdfType.List ? new NdfCollection() : new NdfMapList();

                for (int i = 0; i < contBufferlen; i++)
                {
                    CollectionItemValueHolder res;
                    if (type == NdfType.List)
                    {
                        res = new CollectionItemValueHolder(ReadValue(ms, binary), binary);
                    }
                    else
                    {
                        res = new CollectionItemValueHolder(
                            new NdfMap(
                                new MapValueHolder(ReadValue(ms, binary), binary),
                                new MapValueHolder(ReadValue(ms, binary), binary),
                                binary), binary);
                    }

                    lstValue.Add(res);
                }

                value = lstValue;
                break;

            case NdfType.Map:
                value = new NdfMap(
                    new MapValueHolder(ReadValue(ms, binary), binary),
                    new MapValueHolder(ReadValue(ms, binary), binary),
                    binary);
                break;

            default:
                var contBuffer = new byte[contBufferlen];
                ms.Read(contBuffer, 0, contBuffer.Length);

                value = NdfTypeManager.GetValue(contBuffer, type, binary);
                break;
            }

            return(value);
        }
        /// <summary>
        /// Reads the value of a Property inside a object instance.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="triggerBreak"></param>
        /// <param name="prop"></param>
        /// <returns>A NdfValueWrapper Instance.</returns>
        protected NdfValueWrapper ReadValue(MemoryStream ms, out bool triggerBreak, NdfPropertyValue prop)
        {
            var             buffer = new byte[4];
            uint            contBufferlen;
            NdfValueWrapper value;

            triggerBreak = false;

            ms.Read(buffer, 0, buffer.Length);
            var type = NdfTypeManager.GetType(buffer);

            if (type == NdfType.Reference)
            {
                ms.Read(buffer, 0, buffer.Length);
                type = NdfTypeManager.GetType(buffer);
            }

            switch (type)
            {
            case NdfType.WideString:
            case NdfType.List:
            case NdfType.MapList:
                ms.Read(buffer, 0, buffer.Length);
                contBufferlen = BitConverter.ToUInt32(buffer, 0);
                break;

            default:
                contBufferlen = NdfTypeManager.SizeofType(type);
                break;
            }

            if (type == NdfType.Unknown)
            {
                //var t = _unknownTypes.SingleOrDefault(x => Utils.ByteArrayCompare(x, buffer));

                //if (t == default(byte[]))
                //{
                //    _unknownTypes.Add(buffer);
                //    _unknownTypesCount.Add(1);
                //}
                //else
                //    _unknownTypesCount[_unknownTypes.IndexOf(t)]++;

                triggerBreak = true;
                return(new NdfUnkown(buffer, ms.Position));
            }

            if (type == NdfType.List || type == NdfType.MapList)
            {
                NdfCollection lstValue;

                if (type == NdfType.List)
                {
                    lstValue = new NdfCollection(ms.Position);
                }
                else
                {
                    lstValue = new NdfMapList(ms.Position);
                }

                CollectionItemValueHolder res;

                for (int i = 0; i < contBufferlen; i++)
                {
                    if (type == NdfType.List)
                    {
                        res = new CollectionItemValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset);
                    }
                    else
                    {
                        res = new CollectionItemValueHolder(
                            new NdfMap(
                                new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset),
                                new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset),
                                ms.Position),
                            this, prop.InstanceOffset);
                    }

                    lstValue.Add(res);

                    if (triggerBreak)
                    {
                        break;
                    }
                }

                value = lstValue;
            }
            else if (type == NdfType.Map)
            {
                value = new NdfMap(
                    new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset),
                    new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset),
                    ms.Position);
            }
            else
            {
                var contBuffer = new byte[contBufferlen];
                ms.Read(contBuffer, 0, contBuffer.Length);

                if (prop != null)
                {
                    prop.ValueData = contBuffer;
                }

                value = NdfTypeManager.GetValue(contBuffer, type, this, ms.Position - contBuffer.Length);
            }

            return(value);
        }