Ejemplo n.º 1
0
		public void Serialize(object value, Type sourceType, MaxSerializationAttribute maxSerializationAttribute, ByteWriter byteWriter)
		{
			if (value == null)
				return;

			var timeSpan = sourceType == typeof(TimeSpan?) ? ((TimeSpan?)value).Value : (TimeSpan)value;

			var round = (int)Math.Round(timeSpan.TotalHours * 2);
			byteWriter.Write((byte)round, maxSerializationAttribute.BytePos);
		}
Ejemplo n.º 2
0
 public byte[] write(ByteWriter byteWriter = null)
 {
     
     ByteWriter writer = byteWriter == null ? new ByteWriter() : byteWriter;
     
     writer.WriteString(key);
     writer.WriteString(value);
      
     
     return writer.GetBuffer();
     
 }
		public void Serialize(object value, Type sourceType, MaxSerializationAttribute maxSerializationAttribute, ByteWriter byteWriter)
		{
			var maxRfAddress = value as MaxRfAddress;
			if (maxRfAddress != null)
			{
				for (var index = 0; index < maxRfAddress.Bytes.Length; index++)
				{
					var b = maxRfAddress.Bytes[index];
					byteWriter.Write(b, maxSerializationAttribute.BytePos + index);
				}
			}
		}
Ejemplo n.º 4
0
		public void Serialize(object value, Type sourceType, MaxSerializationAttribute maxSerializationAttribute, ByteWriter byteWriter)
		{
			if (value == null)
				return;

			var dateTime = sourceType == typeof(DateTime?) ? ((DateTime?)value).Value : (DateTime)value;

			var leftMonth = (dateTime.Month & 0xe) << 4;
			var rightMonth = (dateTime.Month & 0x1) << 6;
			var day = dateTime.Day & 0x1f;
			var year = (dateTime.Year - 2000) & 0x1f;

			var firstByte = (byte)(leftMonth | day);
			var secondByte = (byte)(rightMonth | year);

			byteWriter.Write(firstByte, maxSerializationAttribute.BytePos);
			byteWriter.Write(secondByte, maxSerializationAttribute.BytePos + 1);
		}
Ejemplo n.º 5
0
		public void Serialize(object value, Type sourceType, MaxSerializationAttribute maxSerializationAttribute, ByteWriter byteWriter)
		{
			byte[] bytes;
			var baseType = sourceType.GetTypeInfo().IsEnum ? sourceType.GetTypeInfo().DeclaredFields.First().FieldType : sourceType;
			if (baseType == typeof(int))
			{
				var intValue = (int)value;
				bytes = BitConverter.GetBytes(intValue);
			}
			else if (baseType == typeof(long))
			{
				var longValue = (long)value;
				bytes = BitConverter.GetBytes(longValue);
			}
			else if (baseType == typeof(short))
			{
				var longValue = (short)value;
				bytes = BitConverter.GetBytes(longValue);
			}
			else
			{
				throw new Exception();
			}

			bytes = bytes.Take(maxSerializationAttribute.ByteSpan).Reverse().ToArray();

			for (var index = 0; index < bytes.Length && index < maxSerializationAttribute.ByteSpan; index++)
			{
				var b = bytes[index];

				var bitSpan = maxSerializationAttribute.ByteSpan > 1
					? maxSerializationAttribute.ByteSpan*8
					: maxSerializationAttribute.BitSpan;

				var mask = (1 << bitSpan) - 1;
				var value2 = (b & mask) << maxSerializationAttribute.BitPos;
				// 0x00 0x04 0x40 0x00 0x00 0x00
				byteWriter.Write((byte)value2, maxSerializationAttribute.BytePos + index);
			}
		}
