Esempio n. 1
0
        public CoralBody(CBORObject node, CoralDictionary dictionary)
        {
            if (node.Type != CBORType.Array)
            {
                throw new ArgumentException("Invalid node type");
            }

            foreach (CBORObject child in node.Values)
            {
                if (node.Type != CBORType.Array)
                {
                    throw new ArgumentException("Invalid node type");
                }

                switch (node[0].AsInt32())
                {
                case 1:
                    _items.Add(new CoralLink(node, dictionary));
                    break;

                default:
                    throw new ArgumentException("Unrecognized CoRAL node type");
                }
            }
        }
Esempio n. 2
0
        public override CBORObject EncodeToCBORObject(Cori coriBase, CoralDictionary dictionary)
        {
            CBORObject node = CBORObject.NewArray();

            node.Add(3);
            node.Add(dictionary.Lookup(OperationType, false));

            CBORObject o = dictionary.Lookup(Target, false);

            if (o.Type == CBORType.Integer)
            {
                node.Add(o);
            }
            else
            {
                node.Add(Target.MakeRelative(coriBase).Data);
            }

            if (FormFields.Count > 0)
            {
                CBORObject fields = CBORObject.NewArray();
                foreach (CoralFormField f in FormFields)
                {
                    f.EncodeToCBOR(fields, coriBase, dictionary);
                }

                node.Add(fields);
            }

            return(node);
        }
Esempio n. 3
0
        public override CBORObject EncodeToCBORObject(CoralDictionary dictionary)
        {
            CBORObject node = CBORObject.NewArray();

            node.Add(1);
            node.Add(Ciri.ToCbor(Uri));

            return(node);
        }
Esempio n. 4
0
        /// <summary>
        /// Encode a CoRAL document to a binary value
        /// </summary>
        /// <param name="contextCori">Context to evaluate the document relative to</param>
        /// <param name="dictionary">Dictionary to be used for compression</param>
        /// <returns></returns>
        public byte[] EncodeToBytes(Cori contextCori, CoralDictionary dictionary = null)
        {
            if (dictionary == null)
            {
                dictionary = CoralDictionary.Default;
            }

            return(EncodeToCBORObject(contextCori, dictionary).EncodeToBytes());
        }
Esempio n. 5
0
        static CoralBody DecodeFromBytes(byte[] encoded, CoralDictionary dictionary = null)
        {
            CBORObject obj = CBORObject.DecodeFromBytes(encoded);

            if (dictionary == null)
            {
                dictionary = CoralDictionary.Default;
            }
            return(new CoralBody(obj, dictionary));
        }
Esempio n. 6
0
        // link = [2, relation-type, link-target, ?body]
        // relation-type = text
        // link-target = CoRI / literal
        // CoRI = <Defined in Section X of RFC XXXX>
        // literal = bool / int / float / time / bytes / text / null

        public override CBORObject EncodeToCBORObject(Cori coriBase, CoralDictionary dictionary)
        {
            CBORObject node = CBORObject.NewArray();

            node.Add(2);
            if (RelationType != null)
            {
                node.Add(dictionary.Lookup(RelationType, false));
            }
            else
            {
                node.Add(RelationTypeInt);
            }


            if (Target != null)
            {
                CBORObject o = dictionary.Lookup(Target, true);
                if (o.Type == CBORType.Integer)
                {
                    node.Add(o);
                }
                else
                {
                    if (coriBase != null)
                    {
                        Cori relative = Target.MakeRelative(coriBase);
                        node.Add(relative.Data);
                    }
                    else
                    {
                        node.Add(Target.Data);
                    }
                }
            }
            else if (Value != null)
            {
                node.Add(dictionary.Lookup(Value, true));
            }
            else if (TargetInt != null)
            {
                node.Add(CBORObject.FromObjectAndTag(TargetInt, CoralDictionary.DictionaryTag));
            }

            if (Body != null)
            {
                node.Add(Body.EncodeToCBORObject(coriBase, dictionary));
            }

            return(node);
        }
