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