コード例 #1
0
 public DateTime FromCBORObject(CBORObject obj)
 {
     if (obj.HasMostOuterTag(0))
     {
         try {
             return(StringToDateTime(obj.AsString()));
         } catch (OverflowException ex) {
             throw new CBORException(ex.Message, ex);
         } catch (ArgumentException ex) {
             throw new CBORException(ex.Message, ex);
         }
     }
     else if (obj.HasMostOuterTag(1))
     {
         if (!obj.IsNumber || !obj.AsNumber().IsFinite())
         {
             throw new CBORException("Not a finite number");
         }
         EDecimal dec;
         dec = (EDecimal)obj.ToObject(typeof(EDecimal));
         var lesserFields = new int[7];
         var year         = new EInteger[1];
         CBORUtilities.BreakDownSecondsSinceEpoch(
             dec,
             year,
             lesserFields);
         return(PropertyMap.BuildUpDateTime(year[0], lesserFields));
     }
     throw new CBORException("Not tag 0 or 1");
 }
コード例 #2
0
ファイル: PropertyMap.cs プロジェクト: serenabenny/adfsmfa
        public static object ObjectToEnum(CBORObject obj, Type enumType)
        {
            Type   utype = Enum.GetUnderlyingType(enumType);
            object ret   = null;

            if (obj.IsNumber && obj.AsNumber().IsInteger())
            {
                ret = Enum.ToObject(enumType, TypeToIntegerObject(obj, utype));
                if (!Enum.IsDefined(enumType, ret))
                {
                    throw new CBORException("Unrecognized enum value: " +
                                            obj.ToString());
                }
                return(ret);
            }
            else if (obj.Type == CBORType.TextString)
            {
                var nameString = obj.AsString();
                foreach (var name in Enum.GetNames(enumType))
                {
                    if (nameString.Equals(name, StringComparison.Ordinal))
                    {
                        return(Enum.Parse(enumType, name));
                    }
                }
                throw new CBORException("Not found: " + obj.ToString());
            }
            else
            {
                throw new CBORException("Unrecognized enum value: " +
                                        obj.ToString());
            }
        }
コード例 #3
0
ファイル: CBORReader.cs プロジェクト: serenabenny/adfsmfa
        private static CBORObject ResolveSharedRefs(
            CBORObject obj,
            SharedRefs sharedRefs)
        {
            if (obj == null)
            {
                return(null);
            }
            CBORType type   = obj.Type;
            bool     hasTag = obj.HasMostOuterTag(29);

            if (hasTag)
            {
                CBORObject untagged = obj.UntagOne();
                if (untagged.IsTagged ||
                    untagged.Type != CBORType.Integer ||
                    untagged.AsNumber().IsNegative())
                {
                    throw new CBORException(
                              "Shared ref index must be an untagged integer 0 or greater");
                }
                return(sharedRefs.GetObject(untagged.AsEIntegerValue()));
            }
            hasTag = obj.HasMostOuterTag(28);
            if (hasTag)
            {
                obj = obj.UntagOne();
                sharedRefs.AddObject(obj);
            }
            if (type == CBORType.Map)
            {
                foreach (CBORObject key in obj.Keys)
                {
                    CBORObject value    = obj[key];
                    CBORObject newvalue = ResolveSharedRefs(value, sharedRefs);
                    if (value != newvalue)
                    {
                        obj[key] = newvalue;
                    }
                }
            }
            else if (type == CBORType.Array)
            {
                for (var i = 0; i < obj.Count; ++i)
                {
                    obj[i] = ResolveSharedRefs(obj[i], sharedRefs);
                }
            }
            return(obj);
        }
コード例 #4
0
ファイル: PropertyMap.cs プロジェクト: serenabenny/adfsmfa
 // TODO: Replace* Legacy with AsNumber methods
 // in next major version
 private static object TypeToIntegerObject(CBORObject objThis, Type t)
 {
     if (t.Equals(typeof(int)))
     {
         return(objThis.AsInt32());
     }
     if (t.Equals(typeof(short)))
     {
         return(objThis.AsNumber().ToInt16Checked());
     }
     if (t.Equals(typeof(ushort)))
     {
         return(objThis.AsUInt16Legacy());
     }
     if (t.Equals(typeof(byte)))
     {
         return(objThis.AsByteLegacy());
     }
     if (t.Equals(typeof(sbyte)))
     {
         return(objThis.AsSByteLegacy());
     }
     if (t.Equals(typeof(long)))
     {
         return(objThis.AsNumber().ToInt64Checked());
     }
     if (t.Equals(typeof(uint)))
     {
         return(objThis.AsUInt32Legacy());
     }
     if (t.Equals(typeof(ulong)))
     {
         return(objThis.AsUInt64Legacy());
     }
     throw new CBORException("Type not supported");
 }
