Example #1
0
 private object MapDouble(string value, Type valType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
 {
     CheckExpectedType(valType, typeof(double), mappingStack);
     mappedType = typeof(double);
     return(OnStack("double", mappingStack, delegate()
     {
         try
         {
             return double.Parse(value, CultureInfo.InvariantCulture.NumberFormat);
         }
         catch (Exception)
         {
             throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType
                                                    + " contains invalid double value " + StackDump(mappingStack));
         }
     }));
 }
Example #2
0
        private object MapNilValue(string p, Type type, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
        {
            if (type == null ||
                (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) ||
                (!type.IsPrimitive || !type.IsValueType) ||
                type == typeof(object))
            {
                mappedType = type;
                return(null);
            }

            throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType
                                                   + " contains <nil> value which cannot be mapped to type "
                                                   + (type != null && type != typeof(object) ? type.Name : "object")
                                                   + " "
                                                   + StackDump(mappingStack));
        }
        XmlRpcFaultException ParseFault(IEnumerator <Node> iter, MappingStack parseStack, MappingAction mappingAction)
        {
            iter.MoveNext();  // move to StructValue
            var    faultStruct = MapHashtable(iter, null, parseStack, mappingAction, out Type parsedType) as XmlRpcStruct;
            object faultCode   = faultStruct["faultCode"];
            object faultString = faultStruct["faultString"];

            if (faultCode is string)
            {
                if (!int.TryParse(faultCode as string, out int value))
                {
                    throw new XmlRpcInvalidXmlRpcException("faultCode not int or string");
                }

                faultCode = value;
            }
            return(new XmlRpcFaultException((int)faultCode, (string)faultString));
        }
Example #4
0
        private object MapInt(string value, Type valType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
        {
            CheckExpectedType(valType, typeof(int), mappingStack);

            if (valType != null && valType.IsEnum)
            {
                return(MapNumberToEnum(value, valType, "int", mappingStack, mappingAction, out mappedType));
            }

            mappedType = typeof(int);

            return(OnStack("integer", mappingStack, () =>
            {
                if (!int.TryParse(value, out int ret))
                {
                    throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType
                                                           + " contains invalid int value " + StackDump(mappingStack));
                }
                return ret;
            }));
        }
Example #5
0
        private object MapLong(string value, Type valType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
        {
            CheckExpectedType(valType, typeof(long), mappingStack);

            if (valType != null && valType.IsEnum)
            {
                return(MapNumberToEnum(value, valType, "i8", mappingStack, mappingAction, out mappedType));
            }

            mappedType = typeof(long);

            return(OnStack("i8", mappingStack, () =>
            {
                if (!long.TryParse(value, out long ret))
                {
                    throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType
                                                           + $" contains invalid i8 value {StackDump(mappingStack)}");
                }
                return ret;
            }));
        }
Example #6
0
        private object MapBoolean(string value, Type valType, MappingStack mappingStack,
                                  MappingAction mappingAction, out Type mappedType)
        {
            CheckExpectedType(valType, typeof(bool), mappingStack);
            mappedType = typeof(bool);
            return(OnStack("boolean", mappingStack, () =>
            {
                if (value == "1")
                {
                    return true;
                }

                if (value == "0")
                {
                    return false;
                }

                throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType
                                                       + $" contains invalid boolean value {StackDump(mappingStack)}");
            }));
        }
Example #7
0
        private void CheckExpectedType(Type expectedType, Type actualType, MappingStack mappingStack)
        {
            if (expectedType != null && expectedType.IsEnum)
            {
                Type[] i4Types = { typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int) };
                Type[] i8Types = { typeof(uint), typeof(long) };

                var underlyingType = Enum.GetUnderlyingType(expectedType);

                if (Array.IndexOf(i4Types, underlyingType) >= 0)
                {
                    expectedType = typeof(int);
                }
                else if (Array.IndexOf(i8Types, underlyingType) >= 0)
                {
                    expectedType = typeof(long);
                }
                else
                {
                    throw new XmlRpcInvalidEnumValue(mappingStack.MappingType +
                                                     " contains "
                                                     + XmlRpcTypeInfo.GetXmlRpcTypeString(actualType)
                                                     + " which cannot be mapped to  "
                                                     + XmlRpcTypeInfo.GetXmlRpcTypeString(expectedType)
                                                     + " " + StackDump(mappingStack));
                }
            }
            // TODO: throw exception for invalid enum type
            if (expectedType != null && expectedType != typeof(object) && expectedType != actualType &&
                (actualType.IsValueType && expectedType != typeof(Nullable <>).MakeGenericType(actualType)))
            {
                throw new XmlRpcTypeMismatchException(mappingStack.MappingType +
                                                      " contains "
                                                      + XmlRpcTypeInfo.GetXmlRpcTypeString(actualType)
                                                      + " value where "
                                                      + XmlRpcTypeInfo.GetXmlRpcTypeString(expectedType)
                                                      + " expected " + StackDump(mappingStack));
            }
        }
