Esempio n. 1
0
        private static bool CheckCircularRef(
            IList <CBORObject> stack,
            CBORObject parent,
            CBORObject child)
        {
            if (child.Type != CBORType.Array && child.Type != CBORType.Map)
            {
                return(false);
            }
            CBORObject childUntag = child.Untag();

            if (parent.Untag() == childUntag)
            {
                throw new CBORException("Circular reference in CBOR object");
            }
            if (stack != null)
            {
                foreach (CBORObject o in stack)
                {
                    if (o.Untag() == childUntag)
                    {
                        throw new CBORException("Circular reference in CBOR object");
                    }
                }
            }
            stack.Add(child);
            return(true);
        }
Esempio n. 2
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());
        }
Esempio n. 3
0
        private static byte[] CtapCanonicalEncode(CBORObject a, int depth)
        {
            CBORObject cbor       = a.Untag();
            CBORType   valueAType = cbor.Type;

            try {
                if (valueAType == CBORType.Array)
                {
                    using (var ms = new MemoryStream()) {
                        CBORObject.WriteValue(ms, 4, cbor.Count);
                        for (var i = 0; i < cbor.Count; ++i)
                        {
                            if (depth >= 3 && IsArrayOrMap(cbor[i]))
                            {
                                throw new CBORException("Nesting level too deep");
                            }
                            byte[] bytes = CtapCanonicalEncode(cbor[i], depth + 1);
                            ms.Write(bytes, 0, bytes.Length);
                        }
                        return(ms.ToArray());
                    }
                }
                else if (valueAType == CBORType.Map)
                {
                    KeyValuePair <byte[], byte[]>         kv1;
                    List <KeyValuePair <byte[], byte[]> > sortedKeys;
                    sortedKeys = new List <KeyValuePair <byte[], byte[]> >();
                    foreach (CBORObject key in cbor.Keys)
                    {
                        if (depth >= 3 && (IsArrayOrMap(key) ||
                                           IsArrayOrMap(cbor[key])))
                        {
                            throw new CBORException("Nesting level too deep");
                        }
                        CheckDepth(key, depth + 1);
                        CheckDepth(cbor[key], depth + 1);
                        // Check if key and value can be canonically encoded
                        // (will throw an exception if they cannot)
                        kv1 = new KeyValuePair <byte[], byte[]>(
                            CtapCanonicalEncode(key, depth + 1),
                            CtapCanonicalEncode(cbor[key], depth + 1));
                        sortedKeys.Add(kv1);
                    }
                    sortedKeys.Sort(ByteComparer);
                    using (var ms = new MemoryStream()) {
                        CBORObject.WriteValue(ms, 5, cbor.Count);
                        byte[] lastKey = null;
                        for (var i = 0; i < sortedKeys.Count; ++i)
                        {
                            kv1 = sortedKeys[i];
                            byte[] bytes = kv1.Key;
                            if (lastKey != null && ByteArraysEqual(bytes, lastKey))
                            {
                                throw new CBORException("duplicate canonical CBOR key");
                            }
                            lastKey = bytes;
                            ms.Write(bytes, 0, bytes.Length);
                            bytes = kv1.Value;
                            ms.Write(bytes, 0, bytes.Length);
                        }
                        return(ms.ToArray());
                    }
                }
            } catch (IOException ex) {
                throw new InvalidOperationException(ex.ToString(), ex);
            }
            if (valueAType == CBORType.SimpleValue ||
                valueAType == CBORType.Boolean || valueAType == CBORType.ByteString ||
                valueAType == CBORType.TextString)
            {
                return(cbor.EncodeToBytes(CBOREncodeOptions.Default));
            }
            else if (valueAType == CBORType.FloatingPoint)
            {
                long bits = cbor.AsDoubleBits();
                return(new byte[] {
                    (byte)0xfb,
                    (byte)((bits >> 56) & 0xffL),
                    (byte)((bits >> 48) & 0xffL),
                    (byte)((bits >> 40) & 0xffL),
                    (byte)((bits >> 32) & 0xffL),
                    (byte)((bits >> 24) & 0xffL),
                    (byte)((bits >> 16) & 0xffL),
                    (byte)((bits >> 8) & 0xffL),
                    (byte)(bits & 0xffL),
                });
            }
            else if (valueAType == CBORType.Integer)
            {
                return(cbor.EncodeToBytes(CBOREncodeOptions.Default));
            }
            else
            {
                throw new ArgumentException("Invalid CBOR type.");
            }
        }