Ejemplo n.º 6
0
        public void TestNTLMv2KeyExchangeMIC()
        {
            byte[] responseKeyNT = NTLMCryptography.NTOWFv2("Password", "User", "TAL-VM6");
            byte[] type1         = new byte[] { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x01, 0x00, 0x00, 0x00, 0x97, 0x82, 0x08, 0xe2,
                                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                                0x0a, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x0f };
            byte[] type2 = new byte[] { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00,
                                        0x38, 0x00, 0x00, 0x00, 0x15, 0x82, 0x8a, 0xe2, 0x63, 0x74, 0x79, 0x77, 0xe1, 0xea, 0x35, 0x51,
                                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x60, 0x00, 0x48, 0x00, 0x00, 0x00,
                                        0x06, 0x00, 0x71, 0x17, 0x00, 0x00, 0x00, 0x0f, 0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00,
                                        0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x02, 0x00, 0x10, 0x00, 0x54, 0x00, 0x41, 0x00,
                                        0x4c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x01, 0x00, 0x10, 0x00,
                                        0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00,
                                        0x04, 0x00, 0x10, 0x00, 0x54, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00,
                                        0x31, 0x00, 0x30, 0x00, 0x03, 0x00, 0x10, 0x00, 0x54, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x2d, 0x00,
                                        0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x07, 0x00, 0x08, 0x00, 0x1f, 0x8a, 0xd4, 0xff,
                                        0x01, 0x91, 0xd2, 0x01, 0x00, 0x00, 0x00, 0x00 };
            byte[] type3 = new byte[] { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x03, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x00,
                                        0x7c, 0x00, 0x00, 0x00, 0x02, 0x01, 0x02, 0x01, 0x94, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x0e, 0x00,
                                        0x58, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x66, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x0e, 0x00,
                                        0x6e, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x96, 0x01, 0x00, 0x00, 0x15, 0x82, 0x88, 0xe2,
                                        0x0a, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x0f, 0x82, 0x3c, 0xff, 0x48, 0xa9, 0x03, 0x13, 0x4c,
                                        0x33, 0x3c, 0x09, 0x87, 0xf3, 0x16, 0x59, 0x89, 0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00,
                                        0x56, 0x00, 0x4d, 0x00, 0x36, 0x00, 0x55, 0x00, 0x73, 0x00, 0x65, 0x00, 0x72, 0x00, 0x54, 0x00,
                                        0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0xb3, 0x06, 0x65, 0xe3, 0x9f, 0x03, 0xe1, 0xc3, 0xd8, 0x28, 0x7c, 0x9c,
                                        0x35, 0x0d, 0x32, 0x4c, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x8a, 0xd4, 0xff,
                                        0x01, 0x91, 0xd2, 0x01, 0x77, 0x71, 0x91, 0x94, 0xb1, 0x6e, 0x66, 0x28, 0x00, 0x00, 0x00, 0x00,
                                        0x02, 0x00, 0x10, 0x00, 0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00,
                                        0x31, 0x00, 0x30, 0x00, 0x01, 0x00, 0x10, 0x00, 0x54, 0x00, 0x41, 0x00, 0x4c, 0x00, 0x2d, 0x00,
                                        0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x04, 0x00, 0x10, 0x00, 0x54, 0x00, 0x61, 0x00,
                                        0x6c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x03, 0x00, 0x10, 0x00,
                                        0x54, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00,
                                        0x07, 0x00, 0x08, 0x00, 0x1f, 0x8a, 0xd4, 0xff, 0x01, 0x91, 0xd2, 0x01, 0x06, 0x00, 0x04, 0x00,
                                        0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x01, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x19, 0x0d, 0x73, 0xca, 0x97, 0x30, 0x2a, 0xa7,
                                        0x7a, 0x1f, 0xb6, 0xad, 0xe2, 0xe5, 0x4a, 0x59, 0x4a, 0x93, 0x7e, 0x37, 0xcd, 0x0c, 0xd7, 0x90,
                                        0x25, 0xc4, 0xaf, 0x8a, 0x17, 0x99, 0x69, 0x56, 0x0a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x1a, 0x00,
                                        0x63, 0x00, 0x69, 0x00, 0x66, 0x00, 0x73, 0x00, 0x2f, 0x00, 0x54, 0x00, 0x61, 0x00, 0x6c, 0x00,
                                        0x2d, 0x00, 0x56, 0x00, 0x4d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x7c, 0xce, 0x0b, 0x92, 0x46, 0x46, 0x0d, 0x5b, 0x3b,
                                        0x11, 0xb4, 0xde, 0x86, 0x28, 0x11 };

            byte[] serverChallenge = new ChallengeMessage(type2).ServerChallenge;
            AuthenticateMessage authenticateMessage = new AuthenticateMessage(type3);

            byte[] ntProofStr         = ByteReader.ReadBytes(authenticateMessage.NtChallengeResponse, 0, 16);
            byte[] sessionBaseKey     = new HMACMD5(responseKeyNT).ComputeHash(ntProofStr);
            byte[] exportedSessionKey = GetExportedSessionKey(sessionBaseKey, authenticateMessage, serverChallenge, null);

            // https://msdn.microsoft.com/en-us/library/cc236695.aspx
            const int micFieldOffset = 72;

            ByteWriter.WriteBytes(type3, micFieldOffset, new byte[16]);
            byte[] temp     = ByteUtils.Concatenate(ByteUtils.Concatenate(type1, type2), type3);
            byte[] mic      = new HMACMD5(exportedSessionKey).ComputeHash(temp);
            byte[] expected = new byte[] { 0x82, 0x3c, 0xff, 0x48, 0xa9, 0x03, 0x13, 0x4c, 0x33, 0x3c, 0x09, 0x87, 0xf3, 0x16, 0x59, 0x89 };

            Assert.True(ByteUtils.AreByteArraysEqual(mic, expected));
        }
Ejemplo n.º 7
0
 internal override void WriteBody(ByteWriter writer)
 {
     writer.WriteUInt32(this.TargetNetID);
 }
