Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
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");
 }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
 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);
     }
 }
Ejemplo n.º 6
0
            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);
            }
Ejemplo n.º 7
0
 public double AsDouble(object obj)
 {
     return(CBORUtilities.Int64BitsToDouble((long)obj));
 }
Ejemplo n.º 8
0
        /// <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.Cbor2.CBORException'>An error occurred in
        /// conversion.</exception>
        public CBORObject DateTimeFieldsToCBORObject(EInteger bigYear, int[]
                                                     lesserFields)
        {
            // TODO: In next minor version, add overload that takes int rather than
            // EInteger
            if (bigYear == null)
            {
                throw new ArgumentNullException(nameof(bigYear));
            }
            if (lesserFields == null)
            {
                throw new ArgumentNullException(nameof(lesserFields));
            }
            // TODO: Make into CBORException in next major version
            if (lesserFields.Length < 7)
            {
                throw new ArgumentException("\"lesserFields\" + \"'s length\" (" +
                                            lesserFields.Length + ") is not greater or equal to 7");
            }
            try {
                CBORUtilities.CheckYearAndLesserFields(bigYear, lesserFields);
                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);
            }
        }
Ejemplo n.º 9
0
        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);
        }
        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.");
            }
        }
Ejemplo n.º 11
0
 public EInteger AsEInteger(object obj)
 {
     return(CBORUtilities.BigIntegerFromDouble((double)obj));
 }
Ejemplo n.º 12
0
        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());
        }
Ejemplo n.º 13
0
 public float AsSingle(object obj)
 {
     return(CBORUtilities.Int32BitsToSingle(
                CBORUtilities.DoubleToRoundedSinglePrecision((long)obj)));
 }
Ejemplo n.º 14
0
 public EInteger AsEInteger(object obj)
 {
     return(CBORUtilities.EIntegerFromDoubleBits((long)obj));
 }
Ejemplo n.º 15
0
 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");
 }
Ejemplo n.º 16
0
 public bool IsNaN(object obj)
 {
     return(CBORUtilities.DoubleBitsNaN((long)obj));
 }
Ejemplo n.º 17
0
 public bool IsIntegral(object obj)
 {
     return(CBORUtilities.IsIntegerValue((long)obj));
 }
Ejemplo n.º 18
0
 public bool CanFitInSingle(object obj)
 {
     return(this.IsNaN(obj) ||
            CBORUtilities.DoubleRetainsSameValueInSingle((long)obj));
 }
Ejemplo n.º 19
0
 private static string RemoveIsPrefix(string pn)
 {
     return(CBORUtilities.NameStartsWithWord(pn, "Is") ? pn.Substring(2) :
            pn);
 }