Esempio n. 4
0
        internal static void WriteJSONToInternal(
            CBORObject obj,
            StringOutput writer,
            JSONOptions options,
            IList <CBORObject> stack)
        {
            if (obj.IsNumber)
            {
                writer.WriteString(CBORNumber.FromCBORObject(obj).ToJSONString());
                return;
            }
            switch (obj.Type)
            {
            case CBORType.Integer:
            case CBORType.FloatingPoint: {
                CBORObject untaggedObj = obj.Untag();
                writer.WriteString(
                    CBORNumber.FromCBORObject(untaggedObj).ToJSONString());
                break;
            }

            case CBORType.Boolean: {
                if (obj.IsTrue)
                {
                    writer.WriteString("true");
                    return;
                }
                if (obj.IsFalse)
                {
                    writer.WriteString("false");
                    return;
                }
                return;
            }

            case CBORType.SimpleValue: {
                writer.WriteString("null");
                return;
            }

            case CBORType.ByteString: {
                byte[] byteArray = obj.GetByteString();
                if (byteArray.Length == 0)
                {
                    writer.WriteString("\"\"");
                    return;
                }
                writer.WriteCodePoint((int)'\"');
                if (obj.HasTag(22))
                {
                    // Base64 with padding
                    Base64.WriteBase64(
                        writer,
                        byteArray,
                        0,
                        byteArray.Length,
                        true);
                }
                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
                {
                    // Base64url no padding
                    Base64.WriteBase64URL(
                        writer,
                        byteArray,
                        0,
                        byteArray.Length,
                        false);
                }
                writer.WriteCodePoint((int)'\"');
                break;
            }

            case CBORType.TextString: {
                string thisString = obj.AsString();
                if (thisString.Length == 0)
                {
                    writer.WriteString("\"\"");
                    return;
                }
                writer.WriteCodePoint((int)'\"');
                WriteJSONStringUnquoted(thisString, writer, options);
                writer.WriteCodePoint((int)'\"');
                break;
            }

            case CBORType.Array: {
                writer.WriteCodePoint((int)'[');
                for (var i = 0; i < obj.Count; ++i)
                {
                    if (i > 0)
                    {
                        writer.WriteCodePoint((int)',');
                    }
                    bool pop = CheckCircularRef(stack, obj, obj[i]);
                    WriteJSONToInternal(obj[i], writer, options, stack);
                    PopRefIfNeeded(stack, pop);
                }
                writer.WriteCodePoint((int)']');
                break;
            }

            case CBORType.Map: {
                var first            = true;
                var hasNonStringKeys = false;
                ICollection <KeyValuePair <CBORObject, CBORObject> > entries =
                    obj.Entries;
                foreach (KeyValuePair <CBORObject, CBORObject> entry in entries)
                {
                    CBORObject key = entry.Key;
                    if (key.Type != CBORType.TextString ||
                        key.IsTagged)
                    {
                        // treat a non-text-string item or a tagged item
                        // as having non-string keys
                        hasNonStringKeys = true;
                        break;
                    }
                }
                if (!hasNonStringKeys)
                {
                    writer.WriteCodePoint((int)'{');
                    foreach (KeyValuePair <CBORObject, CBORObject> entry in entries)
                    {
                        CBORObject key   = entry.Key;
                        CBORObject value = entry.Value;
                        if (!first)
                        {
                            writer.WriteCodePoint((int)',');
                        }
                        writer.WriteCodePoint((int)'\"');
                        WriteJSONStringUnquoted(key.AsString(), writer, options);
                        writer.WriteCodePoint((int)'\"');
                        writer.WriteCodePoint((int)':');
                        bool pop = CheckCircularRef(stack, obj, value);
                        WriteJSONToInternal(value, writer, options, stack);
                        PopRefIfNeeded(stack, pop);
                        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 entries)
                    {
                        CBORObject key   = entry.Key;
                        CBORObject value = entry.Value;
                        string     str   = null;
                        switch (key.Type)
                        {
                        case CBORType.TextString:
                            str = key.AsString();
                            break;

                        case CBORType.Array:
                        case CBORType.Map: {
                            var  sb  = new StringBuilder();
                            var  sw  = new StringOutput(sb);
                            bool pop = CheckCircularRef(stack, obj, key);
                            WriteJSONToInternal(key, sw, options, stack);
                            PopRefIfNeeded(stack, pop);
                            str = sb.ToString();
                            break;
                        }

                        default: str = key.ToJSONString(options);
                            break;
                        }
                        if (stringMap.ContainsKey(str))
                        {
                            throw new CBORException(
                                      "Duplicate JSON string equivalents of map" +
                                      "\u0020keys");
                        }
                        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, options);
                        writer.WriteCodePoint((int)'\"');
                        writer.WriteCodePoint((int)':');
                        bool pop = CheckCircularRef(stack, obj, value);
                        WriteJSONToInternal(value, writer, options, stack);
                        PopRefIfNeeded(stack, pop);
                        first = false;
                    }
                    writer.WriteCodePoint((int)'}');
                }
                break;
            }

            default:
                throw new InvalidOperationException("Unexpected item" +
                                                    "\u0020type");
            }
        }