Example #1
0
        void Read(CodedInputStream stream)
        {
            var  links = new List <DagLink>();
            bool done  = false;

            while (!stream.IsAtEnd && !done)
            {
                var tag = stream.ReadTag();
                switch (WireFormat.GetTagFieldNumber(tag))
                {
                case 1:
                    DataBytes = stream.ReadSomeBytes(stream.ReadLength());
                    done      = true;
                    break;

                case 2:
                    using (var ms = new MemoryStream(stream.ReadSomeBytes(stream.ReadLength())))
                    {
                        links.Add(new DagLink(ms));
                    }
                    break;

                default:
                    throw new InvalidDataException("Unknown field number");
                }
            }

            if (DataBytes == null)
            {
                DataBytes = new byte[0];
            }
            Links = links.ToArray();
        }
        /// <summary>
        /// Reads an unknown field, either parsing it and storing it or skipping it.
        /// </summary>
        /// <remarks>
        /// If the current set of options is empty and we manage to read a field, a new set of options
        /// will be created and returned. Otherwise, the return value is <c>this</c>. This allows
        /// us to start with a singleton empty set of options and just create new ones where necessary.
        /// </remarks>
        /// <param name="input">Input stream to read from. </param>
        /// <returns>The resulting set of custom options, either <c>this</c> or a new set.</returns>
        internal CustomOptions ReadOrSkipUnknownField(CodedInputStream input)
        {
            var tag   = input.LastTag;
            var field = WireFormat.GetTagFieldNumber(tag);

            switch (WireFormat.GetTagWireType(tag))
            {
            case WireFormat.WireType.LengthDelimited:
                return(AddValue(field, new FieldValue(input.ReadBytes())));

            case WireFormat.WireType.Fixed32:
                return(AddValue(field, new FieldValue(input.ReadFixed32())));

            case WireFormat.WireType.Fixed64:
                return(AddValue(field, new FieldValue(input.ReadFixed64())));

            case WireFormat.WireType.Varint:
                return(AddValue(field, new FieldValue(input.ReadRawVarint64())));

            // For StartGroup, EndGroup or any wire format we don't understand,
            // just use the normal behavior (call SkipLastField).
            default:
                input.SkipLastField();
                return(this);
            }
        }
Example #3
0
        private static ValueType HandleValueType(CodedInputStream cis)
        {
            var valueType = new ValueType();

            long pos = cis.ReadLength();

            pos += cis.Position;
            while (pos - cis.Position > 0)
            {
                var tag = cis.ReadTag();

                if (tag > 0)
                {
                    var fieldNumber = WireFormat.GetTagFieldNumber(tag);

                    switch (fieldNumber)
                    {
                    case 1:
                        valueType.Type = cis.ReadInt64();
                        break;

                    case 2:
                        valueType.Unit = cis.ReadInt64();
                        break;

                    default:
                        cis.SkipLastField();
                        break;
                    }
                }
            }

            return(valueType);
        }
        void Read(CodedInputStream stream)
        {
            while (!stream.IsAtEnd)
            {
                var tag = stream.ReadTag();
                switch (WireFormat.GetTagFieldNumber(tag))
                {
                case 1:
                    using (var ms = new MemoryStream(stream.ReadSomeBytes(stream.ReadLength())))
                    {
                        Hash = new MultiHash(ms).ToBase58();
                    }
                    break;

                case 2:
                    Name = stream.ReadString();
                    break;

                case 3:
                    Size = stream.ReadInt64();
                    break;

                default:
                    throw new InvalidDataException("Unknown field number");
                }
            }
        }
