diff --git a/.gitignore b/.gitignore index f45a502..1026bbe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,111 +1,111 @@ -# Build Folders (you can keep bin if you'd like, to store dlls and pdbs) -[Bb]in/ -[Oo]bj/ - -# mstest test results -TestResults - -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. - -# User-specific files -*.suo -*.user -*.sln.docstates - -# Build results -[Dd]ebug/ -[Rr]elease/ -x64/ -*_i.c -*_p.c -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.log -*.vspscc -*.vssscc -.builds - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opensdf -*.sdf - -# Visual Studio profiler -*.psess -*.vsp -*.vspx - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper* - -# NCrunch -*.ncrunch* -.*crunch*.local.xml - -# Installshield output folder -[Ee]xpress - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish - -# Publish Web Output -*.Publish.xml - -# NuGet Packages Directory -packages - -# Windows Azure Build Output -csx -*.build.csdef - -# Windows Store app package directory -AppPackages/ - -# Others -[Bb]in -[Oo]bj -sql -TestResults -[Tt]est[Rr]esult* -*.Cache -ClientBin -[Ss]tyle[Cc]op.* -~$* -*.dbmdl -Generated_Code #added for RIA/Silverlight projects - -# Backup & report files from converting an old project file to a newer -# Visual Studio version. Backup files are not needed, because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -Thumbs.db -.DS_Store - +# Build Folders (you can keep bin if you'd like, to store dlls and pdbs) +[Bb]in/ +[Oo]bj/ + +# mstest test results +TestResults + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +x64/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.log +*.vspscc +*.vssscc +.builds + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper* + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Publish Web Output +*.Publish.xml + +# NuGet Packages Directory +packages + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +[Bb]in +[Oo]bj +sql +TestResults +[Tt]est[Rr]esult* +*.Cache +ClientBin +[Ss]tyle[Cc]op.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +Thumbs.db +.DS_Store + diff --git a/AnimCmd/App.config b/AnimCmd/App.config index 74ade9d..2c307fa 100644 --- a/AnimCmd/App.config +++ b/AnimCmd/App.config @@ -1,6 +1,6 @@ - - - - - - + + + + + + diff --git a/AnimCmd/Classes/Mtable.cs b/AnimCmd/Classes/Mtable.cs index 98f4e3c..d9bbd38 100644 --- a/AnimCmd/Classes/Mtable.cs +++ b/AnimCmd/Classes/Mtable.cs @@ -1,120 +1,120 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Collections; -using System.IO; - -namespace Sm4shCommand.Classes -{ - /// - /// Animation name CRC table. - /// - public unsafe class MTable : IEnumerable - { - public Endianness _endian; - private List _baseList = new List(); - public uint this[int i] - { - get { return _baseList[i]; } - set { _baseList[i] = value; } - } - public int Count { get { return _baseList.Count; } } - public MTable(List CRCTable, Endianness endian) - { - _endian = endian; - _baseList = CRCTable; - } - public MTable() { } - - public void Export(string path) - { - byte[] mtable = new byte[_baseList.Count * 4]; - int p = 0; - foreach (uint val in _baseList) - { - byte[] tmp = BitConverter.GetBytes(val); - if (_endian == Endianness.Big) - Array.Reverse(tmp); - - for (int i = 0; i < 4; i++) - mtable[p + i] = tmp[i]; - p += 4; - } - - File.WriteAllBytes(path, mtable); - } - public void Clear() - { - _baseList = new List(); - } - public void Add(uint var) - { - _baseList.Add(var); - } - public void Remove(uint var) - { - _baseList.Remove(var); - } - public void Remove(int index) - { - _baseList.RemoveAt(index); - } - public int IndexOf(uint var) - { - return _baseList.IndexOf(var); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return (IEnumerator)GetEnumerator(); - } - public MTableEnumerator GetEnumerator() - { - return new MTableEnumerator(_baseList.ToArray()); - } - public class MTableEnumerator : IEnumerator - { - public uint[] _data; - int position = -1; - public MTableEnumerator(uint[] data) - { - _data = data; - } - - public bool MoveNext() - { - position++; - return (position < _data.Length); - } - - public void Reset() - { - position = -1; - } - - object IEnumerator.Current - { - get - { - return Current; - } - } - - public uint Current - { - get - { - try - { - return _data[position]; - } - catch (IndexOutOfRangeException) - { - throw new InvalidOperationException(); - } - } - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Collections; +using System.IO; + +namespace Sm4shCommand.Classes +{ + /// + /// Animation name CRC table. + /// + public unsafe class MTable : IEnumerable + { + public Endianness _endian; + private List _baseList = new List(); + public uint this[int i] + { + get { return _baseList[i]; } + set { _baseList[i] = value; } + } + public int Count { get { return _baseList.Count; } } + public MTable(List CRCTable, Endianness endian) + { + _endian = endian; + _baseList = CRCTable; + } + public MTable() { } + + public void Export(string path) + { + byte[] mtable = new byte[_baseList.Count * 4]; + int p = 0; + foreach (uint val in _baseList) + { + byte[] tmp = BitConverter.GetBytes(val); + if (_endian == Endianness.Big) + Array.Reverse(tmp); + + for (int i = 0; i < 4; i++) + mtable[p + i] = tmp[i]; + p += 4; + } + + File.WriteAllBytes(path, mtable); + } + public void Clear() + { + _baseList = new List(); + } + public void Add(uint var) + { + _baseList.Add(var); + } + public void Remove(uint var) + { + _baseList.Remove(var); + } + public void Remove(int index) + { + _baseList.RemoveAt(index); + } + public int IndexOf(uint var) + { + return _baseList.IndexOf(var); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return (IEnumerator)GetEnumerator(); + } + public MTableEnumerator GetEnumerator() + { + return new MTableEnumerator(_baseList.ToArray()); + } + public class MTableEnumerator : IEnumerator + { + public uint[] _data; + int position = -1; + public MTableEnumerator(uint[] data) + { + _data = data; + } + + public bool MoveNext() + { + position++; + return (position < _data.Length); + } + + public void Reset() + { + position = -1; + } + + object IEnumerator.Current + { + get + { + return Current; + } + } + + public uint Current + { + get + { + try + { + return _data[position]; + } + catch (IndexOutOfRangeException) + { + throw new InvalidOperationException(); + } + } + } + } + } +} diff --git a/AnimCmd/Events.cfg b/AnimCmd/Events.cfg index 81647a8..cfee075 100644 --- a/AnimCmd/Events.cfg +++ b/AnimCmd/Events.cfg @@ -1344,7 +1344,7 @@ NONE NONE 606717F5 -Allow Interupt +Allow_Interupt NONE NONE Allows the move to be interupted with another after this command. diff --git a/AnimCmd/IO/DataSource.cs b/AnimCmd/IO/DataSource.cs index 47b429f..5aa7408 100644 --- a/AnimCmd/IO/DataSource.cs +++ b/AnimCmd/IO/DataSource.cs @@ -1,48 +1,48 @@ -//=========================================================================\\ -// Taken from BrawlLib's source code, credit goes to devs who worked on it.\\ -// (Kryal, Bero, BlackJax96, LibertyErnie, Sammi Husky) \\ -// My deepest apologies to anyone who i've missed \\ -//=========================================================================\\ -namespace System.IO -{ - public unsafe struct DataSource - { - public static readonly DataSource Empty = new DataSource(); - - public VoidPtr Address; - public int Length; - public FileMap Map; - - - public DataSource(VoidPtr addr, int len) - { - Address = addr; - Length = len; - Map = null; - } - public DataSource(FileMap map) - { - Address = map.Address; - Length = map.Length; - Map = map; - } - - public void Close() - { - if (Map != null) { Map.Dispose(); Map = null; } - Address = null; - Length = 0; - } - - public static bool operator ==(DataSource src1, DataSource src2) { return (src1.Address == src2.Address) && (src1.Length == src2.Length) && (src1.Map == src2.Map); } - public static bool operator !=(DataSource src1, DataSource src2) { return (src1.Address != src2.Address) || (src1.Length != src2.Length) || (src1.Map != src2.Map); } - public override bool Equals(object obj) - { - if (obj is DataSource) - return this == (DataSource)obj; - return base.Equals(obj); - } - public override int GetHashCode() { return base.GetHashCode(); } - } - -} +//=========================================================================\\ +// Taken from BrawlLib's source code, credit goes to devs who worked on it.\\ +// (Kryal, Bero, BlackJax96, LibertyErnie, Sammi Husky) \\ +// My deepest apologies to anyone who i've missed \\ +//=========================================================================\\ +namespace System.IO +{ + public unsafe struct DataSource + { + public static readonly DataSource Empty = new DataSource(); + + public VoidPtr Address; + public int Length; + public FileMap Map; + + + public DataSource(VoidPtr addr, int len) + { + Address = addr; + Length = len; + Map = null; + } + public DataSource(FileMap map) + { + Address = map.Address; + Length = map.Length; + Map = map; + } + + public void Close() + { + if (Map != null) { Map.Dispose(); Map = null; } + Address = null; + Length = 0; + } + + public static bool operator ==(DataSource src1, DataSource src2) { return (src1.Address == src2.Address) && (src1.Length == src2.Length) && (src1.Map == src2.Map); } + public static bool operator !=(DataSource src1, DataSource src2) { return (src1.Address != src2.Address) || (src1.Length != src2.Length) || (src1.Map != src2.Map); } + public override bool Equals(object obj) + { + if (obj is DataSource) + return this == (DataSource)obj; + return base.Equals(obj); + } + public override int GetHashCode() { return base.GetHashCode(); } + } + +} diff --git a/AnimCmd/IO/FileMap.cs b/AnimCmd/IO/FileMap.cs index 8590806..5925b64 100644 --- a/AnimCmd/IO/FileMap.cs +++ b/AnimCmd/IO/FileMap.cs @@ -1,182 +1,182 @@ -//=========================================================================\\ -// Taken from BrawlLib's source code, credit goes to devs who worked on it.\\ -// (Kryal, Bero, BlackJax96, LibertyErnie, Sammi Husky) \\ -// My deepest apologies to anyone who i've missed \\ -//=========================================================================\\ - -using System; -using System.Runtime.InteropServices; -using System.IO; -using System.IO.MemoryMappedFiles; - -namespace System.IO -{ - public abstract class FileMap : IDisposable - { - protected VoidPtr _addr; - protected int _length; - protected string _path; - protected FileStream _baseStream; - - public VoidPtr Address { get { return _addr; } } - public int Length { get { return _length; } set { _length = value; } } - public string FilePath { get { return _path; } } - - ~FileMap() { Dispose(); } - public virtual void Dispose() - { - if (_baseStream != null) - { - _baseStream.Close(); - _baseStream.Dispose(); - _baseStream = null; - } -//#if DEBUG -// Console.WriteLine("Closing file map: {0}", _path); -//#endif - GC.SuppressFinalize(this); - } - - public static FileMap FromFile(string path) { return FromFile(path, FileMapProtect.ReadWrite, 0, 0); } - public static FileMap FromFile(string path, FileMapProtect prot) { return FromFile(path, prot, 0, 0); } - public static FileMap FromFile(string path, FileMapProtect prot, int offset, int length) { return FromFile(path, prot, 0, 0, FileOptions.RandomAccess); } - public static FileMap FromFile(string path, FileMapProtect prot, int offset, int length, FileOptions options) - { - FileStream stream; - FileMap map; - try { stream = new FileStream(path, FileMode.Open, (prot == FileMapProtect.ReadWrite) ? FileAccess.ReadWrite : FileAccess.Read, FileShare.Read, 8, options); } - catch //File is currently in use, but we can copy it to a temp location and read that - { - string tempPath = Path.GetTempFileName(); - File.Copy(path, tempPath, true); - stream = new FileStream(tempPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 8, options | FileOptions.DeleteOnClose); - } - try { map = FromStreamInternal(stream, prot, offset, length); } - catch (Exception x) { stream.Dispose(); throw; } - map._path = path; //In case we're using a temp file - stream.Dispose(); - return map; - } - public static FileMap FromTempFile(int length) - { - FileStream stream = new FileStream(Path.GetTempFileName(), FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 8, FileOptions.RandomAccess | FileOptions.DeleteOnClose); - try { FileMap m = FromStreamInternal(stream, FileMapProtect.ReadWrite, 0, length); stream.Dispose(); return m; } - catch (Exception x) { stream.Dispose(); throw; } - } - - public static FileMap FromStream(FileStream stream) { return FromStream(stream, FileMapProtect.ReadWrite, 0, 0); } - public static FileMap FromStream(FileStream stream, FileMapProtect prot) { return FromStream(stream, prot, 0, 0); } - public static FileMap FromStream(FileStream stream, FileMapProtect prot, int offset, int length) - { - //FileStream newStream = new FileStream(stream.Name, FileMode.Open, prot == FileMapProtect.Read ? FileAccess.Read : FileAccess.ReadWrite, FileShare.Read, 8, FileOptions.RandomAccess); - //try { return FromStreamInternal(newStream, prot, offset, length); } - //catch (Exception x) { newStream.Dispose(); throw x; } - - if (length == 0) - length = (int)stream.Length; - - switch (Environment.OSVersion.Platform) - { - case PlatformID.Win32NT: - return new wFileMap(stream.SafeFileHandle.DangerousGetHandle(), prot, offset, (uint)length) { _path = stream.Name }; - default: - return new cFileMap(stream, prot, offset, length) { _path = stream.Name }; - } - -//#if DEBUG -// Console.WriteLine("Opening file map: {0}", stream.Name); -//#endif - } - - public static FileMap FromStreamInternal(FileStream stream, FileMapProtect prot, int offset, int length) - { - if (length == 0) - length = (int)stream.Length; - - switch (Environment.OSVersion.Platform) - { - case PlatformID.Win32NT: - return new wFileMap(stream.SafeFileHandle.DangerousGetHandle(), prot, offset, (uint)length) { _baseStream = stream, _path = stream.Name }; - default: - return new cFileMap(stream, prot, offset, length) { _baseStream = stream, _path = stream.Name }; - } - -//#if DEBUG -// Console.WriteLine("Opening file map: {0}", stream.Name); -//#endif - - } - - } - - public enum FileMapProtect : uint - { - Read = 0x01, - ReadWrite = 0x02 - } - - public class wFileMap : FileMap - { - internal wFileMap(VoidPtr hFile, FileMapProtect protect, long offset, uint length) - { - long maxSize = offset + length; - uint maxHigh = (uint)(maxSize >> 32); - uint maxLow = (uint)maxSize; - Win32._FileMapProtect mProtect; Win32._FileMapAccess mAccess; - if (protect == FileMapProtect.ReadWrite) - { - mProtect = Win32._FileMapProtect.ReadWrite; - mAccess = Win32._FileMapAccess.Write; - } - else - { - mProtect = Win32._FileMapProtect.ReadOnly; - mAccess = Win32._FileMapAccess.Read; - } - - using (Win32.SafeHandle h = Win32.CreateFileMapping(hFile, null, mProtect, maxHigh, maxLow, null)) - { - h.ErrorCheck(); - _addr = Win32.MapViewOfFile(h.Handle, mAccess, (uint)(offset >> 32), (uint)offset, length); - if (!_addr) Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - _length = (int)length; - } - } - - public override void Dispose() - { - if (_addr) - { - Win32.FlushViewOfFile(_addr, 0); - Win32.UnmapViewOfFile(_addr); - _addr = null; - } - base.Dispose(); - } - } - - - public unsafe class cFileMap : FileMap - { - protected MemoryMappedFile _mappedFile; - protected MemoryMappedViewAccessor _mappedFileAccessor; - - public cFileMap(FileStream stream, FileMapProtect protect, int offset, int length) - { - MemoryMappedFileAccess cProtect = (protect == FileMapProtect.ReadWrite) ? MemoryMappedFileAccess.ReadWrite : MemoryMappedFileAccess.Read; - _length = length; - _mappedFile = MemoryMappedFile.CreateFromFile(stream, stream.Name, _length, cProtect, null, HandleInheritability.None, true); - _mappedFileAccessor = _mappedFile.CreateViewAccessor(offset, _length, cProtect); - _addr = _mappedFileAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(); - } - - public override void Dispose() - { - if (_mappedFile != null) - _mappedFile.Dispose(); - if (_mappedFileAccessor != null) - _mappedFileAccessor.Dispose(); - base.Dispose(); - } - } -} +//=========================================================================\\ +// Taken from BrawlLib's source code, credit goes to devs who worked on it.\\ +// (Kryal, Bero, BlackJax96, LibertyErnie, Sammi Husky) \\ +// My deepest apologies to anyone who i've missed \\ +//=========================================================================\\ + +using System; +using System.Runtime.InteropServices; +using System.IO; +using System.IO.MemoryMappedFiles; + +namespace System.IO +{ + public abstract class FileMap : IDisposable + { + protected VoidPtr _addr; + protected int _length; + protected string _path; + protected FileStream _baseStream; + + public VoidPtr Address { get { return _addr; } } + public int Length { get { return _length; } set { _length = value; } } + public string FilePath { get { return _path; } } + + ~FileMap() { Dispose(); } + public virtual void Dispose() + { + if (_baseStream != null) + { + _baseStream.Close(); + _baseStream.Dispose(); + _baseStream = null; + } +//#if DEBUG +// Console.WriteLine("Closing file map: {0}", _path); +//#endif + GC.SuppressFinalize(this); + } + + public static FileMap FromFile(string path) { return FromFile(path, FileMapProtect.ReadWrite, 0, 0); } + public static FileMap FromFile(string path, FileMapProtect prot) { return FromFile(path, prot, 0, 0); } + public static FileMap FromFile(string path, FileMapProtect prot, int offset, int length) { return FromFile(path, prot, 0, 0, FileOptions.RandomAccess); } + public static FileMap FromFile(string path, FileMapProtect prot, int offset, int length, FileOptions options) + { + FileStream stream; + FileMap map; + try { stream = new FileStream(path, FileMode.Open, (prot == FileMapProtect.ReadWrite) ? FileAccess.ReadWrite : FileAccess.Read, FileShare.Read, 8, options); } + catch //File is currently in use, but we can copy it to a temp location and read that + { + string tempPath = Path.GetTempFileName(); + File.Copy(path, tempPath, true); + stream = new FileStream(tempPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 8, options | FileOptions.DeleteOnClose); + } + try { map = FromStreamInternal(stream, prot, offset, length); } + catch (Exception x) { stream.Dispose(); throw; } + map._path = path; //In case we're using a temp file + stream.Dispose(); + return map; + } + public static FileMap FromTempFile(int length) + { + FileStream stream = new FileStream(Path.GetTempFileName(), FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 8, FileOptions.RandomAccess | FileOptions.DeleteOnClose); + try { FileMap m = FromStreamInternal(stream, FileMapProtect.ReadWrite, 0, length); stream.Dispose(); return m; } + catch (Exception x) { stream.Dispose(); throw; } + } + + public static FileMap FromStream(FileStream stream) { return FromStream(stream, FileMapProtect.ReadWrite, 0, 0); } + public static FileMap FromStream(FileStream stream, FileMapProtect prot) { return FromStream(stream, prot, 0, 0); } + public static FileMap FromStream(FileStream stream, FileMapProtect prot, int offset, int length) + { + //FileStream newStream = new FileStream(stream.Name, FileMode.Open, prot == FileMapProtect.Read ? FileAccess.Read : FileAccess.ReadWrite, FileShare.Read, 8, FileOptions.RandomAccess); + //try { return FromStreamInternal(newStream, prot, offset, length); } + //catch (Exception x) { newStream.Dispose(); throw x; } + + if (length == 0) + length = (int)stream.Length; + + switch (Environment.OSVersion.Platform) + { + case PlatformID.Win32NT: + return new wFileMap(stream.SafeFileHandle.DangerousGetHandle(), prot, offset, (uint)length) { _path = stream.Name }; + default: + return new cFileMap(stream, prot, offset, length) { _path = stream.Name }; + } + +//#if DEBUG +// Console.WriteLine("Opening file map: {0}", stream.Name); +//#endif + } + + public static FileMap FromStreamInternal(FileStream stream, FileMapProtect prot, int offset, int length) + { + if (length == 0) + length = (int)stream.Length; + + switch (Environment.OSVersion.Platform) + { + case PlatformID.Win32NT: + return new wFileMap(stream.SafeFileHandle.DangerousGetHandle(), prot, offset, (uint)length) { _baseStream = stream, _path = stream.Name }; + default: + return new cFileMap(stream, prot, offset, length) { _baseStream = stream, _path = stream.Name }; + } + +//#if DEBUG +// Console.WriteLine("Opening file map: {0}", stream.Name); +//#endif + + } + + } + + public enum FileMapProtect : uint + { + Read = 0x01, + ReadWrite = 0x02 + } + + public class wFileMap : FileMap + { + internal wFileMap(VoidPtr hFile, FileMapProtect protect, long offset, uint length) + { + long maxSize = offset + length; + uint maxHigh = (uint)(maxSize >> 32); + uint maxLow = (uint)maxSize; + Win32._FileMapProtect mProtect; Win32._FileMapAccess mAccess; + if (protect == FileMapProtect.ReadWrite) + { + mProtect = Win32._FileMapProtect.ReadWrite; + mAccess = Win32._FileMapAccess.Write; + } + else + { + mProtect = Win32._FileMapProtect.ReadOnly; + mAccess = Win32._FileMapAccess.Read; + } + + using (Win32.SafeHandle h = Win32.CreateFileMapping(hFile, null, mProtect, maxHigh, maxLow, null)) + { + h.ErrorCheck(); + _addr = Win32.MapViewOfFile(h.Handle, mAccess, (uint)(offset >> 32), (uint)offset, length); + if (!_addr) Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); + _length = (int)length; + } + } + + public override void Dispose() + { + if (_addr) + { + Win32.FlushViewOfFile(_addr, 0); + Win32.UnmapViewOfFile(_addr); + _addr = null; + } + base.Dispose(); + } + } + + + public unsafe class cFileMap : FileMap + { + protected MemoryMappedFile _mappedFile; + protected MemoryMappedViewAccessor _mappedFileAccessor; + + public cFileMap(FileStream stream, FileMapProtect protect, int offset, int length) + { + MemoryMappedFileAccess cProtect = (protect == FileMapProtect.ReadWrite) ? MemoryMappedFileAccess.ReadWrite : MemoryMappedFileAccess.Read; + _length = length; + _mappedFile = MemoryMappedFile.CreateFromFile(stream, stream.Name, _length, cProtect, null, HandleInheritability.None, true); + _mappedFileAccessor = _mappedFile.CreateViewAccessor(offset, _length, cProtect); + _addr = _mappedFileAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(); + } + + public override void Dispose() + { + if (_mappedFile != null) + _mappedFile.Dispose(); + if (_mappedFileAccessor != null) + _mappedFileAccessor.Dispose(); + base.Dispose(); + } + } +} diff --git a/AnimCmd/IO/Win32.cs b/AnimCmd/IO/Win32.cs index 3827102..a9afe8a 100644 --- a/AnimCmd/IO/Win32.cs +++ b/AnimCmd/IO/Win32.cs @@ -1,129 +1,129 @@ -//=========================================================================\\ -// Taken from BrawlLib's source code, credit goes to devs who worked on it.\\ -// (Kryal, Bero, BlackJax96, LibertyErnie, Sammi Husky) \\ -// My deepest apologies to anyone who i've missed \\ -//=========================================================================\\ - -using System; -using System.Runtime.InteropServices; -using System.Diagnostics; - -namespace System -{ - static partial class Win32 - { - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public class SafeHandle : IDisposable - { - private uint _handle; - public VoidPtr Handle { get { return _handle; } } - - public SafeHandle(VoidPtr handle) { _handle = handle; } - - ~SafeHandle() { Dispose(); } - public void Dispose() { if (_handle != 0) { CloseHandle(_handle); _handle = 0; } GC.SuppressFinalize(this); } - public void ErrorCheck() { if (_handle == 0) Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } - - public static implicit operator SafeHandle(VoidPtr handle) { return new SafeHandle(handle); } - - internal static unsafe SafeHandle Duplicate(VoidPtr hFile) - { - VoidPtr hProc = Process.GetCurrentProcess().Handle; - if (!DuplicateHandle(hProc, hFile, hProc, out hFile, 0, false, 2)) - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - return new SafeHandle(hFile); - } - } - - [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)] - public static extern bool CloseHandle(VoidPtr hObject); - [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError=true)] - public static extern bool DuplicateHandle(VoidPtr hSourceProcessHandle, VoidPtr hSourceHandle, VoidPtr hTargetProcessHandle, out VoidPtr lpTargetHandle, uint dwDesiredAccess, bool bInheritHandle, uint dwOptions); - - - [DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)] - public static extern void MoveMemory(VoidPtr dest, VoidPtr src, uint size); - [DllImport("Kernel32.dll", EntryPoint = "RtlFillMemory", SetLastError = false)] - public static extern void FillMemory(VoidPtr dest, uint length, byte value); - - [DllImport("user32.dll", SetLastError=true)] - public static extern VoidPtr GetDC(VoidPtr hWnd); - [DllImport("user32.dll")] - public static extern int ReleaseDC(VoidPtr hWnd, VoidPtr hDC); - - #region File Mapping - [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - public static extern VoidPtr CreateFileMapping(VoidPtr hFile, VoidPtr lpAttributes, _FileMapProtect flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName); - [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - public static extern bool FlushViewOfFile(VoidPtr lpBaseAddress, uint dwNumberOfBytesToFlush); - [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - public static extern VoidPtr MapViewOfFile(VoidPtr hFileMappingObject, _FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap); - [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - public static extern VoidPtr MapViewOfFileEx(VoidPtr hFileMappingObject, _FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap, VoidPtr lpBaseAddress); - [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)] - public static extern VoidPtr OpenFileMapping(_FileMapAccess dwDesiredAccess, bool bInheritHandle, string lpName); - [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)] - public static extern bool UnmapViewOfFile(VoidPtr lpBaseAddress); - - - //private class FILEMAP : IDisposable - //{ - // public VoidPtr _handle; - - // //private FILEMAP(VoidPtr handle) { _handle = handle; } - // public FILEMAP(IntPtr hFile, VoidPtr lpAttributes, _FileMapProtect flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName) - // { - // _handle = CreateFileMappingW(hFile, lpAttributes, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName); - // if (!_handle) - // Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - // } - // ~FILEMAP() { Dispose(); } - // public void Dispose() - // { - // if (_handle) - // { - // CloseHandle(_handle); - // _handle = null; - // } - // } - // //public static FILEMAP Open(_FileMapAccess dwDesiredAccess, bool bInheritHandle, string lpName) - // //{ - // // VoidPtr _handle = OpenFileMappingW(dwDesiredAccess, bInheritHandle, lpName); - // // if (!_handle) - // // Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - // // return new FILEMAP(_handle); - // //} - //} - - [Flags] - public enum _FileMapProtect : uint - { - ExecuteRead = 0x20, - ExecuteReadWrite = 0x40, - ExecuteWriteCopy = 0x80, - ReadOnly = 0x02, - ReadWrite = 0x04, - WriteCopy = 0x08, - - Commit = 0x8000000, - Image = 0x1000000, - LargePages = 0x80000000, - NoCache = 0x10000000, - Reserve = 0x4000000, - WriteCombine = 0x40000000 - } - - [Flags] - public enum _FileMapAccess : uint - { - Copy = 0x01, - Write = 0x02, - Read = 0x04, - Execute = 0x20, - All = 0x000F001F - } - #endregion - - - } -} +//=========================================================================\\ +// Taken from BrawlLib's source code, credit goes to devs who worked on it.\\ +// (Kryal, Bero, BlackJax96, LibertyErnie, Sammi Husky) \\ +// My deepest apologies to anyone who i've missed \\ +//=========================================================================\\ + +using System; +using System.Runtime.InteropServices; +using System.Diagnostics; + +namespace System +{ + static partial class Win32 + { + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public class SafeHandle : IDisposable + { + private uint _handle; + public VoidPtr Handle { get { return _handle; } } + + public SafeHandle(VoidPtr handle) { _handle = handle; } + + ~SafeHandle() { Dispose(); } + public void Dispose() { if (_handle != 0) { CloseHandle(_handle); _handle = 0; } GC.SuppressFinalize(this); } + public void ErrorCheck() { if (_handle == 0) Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } + + public static implicit operator SafeHandle(VoidPtr handle) { return new SafeHandle(handle); } + + internal static unsafe SafeHandle Duplicate(VoidPtr hFile) + { + VoidPtr hProc = Process.GetCurrentProcess().Handle; + if (!DuplicateHandle(hProc, hFile, hProc, out hFile, 0, false, 2)) + Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); + return new SafeHandle(hFile); + } + } + + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)] + public static extern bool CloseHandle(VoidPtr hObject); + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError=true)] + public static extern bool DuplicateHandle(VoidPtr hSourceProcessHandle, VoidPtr hSourceHandle, VoidPtr hTargetProcessHandle, out VoidPtr lpTargetHandle, uint dwDesiredAccess, bool bInheritHandle, uint dwOptions); + + + [DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)] + public static extern void MoveMemory(VoidPtr dest, VoidPtr src, uint size); + [DllImport("Kernel32.dll", EntryPoint = "RtlFillMemory", SetLastError = false)] + public static extern void FillMemory(VoidPtr dest, uint length, byte value); + + [DllImport("user32.dll", SetLastError=true)] + public static extern VoidPtr GetDC(VoidPtr hWnd); + [DllImport("user32.dll")] + public static extern int ReleaseDC(VoidPtr hWnd, VoidPtr hDC); + + #region File Mapping + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern VoidPtr CreateFileMapping(VoidPtr hFile, VoidPtr lpAttributes, _FileMapProtect flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName); + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern bool FlushViewOfFile(VoidPtr lpBaseAddress, uint dwNumberOfBytesToFlush); + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern VoidPtr MapViewOfFile(VoidPtr hFileMappingObject, _FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap); + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern VoidPtr MapViewOfFileEx(VoidPtr hFileMappingObject, _FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap, VoidPtr lpBaseAddress); + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)] + public static extern VoidPtr OpenFileMapping(_FileMapAccess dwDesiredAccess, bool bInheritHandle, string lpName); + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)] + public static extern bool UnmapViewOfFile(VoidPtr lpBaseAddress); + + + //private class FILEMAP : IDisposable + //{ + // public VoidPtr _handle; + + // //private FILEMAP(VoidPtr handle) { _handle = handle; } + // public FILEMAP(IntPtr hFile, VoidPtr lpAttributes, _FileMapProtect flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName) + // { + // _handle = CreateFileMappingW(hFile, lpAttributes, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName); + // if (!_handle) + // Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); + // } + // ~FILEMAP() { Dispose(); } + // public void Dispose() + // { + // if (_handle) + // { + // CloseHandle(_handle); + // _handle = null; + // } + // } + // //public static FILEMAP Open(_FileMapAccess dwDesiredAccess, bool bInheritHandle, string lpName) + // //{ + // // VoidPtr _handle = OpenFileMappingW(dwDesiredAccess, bInheritHandle, lpName); + // // if (!_handle) + // // Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); + // // return new FILEMAP(_handle); + // //} + //} + + [Flags] + public enum _FileMapProtect : uint + { + ExecuteRead = 0x20, + ExecuteReadWrite = 0x40, + ExecuteWriteCopy = 0x80, + ReadOnly = 0x02, + ReadWrite = 0x04, + WriteCopy = 0x08, + + Commit = 0x8000000, + Image = 0x1000000, + LargePages = 0x80000000, + NoCache = 0x10000000, + Reserve = 0x4000000, + WriteCombine = 0x40000000 + } + + [Flags] + public enum _FileMapAccess : uint + { + Copy = 0x01, + Write = 0x02, + Read = 0x04, + Execute = 0x20, + All = 0x000F001F + } + #endregion + + + } +} diff --git a/AnimCmd/Properties/AssemblyInfo.cs b/AnimCmd/Properties/AssemblyInfo.cs index 0b3dcf5..c283eae 100644 --- a/AnimCmd/Properties/AssemblyInfo.cs +++ b/AnimCmd/Properties/AssemblyInfo.cs @@ -1,36 +1,36 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("AnimCmd")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Microsoft")] -[assembly: AssemblyProduct("AnimCmd")] -[assembly: AssemblyCopyright("Copyright © Microsoft 2014")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("e262a146-d7f5-402e-ac24-07593f7301ae")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("AnimCmd")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("AnimCmd")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e262a146-d7f5-402e-ac24-07593f7301ae")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/AnimCmd/Properties/Settings.Designer.cs b/AnimCmd/Properties/Settings.Designer.cs index 66faa36..377322f 100644 --- a/AnimCmd/Properties/Settings.Designer.cs +++ b/AnimCmd/Properties/Settings.Designer.cs @@ -1,26 +1,26 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace Sm4shCommand.Properties { - - - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { - - private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default { - get { - return defaultInstance; - } - } - } -} +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Sm4shCommand.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/AnimCmd/Properties/Settings.settings b/AnimCmd/Properties/Settings.settings index 3964565..abf36c5 100644 --- a/AnimCmd/Properties/Settings.settings +++ b/AnimCmd/Properties/Settings.settings @@ -1,7 +1,7 @@ - - - - - - - + + + + + + + diff --git a/AnimCmd/System/Windows/Components/FolderSelectDialog.designer.cs b/AnimCmd/System/Windows/Components/FolderSelectDialog.designer.cs index 4229353..9d52b71 100644 --- a/AnimCmd/System/Windows/Components/FolderSelectDialog.designer.cs +++ b/AnimCmd/System/Windows/Components/FolderSelectDialog.designer.cs @@ -1,36 +1,36 @@ -namespace System.Windows.Forms -{ - partial class FolderSelectDialog - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Component Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - components = new System.ComponentModel.Container(); - } - - #endregion - } -} +namespace System.Windows.Forms +{ + partial class FolderSelectDialog + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/AnimCmd/Type Extensions/BigEndianTypes.cs b/AnimCmd/Type Extensions/BigEndianTypes.cs index 7c74d6e..fb404ea 100644 --- a/AnimCmd/Type Extensions/BigEndianTypes.cs +++ b/AnimCmd/Type Extensions/BigEndianTypes.cs @@ -1,503 +1,503 @@ -using System; -using System.Runtime.InteropServices; - -namespace System -{ - [StructLayout(LayoutKind.Sequential)] - public struct bint : IConvertible - { - public int Data { get { return _data; } set { _data = value; } } - private int _data; - public static implicit operator int(bint val) { return val._data.Reverse(); } - public static implicit operator bint(int val) { return new bint { _data = val.Reverse() }; } - public static explicit operator uint(bint val) { return (uint)val._data.Reverse(); } - public static explicit operator bint(uint val) { return new bint { _data = (int)val.Reverse() }; } - - #region IConvertible Members - - public TypeCode GetTypeCode() - { - return Convert.GetTypeCode(_data.Reverse()); - } - - public bool ToBoolean(IFormatProvider provider) - { - return Convert.ToBoolean(_data.Reverse()); - } - - public byte ToByte(IFormatProvider provider) - { - return Convert.ToByte(_data.Reverse()); - } - - public char ToChar(IFormatProvider provider) - { - return Convert.ToChar(_data.Reverse()); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - return Convert.ToDateTime(_data.Reverse()); - } - - public decimal ToDecimal(IFormatProvider provider) - { - return Convert.ToDecimal(_data.Reverse()); - } - - public double ToDouble(IFormatProvider provider) - { - return Convert.ToDouble(_data.Reverse()); - } - - public short ToInt16(IFormatProvider provider) - { - return Convert.ToInt16(_data.Reverse()); - } - - public int ToInt32(IFormatProvider provider) - { - return Convert.ToInt32(_data.Reverse()); - } - - public long ToInt64(IFormatProvider provider) - { - return Convert.ToInt64(_data.Reverse()); - } - - public sbyte ToSByte(IFormatProvider provider) - { - return Convert.ToSByte(_data.Reverse()); - } - - public float ToSingle(IFormatProvider provider) - { - return Convert.ToSingle(_data.Reverse()); - } - - public string ToString(IFormatProvider provider) - { - return Convert.ToString(_data.Reverse()); - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - return Convert.ChangeType(_data.Reverse(), conversionType, provider); - } - - public ushort ToUInt16(IFormatProvider provider) - { - return Convert.ToUInt16(_data.Reverse()); - } - - public uint ToUInt32(IFormatProvider provider) - { - return Convert.ToUInt32(_data.Reverse()); - } - - public ulong ToUInt64(IFormatProvider provider) - { - return Convert.ToUInt64(_data.Reverse()); - } - - #endregion - } - - [StructLayout(LayoutKind.Sequential)] - public struct buint : IConvertible - { - public uint Data { get { return _data; } set { _data = value; } } - private uint _data; - public static implicit operator uint(buint val) { return val._data.Reverse(); } - public static implicit operator buint(uint val) { return new buint { _data = val.Reverse() }; } - public static explicit operator int(buint val) { return (int)val._data.Reverse(); } - public static explicit operator buint(int val) { return new buint { _data = (uint)val.Reverse() }; } - - #region IConvertible Members - - public TypeCode GetTypeCode() - { - return Convert.GetTypeCode(_data.Reverse()); - } - - public bool ToBoolean(IFormatProvider provider) - { - return Convert.ToBoolean(_data.Reverse()); - } - - public byte ToByte(IFormatProvider provider) - { - return Convert.ToByte(_data.Reverse()); - } - - public char ToChar(IFormatProvider provider) - { - return Convert.ToChar(_data.Reverse()); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - return Convert.ToDateTime(_data.Reverse()); - } - - public decimal ToDecimal(IFormatProvider provider) - { - return Convert.ToDecimal(_data.Reverse()); - } - - public double ToDouble(IFormatProvider provider) - { - return Convert.ToDouble(_data.Reverse()); - } - - public short ToInt16(IFormatProvider provider) - { - return Convert.ToInt16(_data.Reverse()); - } - - public int ToInt32(IFormatProvider provider) - { - return Convert.ToInt32(_data.Reverse()); - } - - public long ToInt64(IFormatProvider provider) - { - return Convert.ToInt64(_data.Reverse()); - } - - public sbyte ToSByte(IFormatProvider provider) - { - return Convert.ToSByte(_data.Reverse()); - } - - public float ToSingle(IFormatProvider provider) - { - return Convert.ToSingle(_data.Reverse()); - } - - public string ToString(IFormatProvider provider) - { - return Convert.ToString(_data.Reverse()); - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - return Convert.ChangeType(_data.Reverse(), conversionType, provider); - } - - public ushort ToUInt16(IFormatProvider provider) - { - return Convert.ToUInt16(_data.Reverse()); - } - - public uint ToUInt32(IFormatProvider provider) - { - return Convert.ToUInt32(_data.Reverse()); - } - - public ulong ToUInt64(IFormatProvider provider) - { - return Convert.ToUInt64(_data.Reverse()); - } - - #endregion - } - - [StructLayout(LayoutKind.Sequential)] - public struct bfloat : IConvertible - { - public float Data { get { return _data; } set { _data = value; } } - private float _data; - public static implicit operator float(bfloat val) { return val._data.Reverse(); } - public static implicit operator bfloat(float val) { return new bfloat { _data = val.Reverse() }; } - - #region IConvertible Members - - public TypeCode GetTypeCode() - { - return Convert.GetTypeCode(_data.Reverse()); - } - - public bool ToBoolean(IFormatProvider provider) - { - return Convert.ToBoolean(_data.Reverse()); - } - - public byte ToByte(IFormatProvider provider) - { - return Convert.ToByte(_data.Reverse()); - } - - public char ToChar(IFormatProvider provider) - { - return Convert.ToChar(_data.Reverse()); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - return Convert.ToDateTime(_data.Reverse()); - } - - public decimal ToDecimal(IFormatProvider provider) - { - return Convert.ToDecimal(_data.Reverse()); - } - - public double ToDouble(IFormatProvider provider) - { - return Convert.ToDouble(_data.Reverse()); - } - - public short ToInt16(IFormatProvider provider) - { - return Convert.ToInt16(_data.Reverse()); - } - - public int ToInt32(IFormatProvider provider) - { - return Convert.ToInt32(_data.Reverse()); - } - - public long ToInt64(IFormatProvider provider) - { - return Convert.ToInt64(_data.Reverse()); - } - - public sbyte ToSByte(IFormatProvider provider) - { - return Convert.ToSByte(_data.Reverse()); - } - - public float ToSingle(IFormatProvider provider) - { - return Convert.ToSingle(_data.Reverse()); - } - - public string ToString(IFormatProvider provider) - { - return Convert.ToString(_data.Reverse()); - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - return Convert.ChangeType(_data.Reverse(), conversionType, provider); - } - - public ushort ToUInt16(IFormatProvider provider) - { - return Convert.ToUInt16(_data.Reverse()); - } - - public uint ToUInt32(IFormatProvider provider) - { - return Convert.ToUInt32(_data.Reverse()); - } - - public ulong ToUInt64(IFormatProvider provider) - { - return Convert.ToUInt64(_data.Reverse()); - } - - #endregion - } - - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct bshort : IConvertible - { - public short Data { get { return _data; } set { _data = value; } } - private short _data; - public static implicit operator short(bshort val) { return val._data.Reverse(); } - public static implicit operator bshort(short val) { return new bshort { _data = val.Reverse() }; } - public static explicit operator ushort(bshort val) { return (ushort)val._data.Reverse(); } - public static explicit operator bshort(ushort val) { return new bshort { _data = (short)val.Reverse() }; } - - #region IConvertible Members - - public TypeCode GetTypeCode() - { - return Convert.GetTypeCode(_data.Reverse()); - } - - public bool ToBoolean(IFormatProvider provider) - { - return Convert.ToBoolean(_data.Reverse()); - } - - public byte ToByte(IFormatProvider provider) - { - return Convert.ToByte(_data.Reverse()); - } - - public char ToChar(IFormatProvider provider) - { - return Convert.ToChar(_data.Reverse()); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - return Convert.ToDateTime(_data.Reverse()); - } - - public decimal ToDecimal(IFormatProvider provider) - { - return Convert.ToDecimal(_data.Reverse()); - } - - public double ToDouble(IFormatProvider provider) - { - return Convert.ToDouble(_data.Reverse()); - } - - public short ToInt16(IFormatProvider provider) - { - return Convert.ToInt16(_data.Reverse()); - } - - public int ToInt32(IFormatProvider provider) - { - return Convert.ToInt32(_data.Reverse()); - } - - public long ToInt64(IFormatProvider provider) - { - return Convert.ToInt64(_data.Reverse()); - } - - public sbyte ToSByte(IFormatProvider provider) - { - return Convert.ToSByte(_data.Reverse()); - } - - public float ToSingle(IFormatProvider provider) - { - return Convert.ToSingle(_data.Reverse()); - } - - public string ToString(IFormatProvider provider) - { - return Convert.ToString(_data.Reverse()); - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - return Convert.ChangeType(_data.Reverse(), conversionType, provider); - } - - public ushort ToUInt16(IFormatProvider provider) - { - return Convert.ToUInt16(_data.Reverse()); - } - - public uint ToUInt32(IFormatProvider provider) - { - return Convert.ToUInt32(_data.Reverse()); - } - - public ulong ToUInt64(IFormatProvider provider) - { - return Convert.ToUInt64(_data.Reverse()); - } - - #endregion - } - - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public struct bushort : IConvertible - { - public ushort Data { get { return _data; } set { _data = value; } } - private ushort _data; - public static implicit operator ushort(bushort val) { return val._data.Reverse(); } - public static implicit operator bushort(ushort val) { return new bushort { _data = val.Reverse() }; } - public static explicit operator short(bushort val) { return (short)val._data.Reverse(); } - public static explicit operator bushort(short val) { return new bushort { _data = (ushort)val.Reverse() }; } - - #region IConvertible Members - - public TypeCode GetTypeCode() - { - return Convert.GetTypeCode(_data.Reverse()); - } - - public bool ToBoolean(IFormatProvider provider) - { - return Convert.ToBoolean(_data.Reverse()); - } - - public byte ToByte(IFormatProvider provider) - { - return Convert.ToByte(_data.Reverse()); - } - - public char ToChar(IFormatProvider provider) - { - return Convert.ToChar(_data.Reverse()); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - return Convert.ToDateTime(_data.Reverse()); - } - - public decimal ToDecimal(IFormatProvider provider) - { - return Convert.ToDecimal(_data.Reverse()); - } - - public double ToDouble(IFormatProvider provider) - { - return Convert.ToDouble(_data.Reverse()); - } - - public short ToInt16(IFormatProvider provider) - { - return Convert.ToInt16(_data.Reverse()); - } - - public int ToInt32(IFormatProvider provider) - { - return Convert.ToInt32(_data.Reverse()); - } - - public long ToInt64(IFormatProvider provider) - { - return Convert.ToInt64(_data.Reverse()); - } - - public sbyte ToSByte(IFormatProvider provider) - { - return Convert.ToSByte(_data.Reverse()); - } - - public float ToSingle(IFormatProvider provider) - { - return Convert.ToSingle(_data.Reverse()); - } - - public string ToString(IFormatProvider provider) - { - return Convert.ToString(_data.Reverse()); - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - return Convert.ChangeType(_data.Reverse(), conversionType, provider); - } - - public ushort ToUInt16(IFormatProvider provider) - { - return Convert.ToUInt16(_data.Reverse()); - } - - public uint ToUInt32(IFormatProvider provider) - { - return Convert.ToUInt32(_data.Reverse()); - } - - public ulong ToUInt64(IFormatProvider provider) - { - return Convert.ToUInt64(_data.Reverse()); - } - - #endregion - } -} +using System; +using System.Runtime.InteropServices; + +namespace System +{ + [StructLayout(LayoutKind.Sequential)] + public struct bint : IConvertible + { + public int Data { get { return _data; } set { _data = value; } } + private int _data; + public static implicit operator int(bint val) { return val._data.Reverse(); } + public static implicit operator bint(int val) { return new bint { _data = val.Reverse() }; } + public static explicit operator uint(bint val) { return (uint)val._data.Reverse(); } + public static explicit operator bint(uint val) { return new bint { _data = (int)val.Reverse() }; } + + #region IConvertible Members + + public TypeCode GetTypeCode() + { + return Convert.GetTypeCode(_data.Reverse()); + } + + public bool ToBoolean(IFormatProvider provider) + { + return Convert.ToBoolean(_data.Reverse()); + } + + public byte ToByte(IFormatProvider provider) + { + return Convert.ToByte(_data.Reverse()); + } + + public char ToChar(IFormatProvider provider) + { + return Convert.ToChar(_data.Reverse()); + } + + public DateTime ToDateTime(IFormatProvider provider) + { + return Convert.ToDateTime(_data.Reverse()); + } + + public decimal ToDecimal(IFormatProvider provider) + { + return Convert.ToDecimal(_data.Reverse()); + } + + public double ToDouble(IFormatProvider provider) + { + return Convert.ToDouble(_data.Reverse()); + } + + public short ToInt16(IFormatProvider provider) + { + return Convert.ToInt16(_data.Reverse()); + } + + public int ToInt32(IFormatProvider provider) + { + return Convert.ToInt32(_data.Reverse()); + } + + public long ToInt64(IFormatProvider provider) + { + return Convert.ToInt64(_data.Reverse()); + } + + public sbyte ToSByte(IFormatProvider provider) + { + return Convert.ToSByte(_data.Reverse()); + } + + public float ToSingle(IFormatProvider provider) + { + return Convert.ToSingle(_data.Reverse()); + } + + public string ToString(IFormatProvider provider) + { + return Convert.ToString(_data.Reverse()); + } + + public object ToType(Type conversionType, IFormatProvider provider) + { + return Convert.ChangeType(_data.Reverse(), conversionType, provider); + } + + public ushort ToUInt16(IFormatProvider provider) + { + return Convert.ToUInt16(_data.Reverse()); + } + + public uint ToUInt32(IFormatProvider provider) + { + return Convert.ToUInt32(_data.Reverse()); + } + + public ulong ToUInt64(IFormatProvider provider) + { + return Convert.ToUInt64(_data.Reverse()); + } + + #endregion + } + + [StructLayout(LayoutKind.Sequential)] + public struct buint : IConvertible + { + public uint Data { get { return _data; } set { _data = value; } } + private uint _data; + public static implicit operator uint(buint val) { return val._data.Reverse(); } + public static implicit operator buint(uint val) { return new buint { _data = val.Reverse() }; } + public static explicit operator int(buint val) { return (int)val._data.Reverse(); } + public static explicit operator buint(int val) { return new buint { _data = (uint)val.Reverse() }; } + + #region IConvertible Members + + public TypeCode GetTypeCode() + { + return Convert.GetTypeCode(_data.Reverse()); + } + + public bool ToBoolean(IFormatProvider provider) + { + return Convert.ToBoolean(_data.Reverse()); + } + + public byte ToByte(IFormatProvider provider) + { + return Convert.ToByte(_data.Reverse()); + } + + public char ToChar(IFormatProvider provider) + { + return Convert.ToChar(_data.Reverse()); + } + + public DateTime ToDateTime(IFormatProvider provider) + { + return Convert.ToDateTime(_data.Reverse()); + } + + public decimal ToDecimal(IFormatProvider provider) + { + return Convert.ToDecimal(_data.Reverse()); + } + + public double ToDouble(IFormatProvider provider) + { + return Convert.ToDouble(_data.Reverse()); + } + + public short ToInt16(IFormatProvider provider) + { + return Convert.ToInt16(_data.Reverse()); + } + + public int ToInt32(IFormatProvider provider) + { + return Convert.ToInt32(_data.Reverse()); + } + + public long ToInt64(IFormatProvider provider) + { + return Convert.ToInt64(_data.Reverse()); + } + + public sbyte ToSByte(IFormatProvider provider) + { + return Convert.ToSByte(_data.Reverse()); + } + + public float ToSingle(IFormatProvider provider) + { + return Convert.ToSingle(_data.Reverse()); + } + + public string ToString(IFormatProvider provider) + { + return Convert.ToString(_data.Reverse()); + } + + public object ToType(Type conversionType, IFormatProvider provider) + { + return Convert.ChangeType(_data.Reverse(), conversionType, provider); + } + + public ushort ToUInt16(IFormatProvider provider) + { + return Convert.ToUInt16(_data.Reverse()); + } + + public uint ToUInt32(IFormatProvider provider) + { + return Convert.ToUInt32(_data.Reverse()); + } + + public ulong ToUInt64(IFormatProvider provider) + { + return Convert.ToUInt64(_data.Reverse()); + } + + #endregion + } + + [StructLayout(LayoutKind.Sequential)] + public struct bfloat : IConvertible + { + public float Data { get { return _data; } set { _data = value; } } + private float _data; + public static implicit operator float(bfloat val) { return val._data.Reverse(); } + public static implicit operator bfloat(float val) { return new bfloat { _data = val.Reverse() }; } + + #region IConvertible Members + + public TypeCode GetTypeCode() + { + return Convert.GetTypeCode(_data.Reverse()); + } + + public bool ToBoolean(IFormatProvider provider) + { + return Convert.ToBoolean(_data.Reverse()); + } + + public byte ToByte(IFormatProvider provider) + { + return Convert.ToByte(_data.Reverse()); + } + + public char ToChar(IFormatProvider provider) + { + return Convert.ToChar(_data.Reverse()); + } + + public DateTime ToDateTime(IFormatProvider provider) + { + return Convert.ToDateTime(_data.Reverse()); + } + + public decimal ToDecimal(IFormatProvider provider) + { + return Convert.ToDecimal(_data.Reverse()); + } + + public double ToDouble(IFormatProvider provider) + { + return Convert.ToDouble(_data.Reverse()); + } + + public short ToInt16(IFormatProvider provider) + { + return Convert.ToInt16(_data.Reverse()); + } + + public int ToInt32(IFormatProvider provider) + { + return Convert.ToInt32(_data.Reverse()); + } + + public long ToInt64(IFormatProvider provider) + { + return Convert.ToInt64(_data.Reverse()); + } + + public sbyte ToSByte(IFormatProvider provider) + { + return Convert.ToSByte(_data.Reverse()); + } + + public float ToSingle(IFormatProvider provider) + { + return Convert.ToSingle(_data.Reverse()); + } + + public string ToString(IFormatProvider provider) + { + return Convert.ToString(_data.Reverse()); + } + + public object ToType(Type conversionType, IFormatProvider provider) + { + return Convert.ChangeType(_data.Reverse(), conversionType, provider); + } + + public ushort ToUInt16(IFormatProvider provider) + { + return Convert.ToUInt16(_data.Reverse()); + } + + public uint ToUInt32(IFormatProvider provider) + { + return Convert.ToUInt32(_data.Reverse()); + } + + public ulong ToUInt64(IFormatProvider provider) + { + return Convert.ToUInt64(_data.Reverse()); + } + + #endregion + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct bshort : IConvertible + { + public short Data { get { return _data; } set { _data = value; } } + private short _data; + public static implicit operator short(bshort val) { return val._data.Reverse(); } + public static implicit operator bshort(short val) { return new bshort { _data = val.Reverse() }; } + public static explicit operator ushort(bshort val) { return (ushort)val._data.Reverse(); } + public static explicit operator bshort(ushort val) { return new bshort { _data = (short)val.Reverse() }; } + + #region IConvertible Members + + public TypeCode GetTypeCode() + { + return Convert.GetTypeCode(_data.Reverse()); + } + + public bool ToBoolean(IFormatProvider provider) + { + return Convert.ToBoolean(_data.Reverse()); + } + + public byte ToByte(IFormatProvider provider) + { + return Convert.ToByte(_data.Reverse()); + } + + public char ToChar(IFormatProvider provider) + { + return Convert.ToChar(_data.Reverse()); + } + + public DateTime ToDateTime(IFormatProvider provider) + { + return Convert.ToDateTime(_data.Reverse()); + } + + public decimal ToDecimal(IFormatProvider provider) + { + return Convert.ToDecimal(_data.Reverse()); + } + + public double ToDouble(IFormatProvider provider) + { + return Convert.ToDouble(_data.Reverse()); + } + + public short ToInt16(IFormatProvider provider) + { + return Convert.ToInt16(_data.Reverse()); + } + + public int ToInt32(IFormatProvider provider) + { + return Convert.ToInt32(_data.Reverse()); + } + + public long ToInt64(IFormatProvider provider) + { + return Convert.ToInt64(_data.Reverse()); + } + + public sbyte ToSByte(IFormatProvider provider) + { + return Convert.ToSByte(_data.Reverse()); + } + + public float ToSingle(IFormatProvider provider) + { + return Convert.ToSingle(_data.Reverse()); + } + + public string ToString(IFormatProvider provider) + { + return Convert.ToString(_data.Reverse()); + } + + public object ToType(Type conversionType, IFormatProvider provider) + { + return Convert.ChangeType(_data.Reverse(), conversionType, provider); + } + + public ushort ToUInt16(IFormatProvider provider) + { + return Convert.ToUInt16(_data.Reverse()); + } + + public uint ToUInt32(IFormatProvider provider) + { + return Convert.ToUInt32(_data.Reverse()); + } + + public ulong ToUInt64(IFormatProvider provider) + { + return Convert.ToUInt64(_data.Reverse()); + } + + #endregion + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct bushort : IConvertible + { + public ushort Data { get { return _data; } set { _data = value; } } + private ushort _data; + public static implicit operator ushort(bushort val) { return val._data.Reverse(); } + public static implicit operator bushort(ushort val) { return new bushort { _data = val.Reverse() }; } + public static explicit operator short(bushort val) { return (short)val._data.Reverse(); } + public static explicit operator bushort(short val) { return new bushort { _data = (ushort)val.Reverse() }; } + + #region IConvertible Members + + public TypeCode GetTypeCode() + { + return Convert.GetTypeCode(_data.Reverse()); + } + + public bool ToBoolean(IFormatProvider provider) + { + return Convert.ToBoolean(_data.Reverse()); + } + + public byte ToByte(IFormatProvider provider) + { + return Convert.ToByte(_data.Reverse()); + } + + public char ToChar(IFormatProvider provider) + { + return Convert.ToChar(_data.Reverse()); + } + + public DateTime ToDateTime(IFormatProvider provider) + { + return Convert.ToDateTime(_data.Reverse()); + } + + public decimal ToDecimal(IFormatProvider provider) + { + return Convert.ToDecimal(_data.Reverse()); + } + + public double ToDouble(IFormatProvider provider) + { + return Convert.ToDouble(_data.Reverse()); + } + + public short ToInt16(IFormatProvider provider) + { + return Convert.ToInt16(_data.Reverse()); + } + + public int ToInt32(IFormatProvider provider) + { + return Convert.ToInt32(_data.Reverse()); + } + + public long ToInt64(IFormatProvider provider) + { + return Convert.ToInt64(_data.Reverse()); + } + + public sbyte ToSByte(IFormatProvider provider) + { + return Convert.ToSByte(_data.Reverse()); + } + + public float ToSingle(IFormatProvider provider) + { + return Convert.ToSingle(_data.Reverse()); + } + + public string ToString(IFormatProvider provider) + { + return Convert.ToString(_data.Reverse()); + } + + public object ToType(Type conversionType, IFormatProvider provider) + { + return Convert.ChangeType(_data.Reverse(), conversionType, provider); + } + + public ushort ToUInt16(IFormatProvider provider) + { + return Convert.ToUInt16(_data.Reverse()); + } + + public uint ToUInt32(IFormatProvider provider) + { + return Convert.ToUInt32(_data.Reverse()); + } + + public ulong ToUInt64(IFormatProvider provider) + { + return Convert.ToUInt64(_data.Reverse()); + } + + #endregion + } +} diff --git a/AnimCmd/Type Extensions/ByteExtension.cs b/AnimCmd/Type Extensions/ByteExtension.cs index 47623e6..21a4a97 100644 --- a/AnimCmd/Type Extensions/ByteExtension.cs +++ b/AnimCmd/Type Extensions/ByteExtension.cs @@ -1,23 +1,23 @@ -using System; - -namespace System -{ - public static class ByteExtension - { - public static int CompareBits(this Byte b1, byte b2) - { - for (int i = 8, b = 0x80; i-- != 0; b >>= 1) - if ((b1 & b) != (b2 & b)) - return i; - return 0; - } - public static int CountBits(this Byte b) - { - int count = 0; - for (int i = 0; i < 8; i++) - if (((b >> i) & 1) != 0) - count++; - return count; - } - } -} +using System; + +namespace System +{ + public static class ByteExtension + { + public static int CompareBits(this Byte b1, byte b2) + { + for (int i = 8, b = 0x80; i-- != 0; b >>= 1) + if ((b1 & b) != (b2 & b)) + return i; + return 0; + } + public static int CountBits(this Byte b) + { + int count = 0; + for (int i = 0; i < 8; i++) + if (((b >> i) & 1) != 0) + count++; + return count; + } + } +} diff --git a/AnimCmd/Type Extensions/DoubleExtension.cs b/AnimCmd/Type Extensions/DoubleExtension.cs index 8e03152..5c0eb88 100644 --- a/AnimCmd/Type Extensions/DoubleExtension.cs +++ b/AnimCmd/Type Extensions/DoubleExtension.cs @@ -1,28 +1,28 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace System -{ - public static unsafe class DoubleExtension - { - private static readonly float[] _quantTable = new float[]{ - 1f, 2f, 4f, 8f, 16f, 32f, 64f, 128f, 256f, 512f, 1024f, 2048f, 4096f, 8192f, 16384f, 32768f, - 65536f, 131072f, 262144f, 524288f, 1048576f, 2097152f, 4194304f, 8388608f, 16777216f, 33554432f, 67108864f, - 134217728f, 268435456f, 536870912f, 1073741824f, 2147483648f, 4294967296f - }; - - - public static float Unquantize(byte* data, int scale) - { - float value; - - float scaleVal = (scale < 32) ? (1.0f / _quantTable[scale]) : (_quantTable[64 - scale]); - - value = data[0] * scaleVal; - - return value; - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace System +{ + public static unsafe class DoubleExtension + { + private static readonly float[] _quantTable = new float[]{ + 1f, 2f, 4f, 8f, 16f, 32f, 64f, 128f, 256f, 512f, 1024f, 2048f, 4096f, 8192f, 16384f, 32768f, + 65536f, 131072f, 262144f, 524288f, 1048576f, 2097152f, 4194304f, 8388608f, 16777216f, 33554432f, 67108864f, + 134217728f, 268435456f, 536870912f, 1073741824f, 2147483648f, 4294967296f + }; + + + public static float Unquantize(byte* data, int scale) + { + float value; + + float scaleVal = (scale < 32) ? (1.0f / _quantTable[scale]) : (_quantTable[64 - scale]); + + value = data[0] * scaleVal; + + return value; + } + } +} diff --git a/AnimCmd/Type Extensions/EncodingExtension.cs b/AnimCmd/Type Extensions/EncodingExtension.cs index 7fbe6bc..89e4701 100644 --- a/AnimCmd/Type Extensions/EncodingExtension.cs +++ b/AnimCmd/Type Extensions/EncodingExtension.cs @@ -1,23 +1,23 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace System.Text -{ - public unsafe static class EncodingExtension - { - public static string GetString(this Encoding encoding, sbyte* ptr) - { - int count = 0; - while (*(ptr + count++) != '\0') ; - - return new string((sbyte*)ptr, 0, count - 1, encoding); - } - - public static string GetString(this Encoding encoding, VoidPtr ptr) - { - return encoding.GetString((sbyte*)ptr); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace System.Text +{ + public unsafe static class EncodingExtension + { + public static string GetString(this Encoding encoding, sbyte* ptr) + { + int count = 0; + while (*(ptr + count++) != '\0') ; + + return new string((sbyte*)ptr, 0, count - 1, encoding); + } + + public static string GetString(this Encoding encoding, VoidPtr ptr) + { + return encoding.GetString((sbyte*)ptr); + } + } +} diff --git a/AnimCmd/Type Extensions/Int16Extension.cs b/AnimCmd/Type Extensions/Int16Extension.cs index 354c4e3..0981ee0 100644 --- a/AnimCmd/Type Extensions/Int16Extension.cs +++ b/AnimCmd/Type Extensions/Int16Extension.cs @@ -1,13 +1,13 @@ -using System; - -namespace System -{ - public static class Int16Extension - { - public static Int16 Reverse(this Int16 value) - { - return (short)(((value >> 8) & 0xFF) | (value << 8)); - } - } -} - +using System; + +namespace System +{ + public static class Int16Extension + { + public static Int16 Reverse(this Int16 value) + { + return (short)(((value >> 8) & 0xFF) | (value << 8)); + } + } +} + diff --git a/AnimCmd/Type Extensions/Int32Extension.cs b/AnimCmd/Type Extensions/Int32Extension.cs index dab3a93..b1ebfa9 100644 --- a/AnimCmd/Type Extensions/Int32Extension.cs +++ b/AnimCmd/Type Extensions/Int32Extension.cs @@ -1,24 +1,24 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace System -{ - public static class Int32Extension - { - public static unsafe Int32 Reverse(this Int32 value) - { - return ((value >> 24) & 0xFF) | (value << 24) | ((value >> 8) & 0xFF00) | ((value & 0xFF00) << 8); - } - public static Int32 Align(this Int32 value, int align) - { - return (value + align - 1) / align * align; - } - public static Int32 Clamp(this Int32 value, int min, int max) - { - if (value <= min) return min; - if (value >= max) return max; - return value; - } - } -} +using System; +using System.Collections.Generic; +using System.Text; + +namespace System +{ + public static class Int32Extension + { + public static unsafe Int32 Reverse(this Int32 value) + { + return ((value >> 24) & 0xFF) | (value << 24) | ((value >> 8) & 0xFF00) | ((value & 0xFF00) << 8); + } + public static Int32 Align(this Int32 value, int align) + { + return (value + align - 1) / align * align; + } + public static Int32 Clamp(this Int32 value, int min, int max) + { + if (value <= min) return min; + if (value >= max) return max; + return value; + } + } +} diff --git a/AnimCmd/Type Extensions/Int64Extension.cs b/AnimCmd/Type Extensions/Int64Extension.cs index 7979d82..8fe832b 100644 --- a/AnimCmd/Type Extensions/Int64Extension.cs +++ b/AnimCmd/Type Extensions/Int64Extension.cs @@ -1,25 +1,25 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace System -{ - public static class Int64Extension - { - public static Int64 Align(this Int64 value, int align) - { - if (value < 0) return 0; - if (align <= 1) return value; - long temp = value % align; - if (temp != 0) value += align - temp; - return value; - } - public static Int64 Clamp(this Int64 value, long min, long max) - { - if (value <= min) return min; - if (value >= max) return max; - return value; - } - } -} - +using System; +using System.Collections.Generic; +using System.Text; + +namespace System +{ + public static class Int64Extension + { + public static Int64 Align(this Int64 value, int align) + { + if (value < 0) return 0; + if (align <= 1) return value; + long temp = value % align; + if (temp != 0) value += align - temp; + return value; + } + public static Int64 Clamp(this Int64 value, long min, long max) + { + if (value <= min) return min; + if (value >= max) return max; + return value; + } + } +} + diff --git a/AnimCmd/Type Extensions/RoundingExtensions.cs b/AnimCmd/Type Extensions/RoundingExtensions.cs index d38cf31..2438f1a 100644 --- a/AnimCmd/Type Extensions/RoundingExtensions.cs +++ b/AnimCmd/Type Extensions/RoundingExtensions.cs @@ -1,77 +1,77 @@ -using System; - -namespace System -{ - public static class RoundingExtensions - { - public static byte RoundUp(this byte value, int factor) - { - if (factor <= 0) return value; - return (byte)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); - } - public static byte RoundDown(this byte value, int factor) - { - if (factor <= 0) return value; - return (byte)(value - value % factor); - } - - public static UInt16 RoundUp(this UInt16 value, int factor) - { - if (factor <= 0) return value; - return (ushort)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); - } - - public static UInt16 RoundDown(this UInt16 value, int factor) - { - if (factor <= 0) return value; - return (ushort)(value - value % factor); - } - - public static UInt32 RoundUp(this UInt32 value, int factor) - { - if (factor <= 0) return value; - return (uint)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); - } - - public static UInt32 RoundDown(this UInt32 value, int factor) - { - if (factor <= 0) return value; - return (uint)(value - value % factor); - } - - public static Int16 RoundUp(this Int16 value, int factor) - { - if (factor <= 0) return value; - return (short)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); - } - public static Int16 RoundDown(this Int16 value, int factor) - { - if (factor <= 0) return value; - return (short)(value - value % factor); - } - - public static Int32 RoundUp(this Int32 value, int factor) - { - if (factor <= 0) return value; - return (int)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); - } - - public static Int32 RoundDown(this Int32 value, int factor) - { - if (factor <= 0) return value; - return (int)(value - value % factor); - } - - public static Int64 RoundUp(this Int64 value, int factor) - { - if (factor <= 0) return value; - return (long)((value + (factor - 1)) - (value + (factor - 1)) % factor); - } - - public static Int64 RoundDown(this Int64 value, int factor) - { - if (factor <= 0) return value; - return (long)(value - value % factor); - } - } +using System; + +namespace System +{ + public static class RoundingExtensions + { + public static byte RoundUp(this byte value, int factor) + { + if (factor <= 0) return value; + return (byte)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); + } + public static byte RoundDown(this byte value, int factor) + { + if (factor <= 0) return value; + return (byte)(value - value % factor); + } + + public static UInt16 RoundUp(this UInt16 value, int factor) + { + if (factor <= 0) return value; + return (ushort)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); + } + + public static UInt16 RoundDown(this UInt16 value, int factor) + { + if (factor <= 0) return value; + return (ushort)(value - value % factor); + } + + public static UInt32 RoundUp(this UInt32 value, int factor) + { + if (factor <= 0) return value; + return (uint)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); + } + + public static UInt32 RoundDown(this UInt32 value, int factor) + { + if (factor <= 0) return value; + return (uint)(value - value % factor); + } + + public static Int16 RoundUp(this Int16 value, int factor) + { + if (factor <= 0) return value; + return (short)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); + } + public static Int16 RoundDown(this Int16 value, int factor) + { + if (factor <= 0) return value; + return (short)(value - value % factor); + } + + public static Int32 RoundUp(this Int32 value, int factor) + { + if (factor <= 0) return value; + return (int)((value + (factor - 1)) - ((value + (factor - 1)) % factor)); + } + + public static Int32 RoundDown(this Int32 value, int factor) + { + if (factor <= 0) return value; + return (int)(value - value % factor); + } + + public static Int64 RoundUp(this Int64 value, int factor) + { + if (factor <= 0) return value; + return (long)((value + (factor - 1)) - (value + (factor - 1)) % factor); + } + + public static Int64 RoundDown(this Int64 value, int factor) + { + if (factor <= 0) return value; + return (long)(value - value % factor); + } + } } \ No newline at end of file diff --git a/AnimCmd/Type Extensions/ShortExtension.cs b/AnimCmd/Type Extensions/ShortExtension.cs index 29243c3..06249ec 100644 --- a/AnimCmd/Type Extensions/ShortExtension.cs +++ b/AnimCmd/Type Extensions/ShortExtension.cs @@ -1,22 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace System -{ - public static class SingleExtension - { - public static unsafe Single Reverse(this Single value) - { - *(uint*)(&value) = ((uint*)&value)->Reverse(); - return value; - } - - //private static double _double2fixmagic = 68719476736.0f * 1.5f; - //public static unsafe Int32 ToInt32(this Single value) - //{ - // double v = value + _double2fixmagic; - // return *((int*)&v) >> 16; - //} - } -} +using System; +using System.Collections.Generic; +using System.Text; + +namespace System +{ + public static class SingleExtension + { + public static unsafe Single Reverse(this Single value) + { + *(uint*)(&value) = ((uint*)&value)->Reverse(); + return value; + } + + //private static double _double2fixmagic = 68719476736.0f * 1.5f; + //public static unsafe Int32 ToInt32(this Single value) + //{ + // double v = value + _double2fixmagic; + // return *((int*)&v) >> 16; + //} + } +} diff --git a/AnimCmd/Type Extensions/StringExtension.cs b/AnimCmd/Type Extensions/StringExtension.cs index ea08543..89fd594 100644 --- a/AnimCmd/Type Extensions/StringExtension.cs +++ b/AnimCmd/Type Extensions/StringExtension.cs @@ -1,47 +1,47 @@ -using System; - -namespace System -{ - public static class StringExtension - { - public static string TruncateAndFill(this string s, int length, char fillChar) - { - int min = length; - min = Math.Max(min, 0); - min = Math.Min(min, s.Length); - - return s.Substring(0, min).PadRight(length, fillChar); - } - - public static string TruncateAndTerminate(this string s, int length) - { - int min = length - 1; - min = Math.Max(min, 0); - min = Math.Min(min, s.Length); - - return s.Substring(0, min).PadRight(length, '\0'); - } - - public static string Terminate(this string s, params char[] terminators) - { - int index = s.IndexOfAny(terminators); - - if (index == -1) - return s; - - return s.Substring(0, index); - } - - public static unsafe int IndexOfOccurance(this string s, char c, int index) - { - int len = s.Length; - fixed (char* cPtr = s) - { - for (int i = 0, count = 0; i < len; i++) - if ((cPtr[i] == c) && (count++ == index)) - return i; - } - return -1; - } - } -} +using System; + +namespace System +{ + public static class StringExtension + { + public static string TruncateAndFill(this string s, int length, char fillChar) + { + int min = length; + min = Math.Max(min, 0); + min = Math.Min(min, s.Length); + + return s.Substring(0, min).PadRight(length, fillChar); + } + + public static string TruncateAndTerminate(this string s, int length) + { + int min = length - 1; + min = Math.Max(min, 0); + min = Math.Min(min, s.Length); + + return s.Substring(0, min).PadRight(length, '\0'); + } + + public static string Terminate(this string s, params char[] terminators) + { + int index = s.IndexOfAny(terminators); + + if (index == -1) + return s; + + return s.Substring(0, index); + } + + public static unsafe int IndexOfOccurance(this string s, char c, int index) + { + int len = s.Length; + fixed (char* cPtr = s) + { + for (int i = 0, count = 0; i < len; i++) + if ((cPtr[i] == c) && (count++ == index)) + return i; + } + return -1; + } + } +} diff --git a/AnimCmd/Type Extensions/UInt16Extension.cs b/AnimCmd/Type Extensions/UInt16Extension.cs index 0132d54..ab00481 100644 --- a/AnimCmd/Type Extensions/UInt16Extension.cs +++ b/AnimCmd/Type Extensions/UInt16Extension.cs @@ -1,13 +1,13 @@ -using System; - -namespace System -{ - public static class UInt16Extension - { - public static UInt16 Reverse(this UInt16 value) - { - return (ushort)((value >> 8) | (value << 8)); - } - } -} - +using System; + +namespace System +{ + public static class UInt16Extension + { + public static UInt16 Reverse(this UInt16 value) + { + return (ushort)((value >> 8) | (value << 8)); + } + } +} + diff --git a/AnimCmd/Type Extensions/UInt32Extension.cs b/AnimCmd/Type Extensions/UInt32Extension.cs index d49ecc3..521cb1a 100644 --- a/AnimCmd/Type Extensions/UInt32Extension.cs +++ b/AnimCmd/Type Extensions/UInt32Extension.cs @@ -1,22 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace System -{ - public static class UInt32Extension - { - public static UInt32 Reverse(this UInt32 value) - { - return ((value >> 24) & 0xFF) | (value << 24) | ((value >> 8) & 0xFF00) | ((value & 0xFF00) << 8); - } - public static UInt32 Align(this UInt32 value, uint align) - { - if (align <= 1) return value; - uint temp = value % align; - if (temp != 0) value += align - temp; - return value; - } - } -} - +using System; +using System.Collections.Generic; +using System.Text; + +namespace System +{ + public static class UInt32Extension + { + public static UInt32 Reverse(this UInt32 value) + { + return ((value >> 24) & 0xFF) | (value << 24) | ((value >> 8) & 0xFF00) | ((value & 0xFF00) << 8); + } + public static UInt32 Align(this UInt32 value, uint align) + { + if (align <= 1) return value; + uint temp = value % align; + if (temp != 0) value += align - temp; + return value; + } + } +} + diff --git a/AnimCmd/Type Extensions/VoidPtr.cs b/AnimCmd/Type Extensions/VoidPtr.cs index b75eb6e..359f745 100644 --- a/AnimCmd/Type Extensions/VoidPtr.cs +++ b/AnimCmd/Type Extensions/VoidPtr.cs @@ -1,49 +1,49 @@ -using System; -using System.Runtime.InteropServices; - -namespace System -{ - [StructLayout(LayoutKind.Sequential, Pack = 1)] - public unsafe struct VoidPtr - { - //address - public void* address; - - public static VoidPtr operator +(VoidPtr p1, VoidPtr p2) { return new VoidPtr() { address = (void*)((long)p1.address + (long)p2.address) }; } - public static VoidPtr operator -(VoidPtr p1, VoidPtr p2) { return new VoidPtr() { address = (void*)((long)p1.address - (long)p2.address) }; } - public static VoidPtr operator +(VoidPtr p1, uint addr) { return new VoidPtr() { address = (void*)((long)p1.address + addr) }; } - public static VoidPtr operator +(VoidPtr p1, int addr) { return new VoidPtr() { address = (void*)((long)p1.address + addr) }; } - public static VoidPtr operator -(VoidPtr p1, int addr) { return new VoidPtr() { address = (void*)((long)p1.address - addr) }; } - public static bool operator >(VoidPtr p1, VoidPtr p2) { return p1.address > p2.address; } - public static bool operator <(VoidPtr p1, VoidPtr p2) { return p1.address < p2.address; } - public static bool operator >=(VoidPtr p1, VoidPtr p2) { return p1.address >= p2.address; } - public static bool operator <=(VoidPtr p1, VoidPtr p2) { return p1.address <= p2.address; } - public static bool operator ==(VoidPtr p1, VoidPtr p2) { return p1.address == p2.address; } - public static bool operator !=(VoidPtr p1, VoidPtr p2) { return p1.address != p2.address; } - - public VoidPtr this[int count, int stride] - { - get { return this + (count * stride); } - } - - //type casts - public static implicit operator bool(VoidPtr ptr) { return ptr.address != null; } - public static implicit operator void*(VoidPtr ptr) { return ptr.address; } - public static implicit operator VoidPtr(void* ptr) { return new VoidPtr() { address = ptr }; } - public static implicit operator uint(VoidPtr ptr) { return (uint)ptr.address; } - public static implicit operator VoidPtr(uint ptr) { return new VoidPtr() { address = (void*)ptr }; } - public static implicit operator int(VoidPtr ptr) { return (int)ptr.address; } - public static implicit operator VoidPtr(int ptr) { return new VoidPtr() { address = (void*)ptr }; } - public static implicit operator VoidPtr(IntPtr ptr) { return new VoidPtr() { address = (void*)ptr }; } - public static implicit operator IntPtr(VoidPtr ptr) { return (IntPtr)ptr.address; } - - public override int GetHashCode() { return (int)address; } - public override bool Equals(object obj) { return base.Equals(obj); } - - public static void Swap(float* p1, float* p2) { float f = *p1; *p1 = *p2; *p2 = f; } - public static void Swap(int* p1, int* p2) { int f = *p1; *p1 = *p2; *p2 = f; } - public static void Swap(short* p1, short* p2) { short f = *p1; *p1 = *p2; *p2 = f; } - public static void Swap(ushort* p1, ushort* p2) { ushort f = *p1; *p1 = *p2; *p2 = f; } - public static void Swap(byte* p1, byte* p2) { byte f = *p1; *p1 = *p2; *p2 = f; } - } -} +using System; +using System.Runtime.InteropServices; + +namespace System +{ + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public unsafe struct VoidPtr + { + //address + public void* address; + + public static VoidPtr operator +(VoidPtr p1, VoidPtr p2) { return new VoidPtr() { address = (void*)((long)p1.address + (long)p2.address) }; } + public static VoidPtr operator -(VoidPtr p1, VoidPtr p2) { return new VoidPtr() { address = (void*)((long)p1.address - (long)p2.address) }; } + public static VoidPtr operator +(VoidPtr p1, uint addr) { return new VoidPtr() { address = (void*)((long)p1.address + addr) }; } + public static VoidPtr operator +(VoidPtr p1, int addr) { return new VoidPtr() { address = (void*)((long)p1.address + addr) }; } + public static VoidPtr operator -(VoidPtr p1, int addr) { return new VoidPtr() { address = (void*)((long)p1.address - addr) }; } + public static bool operator >(VoidPtr p1, VoidPtr p2) { return p1.address > p2.address; } + public static bool operator <(VoidPtr p1, VoidPtr p2) { return p1.address < p2.address; } + public static bool operator >=(VoidPtr p1, VoidPtr p2) { return p1.address >= p2.address; } + public static bool operator <=(VoidPtr p1, VoidPtr p2) { return p1.address <= p2.address; } + public static bool operator ==(VoidPtr p1, VoidPtr p2) { return p1.address == p2.address; } + public static bool operator !=(VoidPtr p1, VoidPtr p2) { return p1.address != p2.address; } + + public VoidPtr this[int count, int stride] + { + get { return this + (count * stride); } + } + + //type casts + public static implicit operator bool(VoidPtr ptr) { return ptr.address != null; } + public static implicit operator void*(VoidPtr ptr) { return ptr.address; } + public static implicit operator VoidPtr(void* ptr) { return new VoidPtr() { address = ptr }; } + public static implicit operator uint(VoidPtr ptr) { return (uint)ptr.address; } + public static implicit operator VoidPtr(uint ptr) { return new VoidPtr() { address = (void*)ptr }; } + public static implicit operator int(VoidPtr ptr) { return (int)ptr.address; } + public static implicit operator VoidPtr(int ptr) { return new VoidPtr() { address = (void*)ptr }; } + public static implicit operator VoidPtr(IntPtr ptr) { return new VoidPtr() { address = (void*)ptr }; } + public static implicit operator IntPtr(VoidPtr ptr) { return (IntPtr)ptr.address; } + + public override int GetHashCode() { return (int)address; } + public override bool Equals(object obj) { return base.Equals(obj); } + + public static void Swap(float* p1, float* p2) { float f = *p1; *p1 = *p2; *p2 = f; } + public static void Swap(int* p1, int* p2) { int f = *p1; *p1 = *p2; *p2 = f; } + public static void Swap(short* p1, short* p2) { short f = *p1; *p1 = *p2; *p2 = f; } + public static void Swap(ushort* p1, ushort* p2) { ushort f = *p1; *p1 = *p2; *p2 = f; } + public static void Swap(byte* p1, byte* p2) { byte f = *p1; *p1 = *p2; *p2 = f; } + } +} diff --git a/AnimCmd/crc32.cs b/AnimCmd/crc32.cs index 0b09b48..c4915b3 100644 --- a/AnimCmd/crc32.cs +++ b/AnimCmd/crc32.cs @@ -1,120 +1,120 @@ - -// Copyright (c) Damien Guard. All rights reserved. -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. -// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -// Originally published at http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net - -using System; -using System.Collections.Generic; -using System.Security.Cryptography; - -namespace System.Security.Cryptography -{ - /// - /// Implements a 32-bit CRC hash algorithm compatible with Zip etc. - /// - /// - /// Crc32 should only be used for backward compatibility with older file formats - /// and algorithms. It is not secure enough for new applications. - /// If you need to call multiple times for the same data either use the HashAlgorithm - /// interface or remember that the result of one Compute call needs to be ~ (XOR) before - /// being passed in as the seed for the next Compute call. - /// - public sealed class Crc32 : HashAlgorithm - { - public const UInt32 DefaultPolynomial = 0xedb88320u; - public const UInt32 DefaultSeed = 0xffffffffu; - - static UInt32[] defaultTable; - - readonly UInt32 seed; - readonly UInt32[] table; - UInt32 hash; - - public Crc32() - : this(DefaultPolynomial, DefaultSeed) - { - } - - public Crc32(UInt32 polynomial, UInt32 seed) - { - table = InitializeTable(polynomial); - this.seed = hash = seed; - } - - public override void Initialize() - { - hash = seed; - } - - protected override void HashCore(byte[] array, int ibStart, int cbSize) - { - hash = CalculateHash(table, hash, array, ibStart, cbSize); - } - - protected override byte[] HashFinal() - { - var hashBuffer = UInt32ToBigEndianBytes(~hash); - HashValue = hashBuffer; - return hashBuffer; - } - - public override int HashSize { get { return 32; } } - - public static UInt32 Compute(byte[] buffer) - { - return Compute(DefaultSeed, buffer); - } - - public static UInt32 Compute(UInt32 seed, byte[] buffer) - { - return Compute(DefaultPolynomial, seed, buffer); - } - - public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer) - { - return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length); - } - - static UInt32[] InitializeTable(UInt32 polynomial) - { - if (polynomial == DefaultPolynomial && defaultTable != null) - return defaultTable; - - var createTable = new UInt32[256]; - for (var i = 0; i < 256; i++) - { - var entry = (UInt32)i; - for (var j = 0; j < 8; j++) - if ((entry & 1) == 1) - entry = (entry >> 1) ^ polynomial; - else - entry = entry >> 1; - createTable[i] = entry; - } - - if (polynomial == DefaultPolynomial) - defaultTable = createTable; - - return createTable; - } - - static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList buffer, int start, int size) - { - var crc = seed; - for (var i = start; i < size - start; i++) - crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff]; - return crc; - } - - static byte[] UInt32ToBigEndianBytes(UInt32 uint32) - { - var result = BitConverter.GetBytes(uint32); - - if (BitConverter.IsLittleEndian) - Array.Reverse(result); - - return result; - } - } + +// Copyright (c) Damien Guard. All rights reserved. +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +// Originally published at http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net + +using System; +using System.Collections.Generic; +using System.Security.Cryptography; + +namespace System.Security.Cryptography +{ + /// + /// Implements a 32-bit CRC hash algorithm compatible with Zip etc. + /// + /// + /// Crc32 should only be used for backward compatibility with older file formats + /// and algorithms. It is not secure enough for new applications. + /// If you need to call multiple times for the same data either use the HashAlgorithm + /// interface or remember that the result of one Compute call needs to be ~ (XOR) before + /// being passed in as the seed for the next Compute call. + /// + public sealed class Crc32 : HashAlgorithm + { + public const UInt32 DefaultPolynomial = 0xedb88320u; + public const UInt32 DefaultSeed = 0xffffffffu; + + static UInt32[] defaultTable; + + readonly UInt32 seed; + readonly UInt32[] table; + UInt32 hash; + + public Crc32() + : this(DefaultPolynomial, DefaultSeed) + { + } + + public Crc32(UInt32 polynomial, UInt32 seed) + { + table = InitializeTable(polynomial); + this.seed = hash = seed; + } + + public override void Initialize() + { + hash = seed; + } + + protected override void HashCore(byte[] array, int ibStart, int cbSize) + { + hash = CalculateHash(table, hash, array, ibStart, cbSize); + } + + protected override byte[] HashFinal() + { + var hashBuffer = UInt32ToBigEndianBytes(~hash); + HashValue = hashBuffer; + return hashBuffer; + } + + public override int HashSize { get { return 32; } } + + public static UInt32 Compute(byte[] buffer) + { + return Compute(DefaultSeed, buffer); + } + + public static UInt32 Compute(UInt32 seed, byte[] buffer) + { + return Compute(DefaultPolynomial, seed, buffer); + } + + public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer) + { + return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length); + } + + static UInt32[] InitializeTable(UInt32 polynomial) + { + if (polynomial == DefaultPolynomial && defaultTable != null) + return defaultTable; + + var createTable = new UInt32[256]; + for (var i = 0; i < 256; i++) + { + var entry = (UInt32)i; + for (var j = 0; j < 8; j++) + if ((entry & 1) == 1) + entry = (entry >> 1) ^ polynomial; + else + entry = entry >> 1; + createTable[i] = entry; + } + + if (polynomial == DefaultPolynomial) + defaultTable = createTable; + + return createTable; + } + + static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList buffer, int start, int size) + { + var crc = seed; + for (var i = start; i < size - start; i++) + crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff]; + return crc; + } + + static byte[] UInt32ToBigEndianBytes(UInt32 uint32) + { + var result = BitConverter.GetBytes(uint32); + + if (BitConverter.IsLittleEndian) + Array.Reverse(result); + + return result; + } + } } \ No newline at end of file