Esempio n. 7
0
        public override CBORObject EncodeToCBORObject(CoralDictionary dictionary)
        {
            CBORObject node = CBORObject.NewArray();

            node.Add(2);
            node.Add(dictionary.Lookup(RelationType));
            node.Add(dictionary.Lookup(Target));
            if (Body != null)
            {
                node.Add(Body.EncodeToCBORObject(dictionary));
            }

            return(node);
        }
Esempio n. 8
0
        public override CBORObject EncodeToCBORObject(CoralDictionary dictionary)
        {
            CBORObject node = CBORObject.NewArray();

            node.Add(3);
            node.Add(dictionary.Lookup(_operationType));
            node.Add(dictionary.Lookup(_target));
            if (Body.Length > 0)
            {
                node.Add(Body.EncodeToCBORObject(dictionary));
            }

            return(node);
        }
Esempio n. 9
0
        public CoralLink(CBORObject node, CoralDictionary dictionary)
        {
            if (node[0].AsInt32() != 2)
            {
                throw new ArgumentException("Not an encoded CoRAL link");
            }

            RelationType = dictionary.Reverse(node[1]).AsString();
            Target       = dictionary.Reverse(node[2]);
            if (node.Count == 4)
            {
                Body = new CoralBody(node[3], dictionary);
            }
        }
Esempio n. 10
0
        public CoralFormField(CBORObject type, CBORObject value, Cori baseCori, CoralDictionary dictionary)
        {
            CBORObject o = (CBORObject)dictionary.Reverse(type, false);

            if (o == null)
            {
                FieldTypeInt = type.AsInt32();
            }
            else if (o.Type == CBORType.Array)
            {
                FieldType = new Cori(o);
                if (type.Type == CBORType.Integer)
                {
                    FieldTypeInt = type.AsInt32();
                }
            }
            else
            {
                throw new ArgumentException("Not a valid form field type");
            }

            o = (CBORObject)dictionary.Reverse(value, true);

            if (o == null)
            {
                LiteralInt = value.AsInt32();
            }
            else if (o.Type == CBORType.Array)
            {
                Url = new Cori(o);
                if (baseCori != null)
                {
                    Url = Url.ResolveTo(baseCori);
                }

                if (value.Type == CBORType.Integer)
                {
                    LiteralInt = value.Untag().AsInt32();
                }
            }
            else
            {
                Literal = o;
                if (value.IsTagged && value.HasOneTag(CoralDictionary.DictionaryTag) && value.Type == CBORType.Integer)
                {
                    LiteralInt = value.Untag().AsInt32();
                }
            }
        }
Esempio n. 11
0
        public CBORObject EncodeToCBORObject(CoralDictionary dictionary = null)
        {
            CBORObject root = CBORObject.NewArray();

            if (dictionary == null)
            {
                dictionary = CoralDictionary.Default;
            }

            foreach (CoralItem item in _items)
            {
                root.Add(item.EncodeToCBORObject(dictionary));
            }

            return(root);
        }
        /// <inheritdoc />
        public override CBORObject EncodeToCBORObject(Cori baseCori, CoralDictionary dictionary)
        {
            CBORObject result = CBORObject.NewArray();

            result.Add(1);
            if (baseCori != null)
            {
                Cori relative = BaseValue.MakeRelative(baseCori);
                result.Add(relative.Data);
            }
            else
            {
                result.Add(BaseValue.Data);
            }

            return(result);
        }