Ejemplo n.º 8
0
        public void Write_InternalFixArray()
        {
            ByteWriter s = new ByteWriter();

            WriteBuilder b = new WriteBuilder();
            Writer w = b.Create(s);

            ListContainer val = new ListContainer();
            val.Field1 = new int[3];
            val.Field1[0] = 1;
            val.Field1[1] = 2;
            val.Field1[2] = 3;
            val.Field2 = new List<uint>();
            val.Field3 = "";

            w.Write<ListContainer>(val);

            Assert.AreEqual(new byte[]{
                0x00, 0x00, 0x00, 0x01,
                0x00, 0x00, 0x00, 0x02,
                0x00, 0x00, 0x00, 0x03,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00}, s.ToArray());
        }
Ejemplo n.º 9
0
 public override void DoWrite(ByteWriter BW)
 {
 }
Ejemplo n.º 10
0
 protected override void WriteBody(ByteWriter writer)
 {
     writer.WriteFixedString(SoundName, 1024);
     writer.WriteUInt32(OwnerNetID);
 }
 protected override void WriteBody(ByteWriter writer)
 {
     writer.WriteFixedStringLast(MessageID, 128);
 }
Ejemplo n.º 12
0
 public byte[] write(ByteWriter byteWriter = null)
 {
     
     ByteWriter writer = byteWriter == null ? new ByteWriter() : byteWriter;
     
     writer.Write(messageType);
      
     // write array settings
     writer.Write(settings.Count);
     
     for (int i_1 = 0; i_1 < settings.Count; i_1++)
     {
          
         settings[i_1].write(writer);
         
     }
     
      
     
     return writer.GetBuffer();
     
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Encodes this instance.
 /// </summary>
 internal override void Encode(ByteWriter Packet)
 {
     Packet.AddInt(this.Id);
     base.Encode(Packet);
 }
Ejemplo n.º 14
0
 protected override void InternalGetBytes(ByteWriter writer)
 {
     writer.WriteUInt32(RecipientChannel);
 }
Ejemplo n.º 15
0
        public static void CompressBinaryTree(short[] input, ByteWriter output, short start, short end)
        {
            short length = (short)(end - start);
            short middle;

            byte finalBinaryDataByte;

            if (length <= 2)
            {
                if (length == 1)
                {
                    output.WriteShort(input[start]);
                }
                else if (length == 2)
                {
                    output.WriteShort(input[start]);
                    output.WriteShort(input[start+1]);
                }
                return;
            }

            middle = (short)(start + length / 2);

            // This is also 2 booleans(rather 8, to be precise).
            // They tell if the array is filled with 1 simple block or more complex. (True: simple, False: complex)
            finalBinaryDataByte = 0;

            Action<byte> lambda = null;
            lambda = new Action<byte>(b =>
                {
                    short l_start;
                    short l_end;
                    short l_id;

                    if (b == 0)
                    {
                        if (length == 1)
                        {
                            output.WriteByte(finalBinaryDataByte);
                            return;
                        }
                        l_start = start;
                        l_end = middle;
                    }
                    else
                    {
                        l_start = middle;
                        l_end = end;
                    }

                    l_id = getType(input, l_start, l_end);

                    //if (l_id == -1)
                    //    throw new Exception("Negative value(-1) found in data! Only possetive values should be used. Negative values may be allowed, but -1 is forbidden!");

                    bool isSimple = (l_id != -1);

                    if (isSimple)
                        finalBinaryDataByte |= (byte)(1 << b);

                    if (b > 0) //quadtree does not have more than 4 children!
                    {
                        // Goes to back to the beginning...
                        lambda((byte)(b - 1));
                    }
                    else
                    {
                        output.WriteByte(finalBinaryDataByte);
                    }

                    if (isSimple)
                    {
                        output.WriteShort(l_id);
                    }
                    else
                    {
                        // declares another quad inside the square
                        CompressBinaryTree(input, output, l_start, l_end);
                    }

                    // Goes back to the end...
                    return;
                });

            // 1a -> 0ab-> 1b
            lambda(1);
        }
Ejemplo n.º 16
0
        public bool Serialize(Type _Type, object _Object, out byte[] _Bytes)
        {
            if (_Type != typeof(Mesh))
            {
                _Bytes = null;
                return(false);
            }

            ByteWriter var_Writer = new ByteWriter();

            if (_Object == null)
            {
                var_Writer.Write((bool)true);
                _Bytes = var_Writer.ToArray();
                return(true);
            }
            else
            {
                var_Writer.Write((bool)false);
            }

            //

            Mesh var_Mesh = _Object as Mesh;

            //Bindposes
            byte[] var_Bytes = Serializer.Internal_Serialize(typeof(Matrix4x4[]), var_Mesh.bindposes);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

            //BoneWeights
            var_Bytes = Serializer.Internal_Serialize(typeof(BoneWeight[]), var_Mesh.boneWeights);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

            //Bounds
            var_Bytes = Serializer.Internal_Serialize(typeof(Bounds), var_Mesh.bounds);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

            //Vertices
            var_Bytes = Serializer.Internal_Serialize(typeof(Vector3[]), var_Mesh.vertices);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

            //Normals
            var_Bytes = Serializer.Internal_Serialize(typeof(Vector3[]), var_Mesh.normals);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

            //Colors
            var_Bytes = Serializer.Internal_Serialize(typeof(Color[]), var_Mesh.colors);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

            //Colors32
            var_Bytes = Serializer.Internal_Serialize(typeof(Color32[]), var_Mesh.colors32);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

            //UV
            var_Bytes = Serializer.Internal_Serialize(typeof(Vector2[]), var_Mesh.uv);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);
            var_Bytes = Serializer.Internal_Serialize(typeof(Vector2[]), var_Mesh.uv2);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);
            var_Bytes = Serializer.Internal_Serialize(typeof(Vector2[]), var_Mesh.uv3);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);
            var_Bytes = Serializer.Internal_Serialize(typeof(Vector2[]), var_Mesh.uv4);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

            //SubMeshCount
            var_Writer.Write(var_Mesh.subMeshCount);
            for (int s = 0; s < var_Mesh.subMeshCount; s++)
            {
                var_Bytes = Serializer.Internal_Serialize(typeof(int[]), var_Mesh.GetIndices(s));
                var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

                var_Writer.Write((byte)var_Mesh.GetTopology(s));

                var_Bytes = Serializer.Internal_Serialize(typeof(int[]), var_Mesh.GetTriangles(s));
                var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);
            }

            //Tangents
            var_Bytes = Serializer.Internal_Serialize(typeof(Vector4[]), var_Mesh.tangents);
            var_Writer.WriteBytesAndSize(var_Bytes, var_Bytes.Length);

            _Bytes = var_Writer.ToArray();
            return(true);
        }
 internal override void WriteBody(ByteWriter writer)
 {
     writer.WriteByte(this.Slot);
 }
 public void WriteBytes(int recordOffset, byte[] recordBytes, int bytesToWrite)
 {
     ByteWriter.WriteBytes(Data, recordOffset - m_dataOffset, recordBytes, bytesToWrite);
 }
