aboutsummaryrefslogtreecommitdiffstats
path: root/java/com/jcraft/jzlib/CRC32.java
blob: 30457968f4f866b0676ef0b6fe6436297f12a8f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*-mode:java; c-basic-offset:2; -*- */
/*
Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright 
     notice, this list of conditions and the following disclaimer in 
     the documentation and/or other materials provided with the distribution.

  3. The names of the authors may not be used to endorse or promote products
     derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * This program is based on zlib-1.1.3, so all credit should go authors
 * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
 * and contributors of zlib.
 */

package com.jcraft.jzlib;

final public class CRC32 implements Checksum {

  /*
   *  The following logic has come from RFC1952.
   */
  private int v = 0;
  private static int[] crc_table = null;
  static {
    crc_table = new int[256];
    for (int n = 0; n < 256; n++) {
      int c = n;
      for (int k = 8;  --k >= 0; ) {
        if ((c & 1) != 0)
	  c = 0xedb88320 ^ (c >>> 1);
        else
          c = c >>> 1;
      }
      crc_table[n] = c;
    }
  }

  public void update (byte[] buf, int index, int len) {
    int c = ~v;
    while (--len >= 0)
      c = crc_table[(c^buf[index++])&0xff]^(c >>> 8);
    v = ~c;
  }

  public void reset(){
    v = 0;
  }

  public void reset(long vv){
    v = (int)(vv&0xffffffffL);
  }

  public long getValue(){
    return (long)(v&0xffffffffL);
  }

  // The following logic has come from zlib.1.2.
  private static final int GF2_DIM = 32;
  static long combine(long crc1, long crc2, long len2){
    long row;
    long[] even = new long[GF2_DIM];
    long[] odd = new long[GF2_DIM];

    // degenerate case (also disallow negative lengths)
    if (len2 <= 0)
      return crc1;

    // put operator for one zero bit in odd
    odd[0] = 0xedb88320L;          // CRC-32 polynomial
    row = 1;
    for (int n = 1; n < GF2_DIM; n++) {
        odd[n] = row;
        row <<= 1;
    }

    // put operator for two zero bits in even
    gf2_matrix_square(even, odd);

    // put operator for four zero bits in odd
    gf2_matrix_square(odd, even);

    // apply len2 zeros to crc1 (first square will put the operator for one
    // zero byte, eight zero bits, in even)
    do {
      // apply zeros operator for this bit of len2
      gf2_matrix_square(even, odd);
      if ((len2 & 1)!=0)
        crc1 = gf2_matrix_times(even, crc1);
      len2 >>= 1;

      // if no more bits set, then done
      if (len2 == 0)
        break;

      // another iteration of the loop with odd and even swapped
      gf2_matrix_square(odd, even);
      if ((len2 & 1)!=0)
        crc1 = gf2_matrix_times(odd, crc1);
      len2 >>= 1;

      // if no more bits set, then done
    } while (len2 != 0);

    /* return combined crc */
    crc1 ^= crc2;
    return crc1;
  }

  private static long gf2_matrix_times(long[] mat, long vec){
    long sum = 0;
    int index = 0;
    while (vec!=0) {
      if ((vec & 1)!=0)
        sum ^= mat[index];
      vec >>= 1;
      index++;
    }
    return sum;
  }

  static final void gf2_matrix_square(long[] square, long[] mat) {
    for (int n = 0; n < GF2_DIM; n++)
      square[n] = gf2_matrix_times(mat, mat[n]);
  }

  /*
  private java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();

  public void update(byte[] buf, int index, int len){
    if(buf==null) {crc32.reset();}
    else{crc32.update(buf, index, len);}
  }
  public void reset(){
    crc32.reset();
  }
  public void reset(long init){
    if(init==0L){
      crc32.reset();
    }
    else{
      System.err.println("unsupported operation");
    }
  }
  public long getValue(){
    return crc32.getValue();
  }
*/
  public CRC32 copy(){
    CRC32 foo = new CRC32();
    foo.v = this.v;
    return foo;
  }

  public static int[] getCRC32Table(){
    int[] tmp = new int[crc_table.length];
    System.arraycopy(crc_table, 0, tmp, 0, tmp.length);
    return tmp;
  }
}