diff options
Diffstat (limited to 'OpenSim')
-rw-r--r-- | OpenSim/Framework/Crc32.cs | 139 | ||||
-rw-r--r-- | OpenSim/Framework/MuteData.cs | 41 | ||||
-rw-r--r-- | OpenSim/Region/CoreModules/Avatar/InstantMessage/MuteListModuleTst.cs | 224 | ||||
-rw-r--r-- | OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs | 125 |
4 files changed, 409 insertions, 120 deletions
diff --git a/OpenSim/Framework/Crc32.cs b/OpenSim/Framework/Crc32.cs new file mode 100644 index 0000000..7ad1566 --- /dev/null +++ b/OpenSim/Framework/Crc32.cs | |||
@@ -0,0 +1,139 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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 | |||
28 | using System; | ||
29 | using System.Security.Cryptography; | ||
30 | |||
31 | namespace OpenSim.Framework | ||
32 | { | ||
33 | // this is more generic than openmetaverse CRC32 | ||
34 | |||
35 | public class Crc32 : HashAlgorithm | ||
36 | { | ||
37 | public const UInt32 DefaultPolynomial = 0xedb88320; | ||
38 | public const UInt32 DefaultSeed = 0xffffffff; | ||
39 | |||
40 | private UInt32 hash; | ||
41 | private UInt32 seed; | ||
42 | private UInt32[] table; | ||
43 | private static UInt32[] defaultTable; | ||
44 | |||
45 | public Crc32() | ||
46 | { | ||
47 | table = InitializeTable(DefaultPolynomial); | ||
48 | seed = DefaultSeed; | ||
49 | Initialize(); | ||
50 | } | ||
51 | |||
52 | public Crc32(UInt32 polynomial, UInt32 seed) | ||
53 | { | ||
54 | table = InitializeTable(polynomial); | ||
55 | this.seed = seed; | ||
56 | Initialize(); | ||
57 | } | ||
58 | |||
59 | public override void Initialize() | ||
60 | { | ||
61 | hash = seed; | ||
62 | } | ||
63 | |||
64 | protected override void HashCore(byte[] buffer, int start, int length) | ||
65 | { | ||
66 | hash = CalculateHash(table, hash, buffer, start, length); | ||
67 | } | ||
68 | |||
69 | protected override byte[] HashFinal() | ||
70 | { | ||
71 | byte[] hashBuffer = UInt32ToBigEndianBytes(~hash); | ||
72 | this.HashValue = hashBuffer; | ||
73 | return hashBuffer; | ||
74 | } | ||
75 | |||
76 | public override int HashSize | ||
77 | { | ||
78 | get { return 32; } | ||
79 | } | ||
80 | |||
81 | public static UInt32 Compute(byte[] buffer) | ||
82 | { | ||
83 | return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length); | ||
84 | } | ||
85 | |||
86 | public static UInt32 Compute(UInt32 seed, byte[] buffer) | ||
87 | { | ||
88 | return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length); | ||
89 | } | ||
90 | |||
91 | public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer) | ||
92 | { | ||
93 | return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length); | ||
94 | } | ||
95 | |||
96 | private static UInt32[] InitializeTable(UInt32 polynomial) | ||
97 | { | ||
98 | if (polynomial == DefaultPolynomial && defaultTable != null) | ||
99 | return defaultTable; | ||
100 | |||
101 | UInt32[] createTable = new UInt32[256]; | ||
102 | for (int i = 0; i < 256; i++) | ||
103 | { | ||
104 | UInt32 entry = (UInt32)i; | ||
105 | for (int j = 0; j < 8; j++) | ||
106 | if ((entry & 1) == 1) | ||
107 | entry = (entry >> 1) ^ polynomial; | ||
108 | else | ||
109 | entry = entry >> 1; | ||
110 | createTable[i] = entry; | ||
111 | } | ||
112 | |||
113 | if (polynomial == DefaultPolynomial) | ||
114 | defaultTable = createTable; | ||
115 | |||
116 | return createTable; | ||
117 | } | ||
118 | |||
119 | private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size) | ||
120 | { | ||
121 | UInt32 crc = seed; | ||
122 | for (int i = start; i < size; i++) | ||
123 | unchecked | ||
124 | { | ||
125 | crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff]; | ||
126 | } | ||
127 | return crc; | ||
128 | } | ||
129 | |||
130 | private byte[] UInt32ToBigEndianBytes(UInt32 x) | ||
131 | { | ||
132 | return new byte[] { | ||
133 | (byte)((x >> 24) & 0xff), | ||
134 | (byte)((x >> 16) & 0xff), | ||
135 | (byte)((x >> 8) & 0xff), | ||
136 | (byte)(x & 0xff) }; | ||
137 | } | ||
138 | } | ||
139 | } | ||
diff --git a/OpenSim/Framework/MuteData.cs b/OpenSim/Framework/MuteData.cs new file mode 100644 index 0000000..7c946d6 --- /dev/null +++ b/OpenSim/Framework/MuteData.cs | |||
@@ -0,0 +1,41 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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 | |||
28 | using OpenMetaverse; | ||
29 | |||
30 | namespace OpenSim.Framework | ||
31 | { | ||
32 | public class MuteData | ||
33 | { | ||
34 | public UUID AgentID; | ||
35 | public UUID MuteID; | ||
36 | public string MuteName; | ||
37 | public int MuteType; | ||
38 | public int MuteFlags; | ||
39 | public int Stamp; | ||
40 | } | ||
41 | } | ||
diff --git a/OpenSim/Region/CoreModules/Avatar/InstantMessage/MuteListModuleTst.cs b/OpenSim/Region/CoreModules/Avatar/InstantMessage/MuteListModuleTst.cs new file mode 100644 index 0000000..7ade511 --- /dev/null +++ b/OpenSim/Region/CoreModules/Avatar/InstantMessage/MuteListModuleTst.cs | |||
@@ -0,0 +1,224 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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 | using System; | ||
28 | using System.Collections.Generic; | ||
29 | using System.Reflection; | ||
30 | using System.Text; | ||
31 | using log4net; | ||
32 | using Nini.Config; | ||
33 | using OpenMetaverse; | ||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Framework.Servers; | ||
36 | using OpenSim.Framework.Client; | ||
37 | using OpenSim.Region.Framework.Interfaces; | ||
38 | using OpenSim.Region.Framework.Scenes; | ||
39 | using Mono.Addins; | ||
40 | using OpenSim.Data; | ||
41 | using OpenSim.Data.MySQL; | ||
42 | using MySql.Data.MySqlClient; | ||
43 | using System.Security.Cryptography; | ||
44 | |||
45 | namespace OpenSim.Region.CoreModules.Avatar.InstantMessage | ||
46 | { | ||
47 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "MuteListModuleTst")] | ||
48 | public class MuteModuleTst : ISharedRegionModule | ||
49 | { | ||
50 | private static readonly ILog m_log = LogManager.GetLogger( | ||
51 | MethodBase.GetCurrentMethod().DeclaringType); | ||
52 | |||
53 | protected bool m_Enabled = false; | ||
54 | protected List<Scene> m_SceneList = new List<Scene>(); | ||
55 | protected MuteTableHandler m_MuteTable; | ||
56 | protected string m_DatabaseConnect; | ||
57 | |||
58 | public void Initialise(IConfigSource config) | ||
59 | { | ||
60 | IConfig cnf = config.Configs["Messaging"]; | ||
61 | if (cnf == null) | ||
62 | return; | ||
63 | |||
64 | if (cnf.GetString("MuteListModule", "None") != "MuteListModuleTst") | ||
65 | return; | ||
66 | |||
67 | m_DatabaseConnect = cnf.GetString("MuteDatabaseConnect", String.Empty); | ||
68 | if (m_DatabaseConnect == String.Empty) | ||
69 | { | ||
70 | m_log.Debug("[MuteModuleTst]: MuteDatabaseConnect missing or empty"); | ||
71 | return; | ||
72 | } | ||
73 | |||
74 | try | ||
75 | { | ||
76 | m_MuteTable = new MuteTableHandler(m_DatabaseConnect, "XMute", String.Empty); | ||
77 | } | ||
78 | catch | ||
79 | { | ||
80 | m_log.Error("[MuteListModuleTst]: Failed to open/create database table"); | ||
81 | return; | ||
82 | } | ||
83 | |||
84 | m_Enabled = true; | ||
85 | } | ||
86 | |||
87 | public void AddRegion(Scene scene) | ||
88 | { | ||
89 | if (!m_Enabled) | ||
90 | return; | ||
91 | |||
92 | lock (m_SceneList) | ||
93 | { | ||
94 | m_SceneList.Add(scene); | ||
95 | |||
96 | scene.EventManager.OnNewClient += OnNewClient; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | public void RegionLoaded(Scene scene) | ||
101 | { | ||
102 | if (!m_Enabled) | ||
103 | return; | ||
104 | |||
105 | IXfer xfer = scene.RequestModuleInterface<IXfer>(); | ||
106 | if (xfer == null) | ||
107 | m_log.ErrorFormat("[MuteListModuleTst]: Xfer not availble in region {0}", scene.Name); | ||
108 | } | ||
109 | |||
110 | public void RemoveRegion(Scene scene) | ||
111 | { | ||
112 | if (!m_Enabled) | ||
113 | return; | ||
114 | |||
115 | lock (m_SceneList) | ||
116 | { | ||
117 | m_SceneList.Remove(scene); | ||
118 | } | ||
119 | } | ||
120 | |||
121 | public void PostInitialise() | ||
122 | { | ||
123 | if (!m_Enabled) | ||
124 | return; | ||
125 | |||
126 | m_log.Debug("[MuteListModuleTst]: Mute list enabled"); | ||
127 | } | ||
128 | |||
129 | public string Name | ||
130 | { | ||
131 | get { return "MuteListModuleTst"; } | ||
132 | } | ||
133 | |||
134 | public Type ReplaceableInterface | ||
135 | { | ||
136 | get { return null; } | ||
137 | } | ||
138 | |||
139 | public void Close() | ||
140 | { | ||
141 | } | ||
142 | |||
143 | private void OnNewClient(IClientAPI client) | ||
144 | { | ||
145 | client.OnMuteListRequest += OnMuteListRequest; | ||
146 | client.OnUpdateMuteListEntry += OnUpdateMuteListEntry; | ||
147 | client.OnRemoveMuteListEntry += OnRemoveMuteListEntry; | ||
148 | } | ||
149 | |||
150 | private void OnMuteListRequest(IClientAPI client, uint crc) | ||
151 | { | ||
152 | IXfer xfer = client.Scene.RequestModuleInterface<IXfer>(); | ||
153 | if (xfer == null) | ||
154 | { | ||
155 | if(crc == 0) | ||
156 | client.SendEmpytMuteList(); | ||
157 | else | ||
158 | client.SendUseCachedMuteList(); | ||
159 | return; | ||
160 | } | ||
161 | |||
162 | MuteData[] data = m_MuteTable.Get("AgentID", client.AgentId.ToString()); | ||
163 | if (data == null || data.Length == 0) | ||
164 | { | ||
165 | if(crc == 0) | ||
166 | client.SendEmpytMuteList(); | ||
167 | else | ||
168 | client.SendUseCachedMuteList(); | ||
169 | return; | ||
170 | } | ||
171 | |||
172 | StringBuilder sb = new StringBuilder(16384); | ||
173 | |||
174 | foreach (MuteData d in data) | ||
175 | sb.AppendFormat("{0} {1} {2}|{3}\n", | ||
176 | d.MuteType, | ||
177 | d.MuteID.ToString(), | ||
178 | d.MuteName, | ||
179 | d.MuteFlags); | ||
180 | |||
181 | Byte[] filedata = Util.UTF8.GetBytes(sb.ToString()); | ||
182 | |||
183 | uint dataCrc = Crc32.Compute(filedata); | ||
184 | |||
185 | if (dataCrc == crc) | ||
186 | { | ||
187 | if(crc == 0) | ||
188 | client.SendEmpytMuteList(); | ||
189 | else | ||
190 | client.SendUseCachedMuteList(); | ||
191 | return; | ||
192 | } | ||
193 | |||
194 | string filename = "mutes"+client.AgentId.ToString(); | ||
195 | xfer.AddNewFile(filename, filedata); | ||
196 | client.SendMuteListUpdate(filename); | ||
197 | } | ||
198 | |||
199 | private void OnUpdateMuteListEntry(IClientAPI client, UUID muteID, string muteName, int muteType, uint muteFlags) | ||
200 | { | ||
201 | MuteData mute = new MuteData(); | ||
202 | |||
203 | mute.AgentID = client.AgentId; | ||
204 | mute.MuteID = muteID; | ||
205 | mute.MuteName = muteName; | ||
206 | mute.MuteType = muteType; | ||
207 | mute.MuteFlags = (int)muteFlags; | ||
208 | mute.Stamp = Util.UnixTimeSinceEpoch(); | ||
209 | |||
210 | m_MuteTable.Store(mute); | ||
211 | } | ||
212 | |||
213 | private void OnRemoveMuteListEntry(IClientAPI client, UUID muteID, string muteName) | ||
214 | { | ||
215 | m_MuteTable.Delete(new string[] { "AgentID", | ||
216 | "MuteID", | ||
217 | "MuteName" }, | ||
218 | new string[] { client.AgentId.ToString(), | ||
219 | muteID.ToString(), | ||
220 | muteName }); | ||
221 | } | ||
222 | } | ||
223 | } | ||
224 | |||
diff --git a/OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs b/OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs index fb5239f..ac542c2 100644 --- a/OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs +++ b/OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs | |||
@@ -27,6 +27,7 @@ | |||
27 | using System; | 27 | using System; |
28 | using System.Collections.Generic; | 28 | using System.Collections.Generic; |
29 | using System.Reflection; | 29 | using System.Reflection; |
30 | using System.Text; | ||
30 | using log4net; | 31 | using log4net; |
31 | using Nini.Config; | 32 | using Nini.Config; |
32 | using OpenMetaverse; | 33 | using OpenMetaverse; |
@@ -43,16 +44,6 @@ using System.Security.Cryptography; | |||
43 | 44 | ||
44 | namespace OpenSim.Region.CoreModules.Avatar.InstantMessage | 45 | namespace OpenSim.Region.CoreModules.Avatar.InstantMessage |
45 | { | 46 | { |
46 | public class MuteData | ||
47 | { | ||
48 | public UUID AgentID; | ||
49 | public UUID MuteID; | ||
50 | public string MuteName; | ||
51 | public int MuteType; | ||
52 | public int MuteFlags; | ||
53 | public int Stamp; | ||
54 | } | ||
55 | |||
56 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XMute")] | 47 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XMute")] |
57 | public class XMuteModule : ISharedRegionModule | 48 | public class XMuteModule : ISharedRegionModule |
58 | { | 49 | { |
@@ -163,17 +154,16 @@ namespace OpenSim.Region.CoreModules.Avatar.InstantMessage | |||
163 | } | 154 | } |
164 | else | 155 | else |
165 | { | 156 | { |
166 | List<string> mutes = new List<string>(); | 157 | StringBuilder sb = new StringBuilder(1024); |
167 | 158 | ||
168 | foreach (MuteData d in data) | 159 | foreach (MuteData d in data) |
169 | mutes.Add(String.Format("{0} {1} {2}|{3}", | 160 | sb.AppendFormat("{0} {1} {2}|{3}\n", |
170 | d.MuteType, | 161 | d.MuteType, |
171 | d.MuteID.ToString(), | 162 | d.MuteID.ToString(), |
172 | d.MuteName, | 163 | d.MuteName, |
173 | d.MuteFlags)); | 164 | d.MuteFlags); |
174 | 165 | ||
175 | Byte[] filedata = Util.UTF8.GetBytes(String.Join("\n", | 166 | Byte[] filedata = Util.UTF8.GetBytes(sb.ToString()); |
176 | mutes.ToArray()) + "\n"); | ||
177 | 167 | ||
178 | uint dataCrc = Crc32.Compute(filedata); | 168 | uint dataCrc = Crc32.Compute(filedata); |
179 | 169 | ||
@@ -248,110 +238,5 @@ namespace OpenSim.Region.CoreModules.Avatar.InstantMessage | |||
248 | } | 238 | } |
249 | } | 239 | } |
250 | } | 240 | } |
251 | |||
252 | public class Crc32 : HashAlgorithm | ||
253 | { | ||
254 | public const UInt32 DefaultPolynomial = 0xedb88320; | ||
255 | public const UInt32 DefaultSeed = 0xffffffff; | ||
256 | |||
257 | private UInt32 hash; | ||
258 | private UInt32 seed; | ||
259 | private UInt32[] table; | ||
260 | private static UInt32[] defaultTable; | ||
261 | |||
262 | public Crc32() | ||
263 | { | ||
264 | table = InitializeTable(DefaultPolynomial); | ||
265 | seed = DefaultSeed; | ||
266 | Initialize(); | ||
267 | } | ||
268 | |||
269 | public Crc32(UInt32 polynomial, UInt32 seed) | ||
270 | { | ||
271 | table = InitializeTable(polynomial); | ||
272 | this.seed = seed; | ||
273 | Initialize(); | ||
274 | } | ||
275 | |||
276 | public override void Initialize() | ||
277 | { | ||
278 | hash = seed; | ||
279 | } | ||
280 | |||
281 | protected override void HashCore(byte[] buffer, int start, int length) | ||
282 | { | ||
283 | hash = CalculateHash(table, hash, buffer, start, length); | ||
284 | } | ||
285 | |||
286 | protected override byte[] HashFinal() | ||
287 | { | ||
288 | byte[] hashBuffer = UInt32ToBigEndianBytes(~hash); | ||
289 | this.HashValue = hashBuffer; | ||
290 | return hashBuffer; | ||
291 | } | ||
292 | |||
293 | public override int HashSize | ||
294 | { | ||
295 | get { return 32; } | ||
296 | } | ||
297 | |||
298 | public static UInt32 Compute(byte[] buffer) | ||
299 | { | ||
300 | return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length); | ||
301 | } | ||
302 | |||
303 | public static UInt32 Compute(UInt32 seed, byte[] buffer) | ||
304 | { | ||
305 | return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length); | ||
306 | } | ||
307 | |||
308 | public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer) | ||
309 | { | ||
310 | return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length); | ||
311 | } | ||
312 | |||
313 | private static UInt32[] InitializeTable(UInt32 polynomial) | ||
314 | { | ||
315 | if (polynomial == DefaultPolynomial && defaultTable != null) | ||
316 | return defaultTable; | ||
317 | |||
318 | UInt32[] createTable = new UInt32[256]; | ||
319 | for (int i = 0; i < 256; i++) | ||
320 | { | ||
321 | UInt32 entry = (UInt32)i; | ||
322 | for (int j = 0; j < 8; j++) | ||
323 | if ((entry & 1) == 1) | ||
324 | entry = (entry >> 1) ^ polynomial; | ||
325 | else | ||
326 | entry = entry >> 1; | ||
327 | createTable[i] = entry; | ||
328 | } | ||
329 | |||
330 | if (polynomial == DefaultPolynomial) | ||
331 | defaultTable = createTable; | ||
332 | |||
333 | return createTable; | ||
334 | } | ||
335 | |||
336 | private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size) | ||
337 | { | ||
338 | UInt32 crc = seed; | ||
339 | for (int i = start; i < size; i++) | ||
340 | unchecked | ||
341 | { | ||
342 | crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff]; | ||
343 | } | ||
344 | return crc; | ||
345 | } | ||
346 | |||
347 | private byte[] UInt32ToBigEndianBytes(UInt32 x) | ||
348 | { | ||
349 | return new byte[] { | ||
350 | (byte)((x >> 24) & 0xff), | ||
351 | (byte)((x >> 16) & 0xff), | ||
352 | (byte)((x >> 8) & 0xff), | ||
353 | (byte)(x & 0xff) }; | ||
354 | } | ||
355 | } | ||
356 | } | 241 | } |
357 | 242 | ||