Ejemplo n.º 19
0
 public byte[] write(ByteWriter byteWriter = null)
 {
     
     ByteWriter writer = byteWriter == null ? new ByteWriter() : byteWriter;
     
     writer.Write(messageType);
     writer.Write(error);
      
     
     return writer.GetBuffer();
     
 }
Ejemplo n.º 20
0
 protected override void WriteBody(ByteWriter writer)
 {
     writer.WriteFloat(FadeTime);
     writer.WriteFloat(FadeTargetValue);
 }
Ejemplo n.º 21
0
 protected override void WriteBody(ByteWriter writer)
 {
     writer.WriteByte(QuestEvent);
     writer.WriteUInt32(QuestID);
 }
Ejemplo n.º 22
0
        public override void WriteBytes(byte[] buffer, int offset)
        {
            int fixedLength = FixedLengthV20001;

            if (Version >= ScatterMinimumVersion)
            {
                fixedLength += 4;
                if (Version >= TeamIDMinimumVersion)
                {
                    fixedLength += 4;
                }
            }

            int nextOffset  = fixedLength;;
            int identOffset = 0;

            if (Ident != null && Ident.Length > 0)
            {
                identOffset = nextOffset;
                nextOffset += Ident.Length + 1;
            }

            int teamIDOffset = 0;

            if (Version >= TeamIDMinimumVersion && TeamID != null && TeamID.Length > 0)
            {
                teamIDOffset = nextOffset;
                nextOffset  += TeamID.Length + 1;
            }

            int specialHashesOffset = nextOffset;
            int hashOffset          = specialHashesOffset + SpecialHashes.Count * HashSize;

            BigEndianWriter.WriteUInt32(buffer, offset + 0, Signature);
            BigEndianWriter.WriteUInt32(buffer, offset + 4, (uint)Length);
            BigEndianWriter.WriteUInt32(buffer, offset + 8, Version);
            BigEndianWriter.WriteUInt32(buffer, offset + 12, (uint)Flags);
            BigEndianWriter.WriteUInt32(buffer, offset + 16, (uint)hashOffset);
            BigEndianWriter.WriteUInt32(buffer, offset + 20, (uint)identOffset);
            BigEndianWriter.WriteUInt32(buffer, offset + 24, (uint)SpecialHashes.Count);
            BigEndianWriter.WriteUInt32(buffer, offset + 28, (uint)CodeHashes.Count);
            BigEndianWriter.WriteUInt32(buffer, offset + 32, CodeLimit);
            ByteWriter.WriteByte(buffer, offset + 36, HashSize);
            ByteWriter.WriteByte(buffer, offset + 37, (byte)HashType);
            ByteWriter.WriteByte(buffer, offset + 38, Platform);
            ByteWriter.WriteByte(buffer, offset + 39, PageSizeLog2);
            BigEndianWriter.WriteUInt32(buffer, offset + 40, Spare2);
            if (Version >= ScatterMinimumVersion)
            {
                BigEndianWriter.WriteUInt32(buffer, offset + 44, 0);
                if (Version >= TeamIDMinimumVersion)
                {
                    BigEndianWriter.WriteUInt32(buffer, offset + 48, (uint)teamIDOffset);
                }
            }

            if (Ident != null && Ident.Length > 0)
            {
                ByteWriter.WriteNullTerminatedAnsiString(buffer, offset + (int)identOffset, Ident);
            }
            if (Version >= TeamIDMinimumVersion && TeamID != null && TeamID.Length > 0)
            {
                ByteWriter.WriteNullTerminatedAnsiString(buffer, offset + (int)teamIDOffset, TeamID);
            }

            for (int index = 0; index < SpecialHashes.Count; index++)
            {
                if (SpecialHashes[index].Length != HashSize)
                {
                    throw new ArgumentException("Hash length does not match declared HashSize");
                }
                ByteWriter.WriteBytes(buffer, offset + specialHashesOffset + index * (int)HashSize, SpecialHashes[index]);
            }

            for (int index = 0; index < CodeHashes.Count; index++)
            {
                if (CodeHashes[index].Length != HashSize)
                {
                    throw new ArgumentException("Hash length does not match declared HashSize");
                }
                ByteWriter.WriteBytes(buffer, offset + hashOffset + index * (int)HashSize, CodeHashes[index]);
            }
        }
