Example #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");
 }
Example #2
0
        private static CBORObject ValidateObject(CBORObject obj)
        {
            if (obj.Type != CBORType.TextString)
            {
                throw new CBORException("URI/IRI must be a text string");
            }
            bool isiri    = obj.HasMostOuterTag(266);
            bool isiriref = obj.HasMostOuterTag(267);

            if (
                isiriref && !URIUtility.IsValidIRI(
                    obj.AsString(),
                    URIUtility.ParseMode.IRIStrict))
            {
                throw new CBORException("String is not a valid IRI Reference");
            }
            if (
                isiri && (!URIUtility.IsValidIRI(
                              obj.AsString(),
                              URIUtility.ParseMode.IRIStrict) ||
                          !URIUtility.HasScheme(obj.AsString())))
            {
                throw new CBORException("String is not a valid IRI");
            }
            if (!URIUtility.IsValidIRI(
                    obj.AsString(),
                    URIUtility.ParseMode.URIStrict) ||
                !URIUtility.HasScheme(obj.AsString()))
            {
                throw new CBORException("String is not a valid URI");
            }
            return(obj);
        }
Example #3
0
        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);
        }
Example #4
0
 public Uri FromCBORObject(CBORObject obj)
 {
     if (obj.HasMostOuterTag(32) ||
         obj.HasMostOuterTag(266) ||
         obj.HasMostOuterTag(267))
     {
         ValidateObject(obj);
         try {
             return(new Uri(obj.AsString()));
         } catch (Exception ex) {
             throw new CBORException(ex.Message, ex);
         }
     }
     throw new CBORException();
 }
Example #5
0
        public Guid FromCBORObject(CBORObject obj)
        {
            if (!obj.HasMostOuterTag(37))
            {
                throw new CBORException("Must have outermost tag 37");
            }
            ValidateObject(obj);
            byte[] bytes     = obj.GetByteString();
            var    guidChars = new char[36];
            string hex       = "0123456789abcdef";
            var    index     = 0;

            for (var i = 0; i < 16; ++i)
            {
                if (i == 4 || i == 6 || i == 8 || i == 10)
                {
                    guidChars[index++] = '-';
                }
                guidChars[index++] = hex[(int)(bytes[i] >> 4) & 15];
                guidChars[index++] = hex[(int)bytes[i] & 15];
            }
            string guidString = new String(guidChars);

            return(new Guid(guidString));
        }