Exemplo n.º 1
0
        public byte ReadByte()
        {
            var a = A.ReadByte();
            var b = B.ReadByte();

            Verify(a, b);
            return(a);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses the tick internally
        /// </summary>
        /// <returns><c>true</c>, if tick was parsed, <c>false</c> otherwise.</returns>
        private bool ParseTick()
        {
            DemoCommand command = (DemoCommand)BitStream.ReadByte();

            BitStream.ReadInt(32);          // tick number
            BitStream.ReadByte();           // player slot

            this.CurrentTick++;             // = TickNum;

            switch (command)
            {
            case DemoCommand.Synctick:
                break;

            case DemoCommand.Stop:
                return(false);

            case DemoCommand.ConsoleCommand:
                BitStream.BeginChunk(BitStream.ReadSignedInt(32) * 8);
                BitStream.EndChunk();
                break;

            case DemoCommand.DataTables:
                BitStream.BeginChunk(BitStream.ReadSignedInt(32) * 8);
                SendTableParser.ParsePacket(BitStream);
                BitStream.EndChunk();

                //Map the weapons in the equipmentMapping-Dictionary.
                MapEquipment();

                //And now we have the entities, we can bind events on them.
                BindEntites();

                break;

            case DemoCommand.StringTables:
                BitStream.BeginChunk(BitStream.ReadSignedInt(32) * 8);
                StringTables.ParsePacket(BitStream, this);
                BitStream.EndChunk();
                break;

            case DemoCommand.UserCommand:
                BitStream.ReadInt(32);
                BitStream.BeginChunk(BitStream.ReadSignedInt(32) * 8);
                BitStream.EndChunk();
                break;

            case DemoCommand.Signon:
            case DemoCommand.Packet:
                ParseDemoPacket();
                break;

            default:
                throw new Exception("Can't handle Demo-Command " + command);
            }

            return(true);
        }
Exemplo n.º 3
0
        public static string ReadDataTableString(this IBitStream bs)
        {
            using (var memstream = new MemoryStream()) {
                // not particulary efficient, but probably fine
                for (byte b = bs.ReadByte(); b != 0; b = bs.ReadByte())
                {
                    memstream.WriteByte(b);
                }

                return(Encoding.Default.GetString(memstream.GetBuffer(), 0, checked ((int)memstream.Length)));
            }
        }
        /// <summary>
        /// Returns a string of count length containing characters extracted from the bit stream.
        /// </summary>
        /// <param name="bitStream"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string ExtractCharacters(IBitStream bitStream, int count)
        {
            var result = new StringBuilder();

            for (int i = 0; i < count; ++i)
            {
                var ch        = '\0';
                var encodedCh = bitStream.ReadByte(6);
                if (encodedCh > 0 && encodedCh < 27)
                {
                    ch = (char)('A' + (encodedCh - 1));
                }
                else if (encodedCh == 32)
                {
                    ch = ' ';
                }
                else if (encodedCh > 47 && encodedCh < 58)
                {
                    ch = (char)('0' + (encodedCh - 48));
                }
                if (ch != '\0')
                {
                    result.Append(ch);
                }
            }

            return(result.ToString());
        }
Exemplo n.º 5
0
        public static int ReadProtobufVarIntStub(IBitStream reader)
        {
            byte b      = 0x80;
            int  result = 0;

            for (int count = 0; (b & 0x80) != 0; count++)
            {
                b = reader.ReadByte();

                if ((count < 4) || ((count == 4) && (((b & 0xF8) == 0) || ((b & 0xF8) == 0xF8))))
                {
                    result |= (b & ~0x80) << (7 * count);
                }
                else
                {
                    if (count >= 10)
                    {
                        throw new OverflowException("Nope nope nope nope! 10 bytes max!");
                    }
                    if ((count == 9) ? (b != 1) : ((b & 0x7F) != 0x7F))
                    {
                        throw new NotSupportedException("more than 32 bits are not supported");
                    }
                }
            }

            return(result);
        }
Exemplo n.º 6
0
		public void ParsePacket(IBitStream reader, DemoParser parser)
        {
			int numTables = reader.ReadByte();

			for (int i = 0; i < numTables; i++) {
				string tableName = reader.ReadString();

				ParseStringTable(reader, tableName, parser);
			}
        }
Exemplo n.º 7
0
        public void ParsePacket(IBitStream reader, DemoParser parser)
        {
            int numTables = reader.ReadByte();

            for (int i = 0; i < numTables; i++)
            {
                string tableName = reader.ReadString();

                ParseStringTable(reader, tableName, parser);
            }
        }
Exemplo n.º 8
0
        public static uint Compute32(IBitStream stream)
        {
            uint crc = 0xFFFFFFFF;

            while (!stream.ChunkFinished)
            {
                byte index = (byte)(((crc) & 0xFF) ^ stream.ReadByte());
                crc = (uint)((crc >> 8) ^ table[index]);
            }
            return(~crc);
        }
Exemplo n.º 9
0
        public void TestReadByte()
        {
            int bitOffset = 0;
            int totalBits = data.Length * 8;

            while (bitOffset < totalBits)
            {
                int thisTime = Math.Min(rng.Next(8) + 1, totalBits - bitOffset);
                dbgAll.ReadByte(thisTime);
                bitOffset += thisTime;
            }
        }
        /// <summary>
        /// Returns a string of count length containing characters extracted from the bit stream.
        /// </summary>
        /// <param name="bitStream"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string ExtractCharacters(IBitStream bitStream, int count)
        {
            var result = new StringBuilder();
            for(int i = 0;i < count;++i) {
                var ch = '\0';
                var encodedCh = bitStream.ReadByte(6);
                if(encodedCh > 0 && encodedCh < 27) ch = (char)('A' + (encodedCh - 1));
                else if(encodedCh == 32) ch = ' ';
                else if(encodedCh > 47 && encodedCh < 58) ch = (char)('0' + (encodedCh - 48));
                if(ch != '\0') result.Append(ch);
            }

            return result.ToString();
        }
Exemplo n.º 11
0
        public static string ReadString(this IBitStream bs, int limit)
        {
            var result = new List <byte>(512);

            for (int pos = 0; pos < limit; pos++)
            {
                var b = bs.ReadByte();
                if ((b == 0) || (b == 10))
                {
                    break;
                }
                result.Add(b);
            }
            return(Encoding.ASCII.GetString(result.ToArray()));
        }
Exemplo n.º 12
0
        public static uint ReadVarInt(this IBitStream bs)
        {
            uint tmpByte = 0x80;
            uint result  = 0;

            for (int count = 0; (tmpByte & 0x80) != 0; count++)
            {
                if (count > 5)
                {
                    throw new InvalidDataException("VarInt32 out of range");
                }
                tmpByte = bs.ReadByte();
                result |= (tmpByte & 0x7F) << (7 * count);
            }
            return(result);
        }
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="rawMessage"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public ModeSMessage Translate(byte[] rawMessage, int start)
        {
            ModeSMessage result = null;

            var messageLength = rawMessage == null ? 0 : rawMessage.Length - start;

            if (messageLength > 6)
            {
                _BitStream.Initialise(rawMessage);
                _BitStream.Skip(start * 8);
                var downlinkFormatValue = _BitStream.ReadByte(5);
                if (downlinkFormatValue >= 24)
                {
                    downlinkFormatValue = 24;
                    _BitStream.Skip(-3);
                }

                bool isLongFrame = true;
                switch (downlinkFormatValue)
                {
                case 16:
                case 17:
                case 18:
                case 19:
                case 20:
                case 21:
                case 24:    if (messageLength > 13)
                    {
                        result = new ModeSMessage();
                    }
                    break;

                default:    isLongFrame = false; result = new ModeSMessage(); break;
                }

                if (result != null)
                {
                    result.DownlinkFormat = (DownlinkFormat)downlinkFormatValue;

                    switch (result.DownlinkFormat)
                    {
                    case DownlinkFormat.ShortAirToAirSurveillance:      DecodeShortAirToAirSurveillance(result); break;             // DF0

                    case DownlinkFormat.SurveillanceAltitudeReply:      DecodeSurveillanceAltitudeReply(result); break;             // DF4

                    case DownlinkFormat.SurveillanceIdentityReply:      DecodeSurveillanceIdentityReply(result); break;             // DF5

                    case DownlinkFormat.AllCallReply:                   DecodeAllCallReply(result); break;                          // DF11

                    case DownlinkFormat.LongAirToAirSurveillance:       DecodeLongAirToAirSurveillance(result); break;              // DF16

                    case DownlinkFormat.ExtendedSquitter:               DecodeExtendedSquitter(result); break;                      // DF17

                    case DownlinkFormat.ExtendedSquitterNonTransponder: DecodeExtendedSquitterNonTransponder(result); break;        // DF18

                    case DownlinkFormat.MilitaryExtendedSquitter:       DecodeMilitaryExtendedSquitter(result); break;              // DF19

                    case DownlinkFormat.CommBAltitudeReply:             DecodeCommBAltitudeReply(result); break;                    // DF20

                    case DownlinkFormat.CommBIdentityReply:             DecodeCommBIdentityReply(result); break;                    // DF21

                    case DownlinkFormat.CommD:                          DecodeCommD(result); break;                                 // DF24
                    }

                    if (_Statistics.Lock != null)
                    {
                        lock (_Statistics.Lock) {
                            ++_Statistics.ModeSMessagesReceived;
                            ++_Statistics.ModeSDFCount[(int)result.DownlinkFormat];
                            if (isLongFrame)
                            {
                                ++_Statistics.ModeSLongFrameMessagesReceived;
                            }
                            else
                            {
                                ++_Statistics.ModeSShortFrameMessagesReceived;
                            }
                        }
                    }
                }
            }

            return(result);
        }
Exemplo n.º 14
0
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="modeSMessage"></param>
        /// <returns></returns>
        public AdsbMessage Translate(ModeSMessage modeSMessage)
        {
            if (Statistics == null)
            {
                throw new InvalidOperationException("Statistics must be supplied before Translate can be called");
            }

            AdsbMessage result = null;

            if (modeSMessage != null && modeSMessage.ExtendedSquitterMessage != null && modeSMessage.ExtendedSquitterMessage.Length == 7)
            {
                _BitStream.Initialise(modeSMessage.ExtendedSquitterMessage);

                result = new AdsbMessage(modeSMessage);

                if (IsCoarseFormatTisb(result))
                {
                    DecodeCoarseTisbAirbornePosition(result);
                }
                else
                {
                    result.Type = _BitStream.ReadByte(5);

                    switch (result.Type)
                    {
                    case 0:     DecodeAirbornePosition(result); break;

                    case 1:
                    case 2:
                    case 3:
                    case 4:     DecodeIdentification(result); break;

                    case 5:
                    case 6:
                    case 7:
                    case 8:     DecodeSurfacePosition(result); break;

                    case 9:
                    case 10:
                    case 11:
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                    case 16:
                    case 17:
                    case 18:    DecodeAirbornePosition(result); break;

                    case 19:    DecodeVelocity(result); break;

                    case 20:
                    case 21:
                    case 22:    DecodeAirbornePosition(result); break;

                    case 28:    DecodeAircraftStatus(result); break;

                    case 29:    DecodeTargetStateAndStatus(result); break;

                    case 31:    DecodeAircraftOperationalStatus(result); break;
                    }
                }

                if (Statistics != null)
                {
                    Statistics.Lock(r => {
                        ++r.AdsbCount;
                        ++r.AdsbMessageFormatCount[(int)result.MessageFormat];
                        ++r.AdsbTypeCount[result.Type];
                    });
                }
            }

            return(result);
        }
Exemplo n.º 15
0
        private bool ParseTick()
        {
            DemoCommand command = (DemoCommand)BitStream.ReadByte();

            BitStream.ReadInt(32);          // tick number
            BitStream.ReadByte();           // player slot

            this.CurrentTick++;             // = TickNum;

            switch (command)
            {
            case DemoCommand.Synctick:
                break;

            case DemoCommand.Stop:
                return(false);

            case DemoCommand.ConsoleCommand:
                BitStream.BeginChunk(BitStream.ReadSignedInt(32) * 8);
                BitStream.EndChunk();
                break;

            case DemoCommand.DataTables:
                BitStream.BeginChunk(BitStream.ReadSignedInt(32) * 8);
                SendTableParser.ParsePacket(BitStream);
                BitStream.EndChunk();

                for (int i = 0; i < SendTableParser.ServerClasses.Count; i++)
                {
                    var sc = SendTableParser.ServerClasses[i];

                    if (sc.BaseClasses.Count > 6 && sc.BaseClasses [6].Name == "CWeaponCSBase")
                    {
                        //It is a "weapon" (Gun, C4, ... (...is the cz still a "weapon" after the nerf?))
                        if (sc.BaseClasses.Count > 7)
                        {
                            if (sc.BaseClasses [7].Name == "CWeaponCSBaseGun")
                            {
                                //it is a ratatatata-weapon.
                                var s = sc.DTName.Substring(9).ToLower();
                                equipmentMapping.Add(sc, Equipment.MapEquipment(s));
                            }
                            else if (sc.BaseClasses [7].Name == "CBaseCSGrenade")
                            {
                                //"boom"-weapon.
                                equipmentMapping.Add(sc, Equipment.MapEquipment(sc.DTName.Substring(3).ToLower()));
                            }
                        }
                        else if (sc.Name == "CC4")
                        {
                            //Bomb is neither "ratatata" nor "boom", its "booooooom".
                            equipmentMapping.Add(sc, EquipmentElement.Bomb);
                        }
                        else if (sc.Name == "CKnife" || (sc.BaseClasses.Count > 6 && sc.BaseClasses [6].Name == "CKnife"))
                        {
                            //tsching weapon
                            equipmentMapping.Add(sc, EquipmentElement.Knife);
                        }
                        else if (sc.Name == "CWeaponNOVA" || sc.Name == "CWeaponSawedoff" || sc.Name == "CWeaponXM1014")
                        {
                            equipmentMapping.Add(sc, Equipment.MapEquipment(sc.Name.Substring(7).ToLower()));
                        }
                    }
                }

                BindEntites();

                break;

            case DemoCommand.StringTables:
                BitStream.BeginChunk(BitStream.ReadSignedInt(32) * 8);
                StringTables.ParsePacket(BitStream, this);
                BitStream.EndChunk();
                break;

            case DemoCommand.UserCommand:
                BitStream.ReadInt(32);
                BitStream.BeginChunk(BitStream.ReadSignedInt(32) * 8);
                BitStream.EndChunk();
                break;

            case DemoCommand.Signon:
            case DemoCommand.Packet:
                ParseDemoPacket();
                break;

            default:
                throw new Exception("Can't handle Demo-Command " + command);
            }

            return(true);
        }
Exemplo n.º 16
0
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="rawMessage"></param>
        /// <param name="start"></param>
        /// <param name="signalLevel"></param>
        /// <param name="isMlat"></param>
        /// <returns></returns>
        public ModeSMessage Translate(byte[] rawMessage, int start, int?signalLevel, bool isMlat)
        {
            if (Statistics == null)
            {
                throw new InvalidOperationException("Statistics must be provided before Translate can do any work");
            }
            ModeSMessage result = null;

            var messageLength = rawMessage == null ? 0 : rawMessage.Length - start;

            if (messageLength > 6)
            {
                _BitStream.Initialise(rawMessage);
                _BitStream.Skip(start * 8);
                var downlinkFormatValue = _BitStream.ReadByte(5);
                if (downlinkFormatValue >= 24)
                {
                    downlinkFormatValue = 24;
                    _BitStream.Skip(-3);
                }

                bool isLongFrame = true;
                switch (downlinkFormatValue)
                {
                case 16:
                case 17:
                case 18:
                case 19:
                case 20:
                case 21:
                case 24:    if (messageLength > 13)
                    {
                        result = new ModeSMessage();
                    }
                    break;

                default:    isLongFrame = false; result = new ModeSMessage(); break;
                }

                if (result != null)
                {
                    result.SignalLevel    = signalLevel;
                    result.IsMlat         = isMlat;
                    result.DownlinkFormat = (DownlinkFormat)downlinkFormatValue;

                    switch (result.DownlinkFormat)
                    {
                    case DownlinkFormat.ShortAirToAirSurveillance:      DecodeShortAirToAirSurveillance(result); break;             // DF0

                    case DownlinkFormat.SurveillanceAltitudeReply:      DecodeSurveillanceAltitudeReply(result); break;             // DF4

                    case DownlinkFormat.SurveillanceIdentityReply:      DecodeSurveillanceIdentityReply(result); break;             // DF5

                    case DownlinkFormat.AllCallReply:                   DecodeAllCallReply(result); break;                          // DF11

                    case DownlinkFormat.LongAirToAirSurveillance:       DecodeLongAirToAirSurveillance(result); break;              // DF16

                    case DownlinkFormat.ExtendedSquitter:               DecodeExtendedSquitter(result); break;                      // DF17

                    case DownlinkFormat.ExtendedSquitterNonTransponder: DecodeExtendedSquitterNonTransponder(result); break;        // DF18

                    case DownlinkFormat.MilitaryExtendedSquitter:       DecodeMilitaryExtendedSquitter(result); break;              // DF19

                    case DownlinkFormat.CommBAltitudeReply:             DecodeCommBAltitudeReply(result); break;                    // DF20

                    case DownlinkFormat.CommBIdentityReply:             DecodeCommBIdentityReply(result); break;                    // DF21

                    case DownlinkFormat.CommD:                          DecodeCommD(result); break;                                 // DF24
                    }

                    if (Statistics != null)
                    {
                        Statistics.Lock(r => {
                            ++r.ModeSMessagesReceived;
                            ++r.ModeSDFStatistics[(int)result.DownlinkFormat].MessagesReceived;
                            if (isLongFrame)
                            {
                                ++r.ModeSLongFrameMessagesReceived;
                            }
                            else
                            {
                                ++r.ModeSShortFrameMessagesReceived;
                            }
                        });
                    }
                }
            }

            return(result);
        }
Exemplo n.º 17
0
 public void BitStream_LengthRemaining_Returns_Zero_If_Called_After_Stream_Has_Been_Depleted()
 {
     _BitStream.Initialise(new byte[] { 0x0f });
     _BitStream.ReadByte(8);
     Assert.AreEqual(0, _BitStream.LengthRemaining);
 }
Exemplo n.º 18
0
        public static int ReadProtobufVarIntStub(IBitStream reader)
        {
            byte b = 0x80;
            int result = 0;
            for (int count = 0; (b & 0x80) != 0; count++) {
                b = reader.ReadByte();

                if ((count < 4) || ((count == 4) && (((b & 0xF8) == 0) || ((b & 0xF8) == 0xF8))))
                    result |= (b & ~0x80) << (7 * count);
                else {
                    if (count >= 10)
                        throw new OverflowException("Nope nope nope nope! 10 bytes max!");
                    if ((count == 9) ? (b != 1) : ((b & 0x7F) != 0x7F))
                        throw new NotSupportedException("more than 32 bits are not supported");
                }
            }

            return result;
        }
Exemplo n.º 19
0
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="modeSMessage"></param>
        /// <returns></returns>
        public AdsbMessage Translate(ModeSMessage modeSMessage)
        {
            AdsbMessage result = null;

            if (modeSMessage != null && modeSMessage.ExtendedSquitterMessage != null && modeSMessage.ExtendedSquitterMessage.Length == 7)
            {
                _BitStream.Initialise(modeSMessage.ExtendedSquitterMessage);

                result      = new AdsbMessage(modeSMessage);
                result.Type = _BitStream.ReadByte(5);

                switch (result.Type)
                {
                case 0:     DecodeAirbornePosition(result); break;

                case 1:
                case 2:
                case 3:
                case 4:     DecodeIdentification(result); break;

                case 5:
                case 6:
                case 7:
                case 8:     DecodeSurfacePosition(result); break;

                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 17:
                case 18:    DecodeAirbornePosition(result); break;

                case 19:    DecodeVelocity(result); break;

                case 20:
                case 21:
                case 22:    DecodeAirbornePosition(result); break;

                case 28:    DecodeAircraftStatus(result); break;

                case 29:    DecodeTargetStateAndStatus(result); break;

                case 31:    DecodeAircraftOperationalStatus(result); break;
                }

                if (_Statistics.Lock != null)
                {
                    lock (_Statistics.Lock) {
                        ++_Statistics.AdsbCount;
                        ++_Statistics.AdsbMessageFormatCount[(int)result.MessageFormat];
                        ++_Statistics.AdsbTypeCount[result.Type];
                    }
                }
            }

            return(result);
        }