Ejemplo n.º 23
0
        public void Write_AttrMapping()
        {
            CompleteFile cf = new CompleteFile();
            cf.FileName = "sillyprog";
            cf.Type = new FileType();
            cf.Type.Type = FileKind.Exec;
            cf.Type.Interpretor = "lisp";
            cf.Owner = "john";
            cf.Data = new byte[] { 0x28, 0x71, 0x75, 0x69, 0x74, 0x29 };

            ByteWriter s = new ByteWriter();

            WriteBuilder b = new WriteBuilder()
                //.Map<CompleteFile>(CompleteFile.Write)
                //.Map<FileType>(FileType.Write)
                ;
            Writer w = b.Create(s);

            w.Write<CompleteFile>(cf);

            byte[] expected = new byte[]
            {
                0x00, 0x00, 0x00, 0x09,
                0x73, 0x69, 0x6c, 0x6c,
                0x79, 0x70, 0x72, 0x6f,
                0x67, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x02,
                0x00, 0x00, 0x00, 0x04,
                0x6c, 0x69, 0x73, 0x70,
                0x00, 0x00, 0x00, 0x04,
                0x6a, 0x6f, 0x68, 0x6e,
                0x00, 0x00, 0x00, 0x06,
                0x28, 0x71, 0x75, 0x69,
                0x74, 0x29, 0x00, 0x00
            };

            Assert.AreEqual(expected, s.ToArray());
        }
Ejemplo n.º 24
0
        private void Generate(IRandomGenExtended r, int depth, ByteWriter bs)
        {
            int majorType = valueMajorTypes[r.GetInt32(valueMajorTypes.Length)];

            if (bs.ByteLength > 2000000)
            {
                majorType = valueMajorTypesHighLength[r.GetInt32(
                                                          valueMajorTypesHighLength.Length)];
            }
            if (majorType == 3 || majorType == 2)
            {
                int len = r.GetInt32(1000);
                if (r.GetInt32(50) == 0 && depth < 2)
                {
                    var v = (long)r.GetInt32(100000) * r.GetInt32(100000);
                    len = (int)(v / 100000);
                }
                if (depth > 6)
                {
                    len = r.GetInt32(100) == 0 ? 1 : 0;
                }
                // TODO: Ensure key uniqueness
                if (r.GetInt32(2) == 0)
                {
                    // Indefinite length
                    bs.Write(0x1f + (majorType * 0x20));
                    while (len > 0)
                    {
                        int sublen = r.GetInt32(len + 1);
                        GenerateArgument(r, majorType, sublen, bs);
                        if (majorType == 3)
                        {
                            GenerateUtf8(r, bs, sublen);
                        }
                        else
                        {
                            for (int i = 0; i < sublen; ++i)
                            {
                                bs.Write(r.GetInt32(256));
                            }
                        }
                        len -= sublen;
                    }
                    bs.Write(0xff);
                }
                else
                {
                    // Definite length
                    GenerateArgument(r, majorType, len, bs);
                    if (majorType == 3)
                    {
                        GenerateUtf8(r, bs, len);
                    }
                    else
                    {
                        for (int i = 0; i < len; ++i)
                        {
                            bs.Write(r.GetInt32(256));
                        }
                    }
                }
                return;
            }
            else if (majorType == 4 || majorType == 5)
            {
                int len = r.GetInt32(8);
                if (r.GetInt32(50) == 0 && depth < 2)
                {
                    var v = (long)r.GetInt32(1000) * r.GetInt32(1000);
                    len = (int)(v / 1000);
                }
                bool indefiniteLength = r.GetInt32(2) == 0;
                if (indefiniteLength)
                {
                    bs.Write(0x1f + (majorType * 0x20));
                }
                else
                {
                    GenerateArgument(r, majorType, len, bs);
                }
                for (int i = 0; i < len; ++i)
                {
                    this.Generate(r, depth + 1, bs);
                    if (majorType == 5)
                    {
                        this.Generate(r, depth + 1, bs);
                    }
                }
                if (indefiniteLength)
                {
                    bs.Write(0xff);
                }
                return;
            }
            int arg = r.GetInt32(5);

            switch (arg)
            {
            case 0:
                bs.Write((majorType * 0x20) + r.GetInt32(0x18));
                break;

            case 1:
                bs.Write((majorType * 0x20) + 0x18);
                if (majorType == 7)
                {
                    bs.Write(32 + r.GetInt32(224));
                }
                else
                {
                    bs.Write(r.GetInt32(256));
                }
                break;

            case 2:
                bs.Write((majorType * 0x20) + 0x19);
                for (int i = 0; i < 2; ++i)
                {
                    bs.Write(r.GetInt32(256));
                }
                break;

            case 3:
                bs.Write((majorType * 0x20) + 0x1a);
                for (int i = 0; i < 4; ++i)
                {
                    bs.Write(r.GetInt32(256));
                }
                break;

            case 4:
                bs.Write((majorType * 0x20) + 0x1b);
                for (int i = 0; i < 8; ++i)
                {
                    bs.Write(r.GetInt32(256));
                }
                break;
            }
            if (majorType == 6)
            {
                this.Generate(r, depth + 1, bs);
            }
        }