コード例 #5
0
ファイル: PropertyMap.cs プロジェクト: serenabenny/adfsmfa
        public static object TypeToObject(
            CBORObject objThis,
            Type t,
            CBORTypeMapper mapper,
            PODOptions options,
            int depth)
        {
            if (t.Equals(typeof(int)))
            {
                return(objThis.AsInt32());
            }
            if (t.Equals(typeof(short)))
            {
                return(objThis.AsNumber().ToInt16Checked());
            }
            if (t.Equals(typeof(ushort)))
            {
                return(objThis.AsUInt16Legacy());
            }
            if (t.Equals(typeof(byte)))
            {
                return(objThis.AsByteLegacy());
            }
            if (t.Equals(typeof(sbyte)))
            {
                return(objThis.AsSByteLegacy());
            }
            if (t.Equals(typeof(long)))
            {
                return(objThis.AsNumber().ToInt64Checked());
            }
            if (t.Equals(typeof(uint)))
            {
                return(objThis.AsUInt32Legacy());
            }
            if (t.Equals(typeof(ulong)))
            {
                return(objThis.AsUInt64Legacy());
            }
            if (t.Equals(typeof(double)))
            {
                return(objThis.AsDouble());
            }
            if (t.Equals(typeof(decimal)))
            {
                return(objThis.AsDecimal());
            }
            if (t.Equals(typeof(float)))
            {
                return(objThis.AsSingle());
            }
            if (t.Equals(typeof(bool)))
            {
                return(objThis.AsBoolean());
            }
            if (t.Equals(typeof(char)))
            {
                if (objThis.Type == CBORType.TextString)
                {
                    string s = objThis.AsString();
                    if (s.Length != 1)
                    {
                        throw new CBORException("Can't convert to char");
                    }
                    return(s[0]);
                }
                if (objThis.IsNumber && objThis.AsNumber().CanFitInInt32())
                {
                    int c = objThis.AsNumber().ToInt32IfExact();
                    if (c < 0 || c >= 0x10000)
                    {
                        throw new CBORException("Can't convert to char");
                    }
                    return((char)c);
                }
                throw new CBORException("Can't convert to char");
            }
            if (t.Equals(typeof(DateTime)))
            {
                return(new CBORDateConverter().FromCBORObject(objThis));
            }
            if (t.Equals(typeof(Guid)))
            {
                return(new CBORUuidConverter().FromCBORObject(objThis));
            }
            if (t.Equals(typeof(Uri)))
            {
                return(new CBORUriConverter().FromCBORObject(objThis));
            }
            if (IsAssignableFrom(typeof(Enum), t))
            {
                return(ObjectToEnum(objThis, t));
            }
            if (IsGenericType(t))
            {
                Type td = t.GetGenericTypeDefinition();
                // Nullable types
                if (td.Equals(typeof(Nullable <>)))
                {
                    Type nullableType = Nullable.GetUnderlyingType(t);
                    if (objThis.IsNull)
                    {
                        return(Activator.CreateInstance(t));
                    }
                    else
                    {
                        object wrappedObj = objThis.ToObject(
                            nullableType,
                            mapper,
                            options,
                            depth + 1);
                        return(Activator.CreateInstance(
                                   t,
                                   wrappedObj));
                    }
                }
            }
            if (objThis.Type == CBORType.ByteString)
            {
                if (t.Equals(typeof(byte[])))
                {
                    byte[] bytes   = objThis.GetByteString();
                    var    byteret = new byte[bytes.Length];
                    Array.Copy(bytes, 0, byteret, 0, byteret.Length);
                    return(byteret);
                }
            }
            if (objThis.Type == CBORType.Array)
            {
                Type   objectType = typeof(object);
                var    isList     = false;
                object listObject = null;
        #if NET40 || NET20
                if (IsAssignableFrom(typeof(Array), t))
                {
                    Type  elementType = t.GetElementType();
                    Array array       = Array.CreateInstance(
                        elementType,
                        GetDimensions(objThis));
                    return(FillArray(
                               array,
                               elementType,
                               objThis,
                               mapper,
                               options,
                               depth));
                }
                if (t.IsGenericType)
                {
                    Type td = t.GetGenericTypeDefinition();
                    isList = td.Equals(typeof(List <>)) || td.Equals(typeof(IList <>)) ||
                             td.Equals(typeof(ICollection <>)) ||
                             td.Equals(typeof(IEnumerable <>));
                }
                isList = isList && t.GetGenericArguments().Length == 1;
                if (isList)
                {
                    objectType = t.GetGenericArguments()[0];
                    Type listType = typeof(List <>).MakeGenericType(objectType);
                    listObject = Activator.CreateInstance(listType);
                }
        #else
                if (IsAssignableFrom(typeof(Array), t))
                {
                    Type  elementType = t.GetElementType();
                    Array array       = Array.CreateInstance(
                        elementType,
                        GetDimensions(objThis));
                    return(FillArray(
                               array,
                               elementType,
                               objThis,
                               mapper,
                               options,
                               depth));
                }
                if (t.GetTypeInfo().IsGenericType)
                {
                    Type td = t.GetGenericTypeDefinition();
                    isList = td.Equals(typeof(List <>)) || td.Equals(typeof(IList <>)) ||
                             td.Equals(typeof(ICollection <>)) ||

                             td.Equals(typeof(IEnumerable <>));
                }
                isList = isList && t.GenericTypeArguments.Length == 1;
                if (isList)
                {
                    objectType = t.GenericTypeArguments[0];
                    Type listType = typeof(List <>).MakeGenericType(objectType);
                    listObject = Activator.CreateInstance(listType);
                }
        #endif
                if (listObject == null)
                {
                    if (t.Equals(typeof(IList)) ||
                        t.Equals(typeof(ICollection)) || t.Equals(typeof(IEnumerable)))
                    {
                        listObject = new List <object>();
                        objectType = typeof(object);
                    }
                }
                if (listObject != null)
                {
                    System.Collections.IList ie = (System.Collections.IList)listObject;
                    foreach (CBORObject value in objThis.Values)
                    {
                        ie.Add(value.ToObject(objectType, mapper, options, depth + 1));
                    }
                    return(listObject);
                }
            }
            if (objThis.Type == CBORType.Map)
            {
                var    isDict     = false;
                Type   keyType    = null;
                Type   valueType  = null;
                object dictObject = null;
        #if NET40 || NET20
                isDict = t.IsGenericType;
                if (t.IsGenericType)
                {
                    Type td = t.GetGenericTypeDefinition();
                    isDict = td.Equals(typeof(Dictionary <,>)) ||
                             td.Equals(typeof(IDictionary <,>));
                }
                // DebugUtility.Log("list=" + isDict);
                isDict = isDict && t.GetGenericArguments().Length == 2;
                // DebugUtility.Log("list=" + isDict);
                if (isDict)
                {
                    keyType   = t.GetGenericArguments()[0];
                    valueType = t.GetGenericArguments()[1];
                    Type listType = typeof(Dictionary <,>).MakeGenericType(
                        keyType,
                        valueType);
                    dictObject = Activator.CreateInstance(listType);
                }
        #else
                isDict = t.GetTypeInfo().IsGenericType;
                if (t.GetTypeInfo().IsGenericType)
                {
                    Type td = t.GetGenericTypeDefinition();
                    isDict = td.Equals(typeof(Dictionary <,>)) ||
                             td.Equals(typeof(IDictionary <,>));
                }
                // DebugUtility.Log("list=" + isDict);
                isDict = isDict && t.GenericTypeArguments.Length == 2;
                // DebugUtility.Log("list=" + isDict);
                if (isDict)
                {
                    keyType   = t.GenericTypeArguments[0];
                    valueType = t.GenericTypeArguments[1];
                    Type listType = typeof(Dictionary <,>).MakeGenericType(
                        keyType,
                        valueType);
                    dictObject = Activator.CreateInstance(listType);
                }
        #endif
                if (dictObject == null)
                {
                    if (t.Equals(typeof(IDictionary)))
                    {
                        dictObject = new Dictionary <object, object>();
                        keyType    = typeof(object);
                        valueType  = typeof(object);
                    }
                }
                if (dictObject != null)
                {
                    System.Collections.IDictionary idic =
                        (System.Collections.IDictionary)dictObject;
                    foreach (CBORObject key in objThis.Keys)
                    {
                        CBORObject value = objThis[key];
                        idic.Add(
                            key.ToObject(keyType, mapper, options, depth + 1),
                            value.ToObject(valueType, mapper, options, depth + 1));
                    }
                    return(dictObject);
                }
                if (mapper != null)
                {
                    if (!mapper.FilterTypeName(t.FullName))
                    {
                        throw new CBORException("Type " + t.FullName +
                                                " not supported");
                    }
                }
                else
                {
                    if (t.FullName != null && (
                            StartsWith(t.FullName, "Microsoft.Win32.") ||
                            StartsWith(t.FullName, "System.IO.")))
                    {
                        throw new CBORException("Type " + t.FullName +
                                                " not supported");
                    }
                    if (StartsWith(t.FullName, "System.") &&
                        !HasCustomAttribute(t, "System.SerializableAttribute"))
                    {
                        throw new CBORException("Type " + t.FullName +
                                                " not supported");
                    }
                }
                var values    = new List <KeyValuePair <string, CBORObject> >();
                var propNames = PropertyMap.GetPropertyNames(
                    t,
                    options != null ? options.UseCamelCase : true);
                foreach (string key in propNames)
                {
                    if (objThis.ContainsKey(key))
                    {
                        CBORObject cborValue = objThis[key];
                        var        dict      = new KeyValuePair <string, CBORObject>(
                            key,
                            cborValue);
                        values.Add(dict);
                    }
                }
                return(PropertyMap.ObjectWithProperties(
                           t,
                           values,
                           mapper,
                           options,
                           depth));
            }
            else
            {
                throw new CBORException();
            }
        }