Util.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Security.Cryptography;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using libsecondlife;
  33. using libsecondlife.Packets;
  34. namespace OpenSim.Framework.Utilities
  35. {
  36. public class Util
  37. {
  38. private static Random randomClass = new Random();
  39. private static uint nextXferID = 5000;
  40. private static object XferLock = new object();
  41. public static ulong UIntsToLong(uint X, uint Y)
  42. {
  43. return Helpers.UIntsToLong(X, Y);
  44. }
  45. public static Random RandomClass
  46. {
  47. get
  48. {
  49. return randomClass;
  50. }
  51. }
  52. public static uint GetNextXferID()
  53. {
  54. uint id = 0;
  55. lock(XferLock)
  56. {
  57. id = nextXferID;
  58. nextXferID++;
  59. }
  60. return id;
  61. }
  62. public static int UnixTimeSinceEpoch()
  63. {
  64. TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
  65. int timestamp = (int)t.TotalSeconds;
  66. return timestamp;
  67. }
  68. public static string Md5Hash(string pass)
  69. {
  70. MD5 md5 = MD5CryptoServiceProvider.Create();
  71. byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pass));
  72. StringBuilder sb = new StringBuilder();
  73. for (int i = 0; i < dataMd5.Length; i++)
  74. sb.AppendFormat("{0:x2}", dataMd5[i]);
  75. return sb.ToString();
  76. }
  77. //public static int fast_distance2d(int x, int y)
  78. //{
  79. // x = System.Math.Abs(x);
  80. // y = System.Math.Abs(y);
  81. // int min = System.Math.Min(x, y);
  82. // return (x + y - (min >> 1) - (min >> 2) + (min >> 4));
  83. //}
  84. public static string FieldToString(byte[] bytes)
  85. {
  86. return FieldToString(bytes, String.Empty);
  87. }
  88. /// <summary>
  89. /// Convert a variable length field (byte array) to a string, with a
  90. /// field name prepended to each line of the output
  91. /// </summary>
  92. /// <remarks>If the byte array has unprintable characters in it, a
  93. /// hex dump will be put in the string instead</remarks>
  94. /// <param name="bytes">The byte array to convert to a string</param>
  95. /// <param name="fieldName">A field name to prepend to each line of output</param>
  96. /// <returns>An ASCII string or a string containing a hex dump, minus
  97. /// the null terminator</returns>
  98. public static string FieldToString(byte[] bytes, string fieldName)
  99. {
  100. // Check for a common case
  101. if (bytes.Length == 0) return String.Empty;
  102. StringBuilder output = new StringBuilder();
  103. bool printable = true;
  104. for (int i = 0; i < bytes.Length; ++i)
  105. {
  106. // Check if there are any unprintable characters in the array
  107. if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09
  108. && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00)
  109. {
  110. printable = false;
  111. break;
  112. }
  113. }
  114. if (printable)
  115. {
  116. if (fieldName.Length > 0)
  117. {
  118. output.Append(fieldName);
  119. output.Append(": ");
  120. }
  121. if (bytes[bytes.Length - 1] == 0x00)
  122. output.Append(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1));
  123. else
  124. output.Append(UTF8Encoding.UTF8.GetString(bytes));
  125. }
  126. else
  127. {
  128. for (int i = 0; i < bytes.Length; i += 16)
  129. {
  130. if (i != 0)
  131. output.Append(Environment.NewLine);
  132. if (fieldName.Length > 0)
  133. {
  134. output.Append(fieldName);
  135. output.Append(": ");
  136. }
  137. for (int j = 0; j < 16; j++)
  138. {
  139. if ((i + j) < bytes.Length)
  140. output.Append(String.Format("{0:X2} ", bytes[i + j]));
  141. else
  142. output.Append(" ");
  143. }
  144. for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
  145. {
  146. if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
  147. output.Append((char)bytes[i + j]);
  148. else
  149. output.Append(".");
  150. }
  151. }
  152. }
  153. return output.ToString();
  154. }
  155. public Util()
  156. {
  157. }
  158. }
  159. }