Ejemplo n.º 25
0
 internal override void WriteBody(ByteWriter writer)
 {
     writer.WriteUInt32(this.TargetNetID);
     writer.WriteFloat(this.ExpAmmount);
 }
Ejemplo n.º 26
0
 internal override void Encode(ByteWriter Writer)
 {
     base.Encode(Writer);
     Writer.AddInt(this.Level);
 }
Ejemplo n.º 27
0
 internal override void WriteBody(ByteWriter writer)
 {
     writer.WriteBasicAttackData(this.BasicAttackData);
 }
Ejemplo n.º 28
0
 internal override void WriteBody(ByteWriter writer)
 {
     writer.WriteByte(BotCountOrder);
     writer.WriteByte(BotCountChaos);
 }
 internal override void WriteBody(ByteWriter writer)
 {
 }
Ejemplo n.º 30
0
 protected override void WriteBody(ByteWriter writer)
 {
     writer.WritePad(4);
     writer.WriteTipConfig(TipConfig);
     writer.WritePad(8);
 }
Ejemplo n.º 31
0
        public void WriteStruct()
        {
            ByteWriter s = new ByteWriter();

            WriteBuilder b = new WriteBuilder();
            Writer w = b.Create(s);

            StructInt val = new StructInt();
            val.Field1 = 0x1234ABCD;
            val.Field2 = 0xCDEF9876;

            w.Write<StructInt>(val);

            Assert.AreEqual(new byte[]{0x12, 0x34, 0xAB, 0xCD, 0xCD, 0xEF, 0x98, 0x76}, s.ToArray());
        }
Ejemplo n.º 32
0
 public override void DoWrite(ByteWriter BW)
 {
     BW.AppendInt32(ActivePlayerIndex);
     BW.AppendInt32(ActiveUnitIndex);
 }
Ejemplo n.º 33
0
        public void Write_TwoItem()
        {
            ByteWriter s = new ByteWriter();

            WriteBuilder b = new WriteBuilder();
            Writer w = b.Create(s);

            ListItem val = new ListItem();
            val.Field1 = 1;
            val.Field2 = null;
            val.Field3 = new ListItem();
            val.Field3.Field1 = 1;
            val.Field3.Field2 = 3;
            val.Field3.Field3 = null;

            w.Write<ListItem>(val);

            Assert.AreEqual(new byte[]{
                0x00, 0x00, 0x00, 0x01,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x01,
                0x00, 0x00, 0x00, 0x01,
                0x00, 0x00, 0x00, 0x01,
                0x00, 0x00, 0x00, 0x03,
                0x00, 0x00, 0x00, 0x00}, s.ToArray());
        }
Ejemplo n.º 34
0
 public override void Write(ByteWriter writer)
 {
     writer.Write(FeeRate, 8);
 }
 protected override void WriteBody(ByteWriter writer)
 {
     writer.WriteUInt32(TargetNetID);
     writer.WriteVector3(TargetPosition);
 }
Ejemplo n.º 36
0
 internal override void WriteBody(ByteWriter writer)
 {
     writer.WriteUInt32(this.UnitNetID);
     writer.WriteFixedString(this.IconName, 64);
 }
Ejemplo n.º 37
0
 public void Write(ByteWriter writer)
 {
     writer.Write(this.DbVersion);
     writer.Write(this.Password);
     writer.Skip(178);
 }
Ejemplo n.º 38
0
 internal override void WriteBody(ByteWriter writer)
 {
     //Unused
 }
