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.IsFinite) { throw new CBORException("Not a finite number"); } EDecimal dec = obj.AsEDecimal(); 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"); }
internal static CBORObject ParseSmallNumber(int digit, JSONOptions options) { #if DEBUG if (digit < 0) { throw new ArgumentException("digit (" + digit + ") is not greater" + "\u0020or equal to 0"); } #endif if (options != null && options.NumberConversion == JSONOptions.ConversionMode.Double) { return(CBORObject.FromFloatingPointBits( CBORUtilities.IntegerToDoubleBits(digit), 8)); } else if (options != null && options.NumberConversion == JSONOptions.ConversionMode.Decimal128) { return(CBORObject.FromObject(EDecimal.FromInt32(digit))); } else { // NOTE: Assumes digit is nonnegative, so PreserveNegativeZeros is irrelevant return(CBORObject.FromObject(digit)); } }
private static string DateTimeToString(DateTime bi) { var lesserFields = new int[7]; var year = new EInteger[1]; PropertyMap.BreakDownDateTime(bi, year, lesserFields); return(CBORUtilities.ToAtomDateTimeString(year[0], lesserFields)); }
public static DateTime StringToDateTime(string str) { var lesserFields = new int[7]; var year = new EInteger[1]; CBORUtilities.ParseAtomDateTimeString(str, year, lesserFields); return(PropertyMap.BuildUpDateTime(year[0], lesserFields)); }
private static string DateTimeToString(DateTime bi) { var lesserFields = new int[7]; var year = new EInteger[1]; PropertyMap.BreakDownDateTime(bi, year, lesserFields); // TODO: Change to true in next major version return(CBORUtilities.ToAtomDateTimeString(year[0], lesserFields, false)); }
private static string DateTimeToString(DateTime bi) { try { var lesserFields = new int[7]; var outYear = new EInteger[1]; PropertyMap.BreakDownDateTime(bi, outYear, lesserFields); return(CBORUtilities.ToAtomDateTimeString(outYear[0], lesserFields)); } catch (ArgumentException ex) { throw new CBORException(ex.Message, ex); } }
/// <summary>Returns the value of this object in text form.</summary> /// <returns>A text string representing the value of this /// object.</returns> public override string ToString() { switch (this.kind) { case Kind.Integer: { var longItem = (long)this.value; return(CBORUtilities.LongToString(longItem)); } default: return((this.value == null) ? String.Empty : this.value.ToString()); } }
public string GetAdjustedName(bool useCamelCase) { string thisName = this.Name; if (useCamelCase) { if (CBORUtilities.NameStartsWithWord(thisName, "Is")) { thisName = thisName.Substring(2); } thisName = CBORUtilities.FirstCharLower(thisName); } else { thisName = CBORUtilities.FirstCharUpper(thisName); } return(thisName); }
internal string ToJSONString() { switch (this.kind) { case Kind.Double: { var f = (double)this.value; if (Double.IsNegativeInfinity(f) || Double.IsPositiveInfinity(f) || Double.IsNaN(f)) { return("null"); } string dblString = CBORUtilities.DoubleToString(f); return(CBORUtilities.TrimDotZero(dblString)); } case Kind.Integer: { var longItem = (long)this.value; return(CBORUtilities.LongToString(longItem)); } case Kind.EInteger: { return(((EInteger)this.value).ToString()); } case Kind.EDecimal: { var dec = (EDecimal)this.value; if (dec.IsInfinity() || dec.IsNaN()) { return("null"); } else { return(dec.ToString()); } } case Kind.EFloat: { var flo = (EFloat)this.value; if (flo.IsInfinity() || flo.IsNaN()) { return("null"); } if (flo.IsFinite && flo.Exponent.Abs().CompareTo((EInteger)2500) > 0) { // Too inefficient to convert to a decimal number // from a bigfloat with a very high exponent, // so convert to double instead double f = flo.ToDouble(); if (Double.IsNegativeInfinity(f) || Double.IsPositiveInfinity(f) || Double.IsNaN(f)) { return("null"); } string dblString = CBORUtilities.DoubleToString(f); return(CBORUtilities.TrimDotZero(dblString)); } return(flo.ToString()); } case Kind.ERational: { var dec = (ERational)this.value; EDecimal f = dec.ToEDecimalExactIfPossible( EContext.Decimal128.WithUnlimitedExponents()); if (!f.IsFinite) { return("null"); } else { return(f.ToString()); } } default: throw new InvalidOperationException(); } }
public EInteger AsEInteger(object obj) { return(CBORUtilities.EIntegerFromDoubleBits((long)obj)); }
public EInteger AsEInteger(object obj) { return(CBORUtilities.BigIntegerFromSingle((float)obj)); }
public EInteger AsEInteger(object obj) { return(CBORUtilities.BigIntegerFromDouble((double)obj)); }
internal static CBORObject ParseJSONNumber( string chars, int offset, int count, JSONOptions options, int[] endOfNumber) { if (chars == null || chars.Length == 0 || count <= 0) { return(null); } if (offset < 0 || offset > chars.Length) { return(null); } if (count > chars.Length || chars.Length - offset < count) { return(null); } JSONOptions opt = options ?? CBORDataUtilities.DefaultOptions; bool preserveNegativeZero = options.PreserveNegativeZero; JSONOptions.ConversionMode kind = options.NumberConversion; int endPos = offset + count; int initialOffset = offset; var negative = false; if (chars[initialOffset] == '-') { ++offset; negative = true; } int numOffset = offset; var haveDecimalPoint = false; var haveDigits = false; var haveDigitsAfterDecimal = false; var haveExponent = false; int i = offset; var decimalPointPos = -1; // Check syntax int k = i; if (endPos - 1 > k && chars[k] == '0' && chars[k + 1] >= '0' && chars[k + 1] <= '9') { if (endOfNumber != null) { endOfNumber[0] = k + 2; } return(null); } for (; k < endPos; ++k) { char c = chars[k]; if (c >= '0' && c <= '9') { haveDigits = true; haveDigitsAfterDecimal |= haveDecimalPoint; } else if (c == '.') { if (!haveDigits || haveDecimalPoint) { // no digits before the decimal point, // or decimal point already seen if (endOfNumber != null) { endOfNumber[0] = k; } return(null); } haveDecimalPoint = true; decimalPointPos = k; } else if (c == 'E' || c == 'e') { ++k; haveExponent = true; break; } else { if (endOfNumber != null) { endOfNumber[0] = k; // Check if character can validly appear after a JSON number if (c != ',' && c != ']' && c != '}' && c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09) { return(null); } else { endPos = k; break; } } return(null); } } if (!haveDigits || (haveDecimalPoint && !haveDigitsAfterDecimal)) { if (endOfNumber != null) { endOfNumber[0] = k; } return(null); } var exponentPos = -1; var negativeExp = false; if (haveExponent) { haveDigits = false; if (k == endPos) { if (endOfNumber != null) { endOfNumber[0] = k; } return(null); } char c = chars[k]; if (c == '+') { ++k; } else if (c == '-') { negativeExp = true; ++k; } for (; k < endPos; ++k) { c = chars[k]; if (c >= '0' && c <= '9') { if (exponentPos < 0 && c != '0') { exponentPos = k; } haveDigits = true; } else if (endOfNumber != null) { endOfNumber[0] = k; // Check if character can validly appear after a JSON number if (c != ',' && c != ']' && c != '}' && c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09) { return(null); } else { endPos = k; break; } } else { return(null); } } if (!haveDigits) { if (endOfNumber != null) { endOfNumber[0] = k; } return(null); } } if (endOfNumber != null) { endOfNumber[0] = endPos; } if (exponentPos >= 0 && endPos - exponentPos > 20) { // Exponent too high for precision to overcome (which // has a length no bigger than Int32.MaxValue, which is 10 digits // long) if (negativeExp) { // underflow if (kind == JSONOptions.ConversionMode.Double || kind == JSONOptions.ConversionMode.IntOrFloat) { if (!negative) { return(CBORObject.FromFloatingPointBits(0, 2)); } else { return(CBORObject.FromFloatingPointBits(0x8000, 2)); } } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble) { return(CBORObject.FromObject(0)); } } else { // overflow if (kind == JSONOptions.ConversionMode.Double || kind == JSONOptions.ConversionMode.IntOrFloatFromDouble || kind == JSONOptions.ConversionMode.IntOrFloat) { return(CBORObject.FromFloatingPointBits( negative ? DoubleNegInfinity : DoublePosInfinity, 8)); } else if (kind == JSONOptions.ConversionMode.Decimal128) { return(CBORObject.FromObject(negative ? EDecimal.NegativeInfinity : EDecimal.PositiveInfinity)); } } } if (!haveExponent && !haveDecimalPoint && (endPos - numOffset) <= 16) { // Very common case of all-digit JSON number strings // less than 2^53 (with or without number sign) long v = 0L; int vi = numOffset; for (; vi < endPos; ++vi) { v = (v * 10) + (int)(chars[vi] - '0'); } if ((v != 0 || !negative) && v < (1L << 53) - 1) { if (negative) { v = -v; } if (kind == JSONOptions.ConversionMode.Double) { return(CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8)); } else if (kind == JSONOptions.ConversionMode.Decimal128) { return(CBORObject.FromObject(EDecimal.FromInt64(v))); } else { return(CBORObject.FromObject(v)); } } } if (kind == JSONOptions.ConversionMode.Full) { if (!haveDecimalPoint && !haveExponent) { EInteger ei = EInteger.FromSubstring(chars, initialOffset, endPos); if (preserveNegativeZero && ei.IsZero && negative) { // TODO: In next major version, change to EDecimal.NegativeZero return(CBORObject.FromFloatingPointBits(0x8000, 2)); } return(CBORObject.FromObject(ei)); } if (!haveExponent && haveDecimalPoint) { // No more than 18 digits plus one decimal point (which // should fit a long) long lv = 0L; int expo = -(endPos - (decimalPointPos + 1)); int vi = numOffset; var digitCount = 0; for (; vi < decimalPointPos; ++vi) { if (digitCount < 0 || digitCount >= 18) { digitCount = -1; break; } else if (digitCount > 0 || chars[vi] != '0') { ++digitCount; } lv = checked ((lv * 10) + (int)(chars[vi] - '0')); } for (vi = decimalPointPos + 1; vi < endPos; ++vi) { if (digitCount < 0 || digitCount >= 18) { digitCount = -1; break; } else if (digitCount > 0 || chars[vi] != '0') { ++digitCount; } lv = checked ((lv * 10) + (int)(chars[vi] - '0')); } if (negative) { lv = -lv; } if (digitCount >= 0 && (!negative || lv != 0)) { if (expo == 0) { return(CBORObject.FromObject(lv)); } else { CBORObject cbor = CBORObject.FromArrayBackedObject( new CBORObject[] { CBORObject.FromObject(expo), CBORObject.FromObject(lv), }); return(cbor.WithTag(4)); } } } // DebugUtility.Log("convfull " + chars.Substring(initialOffset, endPos - // initialOffset)); EDecimal ed = EDecimal.FromString( chars, initialOffset, endPos - initialOffset); if (ed.IsZero && negative) { if (ed.Exponent.IsZero) { // TODO: In next major version, use EDecimal // for preserveNegativeZero return(preserveNegativeZero ? CBORObject.FromFloatingPointBits(0x8000, 2) : CBORObject.FromObject(0)); } else if (!preserveNegativeZero) { return(CBORObject.FromObject(ed.Negate())); } else { return(CBORObject.FromObject(ed)); } } else { return(ed.Exponent.IsZero ? CBORObject.FromObject(ed.Mantissa) : CBORObject.FromObject(ed)); } } else if (kind == JSONOptions.ConversionMode.Double) { EFloat ef = EFloat.FromString( chars, initialOffset, endPos - initialOffset, EContext.Binary64); long lb = ef.ToDoubleBits(); if (!preserveNegativeZero && (lb == 1L << 63 || lb == 0L)) { lb = 0L; } return(CBORObject.FromFloatingPointBits(lb, 8)); } else if (kind == JSONOptions.ConversionMode.Decimal128) { EDecimal ed = EDecimal.FromString( chars, initialOffset, endPos - initialOffset, EContext.Decimal128); if (!preserveNegativeZero && ed.IsNegative && ed.IsZero) { ed = ed.Negate(); } return(CBORObject.FromObject(ed)); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble) { EFloat ef = EFloat.FromString( chars, initialOffset, endPos - initialOffset, EContext.Binary64); long lb = ef.ToDoubleBits(); return((!CBORUtilities.IsBeyondSafeRange(lb) && CBORUtilities.IsIntegerValue(lb)) ? CBORObject.FromObject(CBORUtilities.GetIntegerValue(lb)) : CBORObject.FromFloatingPointBits(lb, 8)); } else if (kind == JSONOptions.ConversionMode.IntOrFloat) { EContext ctx = EContext.Binary64.WithBlankFlags(); EFloat ef = EFloat.FromString( chars, initialOffset, endPos - initialOffset, ctx); long lb = ef.ToDoubleBits(); if ((ctx.Flags & EContext.FlagInexact) != 0) { // Inexact conversion to double, meaning that the string doesn't // represent an integer in [-(2^53)+1, 2^53), which is representable // exactly as double, so treat as ConversionMode.Double if (!preserveNegativeZero && (lb == 1L << 63 || lb == 0L)) { lb = 0L; } return(CBORObject.FromFloatingPointBits(lb, 8)); } else { // Exact conversion; treat as ConversionMode.IntToFloatFromDouble return((!CBORUtilities.IsBeyondSafeRange(lb) && CBORUtilities.IsIntegerValue(lb)) ? CBORObject.FromObject(CBORUtilities.GetIntegerValue(lb)) : CBORObject.FromFloatingPointBits(lb, 8)); } } else { throw new ArgumentException("Unsupported conversion kind."); } }
public CBORObject ReadForFirstByte( int firstbyte, CBORTypeFilter filter) { if (this.depth > 500) { throw new CBORException("Too deeply nested"); } if (firstbyte < 0) { throw new CBORException("Premature end of data"); } if (firstbyte == 0xff) { throw new CBORException("Unexpected break code encountered"); } int type = (firstbyte >> 5) & 0x07; int additional = firstbyte & 0x1f; int expectedLength = CBORObject.GetExpectedLength(firstbyte); // Data checks if (expectedLength == -1) { // if the head byte is invalid throw new CBORException("Unexpected data encountered"); } if (filter != null) { // Check for valid major types if asked if (!filter.MajorTypeMatches(type)) { throw new CBORException("Unexpected data type encountered"); } if (firstbyte >= 0xe0 && firstbyte <= 0xff && firstbyte != 0xf9 && firstbyte != 0xfa && firstbyte != 0xfb) { if (!filter.NonFPSimpleValueAllowed()) { throw new CBORException("Unexpected data type encountered"); } } } // Check if this represents a fixed object CBORObject fixedObject = CBORObject.GetFixedObject(firstbyte); if (fixedObject != null) { return(fixedObject); } // Read fixed-length data byte[] data = null; if (expectedLength != 0) { data = new byte[expectedLength]; // include the first byte because GetFixedLengthObject // will assume it exists for some head bytes data[0] = unchecked ((byte)firstbyte); if (expectedLength > 1 && this.stream.Read(data, 1, expectedLength - 1) != expectedLength - 1) { throw new CBORException("Premature end of data"); } CBORObject cbor = CBORObject.GetFixedLengthObject(firstbyte, data); if (this.stringRefs != null && (type == 2 || type == 3)) { this.stringRefs.AddStringIfNeeded(cbor, expectedLength - 1); } return(cbor); } var uadditional = (long)additional; EInteger bigintAdditional = EInteger.Zero; var hasBigAdditional = false; data = new byte[8]; var lowAdditional = 0; switch (firstbyte & 0x1f) { case 24: { int tmp = this.stream.ReadByte(); if (tmp < 0) { throw new CBORException("Premature end of data"); } lowAdditional = tmp; uadditional = lowAdditional; break; } case 25: { if (this.stream.Read(data, 0, 2) != 2) { throw new CBORException("Premature end of data"); } lowAdditional = ((int)(data[0] & (int)0xff)) << 8; lowAdditional |= (int)(data[1] & (int)0xff); uadditional = lowAdditional; break; } case 26: { if (this.stream.Read(data, 0, 4) != 4) { throw new CBORException("Premature end of data"); } uadditional = ((long)(data[0] & (long)0xff)) << 24; uadditional |= ((long)(data[1] & (long)0xff)) << 16; uadditional |= ((long)(data[2] & (long)0xff)) << 8; uadditional |= (long)(data[3] & (long)0xff); break; } case 27: { if (this.stream.Read(data, 0, 8) != 8) { throw new CBORException("Premature end of data"); } if ((((int)data[0]) & 0x80) != 0) { // Won't fit in a signed 64-bit number var uabytes = new byte[9]; uabytes[0] = data[7]; uabytes[1] = data[6]; uabytes[2] = data[5]; uabytes[3] = data[4]; uabytes[4] = data[3]; uabytes[5] = data[2]; uabytes[6] = data[1]; uabytes[7] = data[0]; uabytes[8] = 0; hasBigAdditional = true; bigintAdditional = EInteger.FromBytes(uabytes, true); } else { uadditional = ((long)(data[0] & (long)0xff)) << 56; uadditional |= ((long)(data[1] & (long)0xff)) << 48; uadditional |= ((long)(data[2] & (long)0xff)) << 40; uadditional |= ((long)(data[3] & (long)0xff)) << 32; uadditional |= ((long)(data[4] & (long)0xff)) << 24; uadditional |= ((long)(data[5] & (long)0xff)) << 16; uadditional |= ((long)(data[6] & (long)0xff)) << 8; uadditional |= (long)(data[7] & (long)0xff); } break; } } // The following doesn't check for major types 0 and 1, // since all of them are fixed-length types and are // handled in the call to GetFixedLengthObject. if (type == 2) // Byte string { if (additional == 31) { // Streaming byte string using (var ms = new MemoryStream()) { // Requires same type as this one while (true) { int nextByte = this.stream.ReadByte(); if (nextByte == 0xff) { // break if the "break" code was read break; } long len = ReadDataLength(this.stream, nextByte, 2); if ((len >> 63) != 0 || len > Int32.MaxValue) { throw new CBORException("Length" + ToUnsignedBigInteger(len) + " is bigger than supported "); } if (nextByte != 0x40) { // NOTE: 0x40 means the empty byte string ReadByteData(this.stream, len, ms); } } if (ms.Position > Int32.MaxValue) { throw new CBORException("Length of bytes to be streamed is bigger than supported "); } data = ms.ToArray(); return(new CBORObject( CBORObject.CBORObjectTypeByteString, data)); } } else { if (hasBigAdditional) { throw new CBORException("Length of " + CBORUtilities.BigIntToString(bigintAdditional) + " is bigger than supported"); } if (uadditional > Int32.MaxValue) { throw new CBORException("Length of " + CBORUtilities.LongToString(uadditional) + " is bigger than supported"); } data = ReadByteData(this.stream, uadditional, null); var cbor = new CBORObject(CBORObject.CBORObjectTypeByteString, data); if (this.stringRefs != null) { int hint = (uadditional > Int32.MaxValue || hasBigAdditional) ? Int32.MaxValue : (int)uadditional; this.stringRefs.AddStringIfNeeded(cbor, hint); } return(cbor); } } if (type == 3) // Text string { if (additional == 31) { // Streaming text string var builder = new StringBuilder(); while (true) { int nextByte = this.stream.ReadByte(); if (nextByte == 0xff) { // break if the "break" code was read break; } long len = ReadDataLength(this.stream, nextByte, 3); if ((len >> 63) != 0 || len > Int32.MaxValue) { throw new CBORException("Length" + ToUnsignedBigInteger(len) + " is bigger than supported"); } if (nextByte != 0x60) { // NOTE: 0x60 means the empty string if (PropertyMap.ExceedsKnownLength(this.stream, len)) { throw new CBORException("Premature end of data"); } switch ( DataUtilities.ReadUtf8( this.stream, (int)len, builder, false)) { case -1: throw new CBORException("Invalid UTF-8"); case -2: throw new CBORException("Premature end of data"); } } } return(new CBORObject( CBORObject.CBORObjectTypeTextString, builder.ToString())); } else { if (hasBigAdditional) { throw new CBORException("Length of " + CBORUtilities.BigIntToString(bigintAdditional) + " is bigger than supported"); } if (uadditional > Int32.MaxValue) { throw new CBORException("Length of " + CBORUtilities.LongToString(uadditional) + " is bigger than supported"); } if (PropertyMap.ExceedsKnownLength(this.stream, uadditional)) { throw new CBORException("Premature end of data"); } var builder = new StringBuilder(); switch ( DataUtilities.ReadUtf8( this.stream, (int)uadditional, builder, false)) { case -1: throw new CBORException("Invalid UTF-8"); case -2: throw new CBORException("Premature end of data"); } var cbor = new CBORObject( CBORObject.CBORObjectTypeTextString, builder.ToString()); if (this.stringRefs != null) { int hint = (uadditional > Int32.MaxValue || hasBigAdditional) ? Int32.MaxValue : (int)uadditional; this.stringRefs.AddStringIfNeeded(cbor, hint); } return(cbor); } } if (type == 4) // Array { CBORObject cbor = CBORObject.NewArray(); if (additional == 31) { var vtindex = 0; // Indefinite-length array while (true) { int headByte = this.stream.ReadByte(); if (headByte < 0) { throw new CBORException("Premature end of data"); } if (headByte == 0xff) { // Break code was read break; } if (filter != null && !filter.ArrayIndexAllowed(vtindex)) { throw new CBORException("Array is too long"); } ++this.depth; CBORObject o = this.ReadForFirstByte( headByte, filter == null ? null : filter.GetSubFilter(vtindex)); --this.depth; cbor.Add(o); ++vtindex; } return(cbor); } if (hasBigAdditional) { throw new CBORException("Length of " + CBORUtilities.BigIntToString(bigintAdditional) + " is bigger than supported"); } if (uadditional > Int32.MaxValue) { throw new CBORException("Length of " + CBORUtilities.LongToString(uadditional) + " is bigger than supported"); } if (filter != null && !filter.ArrayLengthMatches(uadditional)) { throw new CBORException("Array is too long"); } if (PropertyMap.ExceedsKnownLength(this.stream, uadditional)) { throw new CBORException("Remaining data too small for array length"); } ++this.depth; for (long i = 0; i < uadditional; ++i) { cbor.Add( this.Read(filter == null ? null : filter.GetSubFilter(i))); } --this.depth; return(cbor); } if (type == 5) // Map, type 5 { CBORObject cbor = CBORObject.NewMap(); if (additional == 31) { // Indefinite-length map while (true) { int headByte = this.stream.ReadByte(); if (headByte < 0) { throw new CBORException("Premature end of data"); } if (headByte == 0xff) { // Break code was read break; } ++this.depth; CBORObject key = this.ReadForFirstByte(headByte, null); CBORObject value = this.Read(null); --this.depth; if (this.policy == CBORDuplicatePolicy.Disallow) { if (cbor.ContainsKey(key)) { throw new CBORException("Duplicate key already exists: " + key); } } cbor[key] = value; } return(cbor); } if (hasBigAdditional) { throw new CBORException("Length of " + CBORUtilities.BigIntToString(bigintAdditional) + " is bigger than supported"); } if (uadditional > Int32.MaxValue) { throw new CBORException("Length of " + CBORUtilities.LongToString(uadditional) + " is bigger than supported"); } if (PropertyMap.ExceedsKnownLength(this.stream, uadditional)) { throw new CBORException("Remaining data too small for map length"); } for (long i = 0; i < uadditional; ++i) { ++this.depth; CBORObject key = this.Read(null); CBORObject value = this.Read(null); --this.depth; if (this.policy == CBORDuplicatePolicy.Disallow) { if (cbor.ContainsKey(key)) { throw new CBORException("Duplicate key already exists: " + key); } } cbor[key] = value; } return(cbor); } if (type == 6) // Tagged item { ICBORTag taginfo = null; var haveFirstByte = false; var newFirstByte = -1; if (!hasBigAdditional) { if (filter != null && !filter.TagAllowed(uadditional)) { throw new CBORException("Unexpected tag encountered: " + uadditional); } int uad = uadditional >= 257 ? 257 : (uadditional < 0 ? 0 : (int)uadditional); switch (uad) { case 256: // Tag 256: String namespace this.stringRefs = this.stringRefs ?? (new StringRefs()); this.stringRefs.Push(); break; case 25: // String reference if (this.stringRefs == null) { throw new CBORException("No stringref namespace"); } break; case 28: case 29: this.hasSharableObjects = true; break; } taginfo = CBORObject.FindTagConverterLong(uadditional); } else { if (filter != null && !filter.TagAllowed(bigintAdditional)) { throw new CBORException("Unexpected tag encountered: " + uadditional); } taginfo = CBORObject.FindTagConverter(bigintAdditional); } ++this.depth; CBORObject o = haveFirstByte ? this.ReadForFirstByte( newFirstByte, taginfo == null ? null : taginfo.GetTypeFilter()) : this.Read(taginfo == null ? null : taginfo.GetTypeFilter()); --this.depth; if (hasBigAdditional) { return(CBORObject.FromObjectAndTag(o, bigintAdditional)); } if (uadditional < 65536) { int uaddl = uadditional >= 257 ? 257 : (uadditional < 0 ? 0 : (int)uadditional); switch (uaddl) { case 256: // string tag this.stringRefs.Pop(); break; case 25: // stringref tag return(this.stringRefs.GetString(o.AsEInteger())); } return(CBORObject.FromObjectAndTag( o, (int)uadditional)); } return(CBORObject.FromObjectAndTag( o, (EInteger)uadditional)); } throw new CBORException("Unexpected data encountered"); }
internal static void WriteJSONToInternal( CBORObject obj, StringOutput writer) { int type = obj.ItemType; object thisItem = obj.ThisItem; switch (type) { case CBORObject.CBORObjectTypeSimpleValue: { if (obj.IsTrue) { writer.WriteString("true"); return; } if (obj.IsFalse) { writer.WriteString("false"); return; } writer.WriteString("null"); return; } case CBORObject.CBORObjectTypeSingle: { var f = (float)thisItem; if (Single.IsNegativeInfinity(f) || Single.IsPositiveInfinity(f) || Single.IsNaN(f)) { writer.WriteString("null"); return; } writer.WriteString( CBORObject.TrimDotZero( CBORUtilities.SingleToString(f))); return; } case CBORObject.CBORObjectTypeDouble: { var f = (double)thisItem; if (Double.IsNegativeInfinity(f) || Double.IsPositiveInfinity(f) || Double.IsNaN(f)) { writer.WriteString("null"); return; } string dblString = CBORUtilities.DoubleToString(f); writer.WriteString( CBORObject.TrimDotZero(dblString)); return; } case CBORObject.CBORObjectTypeInteger: { var longItem = (long)thisItem; writer.WriteString(CBORUtilities.LongToString(longItem)); return; } case CBORObject.CBORObjectTypeBigInteger: { writer.WriteString( CBORUtilities.BigIntToString((EInteger)thisItem)); return; } case CBORObject.CBORObjectTypeExtendedDecimal: { var dec = (EDecimal)thisItem; if (dec.IsInfinity() || dec.IsNaN()) { writer.WriteString("null"); } else { writer.WriteString(dec.ToString()); } return; } case CBORObject.CBORObjectTypeExtendedFloat: { var flo = (EFloat)thisItem; if (flo.IsInfinity() || flo.IsNaN()) { writer.WriteString("null"); return; } if (flo.IsFinite && flo.Exponent.Abs().CompareTo((EInteger)2500) > 0) { // Too inefficient to convert to a decimal number // from a bigfloat with a very high exponent, // so convert to double instead double f = flo.ToDouble(); if (Double.IsNegativeInfinity(f) || Double.IsPositiveInfinity(f) || Double.IsNaN(f)) { writer.WriteString("null"); return; } string dblString = CBORUtilities.DoubleToString(f); writer.WriteString( CBORObject.TrimDotZero(dblString)); return; } writer.WriteString(flo.ToString()); return; } case CBORObject.CBORObjectTypeByteString: { var byteArray = (byte[])thisItem; if (byteArray.Length == 0) { writer.WriteString("\"\""); return; } writer.WriteCodePoint((int)'\"'); if (obj.HasTag(22)) { Base64.WriteBase64( writer, byteArray, 0, byteArray.Length, false); } else if (obj.HasTag(23)) { // Write as base16 for (int i = 0; i < byteArray.Length; ++i) { writer.WriteCodePoint((int)Hex16[(byteArray[i] >> 4) & 15]); writer.WriteCodePoint((int)Hex16[byteArray[i] & 15]); } } else { Base64.WriteBase64URL( writer, byteArray, 0, byteArray.Length, false); } writer.WriteCodePoint((int)'\"'); break; } case CBORObject.CBORObjectTypeTextString: { var thisString = (string)thisItem; if (thisString.Length == 0) { writer.WriteString("\"\""); return; } writer.WriteCodePoint((int)'\"'); WriteJSONStringUnquoted(thisString, writer); writer.WriteCodePoint((int)'\"'); break; } case CBORObject.CBORObjectTypeArray: { var first = true; writer.WriteCodePoint((int)'['); foreach (CBORObject i in obj.AsList()) { if (!first) { writer.WriteCodePoint((int)','); } WriteJSONToInternal(i, writer); first = false; } writer.WriteCodePoint((int)']'); break; } case CBORObject.CBORObjectTypeExtendedRational: { var dec = (ERational)thisItem; EDecimal f = dec.ToEDecimalExactIfPossible( EContext.Decimal128.WithUnlimitedExponents()); if (!f.IsFinite) { writer.WriteString("null"); } else { writer.WriteString(f.ToString()); } break; } case CBORObject.CBORObjectTypeMap: { var first = true; var hasNonStringKeys = false; IDictionary <CBORObject, CBORObject> objMap = obj.AsMap(); foreach (KeyValuePair <CBORObject, CBORObject> entry in objMap) { CBORObject key = entry.Key; if (key.ItemType != CBORObject.CBORObjectTypeTextString) { hasNonStringKeys = true; break; } } if (!hasNonStringKeys) { writer.WriteCodePoint((int)'{'); foreach (KeyValuePair <CBORObject, CBORObject> entry in objMap) { CBORObject key = entry.Key; CBORObject value = entry.Value; if (!first) { writer.WriteCodePoint((int)','); } writer.WriteCodePoint((int)'\"'); WriteJSONStringUnquoted((string)key.ThisItem, writer); writer.WriteCodePoint((int)'\"'); writer.WriteCodePoint((int)':'); WriteJSONToInternal(value, writer); first = false; } writer.WriteCodePoint((int)'}'); } else { // This map has non-string keys IDictionary <string, CBORObject> stringMap = new Dictionary <string, CBORObject>(); // Copy to a map with String keys, since // some keys could be duplicates // when serialized to strings foreach (KeyValuePair <CBORObject, CBORObject> entry in objMap) { CBORObject key = entry.Key; CBORObject value = entry.Value; string str = (key.ItemType == CBORObject.CBORObjectTypeTextString) ? ((string)key.ThisItem) : key.ToJSONString(); stringMap[str] = value; } first = true; writer.WriteCodePoint((int)'{'); foreach (KeyValuePair <string, CBORObject> entry in stringMap) { string key = entry.Key; CBORObject value = entry.Value; if (!first) { writer.WriteCodePoint((int)','); } writer.WriteCodePoint((int)'\"'); WriteJSONStringUnquoted((string)key, writer); writer.WriteCodePoint((int)'\"'); writer.WriteCodePoint((int)':'); WriteJSONToInternal(value, writer); first = false; } writer.WriteCodePoint((int)'}'); } break; } default: throw new InvalidOperationException("Unexpected item type"); } }
public bool CanFitInSingle(object obj) { return(this.IsNaN(obj) || CBORUtilities.DoubleRetainsSameValueInSingle((long)obj)); }
private static string RemoveIsPrefix(string pn) { return(CBORUtilities.NameStartsWithWord(pn, "Is") ? pn.Substring(2) : pn); }
private CBORObject ReadStringArrayMap(int type, long uadditional) { bool canonical = this.options.Ctap2Canonical; if (type == 2 || type == 3) // Byte string or text string { if ((uadditional >> 31) != 0) { throw new CBORException("Length of " + ToUnsignedEInteger(uadditional).ToString() + " is bigger" + "\u0020than supported"); } int hint = (uadditional > Int32.MaxValue || (uadditional >> 63) != 0) ? Int32.MaxValue : (int)uadditional; byte[] data = ReadByteData(this.stream, uadditional, null); if (type == 3) { if (!CBORUtilities.CheckUtf8(data)) { throw new CBORException("Invalid UTF-8"); } return(this.ObjectFromUtf8Array(data, hint)); } else { return(this.ObjectFromByteArray(data, hint)); } } if (type == 4) // Array { if (this.options.Ctap2Canonical && this.depth >= 4) { throw new CBORException("Depth too high in canonical CBOR"); } CBORObject cbor = CBORObject.NewArray(); if ((uadditional >> 31) != 0) { throw new CBORException("Length of " + ToUnsignedEInteger(uadditional).ToString() + " is bigger than" + "\u0020supported"); } if (PropertyMap.ExceedsKnownLength(this.stream, uadditional)) { throw new CBORException("Remaining data too small for array" + "\u0020length"); } ++this.depth; for (long i = 0; i < uadditional; ++i) { cbor.Add( this.ReadInternal()); } --this.depth; return(cbor); } if (type == 5) // Map, type 5 { if (this.options.Ctap2Canonical && this.depth >= 4) { throw new CBORException("Depth too high in canonical CBOR"); } CBORObject cbor = CBORObject.NewMap(); if ((uadditional >> 31) != 0) { throw new CBORException("Length of " + ToUnsignedEInteger(uadditional).ToString() + " is bigger than" + "\u0020supported"); } if (PropertyMap.ExceedsKnownLength(this.stream, uadditional)) { throw new CBORException("Remaining data too small for map" + "\u0020length"); } CBORObject lastKey = null; IComparer <CBORObject> comparer = CBORCanonical.Comparer; for (long i = 0; i < uadditional; ++i) { ++this.depth; CBORObject key = this.ReadInternal(); CBORObject value = this.ReadInternal(); --this.depth; if (this.options.Ctap2Canonical && lastKey != null) { int cmp = comparer.Compare(lastKey, key); if (cmp > 0) { throw new CBORException("Map key not in canonical order"); } else if (cmp == 0) { throw new CBORException("Duplicate map key"); } } if (!this.options.AllowDuplicateKeys) { if (cbor.ContainsKey(key)) { throw new CBORException("Duplicate key already exists"); } } lastKey = key; cbor[key] = value; } return(cbor); } return(null); }
public bool IsIntegral(object obj) { return(CBORUtilities.IsIntegerValue((long)obj)); }
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"); } 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"); } EDecimal dec; dec = (EDecimal)untagobj.ToObject(typeof(EDecimal)); CBORUtilities.BreakDownSecondsSinceEpoch( dec, outYear, lesserFields); return(null); // No error } return("Not tag 0 or 1"); }
public float AsSingle(object obj) { return(CBORUtilities.Int32BitsToSingle( CBORUtilities.DoubleToRoundedSinglePrecision((long)obj))); }
public double AsDouble(object obj) { return(CBORUtilities.Int64BitsToDouble((long)obj)); }
public bool IsNaN(object obj) { return(CBORUtilities.DoubleBitsNaN((long)obj)); }
/// <summary>Converts a date/time in the form of a year, month, day, /// hour, minute, second, fractional seconds, and time offset to a CBOR /// object.</summary> /// <param name='bigYear'>The parameter <paramref name='bigYear'/> is a /// Numbers.EInteger object.</param> /// <param name='lesserFields'>An array that will store the fields /// (other than the year) of the date and time. See the /// TryGetDateTimeFields method for information on the "lesserFields" /// parameter.</param> /// <returns>A CBOR object encoding the given date fields according to /// the conversion type used to create this date converter.</returns> /// <exception cref='ArgumentNullException'>The parameter <paramref /// name='bigYear'/> or <paramref name='lesserFields'/> is /// null.</exception> /// <exception cref='PeterO.Cbor.CBORException'>An error occurred in /// conversion.</exception> public CBORObject DateTimeFieldsToCBORObject(EInteger bigYear, int[] lesserFields) { if (bigYear == null) { throw new ArgumentNullException(nameof(bigYear)); } 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"); } try { switch (this.convType) { case ConversionType.TaggedString: { string str = CBORUtilities.ToAtomDateTimeString(bigYear, lesserFields); return(CBORObject.FromObjectAndTag(str, 0)); } case ConversionType.TaggedNumber: case ConversionType.UntaggedNumber: try { var status = new int[1]; EFloat ef = CBORUtilities.DateTimeToIntegerOrDouble( bigYear, lesserFields, status); if (status[0] == 0) { return(this.convType == ConversionType.TaggedNumber ? CBORObject.FromObjectAndTag(ef.ToEInteger(), 1) : CBORObject.FromObject(ef.ToEInteger())); } else if (status[0] == 1) { return(this.convType == ConversionType.TaggedNumber ? CBORObject.FromFloatingPointBits(ef.ToDoubleBits(), 8).WithTag(1) : CBORObject.FromFloatingPointBits(ef.ToDoubleBits(), 8)); } else { throw new CBORException("Too big or small to fit an integer" + "\u0020or" + "\u0020floating-point number"); } } catch (ArgumentException ex) { throw new CBORException(ex.Message, ex); } default: throw new CBORException("Internal error"); } } catch (ArgumentException ex) { throw new CBORException(ex.Message, ex); } }
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()); }