Esempio n. 13
0
        /// <summary>
        /// Decode a CBOR object into a coral body.
        /// </summary>
        /// <param name="node">CBOR node to be decoded</param>
        /// <param name="contextCori">context to decode URIs</param>
        /// <param name="dictionary">Dictionary used for decompression</param>
        public CoralBody(CBORObject node, Cori contextCori, CoralDictionary dictionary)
        {
            Cori baseCori = contextCori;

            if (node.Type != CBORType.Array)
            {
                throw new ArgumentException("Invalid node type");
            }

            if (contextCori == null || !contextCori.IsAbsolute())
            {
                throw new ArgumentException("Must be resolved to an absolute URI", nameof(contextCori));
            }

            foreach (CBORObject child in node.Values)
            {
                if (child.Type != CBORType.Array)
                {
                    throw new ArgumentException("Invalid node type");
                }

                switch (child[0].AsInt32())
                {
                case 1:
                    CoralBaseDirective d1 = new CoralBaseDirective(child, contextCori);
                    _items.Add(d1);
                    baseCori = d1.BaseValue;
                    break;

                case 2:
                    _items.Add(new CoralLink(child, baseCori, dictionary));
                    break;

                case 3:
                    _items.Add(new CoralForm(child, baseCori, dictionary));
                    break;

                default:
                    throw new ArgumentException("Unrecognized CoRAL node type");
                }
            }
        }
Esempio n. 14
0
        public CBORObject EncodeToCBORObject(Cori coriBase, CoralDictionary dictionary = null)
        {
            CBORObject root = CBORObject.NewArray();

            if (dictionary == null)
            {
                dictionary = CoralDictionary.Default;
            }

            foreach (CoralItem item in _items)
            {
                root.Add(item.EncodeToCBORObject(coriBase, dictionary));
                if (item is CoralBaseDirective d)
                {
                    coriBase = d.BaseValue.ResolveTo(coriBase);
                }
            }

            return(root);
        }
Esempio n. 15
0
        public CBORObject EncodeToCBOR(CBORObject fieldArray, Cori baseCori, CoralDictionary dictionary)
        {
            fieldArray.Add(dictionary.Lookup(FieldType, false));

            if (Literal != null)
            {
                fieldArray.Add(dictionary.Lookup(Literal, true));
            }
            else
            {
                CBORObject x = dictionary.Lookup(Url, true);
                if (x.Type == CBORType.Integer)
                {
                    fieldArray.Add(x);
                }
                else
                {
                    fieldArray.Add(Url.MakeRelative(baseCori).Data);
                }
            }

            return(fieldArray);
        }
Esempio n. 16
0
 public byte[] EncodeToBytes(CoralDictionary dictionary = null)
 {
     return(EncodeToCBORObject(dictionary).EncodeToBytes());
 }
Esempio n. 17
0
        public CoralForm(CBORObject form, Cori baseCori, CoralDictionary dictionary)
        {
            if (form.Type != CBORType.Array && !(form.Count == 3 || form.Count == 4))
            {
                throw new ArgumentException("Invalid form descriptor", nameof(form));
            }

            if (baseCori == null || !baseCori.IsAbsolute())
            {
                throw new ArgumentException("Invalid base reference", nameof(baseCori));
            }

            if (form[0].Type != CBORType.Integer || form[0].AsInt32() != 3)
            {
                throw new ArgumentException("Not a CoRAL form descriptor", nameof(form));
            }

            CBORObject o = (CBORObject)dictionary.Reverse(form[1], false);

            if (o == null)
            {
                OperationTypeInt = form[1].Untag().AsInt32();
            }
            else if (o.Type == CBORType.Array)
            {
                OperationType = new Cori(o);
                if (form[1].Type == CBORType.Integer)
                {
                    OperationTypeInt = form[1].Untag().AsInt32();
                }
            }
            else
            {
                throw new ArgumentException("Invalid operation in CoRAL form");
            }

            o = (CBORObject)dictionary.Reverse(form[2], true);
            if (o == null)
            {
                TargetInt = form[2].Untag().AsInt32();
            }
            else if (o.Type != CBORType.Array)
            {
                throw new ArgumentException("Invalid submission target", nameof(form));
            }
            else
            {
                Target = new Cori(o);
                if (form[2].Type == CBORType.Integer &&
                    form[2].HasTag(CoralDictionary.DictionaryTag))
                {
                    TargetInt = form[2].Untag().AsInt32();
                }

                Target = Target.ResolveTo(baseCori);
            }

            if (form.Count == 4)
            {
                if (form[3].Type != CBORType.Array)
                {
                    throw new ArgumentException("Invalid form field array", nameof(form));
                }

                for (int i = 0; i < form[3].Count; i += 2)
                {
                    FormFields.Add(new CoralFormField(form[3][i], form[3][i + 1], baseCori, dictionary));
                }
            }
        }