Ejemplo n.º 39
0
        void WriteDigitalMessage(int port, bool DoFlush=true)
        {
            if (DigitalPortsChanged[port]) {
            // remember the position in the stream
            long lastPos = Writer.BaseStream.Position;

            // seek to the position
            Writer.Seek((int)DigitalMessages[port],SeekOrigin.Begin);

            // override the message on the stream
            Writer += Util.EncodeDigitalMessage(port,DigitalPins[port]);

            // go back to the last position
            Writer.Seek((int)lastPos,SeekOrigin.Begin);
              } else {
            // remember the position in the stream
            DigitalMessages[port] = Writer.BaseStream.Position;
            // append the message to the stream
            Writer += Util.EncodeDigitalMessage(port,DigitalPins[port]);
              }

              // Flush:
              if (DoFlush) Writer.Flush();
              // and flag as changed:
              DigitalPortsChanged[port] = true;
        }
Ejemplo n.º 40
0
 public override void WriteArgs(ByteWriter writer)
 {
     base.WriteArgs(writer);
     writer.WriteUInt32(CapturePoint);
 }
Ejemplo n.º 41
0
 // Does a hash check and fails if the two byte buffers are not identical.
 public static void CompareFiles(byte[] file, ByteWriter bw)
 {
     Assert.AreEqual(HASH.SHA256(new MemoryStream(file)),
                 HASH.SHA256(new MemoryStream(bw.ToByteArray())));
 }
Ejemplo n.º 42
0
 // Does a hash check and fails if the two byte buffers are not identical.
 public static void CompareFiles(byte[] file, ByteWriter bw)
 {
     Assert.AreEqual(HASH.SHA256(new MemoryStream(file)),
                     HASH.SHA256(new MemoryStream(bw.ToByteArray())));
 }
Ejemplo n.º 43
0
        /// <summary>
        /// Only posetive values are allowed!
        /// Data stored as:
        /// [quad] main;
        /// [quad(complex)] = [(byte) isSimple-booleans], [quad], [quad], [quad], [quad];
        /// [quad(simple)] = [(short) blockoutput];
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void CompressQuadTree(short[,] input, ByteWriter output, short x1, short y1, short x2, short y2)
        {
            short width = (short)(x2 - x1);
            short height = (short)(y2 - y1);

            short middleX;
            short middleY;

            byte finalQuadDataByte;

            if (width == 0 || height == 0)
                return;

            if (width <= 2 && height <= 2)
            {
                if (width == 1 && height == 1)
                {
                    output.WriteShort(input[x1, y1]);
                }
                else if (width == 2 && height == 1)
                {
                    output.WriteShort(input[x1, y1]);
                    output.WriteShort(input[x1+1, y1]);
                }
                else if (width == 1 && height == 2)
                {
                    output.WriteShort(input[x1, y1]);
                    output.WriteShort(input[x1, y1+1]);
                }
                else if (width == 2 && height == 2)
                {
                    output.WriteShort(input[x1, y1]);
                    output.WriteShort(input[x1+1, y1]);
                    output.WriteShort(input[x1, y1+1]);
                    output.WriteShort(input[x1+1, y1+1]);
                }
                return;
            }

            middleX = (short)(x1 + (width >> 1));
            middleY = (short)(y1 + (height >> 1));

            // This is also 4 booleans(rather 8, to be precise).
            // They tell if the area is filled with 1 simple block or more complex. (True: simple, False: complex)
            finalQuadDataByte = 0;

            Action<byte> lambda = null;
            lambda = new Action<byte>(b =>
                {
                    short l_x1;
                    short l_y1;
                    short l_x2;
                    short l_y2;
                    short l_id;

                    switch (b)
                    {
                        // Strong values take half or more than half of the size,
                        // weak values are take half or more than half of the size.
                        // Example: weak: 3/2 = 1, strong: 3-3/2 = 2
                        case 0:
                            //weak x, weak y
                            if (width == 1 || height == 1)
                            {
                                output.WriteByte(finalQuadDataByte);
                                return;
                            }

                            l_x1 = x1;
                            l_y1 = y1;
                            l_x2 = middleX;
                            l_y2 = middleY;
                            break;

                        case 1:
                            //strong x, weak y
                            if (height == 1)
                            {
                                // Goes to back to the beginning...
                                lambda(0);
                                return;
                            }

                            l_x1 = middleX;
                            l_y1 = y1;
                            l_x2 = x2;
                            l_y2 = middleY;
                            break;

                        case 2:
                            //weak x, strong y
                            if (width == 1)
                            {
                                // Goes to back to the beginning...
                                lambda(1);
                                return;
                            }

                            l_x1 = x1;
                            l_y1 = middleY;
                            l_x2 = middleX;
                            l_y2 = y2;
                            break;

                        case 3:
                            //strong x, strong y
                            l_x1 = middleX;
                            l_y1 = middleY;
                            l_x2 = x2;
                            l_y2 = y2;
                            break;

                        default:
                            throw new Exception(b.ToString() + " is an invalid quadtree child index. It can only be 0,1,2 or 3.");
                    }

                    l_id = getType(input, l_x1, l_y1, l_x2, l_y2);

                    bool isSimple = (l_id != -1);

                    if (isSimple)
                        finalQuadDataByte |= (byte)(1 << b);

                    if (b > 0) //quadtree does not have more than 4 children!
                    {
                        // Goes to back to the beginning...
                        lambda((byte)(b - 1));
                    }
                    else
                    {
                        output.WriteByte(finalQuadDataByte);
                    }

                    if (isSimple)
                    {
                        output.WriteShort(l_id);
                    }
                    else
                    {
                        // declares another quad inside the square
                        CompressQuadTree(input, output, l_x1, l_y1, l_x2, l_y2);
                    }

                    // Goes back to the end...
                    return;

                });

            // starts from the end and ends with the end.
            // 3-> 2-> 1-> 0-> 1-> 2-> 3
            lambda(3);
        }
