Guest User

Untitled

a guest
Jul 1st, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Security.Cryptography;
  6. //using XStep.LUAMAN;
  7.  
  8. namespace XStep
  9. {
  10. static class Utils
  11. {
  12. #region clamp
  13.  
  14. public static byte Clamp(byte x, byte min, byte max)
  15. { return Math.Max(min, Math.Min(x, max)); }
  16. public static int Clamp(int x, int min, int max)
  17. { return Math.Max(min, Math.Min(x, max)); }
  18. public static uint Clamp(uint x, uint min, uint max)
  19. { return Math.Max(min, Math.Min(x, max)); }
  20. public static long Clamp(long x, long min, long max)
  21. { return Math.Max(min, Math.Min(x, max)); }
  22. public static ulong Clamp(ulong x, ulong min, ulong max)
  23. { return Math.Max(min, Math.Min(x, max)); }
  24. public static float Clamp(float x, float min, float max)
  25. { return Math.Max(min, Math.Min(x, max)); }
  26. public static double Clamp(double x, double min, double max)
  27. { return Math.Max(min, Math.Min(x, max)); }
  28.  
  29. #endregion
  30.  
  31. #region scale
  32.  
  33. public static byte Scale(byte x, byte srcmin, byte srcmax, byte destmin, byte destmax)
  34. { return (byte)(((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin)); }
  35. public static int Scale(int x, int srcmin, int srcmax, int destmin, int destmax)
  36. { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
  37. public static uint Scale(uint x, uint srcmin, uint srcmax, uint destmin, uint destmax)
  38. { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
  39. public static long Scale(long x, long srcmin, long srcmax, long destmin, long destmax)
  40. { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
  41. public static ulong Scale(ulong x, ulong srcmin, ulong srcmax, ulong destmin, ulong destmax)
  42. { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
  43. public static float Scale(float x, float srcmin, float srcmax, float destmin, float destmax)
  44. { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
  45. public static double Scale(double x, double srcmin, double srcmax, double destmin, double destmax)
  46. { return ((x - srcmin) * (destmax - destmin)) / ((srcmax - srcmin) + destmin); }
  47.  
  48. #endregion
  49.  
  50. #region wrap
  51.  
  52. public static byte Wrap(byte x, byte limit)
  53. { return (byte)(x % limit); }
  54. public static uint Wrap(uint x, uint limit)
  55. { return x % limit; }
  56. public static ulong Wrap(ulong x, ulong limit)
  57. { return x % limit; }
  58. public static int Wrap(int x, int limit)
  59. { if (x < 0) x = limit - 1; return x % limit; }
  60. public static long Wrap(long x, long limit)
  61. { if (x < 0) x = limit - 1; return x % limit; }
  62.  
  63. #endregion
  64. }
  65.  
  66. static class Crypto
  67. {
  68. #region Adler32
  69.  
  70. public static int GetAdler32Bytes(byte[] data)
  71. {
  72. int a = 0;
  73. int b = 1;
  74. int mod = 65521;
  75.  
  76. for (int idx = 0; idx < data.Length; idx++)
  77. {
  78. a = (a + data[idx]) % mod;
  79. b = (b + a) % mod;
  80. }
  81.  
  82. return (b << 16) | a;
  83. }
  84.  
  85. #endregion
  86.  
  87. #region MD5
  88.  
  89. public static byte[] GetMD5Hash(byte[] data)
  90. { return MD5.Create().ComputeHash(data); }
  91. public static byte[] GetMD5Hash(string text)
  92. { return GetMD5Hash(Encoding.Default.GetBytes(text)); }
  93.  
  94. #endregion
  95.  
  96. #region SHA1
  97.  
  98. public static byte[] GetSHA1Hash(byte[] data)
  99. { return SHA1.Create().ComputeHash(data); }
  100. public static byte[] GetSHA1Hash(string text)
  101. { return GetSHA1Hash(Encoding.Default.GetBytes(text)); }
  102.  
  103. #endregion
  104.  
  105. #region SillyNice
  106.  
  107. public static byte[] SillyCrypt(byte[] data)
  108. {
  109. for (int i = 0; i < data.Length; i++)
  110. {
  111. data[i] = (byte)~data[i];
  112. //data[i] ^= (byte)(data.Length * i);
  113. }
  114. return data;
  115. }
  116.  
  117. public static byte[] NiceCrypt(byte[] data)
  118. {
  119. throw new NotSupportedException("NiceCrypt is not yet supported");
  120. //return data;
  121. }
  122.  
  123. #endregion
  124. }
  125.  
  126. static class FileUtil
  127. {
  128. public static string[] GetImagesFromDir(string directory)
  129. {
  130. List<string> ls = new List<string>();
  131.  
  132. ls.AddRange(Directory.GetFiles(directory, "*.png", SearchOption.TopDirectoryOnly));
  133. ls.AddRange(Directory.GetFiles(directory, "*.jpg", SearchOption.TopDirectoryOnly));
  134. ls.AddRange(Directory.GetFiles(directory, "*.bmp", SearchOption.TopDirectoryOnly));
  135.  
  136. return ls.ToArray();
  137. }
  138. #if false
  139. public static void Compress(string infi, string oufi)
  140. { File.WriteAllBytes(oufi, SevenZipHelper.Compress(File.ReadAllBytes(infi))); }
  141. public static void Decompress(string infi, string oufi)
  142. { File.WriteAllBytes(oufi, SevenZipHelper.Decompress(File.ReadAllBytes(infi))); }
  143. #endif
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment