Esempio n. 1
0
        /// <summary>
        /// Read a string reference.
        /// </summary>
        /// <param name="context">
        /// AMF decoding context.
        /// </param>
        /// <param name="reader">
        /// AMF reader.
        /// </param>
        /// <param name="index">
        /// Reference index.
        /// </param>
        /// <param name="reference">
        /// Reference value.
        /// </param>
        /// <returns>
        /// Referenced string or <c>null</c> if value does not contain a reference.
        /// </returns>
        /// <exception cref="SerializationException">
        /// Invalid reference.
        /// </exception>
        private static string ReadStringReference(
            AmfContext context,
            AmfStreamReader reader,
            out int index,
            out int reference)
        {
            reference = reader.ReadAMF3IntegerData(); //ReadUint29(reader);

            // The first bit is a flag with value 0
            if ((reference & 0x1) == 0)
            {
                // The remaining 1 to 28 significant bits are used to encode a string reference table index
                index = reference >> 1;

                if (context.StringReferences.Count <= index)
                {
                    throw new SerializationException("Invalid reference index: " + index);
                }

                return(context.StringReferences[index]);
            }

            index = -1;
            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Read a string.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <param name="reader">
        /// The reader.
        /// </param>
        /// <param name="output">
        /// The output.
        /// </param>
        /// <remarks>
        /// Type declaration:
        ///     <c>
        ///         U29S-ref = U29 (The first (low) bit is a flag with value 0. The remaining 1 to 28
        ///         significant bits are used to encode a string reference table index (an integer)).
        ///         U29S-value = U29 (The first (low) bit is a flag with value 1. The remaining 1 to 28 significant
        ///         bits are used to encode the byte-length of the UTF-8 encoded representation of the string).
        ///         UTF-8-empty = 0x01 (The UTF-8-vr empty string which is never sent by reference).
        ///         UTF-8-vr = U29S-ref | (U29S-value *(UTF8-char))
        ///         string-type = string-marker UTF-8-vr
        ///     </c>
        /// </remarks>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private static string ReadString(AmfContext context, AmfStreamReader reader, XmlWriter output = null)
        {
            int    index, reference;
            string cache;

            if ((cache = ReadStringReference(context, reader, out index, out reference)) != null)
            {
                if (output != null)
                {
                    output.WriteStartElement(AmfxContent.String);
                    output.WriteValue(cache);
//                    output.WriteStartElement(AmfxContent.String);
//                    output.WriteAttributeString(AmfxContent.StringId, index.ToString());
                    output.WriteEndElement();
                }

                return(cache);
            }

            // Get string length
            int    length2 = reference >> 1;
            string value   = ReadUtf8(reader, length2);

            if (value != string.Empty)
            {
                context.StringReferences.Add(value);
            }

            if (output != null)
            {
                output.WriteStartElement(AmfxContent.String);
                output.WriteValue(value);
                output.WriteEndElement();
            }

            return(value);

            int  handle = reader.ReadAMF3IntegerData();
            bool inline = (handle & 1) != 0;

            handle = handle >> 1;
            if (inline)
            {
                int length = handle;
                if (length == 0)
                {
                    return(string.Empty);
                }

                string str = reader.ReadUTF(length);
                context.StringReferences.Add(str);
                if (output != null)
                {
                    output.WriteStartElement(AmfxContent.String);
                    output.WriteValue(str);
                    output.WriteEndElement();
                }

                return(str);
            }

            if (output != null)
            {
                output.WriteStartElement(AmfxContent.String);
                output.WriteAttributeString(AmfxContent.StringId, handle.ToString());
                output.WriteEndElement();
            }

            return(context.StringReferences[handle]);
        }