Пример #1
0
 protected private override void convertSpanImpl(ReadOnlySpan <string?> src, Span <bool> dst)
 {
     for (int i = 0; i < src.Length; i++)
     {
         dst[i] = ASString.AS_toBoolean(src[i]);
     }
 }
Пример #2
0
        public void IndexedValueIndexer()
        {
            ASArray array = new ASArray(new IASValue[] { null, new ASString("abc"), null });

            Assert.AreEqual(new ASString("abc"), array[1]);
            array[1] = new ASString("def");
            Assert.AreEqual(new ASString("def"), array[1]);
        }
Пример #3
0
        /// <summary>
        /// Creates a new <see cref="ASQName"/> object from a namespace and a local name.
        /// </summary>
        /// <param name="ns">The namespace of the XML name. If this is null, the QName will match
        /// XML elements and attributes in any namespace.</param>
        /// <param name="localName">The local name of the XML name. If this is the string "*",
        /// the QName will match XML elements and attributes with any local name.</param>
        public ASQName(ASNamespace?ns, string localName)
        {
            if (ns != null)
            {
                (uri, prefix) = (ns.uri, ns.prefix);
            }

            this.localName = ASString.AS_convertString(localName);
        }
Пример #4
0
        public void SpecialConstructorInitializesProperties()
        {
            ASString  content = new ASString("abc");
            AMFHeader header  = new AMFHeader("header", true, content);

            Assert.AreEqual("header", header.Name);
            Assert.IsTrue(header.MustUnderstand);
            Assert.AreEqual(content, header.Content);
        }
Пример #5
0
        public void SpecialConstructorInitializesProperties()
        {
            ASString content = new ASString("abc");
            AMFBody  body    = new AMFBody("request", "response", content);

            Assert.AreEqual("request", body.RequestTarget);
            Assert.AreEqual("response", body.ResponseTarget);
            Assert.AreSame(content, body.Content);
        }
Пример #6
0
        public void WriteObject_Strings_LongFormat_AMF0()
        {
            ASString value = new ASString(new string('a', 100000));

            output.BeginObjectStream();
            output.WriteObject(value);
            output.EndObjectStream();

            Assert.AreEqual(1 /*code*/ + 4 /*length*/ + 100000 /*value*/, stream.Length);
        }
Пример #7
0
        public void ReadObject_Strings(AMFObjectEncoding objectEncoding, byte[] bytes, string expectedValue)
        {
            SetStreamContents(bytes);

            input.BeginObjectStream();
            ASString result = (ASString)input.ReadObject();

            Assert.AreEqual(objectEncoding, input.ObjectEncoding);
            input.EndObjectStream();

            Assert.AreEqual(expectedValue, result.Value);
        }
Пример #8
0
        /// <summary>
        /// Creates a new <see cref="ASQName"/> object from a local name. The default namespace will
        /// be used.
        /// </summary>
        /// <param name="localName">The local name of the XML name. This is the name of the XML
        /// element or attribute without the namespace. If this is the string "*", the QName will
        /// match XML elements and attributes with any name in any namespace. Otherwise, its namespace
        /// will be set to the default XML namespace.</param>
        public ASQName(string?localName)
        {
            if (localName != null && localName.Length == 1 && localName[0] == '*')
            {
                (uri, prefix) = (null, null);
            }
            else
            {
                ASNamespace defaultNS = ASNamespace.getDefault();
                (uri, prefix) = (defaultNS.uri, defaultNS.prefix);
            }

            this.localName = ASString.AS_convertString(localName);
        }
Пример #9
0
        /// <summary>
        /// Initializes the RegExp object using the given pattern and flags.
        /// </summary>
        /// <param name="pattern">The regular expression pattern string.</param>
        /// <param name="flags">The flags string of the regular expression.</param>
        private void _init(string?pattern, string?flags)
        {
            pattern = ASString.AS_convertString(pattern);
            flags   = ASString.AS_convertString(flags);

            m_source = pattern;
            RegexOptions regexOptions = RegexOptions.CultureInvariant | RegexOptions.ECMAScript;

            for (int i = 0; i < flags.Length; i++)
            {
                switch (flags[i])
                {
                case 'i':
                    regexOptions |= RegexOptions.IgnoreCase;
                    break;

                case 'x':
                    m_auxFlags |= AUX_FLAG_EXTENDED;
                    break;

                case 'm':
                    m_auxFlags |= AUX_FLAG_MULTILINE;
                    break;

                case 's':
                    m_auxFlags |= AUX_FLAG_DOTALL;
                    break;

                case 'g':
                    m_auxFlags |= AUX_FLAG_GLOBAL;
                    break;
                }
            }

            var transpiler = new RegexTranspiler();

            transpiler.transpile(
                pattern,
                (m_auxFlags & AUX_FLAG_MULTILINE) != 0,
                (m_auxFlags & AUX_FLAG_DOTALL) != 0,
                (m_auxFlags & AUX_FLAG_EXTENDED) != 0
                );

            m_internalRegex = new Regex(transpiler.transpiledPattern, regexOptions);

            m_groupNames = transpiler.getGroupNames();
            m_groupCount = transpiler.groupCount;
        }
