/// <summary>
 /// Encode the changes in the IPofValue hierarchy recursively.
 /// </summary>
 /// <param name="writer">
 /// DataWriter to write changes into.
 /// </param>
 /// <param name="value">
 /// POF value to encode.
 /// </param>
 /// <param name="pos">
 /// Current position in the original POF stream.
 /// </param>
 /// <returns>
 /// Current position in the original POF stream.
 /// </returns>
 protected virtual int EncodeValue(DataWriter writer,
                                   AbstractPofValue value, int pos)
 {
     if (value.IsDirty)
     {
         int of = value.Offset;
         if (pos < of)
         {
             WriteExtract(writer, 0, pos, of - pos);
         }
         byte[] ab = value.GetSerializedValue().ToByteArray();
         WriteAppend(writer, 0, ab, 0, ab.Length);
         pos = of + value.Size;
     }
     else if (value is ComplexPofValue)
     {
         IEnumerator en = ((ComplexPofValue)value).GetChildrenEnumerator();
         while (en.MoveNext())
         {
             var entry = (DictionaryEntry)en.Current;
             pos = EncodeValue(writer, (AbstractPofValue)entry.Value, pos);
         }
     }
     // else if SimplePofValue: handled by isDirty block
     return(pos);
 }
 /// <summary>
 /// Encode the changes in the IPofValue hierarchy recursively.
 /// </summary>
 /// <param name="writer">
 /// DataWriter to write changes into.
 /// </param>
 /// <param name="value">
 /// POF value to encode.
 /// </param>
 /// <param name="pos">
 /// Current position in the original POF stream.
 /// </param>
 /// <returns>
 /// Current position in the original POF stream.
 /// </returns>
 protected virtual int EncodeValue(DataWriter writer,
                                   AbstractPofValue value, int pos)
 {
     if (value.IsDirty)
     {
         int of = value.Offset;
         if (pos < of)
         {
             CopyFromOriginal(writer, pos, of - pos);
         }
         value.GetSerializedValue().WriteTo(writer);
         pos = of + value.Size;
     }
     else if (value is ComplexPofValue)
     {
         IEnumerator en = ((ComplexPofValue)value).GetChildrenEnumerator();
         while (en.MoveNext())
         {
             var entry = (DictionaryEntry)en.Current;
             pos = EncodeValue(writer, (AbstractPofValue)entry.Value, pos);
         }
     }
     // else if SimplePofValue: handled by IsDirty block
     return(pos);
 }
        /// <summary>
        /// Parses POF-encoded binary and returns an instance of a
        /// <see cref="IPofValue"/> wrapper for it.
        /// </summary>
        /// <param name="valueParent">
        /// Parent POF value.
        /// </param>
        /// <param name="nType">
        /// Type identifier of this POF value.
        /// </param>
        /// <param name="binValue">
        /// POF-encoded binary value.
        /// </param>
        /// <param name="ctx">
        /// POF context to use.
        /// </param>
        /// <param name="of">
        /// Offset of the parsed value from the beginning of the POF stream.
        /// </param>
        /// <returns>
        /// An IPofValue instance.
        /// </returns>
        internal static IPofValue ParseUniformValue(IPofValue valueParent,
                                                    int nType, Binary binValue, IPofContext ctx, int of)
        {
            AbstractPofValue val = InstantiatePofValue(valueParent, nType,
                                                       binValue, ctx, of, binValue.GetReader());

            val.SetUniformEncoded();
            return(val);
        }
            // ----- methods ------------------------------------------------

            /// <summary>
            /// Encode changes made to this POF value in FMT_BINDIFF delta format,
            /// as defined by the <see cref="BinaryDeltaCompressor"/> class.
            /// </summary>
            /// <returns>
            /// A binary delta containing the changes that can be applied to
            /// the original buffer to reflect the current state of this
            /// POF value.
            /// </returns>
            public virtual Binary Encode()
            {
                AbstractPofValue value  = PofValue;
                DataWriter       writer = EnsureWriter(null,
                                                       value.DirtyBytesCount * 2);
                int pos   = EncodeValue(writer, value, 0);
                int cbOld = value.BinaryValue.Length;

                if (pos < cbOld)
                {
                    WriteExtract(writer, 0, pos, cbOld - pos);
                }
                FinalizeDelta(writer);

                return(((BinaryMemoryStream)writer.BaseStream).ToBinary());
            }
            // ----- methods ------------------------------------------------

            /// <summary>
            /// Encode changes made to this POF value in FMT_REPLACE delta
            /// format, as defined by the <see cref="BinaryDeltaCompressor"/>
            /// class.
            /// </summary>
            /// <returns>
            /// A binary delta containing the changes that can be applied to
            /// the original buffer to reflect the current state of this
            /// POF value.
            /// </returns>
            public virtual Binary Encode()
            {
                AbstractPofValue value = PofValue;

                var buf    = new BinaryMemoryStream(value.DirtyBytesCount * 2);
                var writer = new DataWriter(buf);

                writer.Write(FMT_REPLACE);
                int pos   = EncodeValue(writer, value, 0);
                int cbOld = value.BinaryValue.Length;

                if (pos < cbOld)
                {
                    CopyFromOriginal(writer, pos, cbOld - pos);
                }

                return(buf.ToBinary());
            }
Пример #6
0
        /// <summary>
        /// Find the child value with the specified index.
        /// </summary>
        /// <param name="nIndex">
        /// Index of the child value to find.
        /// </param>
        /// <returns>
        /// The child value.
        /// </returns>
        protected IPofValue FindChild(int nIndex)
        {
            int ofStart = m_ofChildren;
            int iStart  = GetLastChildIndex(nIndex);

            if (iStart >= 0)
            {
                AbstractPofValue lastChild = (AbstractPofValue)m_aChildren[iStart];
                ofStart = lastChild.Offset - Offset + lastChild.Size;
                iStart  = iStart + 1;
            }
            else
            {
                iStart = 0;
            }

            return(FindChildInternal(nIndex, ofStart, iStart));
        }
            // ----- constructors -------------------------------------------

            /// <summary>
            /// Construct a PofValueReader instance.
            /// </summary>
            public PofValueReader(AbstractPofValue value)
                : base(new DataReader(value.BinaryValue.GetStream()), value.PofContext)
            {
                m_value = value;
            }
            // ----- constructors -------------------------------------------

            /// <summary>
            /// Construct a ReplacementEncoder instance.
            /// </summary>
            public ReplacementEncoder(AbstractPofValue value)
            {
                m_value = value;
            }
            // ----- constructors -------------------------------------------

            /// <summary>
            /// Construct a BinaryDiffEncoder instance.
            /// </summary>
            public BinaryDiffEncoder(AbstractPofValue value)
            {
                m_value = value;
            }