|
| 1 | +/* |
| 2 | + * Copyright 2019 Volker Berlin (i-net software) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package java.util.zip; |
| 17 | + |
| 18 | +import de.inetsoftware.jwebassembly.api.annotation.Replace; |
| 19 | + |
| 20 | +/** |
| 21 | + * Replacement methods for the class java.util.zip.CRC32 |
| 22 | + * |
| 23 | + * @author Volker Berlin |
| 24 | + */ |
| 25 | +class ReplacementForCRC32 { |
| 26 | + |
| 27 | + /** |
| 28 | + * Replacement for CRC32.update(int, int) |
| 29 | + */ |
| 30 | + @Replace( "java/util/zip/CRC32.update(II)I" ) |
| 31 | + static int update( int crc, int b ) { |
| 32 | + return 0; // for Java compiler |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Replacement for CRC32.updateBytes(int, byte[], int, int) |
| 37 | + */ |
| 38 | + @Replace( "java/util/zip/CRC32.updateBytes(I[BII)I" ) |
| 39 | + static int updateBytes( int crc, byte[] data, int off, int len ) { |
| 40 | + int[] crcTable = cachedCrcTable; |
| 41 | + if( crcTable == null ) { |
| 42 | + crcTable = makeCRCTable(); |
| 43 | + } |
| 44 | + |
| 45 | + crc ^= -1; |
| 46 | + for( ; off < len; off++ ) { |
| 47 | + crc = (crc >>> 8) ^ crcTable[(crc ^ data[off]) & 0xFF]; |
| 48 | + } |
| 49 | + |
| 50 | + return crc ^ -1; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Create table for a byte-wise 32-bit CRC calculation on the polynomial: |
| 55 | + * x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. |
| 56 | + * |
| 57 | + * @return the table |
| 58 | + */ |
| 59 | + private static int[] makeCRCTable() { |
| 60 | + int[] crcTable = cachedCrcTable; |
| 61 | + if( crcTable != null ) { |
| 62 | + return crcTable; |
| 63 | + } |
| 64 | + crcTable = new int[256]; |
| 65 | + for( int n = 0; n < 256; n++ ) { |
| 66 | + int c = n; |
| 67 | + for( int k = 0; k < 8; k++ ) { |
| 68 | + c = ((c & 1) == 1 ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)); |
| 69 | + } |
| 70 | + crcTable[n] = c; |
| 71 | + } |
| 72 | + return cachedCrcTable = crcTable; |
| 73 | + } |
| 74 | + |
| 75 | + private static int[] cachedCrcTable; |
| 76 | +} |
0 commit comments