1 28 29 package com.caucho.util; 30 31 34 public class Crc64 { 35 private static final long POLY64REV = 0xd800000000000000L; 36 private static final long []CRC_TABLE; 37 38 41 public static long generate(String value) 42 { 43 int len = value.length(); 44 long crc = 0; 45 46 for (int i = 0; i < len; i++) 47 crc = next(crc, value.charAt(i)); 48 49 return crc; 50 } 51 52 55 public static long generate(long crc, String value) 56 { 57 int len = value.length(); 58 59 for (int i = 0; i < len; i++) { 60 char ch = value.charAt(i); 61 62 if (ch > 0xff) 63 crc = next(crc, (ch >> 8)); 64 65 crc = next(crc, ch); 66 } 67 68 return crc; 69 } 70 71 74 public static long generate(long crc, char []buffer, int offset, int len) 75 { 76 for (int i = 0; i < len; i++) { 77 char ch = buffer[offset + i]; 78 79 if (ch > 0xff) 80 crc = next(crc, (ch >> 8)); 81 82 crc = next(crc, ch); 83 } 84 85 return crc; 86 } 87 88 91 public static long generate(long crc, byte ch) 92 { 93 return (crc >>> 8) ^ CRC_TABLE[((int) crc ^ ch) & 0xff]; 94 } 95 96 99 public static long next(long crc, int ch) 100 { 101 return (crc >>> 8) ^ CRC_TABLE[((int) crc ^ ch) & 0xff]; 102 } 103 104 static { 105 CRC_TABLE = new long[256]; 106 107 for (int i = 0; i < 256; i++) { 108 long v = i; 109 110 for (int j = 0; j < 8; j++) { 111 boolean flag = (v & 1) != 0; 112 113 long newV = v >>> 1; 114 115 if ((v & 0x100000000L) != 0) 116 newV |= 0x100000000L; 117 118 if ((v & 1) != 0) 119 newV ^= POLY64REV; 120 121 v = newV; 122 } 123 124 CRC_TABLE[i] = v; 125 } 126 } 127 } 128 | Popular Tags |