Пример #10
0
        public void CanGetAndSetProperties()
        {
            ASString  content = new ASString("abc");
            AMFHeader header  = new AMFHeader();

            Assert.IsNull(header.Content);
            header.Content = content;
            Assert.AreSame(content, header.Content);

            Assert.IsFalse(header.MustUnderstand);
            header.MustUnderstand = true;
            Assert.IsTrue(header.MustUnderstand);

            Assert.IsNull(header.Name);
            header.Name = "abc";
            Assert.AreEqual("abc", header.Name);
        }
Пример #11
0
        public void CanGetAndSetProperties()
        {
            ASString content = new ASString("abc");

            AMFBody body = new AMFBody();

            Assert.IsNull(body.Content);
            body.Content = content;
            Assert.AreSame(content, body.Content);

            Assert.IsNull(body.RequestTarget);
            body.RequestTarget = "abc";
            Assert.AreEqual("abc", body.RequestTarget);

            Assert.IsNull(body.ResponseTarget);
            body.ResponseTarget = "def";
            Assert.AreEqual("def", body.ResponseTarget);
        }
Пример #12
0
        /// <summary>
        /// Creates an <see cref="XMLGenName"/> instance from a <see cref="QName"/>.
        /// </summary>
        /// <returns>The created <see cref="XMLGenName"/> instance.</returns>
        /// <param name="qname">A <see cref="QName"/> instance.</param>
        /// <param name="bindOptions">The binding options used in the lookup for which a generalized
        /// name must be created. The following flags are used here:
        /// <see cref="BindOptions.ATTRIBUTE"/> and <see cref="BindOptions.RUNTIME_NAME"/>.</param>
        public static XMLGenName fromQName(QName qname, BindOptions bindOptions)
        {
            bool   isAttr = (bindOptions & BindOptions.ATTRIBUTE) != 0;
            string?uri    = qname.ns.uri;
            string?localName;

            if ((bindOptions & BindOptions.RUNTIME_NAME) != 0)
            {
                localName = ASString.AS_convertString(qname.localName);
            }
            else
            {
                localName = qname.localName;
            }

            if (localName == null || localName.Length == 0)
            {
                return(new XMLGenName(uri: uri, localName: localName, isAttr: isAttr));
            }

            char firstChar = localName[0];

            if (qname.ns.isPublic && (uint)(firstChar - '0') <= 9)
            {
                NumberFormatHelper.parseArrayIndex(localName, allowLeadingZeroes: false, out uint arrindex);
                if ((int)arrindex >= 0)
                {
                    return(new XMLGenName(index: (int)arrindex, isIndex: true, isAttr: isAttr));
                }
            }

            if (firstChar == '@' && (bindOptions & BindOptions.ATTRIBUTE) == 0)
            {
                isAttr    = true;
                localName = localName.Substring(1);
            }

            if (localName.Length == 1 && localName[0] == '*')
            {
                localName = null;
            }

            return(new XMLGenName(uri: uri, localName: localName, isAttr: isAttr));
        }
Пример #13
0
        /// <summary>
        /// Creates a new <see cref="ASNamespace"/> with a URI and a prefix.
        /// </summary>
        ///
        /// <param name="prefix">The prefix of the namespace. If this is not a valid XML name, the
        /// namespace is considered to be prefixless and cannot be used with methods requiring prefixed
        /// namespaces.</param>
        /// <param name="uri">
        /// The URI of the namespace. If this is the empty string, the prefix must be
        /// the empty string, otherwise an error is thrown.
        /// </param>
        ///
        /// <exception cref="AVM2Exception">
        /// <list type="bullet">
        /// <item>
        /// <description>TypeError #1098: If <paramref name="uri"/> is the empty string, but
        /// <paramref name="prefix"/> is not the empty string.</description>
        /// </item>
        /// </list>
        /// </exception>
        public ASNamespace(string prefix, string uri)
        {
            prefix = ASString.AS_convertString(prefix);
            uri    = ASString.AS_convertString(uri);

            if (uri.Length == 0)
            {
                if (prefix.Length != 0)
                {
                    throw ErrorHelper.createError(ErrorCode.XML_ILLEGAL_PREFIX_PUBLIC_NAMESPACE, prefix);
                }
                (this.prefix, this.uri) = ("", "");
            }
            else
            {
                this.prefix = (prefix.Length == 0 || XMLHelper.isValidName(prefix)) ? prefix : null;
                this.uri    = uri;
            }
        }
Пример #14
0
        public void ReadObject_Strings_Caching_AMF3()
        {
            ASString empty  = new ASString("");
            ASString valueA = new ASString("a");
            ASString valueB = new ASString("b");
            ASString valueC = new ASString("c");

            byte[] bytes = new byte[] { (byte)AMF0ObjectTypeCode.AMF3Data,
                                        (byte)AMF3ObjectTypeCode.String, 0x01,                                                                                                                                 // ""
                                        (byte)AMF3ObjectTypeCode.String, 0x03, 0x61,                                                                                                                           // valueA
                                        (byte)AMF3ObjectTypeCode.String, 0x01,                                                                                                                                 // ""
                                        (byte)AMF3ObjectTypeCode.String, 0x03, 0x62,                                                                                                                           // valueB
                                        (byte)AMF3ObjectTypeCode.String, 0x00,                                                                                                                                 // valueA (by ref)
                                        (byte)AMF3ObjectTypeCode.Xml, 0x02,                                                                                                                                    // valueB (by ref)
                                        (byte)AMF3ObjectTypeCode.Array, 0x05, 0x02, (byte)AMF3ObjectTypeCode.String, 0x00, 0x01, (byte)AMF3ObjectTypeCode.String, 0x00, (byte)AMF3ObjectTypeCode.String, 0x02, // array
                                        (byte)AMF3ObjectTypeCode.Object, 0x1b, 0x02, 0x03, 0x63, (byte)AMF3ObjectTypeCode.String, 0x04, 0x00, (byte)AMF3ObjectTypeCode.String, 0x02, 0x01                      // object
            };
            SetStreamContents(bytes);

            input.BeginObjectStream();
            Assert.AreEqual(empty, input.ReadObject());  // empty strings are not cached
            Assert.AreEqual(valueA, input.ReadObject()); // will get string ref #0
            Assert.AreEqual(empty, input.ReadObject());  // empty strings are not cached
            Assert.AreEqual(valueB, input.ReadObject()); // will get string ref #1
            Assert.AreEqual(valueA, input.ReadObject()); // will use string ref #0
            ASXmlDocument xml = (ASXmlDocument)input.ReadObject();

            Assert.AreEqual(valueB.Value, xml.XmlString); // XML contents are same as valueB, will use ref #1
            ASArray array = (ASArray)input.ReadObject();  // Array contains valueA and valueB and mixed values with key valueB and value valueA

            CollectionAssert.AreElementsEqual(new object[] { valueA, valueB }, array.IndexedValues);
            Assert.AreEqual(valueA, array.DynamicProperties[valueB.Value]);
            ASObject obj = (ASObject)input.ReadObject();  // Object has class name valueB contains member with key valueC and value valueA dynamic property with key valueA and value valueB

            CollectionAssert.AreElementsEqual(new object[] { valueC }, obj.MemberValues);
            Assert.AreEqual(valueB, obj.DynamicProperties[valueA.Value]);
            Assert.AreEqual(valueB.Value, obj.Class.ClassAlias);
            Assert.AreEqual(ASClassLayout.Dynamic, obj.Class.Layout);
            CollectionAssert.AreElementsEqual(new string[] { valueC.Value }, obj.Class.MemberNames);

            Assert.AreEqual(AMFObjectEncoding.AMF3, input.ObjectEncoding);
            input.EndObjectStream();
        }
