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); }
internal static string ToStringHelper(CBORObject obj, int depth) { StringBuilder sb = null; string simvalue = null; CBORType type = obj.Type; CBORObject curobject; if (obj.IsTagged) { if (sb == null) { if (type == CBORType.TextString) { // The default capacity of StringBuilder may be too small // for many strings, so set a suggested capacity // explicitly string str = obj.AsString(); sb = new StringBuilder(Math.Min(str.Length, 4096) + 16); } else { sb = new StringBuilder(); } } // Append opening tags if needed curobject = obj; while (curobject.IsTagged) { EInteger ei = curobject.MostOuterTag; sb.Append(ei.ToString()); sb.Append('('); curobject = curobject.UntagOne(); } } switch (type) { case CBORType.SimpleValue: sb = sb ?? new StringBuilder(); if (obj.IsUndefined) { sb.Append("undefined"); } else if (obj.IsNull) { sb.Append("null"); } else { sb.Append("simple("); int thisItemInt = obj.SimpleValue; char c; if (thisItemInt >= 100) { // NOTE: '0'-'9' have ASCII code 0x30-0x39 c = (char)(0x30 + ((thisItemInt / 100) % 10)); sb.Append(c); } if (thisItemInt >= 10) { c = (char)(0x30 + ((thisItemInt / 10) % 10)); sb.Append(c); c = (char)(0x30 + (thisItemInt % 10)); } else { c = (char)(0x30 + thisItemInt); } sb.Append(c); sb.Append(')'); } break; case CBORType.Boolean: case CBORType.Integer: simvalue = obj.Untag().ToJSONString(); if (sb == null) { return(simvalue); } sb.Append(simvalue); break; case CBORType.FloatingPoint: { long bits = obj.AsDoubleBits(); simvalue = bits == DoubleNegInfinity ? "-Infinity" : ( bits == DoublePosInfinity ? "Infinity" : ( CBORUtilities.DoubleBitsNaN(bits) ? "NaN" : obj.Untag().ToJSONString())); if (sb == null) { return(simvalue); } sb.Append(simvalue); break; } case CBORType.ByteString: { sb = sb ?? new StringBuilder(); sb.Append("h'"); byte[] data = obj.GetByteString(); int length = data.Length; for (var i = 0; i < length; ++i) { sb.Append(HexAlphabet[(data[i] >> 4) & 15]); sb.Append(HexAlphabet[data[i] & 15]); } sb.Append((char)0x27); break; } case CBORType.TextString: { sb = sb == null ? new StringBuilder() : sb; sb.Append('\"'); string ostring = obj.AsString(); sb.Append(ostring); sb.Append('\"'); break; } case CBORType.Array: { sb = sb ?? new StringBuilder(); var first = true; sb.Append('['); if (depth >= 50) { sb.Append("..."); } else { for (var i = 0; i < obj.Count; ++i) { if (!first) { sb.Append(", "); } sb.Append(ToStringHelper(obj[i], depth + 1)); first = false; } } sb.Append(']'); break; } case CBORType.Map: { sb = sb ?? new StringBuilder(); var first = true; sb.Append('{'); if (depth >= 50) { sb.Append("..."); } else { ICollection <KeyValuePair <CBORObject, CBORObject> > entries = obj.Entries; foreach (KeyValuePair <CBORObject, CBORObject> entry in entries) { CBORObject key = entry.Key; CBORObject value = entry.Value; if (!first) { sb.Append(", "); } sb.Append(ToStringHelper(key, depth + 1)); sb.Append(": "); sb.Append(ToStringHelper(value, depth + 1)); first = false; } } sb.Append('}'); break; } default: { sb = sb ?? new StringBuilder(); sb.Append("???"); break; } } // Append closing tags if needed curobject = obj; while (curobject.IsTagged) { sb.Append(')'); curobject = curobject.UntagOne(); } return(sb.ToString()); }
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"); }