//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using GameFramework; using System; using System.IO; /// /// 对 BinaryReader 和 BinaryWriter 的扩展方法。 /// public static class BinaryExtension { private static readonly byte[] s_CachedBytes = new byte[byte.MaxValue + 1]; /// /// 从二进制流读取编码后的 32 位有符号整数。 /// /// 要读取的二进制流。 /// 读取的 32 位有符号整数。 public static int Read7BitEncodedInt32(this BinaryReader binaryReader) { int value = 0; int shift = 0; byte b; do { if (shift >= 35) { throw new GameFrameworkException("7 bit encoded int is invalid."); } b = binaryReader.ReadByte(); value |= (b & 0x7f) << shift; shift += 7; } while ((b & 0x80) != 0); return value; } /// /// 向二进制流写入编码后的 32 位有符号整数。 /// /// 要写入的二进制流。 /// 要写入的 32 位有符号整数。 public static void Write7BitEncodedInt32(this BinaryWriter binaryWriter, int value) { uint num = (uint)value; while (num >= 0x80) { binaryWriter.Write((byte)(num | 0x80)); num >>= 7; } binaryWriter.Write((byte)num); } /// /// 从二进制流读取编码后的 32 位无符号整数。 /// /// 要读取的二进制流。 /// 读取的 32 位无符号整数。 public static uint Read7BitEncodedUInt32(this BinaryReader binaryReader) { return (uint)Read7BitEncodedInt32(binaryReader); } /// /// 向二进制流写入编码后的 32 位无符号整数。 /// /// 要写入的二进制流。 /// 要写入的 32 位无符号整数。 public static void Write7BitEncodedUInt32(this BinaryWriter binaryWriter, uint value) { Write7BitEncodedInt32(binaryWriter, (int)value); } /// /// 从二进制流读取编码后的 64 位有符号整数。 /// /// 要读取的二进制流。 /// 读取的 64 位有符号整数。 public static long Read7BitEncodedInt64(this BinaryReader binaryReader) { long value = 0L; int shift = 0; byte b; do { if (shift >= 70) { throw new GameFrameworkException("7 bit encoded int is invalid."); } b = binaryReader.ReadByte(); value |= (b & 0x7fL) << shift; shift += 7; } while ((b & 0x80) != 0); return value; } /// /// 向二进制流写入编码后的 64 位有符号整数。 /// /// 要写入的二进制流。 /// 要写入的 64 位有符号整数。 public static void Write7BitEncodedInt64(this BinaryWriter binaryWriter, long value) { ulong num = (ulong)value; while (num >= 0x80) { binaryWriter.Write((byte)(num | 0x80)); num >>= 7; } binaryWriter.Write((byte)num); } /// /// 从二进制流读取编码后的 64 位无符号整数。 /// /// 要读取的二进制流。 /// 读取的 64 位无符号整数。 public static ulong Read7BitEncodedUInt64(this BinaryReader binaryReader) { return (ulong)Read7BitEncodedInt64(binaryReader); } /// /// 向二进制流写入编码后的 64 位无符号整数。 /// /// 要写入的二进制流。 /// 要写入的 64 位无符号整数。 public static void Write7BitEncodedUInt64(this BinaryWriter binaryWriter, ulong value) { Write7BitEncodedInt64(binaryWriter, (long)value); } /// /// 从二进制流读取加密字符串。 /// /// 要读取的二进制流。 /// 密钥数组。 /// 读取的字符串。 public static string ReadEncryptedString(this BinaryReader binaryReader, byte[] encryptBytes) { byte length = binaryReader.ReadByte(); if (length <= 0) { return null; } if (length > byte.MaxValue) { throw new GameFrameworkException("String is too long."); } for (byte i = 0; i < length; i++) { s_CachedBytes[i] = binaryReader.ReadByte(); } Utility.Encryption.GetSelfXorBytes(s_CachedBytes, 0, length, encryptBytes); string value = Utility.Converter.GetString(s_CachedBytes, 0, length); Array.Clear(s_CachedBytes, 0, length); return value; } /// /// 向二进制流写入加密字符串。 /// /// 要写入的二进制流。 /// 要写入的字符串。 /// 密钥数组。 public static void WriteEncryptedString(this BinaryWriter binaryWriter, string value, byte[] encryptBytes) { if (string.IsNullOrEmpty(value)) { binaryWriter.Write((byte)0); return; } int length = Utility.Converter.GetBytes(value, s_CachedBytes); if (length > byte.MaxValue) { throw new GameFrameworkException(Utility.Text.Format("String '{0}' is too long.", value)); } Utility.Encryption.GetSelfXorBytes(s_CachedBytes, encryptBytes); binaryWriter.Write((byte)length); binaryWriter.Write(s_CachedBytes, 0, length); } }