Пример #15
0
        public void WriteObject_Strings_Caching_AMF3()
        {
            ASString      empty  = new ASString("");
            ASString      valueA = new ASString("a");
            ASString      valueB = new ASString("b");
            ASString      valueC = new ASString("c");
            ASXmlDocument xml    = new ASXmlDocument(valueB.Value);
            ASArray       array  = new ASArray(new IASValue[] { valueA, valueB });

            array.DynamicProperties[valueB.Value] = valueA;
            ASClass  clazz = new ASClass(valueB.Value, ASClassLayout.Dynamic, new string[] { valueC.Value });
            ASObject obj   = new ASObject(clazz);

            obj.MemberValues[0] = valueC;
            obj.DynamicProperties[valueA.Value] = valueB;

            output.ObjectEncoding = AMFObjectEncoding.AMF3;
            output.BeginObjectStream();
            output.WriteObject(empty);  // empty strings are not cached
            output.WriteObject(valueA); // will get string ref #0
            output.WriteObject(empty);  // empty strings are not cached
            output.WriteObject(valueB); // will get string ref #1
            output.WriteObject(valueA); // will use string ref #0
            output.WriteObject(xml);    // XML contents are same as valueB, will use ref #1
            output.WriteObject(array);  // Array contains valueA and valueB and mixed values with key valueB and value valueA
            output.WriteObject(obj);    // Object has class name valueB contains member with key valueC and value valueA dynamic property with key valueA and value valueB
            output.EndObjectStream();

            CollectionAssert.AreElementsEqual(
                new byte[] { (byte)AMF0ObjectTypeCode.AMF3Data,
                             (byte)AMF3ObjectTypeCode.String, 0x01,                                                                                                                                 // ""
                             (byte)AMF3ObjectTypeCode.String, 0x03, 0x61,                                                                                                                           // valueA
                             (byte)AMF3ObjectTypeCode.String, 0x01,                                                                                                                                 // ""
                             (byte)AMF3ObjectTypeCode.String, 0x03, 0x62,                                                                                                                           // valueB
                             (byte)AMF3ObjectTypeCode.String, 0x00,                                                                                                                                 // valueA (by ref)
                             (byte)AMF3ObjectTypeCode.Xml, 0x02,                                                                                                                                    // valueB (by ref)
                             (byte)AMF3ObjectTypeCode.Array, 0x05, 0x02, (byte)AMF3ObjectTypeCode.String, 0x00, 0x01, (byte)AMF3ObjectTypeCode.String, 0x00, (byte)AMF3ObjectTypeCode.String, 0x02, // array
                             (byte)AMF3ObjectTypeCode.Object, 0x1b, 0x02, 0x03, 0x63, (byte)AMF3ObjectTypeCode.String, 0x04, 0x00, (byte)AMF3ObjectTypeCode.String, 0x02, 0x01                      // object
                }, stream.ToArray());
        }
Пример #16
0
        /// <summary>
        /// Creates a new <see cref="ASQName"/> object with a prefixed namespace specified by a URI
        /// and prefix, and a local name.
        /// </summary>
        ///
        /// <param name="prefix">The prefix of the namespace of the XML name.</param>
        /// <param name="uri">The URI of the namespace of the XML name. If this is null, the QName will
        /// match XML elements and attributes in any namespace and <paramref name="prefix"/> is ignored.</param>
        /// <param name="localName">The local name of the XML name. This is the name of the XML
        /// element or attribute without the namespace. If this is the string "*", the QName
        /// will match XML elements and attributes with any local name.</param>
        ///
        /// <exception cref="AVM2Exception">
        /// <list type="bullet">
        /// <item>
        /// <description>TypeError #1098: If <paramref name="uri"/> is the empty string, but
        /// <paramref name="prefix"/> is not the empty string.</description>
        /// </item>
        /// </list>
        /// </exception>
        ///
        /// <remarks>
        /// If <paramref name="prefix"/> is null, the QName will not have a prefix. This differs from
        /// the behaviour of the <see cref="ASNamespace(String,String)"/> constructor, which converts
        /// a null prefix to the string "null".
        /// </remarks>
        public ASQName(string?prefix, string?uri, string localName)
        {
            if (uri == null)
            {
                (this.uri, this.prefix) = (null, null);
            }
            else if (uri.Length == 0)
            {
                if (prefix != null && prefix.Length != 0)
                {
                    throw ErrorHelper.createError(ErrorCode.XML_ILLEGAL_PREFIX_PUBLIC_NAMESPACE, prefix);
                }
                (this.uri, this.prefix) = ("", "");
            }
            else
            {
                bool isValidPrefix = prefix == null || prefix.Length == 0 || XMLHelper.isValidName(prefix);
                (this.uri, this.prefix) = (uri, isValidPrefix ? prefix : null);
            }

            this.localName = ASString.AS_convertString(localName);
        }