Ejemplo n.º 44
0
 protected override void WriteBody(ByteWriter writer)
 {
     writer.WriteUInt32(PlayerNetID);
     writer.WriteByte(StatEvent);
 }
Ejemplo n.º 45
0
        public static Pair<bool, byte[]> CompressWorld(short[,] world, short width, short height)
        {
            ByteWriter worldData = new ByteWriter();
            short[] cornerBlocks = new short[2 * width + 2 * height - 2];//new short[796];// 200+200+198+198

            //short i = 0;
            short end = width;
            short lastPos = 0;

            Action<Func<short, short>> lambda = (Func<short, short> borderBlock) =>
                {
                    for (short i = 0; i < end; i++)
                    {
                        cornerBlocks[i + lastPos] = borderBlock(i);
                    }
                    lastPos += end;
                };

            lambda((short i) => { return world[i, 0]; });

            lambda((short i) => { return world[i, height-1]; });

            end = (short)(height - 2);
            lambda((short i) => { return world[0, i+1]; });

            lambda((short i) => { return world[width-1, i+1]; });

            /*for (; i < j; i++) // top
            {
                cornerBlocks[i] = world[i, 0];
            }

            k = j;
            j += width;

            for (; i < j; i++) // bottom
            {
                cornerBlocks[i] = world[i - k, height-1];
            }

            k = (short)(j - 1);
            j += (short)(height - 2);

            for (; i < j; i++) // left
            {
                cornerBlocks[i] = world[0, i - k];
            }

            k = (short)(j - 1);
            j += (short)(height - 2);

            for (; i < j; i++) // right
            {
                cornerBlocks[i] = world[width-1, i - k];
            }*/

            worldData.WriteShort(width);
            worldData.WriteShort(height);

            CompressQuadTree(world, worldData, 1, 1, (short)(width-1), (short)(height-1));
            CompressBinaryTree(cornerBlocks, worldData, 0, lastPos);

            byte[] quadTree = worldData.ToArray();
            byte[] deflated = ByteCompressor.Deflate(quadTree);

            if (deflated.Length < quadTree.Length)
                return new Pair<bool, byte[]>(true, deflated);//ZipCompressor.Compress(worldData.ToArray());
            else
                return new Pair<bool, byte[]>(false, quadTree);
        }
Ejemplo n.º 46
0
 public void WriteBytes(byte[] buffer, int offset)
 {
     AttributeNameLengthInBytes = (byte)AttributeName.Length;
     ByteWriter.WriteByte(buffer, offset + 0, AttributeNameLengthInBytes);
     ByteWriter.WriteAnsiString(buffer, offset + 1, AttributeName, AttributeName.Length);
 }
Ejemplo n.º 47
0
        public void Save(string filename=null)
        {
            var bw = new ByteWriter();

            bw.STRING(FileID);

            bw.UINT((uint)Skills.Count);

            foreach (var s in Skills)
            {
                // Handle empty entries
                if (s == null)
                {
                    for (int i = 0; i < 41; i++)
                    {
                        bw.BYTE(0);
                    }

                    continue;
                }

                bw.BYTE((byte)s.Type);
                bw.STRING(s.Message.ID.ToString());
                bw.STRING(s.Message.SubCategory.ID.ToString());
                bw.UINT(s.IconIndex);
                bw.USHORT(s.ProjectileAnimation);
                bw.UINT(s.CharacterAnimation);
                bw.INT(s.Honor);
                bw.UINT(s.Level);
                bw.BYTE(s.CharacterType);

                uint mana = s.ManaUsage;
                if (s.ManaUsageStyle == ManaUsageStyle.Percentage)
                {
                    mana += 1000000000;
                }
                bw.UINT(mana);

                short range = s.Range;
                if (s.IsAOE)
                {
                    range += 10000;
                }
                bw.SHORT(range);

                bw.BYTE(s.NeedsTarget);

                bw.BYTE(s.Unused1);
                bw.BYTE(s.Unused2);
                bw.BYTE(s.Unused3);

                bw.BYTE(s.EffectType);
                bw.UINT(s.Multiplier);

                bw.UINT(s.Unknown5);
            }

            File.WriteAllBytes(filename ?? FileName, bw.ToByteArray());
        }
Ejemplo n.º 48
0
 protected abstract void InternalGetBytes(ByteWriter writer);