Esempio n. 1
0
        public void Value(RawProtobufValue value)
        {
            this.WriteHeader(this.FieldIndex, value.Storage);

            switch (value.Storage)
            {
            case RawProtobufWireType.Fixed32:
                this.buffer.WriteByte((byte)((value.Number >> 0) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 8) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 16) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 24) & 255));

                break;

            case RawProtobufWireType.Fixed64:
                this.buffer.WriteByte((byte)((value.Number >> 0) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 8) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 16) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 24) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 32) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 40) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 48) & 255));
                this.buffer.WriteByte((byte)((value.Number >> 56) & 255));

                break;

            case RawProtobufWireType.String:
                var buffer = Encoding.UTF8.GetBytes(value.String);

                this.WriteVarInt(buffer.Length);

                this.buffer.Write(buffer, 0, buffer.Length);

                break;

            case RawProtobufWireType.VarInt:
                // See: https://developers.google.com/protocol-buffers/docs/encoding
                var number = this.noZigZagEncoding ? value.Number : (value.Number << 1) ^ (value.Number >> 31);

                this.WriteVarInt(number);

                break;
            }
        }
Esempio n. 2
0
        public bool TryReadValue(out RawProtobufValue value)
        {
            switch (this.fieldType.GetValueOrDefault())
            {
            case RawProtobufWireType.Fixed32:
                var fixed32 = 0;

                fixed32 += this.stream.ReadByte();
                fixed32 += this.stream.ReadByte() << 8;
                fixed32 += this.stream.ReadByte() << 16;
                fixed32 += this.stream.ReadByte() << 24;

                value = new RawProtobufValue(fixed32, RawProtobufWireType.Fixed32);

                this.position += 4;

                return(true);

            case RawProtobufWireType.Fixed64:
                var fixed64 = 0L;

                fixed64 += this.stream.ReadByte();
                fixed64 += (long)this.stream.ReadByte() << 8;
                fixed64 += (long)this.stream.ReadByte() << 16;
                fixed64 += (long)this.stream.ReadByte() << 24;
                fixed64 += (long)this.stream.ReadByte() << 32;
                fixed64 += (long)this.stream.ReadByte() << 40;
                fixed64 += (long)this.stream.ReadByte() << 48;
                fixed64 += (long)this.stream.ReadByte() << 56;

                this.position += 8;

                value = new RawProtobufValue(fixed64, RawProtobufWireType.Fixed64);

                return(true);

            case RawProtobufWireType.String:
                var length = (int)this.ReadVarInt();

                if (length > ReaderState.StringMaxLength)
                {
                    this.Error($"string field exceeds maximum length of {ReaderState.StringMaxLength}");

                    value = default;

                    return(false);
                }

                var buffer = new byte[length];

                if (this.stream.Read(buffer, 0, length) != length)
                {
                    this.Error($"could not read string of length {length}");

                    value = default;

                    return(false);
                }

                this.position += length;

                value = new RawProtobufValue(Encoding.UTF8.GetString(buffer), RawProtobufWireType.String);

                return(true);

            case RawProtobufWireType.VarInt:
                var varint = this.ReadVarInt();

                // See: https://developers.google.com/protocol-buffers/docs/encoding
                var number = this.noZigZagEncoding ? varint : -(varint & 1) ^ (varint >> 1);

                value = new RawProtobufValue(number, RawProtobufWireType.VarInt);

                return(true);

            default:
                this.Error($"unsupported wire type {this.fieldType}");

                value = default;

                return(false);
            }
        }