Instantly share code, notes, and snippets.

View ResizeEx.cs
public class ResizeEx
{
/// <summary>
/// Resizes a one-dimensional array to the specified new size.
/// </summary>
public static void Resize<T>(ref T[] array, int newSize)
{
if(newSize <= 0)
throw new ArgumentOutOfRangeException($@"New Size {newSize} must be greater than 0.");
if(array == null)
View KvPair.cs
[Serializable]
public class KvPair<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable
{
public KvPair(T1 key, T2 value)
{
Key = key;
Value = value;
}
public T1 Key {get;}
public T2 Value{get;}
View CopyFileEx.cs
public bool CopyFileEx(string sourceFile, string targetFile)
{
long bytesLeft = 0;
long bytesProcessed = 0;
long sectorsCopied = 0;
var fileinfo = new FileInfo(sourceFile);
var fileFragments = FileFragments.GetFileAllocation(sourceFile);
if(fileFragments.TotalFragments == 0 || fileinfo.Length == 0)
return false;
var device = new Device(512);
View FileFragments.cs
public class FileFragments
{
private const int FSCTL_GET_RETRIEVAL_POINTERS = 0x00090073;
private const uint FileAccess_GenericRead = 0x80000000;
private const uint FileAccess_GenericWrite = 0x40000000;
private const uint FileShare_Read = 0x00000001;
private const uint FileShare_Write = 0x00000002;
private const uint FileShare_ReadWrite = FileShare_Read | FileShare_Write;
private const uint FileMode_OpenExisting = 0x00000003;
private const uint FileAttributes_Normal = 0x00000080;
View Device.cs
public class Device
{
public enum CreationDisposition : uint
{
CREATE_NEW = 1,
CREATE_ALWAYS = 2,
OPEN_EXISTING = 3,
OPEN_ALWAYS = 4,
TRUNCATE_EXSTING = 5
}
View HashAlgorithmHelper.cs
public static class HashAlgorithmHelper
{
public static byte[] UpdateHash(this HashAlgorithm hash, byte[] buffer)
{
hash.TransformBlock(buffer, 0, buffer.Length, null, 0);
hash.TransformFinalBlock(new byte[0], 0, 0);
hash.Final();
return hash.Hash;
}
View FileHelperEx.cs
/// <summary>
/// Example:
/// var fh = new FileHelperEx();
/// var hash = new SHA512CryptoServiceProvider();
/// hash.Initialize();
/// fh.ReadStream(fileName, buffer => hash.TransformBlock(buffer, 0, buffer.Length, null, 0));
/// hash.Final();
/// var sha512 = new BigInteger(hash.Hash);
/// </summary>
public class FileHelperEx
View ArrayHelper.cs
public static class ArrayHelper
{
/// <summary>
/// Clear T type single dimensional array to either the default value or a set value. (ArrayHelper.cs)
/// </summary>
public static void Clear<T>(this T[] array, T value = default)
{
if (array == null)
throw new Exception("The array can be null.");
for (var i = 0; i < array.Length; i++)
View Converters.cs
public static class Converters
{
public static bool IsValidPrimitive<T>()
{
var type = typeof(T);
switch(Type.GetTypeCode(type))
{
case TypeCode.Boolean:
case TypeCode.Char:
case TypeCode.SByte:
View BMPartialPatternSearch.cs
public class BMPartialPatternSearch
{
public int MinimumSearchLength;
public BMPartialPatternSearch(int min = 5)
{
MinimumSearchLength = min;
}
public(byte[] partialPattern, int idx) SearchPartial(byte[] pattern, byte[] searchArray)
{
var bm = new BoyerMoore(pattern);