private ulong ReadRawVarint64()
        {
            int   shift  = 0;
            ulong result = 0;

            while (shift < 64)
            {
                byte b = ReadByteFromStream();
                result |= (ulong)(b & 0x7F) << shift;
                if ((b & 0x80) == 0)
                {
                    return(result);
                }
                shift += 7;
            }

            throw InvalidProtocolBufferException.MalformedVarint();
        }
        private uint ReadRawVarint32(bool zeroIfEndOfStream = false)
        {
            int tmp;

            if (zeroIfEndOfStream)
            {
                tmp = stream.ReadByte();

                if (tmp == -1)
                {
                    return(0);
                }
            }
            else
            {
                tmp = ReadByteFromStream();
            }

            if (tmp < 128)
            {
                return((uint)tmp);
            }

            int result = tmp & 0x7f;

            if ((tmp = ReadByteFromStream()) < 128)
            {
                result |= tmp << 7;
            }
            else
            {
                result |= (tmp & 0x7f) << 7;
                if ((tmp = ReadByteFromStream()) < 128)
                {
                    result |= tmp << 14;
                }
                else
                {
                    result |= (tmp & 0x7f) << 14;
                    if ((tmp = ReadByteFromStream()) < 128)
                    {
                        result |= tmp << 21;
                    }
                    else
                    {
                        result |= (tmp & 0x7f) << 21;
                        result |= (tmp = ReadByteFromStream()) << 28;
                        if (tmp >= 128)
                        {
                            // Discard upper 32 bits
                            for (int i = 0; i < 5; i++)
                            {
                                if (ReadByteFromStream() < 128)
                                {
                                    return((uint)result);
                                }
                            }

                            throw InvalidProtocolBufferException.MalformedVarint();
                        }
                    }
                }
            }
            return((uint)result);
        }
        public void SkipField(WireType wireType)
        {
            switch (wireType)
            {
            case WireType.Varint:
                int tmp;
                int cnt = 0;
                while ((tmp = ReadByteFromStream()) >= 128)
                {
                    cnt++;

                    if (cnt == 10)
                    {
                        throw InvalidProtocolBufferException.MalformedVarint();
                    }
                }

                break;

            case WireType.Fixed64:
                ReadFromStream(buffForSeek, 0, 8);

                break;

            case WireType.LengthDelimited:
                var len = ReadLength();
                if (len <= buffForSeek.Length)
                {
                    ReadFromStream(buffForSeek, 0, len);
                }
                else
                {
                    ReadFromStream(new byte[len], 0, len);
                }

                break;

            case WireType.StartGroup:
                recursionDepth++;
                uint     tag;
                WireType wt;
                do
                {
                    tag = ReadTag();
                    if (tag == 0)
                    {
                        throw InvalidProtocolBufferException.TruncatedMessage();
                    }

                    wt = WireFormat.GetTagWireType(tag);
                    SkipField(wt);
                } while (wt != WireType.EndGroup);
                recursionDepth--;

                break;

            case WireType.EndGroup:
                break;

            case WireType.Fixed32:
                ReadFromStream(buffForSeek, 0, 4);
                break;

            default:
                throw InvalidProtocolBufferException.UnknownWireType();
                break;
            }
        }