Example #8
0
        protected object MapHashtable(IEnumerator <Node> iter, Type valType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
        {
            mappedType = null;
            var retObj = new XmlRpcStruct();

            mappingStack.Push("struct mapped to XmlRpcStruct");
            try
            {
                while (iter.MoveNext() && iter.Current is StructMember)
                {
                    string rpcName = (iter.Current as StructMember).Value;
                    if (retObj.ContainsKey(rpcName) &&
                        !IgnoreDuplicateMembers)
                    {
                        throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType
                                                               + " contains struct value with duplicate member "
                                                               + rpcName
                                                               + " " + StackDump(mappingStack));
                    }
                    iter.MoveNext();

                    var value = OnStack($"member {rpcName}",
                                        mappingStack,
                                        () => MapValueNode(iter, null, mappingStack, mappingAction));

                    if (!retObj.ContainsKey(rpcName))
                    {
                        retObj[rpcName] = value;
                    }
                }
            }
            finally
            {
                mappingStack.Pop();
            }
            return(retObj);
        }
Example #9
0
        void MapMultiDimElements(IEnumerator <Node> iter, int Rank, int CurRank,
                                 Type elemType, List <object> elements, int[] dimLengths,
                                 MappingStack mappingStack, MappingAction mappingAction)
        {
            //XmlNode dataNode = SelectSingleNode(node, "data");
            //XmlNode[] childNodes = SelectNodes(dataNode, "value");
            //int nodeCount = childNodes.Length;
            ////!! check that multi dim array is not jagged
            //if (dimLengths[CurRank] != 0 && nodeCount != dimLengths[CurRank])
            //{
            //  throw new XmlRpcNonRegularArrayException(
            //    "Multi-dimensional array must not be jagged.");
            //}
            //dimLengths[CurRank] = nodeCount;  // in case first array at this rank
            var nodeCount = 0;

            if (CurRank < (Rank - 1))
            {
                while (iter.MoveNext() && iter.Current is ArrayValue)
                {
                    nodeCount++;
                    MapMultiDimElements(iter, Rank, CurRank + 1, elemType,
                                        elements, dimLengths, mappingStack, mappingAction);
                }
            }
            else
            {
                while (iter.MoveNext() && iter.Current is ValueNode)
                {
                    nodeCount++;
                    object value = MapValueNode(iter, elemType, mappingStack, mappingAction);
                    elements.Add(value);
                }
            }
            dimLengths[CurRank] = nodeCount;
        }
Example #10
0
        private object MapBase64(string value, Type valType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
        {
            CheckExpectedType(valType, typeof(byte[]), mappingStack);
            mappedType = typeof(int);

            return(OnStack("base64", mappingStack, () =>
            {
                if (value == "")
                {
                    return new byte[0];
                }

                try
                {
                    return Convert.FromBase64String(value);
                }
                catch (Exception)
                {
                    throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType
                                                           + " contains invalid base64 value "
                                                           + StackDump(mappingStack));
                }
            }));
        }
Example #11
0
        public object MapValueNode(IEnumerator <Node> iter, Type valType, MappingStack mappingStack, MappingAction mappingAction)
        {
            var valueNode = iter.Current as ValueNode;

            // if suppplied type is System.Object then ignore it because
            // if doesn't provide any useful information (parsing methods
            // expect null in this case)
            if (valType != null && valType.BaseType == null)
            {
                valType = null;
            }

            if (valueNode is StringValue && valueNode.ImplicitValue)
            {
                CheckImplictString(valType, mappingStack);
            }

            Type mappedType;

            if (iter.Current is ArrayValue)
            {
                return(MapArray(iter, valType, mappingStack, mappingAction, out mappedType));
            }

            if (iter.Current is StructValue)
            {
                // if we don't know the expected struct type then we must
                // map the XML-RPC struct as an instance of XmlRpcStruct
                if (valType != null && valType != typeof(XmlRpcStruct) && !valType.IsSubclassOf(typeof(XmlRpcStruct)))
                {
                    return(MapStruct(iter, valType, mappingStack, mappingAction, out mappedType));
                }

                if (valType == null || valType == typeof(object))
                {
                    valType = typeof(XmlRpcStruct);
                }

                // TODO: do we need to validate type here?
                return(MapHashtable(iter, valType, mappingStack, mappingAction, out mappedType));
            }

            if (iter.Current is Base64Value)
            {
                return(MapBase64(valueNode.Value, valType, mappingStack, mappingAction, out mappedType));
            }

            if (iter.Current is IntValue)
            {
                return(MapInt(valueNode.Value, valType, mappingStack, mappingAction, out mappedType));
            }

            if (iter.Current is LongValue)
            {
                return(MapLong(valueNode.Value, valType, mappingStack, mappingAction, out mappedType));
            }

            if (iter.Current is StringValue)
            {
                return(MapString(valueNode.Value, valType, mappingStack, mappingAction, out mappedType));
            }

            if (iter.Current is BooleanValue)
            {
                return(MapBoolean(valueNode.Value, valType, mappingStack, mappingAction, out mappedType));
            }

            if (iter.Current is DoubleValue)
            {
                return(MapDouble(valueNode.Value, valType, mappingStack, mappingAction, out mappedType));
            }

            if (iter.Current is DateTimeValue)
            {
                return(MapDateTime(valueNode.Value, valType, mappingStack, mappingAction, out mappedType));
            }

            if (iter.Current is NilValue)
            {
                return(MapNilValue(valueNode.Value, valType, mappingStack, mappingAction, out mappedType));
            }


            return(null);
        }