Пример #17
0
        public void ReadObject_Strings_LongFormat_AMF0()
        {
            byte[] bytes = new byte[100005];
            bytes[0] = (byte)AMF0ObjectTypeCode.LongString;
            bytes[1] = 0x00;
            bytes[2] = 0x01;
            bytes[3] = 0x86;
            bytes[4] = 0xa0;
            for (int i = 0; i < 100000; i++)
            {
                bytes[i + 5] = 0x61;
            }

            SetStreamContents(bytes);

            input.BeginObjectStream();
            ASString result = (ASString)input.ReadObject();

            Assert.AreEqual(AMFObjectEncoding.AMF0, input.ObjectEncoding);
            input.EndObjectStream();

            Assert.AreEqual(new String('a', 100000), result.Value);
        }
Пример #18
0
        public void convertFromStringTest_array(string[] data)
        {
            runConvertArrayTest <string, int>(data, x => ASString.AS_toInt(x));
            runConvertArrayTest <string, uint>(data, x => ASString.AS_toUint(x));
            runConvertArrayTest <string, double>(data, x => ASString.AS_toNumber(x), AssertHelper.floatIdentical);
            runConvertArrayTest <string, bool>(data, x => ASString.AS_toBoolean(x));
            runConvertArrayTest <string, string>(data, x => x);

            runConvertArrayTest <string, ASObject>(data, x => x, (expected, actual) => {
                if (expected != null)
                {
                    Assert.IsType <ASString>(actual);
                    Assert.Equal((string)expected, (string)actual);
                }
                else
                {
                    Assert.Null(actual);
                }
            });

            runConvertArrayTest <string, ASAny>(data, x => x, (expected, actual) => {
                if (expected.value != null)
                {
                    Assert.IsType <ASString>(actual.value);
                    Assert.Equal((string)expected, (string)actual);
                }
                else
                {
                    AssertHelper.identical(ASAny.@null, actual);
                }
            });

            runConvertArrayTest(data, invalidConversion <string, GenericTypeConvertersTest_CA>);
            runConvertArrayTest(data, invalidConversion <string, GenericTypeConvertersTest_IA>);
            runConvertArrayTest(data, invalidConversion <string, GenericTypeConvertersTest_NonASType1>);
        }
Пример #19
0
 public override uint convert(string?value) => ASString.AS_toUint(value);
Пример #20
0
 public override bool convert(string?value) => ASString.AS_toBoolean(value);
Пример #21
0
 /// <summary>
 /// Creates a new <see cref="ASNamespace"/> with a URI and no prefix.
 /// </summary>
 /// <param name="uri">The URI of the namespace.</param>
 /// <remarks>
 /// The prefix of the namespace will not be set (i.e. is set to null), except when the URI is
 /// the empty string, in which case the prefix will be set to the empty string.
 /// </remarks>
 public ASNamespace(string uri)
 {
     this.uri    = ASString.AS_convertString(uri);
     this.prefix = (this.uri.Length == 0) ? "" : null;
 }
Пример #22
0
 /// <summary>
 /// Creates a new <see cref="ASQName"/> object with a non-prefixed namespace specified by a
 /// URI and a local name.
 /// </summary>
 ///
 /// <param name="uri">The URI of the namespace of the XML name. If this is null, the QName
 /// will match XML elements and attributes in any namespace.</param>
 /// <param name="localName">The local name of the XML name. If this is the string "*",
 /// the QName will match XML elements and attributes with any local name.</param>
 public ASQName(string?uri, string localName)
 {
     this.prefix    = (uri != null && uri.Length == 0) ? "" : null;
     this.uri       = uri;
     this.localName = ASString.AS_convertString(localName);
 }
Пример #23
0
        public void IndexedValueSetIndexerThrowsIfOutOfRange(int index)
        {
            ASArray array = new ASArray(new IASValue[] { null, null, null });

            array[index] = new ASString("abc");
        }
Пример #24
0
 public override double convert(string?value) => ASString.AS_toNumber(value);