コード例 #1
0
ファイル: MiNetClient.cs プロジェクト: WilliamGao1/MiNET
        public byte[] CompressBytes(byte[] input)
        {
            MemoryStream stream = new MemoryStream();
            stream.WriteByte(0x78);
            stream.WriteByte(0x01);
            int checksum;
            using (var compressStream = new ZLibStream(stream, CompressionLevel.Optimal, true))
            {
                NbtBinaryWriter writer = new NbtBinaryWriter(compressStream, true);
                writer.Write(input);

                writer.Flush();

                checksum = compressStream.Checksum;
                writer.Close();
            }

            byte[] checksumBytes = BitConverter.GetBytes(checksum);
            if (BitConverter.IsLittleEndian)
            {
                // Adler32 checksum is big-endian
                Array.Reverse(checksumBytes);
            }
            stream.Write(checksumBytes, 0, checksumBytes.Length);

            var bytes = stream.ToArray();
            stream.Close();

            return bytes;
        }
コード例 #2
0
ファイル: CryptoUtils.cs プロジェクト: CRBairdUSA/MiNET
        public static byte[] CompressJwtBytes(byte[] certChain, byte[] skinData, CompressionLevel compressionLevel)
        {
            MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream();
            stream.WriteByte(0x78);
            int checksum;
            switch (compressionLevel)
            {
                case CompressionLevel.Optimal:
                    stream.WriteByte(0xda);
                    break;
                case CompressionLevel.Fastest:
                    stream.WriteByte(0x9c);
                    break;
                case CompressionLevel.NoCompression:
                    stream.WriteByte(0x01);
                    break;
            }
            using (var compressStream = new ZLibStream(stream, compressionLevel, true))
            {
                {
                    byte[] lenBytes = BitConverter.GetBytes(certChain.Length);
                    //Array.Reverse(lenBytes);
                    compressStream.Write(lenBytes, 0, lenBytes.Length); // ??
                    compressStream.Write(certChain, 0, certChain.Length);
                }
                {
                    byte[] lenBytes = BitConverter.GetBytes(skinData.Length);
                    //Array.Reverse(lenBytes);
                    compressStream.Write(lenBytes, 0, lenBytes.Length); // ??
                    compressStream.Write(skinData, 0, skinData.Length);
                }
                checksum = compressStream.Checksum;
            }

            byte[] checksumBytes = BitConverter.GetBytes(checksum);
            if (BitConverter.IsLittleEndian)
            {
                // Adler32 checksum is big-endian
                Array.Reverse(checksumBytes);
            }
            stream.Write(checksumBytes, 0, checksumBytes.Length);

            var bytes = stream.ToArray();
            stream.Close();

            return bytes;
        }
コード例 #3
0
ファイル: Compression.cs プロジェクト: yungtechboy1/MiNET
        //private static ReadOnlySpan<byte> WriteRawVarInt32(uint value)
        //{
        //	var buf = new Span<byte>(new byte[10]);
        //	int i = 0;
        //	while ((value & -128) != 0)
        //	{
        //		buf[i++] = ((byte) ((value & 0x7F) | 0x80));
        //		value >>= 7;
        //	}

        //	buf[i] = (byte) value;

        //	return buf.Slice(0, i + 1);
        //}

        public static byte[] Compress(Memory <byte> input, bool writeLen = false, CompressionLevel compressionLevel = CompressionLevel.Fastest)
        {
            //if (/*!useOld && */compressionLevel == CompressionLevel.NoCompression)
            //{
            //	// header 2 bytes
            //	// compressed bytes
            //	// - header (1 byte) 0x01
            //	// - size total (2 bytes)
            //	// - ~size total (2 bytes)
            //	// - uncompressed data

            //	int i = 0;
            //	var output = new Span<byte>(new byte[2 + 1 + 2 + 2 + input.Length + 10 + 4]);
            //	output[i++] = 0x78;
            //	output[i++] = 0x01;
            //	output[i++] = 0x01;

            //	int lenAdd = 0;
            //	if (writeLen)
            //	{
            //		ReadOnlySpan<byte> length = WriteRawVarInt32((uint) input.Length);
            //		lenAdd += length.Length;
            //	}

            //	Span<byte> len = BitConverter.GetBytes(input.Length + lenAdd);
            //	len.CopyTo(output.Slice(i));
            //	i += 2;

            //	Span<byte> lenCompl = BitConverter.GetBytes(~(input.Length + lenAdd));
            //	lenCompl.CopyTo(output.Slice(i));
            //	i += 2;


            //	int expectedSize = 2 + 1 + 2 + 2 + input.Length + 4;

            //	if (writeLen)
            //	{
            //		ReadOnlySpan<byte> length = WriteRawVarInt32((uint) input.Length);
            //		length.CopyTo(output.Slice(i));
            //		expectedSize += length.Length;
            //		i += length.Length;
            //	}

            //	input.Span.CopyTo(output.Slice(i));

            //	byte[] result = output.Slice(0, expectedSize).ToArray();
            //	return result;
            //}


            using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream())
            {
                stream.WriteByte(0x78);                 // zlib header
                switch (compressionLevel)
                {
                case CompressionLevel.Optimal:
                    stream.WriteByte(0xda);
                    break;

                case CompressionLevel.Fastest:
                    stream.WriteByte(0x9c);
                    break;

                case CompressionLevel.NoCompression:
                    stream.WriteByte(0x01);
                    break;
                }
                int checksum = 0;
                using (var compressStream = new ZLibStream(stream, compressionLevel, true))
                {
                    if (writeLen)
                    {
                        WriteLength(compressStream, input.Length);
                    }

                    compressStream.Write(input.Span);
                    checksum = compressStream.Checksum;
                }

                var checksumBytes = BitConverter.GetBytes(checksum);
                if (BitConverter.IsLittleEndian)
                {
                    // Adler32 checksum is big-endian
                    Array.Reverse(checksumBytes);
                }
                stream.Write(checksumBytes);

                byte[] bytes = stream.ToArray();
                return(bytes);
            }
        }
コード例 #4
0
ファイル: Player.cs プロジェクト: MrGenga/MiNET
		public static byte[] CompressBytes(byte[] input, CompressionLevel compressionLevel, bool writeLen = false)
		{
			MemoryStream stream = new MemoryStream();
			stream.WriteByte(0x78);
			stream.WriteByte(0x01);
			int checksum;
			using (var compressStream = new ZLibStream(stream, compressionLevel, true))
			{
				byte[] lenBytes = BitConverter.GetBytes(input.Length);
				Array.Reverse(lenBytes);
				if (writeLen) compressStream.Write(lenBytes, 0, lenBytes.Length); // ??
				compressStream.Write(input, 0, input.Length);
				checksum = compressStream.Checksum;
			}

			byte[] checksumBytes = BitConverter.GetBytes(checksum);
			if (BitConverter.IsLittleEndian)
			{
				// Adler32 checksum is big-endian
				Array.Reverse(checksumBytes);
			}
			stream.Write(checksumBytes, 0, checksumBytes.Length);

			var bytes = stream.ToArray();
			stream.Close();

			return bytes;
		}