Example #5
0
        private static Location HandleLocation(CodedInputStream cis)
        {
            var location = new Location();

            var lines = new List <Line>();

            location.Lines = lines;

            long pos = cis.ReadLength();

            pos += cis.Position;
            while (pos - cis.Position > 0)
            {
                var tag = cis.ReadTag();
                if (tag > 0)
                {
                    var fieldNumber = WireFormat.GetTagFieldNumber(tag);

                    switch (fieldNumber)
                    {
                    case 1:
                        location.Id = cis.ReadUInt64();
                        break;

                    case 2:
                        location.MappingId = cis.ReadUInt64();
                        break;

                    case 3:
                        location.Address = cis.ReadUInt64();
                        break;

                    case 4:
                        lines.Add(HandleLine(cis));
                        break;

                    case 5:
                        location.IsFolded = cis.ReadInt32() != 0;
                        break;

                    default:
                        cis.SkipLastField();
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return(location);
        }
Example #6
0
        private static Label HandleLabel(CodedInputStream cis)
        {
            var label = new Label();

            long pos = cis.ReadLength();

            pos += cis.Position;
            while (pos - cis.Position > 0)
            {
                var tag = cis.ReadTag();
                if (tag > 0)
                {
                    var fieldNumber = WireFormat.GetTagFieldNumber(tag);

                    switch (fieldNumber)
                    {
                    case 1:
                        label.Key = cis.ReadInt64();
                        break;

                    case 2:
                        label.Str = cis.ReadInt64();
                        break;

                    case 3:
                        label.Num = cis.ReadInt64();
                        break;

                    case 4:
                        label.NumUnit = cis.ReadInt64();
                        break;

                    default:
                        cis.SkipLastField();
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return(label);
        }
Example #7
0
        public static uint ReadProtoInt(byte[] buffer, int tags)
        {
            uint             tag       = 0;
            string           fieldName = "";
            CodedInputStream input     = CodedInputStream.CreateInstance(buffer);

            while (input.ReadTag(out tag, out fieldName))
            {
                int Field = WireFormat.GetTagFieldNumber(tag);

                WireFormat.WireType WireType = WireFormat.GetTagWireType(tag);


                switch (WireType)
                {
                case WireFormat.WireType.LengthDelimited:
                    ByteString tmps = ByteString.Empty;

                    input.ReadBytes(ref tmps);

                    break;

                case WireFormat.WireType.Fixed32:
                    break;

                case WireFormat.WireType.Fixed64:
                    break;

                case WireFormat.WireType.Varint:
                    uint val = 0;
                    input.ReadUInt32(ref val);

                    if (tags == tag >> 3)
                    {
                        return(val);
                    }
                    break;
                }
            }
            return(0);
        }
        /// <inheritdoc/>
        public T FromProtobuf <T>(Stream stream, bool includesLength = false)
        {
            var instance = (T)Activator.CreateInstance(typeof(T));

            using (var inputStream = new CodedInputStream(stream))
            {
                var messageDescription = _messageDescriptions.GetFor <T>();

                var tag = inputStream.ReadTag();
                while (!inputStream.IsAtEnd)
                {
                    var fieldNumber         = WireFormat.GetTagFieldNumber(tag);
                    var propertyDescription = messageDescription.Properties.SingleOrDefault(_ => _.Number == fieldNumber);
                    if (propertyDescription != null)
                    {
                        object value = null;
                        var    type  = propertyDescription.Property.PropertyType;

                        IValueConverter converter = null;

                        var targetType = type;

                        if (_valueConverters.CanConvert(type))
                        {
                            converter  = _valueConverters.GetConverterFor(type);
                            targetType = type;
                            type       = converter.SerializedAs(type);
                        }

                        value = ReadValue(inputStream, value, type, targetType, converter);
                        propertyDescription.Property.SetValue(instance, value);
                    }

                    tag = inputStream.ReadTag();
                }
            }

            return(instance);
        }
Example #9
0
        public static object[] Unpack(byte[] bytes, Type[] types)
        {
            if (bytes.Length == 0)
            {
                return new object[] { }
            }
            ;
            if (types.Length * bytes.Length == 0)
            {
                throw new Exception("Invalid input.");
            }

            var objs = new object[types.Length];

            using (CodedInputStream stream = new CodedInputStream(bytes))
            {
                for (int i = 0; i < types.Length; i++)
                {
                    var tag         = stream.ReadTag();
                    var fieldNumber = WireFormat.GetTagFieldNumber(tag);
                    var index       = fieldNumber - 1;
                    if (index < i || index > types.Length)
                    {
                        throw new Exception("Invalid input. Wrong parameter order or wrong number of parameters.");
                    }

                    while (i < index)
                    {
                        objs[i] = types[i].GetDefault();
                        i++;
                    }

                    objs[i] = types[i].ReadFromStream(stream);
                }
            }

            return(objs);
        }
    }
Example #10
0
        private static Line HandleLine(CodedInputStream cis)
        {
            var line = new Line();

            long pos = cis.ReadLength();

            pos += cis.Position;
            while (pos - cis.Position > 0)
            {
                var tag = cis.ReadTag();
                if (tag > 0)
                {
                    var fieldNumber = WireFormat.GetTagFieldNumber(tag);

                    switch (fieldNumber)
                    {
                    case 1:
                        line.FunctionId = cis.ReadUInt64();
                        break;

                    case 2:
                        line.LineNumber = cis.ReadInt64();
                        break;

                    default:
                        cis.SkipLastField();
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return(line);
        }
Example #11
0
        public static void ReadProtoRawDataS(List <object> list, byte[] buffer, int tags)
        {
            uint             tag       = 0;
            string           fieldName = "";
            CodedInputStream input     = CodedInputStream.CreateInstance(buffer);

            while (input.ReadTag(out tag, out fieldName))
            {
                int Field = WireFormat.GetTagFieldNumber(tag);
                WireFormat.WireType WireType = WireFormat.GetTagWireType(tag);
                switch (WireType)
                {
                case WireFormat.WireType.LengthDelimited:
                    ByteString tmps = ByteString.Empty;

                    input.ReadBytes(ref tmps);
                    if (tags == tag >> 3)
                    {
                        //return tmps.ToByteArray();
                        list.Add(tmps.ToByteArray());
                    }
                    break;

                case WireFormat.WireType.Fixed32:
                    break;

                case WireFormat.WireType.Fixed64:
                    break;

                case WireFormat.WireType.Varint:
                    int val = 0;
                    input.ReadInt32(ref val);
                    break;
                }
            }
        }
        void Read(CodedInputStream stream)
        {
            while (!stream.IsAtEnd)
            {
                var tag = stream.ReadTag();
                switch (WireFormat.GetTagFieldNumber(tag))
                {
                case 1:
                    Id = Cid.Read(stream);
                    break;

                case 2:
                    Name = stream.ReadString();
                    break;

                case 3:
                    Size = stream.ReadInt64();
                    break;

                default:
                    throw new InvalidDataException("Unknown field number");
                }
            }
        }
Example #13
0
        public static void Main(string[] args)
        {
            var profile = new Profile();

            RomanNumeral  y = new RomanNumeral();
            BinaryNumeral x = y;

            using (var fs = new FileStream(@"C:\users\muks\desktop\perf.pb", FileMode.Open, FileAccess.Read))
            {
                using (var cis = new CodedInputStream(fs))
                {
                    var valueTypeList = new List <ValueType>();
                    var samplesList   = new List <Sample>();
                    var mappingList   = new List <Mapping>();
                    var locationList  = new List <Location>();
                    var stringTable   = new List <string>();

                    profile.SampleTypes = valueTypeList;
                    profile.Samples     = samplesList;
                    profile.Locations   = locationList;
                    profile.StringTable = stringTable;
                    profile.Mappings    = mappingList;

                    while (true)
                    {
                        var tag = cis.ReadTag();
                        if (tag > 0)
                        {
                            var fieldNumber = WireFormat.GetTagFieldNumber(tag);

                            switch (fieldNumber)
                            {
                            case 1:
                                valueTypeList.Add(HandleValueType(cis));
                                break;

                            case 2:
                                samplesList.Add(HandleSample(cis));
                                break;

                            case 3:
                                mappingList.Add(HandleMapping(cis));
                                break;

                            case 4:
                                locationList.Add(HandleLocation(cis));
                                break;

                            case 5:
                                cis.SkipLastField();
                                break;

                            case 6:
                                var strLength = cis.ReadLength();
                                stringTable.Add(System.Text.Encoding.UTF8.GetString(cis.ReadRawBytes(strLength), 0, strLength));
                                break;

                            default:
                                cis.SkipLastField();
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            for (int i = 0; i < profile.Mappings.Count; ++i)
            {
                var mapping = profile.Mappings[i];

                var filename   = profile.StringTable[(int)mapping.FileNameIndex];
                var rangeBegin = mapping.MemoryStart;
                var rangeEnd   = mapping.MemoryLimit;

                Console.WriteLine($"Filename: {filename}, Range Start: {rangeBegin}, Rang End: {rangeEnd}");
            }
        }
Example #14
0
        private static Sample HandleSample(CodedInputStream cis)
        {
            var sample = new Sample();
            var lables = new List <Label>();

            sample.Labels = lables;

            var locationIds = new List <ulong>();

            sample.LocationIds = locationIds;

            var values = new List <long>();

            sample.Values = values;

            long pos = cis.ReadLength();

            pos += cis.Position;
            while (pos - cis.Position > 0)
            {
                var tag = cis.ReadTag();
                if (tag > 0)
                {
                    var fieldNumber = WireFormat.GetTagFieldNumber(tag);

                    switch (fieldNumber)
                    {
                    case 1:
                    {
                        long posLocations = cis.ReadLength();
                        posLocations += cis.Position;
                        while (posLocations - cis.Position > 0)
                        {
                            locationIds.Add(cis.ReadUInt64());
                        }

                        break;
                    }

                    case 2:
                    {
                        long posValues = cis.ReadLength();
                        posValues += cis.Position;
                        while (posValues - cis.Position > 0)
                        {
                            values.Add(cis.ReadInt64());
                        }

                        break;
                    }

                    case 3:
                        lables.Add(HandleLabel(cis));
                        break;

                    default:
                        cis.SkipLastField();
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return(sample);
        }
Example #15
0
        private static Mapping HandleMapping(CodedInputStream cis)
        {
            var mapping = new Mapping();

            long pos = cis.ReadLength();

            pos += cis.Position;
            while (pos - cis.Position > 0)
            {
                var tag = cis.ReadTag();
                if (tag > 0)
                {
                    var fieldNumber = WireFormat.GetTagFieldNumber(tag);

                    switch (fieldNumber)
                    {
                    case 1:
                        mapping.Id = cis.ReadUInt64();
                        break;

                    case 2:
                        mapping.MemoryStart = cis.ReadUInt64();
                        break;

                    case 3:
                        mapping.MemoryLimit = cis.ReadUInt64();
                        break;

                    case 4:
                        mapping.FileOffset = cis.ReadUInt64();
                        break;

                    case 5:
                        mapping.FileNameIndex = cis.ReadInt64();
                        break;

                    case 6:
                        mapping.BuildIdIndex = cis.ReadInt64();
                        break;

                    case 7:
                        mapping.HasFunctions = cis.ReadInt32() != 0;
                        break;

                    case 8:
                        mapping.HasFilenames = cis.ReadInt32() != 0;
                        break;

                    case 9:
                        mapping.HasLineNumbers = cis.ReadInt32() != 0;
                        break;

                    case 10:
                        mapping.HasInlineFrames = cis.ReadInt32() != 0;
                        break;

                    default:
                        cis.SkipLastField();
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return(mapping);
        }