- using System;
- using System.IO;
- using System.Text;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- //using XStep.LUAMAN;
- namespace XStep
- {
- static class Utils
- {
- #region clamp
- public static byte Clamp(byte x, byte min, byte max)
- { return Math.Max(min, Math.Min(x, max)); }
- public static int Clamp(int x, int min, int max)
- { return Math.Max(min, Math.Min(x, max)); }
- public static uint Clamp(uint x, uint min, uint max)
- { return Math.Max(min, Math.Min(x, max)); }
- public static long Clamp(long x, long min, long max)
- { return Math.Max(min, Math.Min(x, max)); }
- public static ulong Clamp(ulong x, ulong min, ulong max)
- { return Math.Max(min, Math.Min(x, max)); }
- public static float Clamp(float x, float min, float max)
- { return Math.Max(min, Math.Min(x, max)); }
- public static double Clamp(double x, double min, double max)
- { return Math.Max(min, Math.Min(x, max)); }
- #endregion
- #region scale
- public static byte Scale(byte x, byte srcmin, byte srcmax, byte destmin, byte destmax)
- { return (byte)(((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin)); }
- public static int Scale(int x, int srcmin, int srcmax, int destmin, int destmax)
- { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
- public static uint Scale(uint x, uint srcmin, uint srcmax, uint destmin, uint destmax)
- { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
- public static long Scale(long x, long srcmin, long srcmax, long destmin, long destmax)
- { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
- public static ulong Scale(ulong x, ulong srcmin, ulong srcmax, ulong destmin, ulong destmax)
- { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
- public static float Scale(float x, float srcmin, float srcmax, float destmin, float destmax)
- { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
- public static double Scale(double x, double srcmin, double srcmax, double destmin, double destmax)
- { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
- #endregion
- #region wrap
- public static byte Wrap(byte x, byte limit)
- { return (byte)(x % limit); }
- public static uint Wrap(uint x, uint limit)
- { return x % limit; }
- public static ulong Wrap(ulong x, ulong limit)
- { return x % limit; }
- public static int Wrap(int x, int limit)
- { if (x < 0) x = limit - 1; return x % limit; }
- public static long Wrap(long x, long limit)
- { if (x < 0) x = limit - 1; return x % limit; }
- #endregion
- }
- static class Crypto
- {
- #region Adler32
- public static int GetAdler32Bytes(byte[] data)
- {
- int a = 0;
- int b = 1;
- int mod = 65521;
- for (int idx = 0; idx < data.Length; idx++)
- {
- a = (a + data[idx]) % mod;
- b = (b + a) % mod;
- }
- return (b << 16) | a;
- }
- #endregion
- #region MD5
- public static byte[] GetMD5Hash(byte[] data)
- { return MD5.Create().ComputeHash(data); }
- public static byte[] GetMD5Hash(string text)
- { return GetMD5Hash(Encoding.Default.GetBytes(text)); }
- #endregion
- #region SHA1
- public static byte[] GetSHA1Hash(byte[] data)
- { return SHA1.Create().ComputeHash(data); }
- public static byte[] GetSHA1Hash(string text)
- { return GetSHA1Hash(Encoding.Default.GetBytes(text)); }
- #endregion
- #region SillyNice
- public static byte[] SillyCrypt(byte[] data)
- {
- for (int i = 0; i < data.Length; i++)
- {
- data[i] = (byte)~data[i];
- //data[i] ^= (byte)(data.Length * i);
- }
- return data;
- }
- public static byte[] NiceCrypt(byte[] data)
- {
- throw new NotSupportedException("NiceCrypt is not yet supported");
- //return data;
- }
- #endregion
- }
- static class FileUtil
- {
- public static string[] GetImagesFromDir(string directory)
- {
- List<string> ls = new List<string>();
- ls.AddRange(Directory.GetFiles(directory, "*.png", SearchOption.TopDirectoryOnly));
- ls.AddRange(Directory.GetFiles(directory, "*.jpg", SearchOption.TopDirectoryOnly));
- ls.AddRange(Directory.GetFiles(directory, "*.bmp", SearchOption.TopDirectoryOnly));
- return ls.ToArray();
- }
- #if false
- public static void Compress(string infi, string oufi)
- { File.WriteAllBytes(oufi, SevenZipHelper.Compress(File.ReadAllBytes(infi))); }
- public static void Decompress(string infi, string oufi)
- { File.WriteAllBytes(oufi, SevenZipHelper.Decompress(File.ReadAllBytes(infi))); }
- #endif
- }
- }