Example #12
0
        private object MapArray(IEnumerator <Node> iter, Type valType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
        {
            mappedType = null;
            // required type must be an array
            if (valType != null && !(valType.IsArray == true || valType == typeof(Array) || valType == typeof(object)))
            {
                throw new XmlRpcTypeMismatchException(mappingStack.MappingType
                                                      + $" contains array value where{XmlRpcTypeInfo.GetXmlRpcTypeString(valType)}"
                                                      + $" expected {StackDump(mappingStack)}");
            }

            if (valType != null)
            {
                XmlRpcType xmlRpcType = XmlRpcTypeInfo.GetXmlRpcType(valType);
                if (xmlRpcType == XmlRpcType.tMultiDimArray)
                {
                    mappingStack.Push("array mapped to type " + valType.Name);
                    return(MapMultiDimArray(iter, valType, mappingStack, mappingAction));
                }
                mappingStack.Push("array mapped to type " + valType.Name);
            }
            else
            {
                mappingStack.Push("array");
            }

            var values   = new List <object>();
            var elemType = DetermineArrayItemType(valType);

            while (iter.MoveNext() && iter.Current is ValueNode)
            {
                mappingStack.Push(string.Format("element {0}", values.Count));
                var value = MapValueNode(iter, elemType, mappingStack, mappingAction);
                values.Add(value);
                mappingStack.Pop();
            }

            var  bGotType = false;
            Type useType  = null;

            foreach (object value in values)
            {
                if (value == null)
                {
                    continue;
                }

                if (bGotType)
                {
                    if (useType != value.GetType())
                    {
                        useType = null;
                    }

                    continue;
                }

                useType  = value.GetType();
                bGotType = true;
            }

            var args = new object[1];

            args[0] = values.Count;
            object retObj = null;

            if (valType != null && valType != typeof(Array) && valType != typeof(object))
            {
                retObj = CreateArrayInstance(valType, args);
            }
            else
            {
                if (useType == null)
                {
                    retObj = CreateArrayInstance(typeof(object[]), args);
                }
                else
                {
                    retObj = Array.CreateInstance(useType, (int)args[0]);
                }
            }

            for (int j = 0; j < values.Count; j++)
            {
                ((Array)retObj).SetValue(values[j], j);
            }

            mappingStack.Pop();

            return(retObj);
        }
Example #13
0
        private object MapStruct(IEnumerator <Node> iter, Type valueType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
        {
            mappedType = null;

            if (valueType.IsPrimitive)
            {
                throw new XmlRpcTypeMismatchException(mappingStack.MappingType
                                                      + " contains struct value where "
                                                      + XmlRpcTypeInfo.GetXmlRpcTypeString(valueType)
                                                      + " expected " + StackDump(mappingStack));
            }
            if (valueType.IsGenericType &&
                valueType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                valueType = valueType.GetGenericArguments()[0];
            }
            object retObj;

            try
            {
                retObj = Activator.CreateInstance(valueType);
            }
            catch (Exception)
            {
                throw new XmlRpcTypeMismatchException(mappingStack.MappingType
                                                      + " contains struct value where "
                                                      + XmlRpcTypeInfo.GetXmlRpcTypeString(valueType)
                                                      + $" expected (as type {valueType.Name}) "
                                                      + StackDump(mappingStack));
            }
            // Note: mapping action on a struct is only applied locally - it
            // does not override the global mapping action when members of the
            // struct are mapped
            var localAction = mappingAction;

            if (valueType != null)
            {
                mappingStack.Push("struct mapped to type " + valueType.Name);
                localAction = StructMappingAction(valueType, mappingAction);
            }
            else
            {
                mappingStack.Push("struct");
            }
            // create map of field names and remove each name from it as
            // processed so we can determine which fields are missing
            var names = new List <string>();

            CreateFieldNamesMap(valueType, names);

            var fieldCount = 0;
            var rpcNames   = new List <string>();

            try
            {
                while (iter.MoveNext())
                {
                    if (!(iter.Current is StructMember))
                    {
                        break;
                    }

                    var rpcName = (iter.Current as StructMember).Value;
                    if (rpcNames.Contains(rpcName))
                    {
                        if (!IgnoreDuplicateMembers)
                        {
                            throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType
                                                                   + $" contains struct value with duplicate member {rpcName} {StackDump(mappingStack)}");
                        }

                        continue;
                    }
                    rpcNames.Add(rpcName);

                    var        name = GetStructName(valueType, rpcName) ?? rpcName;
                    MemberInfo mi   = valueType.GetField(name);
                    if (mi == null)
                    {
                        mi = valueType.GetProperty(name);
                    }

                    if (mi == null)
                    {
                        iter.MoveNext();  // move to value
                        if (iter.Current is ComplexValueNode)
                        {
                            int depth = iter.Current.Depth;
                            while (!(iter.Current is EndComplexValueNode && iter.Current.Depth == depth))
                            {
                                iter.MoveNext();
                            }
                        }
                        continue;
                    }
                    if (names.Contains(name))
                    {
                        names.Remove(name);
                    }
                    else
                    {
                        if (Attribute.IsDefined(mi, typeof(NonSerializedAttribute)))
                        {
                            mappingStack.Push($"member {name}");
                            throw new XmlRpcNonSerializedMember("Cannot map XML-RPC struct member onto member marked as [NonSerialized]: " + StackDump(mappingStack));
                        }
                    }

                    var memberType = mi.MemberType == MemberTypes.Field ? (mi as FieldInfo).FieldType : (mi as PropertyInfo).PropertyType;

                    var mappingMsg = valueType == null
                        ? $"member {name}"
                        : $"member {name} mapped to type {memberType.Name}";

                    iter.MoveNext();
                    var valObj = OnStack(mappingMsg, mappingStack, () => MapValueNode(iter, memberType, mappingStack, mappingAction));

                    if (mi.MemberType == MemberTypes.Field)
                    {
                        (mi as FieldInfo).SetValue(retObj, valObj);
                    }
                    else
                    {
                        (mi as PropertyInfo).SetValue(retObj, valObj, null);
                    }

                    fieldCount++;
                }

                if (localAction == MappingAction.Error && names.Count > 0)
                {
                    ReportMissingMembers(valueType, names, mappingStack);
                }

                return(retObj);
            }
            finally
            {
                mappingStack.Pop();
            }
        }
Example #14
0
        private object MapNumberToEnum(string value, Type enumType, string xmlRpcType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
        {
            mappedType = enumType;
            return(OnStack(xmlRpcType, mappingStack, () =>
            {
                try
                {
                    var lnum = long.Parse(value);
                    var underlyingType = Enum.GetUnderlyingType(enumType);
                    var enumNumberValue = Convert.ChangeType(lnum, underlyingType, null);
                    if (!Enum.IsDefined(enumType, enumNumberValue))
                    {
                        throw new XmlRpcInvalidEnumValue(mappingStack.MappingType
                                                         + $" contains {xmlRpcType} mapped to undefined enum value "
                                                         + StackDump(mappingStack));
                    }

                    return Enum.ToObject(enumType, enumNumberValue);
                }
                catch (XmlRpcInvalidEnumValue)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new XmlRpcInvalidEnumValue(mappingStack.MappingType
                                                     + $" contains invalid or out of range {xmlRpcType} value mapped to enum "
                                                     + StackDump(mappingStack));
                }
            }));
        }
Example #15
0
 private object MapStringToEnum(string value, Type enumType, string xmlRpcType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType)
 {
     mappedType = enumType;
     return(OnStack(xmlRpcType, mappingStack, () =>
     {
         try
         {
             return Enum.Parse(enumType, value, true);
         }
         catch (XmlRpcInvalidEnumValue)
         {
             throw;
         }
         catch (Exception ex)
         {
             throw new XmlRpcInvalidEnumValue(mappingStack.MappingType
                                              + $" contains invalid or out of range {xmlRpcType} value mapped to enum {StackDump(mappingStack)}");
         }
     }));
 }