diff options
author | onefang | 2019-05-19 21:24:15 +1000 |
---|---|---|
committer | onefang | 2019-05-19 21:24:15 +1000 |
commit | 5e4d6cab00cb29cd088ab7b62ab13aff103b64cb (patch) | |
tree | a9fbc62df9eb2d1d9ba2698d8552eae71eca20d8 /OpenSim/Framework/Crc32.cs | |
parent | Add a build script. (diff) | |
download | opensim-SC-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.zip opensim-SC-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.tar.gz opensim-SC-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.tar.bz2 opensim-SC-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.tar.xz |
Dump OpenSim 0.9.0.1 into it's own branch.
Diffstat (limited to 'OpenSim/Framework/Crc32.cs')
-rw-r--r-- | OpenSim/Framework/Crc32.cs | 139 |
1 files changed, 139 insertions, 0 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 | } | ||