Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Collections.Generic;
- namespace KKt
- {
- public class IO
- {
- public unsafe class KKtIO
- {
- protected Stream Stream;
- protected byte CurrentBitReader = 0;
- protected byte CurrentValReader = 0;
- protected byte CurrentBitWriter = 0;
- protected byte CurrentValWriter = 0;
- protected bool HalfByteReader = false;
- protected bool HalfByteWriter = false;
- private readonly byte[] buf = new byte[16];
- public Main.Format format = Main.Format.F;
- public bool IsBE = false;
- public bool IsX = false;
- public int I = 0;
- public long Length => Stream.Length;
- public long Position => Stream.Position;
- public bool CanRead => Stream.CanRead;
- public bool CanSeek => Stream.CanSeek;
- public bool CanTimeout => Stream.CanTimeout;
- public bool CanWrite => Stream.CanWrite;
- protected KKtIO()
- {
- Stream = Stream.Null;
- CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
- HalfByteReader = HalfByteWriter = false;
- Stream = Stream.Null;
- buf = new byte[16];
- I = 0;
- IsBE = false;
- }
- public KKtIO(Stream output)
- {
- Stream = Stream.Null;
- CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
- HalfByteReader = HalfByteWriter = false;
- Stream = output;
- buf = new byte[16];
- I = 0;
- IsBE = false;
- }
- public KKtIO(Stream output, bool isBE)
- {
- Stream = Stream.Null;
- CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
- HalfByteReader = HalfByteWriter = false;
- Stream = output;
- buf = new byte[16];
- I = 0;
- IsBE = isBE;
- }
- public KKtIO(Stream output, int i)
- {
- Stream = Stream.Null;
- CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
- HalfByteReader = HalfByteWriter = false;
- Stream = output;
- buf = new byte[16];
- I = i;
- IsBE = false;
- }
- public KKtIO(Stream output, int i, bool isBE)
- {
- Stream = Stream.Null;
- CurrentBitReader = CurrentValReader = CurrentBitWriter = CurrentValWriter = 0;
- HalfByteReader = HalfByteWriter = false;
- Stream = output;
- buf = new byte[16];
- I = i;
- IsBE = isBE;
- }
- public void Close()
- { CheckWrited(); Dispose(true); }
- protected void Dispose(bool disposing)
- { if (disposing) Stream.Flush(); }
- public void Dispose()
- { CheckWrited(); Dispose(true); }
- public Stream BaseStream
- {
- get
- {
- Flush();
- return Stream;
- }
- }
- public void Flush()
- { Stream.Flush(); }
- public long Seek(long offset, SeekOrigin origin)
- { return Stream.Seek(offset, origin); }
- public void SetLength(long length)
- { Stream.SetLength(length); }
- public void Align(long Align)
- {
- long Position = Stream.Position;
- long Al = Align - Position % Align;
- if (Position % Align != 0)
- Stream.Seek(Stream.Position + Al, 0);
- }
- public bool ReadBoolean()
- { Read(1); return buf[0] == 1; }
- public byte ReadByte()
- { Read(1); return buf[0]; }
- public sbyte ReadSByte()
- { Read(1); return ( sbyte) buf[0]; }
- public sbyte ReadInt8()
- { Read(1); return ( sbyte) buf[0]; }
- public byte ReadUInt8()
- { Read(1); return buf[0]; }
- public short ReadInt16()
- { Read(2); return (short) ((buf[1] << 8) | buf[0]); }
- public ushort ReadUInt16()
- { Read(2); return (ushort) ((buf[1] << 8) | buf[0]); }
- public int ReadInt24()
- { Read(2); return (((buf[2] << 8) | buf[1]) << 8) | buf[0]; }
- public uint ReadUInt24()
- { Read(2); return ( uint) ((((buf[2] << 8) | buf[1]) << 8) | buf[0]); }
- public int ReadInt32()
- { Read(4); return (((((buf[3] << 8) | buf[2]) << 8) | buf[1]) << 8) | buf[0]; }
- public uint ReadUInt32()
- { Read(4); return ( uint)((((((buf[3] << 8) | buf[2]) << 8) | buf[1]) << 8) | buf[0]); }
- public long ReadInt64()
- { uint a = ReadUInt32(); uint b = ReadUInt32(); return (( long)b << 32) | a; }
- public ulong ReadUInt64()
- { uint a = ReadUInt32(); uint b = ReadUInt32(); return ((ulong)b << 32) | a; }
- public double ReadHalf()
- { ushort a = ReadUInt16(); return Main.ToDouble(a); }
- public float ReadSingle()
- { uint a = ReadUInt32(); return *( float*)&a; }
- public double ReadDouble()
- { ulong a = ReadUInt64(); return *(double*)&a; }
- public short ReadInt16(bool Swap)
- { return Endian(ReadInt16()); }
- public ushort ReadUInt16(bool Swap)
- { return Endian(ReadUInt16()); }
- public int ReadInt24(bool Swap)
- { return Endian(ReadInt24()) >> 8; }
- public uint ReadUInt24(bool Swap)
- { return Endian(ReadUInt24()) >> 8; }
- public int ReadInt32(bool Swap)
- { return Endian(ReadInt32()); }
- public uint ReadUInt32(bool Swap)
- { return Endian(ReadUInt32()); }
- public long ReadInt64(bool Swap)
- { return Endian(ReadInt64()); }
- public ulong ReadUInt64(bool Swap)
- { return Endian(ReadUInt64()); }
- public float ReadSingle(bool Swap)
- { uint a = Endian(ReadUInt32()); return *(float*)&a; }
- public short ReadInt16(bool Swap, bool IsBE)
- { return Endian( ReadInt16(), IsBE); }
- public ushort ReadUInt16(bool Swap, bool IsBE)
- { return Endian(ReadUInt16(), IsBE); }
- public int ReadInt24(bool Swap, bool IsBE)
- { return Endian( ReadInt24(), IsBE) >> 8; }
- public uint ReadUInt24(bool Swap, bool IsBE)
- { return Endian(ReadUInt24(), IsBE) >> 8; }
- public int ReadInt32(bool Swap, bool IsBE)
- { return Endian( ReadInt32(), IsBE); }
- public uint ReadUInt32(bool Swap, bool IsBE)
- { return Endian(ReadUInt32(), IsBE); }
- public long ReadInt64(bool Swap, bool IsBE)
- { return Endian( ReadInt64(), IsBE); }
- public ulong ReadUInt64(bool Swap, bool IsBE)
- { return Endian(ReadUInt64(), IsBE); }
- public float ReadSingle(bool Swap, bool IsBE)
- { uint a = Endian(ReadUInt32(), IsBE); return *( float*)&a; }
- public double ReadDouble(bool Swap, bool IsBE)
- { long a = Endian( ReadInt64(), IsBE); return *(double*)&a; }
- public void Write(byte[] Val)
- { Stream.Write(Val, 0, Val.Length); }
- public void Write(byte[] Val, int Length)
- { Stream.Write(Val, 0, Length); }
- public void Write(byte[] Val, int Offset, int Length)
- { Stream.Write(Val, Offset, Length); }
- public void WriteVal(byte Length)
- { Stream.Write(buf, 0, Length); }
- public void Write(char[] val)
- { Write(Encoding.UTF8.GetBytes(val)); }
- public void Write(char[] val, bool UTF8)
- { if (UTF8) Write(Encoding.UTF8.GetBytes(val)); else Write(Encoding.ASCII.GetBytes(val)); }
- public void Write( bool val)
- { ToArray(1, val ? 1 : 0); }
- public void Write( sbyte val)
- { ToArray(1, val); }
- public void Write( byte val)
- { ToArray(1, val); }
- public void Write( short val)
- { ToArray(2, val); }
- public void Write(ushort val)
- { ToArray(2, val); }
- public void Write( int val)
- { ToArray(4, val); }
- public void Write( uint val)
- { ToArray(4, val); }
- public void Write( long val)
- { ToArray(8, val); }
- public void Write( ulong val)
- { ToArray(8, val); }
- public void Write( float val)
- { ToArray(4, *( uint*)&val); }
- public void Write(double val)
- { ToArray(8, *(ulong*)&val); }
- public void Write(char val)
- { Write(new char[] { val }); }
- public void Write(char val, bool UTF8)
- { if (UTF8) Write(Encoding.UTF8.GetBytes(val.ToString())); else Write(Encoding.ASCII.GetBytes(val.ToString())); }
- public void Write(string val)
- { Write(Encoding.UTF8.GetBytes(val)); }
- public void Write(string val, bool UTF8)
- { if (UTF8) Write(Encoding.UTF8.GetBytes(val)); else Write(Encoding.ASCII.GetBytes(val)); }
- public void Write(string Data, string val)
- { if (val != null) if (val != "") if (val.Length > 0)
- Write(Encoding.UTF8.GetBytes(Data + val + "\n")); }
- public void Write( short val, bool Swap)
- { Write(Endian(val, IsBE)); }
- public void Write(ushort val, bool Swap)
- { Write(Endian(val, IsBE)); }
- public void Write( int val, bool Swap)
- { Write(Endian(val, IsBE)); }
- public void Write( uint val, bool Swap)
- { Write(Endian(val, IsBE)); }
- public void Write( long val, bool Swap)
- { Write(Endian(val, IsBE)); }
- public void Write( ulong val, bool Swap)
- { Write(Endian(val, IsBE)); }
- public void Write( float val, bool Swap)
- { Write(Endian(*( uint*)&val, IsBE)); }
- public void Write(double val, bool Swap)
- { Write(Endian(*(ulong*)&val, IsBE)); }
- public void Write( short val, bool Swap, bool IsBE)
- { Write( (short)Endian(val, 2, IsBE)); }
- public void Write(ushort val, bool Swap, bool IsBE)
- { Write((ushort)Endian(val, 2, IsBE)); }
- public void Write( int val, bool Swap, bool IsBE)
- { Write(( int)Endian(val, 4, IsBE)); }
- public void Write( uint val, bool Swap, bool IsBE)
- { Write(( uint)Endian(val, 4, IsBE)); }
- public void Write( long val, bool Swap, bool IsBE)
- { Write(( long)Endian(val, 8, IsBE)); }
- public void Write( ulong val, bool Swap, bool IsBE)
- { Write(( ulong)Endian(val, 8, IsBE)); }
- public void Write( float val, bool Swap, bool IsBE)
- { Write(( uint)Endian(*( uint*)&val, 4, IsBE)); }
- public void Write(double val, bool Swap, bool IsBE)
- { Write(( ulong)Endian(*(ulong*)&val, 8, IsBE)); }
- public short Endian( short BE) { return ( short)Endian(BE, 2, IsBE); }
- public ushort Endian(ushort BE) { return (ushort)Endian(BE, 2, IsBE); }
- public int Endian( int BE) { return ( int)Endian(BE, 4, IsBE); }
- public uint Endian( uint BE) { return ( uint)Endian(BE, 4, IsBE); }
- public long Endian( long BE) { return ( long)Endian(BE, 8, IsBE); }
- public ulong Endian( ulong BE) { return ( ulong)Endian(BE, 8, IsBE); }
- public short Endian( short BE, bool IsBE) { return ( short)Endian(BE, 2, IsBE); }
- public ushort Endian(ushort BE, bool IsBE) { return (ushort)Endian(BE, 2, IsBE); }
- public int Endian( int BE, bool IsBE) { return ( int)Endian(BE, 4, IsBE); }
- public uint Endian( uint BE, bool IsBE) { return ( uint)Endian(BE, 4, IsBE); }
- public long Endian( long BE, bool IsBE) { return ( long)Endian(BE, 8, IsBE); }
- public ulong Endian( ulong BE, bool IsBE) { return ( ulong)Endian(BE, 8, IsBE); }
- public long Endian( long BE, byte Length, bool IsBE)
- {
- if (IsBE)
- {
- for (byte i = 0; i < Length; i++) { buf[i] = (byte)BE; BE >>= 8; }
- BE = 0; for (byte i = 0; i < Length; i++) { BE |= buf[i]; if (i < Length - 1) BE <<= 8; }
- }
- return BE;
- }
- public ulong Endian(ulong BE, byte Length, bool IsBE)
- {
- if (IsBE)
- {
- for (byte i = 0; i < Length; i++) { buf[i] = (byte)BE; BE >>= 8; }
- BE = 0; for (byte i = 0; i < Length; i++) { BE |= buf[i]; if (i < Length - 1) BE <<= 8; }
- }
- return BE;
- }
- public void ToArray(byte Length, long val)
- {
- for (byte i = 0; i < Length; i++) { buf[i] = (byte)val; val >>= 8; }
- WriteVal(Length);
- }
- public void ToArray(byte Length, ulong val)
- {
- for (byte i = 0; i < Length; i++) { buf[i] = (byte)val; val >>= 8; }
- WriteVal(Length);
- }
- public void Read(byte Length)
- { Stream.Read(buf, 0, Length); }
- public byte[] Read(int Offset, int Length)
- { byte[] Val = new byte[Length]; Stream.Read(Val, Offset, Length); return Val; }
- public string ReadString(long Length)
- { return Encoding.UTF8.GetString(ReadBytes(Length)); }
- public string ReadString(long Length, bool UTF8)
- { if (UTF8) return Encoding. UTF8.GetString(ReadBytes(Length));
- else return Encoding.ASCII.GetString(ReadBytes(Length)); }
- public string ReadStringUTF8 (long Length)
- { return Encoding. UTF8.GetString(ReadBytes(Length)); }
- public string ReadStringASCII(long Length)
- { return Encoding.ASCII.GetString(ReadBytes(Length)); }
- public byte[] ReadBytes(long Length)
- { byte[] Buf = new byte[Length]; Stream.Read(Buf, 0, (int)Length); return Buf; }
- public long ReadBits(byte Bits)
- {
- long val = 0;
- for (byte i = 0; i < Bits; i++)
- {
- val <<= 1;
- val |= ReadBit();
- }
- return val;
- }
- public byte ReadBit()
- {
- if (CurrentBitReader >= 8)
- {
- CurrentValReader = ReadByte();
- CurrentBitReader = 0;
- }
- CurrentBitReader++;
- return (byte)((CurrentValReader >> (8 - CurrentBitReader)) & 0x1);
- }
- public void Write(long val, byte Bits)
- {
- byte Val = 0;
- for (byte i = 0; i < Bits; i++)
- {
- Val = (byte)((val >> (byte)(Bits - 1 - i)) & 0x1);
- Write(Val, 0);
- }
- }
- public void Write(byte val, byte Bit)
- {
- CurrentValWriter |= (byte)(val << (7 - CurrentBitWriter));
- CurrentBitWriter++;
- if (CurrentBitWriter > 7)
- {
- Write(CurrentValWriter);
- CurrentValWriter = 0;
- CurrentBitWriter = 0;
- }
- }
- public void CheckWrited()
- {
- if (CurrentValWriter != 0)
- {
- Write(CurrentValWriter);
- CurrentValWriter = 0;
- CurrentBitWriter = 0;
- }
- else if (CurrentBitWriter != 0)
- {
- CurrentValWriter = 0;
- CurrentBitWriter = 0;
- }
- }
- public byte ReadHalfByte()
- {
- Read(1);
- if (HalfByteReader)
- {
- HalfByteReader = false;
- return (byte)(buf[0] & 0x0F);
- }
- else
- {
- HalfByteReader = true;
- Stream.Seek(-1, SeekOrigin.Current);
- return (byte)((buf[0] >> 4) & 0x0F);
- }
- }
- public long ReadHalfByte(int Count)
- {
- long val = 0;
- for (byte i = 0; i < Count; i++)
- {
- val <<= 4;
- val |= ReadHalfByte();
- }
- return val;
- }
- public void Write(byte val, bool HalfByte)
- {
- val = (byte)(val & 0x0F);
- if (HalfByteWriter)
- {
- val = (byte)((Stream.ReadByte()) & 0xF0 | val);
- Stream.Seek(-1, SeekOrigin.Current);
- HalfByteWriter = false;
- }
- else
- {
- val <<= 4;
- HalfByteWriter = true;
- }
- Write(val);
- if (HalfByteWriter)
- Stream.Seek(-1, SeekOrigin.Current);
- }
- public void Write(long val, bool HalfByte, int Count)
- {
- byte Val = 0;
- for (byte i = 0; i < Count; i++)
- {
- Val = (byte)((val >> (byte)((Count - 1 - i) * 4)) & 0xF);
- Write(Val, HalfByte);
- }
- }
- public static KKtIO OpenFileReader(string file)
- {
- KKtIO IO = new KKtIO(new FileStream(file, FileMode.Open , FileAccess.ReadWrite, FileShare.ReadWrite));
- return IO;
- }
- public static KKtIO OpenFileWriter(string file)
- {
- KKtIO IO = new KKtIO(new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite));
- return IO;
- }
- public static KKtIO OpenFileWriter(string file, bool SetLength0)
- {
- KKtIO IO = new KKtIO(new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite));
- IO.SetLength(0);
- return IO;
- }
- public static KKtIO OpenFileWriter(string file, int SetLength)
- {
- KKtIO IO = new KKtIO(new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite));
- IO.SetLength(SetLength);
- return IO;
- }
- public string NullTerminated(byte End)
- {
- string s = "";
- while (true)
- {
- byte a = ReadByte();
- if (a == End)
- break;
- else
- s += Convert.ToChar(a);
- }
- return s;
- }
- public List<byte> NullTerminated(bool Byte)
- {
- List<byte> s = new List<byte>();
- while (true)
- {
- byte a = ReadByte();
- if (a == 0x00)
- break;
- else
- s.Add(a);
- }
- return s;
- }
- public string NullTerminated(bool ReadOffset, ref Main.POF0 POF0)
- {
- GetOffset(ref POF0);
- string s = "";
- long CurrentOffset = Position;
- if (IsX)
- Seek(ReadInt64() + 0x20, 0);
- else
- Seek(ReadUInt32(true), 0);
- s = NullTerminated(0x00);
- Seek(CurrentOffset + (IsX ? 8 : 4), 0);
- return s;
- }
- public byte[] NullTerminated(bool ReadOffset, ref Main.POF0 POF0, bool Byte)
- {
- GetOffset(ref POF0);
- long CurrentOffset = Position;
- if (IsX)
- Seek(ReadInt64() + 0x20, 0);
- else
- Seek(ReadUInt32(true), 0);
- List<byte> S = NullTerminated(Byte);
- byte[] s = new byte[S.Count];
- for (int i = 0; i < S.Count; i++)
- s[i] = S[i];
- Seek(CurrentOffset + (IsX ? 8 : 4), 0);
- return s;
- }
- public long GetOffset(ref Main.POF0 POF0)
- {
- if ((byte)format > 1 && (byte)format < 5)
- {
- POF0.POF0Offsets.Add(Position - (IsX ? 0x20 : 0x00));
- return Position;
- }
- return 0;
- }
- public Main.Header HeaderReader()
- {
- if (Position >= 4)
- Seek(-4, SeekOrigin.Current);
- else
- Seek(0, 0);
- Main.Header Header = new Main.Header();
- Header.Format = format = Main.Format.F2LE;
- Header.Signature = ReadInt32();
- Header.DataSize = ReadInt32();
- Header.Lenght = ReadInt32();
- if (ReadUInt32() == 0x18000000)
- {
- IsBE = Header.IsBE = true;
- Header.Format = format = Main.Format.F2BE;
- }
- ReadUInt32();
- Header.SectionSize = ReadInt32();
- IsBE = Header.IsBE;
- Seek(Header.Lenght, 0);
- Header.Signature = ReadInt32(true);
- return Header;
- }
- public void EOFCWriter()
- {
- Write(Encoding.ASCII.GetBytes("EOFC"));
- Write(0x00);
- Write(0x20);
- Write(0x10000000);
- Write(0x00);
- Write(0x00);
- Write(0x00);
- Write(0x00);
- }
- public bool WAVHeadReader(out Main.WAVHeader Header, out short Bytes, out short Format)
- {
- Header = new Main.WAVHeader();
- Bytes = 0;
- Format = 0;
- if (ReadString(4) != "RIFF")
- return false;
- ReadUInt32();
- if (ReadString(4) != "WAVE")
- return false;
- if (ReadString(4) != "fmt ")
- return false;
- int Offset = ReadInt32();
- Format = ReadInt16();
- if (Format != 0x01 && Format != 0x03)
- return false;
- Header.Channels = ReadInt16();
- Header.SampleRate = ReadInt32();
- ReadInt32();
- ReadInt16();
- Bytes = (short)(ReadInt16() / 0x08);
- if ((Bytes < 2 || Bytes > 4) && Bytes != 8)
- return false;
- Seek(Offset + 0x14, 0);
- if (ReadString(4) != "data")
- return false;
- Header.Size = ReadInt32();
- Header.HeaderSize = (int)Position;
- return true;
- }
- public void WAVHeadWriter(Main.WAVHeader Header, short Bytes)
- {
- Seek(0, 0);
- Write("RIFF");
- Write(Header.Size + 0x26);
- Write("WAVE");
- Write("fmt ");
- Write(0x10);
- Write((short)(Bytes == 2 ? 0x01 : 0x03));
- Write((short)Header.Channels);
- Write(Header.SampleRate);
- Write(Header.SampleRate * Header.Channels * Bytes);
- Write((short)(Header.Channels * Bytes));
- Write((short)(0x08 * Bytes));
- Write("data");
- Write(Header.Size);
- }
- public void POF0Reader(ref Main.POF0 POF0)
- {
- if (ReadString(3).StartsWith("POF"))
- {
- POF0.POF0Offsets = POF0.POF0Offsets.Distinct().OrderBy(x => x).ToList();
- POF0.Header = HeaderReader();
- Seek(POF0.Offset + POF0.Header.Lenght, 0);
- long position = Position;
- POF0.Lenght = ReadInt32();
- while (POF0.Lenght + POF0.Offset + POF0.Header.Lenght > position)
- {
- int a = ReadByte();
- if (a >> 6 == 0)
- break;
- else if (a >> 6 == 1)
- a = a & 0x3F;
- else if (a >> 6 == 2)
- {
- a = a & 0x3F;
- a = (a << 8) | ReadByte();
- }
- else if (a >> 6 == 3)
- {
- a = a & 0x3F;
- a = (a << 8) | ReadByte();
- a = (a << 8) | ReadByte();
- a = (a << 8) | ReadByte();
- }
- a <<= IsX ? 3 : 2;
- POF0.LastOffset += a;
- POF0.Offsets.Add(POF0.LastOffset);
- position = Position;
- }
- for (int i = 0; i < POF0.Offsets.Count; i++)
- if (POF0.Offsets[i] != POF0.POF0Offsets[i])
- Console.WriteLine("Not right POF{0} offset table.\n Expected: {1}\n" +
- " Got: {2}", POF0.Type, POF0.Offsets[i], POF0.POF0Offsets[i]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement