Util.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.Text;
  31. using libsecondlife;
  32. namespace OpenSim.Framework.Utilities
  33. {
  34. public class Util
  35. {
  36. private static Random randomClass = new Random();
  37. private static uint nextXferID = 5000;
  38. private static object XferLock = new object();
  39. public static ulong UIntsToLong(uint X, uint Y)
  40. {
  41. return Helpers.UIntsToLong(X, Y);
  42. }
  43. public static Random RandomClass
  44. {
  45. get
  46. {
  47. return randomClass;
  48. }
  49. }
  50. public static uint GetNextXferID()
  51. {
  52. uint id = 0;
  53. lock(XferLock)
  54. {
  55. id = nextXferID;
  56. nextXferID++;
  57. }
  58. return id;
  59. }
  60. public static int UnixTimeSinceEpoch()
  61. {
  62. TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
  63. int timestamp = (int)t.TotalSeconds;
  64. return timestamp;
  65. }
  66. public static string Md5Hash(string pass)
  67. {
  68. MD5 md5 = MD5CryptoServiceProvider.Create();
  69. byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pass));
  70. StringBuilder sb = new StringBuilder();
  71. for (int i = 0; i < dataMd5.Length; i++)
  72. sb.AppendFormat("{0:x2}", dataMd5[i]);
  73. return sb.ToString();
  74. }
  75. public static string GetRandomCapsPath()
  76. {
  77. LLUUID caps = LLUUID.Random();
  78. string capsPath = caps.ToStringHyphenated();
  79. capsPath = capsPath.Remove(capsPath.Length - 4, 4);
  80. return capsPath;
  81. }
  82. //public static int fast_distance2d(int x, int y)
  83. //{
  84. // x = System.Math.Abs(x);
  85. // y = System.Math.Abs(y);
  86. // int min = System.Math.Min(x, y);
  87. // return (x + y - (min >> 1) - (min >> 2) + (min >> 4));
  88. //}
  89. public static string FieldToString(byte[] bytes)
  90. {
  91. return FieldToString(bytes, String.Empty);
  92. }
  93. /// <summary>
  94. /// Convert a variable length field (byte array) to a string, with a
  95. /// field name prepended to each line of the output
  96. /// </summary>
  97. /// <remarks>If the byte array has unprintable characters in it, a
  98. /// hex dump will be put in the string instead</remarks>
  99. /// <param name="bytes">The byte array to convert to a string</param>
  100. /// <param name="fieldName">A field name to prepend to each line of output</param>
  101. /// <returns>An ASCII string or a string containing a hex dump, minus
  102. /// the null terminator</returns>
  103. public static string FieldToString(byte[] bytes, string fieldName)
  104. {
  105. // Check for a common case
  106. if (bytes.Length == 0) return String.Empty;
  107. StringBuilder output = new StringBuilder();
  108. bool printable = true;
  109. for (int i = 0; i < bytes.Length; ++i)
  110. {
  111. // Check if there are any unprintable characters in the array
  112. if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09
  113. && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00)
  114. {
  115. printable = false;
  116. break;
  117. }
  118. }
  119. if (printable)
  120. {
  121. if (fieldName.Length > 0)
  122. {
  123. output.Append(fieldName);
  124. output.Append(": ");
  125. }
  126. if (bytes[bytes.Length - 1] == 0x00)
  127. output.Append(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1));
  128. else
  129. output.Append(UTF8Encoding.UTF8.GetString(bytes));
  130. }
  131. else
  132. {
  133. for (int i = 0; i < bytes.Length; i += 16)
  134. {
  135. if (i != 0)
  136. output.Append(Environment.NewLine);
  137. if (fieldName.Length > 0)
  138. {
  139. output.Append(fieldName);
  140. output.Append(": ");
  141. }
  142. for (int j = 0; j < 16; j++)
  143. {
  144. if ((i + j) < bytes.Length)
  145. output.Append(String.Format("{0:X2} ", bytes[i + j]));
  146. else
  147. output.Append(" ");
  148. }
  149. for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
  150. {
  151. if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
  152. output.Append((char)bytes[i + j]);
  153. else
  154. output.Append(".");
  155. }
  156. }
  157. }
  158. return output.ToString();
  159. }
  160. public Util()
  161. {
  162. }
  163. }
  164. }