aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/General/Util.cs
diff options
context:
space:
mode:
authorMW2007-06-27 15:28:52 +0000
committerMW2007-06-27 15:28:52 +0000
commit646bbbc84b8010e0dacbeed5342cdb045f46cc49 (patch)
tree770b34d19855363c3c113ab9a0af9a56d821d887 /OpenSim/Framework/General/Util.cs
downloadopensim-SC_OLD-646bbbc84b8010e0dacbeed5342cdb045f46cc49.zip
opensim-SC_OLD-646bbbc84b8010e0dacbeed5342cdb045f46cc49.tar.gz
opensim-SC_OLD-646bbbc84b8010e0dacbeed5342cdb045f46cc49.tar.bz2
opensim-SC_OLD-646bbbc84b8010e0dacbeed5342cdb045f46cc49.tar.xz
Some work on restructuring the namespaces / project names. Note this doesn't compile yet as not all the code has been changed to use the new namespaces. Am committing it now for feedback on the namespaces.
Diffstat (limited to 'OpenSim/Framework/General/Util.cs')
-rw-r--r--OpenSim/Framework/General/Util.cs186
1 files changed, 186 insertions, 0 deletions
diff --git a/OpenSim/Framework/General/Util.cs b/OpenSim/Framework/General/Util.cs
new file mode 100644
index 0000000..8c34c9b
--- /dev/null
+++ b/OpenSim/Framework/General/Util.cs
@@ -0,0 +1,186 @@
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 string GetRandomCapsPath()
85 {
86 LLUUID caps = LLUUID.Random();
87 string capsPath = caps.ToStringHyphenated();
88 capsPath = capsPath.Remove(capsPath.Length - 4, 4);
89 return capsPath;
90 }
91
92 //public static int fast_distance2d(int x, int y)
93 //{
94 // x = System.Math.Abs(x);
95 // y = System.Math.Abs(y);
96
97 // int min = System.Math.Min(x, y);
98
99 // return (x + y - (min >> 1) - (min >> 2) + (min >> 4));
100 //}
101
102 public static string FieldToString(byte[] bytes)
103 {
104 return FieldToString(bytes, String.Empty);
105 }
106
107 /// <summary>
108 /// Convert a variable length field (byte array) to a string, with a
109 /// field name prepended to each line of the output
110 /// </summary>
111 /// <remarks>If the byte array has unprintable characters in it, a
112 /// hex dump will be put in the string instead</remarks>
113 /// <param name="bytes">The byte array to convert to a string</param>
114 /// <param name="fieldName">A field name to prepend to each line of output</param>
115 /// <returns>An ASCII string or a string containing a hex dump, minus
116 /// the null terminator</returns>
117 public static string FieldToString(byte[] bytes, string fieldName)
118 {
119 // Check for a common case
120 if (bytes.Length == 0) return String.Empty;
121
122 StringBuilder output = new StringBuilder();
123 bool printable = true;
124
125 for (int i = 0; i < bytes.Length; ++i)
126 {
127 // Check if there are any unprintable characters in the array
128 if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09
129 && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00)
130 {
131 printable = false;
132 break;
133 }
134 }
135
136 if (printable)
137 {
138 if (fieldName.Length > 0)
139 {
140 output.Append(fieldName);
141 output.Append(": ");
142 }
143
144 if (bytes[bytes.Length - 1] == 0x00)
145 output.Append(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1));
146 else
147 output.Append(UTF8Encoding.UTF8.GetString(bytes));
148 }
149 else
150 {
151 for (int i = 0; i < bytes.Length; i += 16)
152 {
153 if (i != 0)
154 output.Append(Environment.NewLine);
155 if (fieldName.Length > 0)
156 {
157 output.Append(fieldName);
158 output.Append(": ");
159 }
160
161 for (int j = 0; j < 16; j++)
162 {
163 if ((i + j) < bytes.Length)
164 output.Append(String.Format("{0:X2} ", bytes[i + j]));
165 else
166 output.Append(" ");
167 }
168
169 for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
170 {
171 if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
172 output.Append((char)bytes[i + j]);
173 else
174 output.Append(".");
175 }
176 }
177 }
178
179 return output.ToString();
180 }
181 public Util()
182 {
183
184 }
185 }
186}