Esempio n. 18
0
 /// <summary>
 /// Create a new CoRAL document based on a parsed CBOR object
 /// </summary>
 /// <param name="node">Encoded document to use</param>
 /// <param name="contextCori">Context to evaluate the document relative to</param>
 /// <param name="dictionary">Dictionary to be used to reverse compression</param>
 public CoralDocument(CBORObject node, Cori contextCori, CoralDictionary dictionary) : base(node, contextCori, dictionary)
 {
 }
Esempio n. 19
0
 public abstract CBORObject EncodeToCBORObject(Cori baseCori, CoralDictionary dictionary);
Esempio n. 20
0
        /// <summary>
        /// Decode a CBOR object and create a CoRAL document from it
        /// </summary>
        /// <param name="encoded">CBOR encoded CoRAL document</param>
        /// <param name="contextCori">Context to evaluate the document relative to</param>
        /// <param name="dictionary">Dictionary to be used to reverse compression</param>
        /// <returns></returns>
        public static CoralDocument DecodeFromBytes(byte[] encoded, Cori contextCori, CoralDictionary dictionary = null)
        {
            CBORObject obj = CBORObject.DecodeFromBytes(encoded);

            return(DecodeFromCbor(obj, contextCori, dictionary));
        }
Esempio n. 21
0
 /// <summary>
 /// Create a new CoRAL document based on a parsed CBOR object
 /// </summary>
 /// <param name="node">Encoded document to use</param>
 /// <param name="contextCori">Context to evaluate the document relative to</param>
 /// <param name="dictionary">Dictionary to be used to reverse compression</param>
 /// <returns></returns>
 public static CoralDocument DecodeFromCbor(CBORObject node, Cori contextCori, CoralDictionary dictionary = null)
 {
     if (dictionary == null)
     {
         dictionary = CoralDictionary.Default;
     }
     return(new CoralDocument(node, contextCori, dictionary));
 }
Esempio n. 22
0
        /// <summary>
        /// Decode a CBOR encoded CoRAL link into the object for working with
        /// </summary>
        /// <param name="node">CBOR object to be decoded</param>
        /// <param name="baseCori">a URI to make things relative to</param>
        /// <param name="dictionary">dictionary to use for decoding</param>
        public CoralLink(CBORObject node, Cori baseCori, CoralDictionary dictionary)
        {
            if (node[0].AsInt32() != 2)
            {
                throw new ArgumentException("Not an encoded CoRAL link");
            }

            if (dictionary == null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            CBORObject o = (CBORObject)dictionary.Reverse(node[1], false);

            if (o == null)
            {
                RelationTypeInt = node[1].AsInt32();
            }
            else if (o.Type == CBORType.Array)
            {
                RelationType = new Cori(o);
                if (node[1].Type == CBORType.Integer)
                {
                    RelationTypeInt = node[1].AsInt32();
                }
            }
            else
            {
                throw  new ArgumentException("Invalid relation in CoRAL link");
            }

            CBORObject value = (CBORObject)dictionary.Reverse(node[2], true);

            if (value == null)
            {
                if (!node[2].HasOneTag(CoralDictionary.DictionaryTag))
                {
                    throw new ArgumentException("Invalid tagging on value");
                }
                TargetInt = node[2].Untag().AsInt32();
            }
            else if (value.Type == CBORType.Array)
            {
                Target = new Cori(value).ResolveTo(baseCori);
                if (node[2].Type == CBORType.Integer)
                {
                    TargetInt = node[2].Untag().AsInt32();
                }

                baseCori = Target;
            }
            else
            {
                if (node[2].IsTagged && node[2].Type == CBORType.Integer)
                {
                    TargetInt = node[2].Untag().AsInt32();
                }
                Value = value;
            }

            if (node.Count == 4)
            {
                Body = new CoralBody(node[3], baseCori, dictionary);
            }
        }