|
| 1 | +/* |
| 2 | + * Originally ported from: https://github.com/gityf/crc/blob/8045f50ba6e4193d4ee5d2539025fef26e613c9f/crc/crc64.c |
| 3 | + * |
| 4 | + * Copyright (c) 2012, Salvatore Sanfilippo <antirez at gmail dot com> |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * * Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * * Neither the name of Redis nor the names of its contributors may be used |
| 16 | + * to endorse or promote products derived from this software without |
| 17 | + * specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. */ |
| 30 | + |
| 31 | +using System; |
| 32 | +using System.Security.Cryptography; |
| 33 | + |
| 34 | +namespace Java.Interop.Tools.JavaCallableWrappers |
| 35 | +{ |
| 36 | + /// <summary> |
| 37 | + /// CRC64 variant: crc-64-jones 64-bit |
| 38 | + /// * Poly: 0xad93d23594c935a9 |
| 39 | + /// |
| 40 | + /// Changes beyond initial implementation: |
| 41 | + /// * Starting Value: ulong.MaxValue |
| 42 | + /// * XOR length in HashFinal() |
| 43 | + /// * Using spliced table for faster processing |
| 44 | + /// </summary> |
| 45 | + internal static partial class Crc64Helper |
| 46 | + { |
| 47 | + |
| 48 | + internal static byte [] Compute (byte [] array) |
| 49 | + { |
| 50 | + ulong crc = ulong.MaxValue; |
| 51 | + ulong length = 0; |
| 52 | + |
| 53 | + HashCore (array, 0, array.Length, ref crc, ref length); |
| 54 | + |
| 55 | + return BitConverter.GetBytes (crc ^ length); |
| 56 | + } |
| 57 | + |
| 58 | + internal static unsafe void HashCore (byte [] array, int ibStart, int cbSize, ref ulong crc, ref ulong length) |
| 59 | + { |
| 60 | + int len = cbSize; |
| 61 | + int idx = ibStart; |
| 62 | + |
| 63 | + fixed (ulong* tptr = table) { |
| 64 | + fixed (byte* aptr = array) { |
| 65 | + while (len >= 8) { |
| 66 | + crc ^= *((ulong*) (aptr + idx)); |
| 67 | + crc = |
| 68 | + tptr [7 * 256 + (crc & 0xff)] ^ |
| 69 | + tptr [6 * 256 + ((crc >> 8) & 0xff)] ^ |
| 70 | + tptr [5 * 256 + ((crc >> 16) & 0xff)] ^ |
| 71 | + tptr [4 * 256 + ((crc >> 24) & 0xff)] ^ |
| 72 | + tptr [3 * 256 + ((crc >> 32) & 0xff)] ^ |
| 73 | + tptr [2 * 256 + ((crc >> 40) & 0xff)] ^ |
| 74 | + tptr [1 * 256 + ((crc >> 48) & 0xff)] ^ |
| 75 | + tptr [0 * 256 + (crc >> 56)]; |
| 76 | + idx += 8; |
| 77 | + len -= 8; |
| 78 | + } |
| 79 | + |
| 80 | + while (len > 0) { |
| 81 | + crc = tptr [0 * 256 + ((crc ^ aptr [idx]) & 0xff)] ^ (crc >> 8); |
| 82 | + idx++; |
| 83 | + len--; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + length += (ulong) cbSize; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments