Example #1
0
        internal static BarfObjectHeader ReadFrom(IPrimitiveReader reader)
        {
            var value = reader.ReadVarInt32();

            if (value == -1)
            {
                return(NullBarfObjectHeader.Instance);
            }

            if (value < 0)
            {
                reader.RaiseInvalidDataException();
            }
            var result = new BarfObjectHeader {
                Version = value
            };

            value = reader.ReadVarInt32();
            if (value < 0)
            {
                reader.RaiseInvalidDataException();
            }
            result.MinVersion = value;

            value = reader.ReadInt32();
            if (value < 0)
            {
                reader.RaiseInvalidDataException();
            }
            result.Length = value;

            result.StartPosition = reader.BaseStream.Position;

            return(result);
        }
Example #2
0
 public static BarfSerializationTypeInfo Create(BarfObjectHeader originalHeader, byte[] futureData)
 {
     return(new BarfSerializationTypeInfo
     {
         OriginalHeader = originalHeader,
         FutureData = futureData
     });
 }
        public void EndObject(long headerPosition)
        {
            --_recursionCount;

            BarfObjectHeader.EndWrite(Writer, headerPosition);

            EnsureCloseStreamHeader();
        }
        private long BeginObject(int version, int minVersion)
        {
            EnsureOpenStreamHeader();

            var headerPosition = BarfObjectHeader.BeginWrite(Writer, version, minVersion);

            ++_recursionCount;

            return(headerPosition);
        }
        /// <summary>
        /// Ends a region of code that deserializes a barf type.
        /// </summary>
        public void EndObject <T>(BarfObjectHeader header)
        {
            --_objectDepth;

            if (!header.IsNull && Reader.BaseStream.Position < header.EndPosition)
            {
                RaiseInvalidData <T>(header, "There is data remaining but there shouldn't be.");
            }

            if (_objectDepth == 0)
            {
                StreamHeader = null;
                TypeTable    = null;
                if (_streamEnd > 0L)
                {
                    Reader.BaseStream.Seek(_streamEnd, SeekOrigin.Begin);
                    _streamEnd = 0L;
                }
            }
        }
        /// <summary>
        /// Captures the future data.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="header">The header.</param>
        /// <param name="instance">The instance.</param>
        public void CaptureFutureData <T>(BarfObjectHeader header, ref T instance)
            where T : ISerializationInfo
        {
            if (header.EndPosition < Reader.BaseStream.Position)
            {
                RaiseInvalidData <T>(header, "Can't capture future data when the current position is past the current position.");
            }

            var remainingLength = header.EndPosition - Reader.BaseStream.Position;
            var futureData      = new byte[remainingLength];

            Reader.BaseStream.Read(futureData, 0, futureData.Length);

            var typeInfo = BarfSerializationTypeInfo.Create(header, futureData);

            var info = (BarfSerializationInfo)instance.SerializationInfo ?? new BarfSerializationInfo();

            info.Add(typeof(T), typeInfo);

            instance.SerializationInfo = info;
        }
 public void WriteNullObject()
 {
     EnsureOpenStreamHeader();
     BarfObjectHeader.WriteNull(Writer);
     EnsureCloseStreamHeader();
 }
        /// <summary>
        /// Begins a region of code that deserializes a barf type.
        /// </summary>
        public BarfObjectHeader BeginObject <T>()
        {
            var def = BarfTypeDefinition.Get <T>(true);

            bool deserializeTypeTable = false;

            if (StreamHeader == null)
            {
                StreamHeader = BarfStreamHeader.ReadFrom(Reader);
                if (StreamHeader.FrameworkVersion > BarfFormatter.MaxFrameworkVersion)
                {
                    string message = string.Format(
                        "Encountered a BARF formatted stream with FrameworkVersion=\"{0}\" but MaxFrameworkVersion=\"{1}\".",
                        StreamHeader.FrameworkVersion,
                        BarfFormatter.MaxFrameworkVersion);
                    throw new UnhandledVersionException(message);
                }

                deserializeTypeTable = StreamHeader.Flags.IsSet(HeaderFlags.HasNameTable);
            }

            var objectHeader = BarfObjectHeader.ReadFrom(Reader);

            if (!objectHeader.IsNull)
            {
                if (objectHeader.Version < def.MinVersion)
                {
                    var message = string.Format(
                        "Binary data was encoded with Version=\"{0}\" but the current MinVersion=\"{1}\".",
                        objectHeader.Version,
                        def.MinVersion);
                    throw new UnhandledVersionException(message);
                }

                if (def.CurrentVersion < objectHeader.MinVersion)
                {
                    var message = string.Format(
                        "Binary data was encoded with a MinVersion=\"{0}\" but CurrentVersion=\"{1}\" is less than that.",
                        objectHeader.MinVersion,
                        def.CurrentVersion);
                    throw new UnhandledVersionException(message);
                }

                if (objectHeader.Version < def.MinDeserializeVersion)
                {
                    var message = string.Format(
                        "Binary data is Version=\"{0}\" but MinDeserializeVersion=\"{1}\".",
                        objectHeader.Version,
                        def.MinDeserializeVersion);
                    throw new UnhandledVersionException(message);
                }
            }

            if (deserializeTypeTable)
            {
                var currentPosition = Reader.BaseStream.Position;

                Reader.BaseStream.Seek(objectHeader.Length, SeekOrigin.Current);

                TypeTable = BarfTypeTable.ReadFrom(Reader);

                _streamEnd = Reader.BaseStream.Position;

                Reader.BaseStream.Seek(currentPosition, SeekOrigin.Begin);
            }

            ++_objectDepth;
            return(objectHeader);
        }
 public void RaiseInvalidData <T>(BarfObjectHeader header)
 {
     RaiseInvalidData <T>(header, null);
 }
        public void RaiseInvalidData <T>(BarfObjectHeader header, string message)
        {
            var builder = new StringBuilder();
            var w       = XmlWriter.Create(builder, new XmlWriterSettings
            {
                Indent              = true,
                IndentChars         = "\t",
                OmitXmlDeclaration  = true,
                ConformanceLevel    = ConformanceLevel.Fragment,
                NewLineChars        = Environment.NewLine,
                NewLineHandling     = NewLineHandling.Entitize,
                NewLineOnAttributes = false
            });

            w.WriteStartElement("Details");
            {
                if (!string.IsNullOrEmpty(message))
                {
                    w.WriteElementString("Message", message);
                }
                w.WriteElementString("Type", typeof(T).ToString());
                w.WriteElementString("Position", Reader.BaseStream.Position.ToString());
                w.WriteElementString("ObjectDepth", _objectDepth.ToString());
                if (header != null)
                {
                    w.WriteStartElement("ObjectHeader");
                    {
                        w.WriteElementString("IsNull", header.IsNull.ToString());
                        if (!header.IsNull)
                        {
                            w.WriteElementString("Version", header.Version.ToString());
                            w.WriteElementString("MinVersion", header.MinVersion.ToString());
                            w.WriteElementString("Length", header.Length.ToString());
                            w.WriteElementString("StartPosition", header.StartPosition.ToString());
                            w.WriteElementString("EndPosition", header.EndPosition.ToString());
                        }
                    }
                    w.WriteEndElement();
                }
                w.WriteStartElement("CurrentDefinition");
                {
                    var def = BarfTypeDefinition.Get <T>(false);
                    if (def == null || !def.IsValid)
                    {
                        w.WriteElementString("Error", "Couldn't load definition for type - " + typeof(T));
                    }
                    else
                    {
                        w.WriteElementString("CurrentVersion", def.CurrentVersion.ToString());
                        w.WriteElementString("MinVersion", def.MinVersion.ToString());
                        w.WriteElementString("MinDeserializeVersion", def.MinDeserializeVersion.ToString());
                    }
                }
                w.WriteEndElement();
                w.WriteStartElement("StreamHeader");
                {
                    w.WriteElementString("FrameworkVersion", StreamHeader.FrameworkVersion.ToString());
                    w.WriteElementString("Flags", StreamHeader.Flags.ToString());
                    w.WriteElementString("Length", StreamHeader.Length.ToString());
                }
                w.WriteEndElement();
            }
            w.WriteEndElement();
            w.Flush();

            throw new InvalidDataException(builder.ToString());
        }