private static byte[] LoadZeroCompressed(BinaryReader reader) { var ret = new List <byte>(); uint packedLen = reader.ReadSizeEx(); long max = reader.BaseStream.Position + packedLen; while (reader.BaseStream.Position < max) { var opcode = new ZeroCompressOpcode(reader.ReadByte()); if (opcode.FirstIsZero) { for (int n = 0; n < (opcode.FirstLength + 1); n++) { ret.Add(0x00); } } else { int bytes = (int)Math.Min(8 - opcode.FirstLength, max - reader.BaseStream.Position); for (int n = 0; n < bytes; n++) { ret.Add(reader.ReadByte()); } } if (opcode.SecondIsZero) { for (int n = 0; n < (opcode.SecondLength + 1); n++) { ret.Add(0x00); } } else { int bytes = (int)Math.Min(8 - opcode.SecondLength, max - reader.BaseStream.Position); for (int n = 0; n < bytes; n++) { ret.Add(reader.ReadByte()); } } } return(ret.ToArray()); }
public static byte[] LoadZeroCompressed(BinaryReader reader) { MemoryStream retStream = new MemoryStream(); BinaryWriter ret = new BinaryWriter(retStream); uint packedLen = reader.ReadSizeEx(); long max = reader.BaseStream.Position + packedLen; while (reader.BaseStream.Position < max) { var opcode = new ZeroCompressOpcode(reader.ReadByte()); if (opcode.FirstIsZero) { ret.Write(new byte[opcode.FirstLength + 1]); // Faster than whiles } else { int bytes = (int)Math.Min(8 - opcode.FirstLength, max - reader.BaseStream.Position); for (int n = 0; n < bytes; n++) { ret.Write(reader.ReadByte()); } } if (opcode.SecondIsZero) { ret.Write(new byte[opcode.SecondLength + 1]); // Faster than whiles } else { int bytes = (int)Math.Min(8 - opcode.SecondLength, max - reader.BaseStream.Position); for (int n = 0; n < bytes; n++) { ret.Write(reader.ReadByte()); } } } return(retStream.ToArray()); }
public static byte[] LoadZeroCompressed(BinaryReader reader) { MemoryStream retStream = new MemoryStream(); BinaryWriter ret = new BinaryWriter(retStream); uint packedLen = reader.ReadSizeEx(); long max = reader.BaseStream.Position + packedLen; while (reader.BaseStream.Position < max) { var opcode = new ZeroCompressOpcode(reader.ReadByte()); if (opcode.FirstIsZero) { ret.Write(new byte[opcode.FirstLength + 1]); // Faster than whiles } else { int bytes = (int)Math.Min(8 - opcode.FirstLength, max - reader.BaseStream.Position); for (int n = 0; n < bytes; n++) ret.Write(reader.ReadByte()); } if (opcode.SecondIsZero) { ret.Write(new byte[opcode.SecondLength + 1]); // Faster than whiles } else { int bytes = (int)Math.Min(8 - opcode.SecondLength, max - reader.BaseStream.Position); for (int n = 0; n < bytes; n++) ret.Write(reader.ReadByte()); } } return retStream.ToArray(); }
public static void ZeroCompress(BinaryReader reader, MemoryStream stream, BinaryWriter output) { MemoryStream newStream = new MemoryStream(); BinaryWriter newWriter = new BinaryWriter(newStream); byte b = reader.ReadByte(); while(stream.Position < stream.Length) { ZeroCompressOpcode opcode = new ZeroCompressOpcode(0); int opcodeStartShift = 1; // Reserve space for opcode newWriter.Write(opcode.Value); if (b == 0x00) { opcode.FirstIsZero = true; int firstLen = -1; while ((b == 0x00) && (firstLen < 7) && (stream.Position < stream.Length)) { firstLen++; b = reader.ReadByte(); } opcode.FirstLength = (byte)(firstLen); } else { opcode.FirstIsZero = false; opcode.FirstLength = 8; while ((b != 0x00) && (opcode.FirstLength > 0)) { opcode.FirstLength--; opcodeStartShift++; newWriter.Write(b); if (stream.Position < stream.Length) { b = reader.ReadByte(); } else { break; } } } if (stream.Position == stream.Length) { opcode.SecondIsZero = true; opcode.SecondLength = 0; } else if (b == 0x00) { opcode.SecondIsZero = true; int secondLength = -1; while ((b == 0x00) && (opcode.SecondLength < 7) && (stream.Position < stream.Length)) { secondLength++; b = reader.ReadByte(); } opcode.SecondLength = (byte)(secondLength); } else { opcode.SecondIsZero = false; opcode.SecondLength = 8; while ((b != 0x00) && (opcode.SecondLength > 0)) { opcode.SecondLength--; opcodeStartShift++; newWriter.Write(b); if (stream.Position < stream.Length) { b = reader.ReadByte(); } else { break; } } } newWriter.Seek(-opcodeStartShift, SeekOrigin.Current); newWriter.Write(opcode.Value); newWriter.Seek(opcodeStartShift - 1, SeekOrigin.Current); } output.WriteSizeEx((int)(newStream.Length)); if(newStream.Length > 0) { output.Write(newStream.ToArray()); } }
public static void ZeroCompress(BinaryReader reader, MemoryStream stream, BinaryWriter output) { MemoryStream newStream = new MemoryStream(); BinaryWriter newWriter = new BinaryWriter(newStream); byte b = reader.ReadByte(); while (stream.Position < stream.Length) { ZeroCompressOpcode opcode = new ZeroCompressOpcode(0); int opcodeStartShift = 1; // Reserve space for opcode newWriter.Write(opcode.Value); if (b == 0x00) { opcode.FirstIsZero = true; int firstLen = -1; while ((b == 0x00) && (firstLen < 7) && (stream.Position < stream.Length)) { firstLen++; b = reader.ReadByte(); } opcode.FirstLength = (byte)(firstLen); } else { opcode.FirstIsZero = false; opcode.FirstLength = 8; while ((b != 0x00) && (opcode.FirstLength > 0)) { opcode.FirstLength--; opcodeStartShift++; newWriter.Write(b); if (stream.Position < stream.Length) { b = reader.ReadByte(); } else { break; } } } if (stream.Position == stream.Length) { opcode.SecondIsZero = true; opcode.SecondLength = 0; } else if (b == 0x00) { opcode.SecondIsZero = true; int secondLength = -1; while ((b == 0x00) && (opcode.SecondLength < 7) && (stream.Position < stream.Length)) { secondLength++; b = reader.ReadByte(); } opcode.SecondLength = (byte)(secondLength); } else { opcode.SecondIsZero = false; opcode.SecondLength = 8; while ((b != 0x00) && (opcode.SecondLength > 0)) { opcode.SecondLength--; opcodeStartShift++; newWriter.Write(b); if (stream.Position < stream.Length) { b = reader.ReadByte(); } else { break; } } } newWriter.Seek(-opcodeStartShift, SeekOrigin.Current); newWriter.Write(opcode.Value); newWriter.Seek(opcodeStartShift - 1, SeekOrigin.Current); } output.WriteSizeEx((int)(newStream.Length)); if (newStream.Length > 0) { output.Write(newStream.ToArray()); } }
public static byte[] LoadZeroCompressed(BinaryReader reader) { var ret = new List <byte>(); uint packedLen = reader.ReadSizeEx(); long max = reader.BaseStream.Position + packedLen; while (reader.BaseStream.Position < max) { var opcode = new ZeroCompressOpcode(reader.ReadByte()); if (opcode.FirstIsZero) { for (int n = 0; n < (opcode.FirstLength + 1); n++) { ret.Add(0x00); } } else { int bytes = (int)(Math.Min(8 - opcode.FirstLength, max - reader.BaseStream.Position)); for (int n = 0; n < bytes; n++) { ret.Add(reader.ReadByte()); } } if (opcode.SecondIsZero) { for (int n = 0; n < (opcode.SecondLength + 1); n++) { ret.Add(0x00); } } else { int bytes = (int)(Math.Min(8 - opcode.SecondLength, max - reader.BaseStream.Position)); for (int n = 0; n < bytes; n++) { ret.Add(reader.ReadByte()); } } } return(ret.ToArray()); /*MemoryStream retStream = new MemoryStream(); * BinaryWriter ret = new BinaryWriter(retStream); * * uint packedLen = reader.ReadSizeEx(); * long max = reader.BaseStream.Position + packedLen; * * while (reader.BaseStream.Position < max) * { * var opcode = new ZeroCompressOpcode(reader.ReadByte()); * * if (opcode.FirstIsZero) * { * byte len = (byte)(opcode.FirstLength + 1); * while (0 < len--) * ret.Write((byte)(0x00)); * } * else * { * int bytes = (int)Math.Min(8 - opcode.FirstLength, max - reader.BaseStream.Position); * for (int n = 0; n < bytes; n++) * ret.Write(reader.ReadByte()); * } * * if (opcode.SecondIsZero) * { * byte len = (byte)(opcode.SecondLength + 1); * while (0 < len--) * ret.Write((byte)(0x00)); * } * else * { * int bytes = (int)Math.Min(8 - opcode.SecondLength, max - reader.BaseStream.Position); * for (int n = 0; n < bytes; n++) * ret.Write(reader.ReadByte()); * } * } * * return retStream.ToArray();*/ }
public static byte[] LoadZeroCompressed(BinaryReader reader) { var ret = new List<byte>(); uint packedLen = reader.ReadSizeEx(); long max = reader.BaseStream.Position + packedLen; while (reader.BaseStream.Position < max) { var opcode = new ZeroCompressOpcode(reader.ReadByte()); if (opcode.FirstIsZero) { for (int n = 0; n < (opcode.FirstLength + 1); n++) ret.Add(0x00); } else { int bytes = (int)(Math.Min(8 - opcode.FirstLength, max - reader.BaseStream.Position)); for(int n = 0; n < bytes; n ++) ret.Add(reader.ReadByte()); } if(opcode.SecondIsZero) { for(int n = 0; n < (opcode.SecondLength + 1); n ++) ret.Add(0x00); } else { int bytes = (int)(Math.Min(8 - opcode.SecondLength, max - reader.BaseStream.Position)); for(int n = 0; n < bytes; n ++) ret.Add(reader.ReadByte()); } } return ret.ToArray(); /*MemoryStream retStream = new MemoryStream(); BinaryWriter ret = new BinaryWriter(retStream); uint packedLen = reader.ReadSizeEx(); long max = reader.BaseStream.Position + packedLen; while (reader.BaseStream.Position < max) { var opcode = new ZeroCompressOpcode(reader.ReadByte()); if (opcode.FirstIsZero) { byte len = (byte)(opcode.FirstLength + 1); while (0 < len--) ret.Write((byte)(0x00)); } else { int bytes = (int)Math.Min(8 - opcode.FirstLength, max - reader.BaseStream.Position); for (int n = 0; n < bytes; n++) ret.Write(reader.ReadByte()); } if (opcode.SecondIsZero) { byte len = (byte)(opcode.SecondLength + 1); while (0 < len--) ret.Write((byte)(0x00)); } else { int bytes = (int)Math.Min(8 - opcode.SecondLength, max - reader.BaseStream.Position); for (int n = 0; n < bytes; n++) ret.Write(reader.ReadByte()); } } return retStream.ToArray();*/ }