Ejemplo n.º 1
0
 public CBORObject ResolveSharedRefsIfNeeded(CBORObject obj)
 {
     if (this.hasSharableObjects)
     {
         var sharedRefs = new SharedRefs();
         return(ResolveSharedRefs(obj, sharedRefs));
     }
     return(obj);
 }
Ejemplo n.º 2
0
        public CBORObject Read()
        {
            CBORObject obj = this.options.AllowEmpty ?
                             this.ReadInternalOrEOF() : this.ReadInternal();

            if (this.options.ResolveReferences && this.hasSharableObjects)
            {
                var sharedRefs = new SharedRefs();
                return(ResolveSharedRefs(obj, sharedRefs));
            }
            return(obj);
        }
Ejemplo n.º 3
0
        private static CBORObject ResolveSharedRefs(
            CBORObject obj,
            SharedRefs sharedRefs)
        {
            if (obj == null)
            {
                return(null);
            }
            CBORType type   = obj.Type;
            bool     hasTag = obj.HasMostOuterTag(29);

            if (hasTag)
            {
                CBORObject untagged = obj.UntagOne();
                if (untagged.IsTagged ||
                    untagged.Type != CBORType.Integer ||
                    untagged.AsNumber().IsNegative())
                {
                    throw new CBORException(
                              "Shared ref index must be an untagged integer 0 or greater");
                }
                return(sharedRefs.GetObject(untagged.AsEIntegerValue()));
            }
            hasTag = obj.HasMostOuterTag(28);
            if (hasTag)
            {
                obj = obj.UntagOne();
                sharedRefs.AddObject(obj);
            }
            if (type == CBORType.Map)
            {
                foreach (CBORObject key in obj.Keys)
                {
                    CBORObject value    = obj[key];
                    CBORObject newvalue = ResolveSharedRefs(value, sharedRefs);
                    if (value != newvalue)
                    {
                        obj[key] = newvalue;
                    }
                }
            }
            else if (type == CBORType.Array)
            {
                for (var i = 0; i < obj.Count; ++i)
                {
                    obj[i] = ResolveSharedRefs(obj[i], sharedRefs);
                }
            }
            return(obj);
        }
Ejemplo n.º 4
0
        private static CBORObject ResolveSharedRefs(
            CBORObject obj,
            SharedRefs sharedRefs)
        {
            int  type   = obj.ItemType;
            bool hasTag = obj.MostOuterTag.Equals((EInteger)29);

            if (hasTag)
            {
                return(sharedRefs.GetObject(obj.AsEInteger()));
            }
            hasTag = obj.MostOuterTag.Equals((EInteger)28);
            if (hasTag)
            {
                obj = obj.Untag();
                sharedRefs.AddObject(obj);
            }
            if (type == CBORObject.CBORObjectTypeMap)
            {
                foreach (CBORObject key in obj.Keys)
                {
                    CBORObject value    = obj[key];
                    CBORObject newvalue = ResolveSharedRefs(value, sharedRefs);
                    if (value != newvalue)
                    {
                        obj[key] = newvalue;
                    }
                }
            }
            else if (type == CBORObject.CBORObjectTypeArray)
            {
                for (var i = 0; i < obj.Count; ++i)
                {
                    obj[i] = ResolveSharedRefs(obj[i], sharedRefs);
                }
            }
            return(obj);
        }
Ejemplo n.º 5
0
 public CBORReader(Stream inStream)
 {
     this.stream = inStream;
       this.sharedRefs = new SharedRefs();
       this.policy = CBORDuplicatePolicy.Overwrite;
 }