Util.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.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. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using libsecondlife;
  31. using libsecondlife.Packets;
  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 fast_distance2d(int x, int y)
  61. //{
  62. // x = System.Math.Abs(x);
  63. // y = System.Math.Abs(y);
  64. // int min = System.Math.Min(x, y);
  65. // return (x + y - (min >> 1) - (min >> 2) + (min >> 4));
  66. //}
  67. public static string FieldToString(byte[] bytes)
  68. {
  69. return FieldToString(bytes, String.Empty);
  70. }
  71. /// <summary>
  72. /// Convert a variable length field (byte array) to a string, with a
  73. /// field name prepended to each line of the output
  74. /// </summary>
  75. /// <remarks>If the byte array has unprintable characters in it, a
  76. /// hex dump will be put in the string instead</remarks>
  77. /// <param name="bytes">The byte array to convert to a string</param>
  78. /// <param name="fieldName">A field name to prepend to each line of output</param>
  79. /// <returns>An ASCII string or a string containing a hex dump, minus
  80. /// the null terminator</returns>
  81. public static string FieldToString(byte[] bytes, string fieldName)
  82. {
  83. // Check for a common case
  84. if (bytes.Length == 0) return String.Empty;
  85. StringBuilder output = new StringBuilder();
  86. bool printable = true;
  87. for (int i = 0; i < bytes.Length; ++i)
  88. {
  89. // Check if there are any unprintable characters in the array
  90. if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09
  91. && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00)
  92. {
  93. printable = false;
  94. break;
  95. }
  96. }
  97. if (printable)
  98. {
  99. if (fieldName.Length > 0)
  100. {
  101. output.Append(fieldName);
  102. output.Append(": ");
  103. }
  104. if (bytes[bytes.Length - 1] == 0x00)
  105. output.Append(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1));
  106. else
  107. output.Append(UTF8Encoding.UTF8.GetString(bytes));
  108. }
  109. else
  110. {
  111. for (int i = 0; i < bytes.Length; i += 16)
  112. {
  113. if (i != 0)
  114. output.Append(Environment.NewLine);
  115. if (fieldName.Length > 0)
  116. {
  117. output.Append(fieldName);
  118. output.Append(": ");
  119. }
  120. for (int j = 0; j < 16; j++)
  121. {
  122. if ((i + j) < bytes.Length)
  123. output.Append(String.Format("{0:X2} ", bytes[i + j]));
  124. else
  125. output.Append(" ");
  126. }
  127. for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
  128. {
  129. if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
  130. output.Append((char)bytes[i + j]);
  131. else
  132. output.Append(".");
  133. }
  134. }
  135. }
  136. return output.ToString();
  137. }
  138. public Util()
  139. {
  140. }
  141. }
  142. }