diff options
Diffstat (limited to 'OpenSim/Framework/General/Util.cs')
-rw-r--r-- | OpenSim/Framework/General/Util.cs | 368 |
1 files changed, 184 insertions, 184 deletions
diff --git a/OpenSim/Framework/General/Util.cs b/OpenSim/Framework/General/Util.cs index 13e3af2..3333ced 100644 --- a/OpenSim/Framework/General/Util.cs +++ b/OpenSim/Framework/General/Util.cs | |||
@@ -1,184 +1,184 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (c) Contributors, http://www.openmetaverse.org/ | 2 | * Copyright (c) Contributors, http://www.openmetaverse.org/ |
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | 3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. |
4 | * | 4 | * |
5 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: | 6 | * modification, are permitted provided that the following conditions are met: |
7 | * * Redistributions of source code must retain the above copyright | 7 | * * Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. | 8 | * notice, this list of conditions and the following disclaimer. |
9 | * * Redistributions in binary form must reproduce the above copyright | 9 | * * Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the | 10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. | 11 | * documentation and/or other materials provided with the distribution. |
12 | * * Neither the name of the OpenSim Project nor the | 12 | * * Neither the name of the OpenSim Project nor the |
13 | * names of its contributors may be used to endorse or promote products | 13 | * names of its contributors may be used to endorse or promote products |
14 | * derived from this software without specific prior written permission. | 14 | * derived from this software without specific prior written permission. |
15 | * | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY | 16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY |
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | 19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * | 26 | * |
27 | */ | 27 | */ |
28 | using System; | 28 | using System; |
29 | using System.Security.Cryptography; | 29 | using System.Security.Cryptography; |
30 | using System.Text; | 30 | using System.Text; |
31 | using libsecondlife; | 31 | using libsecondlife; |
32 | 32 | ||
33 | namespace OpenSim.Framework.Utilities | 33 | namespace OpenSim.Framework.Utilities |
34 | { | 34 | { |
35 | public class Util | 35 | public class Util |
36 | { | 36 | { |
37 | private static Random randomClass = new Random(); | 37 | private static Random randomClass = new Random(); |
38 | private static uint nextXferID = 5000; | 38 | private static uint nextXferID = 5000; |
39 | private static object XferLock = new object(); | 39 | private static object XferLock = new object(); |
40 | 40 | ||
41 | public static ulong UIntsToLong(uint X, uint Y) | 41 | public static ulong UIntsToLong(uint X, uint Y) |
42 | { | 42 | { |
43 | return Helpers.UIntsToLong(X, Y); | 43 | return Helpers.UIntsToLong(X, Y); |
44 | } | 44 | } |
45 | 45 | ||
46 | public static Random RandomClass | 46 | public static Random RandomClass |
47 | { | 47 | { |
48 | get | 48 | get |
49 | { | 49 | { |
50 | return randomClass; | 50 | return randomClass; |
51 | } | 51 | } |
52 | } | 52 | } |
53 | 53 | ||
54 | public static uint GetNextXferID() | 54 | public static uint GetNextXferID() |
55 | { | 55 | { |
56 | uint id = 0; | 56 | uint id = 0; |
57 | lock(XferLock) | 57 | lock(XferLock) |
58 | { | 58 | { |
59 | id = nextXferID; | 59 | id = nextXferID; |
60 | nextXferID++; | 60 | nextXferID++; |
61 | } | 61 | } |
62 | return id; | 62 | return id; |
63 | } | 63 | } |
64 | 64 | ||
65 | public static int UnixTimeSinceEpoch() | 65 | public static int UnixTimeSinceEpoch() |
66 | { | 66 | { |
67 | TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1)); | 67 | TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1)); |
68 | int timestamp = (int)t.TotalSeconds; | 68 | int timestamp = (int)t.TotalSeconds; |
69 | return timestamp; | 69 | return timestamp; |
70 | } | 70 | } |
71 | 71 | ||
72 | public static string Md5Hash(string pass) | 72 | public static string Md5Hash(string pass) |
73 | { | 73 | { |
74 | MD5 md5 = MD5CryptoServiceProvider.Create(); | 74 | MD5 md5 = MD5CryptoServiceProvider.Create(); |
75 | byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pass)); | 75 | byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pass)); |
76 | StringBuilder sb = new StringBuilder(); | 76 | StringBuilder sb = new StringBuilder(); |
77 | for (int i = 0; i < dataMd5.Length; i++) | 77 | for (int i = 0; i < dataMd5.Length; i++) |
78 | sb.AppendFormat("{0:x2}", dataMd5[i]); | 78 | sb.AppendFormat("{0:x2}", dataMd5[i]); |
79 | return sb.ToString(); | 79 | return sb.ToString(); |
80 | } | 80 | } |
81 | 81 | ||
82 | public static string GetRandomCapsPath() | 82 | public static string GetRandomCapsPath() |
83 | { | 83 | { |
84 | LLUUID caps = LLUUID.Random(); | 84 | LLUUID caps = LLUUID.Random(); |
85 | string capsPath = caps.ToStringHyphenated(); | 85 | string capsPath = caps.ToStringHyphenated(); |
86 | capsPath = capsPath.Remove(capsPath.Length - 4, 4); | 86 | capsPath = capsPath.Remove(capsPath.Length - 4, 4); |
87 | return capsPath; | 87 | return capsPath; |
88 | } | 88 | } |
89 | 89 | ||
90 | //public static int fast_distance2d(int x, int y) | 90 | //public static int fast_distance2d(int x, int y) |
91 | //{ | 91 | //{ |
92 | // x = System.Math.Abs(x); | 92 | // x = System.Math.Abs(x); |
93 | // y = System.Math.Abs(y); | 93 | // y = System.Math.Abs(y); |
94 | 94 | ||
95 | // int min = System.Math.Min(x, y); | 95 | // int min = System.Math.Min(x, y); |
96 | 96 | ||
97 | // return (x + y - (min >> 1) - (min >> 2) + (min >> 4)); | 97 | // return (x + y - (min >> 1) - (min >> 2) + (min >> 4)); |
98 | //} | 98 | //} |
99 | 99 | ||
100 | public static string FieldToString(byte[] bytes) | 100 | public static string FieldToString(byte[] bytes) |
101 | { | 101 | { |
102 | return FieldToString(bytes, String.Empty); | 102 | return FieldToString(bytes, String.Empty); |
103 | } | 103 | } |
104 | 104 | ||
105 | /// <summary> | 105 | /// <summary> |
106 | /// Convert a variable length field (byte array) to a string, with a | 106 | /// Convert a variable length field (byte array) to a string, with a |
107 | /// field name prepended to each line of the output | 107 | /// field name prepended to each line of the output |
108 | /// </summary> | 108 | /// </summary> |
109 | /// <remarks>If the byte array has unprintable characters in it, a | 109 | /// <remarks>If the byte array has unprintable characters in it, a |
110 | /// hex dump will be put in the string instead</remarks> | 110 | /// hex dump will be put in the string instead</remarks> |
111 | /// <param name="bytes">The byte array to convert to a string</param> | 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> | 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 | 113 | /// <returns>An ASCII string or a string containing a hex dump, minus |
114 | /// the null terminator</returns> | 114 | /// the null terminator</returns> |
115 | public static string FieldToString(byte[] bytes, string fieldName) | 115 | public static string FieldToString(byte[] bytes, string fieldName) |
116 | { | 116 | { |
117 | // Check for a common case | 117 | // Check for a common case |
118 | if (bytes.Length == 0) return String.Empty; | 118 | if (bytes.Length == 0) return String.Empty; |
119 | 119 | ||
120 | StringBuilder output = new StringBuilder(); | 120 | StringBuilder output = new StringBuilder(); |
121 | bool printable = true; | 121 | bool printable = true; |
122 | 122 | ||
123 | for (int i = 0; i < bytes.Length; ++i) | 123 | for (int i = 0; i < bytes.Length; ++i) |
124 | { | 124 | { |
125 | // Check if there are any unprintable characters in the array | 125 | // Check if there are any unprintable characters in the array |
126 | if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09 | 126 | if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09 |
127 | && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00) | 127 | && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00) |
128 | { | 128 | { |
129 | printable = false; | 129 | printable = false; |
130 | break; | 130 | break; |
131 | } | 131 | } |
132 | } | 132 | } |
133 | 133 | ||
134 | if (printable) | 134 | if (printable) |
135 | { | 135 | { |
136 | if (fieldName.Length > 0) | 136 | if (fieldName.Length > 0) |
137 | { | 137 | { |
138 | output.Append(fieldName); | 138 | output.Append(fieldName); |
139 | output.Append(": "); | 139 | output.Append(": "); |
140 | } | 140 | } |
141 | 141 | ||
142 | if (bytes[bytes.Length - 1] == 0x00) | 142 | if (bytes[bytes.Length - 1] == 0x00) |
143 | output.Append(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1)); | 143 | output.Append(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1)); |
144 | else | 144 | else |
145 | output.Append(UTF8Encoding.UTF8.GetString(bytes)); | 145 | output.Append(UTF8Encoding.UTF8.GetString(bytes)); |
146 | } | 146 | } |
147 | else | 147 | else |
148 | { | 148 | { |
149 | for (int i = 0; i < bytes.Length; i += 16) | 149 | for (int i = 0; i < bytes.Length; i += 16) |
150 | { | 150 | { |
151 | if (i != 0) | 151 | if (i != 0) |
152 | output.Append(Environment.NewLine); | 152 | output.Append(Environment.NewLine); |
153 | if (fieldName.Length > 0) | 153 | if (fieldName.Length > 0) |
154 | { | 154 | { |
155 | output.Append(fieldName); | 155 | output.Append(fieldName); |
156 | output.Append(": "); | 156 | output.Append(": "); |
157 | } | 157 | } |
158 | 158 | ||
159 | for (int j = 0; j < 16; j++) | 159 | for (int j = 0; j < 16; j++) |
160 | { | 160 | { |
161 | if ((i + j) < bytes.Length) | 161 | if ((i + j) < bytes.Length) |
162 | output.Append(String.Format("{0:X2} ", bytes[i + j])); | 162 | output.Append(String.Format("{0:X2} ", bytes[i + j])); |
163 | else | 163 | else |
164 | output.Append(" "); | 164 | output.Append(" "); |
165 | } | 165 | } |
166 | 166 | ||
167 | for (int j = 0; j < 16 && (i + j) < bytes.Length; j++) | 167 | for (int j = 0; j < 16 && (i + j) < bytes.Length; j++) |
168 | { | 168 | { |
169 | if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E) | 169 | if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E) |
170 | output.Append((char)bytes[i + j]); | 170 | output.Append((char)bytes[i + j]); |
171 | else | 171 | else |
172 | output.Append("."); | 172 | output.Append("."); |
173 | } | 173 | } |
174 | } | 174 | } |
175 | } | 175 | } |
176 | 176 | ||
177 | return output.ToString(); | 177 | return output.ToString(); |
178 | } | 178 | } |
179 | public Util() | 179 | public Util() |
180 | { | 180 | { |
181 | 181 | ||
182 | } | 182 | } |
183 | } | 183 | } |
184 | } | 184 | } |