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