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); }
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); }
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(); }
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)); }
private string TryGetDateTimeFieldsInternal( CBORObject obj, EInteger[] year, int[] lesserFields) { if (obj == null) { return("Object is null"); } if (year == null) { throw new ArgumentNullException(nameof(year)); } EInteger[] outYear = year; if (outYear.Length < 1) { throw new ArgumentException("\"year\" + \"'s length\" (" + outYear.Length + ") is not greater or equal to 1"); } if (lesserFields == null) { throw new ArgumentNullException(nameof(lesserFields)); } if (lesserFields.Length < 7) { throw new ArgumentException("\"lesserFields\" + \"'s length\" (" + lesserFields.Length + ") is not greater or equal to 7"); } if (this.convType == ConversionType.UntaggedNumber) { if (obj.IsTagged) { return("May not be tagged"); } CBORObject untagobj = obj; if (!untagobj.IsNumber) { return("Not a finite number"); } CBORNumber num = untagobj.AsNumber(); if (!num.IsFinite()) { return("Not a finite number"); } if (num.CompareTo(Int64.MinValue) < 0 || num.CompareTo(Int64.MaxValue) > 0) { return("Too big or small to fit a DateTime"); } if (num.CanFitInInt64()) { CBORUtilities.BreakDownSecondsSinceEpoch( num.ToInt64Checked(), outYear, lesserFields); } else { EDecimal dec; dec = (EDecimal)untagobj.ToObject(typeof(EDecimal)); CBORUtilities.BreakDownSecondsSinceEpoch( dec, outYear, lesserFields); } return(null); // no error } if (obj.HasMostOuterTag(0)) { string str = obj.AsString(); try { CBORUtilities.ParseAtomDateTimeString(str, outYear, lesserFields); return(null); // no error } catch (OverflowException ex) { return(ex.Message); } catch (InvalidOperationException ex) { return(ex.Message); } catch (ArgumentException ex) { return(ex.Message); } } else if (obj.HasMostOuterTag(1)) { CBORObject untagobj = obj.UntagOne(); if (!untagobj.IsNumber) { return("Not a finite number"); } CBORNumber num = untagobj.AsNumber(); if (!num.IsFinite()) { return("Not a finite number"); } if (num.CanFitInInt64()) { CBORUtilities.BreakDownSecondsSinceEpoch( num.ToInt64Checked(), outYear, lesserFields); } else { EDecimal dec; dec = (EDecimal)untagobj.ToObject(typeof(EDecimal)); CBORUtilities.BreakDownSecondsSinceEpoch( dec, outYear, lesserFields); } return(null); // No error } return("Not tag 0 or 1"); }