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