emul/mini/src/main/java/java/util/zip/CRC32.java
branchemul
changeset 611 9839e9a75bcf
parent 609 48ef38e9677e
child 640 693745d01b55
     1.1 --- a/emul/mini/src/main/java/java/util/zip/CRC32.java	Wed Jan 30 14:00:17 2013 +0100
     1.2 +++ b/emul/mini/src/main/java/java/util/zip/CRC32.java	Wed Jan 30 14:03:49 2013 +0100
     1.3 @@ -33,7 +33,7 @@
     1.4   */
     1.5  public
     1.6  class CRC32 implements Checksum {
     1.7 -    private int crc;
     1.8 +    private int crc = 0xFFFFFFFF;
     1.9  
    1.10      /**
    1.11       * Creates a new CRC32 object.
    1.12 @@ -49,7 +49,8 @@
    1.13       * @param b the byte to update the checksum with
    1.14       */
    1.15      public void update(int b) {
    1.16 -        crc = update(crc, b);
    1.17 +        byte[] arr = { (byte)b };
    1.18 +        update(arr);
    1.19      }
    1.20  
    1.21      /**
    1.22 @@ -88,6 +89,25 @@
    1.23          return (long)crc & 0xffffffffL;
    1.24      }
    1.25  
    1.26 -    private native static int update(int crc, int b);
    1.27 -    private native static int updateBytes(int crc, byte[] b, int off, int len);
    1.28 +    // XXX: taken from 
    1.29 +    // http://introcs.cs.princeton.edu/java/51data/CRC32.java.html
    1.30 +    private static int updateBytes(int crc, byte[] arr, int off, int len) {
    1.31 +        int poly = 0xEDB88320;   // reverse polynomial
    1.32 +
    1.33 +        while (len-- > 0) {
    1.34 +            byte b = arr[off++];
    1.35 +            int temp = (crc ^ b) & 0xff;
    1.36 +
    1.37 +            // read 8 bits one at a time
    1.38 +            for (int i = 0; i < 8; i++) {
    1.39 +                if ((temp & 1) == 1) {
    1.40 +                    temp = (temp >>> 1) ^ poly;
    1.41 +                } else {
    1.42 +                    temp = (temp >>> 1);
    1.43 +                }
    1.44 +            }
    1.45 +            crc = (crc >>> 8) ^ temp;
    1.46 +        }
    1.47 +        return crc ^ 0xffffffff;
    1.48 +    }
    1.49  }